pg_stat_statements: track number of rows processed by REFRESH MATERIALIZED VIEW.

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
This commit is contained in:
Fujii Masao 2020-11-12 11:26:55 +09:00
parent 03f9cd93ea
commit b62e6056a0
4 changed files with 24 additions and 9 deletions

View 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 -- Track the total number of rows retrieved or affected by the utility
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW -- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
-- and SELECT INTO -- REFRESH MATERIALIZED VIEW and SELECT INTO
-- --
SELECT pg_stat_statements_reset(); SELECT pg_stat_statements_reset();
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; SELECT generate_series(1, 10) c INTO pgss_select_into;
COPY pgss_ctas (a, b) FROM STDIN; COPY pgss_ctas (a, b) FROM STDIN;
CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas; CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
REFRESH MATERIALIZED VIEW pgss_matv;
BEGIN; BEGIN;
DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv; DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
FETCH NEXT pgss_cursor; 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 5 pgss_cursor | 0 | 1 | 5
FETCH FORWARD ALL pgss_cursor | 0 | 1 | 7 FETCH FORWARD ALL pgss_cursor | 0 | 1 | 7
FETCH NEXT pgss_cursor | 0 | 1 | 1 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 generate_series(1, 10) c INTO pgss_select_into | 0 | 1 | 10
SELECT pg_stat_statements_reset() | 0 | 1 | 1 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 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 -- Track user activity and reset them

View File

@ -1171,13 +1171,14 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
INSTR_TIME_SUBTRACT(duration, start); INSTR_TIME_SUBTRACT(duration, start);
/* /*
* Track the total number of rows retrieved or affected by * Track the total number of rows retrieved or affected by the utility
* the utility statements of COPY, FETCH, CREATE TABLE AS, * statements of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED
* CREATE MATERIALIZED VIEW and SELECT INTO. * VIEW, REFRESH MATERIALIZED VIEW and SELECT INTO.
*/ */
rows = (qc && (qc->commandTag == CMDTAG_COPY || rows = (qc && (qc->commandTag == CMDTAG_COPY ||
qc->commandTag == CMDTAG_FETCH || qc->commandTag == CMDTAG_FETCH ||
qc->commandTag == CMDTAG_SELECT)) ? qc->commandTag == CMDTAG_SELECT ||
qc->commandTag == CMDTAG_REFRESH_MATERIALIZED_VIEW)) ?
qc->nprocessed : 0; qc->nprocessed : 0;
/* calc differences of buffer counters. */ /* calc differences of buffer counters. */

View 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 -- Track the total number of rows retrieved or affected by the utility
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW -- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
-- and SELECT INTO -- REFRESH MATERIALIZED VIEW and SELECT INTO
-- --
SELECT pg_stat_statements_reset(); SELECT pg_stat_statements_reset();
@ -265,6 +265,7 @@ COPY pgss_ctas (a, b) FROM STDIN;
13 copy 13 copy
\. \.
CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas; CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
REFRESH MATERIALIZED VIEW pgss_matv;
BEGIN; BEGIN;
DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv; DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
FETCH NEXT pgss_cursor; FETCH NEXT pgss_cursor;

View File

@ -356,6 +356,17 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
ObjectAddressSet(address, RelationRelationId, matviewOid); 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; return address;
} }