From 9ff79b9d4e71822a875c0f5e38f5ec86c7fb079f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 26 Aug 2012 22:48:55 -0400 Subject: 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. --- src/backend/optimizer/path/joinpath.c | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'src/backend/optimizer/path/joinpath.c') diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c index f54c3931ce4..d87ba464014 100644 --- a/src/backend/optimizer/path/joinpath.c +++ b/src/backend/optimizer/path/joinpath.c @@ -149,28 +149,20 @@ add_paths_to_joinrel(PlannerInfo *root, /* * However, when a LATERAL subquery is involved, we have to be a bit - * laxer, because there may simply not be any paths for the joinrel that - * aren't parameterized by whatever the subquery is parameterized by. - * Hence, add to param_source_rels anything that is in the minimum - * parameterization of either input (and not in the other input). - * - * XXX need a more principled way of determining minimum parameterization. + * laxer, because there will simply not be any paths for the joinrel that + * aren't parameterized by whatever the subquery is parameterized by, + * unless its parameterization is resolved within the joinrel. Hence, add + * to param_source_rels anything that is laterally referenced in either + * input and is not in the join already. */ - if (outerrel->cheapest_total_path == NULL) + foreach(lc, root->lateral_info_list) { - Path *cheapest = (Path *) linitial(outerrel->cheapest_parameterized_paths); + LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(lc); - param_source_rels = bms_join(param_source_rels, - bms_difference(PATH_REQ_OUTER(cheapest), - innerrel->relids)); - } - if (innerrel->cheapest_total_path == NULL) - { - Path *cheapest = (Path *) linitial(innerrel->cheapest_parameterized_paths); - - param_source_rels = bms_join(param_source_rels, - bms_difference(PATH_REQ_OUTER(cheapest), - outerrel->relids)); + if (bms_is_member(ljinfo->lateral_rhs, joinrel->relids)) + param_source_rels = bms_join(param_source_rels, + bms_difference(ljinfo->lateral_lhs, + joinrel->relids)); } /* -- cgit v1.2.3