summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2022-11-01 12:48:01 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2022-11-01 12:48:01 -0400
commit56083ff3015b1beadb72724d3b9ddac81f69453c (patch)
tree7ef53b0622eda6d61c2582fb1e4ffd47b4224bb3
parentb02fc7df193b2c71f359cc2b42e21515311ab188 (diff)
downloadpostgresql-56083ff3015b1beadb72724d3b9ddac81f69453c.tar.gz
pg_stat_statements: fetch stmt location/length before it disappears.
When executing a utility statement, we must fetch everything we need out of the PlannedStmt data structure before calling standard_ProcessUtility. In certain cases (possibly only ROLLBACK in extended query protocol), that data structure will get freed during command execution. The situation is probably often harmless in production builds, but in debug builds we intentionally overwrite the freed memory with garbage, leading to picking up garbage values of statement location and length, typically causing an assertion failure later in pg_stat_statements. In non-debug builds, if something did go wrong it would likely lead to storing garbage for 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.org Discussion: https://postgr.es/m/1667307420050.56657@hundsun.com
-rw-r--r--contrib/pg_stat_statements/pg_stat_statements.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 3b32ec53c5..bb72ac76cb 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -962,6 +962,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
DestReceiver *dest, char *completionTag)
{
Node *parsetree = pstmt->utilityStmt;
+ int saved_stmt_location = pstmt->stmt_location;
+ int saved_stmt_len = pstmt->stmt_len;
/*
* If it's an EXECUTE statement, we don't track it and don't increment the
@@ -1011,6 +1013,13 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
}
PG_END_TRY();
+ /*
+ * CAUTION: do not access the *pstmt data structure again below here.
+ * If it was a ROLLBACK or similar, that data structure may have been
+ * freed. We must copy everything we still need into local variables,
+ * which we did above.
+ */
+
INSTR_TIME_SET_CURRENT(duration);
INSTR_TIME_SUBTRACT(duration, start);
@@ -1049,8 +1058,8 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
pgss_store(queryString,
0, /* signal that it's a utility stmt */
- pstmt->stmt_location,
- pstmt->stmt_len,
+ saved_stmt_location,
+ saved_stmt_len,
INSTR_TIME_GET_MILLISEC(duration),
rows,
&bufusage,