pg_stat_statements: fetch stmt location/length before it disappears.
authorTom Lane <[email protected]>
Tue, 1 Nov 2022 16:48:01 +0000 (12:48 -0400)
committerTom Lane <[email protected]>
Tue, 1 Nov 2022 16:48:01 +0000 (12:48 -0400)
commit0f2f5645a1f347bad7bfa4b670ce9aacdb9e634d
treedb6f3826c0590e9d328058344ace678f939e1fbb
parent5a30d43fa9869adc5a6f708dd0994a8a4e53d905
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[email protected]
contrib/pg_stat_statements/pg_stat_statements.c