pgsql: Fix stale initplans after removing a plan level in setrefs.c

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix stale initplans after removing a plan level in setrefs.c
Date: 2026-11-11 06:56:04
Message-ID: E1xR2FY-00000000HVa-1bGC@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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(at)gmail(dot)com>
Reviewed-by: David Rowley <dgrowleyml(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAMbWs4_JzG96+RjzdEpRQ0-rWkuzU1cmj7xybpzFg6GYdGcE8Q@mail.gmail.com
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/9b80b3170b9c5be2f22540facea52f5e38915704

Modified Files
--------------
src/backend/optimizer/plan/setrefs.c | 8 ++++++++
src/test/regress/expected/subselect.out | 31 +++++++++++++++++++++++++++++++
src/test/regress/sql/subselect.sql | 17 +++++++++++++++++
3 files changed, 56 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Richard Guo 2026-11-11 07:12:54 Re: pgsql: doc: Explain that ANALYZE ignores reject_limit in file_fdw
Previous Message Amit Langote 2026-11-11 06:06:03 pgsql: SQL/JSON: Fix PASSING of string types other than text and varcha