1 /*-------------------------------------------------------------------------
4 * The query optimizer external interface.
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.127 2002/11/06 22:31:24 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #ifdef OPTIMIZER_DEBUG
21 #include "nodes/print.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/cost.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/planmain.h"
28 #include "optimizer/planner.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/subselect.h"
31 #include "optimizer/tlist.h"
32 #include "optimizer/var.h"
33 #include "parser/analyze.h"
34 #include "parser/parsetree.h"
35 #include "parser/parse_expr.h"
36 #include "rewrite/rewriteManip.h"
37 #include "utils/lsyscache.h"
40 /* Expression kind codes for preprocess_expression */
41 #define EXPRKIND_TARGET 0
42 #define EXPRKIND_WHERE 1
43 #define EXPRKIND_HAVING 2
46 static Node *pull_up_subqueries(Query *parse, Node *jtnode,
47 bool below_outer_join);
48 static bool is_simple_subquery(Query *subquery);
49 static bool has_nullable_targetlist(Query *subquery);
50 static void resolvenew_in_jointree(Node *jtnode, int varno, List *subtlist);
51 static Node *preprocess_jointree(Query *parse, Node *jtnode);
52 static Node *preprocess_expression(Query *parse, Node *expr, int kind);
53 static void preprocess_qual_conditions(Query *parse, Node *jtnode);
54 static Plan *inheritance_planner(Query *parse, List *inheritlist);
55 static Plan *grouping_planner(Query *parse, double tuple_fraction);
56 static List *make_subplanTargetList(Query *parse, List *tlist,
57 AttrNumber **groupColIdx);
58 static Plan *make_groupsortplan(Query *parse,
60 AttrNumber *grpColIdx,
62 static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
65 /*****************************************************************************
67 * Query optimizer entry point
69 *****************************************************************************/
74 Index save_PlannerQueryLevel;
75 List *save_PlannerParamVar;
78 * The planner can be called recursively (an example is when
79 * eval_const_expressions tries to pre-evaluate an SQL function). So,
80 * these global state variables must be saved and restored.
82 * These vars cannot be moved into the Query structure since their whole
83 * purpose is communication across multiple sub-Queries.
85 * Note we do NOT save and restore PlannerPlanId: it exists to assign
86 * unique IDs to SubPlan nodes, and we want those IDs to be unique for
87 * the life of a backend. Also, PlannerInitPlan is saved/restored in
88 * subquery_planner, not here.
90 save_PlannerQueryLevel = PlannerQueryLevel;
91 save_PlannerParamVar = PlannerParamVar;
93 /* Initialize state for handling outer-level references and params */
94 PlannerQueryLevel = 0; /* will be 1 in top-level subquery_planner */
95 PlannerParamVar = NIL;
97 /* primary planning entry point (may recurse for subqueries) */
98 result_plan = subquery_planner(parse, -1.0 /* default case */ );
100 Assert(PlannerQueryLevel == 0);
102 /* executor wants to know total number of Params used overall */
103 result_plan->nParamExec = length(PlannerParamVar);
105 /* final cleanup of the plan */
106 set_plan_references(result_plan, parse->rtable);
108 /* restore state for outer planner, if any */
109 PlannerQueryLevel = save_PlannerQueryLevel;
110 PlannerParamVar = save_PlannerParamVar;
116 /*--------------------
118 * Invokes the planner on a subquery. We recurse to here for each
119 * sub-SELECT found in the query tree.
121 * parse is the querytree produced by the parser & rewriter.
122 * tuple_fraction is the fraction of tuples we expect will be retrieved.
123 * tuple_fraction is interpreted as explained for grouping_planner, below.
125 * Basically, this routine does the stuff that should only be done once
126 * per Query object. It then calls grouping_planner. At one time,
127 * grouping_planner could be invoked recursively on the same Query object;
128 * that's not currently true, but we keep the separation between the two
129 * routines anyway, in case we need it again someday.
131 * subquery_planner will be called recursively to handle sub-Query nodes
132 * found within the query's expressions and rangetable.
134 * Returns a query plan.
135 *--------------------
138 subquery_planner(Query *parse, double tuple_fraction)
140 List *saved_initplan = PlannerInitPlan;
141 int saved_planid = PlannerPlanId;
146 /* Set up for a new level of subquery */
148 PlannerInitPlan = NIL;
151 * Check to see if any subqueries in the rangetable can be merged into
154 parse->jointree = (FromExpr *)
155 pull_up_subqueries(parse, (Node *) parse->jointree, false);
158 * If so, we may have created opportunities to simplify the jointree.
160 parse->jointree = (FromExpr *)
161 preprocess_jointree(parse, (Node *) parse->jointree);
164 * Do expression preprocessing on targetlist and quals.
166 parse->targetList = (List *)
167 preprocess_expression(parse, (Node *) parse->targetList,
170 preprocess_qual_conditions(parse, (Node *) parse->jointree);
172 parse->havingQual = preprocess_expression(parse, parse->havingQual,
175 /* Also need to preprocess expressions for function RTEs */
176 foreach(lst, parse->rtable)
178 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lst);
180 if (rte->rtekind == RTE_FUNCTION)
181 rte->funcexpr = preprocess_expression(parse, rte->funcexpr,
183 /* These are not targetlist items, but close enough... */
187 * Check for ungrouped variables passed to subplans in targetlist and
188 * HAVING clause (but not in WHERE or JOIN/ON clauses, since those are
189 * evaluated before grouping). We can't do this any earlier because
190 * we must use the preprocessed targetlist for comparisons of grouped
193 if (parse->hasSubLinks &&
194 (parse->groupClause != NIL || parse->hasAggs))
195 check_subplans_for_ungrouped_vars(parse);
198 * A HAVING clause without aggregates is equivalent to a WHERE clause
199 * (except it can only refer to grouped fields). Transfer any
200 * agg-free clauses of the HAVING qual into WHERE. This may seem like
201 * wasting cycles to cater to stupidly-written queries, but there are
202 * other reasons for doing it. Firstly, if the query contains no aggs
203 * at all, then we aren't going to generate an Agg plan node, and so
204 * there'll be no place to execute HAVING conditions; without this
205 * transfer, we'd lose the HAVING condition entirely, which is wrong.
206 * Secondly, when we push down a qual condition into a sub-query, it's
207 * easiest to push the qual into HAVING always, in case it contains
208 * aggs, and then let this code sort it out.
210 * Note that both havingQual and parse->jointree->quals are in
211 * implicitly-ANDed-list form at this point, even though they are
212 * declared as Node *. Also note that contain_agg_clause does not
213 * recurse into sub-selects, which is exactly what we need here.
216 foreach(lst, (List *) parse->havingQual)
218 Node *havingclause = (Node *) lfirst(lst);
220 if (contain_agg_clause(havingclause))
221 newHaving = lappend(newHaving, havingclause);
223 parse->jointree->quals = (Node *)
224 lappend((List *) parse->jointree->quals, havingclause);
226 parse->havingQual = (Node *) newHaving;
229 * Do the main planning. If we have an inherited target relation,
230 * that needs special processing, else go straight to
233 if (parse->resultRelation &&
234 (lst = expand_inherted_rtentry(parse, parse->resultRelation, false))
236 plan = inheritance_planner(parse, lst);
238 plan = grouping_planner(parse, tuple_fraction);
241 * If any subplans were generated, or if we're inside a subplan, build
242 * subPlan, extParam and locParam lists for plan nodes.
244 if (PlannerPlanId != saved_planid || PlannerQueryLevel > 1)
246 (void) SS_finalize_plan(plan, parse->rtable);
249 * At the moment, SS_finalize_plan doesn't handle initPlans and so
250 * we assign them to the topmost plan node.
252 plan->initPlan = PlannerInitPlan;
253 /* Must add the initPlans' extParams to the topmost node's, too */
254 foreach(lst, plan->initPlan)
256 SubPlan *subplan = (SubPlan *) lfirst(lst);
258 plan->extParam = set_unioni(plan->extParam,
259 subplan->plan->extParam);
263 /* Return to outer subquery context */
265 PlannerInitPlan = saved_initplan;
266 /* we do NOT restore PlannerPlanId; that's not an oversight! */
273 * Look for subqueries in the rangetable that can be pulled up into
274 * the parent query. If the subquery has no special features like
275 * grouping/aggregation then we can merge it into the parent's jointree.
277 * below_outer_join is true if this jointree node is within the nullable
278 * side of an outer join. This restricts what we can do.
280 * A tricky aspect of this code is that if we pull up a subquery we have
281 * to replace Vars that reference the subquery's outputs throughout the
282 * parent query, including quals attached to jointree nodes above the one
283 * we are currently processing! We handle this by being careful not to
284 * change the jointree structure while recursing: no nodes other than
285 * subquery RangeTblRef entries will be replaced. Also, we can't turn
286 * ResolveNew loose on the whole jointree, because it'll return a mutated
287 * copy of the tree; we have to invoke it just on the quals, instead.
290 pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
294 if (IsA(jtnode, RangeTblRef))
296 int varno = ((RangeTblRef *) jtnode)->rtindex;
297 RangeTblEntry *rte = rt_fetch(varno, parse->rtable);
298 Query *subquery = rte->subquery;
301 * Is this a subquery RTE, and if so, is the subquery simple
302 * enough to pull up? (If not, do nothing at this node.)
304 * If we are inside an outer join, only pull up subqueries whose
305 * targetlists are nullable --- otherwise substituting their tlist
306 * entries for upper Var references would do the wrong thing (the
307 * results wouldn't become NULL when they're supposed to). XXX
308 * This could be improved by generating pseudo-variables for such
309 * expressions; we'd have to figure out how to get the pseudo-
310 * variables evaluated at the right place in the modified plan
311 * tree. Fix it someday.
313 * Note: even if the subquery itself is simple enough, we can't pull
314 * it up if there is a reference to its whole tuple result.
315 * Perhaps a pseudo-variable is the answer here too.
317 if (rte->rtekind == RTE_SUBQUERY && is_simple_subquery(subquery) &&
318 (!below_outer_join || has_nullable_targetlist(subquery)) &&
319 !contain_whole_tuple_var((Node *) parse, varno, 0))
326 * First, recursively pull up the subquery's subqueries, so
327 * that this routine's processing is complete for its jointree
328 * and rangetable. NB: if the same subquery is referenced
329 * from multiple jointree items (which can't happen normally,
330 * but might after rule rewriting), then we will invoke this
331 * processing multiple times on that subquery. OK because
332 * nothing will happen after the first time. We do have to be
333 * careful to copy everything we pull up, however, or risk
334 * having chunks of structure multiply linked.
336 subquery->jointree = (FromExpr *)
337 pull_up_subqueries(subquery, (Node *) subquery->jointree,
341 * Now make a modifiable copy of the subquery that we can run
342 * OffsetVarNodes and IncrementVarSublevelsUp on.
344 subquery = copyObject(subquery);
347 * Adjust level-0 varnos in subquery so that we can append its
348 * rangetable to upper query's.
350 rtoffset = length(parse->rtable);
351 OffsetVarNodes((Node *) subquery, rtoffset, 0);
354 * Upper-level vars in subquery are now one level closer to their
355 * parent than before.
357 IncrementVarSublevelsUp((Node *) subquery, -1, 1);
360 * Replace all of the top query's references to the subquery's
361 * outputs with copies of the adjusted subtlist items, being
362 * careful not to replace any of the jointree structure.
363 * (This'd be a lot cleaner if we could use
364 * query_tree_mutator.)
366 subtlist = subquery->targetList;
367 parse->targetList = (List *)
368 ResolveNew((Node *) parse->targetList,
369 varno, 0, subtlist, CMD_SELECT, 0);
370 resolvenew_in_jointree((Node *) parse->jointree, varno, subtlist);
371 Assert(parse->setOperations == NULL);
373 ResolveNew(parse->havingQual,
374 varno, 0, subtlist, CMD_SELECT, 0);
376 foreach(rt, parse->rtable)
378 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
380 if (rte->rtekind == RTE_JOIN)
381 rte->joinaliasvars = (List *)
382 ResolveNew((Node *) rte->joinaliasvars,
383 varno, 0, subtlist, CMD_SELECT, 0);
387 * Now append the adjusted rtable entries to upper query. (We
388 * hold off until after fixing the upper rtable entries; no
389 * point in running that code on the subquery ones too.)
391 parse->rtable = nconc(parse->rtable, subquery->rtable);
394 * Pull up any FOR UPDATE markers, too. (OffsetVarNodes
395 * already adjusted the marker values, so just nconc the
398 parse->rowMarks = nconc(parse->rowMarks, subquery->rowMarks);
401 * Miscellaneous housekeeping.
403 parse->hasSubLinks |= subquery->hasSubLinks;
404 /* subquery won't be pulled up if it hasAggs, so no work there */
407 * Return the adjusted subquery jointree to replace the
408 * RangeTblRef entry in my jointree.
410 return (Node *) subquery->jointree;
413 else if (IsA(jtnode, FromExpr))
415 FromExpr *f = (FromExpr *) jtnode;
418 foreach(l, f->fromlist)
419 lfirst(l) = pull_up_subqueries(parse, lfirst(l),
422 else if (IsA(jtnode, JoinExpr))
424 JoinExpr *j = (JoinExpr *) jtnode;
426 /* Recurse, being careful to tell myself when inside outer join */
430 j->larg = pull_up_subqueries(parse, j->larg,
432 j->rarg = pull_up_subqueries(parse, j->rarg,
436 j->larg = pull_up_subqueries(parse, j->larg,
438 j->rarg = pull_up_subqueries(parse, j->rarg,
442 j->larg = pull_up_subqueries(parse, j->larg,
444 j->rarg = pull_up_subqueries(parse, j->rarg,
448 j->larg = pull_up_subqueries(parse, j->larg,
450 j->rarg = pull_up_subqueries(parse, j->rarg,
456 * This is where we fail if upper levels of planner
457 * haven't rewritten UNION JOIN as an Append ...
459 elog(ERROR, "UNION JOIN is not implemented yet");
462 elog(ERROR, "pull_up_subqueries: unexpected join type %d",
468 elog(ERROR, "pull_up_subqueries: unexpected node type %d",
475 * Check a subquery in the range table to see if it's simple enough
476 * to pull up into the parent query.
479 is_simple_subquery(Query *subquery)
482 * Let's just make sure it's a valid subselect ...
484 if (!IsA(subquery, Query) ||
485 subquery->commandType != CMD_SELECT ||
486 subquery->resultRelation != 0 ||
487 subquery->into != NULL ||
489 elog(ERROR, "is_simple_subquery: subquery is bogus");
492 * Can't currently pull up a query with setops. Maybe after querytree
495 if (subquery->setOperations)
499 * Can't pull up a subquery involving grouping, aggregation, sorting,
502 if (subquery->hasAggs ||
503 subquery->groupClause ||
504 subquery->havingQual ||
505 subquery->sortClause ||
506 subquery->distinctClause ||
507 subquery->limitOffset ||
508 subquery->limitCount)
512 * Don't pull up a subquery that has any set-returning functions in
513 * its targetlist. Otherwise we might well wind up inserting
514 * set-returning functions into places where they mustn't go, such as
515 * quals of higher queries.
517 if (expression_returns_set((Node *) subquery->targetList))
521 * Hack: don't try to pull up a subquery with an empty jointree.
522 * query_planner() will correctly generate a Result plan for a
523 * jointree that's totally empty, but I don't think the right things
524 * happen if an empty FromExpr appears lower down in a jointree. Not
525 * worth working hard on this, just to collapse SubqueryScan/Result
528 if (subquery->jointree->fromlist == NIL)
535 * has_nullable_targetlist
536 * Check a subquery in the range table to see if all the non-junk
537 * targetlist items are simple variables (and, hence, will correctly
538 * go to NULL when examined above the point of an outer join).
540 * A possible future extension is to accept strict functions of simple
541 * variables, eg, "x + 1".
544 has_nullable_targetlist(Query *subquery)
548 foreach(l, subquery->targetList)
550 TargetEntry *tle = (TargetEntry *) lfirst(l);
552 /* ignore resjunk columns */
553 if (tle->resdom->resjunk)
556 /* Okay if tlist item is a simple Var */
557 if (tle->expr && IsA(tle->expr, Var))
566 * Helper routine for pull_up_subqueries: do ResolveNew on every expression
567 * in the jointree, without changing the jointree structure itself. Ugly,
568 * but there's no other way...
571 resolvenew_in_jointree(Node *jtnode, int varno, List *subtlist)
575 if (IsA(jtnode, RangeTblRef))
577 /* nothing to do here */
579 else if (IsA(jtnode, FromExpr))
581 FromExpr *f = (FromExpr *) jtnode;
584 foreach(l, f->fromlist)
585 resolvenew_in_jointree(lfirst(l), varno, subtlist);
586 f->quals = ResolveNew(f->quals,
587 varno, 0, subtlist, CMD_SELECT, 0);
589 else if (IsA(jtnode, JoinExpr))
591 JoinExpr *j = (JoinExpr *) jtnode;
593 resolvenew_in_jointree(j->larg, varno, subtlist);
594 resolvenew_in_jointree(j->rarg, varno, subtlist);
595 j->quals = ResolveNew(j->quals,
596 varno, 0, subtlist, CMD_SELECT, 0);
599 * We don't bother to update the colvars list, since it won't be
604 elog(ERROR, "resolvenew_in_jointree: unexpected node type %d",
609 * preprocess_jointree
610 * Attempt to simplify a query's jointree.
612 * If we succeed in pulling up a subquery then we might form a jointree
613 * in which a FromExpr is a direct child of another FromExpr. In that
614 * case we can consider collapsing the two FromExprs into one. This is
615 * an optional conversion, since the planner will work correctly either
616 * way. But we may find a better plan (at the cost of more planning time)
617 * if we merge the two nodes.
619 * NOTE: don't try to do this in the same jointree scan that does subquery
620 * pullup! Since we're changing the jointree structure here, that wouldn't
621 * work reliably --- see comments for pull_up_subqueries().
624 preprocess_jointree(Query *parse, Node *jtnode)
628 if (IsA(jtnode, RangeTblRef))
630 /* nothing to do here... */
632 else if (IsA(jtnode, FromExpr))
634 FromExpr *f = (FromExpr *) jtnode;
638 foreach(l, f->fromlist)
640 Node *child = (Node *) lfirst(l);
642 /* Recursively simplify the child... */
643 child = preprocess_jointree(parse, child);
644 /* Now, is it a FromExpr? */
645 if (child && IsA(child, FromExpr))
648 * Yes, so do we want to merge it into parent? Always do
649 * so if child has just one element (since that doesn't
650 * make the parent's list any longer). Otherwise we have
651 * to be careful about the increase in planning time
652 * caused by combining the two join search spaces into
653 * one. Our heuristic is to merge if the merge will
654 * produce a join list no longer than GEQO_RELS/2.
655 * (Perhaps need an additional user parameter?)
657 FromExpr *subf = (FromExpr *) child;
658 int childlen = length(subf->fromlist);
659 int myothers = length(newlist) + length(lnext(l));
661 if (childlen <= 1 || (childlen + myothers) <= geqo_rels / 2)
663 newlist = nconc(newlist, subf->fromlist);
664 f->quals = make_and_qual(subf->quals, f->quals);
667 newlist = lappend(newlist, child);
670 newlist = lappend(newlist, child);
672 f->fromlist = newlist;
674 else if (IsA(jtnode, JoinExpr))
676 JoinExpr *j = (JoinExpr *) jtnode;
678 /* Can't usefully change the JoinExpr, but recurse on children */
679 j->larg = preprocess_jointree(parse, j->larg);
680 j->rarg = preprocess_jointree(parse, j->rarg);
683 elog(ERROR, "preprocess_jointree: unexpected node type %d",
689 * preprocess_expression
690 * Do subquery_planner's preprocessing work for an expression,
691 * which can be a targetlist, a WHERE clause (including JOIN/ON
692 * conditions), or a HAVING clause.
695 preprocess_expression(Query *parse, Node *expr, int kind)
701 * Simplify constant expressions.
703 * Note that at this point quals have not yet been converted to
704 * implicit-AND form, so we can apply eval_const_expressions directly.
705 * Also note that we need to do this before SS_process_sublinks,
706 * because that routine inserts bogus "Const" nodes.
708 expr = eval_const_expressions(expr);
711 * If it's a qual or havingQual, canonicalize it, and convert it to
712 * implicit-AND format.
714 * XXX Is there any value in re-applying eval_const_expressions after
717 if (kind != EXPRKIND_TARGET)
719 expr = (Node *) canonicalize_qual((Expr *) expr, true);
721 #ifdef OPTIMIZER_DEBUG
722 printf("After canonicalize_qual()\n");
727 /* Expand SubLinks to SubPlans */
728 if (parse->hasSubLinks)
729 expr = SS_process_sublinks(expr);
731 /* Replace uplevel vars with Param nodes */
732 if (PlannerQueryLevel > 1)
733 expr = SS_replace_correlation_vars(expr);
736 * If the query has any join RTEs, try to replace join alias variables
737 * with base-relation variables, to allow quals to be pushed down. We
738 * must do this after sublink processing, since it does not recurse
741 * The flattening pass is expensive enough that it seems worthwhile to
742 * scan the rangetable to see if we can avoid it.
744 has_join_rtes = false;
745 foreach(rt, parse->rtable)
747 RangeTblEntry *rte = lfirst(rt);
749 if (rte->rtekind == RTE_JOIN)
751 has_join_rtes = true;
756 expr = flatten_join_alias_vars(expr, parse->rtable, false);
762 * preprocess_qual_conditions
763 * Recursively scan the query's jointree and do subquery_planner's
764 * preprocessing work on each qual condition found therein.
767 preprocess_qual_conditions(Query *parse, Node *jtnode)
771 if (IsA(jtnode, RangeTblRef))
773 /* nothing to do here */
775 else if (IsA(jtnode, FromExpr))
777 FromExpr *f = (FromExpr *) jtnode;
780 foreach(l, f->fromlist)
781 preprocess_qual_conditions(parse, lfirst(l));
783 f->quals = preprocess_expression(parse, f->quals, EXPRKIND_WHERE);
785 else if (IsA(jtnode, JoinExpr))
787 JoinExpr *j = (JoinExpr *) jtnode;
789 preprocess_qual_conditions(parse, j->larg);
790 preprocess_qual_conditions(parse, j->rarg);
792 j->quals = preprocess_expression(parse, j->quals, EXPRKIND_WHERE);
795 elog(ERROR, "preprocess_qual_conditions: unexpected node type %d",
799 /*--------------------
800 * inheritance_planner
801 * Generate a plan in the case where the result relation is an
804 * We have to handle this case differently from cases where a source
805 * relation is an inheritance set. Source inheritance is expanded at
806 * the bottom of the plan tree (see allpaths.c), but target inheritance
807 * has to be expanded at the top. The reason is that for UPDATE, each
808 * target relation needs a different targetlist matching its own column
809 * set. (This is not so critical for DELETE, but for simplicity we treat
810 * inherited DELETE the same way.) Fortunately, the UPDATE/DELETE target
811 * can never be the nullable side of an outer join, so it's OK to generate
814 * parse is the querytree produced by the parser & rewriter.
815 * inheritlist is an integer list of RT indexes for the result relation set.
817 * Returns a query plan.
818 *--------------------
821 inheritance_planner(Query *parse, List *inheritlist)
823 int parentRTindex = parse->resultRelation;
824 Oid parentOID = getrelid(parentRTindex, parse->rtable);
825 List *subplans = NIL;
829 foreach(l, inheritlist)
831 int childRTindex = lfirsti(l);
832 Oid childOID = getrelid(childRTindex, parse->rtable);
836 /* Generate modified query with this rel as target */
837 subquery = (Query *) adjust_inherited_attrs((Node *) parse,
838 parentRTindex, parentOID,
839 childRTindex, childOID);
841 subplan = grouping_planner(subquery, 0.0 /* retrieve all tuples */ );
842 subplans = lappend(subplans, subplan);
843 /* Save preprocessed tlist from first rel for use in Append */
845 tlist = subplan->targetlist;
848 /* Save the target-relations list for the executor, too */
849 parse->resultRelations = inheritlist;
851 return (Plan *) make_append(subplans, true, tlist);
854 /*--------------------
856 * Perform planning steps related to grouping, aggregation, etc.
857 * This primarily means adding top-level processing to the basic
858 * query plan produced by query_planner.
860 * parse is the querytree produced by the parser & rewriter.
861 * tuple_fraction is the fraction of tuples we expect will be retrieved
863 * tuple_fraction is interpreted as follows:
864 * < 0: determine fraction by inspection of query (normal case)
865 * 0: expect all tuples to be retrieved
866 * 0 < tuple_fraction < 1: expect the given fraction of tuples available
867 * from the plan to be retrieved
868 * tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
869 * expected to be retrieved (ie, a LIMIT specification)
870 * The normal case is to pass -1, but some callers pass values >= 0 to
871 * override this routine's determination of the appropriate fraction.
873 * Returns a query plan.
874 *--------------------
877 grouping_planner(Query *parse, double tuple_fraction)
879 List *tlist = parse->targetList;
881 List *current_pathkeys;
884 if (parse->setOperations)
887 * Construct the plan for set operations. The result will not
888 * need any work except perhaps a top-level sort and/or LIMIT.
890 result_plan = plan_set_operations(parse);
893 * We should not need to call preprocess_targetlist, since we must
894 * be in a SELECT query node. Instead, use the targetlist
895 * returned by plan_set_operations (since this tells whether it
896 * returned any resjunk columns!), and transfer any sort key
897 * information from the original tlist.
899 Assert(parse->commandType == CMD_SELECT);
901 tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);
904 * Can't handle FOR UPDATE here (parser should have checked
905 * already, but let's make sure).
908 elog(ERROR, "SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT");
911 * We set current_pathkeys NIL indicating we do not know sort
912 * order. This is correct when the top set operation is UNION
913 * ALL, since the appended-together results are unsorted even if
914 * the subplans were sorted. For other set operations we could be
915 * smarter --- room for future improvement!
917 current_pathkeys = NIL;
920 * Calculate pathkeys that represent ordering requirements
922 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
924 sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
928 /* No set operations, do regular planning */
930 List *group_pathkeys;
931 AttrNumber *groupColIdx = NULL;
934 bool use_hashed_grouping = false;
936 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
937 tlist = preprocess_targetlist(tlist,
939 parse->resultRelation,
943 * Add TID targets for rels selected FOR UPDATE (should this be
944 * done in preprocess_targetlist?). The executor uses the TID to
945 * know which rows to lock, much as for UPDATE or DELETE.
952 * We've got trouble if the FOR UPDATE appears inside
953 * grouping, since grouping renders a reference to individual
954 * tuple CTIDs invalid. This is also checked at parse time,
955 * but that's insufficient because of rule substitution, query
958 CheckSelectForUpdate(parse);
961 * Currently the executor only supports FOR UPDATE at top
964 if (PlannerQueryLevel > 1)
965 elog(ERROR, "SELECT FOR UPDATE is not allowed in subselects");
967 foreach(l, parse->rowMarks)
969 Index rti = lfirsti(l);
975 resname = (char *) palloc(32);
976 snprintf(resname, 32, "ctid%u", rti);
977 resdom = makeResdom(length(tlist) + 1,
984 SelfItemPointerAttributeNumber,
989 ctid = makeTargetEntry(resdom, (Node *) var);
990 tlist = lappend(tlist, ctid);
995 * Generate appropriate target list for subplan; may be different
996 * from tlist if grouping or aggregation is needed.
998 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
1001 * Calculate pathkeys that represent grouping/ordering
1004 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
1006 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
1010 * Figure out whether we need a sorted result from query_planner.
1012 * If we have a GROUP BY clause, then we want a result sorted
1013 * properly for grouping. Otherwise, if there is an ORDER BY
1014 * clause, we want to sort by the ORDER BY clause. (Note: if we
1015 * have both, and ORDER BY is a superset of GROUP BY, it would be
1016 * tempting to request sort by ORDER BY --- but that might just
1017 * leave us failing to exploit an available sort order at all.
1018 * Needs more thought...)
1020 if (parse->groupClause)
1021 parse->query_pathkeys = group_pathkeys;
1022 else if (parse->sortClause)
1023 parse->query_pathkeys = sort_pathkeys;
1025 parse->query_pathkeys = NIL;
1028 * Figure out whether we expect to retrieve all the tuples that
1029 * the plan can generate, or to stop early due to outside factors
1030 * such as a cursor. If the caller passed a value >= 0, believe
1031 * that value, else do our own examination of the query context.
1033 if (tuple_fraction < 0.0)
1035 /* Initial assumption is we need all the tuples */
1036 tuple_fraction = 0.0;
1039 * Check for retrieve-into-portal, ie DECLARE CURSOR.
1041 * We have no real idea how many tuples the user will ultimately
1042 * FETCH from a cursor, but it seems a good bet that he
1043 * doesn't want 'em all. Optimize for 10% retrieval (you
1044 * gotta better number? Should this be a SETtable parameter?)
1046 if (parse->isPortal)
1047 tuple_fraction = 0.10;
1051 * Adjust tuple_fraction if we see that we are going to apply
1052 * limiting/grouping/aggregation/etc. This is not overridable by
1053 * the caller, since it reflects plan actions that this routine
1054 * will certainly take, not assumptions about context.
1056 if (parse->limitCount != NULL)
1059 * A LIMIT clause limits the absolute number of tuples
1060 * returned. However, if it's not a constant LIMIT then we
1061 * have to punt; for lack of a better idea, assume 10% of the
1062 * plan's result is wanted.
1064 double limit_fraction = 0.0;
1066 if (IsA(parse->limitCount, Const))
1068 Const *limitc = (Const *) parse->limitCount;
1069 int32 count = DatumGetInt32(limitc->constvalue);
1072 * A NULL-constant LIMIT represents "LIMIT ALL", which we
1073 * treat the same as no limit (ie, expect to retrieve all
1076 if (!limitc->constisnull && count > 0)
1078 limit_fraction = (double) count;
1079 /* We must also consider the OFFSET, if present */
1080 if (parse->limitOffset != NULL)
1082 if (IsA(parse->limitOffset, Const))
1086 limitc = (Const *) parse->limitOffset;
1087 offset = DatumGetInt32(limitc->constvalue);
1088 if (!limitc->constisnull && offset > 0)
1089 limit_fraction += (double) offset;
1093 /* OFFSET is an expression ... punt ... */
1094 limit_fraction = 0.10;
1101 /* LIMIT is an expression ... punt ... */
1102 limit_fraction = 0.10;
1105 if (limit_fraction > 0.0)
1108 * If we have absolute limits from both caller and LIMIT,
1109 * use the smaller value; if one is fractional and the
1110 * other absolute, treat the fraction as a fraction of the
1111 * absolute value; else we can multiply the two fractions
1114 if (tuple_fraction >= 1.0)
1116 if (limit_fraction >= 1.0)
1119 tuple_fraction = Min(tuple_fraction, limit_fraction);
1123 /* caller absolute, limit fractional */
1124 tuple_fraction *= limit_fraction;
1125 if (tuple_fraction < 1.0)
1126 tuple_fraction = 1.0;
1129 else if (tuple_fraction > 0.0)
1131 if (limit_fraction >= 1.0)
1133 /* caller fractional, limit absolute */
1134 tuple_fraction *= limit_fraction;
1135 if (tuple_fraction < 1.0)
1136 tuple_fraction = 1.0;
1140 /* both fractional */
1141 tuple_fraction *= limit_fraction;
1146 /* no info from caller, just use limit */
1147 tuple_fraction = limit_fraction;
1152 if (parse->groupClause)
1155 * In GROUP BY mode, we have the little problem that we don't
1156 * really know how many input tuples will be needed to make a
1157 * group, so we can't translate an output LIMIT count into an
1158 * input count. For lack of a better idea, assume 25% of the
1159 * input data will be processed if there is any output limit.
1160 * However, if the caller gave us a fraction rather than an
1161 * absolute count, we can keep using that fraction (which
1162 * amounts to assuming that all the groups are about the same
1165 if (tuple_fraction >= 1.0)
1166 tuple_fraction = 0.25;
1169 * If both GROUP BY and ORDER BY are specified, we will need
1170 * two levels of sort --- and, therefore, certainly need to
1171 * read all the input tuples --- unless ORDER BY is a subset
1172 * of GROUP BY. (We have not yet canonicalized the pathkeys,
1173 * so must use the slower noncanonical comparison method.)
1175 if (parse->groupClause && parse->sortClause &&
1176 !noncanonical_pathkeys_contained_in(sort_pathkeys,
1178 tuple_fraction = 0.0;
1180 else if (parse->hasAggs)
1183 * Ungrouped aggregate will certainly want all the input
1186 tuple_fraction = 0.0;
1188 else if (parse->distinctClause)
1191 * SELECT DISTINCT, like GROUP, will absorb an unpredictable
1192 * number of input tuples per output tuple. Handle the same
1195 if (tuple_fraction >= 1.0)
1196 tuple_fraction = 0.25;
1200 * Generate the best unsorted and presorted paths for this Query
1201 * (but note there may not be any presorted path).
1203 query_planner(parse, sub_tlist, tuple_fraction,
1204 &cheapest_path, &sorted_path);
1207 * We couldn't canonicalize group_pathkeys and sort_pathkeys before
1208 * running query_planner(), so do it now.
1210 group_pathkeys = canonicalize_pathkeys(parse, group_pathkeys);
1211 sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
1214 * Consider whether we might want to use hashed grouping.
1216 if (parse->groupClause)
1219 * Executor doesn't support hashed aggregation with DISTINCT
1220 * aggregates. (Doing so would imply storing *all* the input
1221 * values in the hash table, which seems like a certain loser.)
1223 if (parse->hasAggs &&
1224 (contain_distinct_agg_clause((Node *) tlist) ||
1225 contain_distinct_agg_clause(parse->havingQual)))
1226 use_hashed_grouping = false;
1229 #if 0 /* much more to do here */
1230 /* TEMPORARY HOTWIRE FOR TESTING */
1231 use_hashed_grouping = true;
1237 * Select the best path and create a plan to execute it.
1239 * If no special sort order is wanted, or if the cheapest path is
1240 * already appropriately ordered, use the cheapest path.
1241 * Otherwise, look to see if we have an already-ordered path that is
1242 * cheaper than doing an explicit sort on the cheapest-total-cost
1245 if (parse->query_pathkeys == NIL ||
1246 pathkeys_contained_in(parse->query_pathkeys,
1247 cheapest_path->pathkeys))
1249 result_plan = create_plan(parse, cheapest_path);
1250 current_pathkeys = cheapest_path->pathkeys;
1252 else if (sorted_path)
1254 Path sort_path; /* dummy for result of cost_sort */
1256 cost_sort(&sort_path, parse, parse->query_pathkeys,
1257 sorted_path->parent->rows, sorted_path->parent->width);
1258 sort_path.startup_cost += cheapest_path->total_cost;
1259 sort_path.total_cost += cheapest_path->total_cost;
1260 if (compare_fractional_path_costs(sorted_path, &sort_path,
1261 tuple_fraction) <= 0)
1263 /* Presorted path is cheaper, use it */
1264 result_plan = create_plan(parse, sorted_path);
1265 current_pathkeys = sorted_path->pathkeys;
1269 /* otherwise, doing it the hard way is still cheaper */
1270 result_plan = create_plan(parse, cheapest_path);
1271 current_pathkeys = cheapest_path->pathkeys;
1277 * No sorted path, so we must use the cheapest-total path.
1278 * The actual sort step will be generated below.
1280 result_plan = create_plan(parse, cheapest_path);
1281 current_pathkeys = cheapest_path->pathkeys;
1285 * create_plan() returns a plan with just a "flat" tlist of required
1286 * Vars. We want to insert the sub_tlist as the tlist of the top
1287 * plan node. If the top-level plan node is one that cannot do
1288 * expression evaluation, we must insert a Result node to project the
1290 * Currently, the only plan node we might see here that falls into
1291 * that category is Append.
1293 if (IsA(result_plan, Append))
1295 result_plan = (Plan *) make_result(sub_tlist, NULL, result_plan);
1300 * Otherwise, just replace the flat tlist with the desired tlist.
1302 result_plan->targetlist = sub_tlist;
1306 * Insert AGG or GROUP node if needed, plus an explicit sort step
1309 * HAVING clause, if any, becomes qual of the Agg node
1311 if (use_hashed_grouping)
1313 /* Hashed aggregate plan --- no sort needed */
1314 result_plan = (Plan *) make_agg(tlist,
1315 (List *) parse->havingQual,
1317 length(parse->groupClause),
1320 /* Hashed aggregation produces randomly-ordered results */
1321 current_pathkeys = NIL;
1323 else if (parse->hasAggs)
1325 /* Plain aggregate plan --- sort if needed */
1326 AggStrategy aggstrategy;
1328 if (parse->groupClause)
1330 if (!pathkeys_contained_in(group_pathkeys, current_pathkeys))
1332 result_plan = make_groupsortplan(parse,
1336 current_pathkeys = group_pathkeys;
1338 aggstrategy = AGG_SORTED;
1340 * The AGG node will not change the sort ordering of its
1341 * groups, so current_pathkeys describes the result too.
1346 aggstrategy = AGG_PLAIN;
1347 /* Result will be only one row anyway; no sort order */
1348 current_pathkeys = NIL;
1351 result_plan = (Plan *) make_agg(tlist,
1352 (List *) parse->havingQual,
1354 length(parse->groupClause),
1361 * If there are no Aggs, we shouldn't have any HAVING qual anymore
1363 Assert(parse->havingQual == NULL);
1366 * If we have a GROUP BY clause, insert a group node (plus the
1367 * appropriate sort node, if necessary).
1369 if (parse->groupClause)
1372 * Add an explicit sort if we couldn't make the path come out
1373 * the way the GROUP node needs it.
1375 if (!pathkeys_contained_in(group_pathkeys, current_pathkeys))
1377 result_plan = make_groupsortplan(parse,
1381 current_pathkeys = group_pathkeys;
1384 result_plan = (Plan *) make_group(tlist,
1385 length(parse->groupClause),
1390 } /* end of if (setOperations) */
1393 * If we were not able to make the plan come out in the right order,
1394 * add an explicit sort step.
1396 if (parse->sortClause)
1398 if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys))
1399 result_plan = make_sortplan(parse, tlist, result_plan,
1404 * If there is a DISTINCT clause, add the UNIQUE node.
1406 if (parse->distinctClause)
1408 result_plan = (Plan *) make_unique(tlist, result_plan,
1409 parse->distinctClause);
1413 * Finally, if there is a LIMIT/OFFSET clause, add the LIMIT node.
1415 if (parse->limitOffset || parse->limitCount)
1417 result_plan = (Plan *) make_limit(tlist, result_plan,
1426 * make_subplanTargetList
1427 * Generate appropriate target list when grouping is required.
1429 * When grouping_planner inserts Aggregate or Group plan nodes above
1430 * the result of query_planner, we typically want to pass a different
1431 * target list to query_planner than the outer plan nodes should have.
1432 * This routine generates the correct target list for the subplan.
1434 * The initial target list passed from the parser already contains entries
1435 * for all ORDER BY and GROUP BY expressions, but it will not have entries
1436 * for variables used only in HAVING clauses; so we need to add those
1437 * variables to the subplan target list. Also, if we are doing either
1438 * grouping or aggregation, we flatten all expressions except GROUP BY items
1439 * into their component variables; the other expressions will be computed by
1440 * the inserted nodes rather than by the subplan. For example,
1441 * given a query like
1442 * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
1443 * we want to pass this targetlist to the subplan:
1445 * where the a+b target will be used by the Sort/Group steps, and the
1446 * other targets will be used for computing the final results. (In the
1447 * above example we could theoretically suppress the a and b targets and
1448 * pass down only c,d,a+b, but it's not really worth the trouble to
1449 * eliminate simple var references from the subplan. We will avoid doing
1450 * the extra computation to recompute a+b at the outer level; see
1451 * replace_vars_with_subplan_refs() in setrefs.c.)
1453 * 'parse' is the query being processed.
1454 * 'tlist' is the query's target list.
1455 * 'groupColIdx' receives an array of column numbers for the GROUP BY
1456 * expressions (if there are any) in the subplan's target list.
1458 * The result is the targetlist to be passed to the subplan.
1462 make_subplanTargetList(Query *parse,
1464 AttrNumber **groupColIdx)
1470 *groupColIdx = NULL;
1473 * If we're not grouping or aggregating, nothing to do here;
1474 * query_planner should receive the unmodified target list.
1476 if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
1480 * Otherwise, start with a "flattened" tlist (having just the vars
1481 * mentioned in the targetlist and HAVING qual --- but not upper-
1482 * level Vars; they will be replaced by Params later on).
1484 sub_tlist = flatten_tlist(tlist);
1485 extravars = pull_var_clause(parse->havingQual, false);
1486 sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
1487 freeList(extravars);
1490 * If grouping, create sub_tlist entries for all GROUP BY expressions
1491 * (GROUP BY items that are simple Vars should be in the list
1492 * already), and make an array showing where the group columns are in
1495 numCols = length(parse->groupClause);
1499 AttrNumber *grpColIdx;
1502 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
1503 *groupColIdx = grpColIdx;
1505 foreach(gl, parse->groupClause)
1507 GroupClause *grpcl = (GroupClause *) lfirst(gl);
1508 Node *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
1509 TargetEntry *te = NULL;
1512 /* Find or make a matching sub_tlist entry */
1513 foreach(sl, sub_tlist)
1515 te = (TargetEntry *) lfirst(sl);
1516 if (equal(groupexpr, te->expr))
1521 te = makeTargetEntry(makeResdom(length(sub_tlist) + 1,
1522 exprType(groupexpr),
1523 exprTypmod(groupexpr),
1527 sub_tlist = lappend(sub_tlist, te);
1530 /* and save its resno */
1531 grpColIdx[keyno++] = te->resdom->resno;
1539 * make_groupsortplan
1540 * Add a Sort node to explicitly sort according to the GROUP BY clause.
1542 * Note: the Sort node always just takes a copy of the subplan's tlist
1543 * plus ordering information. (This might seem inefficient if the
1544 * subplan contains complex GROUP BY expressions, but in fact Sort
1545 * does not evaluate its targetlist --- it only outputs the same
1546 * tuples in a new order. So the expressions we might be copying
1547 * are just dummies with no extra execution cost.)
1550 make_groupsortplan(Query *parse,
1552 AttrNumber *grpColIdx,
1555 List *sort_tlist = new_unsorted_tlist(subplan->targetlist);
1559 foreach(gl, groupClause)
1561 GroupClause *grpcl = (GroupClause *) lfirst(gl);
1562 TargetEntry *te = nth(grpColIdx[keyno] - 1, sort_tlist);
1563 Resdom *resdom = te->resdom;
1566 * Check for the possibility of duplicate group-by clauses ---
1567 * the parser should have removed 'em, but the Sort executor
1568 * will get terribly confused if any get through!
1570 if (resdom->reskey == 0)
1572 /* OK, insert the ordering info needed by the executor. */
1573 resdom->reskey = ++keyno;
1574 resdom->reskeyop = grpcl->sortop;
1580 return (Plan *) make_sort(parse, sort_tlist, subplan, keyno);
1585 * Add a Sort node to implement an explicit ORDER BY clause.
1588 make_sortplan(Query *parse, List *tlist, Plan *plannode, List *sortcls)
1595 * First make a copy of the tlist so that we don't corrupt the
1598 sort_tlist = new_unsorted_tlist(tlist);
1602 SortClause *sortcl = (SortClause *) lfirst(i);
1603 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sort_tlist);
1604 Resdom *resdom = tle->resdom;
1607 * Check for the possibility of duplicate order-by clauses --- the
1608 * parser should have removed 'em, but the executor will get
1609 * terribly confused if any get through!
1611 if (resdom->reskey == 0)
1613 /* OK, insert the ordering info needed by the executor. */
1614 resdom->reskey = ++keyno;
1615 resdom->reskeyop = sortcl->sortop;
1621 return (Plan *) make_sort(parse, sort_tlist, plannode, keyno);
1625 * postprocess_setop_tlist
1626 * Fix up targetlist returned by plan_set_operations().
1628 * We need to transpose sort key info from the orig_tlist into new_tlist.
1629 * NOTE: this would not be good enough if we supported resjunk sort keys
1630 * for results of set operations --- then, we'd need to project a whole
1631 * new tlist to evaluate the resjunk columns. For now, just elog if we
1632 * find any resjunk columns in orig_tlist.
1635 postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
1639 foreach(l, new_tlist)
1641 TargetEntry *new_tle = (TargetEntry *) lfirst(l);
1642 TargetEntry *orig_tle;
1644 /* ignore resjunk columns in setop result */
1645 if (new_tle->resdom->resjunk)
1648 Assert(orig_tlist != NIL);
1649 orig_tle = (TargetEntry *) lfirst(orig_tlist);
1650 orig_tlist = lnext(orig_tlist);
1651 if (orig_tle->resdom->resjunk)
1652 elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
1653 Assert(new_tle->resdom->resno == orig_tle->resdom->resno);
1654 Assert(new_tle->resdom->restype == orig_tle->resdom->restype);
1655 new_tle->resdom->ressortgroupref = orig_tle->resdom->ressortgroupref;
1657 if (orig_tlist != NIL)
1658 elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");