Fix placement of initPlans when forcibly materializing a subplan.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 3 Feb 2017 00:11:27 +0000 (19:11 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 3 Feb 2017 00:11:32 +0000 (19:11 -0500)
commit555494d1bc119173bbf712940da823303644d4de
tree201624bac59be468fd295321af6aa90d242eab16
parentaa09b9dcd57cbf18516332bf8310c12ef6f83715
Fix placement of initPlans when forcibly materializing a subplan.

If we forcibly place a Material node atop a finished subplan, we need
to move any initPlans attached to the subplan up to the Material node,
in order to keep SS_finalize_plan() happy.  I'd figured this out in
commit 7b67a0a49 for the case of materializing a cursor plan, but out of
an abundance of caution, I put the initPlan movement hack at the call
site for that case, rather than inside materialize_finished_plan().
That was the wrong thing, because it turns out to also be necessary for
the only other caller of materialize_finished_plan(), ie subselect.c.
We lacked any test cases that exposed the mistake, but bug#14524 from
Wei Congrui shows that it's possible to get an initPlan reference into
the top tlist in that case too, and then SS_finalize_plan() complains.
Hence, move the hack into materialize_finished_plan().

In HEAD, also relocate some recently-added tests in subselect.sql, which
I'd unthinkingly dropped into the middle of a sequence of related tests.

Report: https://postgr.es/m/20170202060020.1400.89021@wrigleys.postgresql.org
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/plan/planner.c
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql