diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2011-09-03 15:35:12 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2011-09-03 15:36:24 -0400 |
commit | b3aaf9081a1a95c245fd605dcf02c91b3a5c3a29 (patch) | |
tree | c3c0c1bd1fdc77722d139f1add99c7abb7a922ef /src/backend/optimizer/prep/prepunion.c | |
parent | 42ad992fdc25fa69db03ff242216f6712da2c56a (diff) | |
download | postgresql-b3aaf9081a1a95c245fd605dcf02c91b3a5c3a29.tar.gz postgresql-b3aaf9081a1a95c245fd605dcf02c91b3a5c3a29.zip |
Rearrange planner to save the whole PlannerInfo (subroot) for a subquery.
Formerly, set_subquery_pathlist and other creators of plans for subqueries
saved only the rangetable and rowMarks lists from the lower-level
PlannerInfo. But there's no reason not to remember the whole PlannerInfo,
and indeed this turns out to simplify matters in a number of places.
The immediate reason for doing this was so that the subroot will still be
accessible when we're trying to extract column statistics out of an
already-planned subquery. But now that I've done it, it seems like a good
code-beautification effort in its own right.
I also chose to get rid of the transient subrtable and subrowmark fields in
SubqueryScan nodes, in favor of having setrefs.c look up the subquery's
RelOptInfo. That required changing all the APIs in setrefs.c to pass
PlannerInfo not PlannerGlobal, which was a large but quite mechanical
transformation.
One side-effect not foreseen at the beginning is that this finally broke
inheritance_planner's assumption that replanning the same subquery RTE N
times would necessarily give interchangeable results each time. That
assumption was always pretty risky, but now we really have to make a
separate RTE for each instance so that there's a place to carry the
separate subroots.
Diffstat (limited to 'src/backend/optimizer/prep/prepunion.c')
-rw-r--r-- | src/backend/optimizer/prep/prepunion.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index 31b101b7a11..b6ab0f53f52 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -130,6 +130,7 @@ plan_set_operations(PlannerInfo *root, double tuple_fraction, Query *parse = root->parse; SetOperationStmt *topop = (SetOperationStmt *) parse->setOperations; Node *node; + RangeTblEntry *leftmostRTE; Query *leftmostQuery; Assert(topop && IsA(topop, SetOperationStmt)); @@ -143,6 +144,13 @@ plan_set_operations(PlannerInfo *root, double tuple_fraction, Assert(parse->distinctClause == NIL); /* + * We'll need to build RelOptInfos for each of the leaf subqueries, + * which are RTE_SUBQUERY rangetable entries in this Query. Prepare the + * index arrays for that. + */ + setup_simple_rel_arrays(root); + + /* * Find the leftmost component Query. We need to use its column names for * all generated tlists (else SELECT INTO won't work right). */ @@ -150,8 +158,8 @@ plan_set_operations(PlannerInfo *root, double tuple_fraction, while (node && IsA(node, SetOperationStmt)) node = ((SetOperationStmt *) node)->larg; Assert(node && IsA(node, RangeTblRef)); - leftmostQuery = rt_fetch(((RangeTblRef *) node)->rtindex, - parse->rtable)->subquery; + leftmostRTE = root->simple_rte_array[((RangeTblRef *) node)->rtindex]; + leftmostQuery = leftmostRTE->subquery; Assert(leftmostQuery != NULL); /* @@ -206,8 +214,9 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, if (IsA(setOp, RangeTblRef)) { RangeTblRef *rtr = (RangeTblRef *) setOp; - RangeTblEntry *rte = rt_fetch(rtr->rtindex, root->parse->rtable); + RangeTblEntry *rte = root->simple_rte_array[rtr->rtindex]; Query *subquery = rte->subquery; + RelOptInfo *rel; PlannerInfo *subroot; Plan *subplan, *plan; @@ -215,6 +224,13 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, Assert(subquery != NULL); /* + * We need to build a RelOptInfo for each leaf subquery. This isn't + * used for anything here, but it carries the subroot data structures + * forward to setrefs.c processing. + */ + rel = build_simple_rel(root, rtr->rtindex, RELOPT_BASEREL); + + /* * Generate plan for primitive subquery */ subplan = subquery_planner(root->glob, subquery, @@ -222,6 +238,10 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, false, tuple_fraction, &subroot); + /* Save subroot and subplan in RelOptInfo for setrefs.c */ + rel->subplan = subplan; + rel->subroot = subroot; + /* * Estimate number of groups if caller wants it. If the subquery used * grouping or aggregation, its output is probably mostly unique @@ -250,9 +270,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, refnames_tlist), NIL, rtr->rtindex, - subplan, - subroot->parse->rtable, - subroot->rowMarks); + subplan); /* * We don't bother to determine the subquery's output ordering since |