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 <[email protected]> Reviewed-by: David Rowley <[email protected]> Discussion: https://postgr.es/m/cambws4_jzg96+rjzdeprq0-rwkuzu1cmj7xybpzfg6gydgc...@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(+)
