1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-21 00:42:43 +03:00

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.
This commit is contained in:
Tom Lane
2011-09-03 15:35:12 -04:00
parent 42ad992fdc
commit b3aaf9081a
19 changed files with 394 additions and 304 deletions

View File

@@ -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));
@@ -142,6 +143,13 @@ plan_set_operations(PlannerInfo *root, double tuple_fraction,
Assert(parse->windowClause == NIL);
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,14 +214,22 @@ 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;
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
*/
@@ -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