Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit495e73c

Browse files
committed
pg_stat_statements: fetch stmt location/length before it disappears.
When executing a utility statement, we must fetch everythingwe need out of the PlannedStmt data structure before callingstandard_ProcessUtility. In certain cases (possibly only ROLLBACKin extended query protocol), that data structure will get freedduring command execution. The situation is probably often harmlessin production builds, but in debug builds we intentionally overwritethe freed memory with garbage, leading to picking up garbage valuesof statement location and length, typically causing an assertionfailure later in pg_stat_statements. In non-debug builds, ifsomething did go wrong it would likely lead to storing garbagefor the query string.Report and fix by zhaoqigui (with cosmetic adjustments by me).It's an old problem, so back-patch to all supported versions.Discussion:https://postgr.es/m/17663-a344fd0675f92128@postgresql.orgDiscussion:https://postgr.es/m/1667307420050.56657@hundsun.com
1 parent84387fc commit495e73c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10831083
{
10841084
Node*parsetree=pstmt->utilityStmt;
10851085
uint64saved_queryId=pstmt->queryId;
1086+
intsaved_stmt_location=pstmt->stmt_location;
1087+
intsaved_stmt_len=pstmt->stmt_len;
10861088

10871089
/*
10881090
* Force utility statements to get queryId zero. We do this even in cases
@@ -1148,6 +1150,16 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11481150
}
11491151
PG_END_TRY();
11501152

1153+
/*
1154+
* CAUTION: do not access the *pstmt data structure again below here.
1155+
* If it was a ROLLBACK or similar, that data structure may have been
1156+
* freed. We must copy everything we still need into local variables,
1157+
* which we did above.
1158+
*
1159+
* For the same reason, we can't risk restoring pstmt->queryId to its
1160+
* former value, which'd otherwise be a good idea.
1161+
*/
1162+
11511163
INSTR_TIME_SET_CURRENT(duration);
11521164
INSTR_TIME_SUBTRACT(duration,start);
11531165

@@ -1172,8 +1184,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11721184

11731185
pgss_store(queryString,
11741186
saved_queryId,
1175-
pstmt->stmt_location,
1176-
pstmt->stmt_len,
1187+
saved_stmt_location,
1188+
saved_stmt_len,
11771189
PGSS_EXEC,
11781190
INSTR_TIME_GET_MILLISEC(duration),
11791191
rows,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp