Re: Fix unsafe PlannedStmt access in pg_stat_statements

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org, Michael Paquier <michael(at)paquier(dot)xyz>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix unsafe PlannedStmt access in pg_stat_statements
Date: 2026-05-12 09:00:16
Message-ID: 0A9A8DAC-BC3C-4C7A-9504-2C6050405544@anarazel.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On May 12, 2026 5:30:53 AM GMT+02:00, Michael Paquier <michael(at)paquier(dot)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(dot)evan(dot)chao(at)gmail(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Maksim.Melnikov 2026-05-12 08:30:55 Re: Incorrect checksum in control file with pg_rewind test