pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Rowley <drowley@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix incorrect reinit of run-time pruning for EPQ rechecks
Date: Thu, 08 Oct 2026 22:04:15 +0000
Message-ID: <E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org> (raw)

Fix incorrect reinit of run-time pruning for EPQ rechecks

For run-time partition pruning running during execution (rather than at
executor startup), EvalPlanQualStart() reuses the es_part_prune_states
from the parent EState.  This could cause an issue when the EPQ recheck is
finished, its EState is cleaned up and the es_query_cxt is deleted by
FreeExecutorState().  That cleanup could free memory that was allocated
again in InitExecPartitionPruneContexts() when it was called for the EPQ's
EState, and naturally, those fields got allocated in the EPQ's
es_query_cxt MemoryContext, which overwrote the ones for the parent
EState.  If pruning occurs again after the EPQ's EState was cleaned up,
then we could have accessed free'd memory.  This certainly caused issues in
debug builds because we clobber freed memory, but may not have in
production, depending on if the MemoryContext being deleted results in a
free().  It may not if the allocation was on the context's init block and
the context was pushed onto context_freelists[] for reuse.

Here we fix this by adding an "initialized" field to PartitionPruneState
and exit early in InitExecPartitionPruneContexts() when the state is
already initialized.

This was broken by bb3ec16e1, but the breakage there is slightly
different as that commit neglected to do what was fixed in 8741e48e5
(which did get backpatched to v18 in 9a82a64ed).

Reported-by: Vladimir Savin <vladimir@encord.com>
Author: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Discussion: https://postgr.es/m/CAP2G_gGpV=rKMLuET4RW-qO41GvBvi6Czsjsj16eT9hAwHxG6w@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/2359968b449a0fd53cfc04d4a644ae02370adec6

Modified Files
--------------
src/backend/executor/execPartition.c               | 12 ++++++
src/include/executor/execPartition.h               |  3 ++
.../expected/eval-plan-qual-partition-prune.out    | 30 +++++++++++++++
src/test/isolation/isolation_schedule              |  1 +
.../specs/eval-plan-qual-partition-prune.spec      | 43 ++++++++++++++++++++++
5 files changed, 89 insertions(+)



view thread (3+ messages)  latest in thread

Message-ID: <E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org>
Permalink:  ../E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: drowley@postgresql.org
  Subject: Re: pgsql: Fix incorrect reinit of run-time pruning for EPQ rechecks
  In-Reply-To: <E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org>
  References: <E1xEwDn-00000000rzr-01VE@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox