pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix reuse of stale hash tables by duplicated hashed SubPlans
Date: Mon, 02 Nov 2026 05:42:15 +0000
Message-ID: <E1xNkoB-00000001kZR-0xt2@gemulon.postgresql.org> (raw)

Fix reuse of stale hash tables by duplicated hashed SubPlans

The same SubPlan can appear more than once in a plan tree.  For
example, when an equivalence class has a constant member containing a
SubPlan, each derived "member = constant" clause references that
SubPlan; a hash join's hash keys and its hash clauses can both contain
it; and extract_restriction_or_clauses() can copy it into a new
restriction clause while leaving it in the original join clause.  Each
reference gets its own SubPlanState, with its own hash tables, but
they all share the subplan's PlanState.

A hashed SubPlan whose subquery references outer query levels must
rebuild its hash tables whenever those parameters change.
ExecHashSubPlan() detected that by checking chgParam of the subplan's
PlanState.  But buildSubPlanHash() rescans that PlanState, which
clears its chgParam, so after one SubPlanState had rebuilt its hash
tables, the others no longer saw the change and kept probing hash
tables built for previous parameter values, producing wrong query
results.

To fix, give each SubPlanState its own flag to mark its hash tables as
stale.  ExecReScan() sets it, alongside propagating chgParam to the
node's subplans, ExecHashSubPlan() checks it instead of the subplan's
chgParam, and buildSubPlanHash() clears it.  The new field fits into
existing padding in SubPlanState, so this does not change the struct's
size or the offsets of its other fields.

Reported-by: Samuel Olaoye <dapsalmy@gmail.com>
Author: Rahul Yadav <rahul@rhyadav.dev>
Reviewed-by: Alexandra Wang <alexandra.wang.oss@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: shihao zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/179087936257.78721.1170374394656748858@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/fcb44c5e579fd32dbe96adf08a9f82ddaa518176

Modified Files
--------------
src/backend/executor/execAmi.c          |  7 +++++++
src/backend/executor/nodeSubplan.c      |  5 +++--
src/include/nodes/execnodes.h           |  1 +
src/test/regress/expected/subselect.out | 33 +++++++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql      | 13 +++++++++++++
5 files changed, 57 insertions(+), 2 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xNkoB-00000001kZR-0xt2@gemulon.postgresql.org>
Permalink:  ../E1xNkoB-00000001kZR-0xt2@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xNkoB-00000001kZR-0xt2@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: rguo@postgresql.org
  Subject: Re: pgsql: Fix reuse of stale hash tables by duplicated hashed SubPlans
  In-Reply-To: <E1xNkoB-00000001kZR-0xt2@gemulon.postgresql.org>
  References: <E1xNkoB-00000001kZR-0xt2@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