我想使用一个函数每天按计划调用来实现多个视图。它看起来像这样:
CREATE OR REPLACE FUNCTION refresh_my_views()
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
-- Array of view names in the order they should be refreshed
views text[] := ARRAY[
'view1',
'view2',
'view3'
-- ... add more view names here as needed
];
view_name text;
start_time timestamp;
end_time timestamp;
BEGIN
-- Iterate over each view name in the array
FOREACH view_name IN ARRAY views LOOP
-- Log the start of the refresh operation
RAISE NOTICE 'Starting refresh of %', view_name;
-- Start the timer
start_time := current_timestamp;
-- Refresh the materialized view
EXECUTE format('REFRESH MATERIALIZED VIEW CONCURRENTLY %s', view_name);
-- Stop the timer
end_time := current_timestamp;
-- Log the completion of the refresh operation and the time taken
RAISE NOTICE 'Completed refresh of % (Time: %)',
view_name, age(end_time, start_time);
END LOOP;
END;
$function$;
但是这个函数需要超过 12 个小时才能运行,似乎过了一段时间我就无法通过它的 PID 来事件
pg_terminate_backend
,并且我无法运行另一个命令来并行刷新任何这些视图。创建视图的速度很快,即使刷新最长的视图也不会超过 15 分钟。
我也没有看到 RAISE NOTICE
声明的消息。
应该只打印时间,最多需要30分钟左右就可以完成。
检查您的系统是否过载,Postgresql 是为在您的计算机外部运行而构建的,并将其导入到您的命令行中。确保检查 Postgresql 日志并读取输出。 像“iostat”这样的工具可以帮助查明问题。 还要检查系统日志。
我也有同样的问题。单独刷新物化视图(在函数外部)可以在 10 秒内完成,但在函数内部它们永远不会完成或只需要很多个小时。有趣的是,时间甚至随着时间的推移而增加,我可以通过日志表看到这一点。 而且,就像您的情况一样,即使是通知也不会被执行。 我有一个具有相同数据的暂存系统,它运行得很好。
这确实是一个奇怪的问题,我将不胜感激任何提示。