Re: Fix unsafe PlannedStmt access in pg_stat_statements

Chao Li <li.evan.chao@gmail.com>

From: Chao Li <li.evan.chao@gmail.com>
To: Michael Paquier <michael@paquier.xyz>
Cc: Andres Freund <andres@anarazel.de>, pgsql-hackers@lists.postgresql.org
Date: 2026-05-13T05:31:01Z
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


> On May 13, 2026, at 12:26, Michael Paquier <michael@paquier.xyz> wrote:
> 
> 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
> <pgss-pstmt.patch>

Yeah, this is the exactly same change I was about to reply. And I agree the current comment is clear enough, no more needed to add.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/