From: Fujii Masao <masao.fujii@oss.nttdata.com>
To: legrand legrand <legrand_legrand@hotmail.com>
To: pgsql-bugs@postgresql.org
Subject: Re: pg_stat_statements: rows not updated for CREATE TABLE AS SELECT statements
Date: Tue, 21 Apr 2020 00:42:48 +0900
Message-ID: <54fa2fec-b133-0fc0-4227-3e308ba68812@oss.nttdata.com> (raw)
In-Reply-To: <1585330995153-0.post@n3.nabble.com>
References: <1584293755198-0.post@n3.nabble.com>
<c9463107-d605-9d9e-54e5-f065f2fc3c1f@oss.nttdata.com>
<1585330995153-0.post@n3.nabble.com>
On 2020/03/28 2:43, legrand legrand wrote:
> Thank you for those answers !
>
>> The utility commands that return CMDTAG_SELECT are
>> only CREATE TABLE AS SELECT and CREATE MATERIALIZED VIEW?
>> I'd just like to confirm that there is no case where "rows" must not
>> be counted when CMDTAG_SELECT is returned.
>
> I don't have any in mind ...
I found that SELECT INTO also returns CMDTAG_SELECT.
>
>> BTW, "rows" should be updated when FETCH or MOVE is executed
>> because each command returns or affects the rows?
>
> Yes they should, but they aren't yet (event with CMDTAG_SELECT added)
>
> Note that implicit cursors behave the same way ;o(
Thanks for confirming this!
Attached is the patch that makes pgss track the total number of rows
retrieved or affected by CREATE TABLE AS, SELECT INTO,
CREATE MATERIALIZED VIEW and FETCH. I think this is new feature
rather than bug fix, so am planning to add this patch into next CommitFest
for v14. Thought?
Regards,
--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION
Attachments:pgss_track_rows_of_utility_v1.patch (text/plain, 5.7K) downloaddiff --git a/contrib/pg_stat_statements/expected/pg_stat_statements.out b/contrib/pg_stat_statements/expected/pg_stat_statements.outindex f615f8c2bf..5e5ee5e899 100644--- a/contrib/pg_stat_statements/expected/pg_stat_statements.out+++ b/contrib/pg_stat_statements/expected/pg_stat_statements.out@@ -527,6 +527,69 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 0 | 0
(9 rows)
+--+-- 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+--+SELECT pg_stat_statements_reset();+ pg_stat_statements_reset +--------------------------+ +(1 row)++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;+BEGIN;+DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;+FETCH NEXT pgss_cursor;+ a | b +---+------+ 1 | ctas+(1 row)++FETCH FORWARD 5 pgss_cursor;+ a | b +---+------+ 2 | ctas+ 3 | ctas+ 4 | ctas+ 5 | ctas+ 6 | ctas+(5 rows)++FETCH FORWARD ALL pgss_cursor;+ a | b +----+------+ 7 | ctas+ 8 | ctas+ 9 | ctas+ 10 | ctas+ 11 | copy+ 12 | copy+ 13 | copy+(7 rows)++COMMIT;+SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";+ query | plans | calls | rows +-------------------------------------------------------------------------------------+-------+-------+------+ BEGIN | 0 | 1 | 0+ COMMIT | 0 | 1 | 0+ COPY pgss_ctas (a, b) FROM STDIN | 0 | 1 | 3+ CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas | 0 | 1 | 13+ CREATE TABLE pgss_ctas AS SELECT a, 'ctas' b FROM generate_series(1, 10) a | 0 | 1 | 10+ DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv | 0 | 1 | 0+ FETCH FORWARD 5 pgss_cursor | 0 | 1 | 5+ FETCH FORWARD ALL pgss_cursor | 0 | 1 | 7+ FETCH NEXT pgss_cursor | 0 | 1 | 1+ 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)+
--
-- Track user activity and reset them
--
@@ -727,6 +790,9 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
--
DROP ROLE regress_stats_user1;
DROP ROLE regress_stats_user2;
+DROP MATERIALIZED VIEW pgss_matv;+DROP TABLE pgss_ctas;+DROP TABLE pgss_select_into;
--
-- [re]plan counting
--
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.cindex 90bc6fd013..9c1689fd96 100644--- a/contrib/pg_stat_statements/pg_stat_statements.c+++ b/contrib/pg_stat_statements/pg_stat_statements.c@@ -1170,7 +1170,15 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
- rows = (qc && qc->commandTag == CMDTAG_COPY) ? qc->nprocessed : 0;+ /*+ * 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.+ */+ rows = (qc && (qc->commandTag == CMDTAG_COPY ||+ qc->commandTag == CMDTAG_FETCH ||+ qc->commandTag == CMDTAG_SELECT)) ?+ qc->nprocessed : 0;
/* calc differences of buffer counters. */
memset(&bufusage, 0, sizeof(BufferUsage));
diff --git a/contrib/pg_stat_statements/sql/pg_stat_statements.sql b/contrib/pg_stat_statements/sql/pg_stat_statements.sqlindex 75c10554a8..3cbaf45c9d 100644--- a/contrib/pg_stat_statements/sql/pg_stat_statements.sql+++ b/contrib/pg_stat_statements/sql/pg_stat_statements.sql@@ -249,6 +249,30 @@ DROP FUNCTION PLUS_TWO(INTEGER);
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+--+SELECT pg_stat_statements_reset();++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;+11 copy+12 copy+13 copy+\.+CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas;+BEGIN;+DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv;+FETCH NEXT pgss_cursor;+FETCH FORWARD 5 pgss_cursor;+FETCH FORWARD ALL pgss_cursor;+COMMIT;++SELECT query, plans, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";+
--
-- Track user activity and reset them
--
@@ -312,6 +336,9 @@ SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
--
DROP ROLE regress_stats_user1;
DROP ROLE regress_stats_user2;
+DROP MATERIALIZED VIEW pgss_matv;+DROP TABLE pgss_ctas;+DROP TABLE pgss_select_into;
--
-- [re]plan counting
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: masao.fujii@oss.nttdata.com, legrand_legrand@hotmail.com
Subject: Re: pg_stat_statements: rows not updated for CREATE TABLE AS SELECT statements
In-Reply-To: <54fa2fec-b133-0fc0-4227-3e308ba68812@oss.nttdata.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox