summaryrefslogtreecommitdiff
path: root/contrib/pg_stat_statements/sql
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-07-02 08:39:25 +0900
committerMichael Paquier <michael@paquier.xyz>2025-07-02 08:39:25 +0900
commitbee23ea4ddc46198c95a4e73a83f453c09e04bf8 (patch)
tree1328ff54b0c2c360c6a20755ce79647fd2782dd6 /contrib/pg_stat_statements/sql
parent184595836ba37e1d35cb8a4e9298dc0eed763746 (diff)
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
Diffstat (limited to 'contrib/pg_stat_statements/sql')
-rw-r--r--contrib/pg_stat_statements/sql/cursors.sql43
1 files changed, 43 insertions, 0 deletions
diff --git a/contrib/pg_stat_statements/sql/cursors.sql b/contrib/pg_stat_statements/sql/cursors.sql
index 61738ac470e..78bb4228433 100644
--- a/contrib/pg_stat_statements/sql/cursors.sql
+++ b/contrib/pg_stat_statements/sql/cursors.sql
@@ -28,3 +28,46 @@ COMMIT;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
+
+-- Normalization of FETCH statements
+BEGIN;
+DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series(1, 10);
+-- implicit directions
+FETCH pgss_cursor;
+FETCH 1 pgss_cursor;
+FETCH 2 pgss_cursor;
+FETCH -1 pgss_cursor;
+-- explicit NEXT
+FETCH NEXT pgss_cursor;
+-- explicit PRIOR
+FETCH PRIOR pgss_cursor;
+-- explicit FIRST
+FETCH FIRST pgss_cursor;
+-- explicit LAST
+FETCH LAST pgss_cursor;
+-- explicit ABSOLUTE
+FETCH ABSOLUTE 1 pgss_cursor;
+FETCH ABSOLUTE 2 pgss_cursor;
+FETCH ABSOLUTE -1 pgss_cursor;
+-- explicit RELATIVE
+FETCH RELATIVE 1 pgss_cursor;
+FETCH RELATIVE 2 pgss_cursor;
+FETCH RELATIVE -1 pgss_cursor;
+-- explicit FORWARD
+FETCH ALL pgss_cursor;
+-- explicit FORWARD ALL
+FETCH FORWARD ALL pgss_cursor;
+-- explicit FETCH FORWARD
+FETCH FORWARD pgss_cursor;
+FETCH FORWARD 1 pgss_cursor;
+FETCH FORWARD 2 pgss_cursor;
+FETCH FORWARD -1 pgss_cursor;
+-- explicit FETCH BACKWARD
+FETCH BACKWARD pgss_cursor;
+FETCH BACKWARD 1 pgss_cursor;
+FETCH BACKWARD 2 pgss_cursor;
+FETCH BACKWARD -1 pgss_cursor;
+-- explicit BACKWARD ALL
+FETCH BACKWARD ALL pgss_cursor;
+COMMIT;
+SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";