Reflect normalization of query strings for utilities in pg_stat_statements

Applying normalization changes how the following query strings are
reflected in pg_stat_statements, by showing Const nodes with a
dollar-signed parameter as this is how such queries are structured
internally once parsed:
- DECLARE
- EXPLAIN
- CREATE MATERIALIZED VIEW
- CREATE TABLE AS

More normalization could be done in the future depending on the parts
where query jumbling is applied (like A_Const nodes?), the changes being
reflected in the regression tests in majority created in de2aca2.  This
just allows the basics to work for utility queries using Const nodes.

Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/Y+MRdEq9W9XVa2AB@paquier.xyz
This commit is contained in:
Michael Paquier 2023-03-08 15:00:50 +09:00
parent be504a3e97
commit daa8365a90
3 changed files with 29 additions and 27 deletions

View File

@ -16,10 +16,10 @@ CLOSE cursor_stats_1;
DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2;
CLOSE cursor_stats_1;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+------------------------------------------------------
calls | rows | query
-------+------+-------------------------------------------------------
2 | 0 | CLOSE cursor_stats_1
2 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 1
2 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT $1
1 | 1 | SELECT pg_stat_statements_reset()
(3 rows)
@ -49,14 +49,14 @@ CLOSE cursor_stats_1;
CLOSE cursor_stats_2;
COMMIT;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+------------------------------------------------------
calls | rows | query
-------+------+-------------------------------------------------------
1 | 0 | BEGIN
1 | 0 | CLOSE cursor_stats_1
1 | 0 | CLOSE cursor_stats_2
1 | 0 | COMMIT
1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2
1 | 0 | DECLARE cursor_stats_2 CURSOR WITH HOLD FOR SELECT 3
1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT $1
1 | 0 | DECLARE cursor_stats_2 CURSOR WITH HOLD FOR SELECT $1
1 | 1 | FETCH 1 IN cursor_stats_1
1 | 1 | FETCH 1 IN cursor_stats_2
1 | 1 | SELECT pg_stat_statements_reset()

View File

@ -226,10 +226,10 @@ EXPLAIN (costs off) SELECT a FROM generate_series(1,10) AS tab(a) WHERE a = 7;
(2 rows)
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+-------------------------------------------------------------------------------
2 | 0 | EXPLAIN (costs off) SELECT 1
2 | 0 | EXPLAIN (costs off) SELECT a FROM generate_series(1,10) AS tab(a) WHERE a = 3
calls | rows | query
-------+------+---------------------------------------------------------------------------------
2 | 0 | EXPLAIN (costs off) SELECT $1
2 | 0 | EXPLAIN (costs off) SELECT a FROM generate_series($1,$2) AS tab(a) WHERE a = $3
1 | 1 | SELECT pg_stat_statements_reset()
(3 rows)
@ -326,12 +326,12 @@ CREATE TABLE ctas_stats_2 AS
FROM generate_series(1, 5) AS tab(a) WHERE a < 4 AND a > 1;
DROP TABLE ctas_stats_2;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+-----------------------------------------------------------------
2 | 2 | CREATE TABLE ctas_stats_1 AS SELECT 1 AS a
2 | 4 | CREATE TABLE ctas_stats_2 AS +
| | SELECT a AS col1, 2::int AS col2 +
| | FROM generate_series(1, 10) AS tab(a) WHERE a < 5 AND a > 2
calls | rows | query
-------+------+--------------------------------------------------------------------
2 | 2 | CREATE TABLE ctas_stats_1 AS SELECT $1 AS a
2 | 4 | CREATE TABLE ctas_stats_2 AS +
| | SELECT a AS col1, $1::int AS col2 +
| | FROM generate_series($2, $3) AS tab(a) WHERE a < $4 AND a > $5
2 | 0 | DROP TABLE ctas_stats_1
2 | 0 | DROP TABLE ctas_stats_2
1 | 1 | SELECT pg_stat_statements_reset()
@ -354,11 +354,11 @@ CREATE MATERIALIZED VIEW matview_stats_1 AS
FROM generate_series(1, 5) AS tab(a) WHERE a < 4 AND a > 3;
DROP MATERIALIZED VIEW matview_stats_1;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+-----------------------------------------------------------------
2 | 2 | CREATE MATERIALIZED VIEW matview_stats_1 AS +
| | SELECT a AS col1, 2::int AS col2 +
| | FROM generate_series(1, 10) AS tab(a) WHERE a < 5 AND a > 2
calls | rows | query
-------+------+--------------------------------------------------------------------
2 | 2 | CREATE MATERIALIZED VIEW matview_stats_1 AS +
| | SELECT a AS col1, $1::int AS col2 +
| | FROM generate_series($2, $3) AS tab(a) WHERE a < $4 AND a > $5
2 | 0 | DROP MATERIALIZED VIEW matview_stats_1
1 | 1 | SELECT pg_stat_statements_reset()
(3 rows)
@ -508,19 +508,19 @@ FETCH FORWARD ALL pgss_cursor;
COMMIT;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+----------------------------------------------------------------------------
calls | rows | query
-------+------+-------------------------------------------------------------------------
1 | 0 | BEGIN
1 | 0 | COMMIT
1 | 3 | COPY pgss_ctas (a, b) FROM STDIN
1 | 13 | CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas
1 | 10 | CREATE TABLE pgss_ctas AS SELECT a, 'ctas' b FROM generate_series(1, 10) a
1 | 10 | CREATE TABLE pgss_ctas AS SELECT a, $1 b FROM generate_series($2, $3) a
1 | 0 | DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv
1 | 5 | FETCH FORWARD 5 pgss_cursor
1 | 7 | FETCH FORWARD ALL pgss_cursor
1 | 1 | FETCH NEXT pgss_cursor
1 | 13 | REFRESH MATERIALIZED VIEW pgss_matv
1 | 10 | SELECT generate_series(1, 10) c INTO pgss_select_into
1 | 10 | SELECT generate_series($1, $2) c INTO pgss_select_into
1 | 1 | SELECT pg_stat_statements_reset()
(12 rows)

View File

@ -836,8 +836,10 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
if (query->utilityStmt)
{
if (pgss_track_utility && !PGSS_HANDLED_UTILITY(query->utilityStmt))
{
query->queryId = UINT64CONST(0);
return;
return;
}
}
/*