0001-94-Doc-Update-PL-pgSQL-sample-function-in-plpgsql.sgml.patch
application/octet-stream
Filename: 0001-94-Doc-Update-PL-pgSQL-sample-function-in-plpgsql.sgml.patch
Type: application/octet-stream
Part: 1
Patch
Format: format-patch
Series: patch 0001
Subject: Doc: Update PL/pgSQL sample function in plpgsql.sgml.
| File | + | − |
|---|---|---|
| doc/src/sgml/plpgsql.sgml | 18 | 10 |
From d533c8db27a3c1eb91d6fe473fc2be5d876a701e Mon Sep 17 00:00:00 2001
From: Amit Kapila <akapila@postgresql.org>
Date: Wed, 11 Sep 2019 09:55:38 +0530
Subject: [PATCH] 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
---
doc/src/sgml/plpgsql.sgml | 28 ++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)
diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml
index e442d2a..0453c78 100644
--- a/doc/src/sgml/plpgsql.sgml
+++ b/doc/src/sgml/plpgsql.sgml
@@ -2386,21 +2386,29 @@ END LOOP <optional> <replaceable>label</replaceable> </optional>;
resulting from the <replaceable>query</replaceable> and the loop body is
executed for each row. Here is an example:
<programlisting>
-CREATE FUNCTION cs_refresh_mviews() RETURNS integer AS $$
+CREATE FUNCTION refresh_mviews() RETURNS integer AS $$
DECLARE
mviews RECORD;
BEGIN
- RAISE NOTICE 'Refreshing materialized views...';
-
- FOR mviews IN SELECT * FROM cs_materialized_views ORDER BY sort_key LOOP
+ 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
- -- Now "mviews" has one record from cs_materialized_views
+ -- Now "mviews" has one record with information about the materialized view
- 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.';
--
1.8.3.1