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

Commita9fdb48

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 parent594b975 commita9fdb48

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

‎contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11171117
DestReceiver*dest,QueryCompletion*qc)
11181118
{
11191119
Node*parsetree=pstmt->utilityStmt;
1120+
intsaved_stmt_location=pstmt->stmt_location;
1121+
intsaved_stmt_len=pstmt->stmt_len;
11201122

11211123
/*
11221124
* If it's an EXECUTE statement, we don't track it and don't increment the
@@ -1167,6 +1169,13 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11671169
}
11681170
PG_END_TRY();
11691171

1172+
/*
1173+
* CAUTION: do not access the *pstmt data structure again below here.
1174+
* If it was a ROLLBACK or similar, that data structure may have been
1175+
* freed. We must copy everything we still need into local variables,
1176+
* which we did above.
1177+
*/
1178+
11701179
INSTR_TIME_SET_CURRENT(duration);
11711180
INSTR_TIME_SUBTRACT(duration,start);
11721181

@@ -1182,8 +1191,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11821191

11831192
pgss_store(queryString,
11841193
0,/* signal that it's a utility stmt */
1185-
pstmt->stmt_location,
1186-
pstmt->stmt_len,
1194+
saved_stmt_location,
1195+
saved_stmt_len,
11871196
PGSS_EXEC,
11881197
INSTR_TIME_GET_MILLISEC(duration),
11891198
rows,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp