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

@ -17,9 +17,9 @@ 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
-------+------+------------------------------------------------------
-------+------+-------------------------------------------------------
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)
@ -50,13 +50,13 @@ CLOSE cursor_stats_2;
COMMIT;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
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

@ -227,9 +227,9 @@ EXPLAIN (costs off) SELECT a FROM generate_series(1,10) AS tab(a) WHERE a = 7;
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
-------+------+---------------------------------------------------------------------------------
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)
@ -327,11 +327,11 @@ CREATE TABLE ctas_stats_2 AS
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 | 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
| | 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()
@ -355,10 +355,10 @@ CREATE MATERIALIZED VIEW matview_stats_1 AS
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
| | 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)
@ -509,18 +509,18 @@ FETCH FORWARD ALL pgss_cursor;
COMMIT;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
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,9 +836,11 @@ 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;
}
}
/*
* If query jumbling were able to identify any ignorable constants, we