Skip to content

Commit b62e605

Browse files
committed
pg_stat_statements: track number of rows processed by REFRESH MATERIALIZED VIEW.
Commit 6023b7e 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
1 parent 03f9cd9 commit b62e605

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

contrib/pg_stat_statements/expected/pg_stat_statements.out

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
530530

531531
--
532532
-- Track the total number of rows retrieved or affected by the utility
533-
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW
534-
-- and SELECT INTO
533+
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
534+
-- REFRESH MATERIALIZED VIEW and SELECT INTO
535535
--
536536
SELECT pg_stat_statements_reset();
537537
pg_stat_statements_reset
@@ -543,6 +543,7 @@ CREATE TABLE pgss_ctas AS SELECT a, 'ctas' b FROM generate_series(1, 10) a;
543543
SELECT generate_series(1, 10) c INTO pgss_select_into;
544544
COPY pgss_ctas (a, b) FROM STDIN;
545545
CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
546+
REFRESH MATERIALIZED VIEW pgss_matv;
546547
BEGIN;
547548
DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
548549
FETCH NEXT pgss_cursor;
@@ -586,10 +587,11 @@ SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE
586587
FETCH FORWARD 5 pgss_cursor | 0 | 1 | 5
587588
FETCH FORWARD ALL pgss_cursor | 0 | 1 | 7
588589
FETCH NEXT pgss_cursor | 0 | 1 | 1
590+
REFRESH MATERIALIZED VIEW pgss_matv | 0 | 1 | 13
589591
SELECT generate_series(1, 10) c INTO pgss_select_into | 0 | 1 | 10
590592
SELECT pg_stat_statements_reset() | 0 | 1 | 1
591593
SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 1 | 0 | 0
592-
(12 rows)
594+
(13 rows)
593595

594596
--
595597
-- Track user activity and reset them

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,13 +1171,14 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11711171
INSTR_TIME_SUBTRACT(duration, start);
11721172

11731173
/*
1174-
* Track the total number of rows retrieved or affected by
1175-
* the utility statements of COPY, FETCH, CREATE TABLE AS,
1176-
* CREATE MATERIALIZED VIEW and SELECT INTO.
1174+
* Track the total number of rows retrieved or affected by the utility
1175+
* statements of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED
1176+
* VIEW, REFRESH MATERIALIZED VIEW and SELECT INTO.
11771177
*/
11781178
rows = (qc && (qc->commandTag == CMDTAG_COPY ||
11791179
qc->commandTag == CMDTAG_FETCH ||
1180-
qc->commandTag == CMDTAG_SELECT)) ?
1180+
qc->commandTag == CMDTAG_SELECT ||
1181+
qc->commandTag == CMDTAG_REFRESH_MATERIALIZED_VIEW)) ?
11811182
qc->nprocessed : 0;
11821183

11831184
/* calc differences of buffer counters. */

contrib/pg_stat_statements/sql/pg_stat_statements.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
252252

253253
--
254254
-- Track the total number of rows retrieved or affected by the utility
255-
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW
256-
-- and SELECT INTO
255+
-- commands of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED VIEW,
256+
-- REFRESH MATERIALIZED VIEW and SELECT INTO
257257
--
258258
SELECT pg_stat_statements_reset();
259259

@@ -265,6 +265,7 @@ COPY pgss_ctas (a, b) FROM STDIN;
265265
13 copy
266266
\.
267267
CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;
268+
REFRESH MATERIALIZED VIEW pgss_matv;
268269
BEGIN;
269270
DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;
270271
FETCH NEXT pgss_cursor;

src/backend/commands/matview.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
356356

357357
ObjectAddressSet(address, RelationRelationId, matviewOid);
358358

359+
/*
360+
* Save the rowcount so that pg_stat_statements can track the total number
361+
* of rows processed by REFRESH MATERIALIZED VIEW command. Note that we
362+
* still don't display the rowcount in the command completion tag output,
363+
* i.e., the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
364+
* command tag is left false in cmdtaglist.h. Otherwise, the change of
365+
* completion tag output might break applications using it.
366+
*/
367+
if (qc)
368+
SetQueryCompletion(qc, CMDTAG_REFRESH_MATERIALIZED_VIEW, processed);
369+
359370
return address;
360371
}
361372

0 commit comments

Comments
 (0)