1
0
mirror of https://github.com/postgres/postgres.git synced 2026-01-29 12:02:15 +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

@@ -3422,15 +3422,44 @@ typedef enum FetchDirection
FETCH_RELATIVE,
} FetchDirection;
typedef enum FetchDirectionKeywords
{
FETCH_KEYWORD_NONE = 0,
FETCH_KEYWORD_NEXT,
FETCH_KEYWORD_PRIOR,
FETCH_KEYWORD_FIRST,
FETCH_KEYWORD_LAST,
FETCH_KEYWORD_ABSOLUTE,
FETCH_KEYWORD_RELATIVE,
FETCH_KEYWORD_ALL,
FETCH_KEYWORD_FORWARD,
FETCH_KEYWORD_FORWARD_ALL,
FETCH_KEYWORD_BACKWARD,
FETCH_KEYWORD_BACKWARD_ALL,
} FetchDirectionKeywords;
#define FETCH_ALL LONG_MAX
typedef struct FetchStmt
{
NodeTag type;
FetchDirection direction; /* see above */
long howMany; /* number of rows, or position argument */
char *portalname; /* name of portal (cursor) */
bool ismove; /* true if MOVE */
/* number of rows, or position argument */
long howMany pg_node_attr(query_jumble_ignore);
/* name of portal (cursor) */
char *portalname;
/* true if MOVE */
bool ismove;
/*
* Set when a direction_keyword (e.g., FETCH FORWARD) is used, to
* distinguish it from a numeric variant (e.g., FETCH 1) for the purpose
* of query jumbling.
*/
FetchDirectionKeywords direction_keyword;
/* token location, or -1 if unknown */
ParseLoc location pg_node_attr(query_jumble_location);
} FetchStmt;
/* ----------------------