Re: Fix unsafe PlannedStmt access in pg_stat_statements

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-hackers@lists.postgresql.org, Chao Li <li.evan.chao@gmail.com>
Date: 2026-05-13T04:26:36Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. pg_stat_statements: Set PlannedStmt to NULL after nested utility execution

  2. pg_stat_statements: Fix potential use-after-free of PlannedStmt

  3. pg_stat_statements: Add counters for generic and custom plans

Attachments

On Tue, May 12, 2026 at 11:00:16AM +0200, Andres Freund wrote:
> Seems like the code should make this clearer, by simply unsetting
> pstmt at the point it becomes unsafe to use? 

Sure, we could do that as well and crash hard if something decides to
do the same mistake.  Like the simple patch attached for example?  I
am not sure if we need to update the comment.  It's pretty clear what
the intention is, at least to me.
--
Michael