diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index e442d2aaea2..0453c78ee03 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -2386,21 +2386,29 @@ END LOOP label ; resulting from the query and the loop body is executed for each row. Here is an example: -CREATE FUNCTION cs_refresh_mviews() RETURNS integer AS $$ +CREATE FUNCTION refresh_mviews() RETURNS integer AS $$ DECLARE mviews RECORD; BEGIN - RAISE NOTICE 'Refreshing materialized views...'; + RAISE NOTICE 'Refreshing all materialized views...'; + FOR mviews IN + SELECT n.nspname AS mv_schema, + c.relname AS mv_name, + pg_catalog.pg_get_userbyid(c.relowner) AS owner + FROM pg_catalog.pg_class c + LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) + WHERE c.relkind = 'm' + ORDER BY 1 + LOOP - FOR mviews IN SELECT * FROM cs_materialized_views ORDER BY sort_key LOOP + -- Now "mviews" has one record with information about the materialized view - -- Now "mviews" has one record from cs_materialized_views - - RAISE NOTICE 'Refreshing materialized view %s ...', quote_ident(mviews.mv_name); - EXECUTE 'TRUNCATE TABLE ' || quote_ident(mviews.mv_name); - EXECUTE 'INSERT INTO ' - || quote_ident(mviews.mv_name) || ' ' - || mviews.mv_query; + RAISE NOTICE 'Refreshing materialized view %.% (owner: %)...', + quote_ident(mviews.mv_schema), + quote_ident(mviews.mv_name), + quote_ident(mviews.owner); + EXECUTE 'REFRESH MATERIALIZED VIEW ' || quote_ident(mviews.mv_schema) + || '.' || quote_ident(mviews.mv_name); END LOOP; RAISE NOTICE 'Done refreshing materialized views.';