1 /*-------------------------------------------------------------------------
4 * The query optimizer external interface.
6 * Portions Copyright (c) 1996-2001, 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.117 2002/05/12 23:43:03 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/paths.h"
25 #include "optimizer/planmain.h"
26 #include "optimizer/planner.h"
27 #include "optimizer/prep.h"
28 #include "optimizer/subselect.h"
29 #include "optimizer/tlist.h"
30 #include "optimizer/var.h"
31 #include "parser/analyze.h"
32 #include "parser/parsetree.h"
33 #include "parser/parse_expr.h"
34 #include "rewrite/rewriteManip.h"
35 #include "utils/lsyscache.h"
38 /* Expression kind codes for preprocess_expression */
39 #define EXPRKIND_TARGET 0
40 #define EXPRKIND_WHERE 1
41 #define EXPRKIND_HAVING 2
44 static Node *pull_up_subqueries(Query *parse, Node *jtnode,
45 bool below_outer_join);
46 static bool is_simple_subquery(Query *subquery);
47 static bool has_nullable_targetlist(Query *subquery);
48 static void resolvenew_in_jointree(Node *jtnode, int varno, List *subtlist);
49 static Node *preprocess_jointree(Query *parse, Node *jtnode);
50 static Node *preprocess_expression(Query *parse, Node *expr, int kind);
51 static void preprocess_qual_conditions(Query *parse, Node *jtnode);
52 static Plan *inheritance_planner(Query *parse, List *inheritlist);
53 static Plan *grouping_planner(Query *parse, double tuple_fraction);
54 static List *make_subplanTargetList(Query *parse, List *tlist,
55 AttrNumber **groupColIdx);
56 static Plan *make_groupplan(Query *parse,
57 List *group_tlist, bool tuplePerGroup,
58 List *groupClause, AttrNumber *grpColIdx,
59 bool is_presorted, Plan *subplan);
60 static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
63 /*****************************************************************************
65 * Query optimizer entry point
67 *****************************************************************************/
72 Index save_PlannerQueryLevel;
73 List *save_PlannerParamVar;
76 * The planner can be called recursively (an example is when
77 * eval_const_expressions tries to pre-evaluate an SQL function). So,
78 * these global state variables must be saved and restored.
80 * These vars cannot be moved into the Query structure since their whole
81 * purpose is communication across multiple sub-Queries.
83 * Note we do NOT save and restore PlannerPlanId: it exists to assign
84 * unique IDs to SubPlan nodes, and we want those IDs to be unique for
85 * the life of a backend. Also, PlannerInitPlan is saved/restored in
86 * subquery_planner, not here.
88 save_PlannerQueryLevel = PlannerQueryLevel;
89 save_PlannerParamVar = PlannerParamVar;
91 /* Initialize state for handling outer-level references and params */
92 PlannerQueryLevel = 0; /* will be 1 in top-level subquery_planner */
93 PlannerParamVar = NIL;
95 /* primary planning entry point (may recurse for subqueries) */
96 result_plan = subquery_planner(parse, -1.0 /* default case */ );
98 Assert(PlannerQueryLevel == 0);
100 /* executor wants to know total number of Params used overall */
101 result_plan->nParamExec = length(PlannerParamVar);
103 /* final cleanup of the plan */
104 set_plan_references(parse, result_plan);
106 /* restore state for outer planner, if any */
107 PlannerQueryLevel = save_PlannerQueryLevel;
108 PlannerParamVar = save_PlannerParamVar;
114 /*--------------------
116 * Invokes the planner on a subquery. We recurse to here for each
117 * sub-SELECT found in the query tree.
119 * parse is the querytree produced by the parser & rewriter.
120 * tuple_fraction is the fraction of tuples we expect will be retrieved.
121 * tuple_fraction is interpreted as explained for grouping_planner, below.
123 * Basically, this routine does the stuff that should only be done once
124 * per Query object. It then calls grouping_planner. At one time,
125 * grouping_planner could be invoked recursively on the same Query object;
126 * that's not currently true, but we keep the separation between the two
127 * routines anyway, in case we need it again someday.
129 * subquery_planner will be called recursively to handle sub-Query nodes
130 * found within the query's expressions and rangetable.
132 * Returns a query plan.
133 *--------------------
136 subquery_planner(Query *parse, double tuple_fraction)
138 List *saved_initplan = PlannerInitPlan;
139 int saved_planid = PlannerPlanId;
144 /* Set up for a new level of subquery */
146 PlannerInitPlan = NIL;
148 #ifdef ENABLE_KEY_SET_QUERY
149 /* this should go away sometime soon */
150 transformKeySetQuery(parse);
154 * Check to see if any subqueries in the rangetable can be merged into
157 parse->jointree = (FromExpr *)
158 pull_up_subqueries(parse, (Node *) parse->jointree, false);
161 * If so, we may have created opportunities to simplify the jointree.
163 parse->jointree = (FromExpr *)
164 preprocess_jointree(parse, (Node *) parse->jointree);
167 * Do expression preprocessing on targetlist and quals.
169 parse->targetList = (List *)
170 preprocess_expression(parse, (Node *) parse->targetList,
173 preprocess_qual_conditions(parse, (Node *) parse->jointree);
175 parse->havingQual = preprocess_expression(parse, parse->havingQual,
179 * Check for ungrouped variables passed to subplans in targetlist and
180 * HAVING clause (but not in WHERE or JOIN/ON clauses, since those are
181 * evaluated before grouping). We can't do this any earlier because
182 * we must use the preprocessed targetlist for comparisons of grouped
185 if (parse->hasSubLinks &&
186 (parse->groupClause != NIL || parse->hasAggs))
187 check_subplans_for_ungrouped_vars(parse);
190 * A HAVING clause without aggregates is equivalent to a WHERE clause
191 * (except it can only refer to grouped fields). Transfer any
192 * agg-free clauses of the HAVING qual into WHERE. This may seem like
193 * wasting cycles to cater to stupidly-written queries, but there are
194 * other reasons for doing it. Firstly, if the query contains no aggs
195 * at all, then we aren't going to generate an Agg plan node, and so
196 * there'll be no place to execute HAVING conditions; without this
197 * transfer, we'd lose the HAVING condition entirely, which is wrong.
198 * Secondly, when we push down a qual condition into a sub-query, it's
199 * easiest to push the qual into HAVING always, in case it contains
200 * aggs, and then let this code sort it out.
202 * Note that both havingQual and parse->jointree->quals are in
203 * implicitly-ANDed-list form at this point, even though they are
204 * declared as Node *. Also note that contain_agg_clause does not
205 * recurse into sub-selects, which is exactly what we need here.
208 foreach(lst, (List *) parse->havingQual)
210 Node *havingclause = (Node *) lfirst(lst);
212 if (contain_agg_clause(havingclause))
213 newHaving = lappend(newHaving, havingclause);
215 parse->jointree->quals = (Node *)
216 lappend((List *) parse->jointree->quals, havingclause);
218 parse->havingQual = (Node *) newHaving;
221 * Do the main planning. If we have an inherited target relation,
222 * that needs special processing, else go straight to
225 if (parse->resultRelation &&
226 (lst = expand_inherted_rtentry(parse, parse->resultRelation, false))
228 plan = inheritance_planner(parse, lst);
230 plan = grouping_planner(parse, tuple_fraction);
233 * If any subplans were generated, or if we're inside a subplan, build
234 * subPlan, extParam and locParam lists for plan nodes.
236 if (PlannerPlanId != saved_planid || PlannerQueryLevel > 1)
238 (void) SS_finalize_plan(plan);
241 * At the moment, SS_finalize_plan doesn't handle initPlans and so
242 * we assign them to the topmost plan node.
244 plan->initPlan = PlannerInitPlan;
245 /* Must add the initPlans' extParams to the topmost node's, too */
246 foreach(lst, plan->initPlan)
248 SubPlan *subplan = (SubPlan *) lfirst(lst);
250 plan->extParam = set_unioni(plan->extParam,
251 subplan->plan->extParam);
255 /* Return to outer subquery context */
257 PlannerInitPlan = saved_initplan;
258 /* we do NOT restore PlannerPlanId; that's not an oversight! */
265 * Look for subqueries in the rangetable that can be pulled up into
266 * the parent query. If the subquery has no special features like
267 * grouping/aggregation then we can merge it into the parent's jointree.
269 * below_outer_join is true if this jointree node is within the nullable
270 * side of an outer join. This restricts what we can do.
272 * A tricky aspect of this code is that if we pull up a subquery we have
273 * to replace Vars that reference the subquery's outputs throughout the
274 * parent query, including quals attached to jointree nodes above the one
275 * we are currently processing! We handle this by being careful not to
276 * change the jointree structure while recursing: no nodes other than
277 * subquery RangeTblRef entries will be replaced. Also, we can't turn
278 * ResolveNew loose on the whole jointree, because it'll return a mutated
279 * copy of the tree; we have to invoke it just on the quals, instead.
282 pull_up_subqueries(Query *parse, Node *jtnode, bool below_outer_join)
286 if (IsA(jtnode, RangeTblRef))
288 int varno = ((RangeTblRef *) jtnode)->rtindex;
289 RangeTblEntry *rte = rt_fetch(varno, parse->rtable);
290 Query *subquery = rte->subquery;
293 * Is this a subquery RTE, and if so, is the subquery simple
294 * enough to pull up? (If not, do nothing at this node.)
296 * If we are inside an outer join, only pull up subqueries whose
297 * targetlists are nullable --- otherwise substituting their tlist
298 * entries for upper Var references would do the wrong thing
299 * (the results wouldn't become NULL when they're supposed to).
300 * XXX This could be improved by generating pseudo-variables for
301 * such expressions; we'd have to figure out how to get the pseudo-
302 * variables evaluated at the right place in the modified plan tree.
305 * Note: even if the subquery itself is simple enough, we can't pull
306 * it up if there is a reference to its whole tuple result. Perhaps
307 * a pseudo-variable is the answer here too.
309 if (rte->rtekind == RTE_SUBQUERY && is_simple_subquery(subquery) &&
310 (!below_outer_join || has_nullable_targetlist(subquery)) &&
311 !contain_whole_tuple_var((Node *) parse, varno, 0))
318 * First, recursively pull up the subquery's subqueries, so
319 * that this routine's processing is complete for its jointree
320 * and rangetable. NB: if the same subquery is referenced
321 * from multiple jointree items (which can't happen normally,
322 * but might after rule rewriting), then we will invoke this
323 * processing multiple times on that subquery. OK because
324 * nothing will happen after the first time. We do have to be
325 * careful to copy everything we pull up, however, or risk
326 * having chunks of structure multiply linked.
328 subquery->jointree = (FromExpr *)
329 pull_up_subqueries(subquery, (Node *) subquery->jointree,
333 * Now make a modifiable copy of the subquery that we can
334 * run OffsetVarNodes on.
336 subquery = copyObject(subquery);
339 * Adjust varnos in subquery so that we can append its
340 * rangetable to upper query's.
342 rtoffset = length(parse->rtable);
343 OffsetVarNodes((Node *) subquery, rtoffset, 0);
346 * Replace all of the top query's references to the subquery's
347 * outputs with copies of the adjusted subtlist items, being
348 * careful not to replace any of the jointree structure.
349 * (This'd be a lot cleaner if we could use query_tree_mutator.)
351 subtlist = subquery->targetList;
352 parse->targetList = (List *)
353 ResolveNew((Node *) parse->targetList,
354 varno, 0, subtlist, CMD_SELECT, 0);
355 resolvenew_in_jointree((Node *) parse->jointree, varno, subtlist);
356 Assert(parse->setOperations == NULL);
358 ResolveNew(parse->havingQual,
359 varno, 0, subtlist, CMD_SELECT, 0);
361 foreach(rt, parse->rtable)
363 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
365 if (rte->rtekind == RTE_JOIN)
366 rte->joinaliasvars = (List *)
367 ResolveNew((Node *) rte->joinaliasvars,
368 varno, 0, subtlist, CMD_SELECT, 0);
372 * Now append the adjusted rtable entries to upper query.
373 * (We hold off until after fixing the upper rtable entries;
374 * no point in running that code on the subquery ones too.)
376 parse->rtable = nconc(parse->rtable, subquery->rtable);
379 * Pull up any FOR UPDATE markers, too. (OffsetVarNodes
380 * already adjusted the marker values, so just nconc the list.)
382 parse->rowMarks = nconc(parse->rowMarks, subquery->rowMarks);
385 * Miscellaneous housekeeping.
387 parse->hasSubLinks |= subquery->hasSubLinks;
388 /* subquery won't be pulled up if it hasAggs, so no work there */
391 * Return the adjusted subquery jointree to replace the
392 * RangeTblRef entry in my jointree.
394 return (Node *) subquery->jointree;
397 else if (IsA(jtnode, FromExpr))
399 FromExpr *f = (FromExpr *) jtnode;
402 foreach(l, f->fromlist)
403 lfirst(l) = pull_up_subqueries(parse, lfirst(l),
406 else if (IsA(jtnode, JoinExpr))
408 JoinExpr *j = (JoinExpr *) jtnode;
410 /* Recurse, being careful to tell myself when inside outer join */
414 j->larg = pull_up_subqueries(parse, j->larg,
416 j->rarg = pull_up_subqueries(parse, j->rarg,
420 j->larg = pull_up_subqueries(parse, j->larg,
422 j->rarg = pull_up_subqueries(parse, j->rarg,
426 j->larg = pull_up_subqueries(parse, j->larg,
428 j->rarg = pull_up_subqueries(parse, j->rarg,
432 j->larg = pull_up_subqueries(parse, j->larg,
434 j->rarg = pull_up_subqueries(parse, j->rarg,
440 * This is where we fail if upper levels of planner
441 * haven't rewritten UNION JOIN as an Append ...
443 elog(ERROR, "UNION JOIN is not implemented yet");
446 elog(ERROR, "pull_up_subqueries: unexpected join type %d",
452 elog(ERROR, "pull_up_subqueries: unexpected node type %d",
459 * Check a subquery in the range table to see if it's simple enough
460 * to pull up into the parent query.
463 is_simple_subquery(Query *subquery)
466 * Let's just make sure it's a valid subselect ...
468 if (!IsA(subquery, Query) ||
469 subquery->commandType != CMD_SELECT ||
470 subquery->resultRelation != 0 ||
471 subquery->into != NULL ||
473 elog(ERROR, "is_simple_subquery: subquery is bogus");
476 * Can't currently pull up a query with setops. Maybe after querytree
479 if (subquery->setOperations)
483 * Can't pull up a subquery involving grouping, aggregation, sorting,
486 if (subquery->hasAggs ||
487 subquery->groupClause ||
488 subquery->havingQual ||
489 subquery->sortClause ||
490 subquery->distinctClause ||
491 subquery->limitOffset ||
492 subquery->limitCount)
496 * Don't pull up a subquery that has any set-returning functions in
497 * its targetlist. Otherwise we might well wind up inserting
498 * set-returning functions into places where they mustn't go,
499 * such as quals of higher queries.
501 if (expression_returns_set((Node *) subquery->targetList))
505 * Hack: don't try to pull up a subquery with an empty jointree.
506 * query_planner() will correctly generate a Result plan for a
507 * jointree that's totally empty, but I don't think the right things
508 * happen if an empty FromExpr appears lower down in a jointree. Not
509 * worth working hard on this, just to collapse SubqueryScan/Result
512 if (subquery->jointree->fromlist == NIL)
519 * has_nullable_targetlist
520 * Check a subquery in the range table to see if all the non-junk
521 * targetlist items are simple variables (and, hence, will correctly
522 * go to NULL when examined above the point of an outer join).
524 * A possible future extension is to accept strict functions of simple
525 * variables, eg, "x + 1".
528 has_nullable_targetlist(Query *subquery)
532 foreach(l, subquery->targetList)
534 TargetEntry *tle = (TargetEntry *) lfirst(l);
536 /* ignore resjunk columns */
537 if (tle->resdom->resjunk)
540 /* Okay if tlist item is a simple Var */
541 if (tle->expr && IsA(tle->expr, Var))
550 * Helper routine for pull_up_subqueries: do ResolveNew on every expression
551 * in the jointree, without changing the jointree structure itself. Ugly,
552 * but there's no other way...
555 resolvenew_in_jointree(Node *jtnode, int varno, List *subtlist)
559 if (IsA(jtnode, RangeTblRef))
561 /* nothing to do here */
563 else if (IsA(jtnode, FromExpr))
565 FromExpr *f = (FromExpr *) jtnode;
568 foreach(l, f->fromlist)
569 resolvenew_in_jointree(lfirst(l), varno, subtlist);
570 f->quals = ResolveNew(f->quals,
571 varno, 0, subtlist, CMD_SELECT, 0);
573 else if (IsA(jtnode, JoinExpr))
575 JoinExpr *j = (JoinExpr *) jtnode;
577 resolvenew_in_jointree(j->larg, varno, subtlist);
578 resolvenew_in_jointree(j->rarg, varno, subtlist);
579 j->quals = ResolveNew(j->quals,
580 varno, 0, subtlist, CMD_SELECT, 0);
583 * We don't bother to update the colvars list, since it won't be
588 elog(ERROR, "resolvenew_in_jointree: unexpected node type %d",
593 * preprocess_jointree
594 * Attempt to simplify a query's jointree.
596 * If we succeed in pulling up a subquery then we might form a jointree
597 * in which a FromExpr is a direct child of another FromExpr. In that
598 * case we can consider collapsing the two FromExprs into one. This is
599 * an optional conversion, since the planner will work correctly either
600 * way. But we may find a better plan (at the cost of more planning time)
601 * if we merge the two nodes.
603 * NOTE: don't try to do this in the same jointree scan that does subquery
604 * pullup! Since we're changing the jointree structure here, that wouldn't
605 * work reliably --- see comments for pull_up_subqueries().
608 preprocess_jointree(Query *parse, Node *jtnode)
612 if (IsA(jtnode, RangeTblRef))
614 /* nothing to do here... */
616 else if (IsA(jtnode, FromExpr))
618 FromExpr *f = (FromExpr *) jtnode;
622 foreach(l, f->fromlist)
624 Node *child = (Node *) lfirst(l);
626 /* Recursively simplify the child... */
627 child = preprocess_jointree(parse, child);
628 /* Now, is it a FromExpr? */
629 if (child && IsA(child, FromExpr))
632 * Yes, so do we want to merge it into parent? Always do
633 * so if child has just one element (since that doesn't
634 * make the parent's list any longer). Otherwise we have
635 * to be careful about the increase in planning time
636 * caused by combining the two join search spaces into
637 * one. Our heuristic is to merge if the merge will
638 * produce a join list no longer than GEQO_RELS/2.
639 * (Perhaps need an additional user parameter?)
641 FromExpr *subf = (FromExpr *) child;
642 int childlen = length(subf->fromlist);
643 int myothers = length(newlist) + length(lnext(l));
645 if (childlen <= 1 || (childlen + myothers) <= geqo_rels / 2)
647 newlist = nconc(newlist, subf->fromlist);
648 f->quals = make_and_qual(f->quals, subf->quals);
651 newlist = lappend(newlist, child);
654 newlist = lappend(newlist, child);
656 f->fromlist = newlist;
658 else if (IsA(jtnode, JoinExpr))
660 JoinExpr *j = (JoinExpr *) jtnode;
662 /* Can't usefully change the JoinExpr, but recurse on children */
663 j->larg = preprocess_jointree(parse, j->larg);
664 j->rarg = preprocess_jointree(parse, j->rarg);
667 elog(ERROR, "preprocess_jointree: unexpected node type %d",
673 * preprocess_expression
674 * Do subquery_planner's preprocessing work for an expression,
675 * which can be a targetlist, a WHERE clause (including JOIN/ON
676 * conditions), or a HAVING clause.
679 preprocess_expression(Query *parse, Node *expr, int kind)
685 * Simplify constant expressions.
687 * Note that at this point quals have not yet been converted to
688 * implicit-AND form, so we can apply eval_const_expressions directly.
689 * Also note that we need to do this before SS_process_sublinks,
690 * because that routine inserts bogus "Const" nodes.
692 expr = eval_const_expressions(expr);
695 * If it's a qual or havingQual, canonicalize it, and convert it to
696 * implicit-AND format.
698 * XXX Is there any value in re-applying eval_const_expressions after
701 if (kind != EXPRKIND_TARGET)
703 expr = (Node *) canonicalize_qual((Expr *) expr, true);
705 #ifdef OPTIMIZER_DEBUG
706 printf("After canonicalize_qual()\n");
711 /* Expand SubLinks to SubPlans */
712 if (parse->hasSubLinks)
713 expr = SS_process_sublinks(expr);
715 /* Replace uplevel vars with Param nodes */
716 if (PlannerQueryLevel > 1)
717 expr = SS_replace_correlation_vars(expr);
720 * If the query has any join RTEs, try to replace join alias variables
721 * with base-relation variables, to allow quals to be pushed down.
722 * We must do this after sublink processing, since it does not recurse
725 * The flattening pass is expensive enough that it seems worthwhile to
726 * scan the rangetable to see if we can avoid it.
728 has_join_rtes = false;
729 foreach(rt, parse->rtable)
731 RangeTblEntry *rte = lfirst(rt);
733 if (rte->rtekind == RTE_JOIN)
735 has_join_rtes = true;
740 expr = flatten_join_alias_vars(expr, parse, false);
746 * preprocess_qual_conditions
747 * Recursively scan the query's jointree and do subquery_planner's
748 * preprocessing work on each qual condition found therein.
751 preprocess_qual_conditions(Query *parse, Node *jtnode)
755 if (IsA(jtnode, RangeTblRef))
757 /* nothing to do here */
759 else if (IsA(jtnode, FromExpr))
761 FromExpr *f = (FromExpr *) jtnode;
764 foreach(l, f->fromlist)
765 preprocess_qual_conditions(parse, lfirst(l));
767 f->quals = preprocess_expression(parse, f->quals, EXPRKIND_WHERE);
769 else if (IsA(jtnode, JoinExpr))
771 JoinExpr *j = (JoinExpr *) jtnode;
773 preprocess_qual_conditions(parse, j->larg);
774 preprocess_qual_conditions(parse, j->rarg);
776 j->quals = preprocess_expression(parse, j->quals, EXPRKIND_WHERE);
779 elog(ERROR, "preprocess_qual_conditions: unexpected node type %d",
783 /*--------------------
784 * inheritance_planner
785 * Generate a plan in the case where the result relation is an
788 * We have to handle this case differently from cases where a source
789 * relation is an inheritance set. Source inheritance is expanded at
790 * the bottom of the plan tree (see allpaths.c), but target inheritance
791 * has to be expanded at the top. The reason is that for UPDATE, each
792 * target relation needs a different targetlist matching its own column
793 * set. (This is not so critical for DELETE, but for simplicity we treat
794 * inherited DELETE the same way.) Fortunately, the UPDATE/DELETE target
795 * can never be the nullable side of an outer join, so it's OK to generate
798 * parse is the querytree produced by the parser & rewriter.
799 * inheritlist is an integer list of RT indexes for the result relation set.
801 * Returns a query plan.
802 *--------------------
805 inheritance_planner(Query *parse, List *inheritlist)
807 int parentRTindex = parse->resultRelation;
808 Oid parentOID = getrelid(parentRTindex, parse->rtable);
809 List *subplans = NIL;
813 foreach(l, inheritlist)
815 int childRTindex = lfirsti(l);
816 Oid childOID = getrelid(childRTindex, parse->rtable);
820 /* Generate modified query with this rel as target */
821 subquery = (Query *) adjust_inherited_attrs((Node *) parse,
822 parentRTindex, parentOID,
823 childRTindex, childOID);
825 subplan = grouping_planner(subquery, 0.0 /* retrieve all tuples */ );
826 subplans = lappend(subplans, subplan);
827 /* Save preprocessed tlist from first rel for use in Append */
829 tlist = subplan->targetlist;
832 /* Save the target-relations list for the executor, too */
833 parse->resultRelations = inheritlist;
835 return (Plan *) make_append(subplans, true, tlist);
838 /*--------------------
840 * Perform planning steps related to grouping, aggregation, etc.
841 * This primarily means adding top-level processing to the basic
842 * query plan produced by query_planner.
844 * parse is the querytree produced by the parser & rewriter.
845 * tuple_fraction is the fraction of tuples we expect will be retrieved
847 * tuple_fraction is interpreted as follows:
848 * < 0: determine fraction by inspection of query (normal case)
849 * 0: expect all tuples to be retrieved
850 * 0 < tuple_fraction < 1: expect the given fraction of tuples available
851 * from the plan to be retrieved
852 * tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
853 * expected to be retrieved (ie, a LIMIT specification)
854 * The normal case is to pass -1, but some callers pass values >= 0 to
855 * override this routine's determination of the appropriate fraction.
857 * Returns a query plan.
858 *--------------------
861 grouping_planner(Query *parse, double tuple_fraction)
863 List *tlist = parse->targetList;
865 List *current_pathkeys;
866 List *group_pathkeys;
868 AttrNumber *groupColIdx = NULL;
870 if (parse->setOperations)
873 * Construct the plan for set operations. The result will not
874 * need any work except perhaps a top-level sort and/or LIMIT.
876 result_plan = plan_set_operations(parse);
879 * We should not need to call preprocess_targetlist, since we must
880 * be in a SELECT query node. Instead, use the targetlist
881 * returned by plan_set_operations (since this tells whether it
882 * returned any resjunk columns!), and transfer any sort key
883 * information from the original tlist.
885 Assert(parse->commandType == CMD_SELECT);
887 tlist = postprocess_setop_tlist(result_plan->targetlist, tlist);
890 * Can't handle FOR UPDATE here (parser should have checked
891 * already, but let's make sure).
894 elog(ERROR, "SELECT FOR UPDATE is not allowed with UNION/INTERSECT/EXCEPT");
897 * We set current_pathkeys NIL indicating we do not know sort
898 * order. This is correct when the top set operation is UNION
899 * ALL, since the appended-together results are unsorted even if
900 * the subplans were sorted. For other set operations we could be
901 * smarter --- room for future improvement!
903 current_pathkeys = NIL;
906 * Calculate pathkeys that represent grouping/ordering
907 * requirements (grouping should always be null, but...)
909 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
911 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
918 /* Preprocess targetlist in case we are inside an INSERT/UPDATE. */
919 tlist = preprocess_targetlist(tlist,
921 parse->resultRelation,
925 * Add TID targets for rels selected FOR UPDATE (should this be
926 * done in preprocess_targetlist?). The executor uses the TID to
927 * know which rows to lock, much as for UPDATE or DELETE.
934 * We've got trouble if the FOR UPDATE appears inside
935 * grouping, since grouping renders a reference to individual
936 * tuple CTIDs invalid. This is also checked at parse time,
937 * but that's insufficient because of rule substitution, query
940 CheckSelectForUpdate(parse);
943 * Currently the executor only supports FOR UPDATE at top
946 if (PlannerQueryLevel > 1)
947 elog(ERROR, "SELECT FOR UPDATE is not allowed in subselects");
949 foreach(l, parse->rowMarks)
951 Index rti = lfirsti(l);
957 resname = (char *) palloc(32);
958 sprintf(resname, "ctid%u", rti);
959 resdom = makeResdom(length(tlist) + 1,
966 SelfItemPointerAttributeNumber,
971 ctid = makeTargetEntry(resdom, (Node *) var);
972 tlist = lappend(tlist, ctid);
977 * Generate appropriate target list for subplan; may be different
978 * from tlist if grouping or aggregation is needed.
980 sub_tlist = make_subplanTargetList(parse, tlist, &groupColIdx);
983 * Calculate pathkeys that represent grouping/ordering
986 group_pathkeys = make_pathkeys_for_sortclauses(parse->groupClause,
988 sort_pathkeys = make_pathkeys_for_sortclauses(parse->sortClause,
992 * Figure out whether we need a sorted result from query_planner.
994 * If we have a GROUP BY clause, then we want a result sorted
995 * properly for grouping. Otherwise, if there is an ORDER BY
996 * clause, we want to sort by the ORDER BY clause. (Note: if we
997 * have both, and ORDER BY is a superset of GROUP BY, it would be
998 * tempting to request sort by ORDER BY --- but that might just
999 * leave us failing to exploit an available sort order at all.
1000 * Needs more thought...)
1002 if (parse->groupClause)
1003 parse->query_pathkeys = group_pathkeys;
1004 else if (parse->sortClause)
1005 parse->query_pathkeys = sort_pathkeys;
1007 parse->query_pathkeys = NIL;
1010 * Figure out whether we expect to retrieve all the tuples that
1011 * the plan can generate, or to stop early due to outside factors
1012 * such as a cursor. If the caller passed a value >= 0, believe
1013 * that value, else do our own examination of the query context.
1015 if (tuple_fraction < 0.0)
1017 /* Initial assumption is we need all the tuples */
1018 tuple_fraction = 0.0;
1021 * Check for retrieve-into-portal, ie DECLARE CURSOR.
1023 * We have no real idea how many tuples the user will ultimately
1024 * FETCH from a cursor, but it seems a good bet that he
1025 * doesn't want 'em all. Optimize for 10% retrieval (you
1026 * gotta better number? Should this be a SETtable parameter?)
1028 if (parse->isPortal)
1029 tuple_fraction = 0.10;
1033 * Adjust tuple_fraction if we see that we are going to apply
1034 * limiting/grouping/aggregation/etc. This is not overridable by
1035 * the caller, since it reflects plan actions that this routine
1036 * will certainly take, not assumptions about context.
1038 if (parse->limitCount != NULL)
1041 * A LIMIT clause limits the absolute number of tuples
1042 * returned. However, if it's not a constant LIMIT then we
1043 * have to punt; for lack of a better idea, assume 10% of the
1044 * plan's result is wanted.
1046 double limit_fraction = 0.0;
1048 if (IsA(parse->limitCount, Const))
1050 Const *limitc = (Const *) parse->limitCount;
1051 int32 count = DatumGetInt32(limitc->constvalue);
1054 * A NULL-constant LIMIT represents "LIMIT ALL", which we
1055 * treat the same as no limit (ie, expect to retrieve all
1058 if (!limitc->constisnull && count > 0)
1060 limit_fraction = (double) count;
1061 /* We must also consider the OFFSET, if present */
1062 if (parse->limitOffset != NULL)
1064 if (IsA(parse->limitOffset, Const))
1068 limitc = (Const *) parse->limitOffset;
1069 offset = DatumGetInt32(limitc->constvalue);
1070 if (!limitc->constisnull && offset > 0)
1071 limit_fraction += (double) offset;
1075 /* OFFSET is an expression ... punt ... */
1076 limit_fraction = 0.10;
1083 /* LIMIT is an expression ... punt ... */
1084 limit_fraction = 0.10;
1087 if (limit_fraction > 0.0)
1090 * If we have absolute limits from both caller and LIMIT,
1091 * use the smaller value; if one is fractional and the
1092 * other absolute, treat the fraction as a fraction of the
1093 * absolute value; else we can multiply the two fractions
1096 if (tuple_fraction >= 1.0)
1098 if (limit_fraction >= 1.0)
1101 tuple_fraction = Min(tuple_fraction, limit_fraction);
1105 /* caller absolute, limit fractional */
1106 tuple_fraction *= limit_fraction;
1107 if (tuple_fraction < 1.0)
1108 tuple_fraction = 1.0;
1111 else if (tuple_fraction > 0.0)
1113 if (limit_fraction >= 1.0)
1115 /* caller fractional, limit absolute */
1116 tuple_fraction *= limit_fraction;
1117 if (tuple_fraction < 1.0)
1118 tuple_fraction = 1.0;
1122 /* both fractional */
1123 tuple_fraction *= limit_fraction;
1128 /* no info from caller, just use limit */
1129 tuple_fraction = limit_fraction;
1134 if (parse->groupClause)
1137 * In GROUP BY mode, we have the little problem that we don't
1138 * really know how many input tuples will be needed to make a
1139 * group, so we can't translate an output LIMIT count into an
1140 * input count. For lack of a better idea, assume 25% of the
1141 * input data will be processed if there is any output limit.
1142 * However, if the caller gave us a fraction rather than an
1143 * absolute count, we can keep using that fraction (which
1144 * amounts to assuming that all the groups are about the same
1147 if (tuple_fraction >= 1.0)
1148 tuple_fraction = 0.25;
1151 * If both GROUP BY and ORDER BY are specified, we will need
1152 * two levels of sort --- and, therefore, certainly need to
1153 * read all the input tuples --- unless ORDER BY is a subset
1154 * of GROUP BY. (We have not yet canonicalized the pathkeys,
1155 * so must use the slower noncanonical comparison method.)
1157 if (parse->groupClause && parse->sortClause &&
1158 !noncanonical_pathkeys_contained_in(sort_pathkeys,
1160 tuple_fraction = 0.0;
1162 else if (parse->hasAggs)
1165 * Ungrouped aggregate will certainly want all the input
1168 tuple_fraction = 0.0;
1170 else if (parse->distinctClause)
1173 * SELECT DISTINCT, like GROUP, will absorb an unpredictable
1174 * number of input tuples per output tuple. Handle the same
1177 if (tuple_fraction >= 1.0)
1178 tuple_fraction = 0.25;
1181 /* Generate the basic plan for this Query */
1182 result_plan = query_planner(parse,
1187 * query_planner returns actual sort order (which is not
1188 * necessarily what we requested) in query_pathkeys.
1190 current_pathkeys = parse->query_pathkeys;
1194 * We couldn't canonicalize group_pathkeys and sort_pathkeys before
1195 * running query_planner(), so do it now.
1197 group_pathkeys = canonicalize_pathkeys(parse, group_pathkeys);
1198 sort_pathkeys = canonicalize_pathkeys(parse, sort_pathkeys);
1201 * If we have a GROUP BY clause, insert a group node (plus the
1202 * appropriate sort node, if necessary).
1204 if (parse->groupClause)
1211 * Decide whether how many tuples per group the Group node needs
1212 * to return. (Needs only one tuple per group if no aggregate is
1213 * present. Otherwise, need every tuple from the group to do the
1214 * aggregation.) Note tuplePerGroup is named backwards :-(
1216 tuplePerGroup = parse->hasAggs;
1219 * If there are aggregates then the Group node should just return
1220 * the same set of vars as the subplan did. If there are no aggs
1221 * then the Group node had better compute the final tlist.
1224 group_tlist = new_unsorted_tlist(result_plan->targetlist);
1226 group_tlist = tlist;
1229 * Figure out whether the path result is already ordered the way
1230 * we need it --- if so, no need for an explicit sort step.
1232 if (pathkeys_contained_in(group_pathkeys, current_pathkeys))
1234 is_sorted = true; /* no sort needed now */
1235 /* current_pathkeys remains unchanged */
1240 * We will need to do an explicit sort by the GROUP BY clause.
1241 * make_groupplan will do the work, but set current_pathkeys
1242 * to indicate the resulting order.
1245 current_pathkeys = group_pathkeys;
1248 result_plan = make_groupplan(parse,
1258 * If aggregate is present, insert the Agg node
1260 * HAVING clause, if any, becomes qual of the Agg node
1264 result_plan = (Plan *) make_agg(tlist,
1265 (List *) parse->havingQual,
1267 /* Note: Agg does not affect any existing sort order of the tuples */
1271 /* If there are no Aggs, we shouldn't have any HAVING qual anymore */
1272 Assert(parse->havingQual == NULL);
1276 * If we were not able to make the plan come out in the right order,
1277 * add an explicit sort step.
1279 if (parse->sortClause)
1281 if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys))
1282 result_plan = make_sortplan(parse, tlist, result_plan,
1287 * If there is a DISTINCT clause, add the UNIQUE node.
1289 if (parse->distinctClause)
1291 result_plan = (Plan *) make_unique(tlist, result_plan,
1292 parse->distinctClause);
1296 * Finally, if there is a LIMIT/OFFSET clause, add the LIMIT node.
1298 if (parse->limitOffset || parse->limitCount)
1300 result_plan = (Plan *) make_limit(tlist, result_plan,
1309 * make_subplanTargetList
1310 * Generate appropriate target list when grouping is required.
1312 * When grouping_planner inserts Aggregate and/or Group plan nodes above
1313 * the result of query_planner, we typically want to pass a different
1314 * target list to query_planner than the outer plan nodes should have.
1315 * This routine generates the correct target list for the subplan.
1317 * The initial target list passed from the parser already contains entries
1318 * for all ORDER BY and GROUP BY expressions, but it will not have entries
1319 * for variables used only in HAVING clauses; so we need to add those
1320 * variables to the subplan target list. Also, if we are doing either
1321 * grouping or aggregation, we flatten all expressions except GROUP BY items
1322 * into their component variables; the other expressions will be computed by
1323 * the inserted nodes rather than by the subplan. For example,
1324 * given a query like
1325 * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
1326 * we want to pass this targetlist to the subplan:
1328 * where the a+b target will be used by the Sort/Group steps, and the
1329 * other targets will be used for computing the final results. (In the
1330 * above example we could theoretically suppress the a and b targets and
1331 * pass down only c,d,a+b, but it's not really worth the trouble to
1332 * eliminate simple var references from the subplan. We will avoid doing
1333 * the extra computation to recompute a+b at the outer level; see
1334 * replace_vars_with_subplan_refs() in setrefs.c.)
1336 * 'parse' is the query being processed.
1337 * 'tlist' is the query's target list.
1338 * 'groupColIdx' receives an array of column numbers for the GROUP BY
1339 * expressions (if there are any) in the subplan's target list.
1341 * The result is the targetlist to be passed to the subplan.
1345 make_subplanTargetList(Query *parse,
1347 AttrNumber **groupColIdx)
1353 *groupColIdx = NULL;
1356 * If we're not grouping or aggregating, nothing to do here;
1357 * query_planner should receive the unmodified target list.
1359 if (!parse->hasAggs && !parse->groupClause && !parse->havingQual)
1363 * Otherwise, start with a "flattened" tlist (having just the vars
1364 * mentioned in the targetlist and HAVING qual --- but not upper-
1365 * level Vars; they will be replaced by Params later on).
1367 sub_tlist = flatten_tlist(tlist);
1368 extravars = pull_var_clause(parse->havingQual, false);
1369 sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
1370 freeList(extravars);
1373 * If grouping, create sub_tlist entries for all GROUP BY expressions
1374 * (GROUP BY items that are simple Vars should be in the list
1375 * already), and make an array showing where the group columns are in
1378 numCols = length(parse->groupClause);
1382 AttrNumber *grpColIdx;
1385 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
1386 *groupColIdx = grpColIdx;
1388 foreach(gl, parse->groupClause)
1390 GroupClause *grpcl = (GroupClause *) lfirst(gl);
1391 Node *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
1392 TargetEntry *te = NULL;
1395 /* Find or make a matching sub_tlist entry */
1396 foreach(sl, sub_tlist)
1398 te = (TargetEntry *) lfirst(sl);
1399 if (equal(groupexpr, te->expr))
1404 te = makeTargetEntry(makeResdom(length(sub_tlist) + 1,
1405 exprType(groupexpr),
1406 exprTypmod(groupexpr),
1410 sub_tlist = lappend(sub_tlist, te);
1413 /* and save its resno */
1414 grpColIdx[keyno++] = te->resdom->resno;
1423 * Add a Group node for GROUP BY processing.
1424 * If we couldn't make the subplan produce presorted output for grouping,
1425 * first add an explicit Sort node.
1428 make_groupplan(Query *parse,
1432 AttrNumber *grpColIdx,
1436 int numCols = length(groupClause);
1441 * The Sort node always just takes a copy of the subplan's tlist
1442 * plus ordering information. (This might seem inefficient if the
1443 * subplan contains complex GROUP BY expressions, but in fact Sort
1444 * does not evaluate its targetlist --- it only outputs the same
1445 * tuples in a new order. So the expressions we might be copying
1446 * are just dummies with no extra execution cost.)
1448 List *sort_tlist = new_unsorted_tlist(subplan->targetlist);
1452 foreach(gl, groupClause)
1454 GroupClause *grpcl = (GroupClause *) lfirst(gl);
1455 TargetEntry *te = nth(grpColIdx[keyno] - 1, sort_tlist);
1456 Resdom *resdom = te->resdom;
1459 * Check for the possibility of duplicate group-by clauses ---
1460 * the parser should have removed 'em, but the Sort executor
1461 * will get terribly confused if any get through!
1463 if (resdom->reskey == 0)
1465 /* OK, insert the ordering info needed by the executor. */
1466 resdom->reskey = ++keyno;
1467 resdom->reskeyop = grpcl->sortop;
1473 subplan = (Plan *) make_sort(parse, sort_tlist, subplan, keyno);
1476 return (Plan *) make_group(group_tlist, tuplePerGroup, numCols,
1477 grpColIdx, subplan);
1482 * Add a Sort node to implement an explicit ORDER BY clause.
1485 make_sortplan(Query *parse, List *tlist, Plan *plannode, List *sortcls)
1492 * First make a copy of the tlist so that we don't corrupt the
1495 sort_tlist = new_unsorted_tlist(tlist);
1499 SortClause *sortcl = (SortClause *) lfirst(i);
1500 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sort_tlist);
1501 Resdom *resdom = tle->resdom;
1504 * Check for the possibility of duplicate order-by clauses --- the
1505 * parser should have removed 'em, but the executor will get
1506 * terribly confused if any get through!
1508 if (resdom->reskey == 0)
1510 /* OK, insert the ordering info needed by the executor. */
1511 resdom->reskey = ++keyno;
1512 resdom->reskeyop = sortcl->sortop;
1518 return (Plan *) make_sort(parse, sort_tlist, plannode, keyno);
1522 * postprocess_setop_tlist
1523 * Fix up targetlist returned by plan_set_operations().
1525 * We need to transpose sort key info from the orig_tlist into new_tlist.
1526 * NOTE: this would not be good enough if we supported resjunk sort keys
1527 * for results of set operations --- then, we'd need to project a whole
1528 * new tlist to evaluate the resjunk columns. For now, just elog if we
1529 * find any resjunk columns in orig_tlist.
1532 postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
1536 foreach(l, new_tlist)
1538 TargetEntry *new_tle = (TargetEntry *) lfirst(l);
1539 TargetEntry *orig_tle;
1541 /* ignore resjunk columns in setop result */
1542 if (new_tle->resdom->resjunk)
1545 Assert(orig_tlist != NIL);
1546 orig_tle = (TargetEntry *) lfirst(orig_tlist);
1547 orig_tlist = lnext(orig_tlist);
1548 if (orig_tle->resdom->resjunk)
1549 elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");
1550 Assert(new_tle->resdom->resno == orig_tle->resdom->resno);
1551 Assert(new_tle->resdom->restype == orig_tle->resdom->restype);
1552 new_tle->resdom->ressortgroupref = orig_tle->resdom->ressortgroupref;
1554 if (orig_tlist != NIL)
1555 elog(ERROR, "postprocess_setop_tlist: resjunk output columns not implemented");