Re: Fix unsafe PlannedStmt access in pg_stat_statements
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: pgsql-hackers@lists.postgresql.org, Michael Paquier <michael@paquier.xyz>,
Chao Li <li.evan.chao@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-05-12T09:00:16Z
Lists: pgsql-hackers
Hi, On May 12, 2026 5:30:53 AM GMT+02:00, Michael Paquier <michael@paquier.xyz> wrote: >On Mon, May 11, 2026 at 04:11:41PM +0800, Chao Li wrote: >> On May 11, 2026, at 16:07, Chao Li <li.evan.chao@gmail.com> wrote: >>> In pgss_ProcessUtility(), there is this comment: >>> ``` >>> /* >>> * 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. >>> * >>> * For the same reason, we can't risk restoring pstmt->queryId to its >>> * former value, which'd otherwise be a good idea. >>> */ >>> ``` >>> >>> The attached patch fixes this by saving pstmt->planOrigin, >>> following the same pattern already used for queryId, stmt_location, >>> and stmt_len. > >Yeah, you are right. This code should save the planOrigin but it does >not do so. Seems like the code should make this clearer, by simply unsetting pstmt at the point it becomes unsafe to use? Andres