1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-27 00:12:01 +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

@@ -7477,6 +7477,8 @@ fetch_args: cursor_name
n->portalname = $1;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| from_in cursor_name
@@ -7486,60 +7488,8 @@ fetch_args: cursor_name
n->portalname = $2;
n->direction = FETCH_FORWARD;
n->howMany = 1;
$$ = (Node *) n;
}
| NEXT opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = 1;
$$ = (Node *) n;
}
| PRIOR opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_BACKWARD;
n->howMany = 1;
$$ = (Node *) n;
}
| FIRST_P opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = 1;
$$ = (Node *) n;
}
| LAST_P opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = -1;
$$ = (Node *) n;
}
| ABSOLUTE_P SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $4;
n->direction = FETCH_ABSOLUTE;
n->howMany = $2;
$$ = (Node *) n;
}
| RELATIVE_P SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $4;
n->direction = FETCH_RELATIVE;
n->howMany = $2;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| SignedIconst opt_from_in cursor_name
@@ -7549,6 +7499,74 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = $1;
n->location = @1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| NEXT opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NEXT;
$$ = (Node *) n;
}
| PRIOR opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_BACKWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_PRIOR;
$$ = (Node *) n;
}
| FIRST_P opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FIRST;
$$ = (Node *) n;
}
| LAST_P opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = -1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_LAST;
$$ = (Node *) n;
}
| ABSOLUTE_P SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $4;
n->direction = FETCH_ABSOLUTE;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_ABSOLUTE;
$$ = (Node *) n;
}
| RELATIVE_P SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);
n->portalname = $4;
n->direction = FETCH_RELATIVE;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_RELATIVE;
$$ = (Node *) n;
}
| ALL opt_from_in cursor_name
@@ -7558,6 +7576,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_ALL;
$$ = (Node *) n;
}
| FORWARD opt_from_in cursor_name
@@ -7567,6 +7587,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FORWARD;
$$ = (Node *) n;
}
| FORWARD SignedIconst opt_from_in cursor_name
@@ -7576,6 +7598,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_FORWARD;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_FORWARD;
$$ = (Node *) n;
}
| FORWARD ALL opt_from_in cursor_name
@@ -7585,6 +7609,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_FORWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FORWARD_ALL;
$$ = (Node *) n;
}
| BACKWARD opt_from_in cursor_name
@@ -7594,6 +7620,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_BACKWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_BACKWARD;
$$ = (Node *) n;
}
| BACKWARD SignedIconst opt_from_in cursor_name
@@ -7603,6 +7631,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_BACKWARD;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_BACKWARD;
$$ = (Node *) n;
}
| BACKWARD ALL opt_from_in cursor_name
@@ -7612,6 +7642,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_BACKWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_BACKWARD_ALL;
$$ = (Node *) n;
}
;