pg_stat_statements: track number of rows processed by REFRESH MATERIALIZED VIEW.
authorFujii Masao <fujii@postgresql.org>
Thu, 12 Nov 2020 02:26:55 +0000 (11:26 +0900)
committerFujii Masao <fujii@postgresql.org>
Thu, 12 Nov 2020 02:26:55 +0000 (11:26 +0900)
Commit 6023b7ea71 allowed pg_stat_statements to track the number
of rows retrieved or affected by some utility commands including
CREATE MATERIALIZED VIEW. However it did not track the rowcount
of REFRESH MATERIALIZED VIEW. This commit allows pg_stat_statements
to track that.

To track that, this commit changes the query completion for
REFRESH MATERIALIZED VIEW so that it saves the rowcount. But note that
the rowcount is still not displayed in the command completion tag output.
That is, the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
command tag is left false in cmdtaglist.h. Otherwise, the change of
completion tag output might break applications using it.

Author: Katsuragi Yuta, Seino Yuki
Reviewed-by: Fujii Masao
Discussion: https://postgr.es/m/71f6bc72f8bbaa06e701f8bd2562c347@oss.nttdata.com
Discussion: https://postgr.es/m/aadbfba9-e4bb-9531-6b3a-d13c31c8f4fe@oss.nttdata.com

contrib/pg_stat_statements/expected/pg_stat_statements.out
contrib/pg_stat_statements/pg_stat_statements.c
contrib/pg_stat_statements/sql/pg_stat_statements.sql
src/backend/commands/matview.c

index e0edb134f3dca3e1309c27c993cb18a919e0880b..2a303a7f0736eb8cd2b1a7e10c5b43c868709020 100644 (file)
@@ -530,8 +530,8 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
 
 --
 -- Track the total number of rows retrieved or affected by the utility
--- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW
--- and SELECT INTO
+-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
+-- REFRESH MATERIALIZED VIEW and SELECT INTO
 --
 SELECT pg_stat_statements_reset();
  pg_stat_statements_reset 
@@ -543,6 +543,7 @@ CREATE TABLE pgss_ctas AS SELECT a, 'ctas' b FROM generate_series(1, 10) a;
 SELECT generate_series(1, 10) c INTO pgss_select_into;
 COPY pgss_ctas (a, b) FROM STDIN;
 CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
+REFRESH MATERIALIZED VIEW pgss_matv;
 BEGIN;
 DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
 FETCH NEXT pgss_cursor;
@@ -586,10 +587,11 @@ SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE
  FETCH FORWARD 5 pgss_cursor                                                         |     0 |     1 |    5
  FETCH FORWARD ALL pgss_cursor                                                       |     0 |     1 |    7
  FETCH NEXT pgss_cursor                                                              |     0 |     1 |    1
+ REFRESH MATERIALIZED VIEW pgss_matv                                                 |     0 |     1 |   13
  SELECT generate_series(1, 10) c INTO pgss_select_into                               |     0 |     1 |   10
  SELECT pg_stat_statements_reset()                                                   |     0 |     1 |    1
  SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" |     1 |     0 |    0
-(12 rows)
+(13 rows)
 
 --
 -- Track user activity and reset them
index 1eac9edaee729ee6cca769221d6e85a93adf5475..dd963c4644a1afbd9f25027c30cb55ea2f0c9e5b 100644 (file)
@@ -1171,13 +1171,14 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
        INSTR_TIME_SUBTRACT(duration, start);
 
        /*
-        * Track the total number of rows retrieved or affected by
-        * the utility statements of COPY, FETCH, CREATE TABLE AS,
-        * CREATE MATERIALIZED VIEW and SELECT INTO.
+        * Track the total number of rows retrieved or affected by the utility
+        * statements of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED
+        * VIEW, REFRESH MATERIALIZED VIEW and SELECT INTO.
         */
        rows = (qc && (qc->commandTag == CMDTAG_COPY ||
                       qc->commandTag == CMDTAG_FETCH ||
-                      qc->commandTag == CMDTAG_SELECT)) ?
+                      qc->commandTag == CMDTAG_SELECT ||
+                      qc->commandTag == CMDTAG_REFRESH_MATERIALIZED_VIEW)) ?
            qc->nprocessed : 0;
 
        /* calc differences of buffer counters. */
index 996a24a293c5b653f2aaf721610ded7eaacdc994..e9f5bb84e38ec47df638cdb6b4d9171b0e57db61 100644 (file)
@@ -252,8 +252,8 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
 
 --
 -- Track the total number of rows retrieved or affected by the utility
--- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW
--- and SELECT INTO
+-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
+-- REFRESH MATERIALIZED VIEW and SELECT INTO
 --
 SELECT pg_stat_statements_reset();
 
@@ -265,6 +265,7 @@ COPY pgss_ctas (a, b) FROM STDIN;
 13 copy
 \.
 CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
+REFRESH MATERIALIZED VIEW pgss_matv;
 BEGIN;
 DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
 FETCH NEXT pgss_cursor;
index f80a9e96a91cf8307229d6cf519945d4adcf4e86..cfc63915f38cde5c13c30758e77d892ed236b53c 100644 (file)
@@ -356,6 +356,17 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
 
    ObjectAddressSet(address, RelationRelationId, matviewOid);
 
+   /*
+    * Save the rowcount so that pg_stat_statements can track the total number
+    * of rows processed by REFRESH MATERIALIZED VIEW command. Note that we
+    * still don't display the rowcount in the command completion tag output,
+    * i.e., the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
+    * command tag is left false in cmdtaglist.h. Otherwise, the change of
+    * completion tag output might break applications using it.
+    */
+   if (qc)
+       SetQueryCompletion(qc, CMDTAG_REFRESH_MATERIALIZED_VIEW, processed);
+
    return address;
 }