From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix stale initplans after removing a plan level in setrefs.c
Date: Wed, 11 Nov 2026 06:56:04 +0000
Message-ID: <E1xR2FY-00000000HVm-26EP@gemulon.postgresql.org> (raw)
Fix stale initplans after removing a plan level in setrefs.c
When set_plan_references() removes a trivial SubqueryScan or a
single-child Append or MergeAppend, clean_up_removed_plan_level()
moves the removed node's initplans to its child. However, the child's
extParam and allParam sets, which SS_finalize_plan() computed before
the move, do not include the Params those initplans depend on. At
rescan, UpdateChangedParamSet() therefore does not propagate a change
in such a Param to the child, and the initplans are not re-evaluated,
so the child keeps using their stale output values, producing wrong
results. This can happen, for example, with a lateral subquery that
scans only one partition of a partitioned table and whose scalar
sub-select references an outer column.
To fix, add the removed node's extParam and allParam sets into the
child's.
Back-patch to all supported branches.
Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CAMbWs4_JzG96+RjzdEpRQ0-rWkuzU1cmj7xybpzFg6GYdGcE8Q@mail.gmail.com
Backpatch-through: 15
Branch
------
REL_15_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/39ed14841cd3e470e4fee2289e0f77b53beec772
Modified Files
--------------
src/backend/optimizer/plan/setrefs.c | 11 +++++++++++
src/test/regress/expected/subselect.out | 31 +++++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql | 17 +++++++++++++++++
3 files changed, 59 insertions(+)
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 stale initplans after removing a plan level in setrefs.c
In-Reply-To: <E1xR2FY-00000000HVm-26EP@gemulon.postgresql.org>
References: <E1xR2FY-00000000HVm-26EP@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