1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Show sizes of FETCH queries as constants in pg_stat_statements

Prior to this patch, every FETCH call would generate a unique queryId
with a different size specified.  Depending on the workloads, this could
lead to a significant bloat in pg_stat_statements, as repeatedly calling
a specific cursor would result in a new queryId each time.  For example,
FETCH 1 c1; and FETCH 2 c1; would produce different queryIds.

This patch improves the situation by normalizing the fetch size, so as
semantically similar statements generate the same queryId.  As a result,
statements like the below, which differ syntactically but have the same
effect, will now share a single queryId:
FETCH FROM c1
FETCH NEXT c1
FETCH 1 c1

In order to do a normalization based on the keyword used in FETCH,
FetchStmt is tweaked with a new FetchDirectionKeywords.  This matters
for "howMany", which could be set to a negative value depending on the
direction, and we want to normalize the queries with enough information
about the direction keywords provided, including RELATIVE, ABSOLUTE or
all the ALL variants.

Author: Sami Imseih <samimseih@gmail.com>
Discussion: https://postgr.es/m/CAA5RZ0tA6LbHCg2qSS+KuM850BZC_+ZgHV7Ug6BXw22TNyF+MA@mail.gmail.com
This commit is contained in:
Michael Paquier
2025-07-02 08:39:25 +09:00
parent 184595836b
commit bee23ea4dd
7 changed files with 304 additions and 62 deletions

View File

@ -57,8 +57,8 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
1 | 0 | COMMIT
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 | FETCH $1 IN cursor_stats_1
1 | 1 | FETCH $1 IN cursor_stats_2
1 | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(9 rows)
@ -68,3 +68,140 @@ SELECT pg_stat_statements_reset() IS NOT NULL AS t;
t
(1 row)
-- Normalization of FETCH statements
BEGIN;
DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series(1, 10);
-- implicit directions
FETCH pgss_cursor;
--
(1 row)
FETCH 1 pgss_cursor;
--
(1 row)
FETCH 2 pgss_cursor;
--
(2 rows)
FETCH -1 pgss_cursor;
--
(1 row)
-- explicit NEXT
FETCH NEXT pgss_cursor;
--
(1 row)
-- explicit PRIOR
FETCH PRIOR pgss_cursor;
--
(1 row)
-- explicit FIRST
FETCH FIRST pgss_cursor;
--
(1 row)
-- explicit LAST
FETCH LAST pgss_cursor;
--
(1 row)
-- explicit ABSOLUTE
FETCH ABSOLUTE 1 pgss_cursor;
--
(1 row)
FETCH ABSOLUTE 2 pgss_cursor;
--
(1 row)
FETCH ABSOLUTE -1 pgss_cursor;
--
(1 row)
-- explicit RELATIVE
FETCH RELATIVE 1 pgss_cursor;
--
(0 rows)
FETCH RELATIVE 2 pgss_cursor;
--
(0 rows)
FETCH RELATIVE -1 pgss_cursor;
--
(1 row)
-- explicit FORWARD
FETCH ALL pgss_cursor;
--
(0 rows)
-- explicit FORWARD ALL
FETCH FORWARD ALL pgss_cursor;
--
(0 rows)
-- explicit FETCH FORWARD
FETCH FORWARD pgss_cursor;
--
(0 rows)
FETCH FORWARD 1 pgss_cursor;
--
(0 rows)
FETCH FORWARD 2 pgss_cursor;
--
(0 rows)
FETCH FORWARD -1 pgss_cursor;
--
(1 row)
-- explicit FETCH BACKWARD
FETCH BACKWARD pgss_cursor;
--
(1 row)
FETCH BACKWARD 1 pgss_cursor;
--
(1 row)
FETCH BACKWARD 2 pgss_cursor;
--
(2 rows)
FETCH BACKWARD -1 pgss_cursor;
--
(1 row)
-- explicit BACKWARD ALL
FETCH BACKWARD ALL pgss_cursor;
--
(6 rows)
COMMIT;
SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | query
-------+--------------------------------------------------------------------
1 | BEGIN
1 | COMMIT
1 | DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series($1, $2)
3 | FETCH ABSOLUTE $1 pgss_cursor
1 | FETCH ALL pgss_cursor
1 | FETCH BACKWARD ALL pgss_cursor
4 | FETCH BACKWARD pgss_cursor
1 | FETCH FIRST pgss_cursor
1 | FETCH FORWARD ALL pgss_cursor
4 | FETCH FORWARD pgss_cursor
1 | FETCH LAST pgss_cursor
1 | FETCH NEXT pgss_cursor
1 | FETCH PRIOR pgss_cursor
3 | FETCH RELATIVE $1 pgss_cursor
4 | FETCH pgss_cursor
1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(16 rows)

View File

@ -1147,7 +1147,7 @@ SELECT toplevel, calls, query FROM pg_stat_statements
t | 1 | COMMIT
t | 1 | DECLARE FOOCUR CURSOR FOR SELECT * from stats_track_tab
f | 1 | DECLARE FOOCUR CURSOR FOR SELECT * from stats_track_tab;
t | 1 | FETCH FORWARD 1 FROM foocur
t | 1 | FETCH FORWARD $1 FROM foocur
t | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(7 rows)
@ -1176,7 +1176,7 @@ SELECT toplevel, calls, query FROM pg_stat_statements
t | 1 | CLOSE foocur
t | 1 | COMMIT
t | 1 | DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_tab
t | 1 | FETCH FORWARD 1 FROM foocur
t | 1 | FETCH FORWARD $1 FROM foocur
t | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(6 rows)

View File

@ -702,7 +702,7 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
1 | 13 | CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas
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 | 5 | FETCH FORWARD $1 pgss_cursor
1 | 7 | FETCH FORWARD ALL pgss_cursor
1 | 1 | FETCH NEXT pgss_cursor
1 | 13 | REFRESH MATERIALIZED VIEW pgss_matv