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
------
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(+)

Reply via email to