Fix up planner infrastructure to support LATERAL properly.

Tom Lane <tgl@sss.pgh.pa.us>

Commit: 9ff79b9d4e71822a875c0f5e38f5ec86c7fb079f
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: 2012-08-27T02:50:23Z
Releases: 9.3.0
Fix up planner infrastructure to support LATERAL properly.

This patch takes care of a number of problems having to do with failure
to choose valid join orders and incorrect handling of lateral references
pulled up from subqueries.  Notable changes:

* Add a LateralJoinInfo data structure similar to SpecialJoinInfo, to
represent join ordering constraints created by lateral references.
(I first considered extending the SpecialJoinInfo structure, but the
semantics are different enough that a separate data structure seems
better.)  Extend join_is_legal() and related functions to prevent trying
to form unworkable joins, and to ensure that we will consider joins that
satisfy lateral references even if the joins would be clauseless.

* Fill in the infrastructure needed for the last few types of relation scan
paths to support parameterization.  We'd have wanted this eventually
anyway, but it is necessary now because a relation that gets pulled up out
of a UNION ALL subquery may acquire a reltargetlist containing lateral
references, meaning that its paths *have* to be parameterized whether or
not we have any code that can push join quals down into the scan.

* Compute data about lateral references early in query_planner(), and save
in RelOptInfo nodes, to avoid repetitive calculations later.

* Assorted corner-case bug fixes.

There's probably still some bugs left, but this is a lot closer to being
real than it was before.

Files

PathChange+/−
src/backend/nodes/copyfuncs.c modified +17 −0
src/backend/nodes/equalfuncs.c modified +12 −0
src/backend/nodes/outfuncs.c modified +16 −0
src/backend/optimizer/path/allpaths.c modified +55 −52
src/backend/optimizer/path/costsize.c modified +38 −14
src/backend/optimizer/path/indxpath.c modified +14 −3
src/backend/optimizer/path/joinpath.c modified +11 −19
src/backend/optimizer/path/joinrels.c modified +80 −6
src/backend/optimizer/path/tidpath.c modified +11 −1
src/backend/optimizer/plan/analyzejoins.c modified +21 −0
src/backend/optimizer/plan/createplan.c modified +37 −2
src/backend/optimizer/plan/initsplan.c modified +240 −27
src/backend/optimizer/plan/planagg.c modified +2 −1
src/backend/optimizer/plan/planmain.c modified +9 −0
src/backend/optimizer/plan/planner.c modified +27 −8
src/backend/optimizer/prep/prepjointree.c modified +6 −2
src/backend/optimizer/prep/prepunion.c modified +1 −0
src/backend/optimizer/util/pathnode.c modified +16 −10
src/backend/optimizer/util/placeholder.c modified +4 −4
src/backend/optimizer/util/relnode.c modified +13 −15
src/backend/optimizer/util/var.c modified +1 −0
src/backend/rewrite/rewriteManip.c modified +3 −0
src/include/nodes/nodes.h modified +1 −0
src/include/nodes/relation.h modified +41 −3
src/include/optimizer/cost.h modified +3 −2
src/include/optimizer/pathnode.h modified +5 −3
src/include/optimizer/planmain.h modified +2 −0
src/include/optimizer/planner.h modified +2 −0
src/test/regress/expected/join.out modified +112 −10
src/test/regress/sql/join.sql modified +17 −0