Skip to content

Commit 9b3c8f0

Browse files
author
Amit Kapila
committed
Doc: Update PL/pgSQL sample function in plpgsql.sgml.
The example used to explain 'Looping Through Query Results' uses pseudo-materialized views. Replace it with a more up-to-date example which does the same thing with actual materialized views, which have been available since PostgreSQL 9.3. In the passing, change '%' as format specifier instead of '%s' as is used in other examples in plpgsql.sgml. Reported-by: Ian Barwick Author: Ian Barwick Reviewed-by: Amit Kapila Backpatch-through: 9.4 Discussion: https://postgr.es/m/9a70d393-7904-4918-c97c-649f6d114b6a@2ndquadrant.com
1 parent ddbd5d8 commit 9b3c8f0

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

doc/src/sgml/plpgsql.sgml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,19 +2437,29 @@ END LOOP <optional> <replaceable>label</replaceable> </optional>;
24372437
resulting from the <replaceable>query</replaceable> and the loop body is
24382438
executed for each row. Here is an example:
24392439
<programlisting>
2440-
CREATE FUNCTION cs_refresh_mviews() RETURNS integer AS $$
2440+
CREATE FUNCTION refresh_mviews() RETURNS integer AS $$
24412441
DECLARE
24422442
mviews RECORD;
24432443
BEGIN
2444-
RAISE NOTICE 'Refreshing materialized views...';
2445-
2446-
FOR mviews IN SELECT * FROM cs_materialized_views ORDER BY sort_key LOOP
2444+
RAISE NOTICE 'Refreshing all materialized views...';
2445+
2446+
FOR mviews IN
2447+
SELECT n.nspname AS mv_schema,
2448+
c.relname AS mv_name,
2449+
pg_catalog.pg_get_userbyid(c.relowner) AS owner
2450+
FROM pg_catalog.pg_class c
2451+
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
2452+
WHERE c.relkind = 'm'
2453+
ORDER BY 1
2454+
LOOP
24472455

2448-
-- Now "mviews" has one record from cs_materialized_views
2456+
-- Now "mviews" has one record with information about the materialized view
24492457

2450-
RAISE NOTICE 'Refreshing materialized view %s ...', quote_ident(mviews.mv_name);
2451-
EXECUTE format('TRUNCATE TABLE %I', mviews.mv_name);
2452-
EXECUTE format('INSERT INTO %I %s', mviews.mv_name, mviews.mv_query);
2458+
RAISE NOTICE 'Refreshing materialized view %.% (owner: %)...',
2459+
quote_ident(mviews.mv_schema),
2460+
quote_ident(mviews.mv_name),
2461+
quote_ident(mviews.owner);
2462+
EXECUTE format('REFRESH MATERIALIZED VIEW %I.%I', mviews.mv_schema, mviews.mv_name);
24532463
END LOOP;
24542464

24552465
RAISE NOTICE 'Done refreshing materialized views.';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy