]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/planner.c
Implement SQL-standard WITH clauses, including WITH RECURSIVE.
[postgresql] / src / backend / optimizer / plan / planner.c
1 /*-------------------------------------------------------------------------
2  *
3  * planner.c
4  *        The query optimizer external interface.
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.244 2008/10/04 21:56:53 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include <limits.h>
19
20 #include "catalog/pg_operator.h"
21 #include "executor/executor.h"
22 #include "executor/nodeAgg.h"
23 #include "miscadmin.h"
24 #include "nodes/makefuncs.h"
25 #include "optimizer/clauses.h"
26 #include "optimizer/cost.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/planmain.h"
30 #include "optimizer/planner.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/subselect.h"
33 #include "optimizer/tlist.h"
34 #include "optimizer/var.h"
35 #ifdef OPTIMIZER_DEBUG
36 #include "nodes/print.h"
37 #endif
38 #include "parser/parse_expr.h"
39 #include "parser/parse_oper.h"
40 #include "parser/parsetree.h"
41 #include "utils/lsyscache.h"
42 #include "utils/syscache.h"
43
44
45 /* GUC parameter */
46 double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;
47
48 /* Hook for plugins to get control in planner() */
49 planner_hook_type planner_hook = NULL;
50
51
52 /* Expression kind codes for preprocess_expression */
53 #define EXPRKIND_QUAL           0
54 #define EXPRKIND_TARGET         1
55 #define EXPRKIND_RTFUNC         2
56 #define EXPRKIND_VALUES         3
57 #define EXPRKIND_LIMIT          4
58 #define EXPRKIND_APPINFO        5
59
60
61 static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
62 static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
63 static Plan *inheritance_planner(PlannerInfo *root);
64 static Plan *grouping_planner(PlannerInfo *root, double tuple_fraction);
65 static bool is_dummy_plan(Plan *plan);
66 static double preprocess_limit(PlannerInfo *root,
67                                  double tuple_fraction,
68                                  int64 *offset_est, int64 *count_est);
69 static void preprocess_groupclause(PlannerInfo *root);
70 static bool choose_hashed_grouping(PlannerInfo *root,
71                                            double tuple_fraction, double limit_tuples,
72                                            Path *cheapest_path, Path *sorted_path,
73                                            double dNumGroups, AggClauseCounts *agg_counts);
74 static bool choose_hashed_distinct(PlannerInfo *root,
75                                            Plan *input_plan, List *input_pathkeys,
76                                            double tuple_fraction, double limit_tuples,
77                                            double dNumDistinctRows);
78 static List *make_subplanTargetList(PlannerInfo *root, List *tlist,
79                                            AttrNumber **groupColIdx, bool *need_tlist_eval);
80 static void locate_grouping_columns(PlannerInfo *root,
81                                                 List *tlist,
82                                                 List *sub_tlist,
83                                                 AttrNumber *groupColIdx);
84 static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
85
86
87 /*****************************************************************************
88  *
89  *         Query optimizer entry point
90  *
91  * To support loadable plugins that monitor or modify planner behavior,
92  * we provide a hook variable that lets a plugin get control before and
93  * after the standard planning process.  The plugin would normally call
94  * standard_planner().
95  *
96  * Note to plugin authors: standard_planner() scribbles on its Query input,
97  * so you'd better copy that data structure if you want to plan more than once.
98  *
99  *****************************************************************************/
100 PlannedStmt *
101 planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
102 {
103         PlannedStmt *result;
104
105         if (planner_hook)
106                 result = (*planner_hook) (parse, cursorOptions, boundParams);
107         else
108                 result = standard_planner(parse, cursorOptions, boundParams);
109         return result;
110 }
111
112 PlannedStmt *
113 standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
114 {
115         PlannedStmt *result;
116         PlannerGlobal *glob;
117         double          tuple_fraction;
118         PlannerInfo *root;
119         Plan       *top_plan;
120         ListCell   *lp,
121                            *lr;
122
123         /* Cursor options may come from caller or from DECLARE CURSOR stmt */
124         if (parse->utilityStmt &&
125                 IsA(parse->utilityStmt, DeclareCursorStmt))
126                 cursorOptions |= ((DeclareCursorStmt *) parse->utilityStmt)->options;
127
128         /*
129          * Set up global state for this planner invocation.  This data is needed
130          * across all levels of sub-Query that might exist in the given command,
131          * so we keep it in a separate struct that's linked to by each per-Query
132          * PlannerInfo.
133          */
134         glob = makeNode(PlannerGlobal);
135
136         glob->boundParams = boundParams;
137         glob->paramlist = NIL;
138         glob->subplans = NIL;
139         glob->subrtables = NIL;
140         glob->rewindPlanIDs = NULL;
141         glob->finalrtable = NIL;
142         glob->relationOids = NIL;
143         glob->invalItems = NIL;
144         glob->transientPlan = false;
145
146         /* Determine what fraction of the plan is likely to be scanned */
147         if (cursorOptions & CURSOR_OPT_FAST_PLAN)
148         {
149                 /*
150                  * We have no real idea how many tuples the user will ultimately FETCH
151                  * from a cursor, but it is often the case that he doesn't want 'em
152                  * all, or would prefer a fast-start plan anyway so that he can
153                  * process some of the tuples sooner.  Use a GUC parameter to decide
154                  * what fraction to optimize for.
155                  */
156                 tuple_fraction = cursor_tuple_fraction;
157
158                 /*
159                  * We document cursor_tuple_fraction as simply being a fraction,
160                  * which means the edge cases 0 and 1 have to be treated specially
161                  * here.  We convert 1 to 0 ("all the tuples") and 0 to a very small
162                  * fraction.
163                  */
164                 if (tuple_fraction >= 1.0)
165                         tuple_fraction = 0.0;
166                 else if (tuple_fraction <= 0.0)
167                         tuple_fraction = 1e-10;
168         }
169         else
170         {
171                 /* Default assumption is we need all the tuples */
172                 tuple_fraction = 0.0;
173         }
174
175         /* primary planning entry point (may recurse for subqueries) */
176         top_plan = subquery_planner(glob, parse, NULL,
177                                                                 false, tuple_fraction, &root);
178
179         /*
180          * If creating a plan for a scrollable cursor, make sure it can run
181          * backwards on demand.  Add a Material node at the top at need.
182          */
183         if (cursorOptions & CURSOR_OPT_SCROLL)
184         {
185                 if (!ExecSupportsBackwardScan(top_plan))
186                         top_plan = materialize_finished_plan(top_plan);
187         }
188
189         /* final cleanup of the plan */
190         Assert(glob->finalrtable == NIL);
191         top_plan = set_plan_references(glob, top_plan, root->parse->rtable);
192         /* ... and the subplans (both regular subplans and initplans) */
193         Assert(list_length(glob->subplans) == list_length(glob->subrtables));
194         forboth(lp, glob->subplans, lr, glob->subrtables)
195         {
196                 Plan       *subplan = (Plan *) lfirst(lp);
197                 List       *subrtable = (List *) lfirst(lr);
198
199                 lfirst(lp) = set_plan_references(glob, subplan, subrtable);
200         }
201
202         /* build the PlannedStmt result */
203         result = makeNode(PlannedStmt);
204
205         result->commandType = parse->commandType;
206         result->canSetTag = parse->canSetTag;
207         result->transientPlan = glob->transientPlan;
208         result->planTree = top_plan;
209         result->rtable = glob->finalrtable;
210         result->resultRelations = root->resultRelations;
211         result->utilityStmt = parse->utilityStmt;
212         result->intoClause = parse->intoClause;
213         result->subplans = glob->subplans;
214         result->rewindPlanIDs = glob->rewindPlanIDs;
215         result->returningLists = root->returningLists;
216         result->rowMarks = parse->rowMarks;
217         result->relationOids = glob->relationOids;
218         result->invalItems = glob->invalItems;
219         result->nParamExec = list_length(glob->paramlist);
220
221         return result;
222 }
223
224
225 /*--------------------
226  * subquery_planner
227  *        Invokes the planner on a subquery.  We recurse to here for each
228  *        sub-SELECT found in the query tree.
229  *
230  * glob is the global state for the current planner run.
231  * parse is the querytree produced by the parser & rewriter.
232  * parent_root is the immediate parent Query's info (NULL at the top level).
233  * hasRecursion is true if this is a recursive WITH query.
234  * tuple_fraction is the fraction of tuples we expect will be retrieved.
235  * tuple_fraction is interpreted as explained for grouping_planner, below.
236  *
237  * If subroot isn't NULL, we pass back the query's final PlannerInfo struct;
238  * among other things this tells the output sort ordering of the plan.
239  *
240  * Basically, this routine does the stuff that should only be done once
241  * per Query object.  It then calls grouping_planner.  At one time,
242  * grouping_planner could be invoked recursively on the same Query object;
243  * that's not currently true, but we keep the separation between the two
244  * routines anyway, in case we need it again someday.
245  *
246  * subquery_planner will be called recursively to handle sub-Query nodes
247  * found within the query's expressions and rangetable.
248  *
249  * Returns a query plan.
250  *--------------------
251  */
252 Plan *
253 subquery_planner(PlannerGlobal *glob, Query *parse,
254                                  PlannerInfo *parent_root,
255                                  bool hasRecursion, double tuple_fraction,
256                                  PlannerInfo **subroot)
257 {
258         int                     num_old_subplans = list_length(glob->subplans);
259         PlannerInfo *root;
260         Plan       *plan;
261         List       *newHaving;
262         bool            hasOuterJoins;
263         ListCell   *l;
264
265         /* Create a PlannerInfo data structure for this subquery */
266         root = makeNode(PlannerInfo);
267         root->parse = parse;
268         root->glob = glob;
269         root->query_level = parent_root ? parent_root->query_level + 1 : 1;
270         root->parent_root = parent_root;
271         root->planner_cxt = CurrentMemoryContext;
272         root->init_plans = NIL;
273         root->cte_plan_ids = NIL;
274         root->eq_classes = NIL;
275         root->append_rel_list = NIL;
276
277         root->hasRecursion = hasRecursion;
278         if (hasRecursion)
279                 root->wt_param_id = SS_assign_worktable_param(root);
280         else
281                 root->wt_param_id = -1;
282         root->non_recursive_plan = NULL;
283
284         /*
285          * If there is a WITH list, process each WITH query and build an
286          * initplan SubPlan structure for it.
287          */
288         if (parse->cteList)
289                 SS_process_ctes(root);
290
291         /*
292          * Look for ANY and EXISTS SubLinks in WHERE and JOIN/ON clauses, and try
293          * to transform them into joins.  Note that this step does not descend
294          * into subqueries; if we pull up any subqueries below, their SubLinks are
295          * processed just before pulling them up.
296          */
297         if (parse->hasSubLinks)
298                 pull_up_sublinks(root);
299
300         /*
301          * Scan the rangetable for set-returning functions, and inline them
302          * if possible (producing subqueries that might get pulled up next).
303          * Recursion issues here are handled in the same way as for SubLinks.
304          */
305         inline_set_returning_functions(root);
306
307         /*
308          * Check to see if any subqueries in the rangetable can be merged into
309          * this query.
310          */
311         parse->jointree = (FromExpr *)
312                 pull_up_subqueries(root, (Node *) parse->jointree, false, false);
313
314         /*
315          * Detect whether any rangetable entries are RTE_JOIN kind; if not, we can
316          * avoid the expense of doing flatten_join_alias_vars().  Also check for
317          * outer joins --- if none, we can skip reduce_outer_joins().
318          * This must be done after we have done pull_up_subqueries, of course.
319          */
320         root->hasJoinRTEs = false;
321         hasOuterJoins = false;
322         foreach(l, parse->rtable)
323         {
324                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
325
326                 if (rte->rtekind == RTE_JOIN)
327                 {
328                         root->hasJoinRTEs = true;
329                         if (IS_OUTER_JOIN(rte->jointype))
330                         {
331                                 hasOuterJoins = true;
332                                 /* Can quit scanning once we find an outer join */
333                                 break;
334                         }
335                 }
336         }
337
338         /*
339          * Expand any rangetable entries that are inheritance sets into "append
340          * relations".  This can add entries to the rangetable, but they must be
341          * plain base relations not joins, so it's OK (and marginally more
342          * efficient) to do it after checking for join RTEs.  We must do it after
343          * pulling up subqueries, else we'd fail to handle inherited tables in
344          * subqueries.
345          */
346         expand_inherited_tables(root);
347
348         /*
349          * Set hasHavingQual to remember if HAVING clause is present.  Needed
350          * because preprocess_expression will reduce a constant-true condition to
351          * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
352          */
353         root->hasHavingQual = (parse->havingQual != NULL);
354
355         /* Clear this flag; might get set in distribute_qual_to_rels */
356         root->hasPseudoConstantQuals = false;
357
358         /*
359          * Do expression preprocessing on targetlist and quals.
360          */
361         parse->targetList = (List *)
362                 preprocess_expression(root, (Node *) parse->targetList,
363                                                           EXPRKIND_TARGET);
364
365         parse->returningList = (List *)
366                 preprocess_expression(root, (Node *) parse->returningList,
367                                                           EXPRKIND_TARGET);
368
369         preprocess_qual_conditions(root, (Node *) parse->jointree);
370
371         parse->havingQual = preprocess_expression(root, parse->havingQual,
372                                                                                           EXPRKIND_QUAL);
373
374         parse->limitOffset = preprocess_expression(root, parse->limitOffset,
375                                                                                            EXPRKIND_LIMIT);
376         parse->limitCount = preprocess_expression(root, parse->limitCount,
377                                                                                           EXPRKIND_LIMIT);
378
379         root->append_rel_list = (List *)
380                 preprocess_expression(root, (Node *) root->append_rel_list,
381                                                           EXPRKIND_APPINFO);
382
383         /* Also need to preprocess expressions for function and values RTEs */
384         foreach(l, parse->rtable)
385         {
386                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
387
388                 if (rte->rtekind == RTE_FUNCTION)
389                         rte->funcexpr = preprocess_expression(root, rte->funcexpr,
390                                                                                                   EXPRKIND_RTFUNC);
391                 else if (rte->rtekind == RTE_VALUES)
392                         rte->values_lists = (List *)
393                                 preprocess_expression(root, (Node *) rte->values_lists,
394                                                                           EXPRKIND_VALUES);
395         }
396
397         /*
398          * In some cases we may want to transfer a HAVING clause into WHERE. We
399          * cannot do so if the HAVING clause contains aggregates (obviously) or
400          * volatile functions (since a HAVING clause is supposed to be executed
401          * only once per group).  Also, it may be that the clause is so expensive
402          * to execute that we're better off doing it only once per group, despite
403          * the loss of selectivity.  This is hard to estimate short of doing the
404          * entire planning process twice, so we use a heuristic: clauses
405          * containing subplans are left in HAVING.      Otherwise, we move or copy the
406          * HAVING clause into WHERE, in hopes of eliminating tuples before
407          * aggregation instead of after.
408          *
409          * If the query has explicit grouping then we can simply move such a
410          * clause into WHERE; any group that fails the clause will not be in the
411          * output because none of its tuples will reach the grouping or
412          * aggregation stage.  Otherwise we must have a degenerate (variable-free)
413          * HAVING clause, which we put in WHERE so that query_planner() can use it
414          * in a gating Result node, but also keep in HAVING to ensure that we
415          * don't emit a bogus aggregated row. (This could be done better, but it
416          * seems not worth optimizing.)
417          *
418          * Note that both havingQual and parse->jointree->quals are in
419          * implicitly-ANDed-list form at this point, even though they are declared
420          * as Node *.
421          */
422         newHaving = NIL;
423         foreach(l, (List *) parse->havingQual)
424         {
425                 Node       *havingclause = (Node *) lfirst(l);
426
427                 if (contain_agg_clause(havingclause) ||
428                         contain_volatile_functions(havingclause) ||
429                         contain_subplans(havingclause))
430                 {
431                         /* keep it in HAVING */
432                         newHaving = lappend(newHaving, havingclause);
433                 }
434                 else if (parse->groupClause)
435                 {
436                         /* move it to WHERE */
437                         parse->jointree->quals = (Node *)
438                                 lappend((List *) parse->jointree->quals, havingclause);
439                 }
440                 else
441                 {
442                         /* put a copy in WHERE, keep it in HAVING */
443                         parse->jointree->quals = (Node *)
444                                 lappend((List *) parse->jointree->quals,
445                                                 copyObject(havingclause));
446                         newHaving = lappend(newHaving, havingclause);
447                 }
448         }
449         parse->havingQual = (Node *) newHaving;
450
451         /*
452          * If we have any outer joins, try to reduce them to plain inner joins.
453          * This step is most easily done after we've done expression
454          * preprocessing.
455          */
456         if (hasOuterJoins)
457                 reduce_outer_joins(root);
458
459         /*
460          * Do the main planning.  If we have an inherited target relation, that
461          * needs special processing, else go straight to grouping_planner.
462          */
463         if (parse->resultRelation &&
464                 rt_fetch(parse->resultRelation, parse->rtable)->inh)
465                 plan = inheritance_planner(root);
466         else
467                 plan = grouping_planner(root, tuple_fraction);
468
469         /*
470          * If any subplans were generated, or if we're inside a subplan, build
471          * initPlan list and extParam/allParam sets for plan nodes, and attach the
472          * initPlans to the top plan node.
473          */
474         if (list_length(glob->subplans) != num_old_subplans ||
475                 root->query_level > 1)
476                 SS_finalize_plan(root, plan, true);
477
478         /* Return internal info if caller wants it */
479         if (subroot)
480                 *subroot = root;
481
482         return plan;
483 }
484
485 /*
486  * preprocess_expression
487  *              Do subquery_planner's preprocessing work for an expression,
488  *              which can be a targetlist, a WHERE clause (including JOIN/ON
489  *              conditions), or a HAVING clause.
490  */
491 static Node *
492 preprocess_expression(PlannerInfo *root, Node *expr, int kind)
493 {
494         /*
495          * Fall out quickly if expression is empty.  This occurs often enough to
496          * be worth checking.  Note that null->null is the correct conversion for
497          * implicit-AND result format, too.
498          */
499         if (expr == NULL)
500                 return NULL;
501
502         /*
503          * If the query has any join RTEs, replace join alias variables with
504          * base-relation variables. We must do this before sublink processing,
505          * else sublinks expanded out from join aliases wouldn't get processed. We
506          * can skip it in VALUES lists, however, since they can't contain any Vars
507          * at all.
508          */
509         if (root->hasJoinRTEs && kind != EXPRKIND_VALUES)
510                 expr = flatten_join_alias_vars(root, expr);
511
512         /*
513          * Simplify constant expressions.
514          *
515          * Note: this also flattens nested AND and OR expressions into N-argument
516          * form.  All processing of a qual expression after this point must be
517          * careful to maintain AND/OR flatness --- that is, do not generate a tree
518          * with AND directly under AND, nor OR directly under OR.
519          *
520          * Because this is a relatively expensive process, we skip it when the
521          * query is trivial, such as "SELECT 2+2;" or "INSERT ... VALUES()". The
522          * expression will only be evaluated once anyway, so no point in
523          * pre-simplifying; we can't execute it any faster than the executor can,
524          * and we will waste cycles copying the tree.  Notice however that we
525          * still must do it for quals (to get AND/OR flatness); and if we are in a
526          * subquery we should not assume it will be done only once.
527          *
528          * For VALUES lists we never do this at all, again on the grounds that we
529          * should optimize for one-time evaluation.
530          */
531         if (kind != EXPRKIND_VALUES &&
532                 (root->parse->jointree->fromlist != NIL ||
533                  kind == EXPRKIND_QUAL ||
534                  root->query_level > 1))
535                 expr = eval_const_expressions(root, expr);
536
537         /*
538          * If it's a qual or havingQual, canonicalize it.
539          */
540         if (kind == EXPRKIND_QUAL)
541         {
542                 expr = (Node *) canonicalize_qual((Expr *) expr);
543
544 #ifdef OPTIMIZER_DEBUG
545                 printf("After canonicalize_qual()\n");
546                 pprint(expr);
547 #endif
548         }
549
550         /* Expand SubLinks to SubPlans */
551         if (root->parse->hasSubLinks)
552                 expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
553
554         /*
555          * XXX do not insert anything here unless you have grokked the comments in
556          * SS_replace_correlation_vars ...
557          */
558
559         /* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
560         if (root->query_level > 1)
561                 expr = SS_replace_correlation_vars(root, expr);
562
563         /*
564          * If it's a qual or havingQual, convert it to implicit-AND format. (We
565          * don't want to do this before eval_const_expressions, since the latter
566          * would be unable to simplify a top-level AND correctly. Also,
567          * SS_process_sublinks expects explicit-AND format.)
568          */
569         if (kind == EXPRKIND_QUAL)
570                 expr = (Node *) make_ands_implicit((Expr *) expr);
571
572         return expr;
573 }
574
575 /*
576  * preprocess_qual_conditions
577  *              Recursively scan the query's jointree and do subquery_planner's
578  *              preprocessing work on each qual condition found therein.
579  */
580 static void
581 preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
582 {
583         if (jtnode == NULL)
584                 return;
585         if (IsA(jtnode, RangeTblRef))
586         {
587                 /* nothing to do here */
588         }
589         else if (IsA(jtnode, FromExpr))
590         {
591                 FromExpr   *f = (FromExpr *) jtnode;
592                 ListCell   *l;
593
594                 foreach(l, f->fromlist)
595                         preprocess_qual_conditions(root, lfirst(l));
596
597                 f->quals = preprocess_expression(root, f->quals, EXPRKIND_QUAL);
598         }
599         else if (IsA(jtnode, JoinExpr))
600         {
601                 JoinExpr   *j = (JoinExpr *) jtnode;
602
603                 preprocess_qual_conditions(root, j->larg);
604                 preprocess_qual_conditions(root, j->rarg);
605
606                 j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
607         }
608         else
609                 elog(ERROR, "unrecognized node type: %d",
610                          (int) nodeTag(jtnode));
611 }
612
613 /*
614  * inheritance_planner
615  *        Generate a plan in the case where the result relation is an
616  *        inheritance set.
617  *
618  * We have to handle this case differently from cases where a source relation
619  * is an inheritance set. Source inheritance is expanded at the bottom of the
620  * plan tree (see allpaths.c), but target inheritance has to be expanded at
621  * the top.  The reason is that for UPDATE, each target relation needs a
622  * different targetlist matching its own column set.  Also, for both UPDATE
623  * and DELETE, the executor needs the Append plan node at the top, else it
624  * can't keep track of which table is the current target table.  Fortunately,
625  * the UPDATE/DELETE target can never be the nullable side of an outer join,
626  * so it's OK to generate the plan this way.
627  *
628  * Returns a query plan.
629  */
630 static Plan *
631 inheritance_planner(PlannerInfo *root)
632 {
633         Query      *parse = root->parse;
634         int                     parentRTindex = parse->resultRelation;
635         List       *subplans = NIL;
636         List       *resultRelations = NIL;
637         List       *returningLists = NIL;
638         List       *rtable = NIL;
639         List       *tlist = NIL;
640         PlannerInfo subroot;
641         ListCell   *l;
642
643         foreach(l, root->append_rel_list)
644         {
645                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
646                 Plan       *subplan;
647
648                 /* append_rel_list contains all append rels; ignore others */
649                 if (appinfo->parent_relid != parentRTindex)
650                         continue;
651
652                 /*
653                  * Generate modified query with this rel as target.
654                  */
655                 memcpy(&subroot, root, sizeof(PlannerInfo));
656                 subroot.parse = (Query *)
657                         adjust_appendrel_attrs((Node *) parse,
658                                                                    appinfo);
659                 subroot.init_plans = NIL;
660                 /* There shouldn't be any OJ info to translate, as yet */
661                 Assert(subroot.join_info_list == NIL);
662
663                 /* Generate plan */
664                 subplan = grouping_planner(&subroot, 0.0 /* retrieve all tuples */ );
665
666                 /*
667                  * If this child rel was excluded by constraint exclusion, exclude it
668                  * from the plan.
669                  */
670                 if (is_dummy_plan(subplan))
671                         continue;
672
673                 /* Save rtable and tlist from first rel for use below */
674                 if (subplans == NIL)
675                 {
676                         rtable = subroot.parse->rtable;
677                         tlist = subplan->targetlist;
678                 }
679
680                 subplans = lappend(subplans, subplan);
681
682                 /* Make sure any initplans from this rel get into the outer list */
683                 root->init_plans = list_concat(root->init_plans, subroot.init_plans);
684
685                 /* Build target-relations list for the executor */
686                 resultRelations = lappend_int(resultRelations, appinfo->child_relid);
687
688                 /* Build list of per-relation RETURNING targetlists */
689                 if (parse->returningList)
690                 {
691                         Assert(list_length(subroot.returningLists) == 1);
692                         returningLists = list_concat(returningLists,
693                                                                                  subroot.returningLists);
694                 }
695         }
696
697         root->resultRelations = resultRelations;
698         root->returningLists = returningLists;
699
700         /* Mark result as unordered (probably unnecessary) */
701         root->query_pathkeys = NIL;
702
703         /*
704          * If we managed to exclude every child rel, return a dummy plan
705          */
706         if (subplans == NIL)
707         {
708                 root->resultRelations = list_make1_int(parentRTindex);
709                 /* although dummy, it must have a valid tlist for executor */
710                 tlist = preprocess_targetlist(root, parse->targetList);
711                 return (Plan *) make_result(root,
712                                                                         tlist,
713                                                                         (Node *) list_make1(makeBoolConst(false,
714                                                                                                                                           false)),
715                                                                         NULL);
716         }
717
718         /*
719          * Planning might have modified the rangetable, due to changes of the
720          * Query structures inside subquery RTEs.  We have to ensure that this
721          * gets propagated back to the master copy.  But can't do this until we
722          * are done planning, because all the calls to grouping_planner need
723          * virgin sub-Queries to work from.  (We are effectively assuming that
724          * sub-Queries will get planned identically each time, or at least that
725          * the impacts on their rangetables will be the same each time.)
726          *
727          * XXX should clean this up someday
728          */
729         parse->rtable = rtable;
730
731         /* Suppress Append if there's only one surviving child rel */
732         if (list_length(subplans) == 1)
733                 return (Plan *) linitial(subplans);
734
735         return (Plan *) make_append(subplans, true, tlist);
736 }
737
738 /*--------------------
739  * grouping_planner
740  *        Perform planning steps related to grouping, aggregation, etc.
741  *        This primarily means adding top-level processing to the basic
742  *        query plan produced by query_planner.
743  *
744  * tuple_fraction is the fraction of tuples we expect will be retrieved
745  *
746  * tuple_fraction is interpreted as follows:
747  *        0: expect all tuples to be retrieved (normal case)
748  *        0 < tuple_fraction < 1: expect the given fraction of tuples available
749  *              from the plan to be retrieved
750  *        tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
751  *              expected to be retrieved (ie, a LIMIT specification)
752  *
753  * Returns a query plan.  Also, root->query_pathkeys is returned as the
754  * actual output ordering of the plan (in pathkey format).
755  *--------------------
756  */
757 static Plan *
758 grouping_planner(PlannerInfo *root, double tuple_fraction)
759 {
760         Query      *parse = root->parse;
761         List       *tlist = parse->targetList;
762         int64           offset_est = 0;
763         int64           count_est = 0;
764         double          limit_tuples = -1.0;
765         Plan       *result_plan;
766         List       *current_pathkeys;
767         double          dNumGroups = 0;
768
769         /* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
770         if (parse->limitCount || parse->limitOffset)
771         {
772                 tuple_fraction = preprocess_limit(root, tuple_fraction,
773                                                                                   &offset_est, &count_est);
774
775                 /*
776                  * If we have a known LIMIT, and don't have an unknown OFFSET, we can
777                  * estimate the effects of using a bounded sort.
778                  */
779                 if (count_est > 0 && offset_est >= 0)
780                         limit_tuples = (double) count_est + (double) offset_est;
781         }
782
783         if (parse->setOperations)
784         {
785                 List       *set_sortclauses;
786
787                 /*
788                  * If there's a top-level ORDER BY, assume we have to fetch all the
789                  * tuples.      This might be too simplistic given all the hackery below
790                  * to possibly avoid the sort; but the odds of accurate estimates
791                  * here are pretty low anyway.
792                  */
793                 if (parse->sortClause)
794                         tuple_fraction = 0.0;
795
796                 /*
797                  * Construct the plan for set operations.  The result will not need
798                  * any work except perhaps a top-level sort and/or LIMIT.  Note that
799                  * any special work for recursive unions is the responsibility of
800                  * plan_set_operations.
801                  */
802                 result_plan = plan_set_operations(root, tuple_fraction,
803                                                                                   &set_sortclauses);
804
805                 /*
806                  * Calculate pathkeys representing the sort order (if any) of the set
807                  * operation's result.  We have to do this before overwriting the sort
808                  * key information...
809                  */
810                 current_pathkeys = make_pathkeys_for_sortclauses(root,
811                                                                                                                  set_sortclauses,
812                                                                                                          result_plan->targetlist,
813                                                                                                                  true);
814
815                 /*
816                  * We should not need to call preprocess_targetlist, since we must be
817                  * in a SELECT query node.      Instead, use the targetlist returned by
818                  * plan_set_operations (since this tells whether it returned any
819                  * resjunk columns!), and transfer any sort key information from the
820                  * original tlist.
821                  */
822                 Assert(parse->commandType == CMD_SELECT);
823
824                 tlist = postprocess_setop_tlist(copyObject(result_plan->targetlist),
825                                                                                 tlist);
826
827                 /*
828                  * Can't handle FOR UPDATE/SHARE here (parser should have checked
829                  * already, but let's make sure).
830                  */
831                 if (parse->rowMarks)
832                         ereport(ERROR,
833                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
834                                          errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
835
836                 /*
837                  * Calculate pathkeys that represent result ordering requirements
838                  */
839                 Assert(parse->distinctClause == NIL);
840                 root->sort_pathkeys = make_pathkeys_for_sortclauses(root,
841                                                                                                                         parse->sortClause,
842                                                                                                                         tlist,
843                                                                                                                         true);
844         }
845         else
846         {
847                 /* No set operations, do regular planning */
848                 List       *sub_tlist;
849                 AttrNumber *groupColIdx = NULL;
850                 bool            need_tlist_eval = true;
851                 QualCost        tlist_cost;
852                 Path       *cheapest_path;
853                 Path       *sorted_path;
854                 Path       *best_path;
855                 long            numGroups = 0;
856                 AggClauseCounts agg_counts;
857                 int                     numGroupCols;
858                 bool            use_hashed_grouping = false;
859
860                 MemSet(&agg_counts, 0, sizeof(AggClauseCounts));
861
862                 /* A recursive query should always have setOperations */
863                 Assert(!root->hasRecursion);
864
865                 /* Preprocess GROUP BY clause, if any */
866                 if (parse->groupClause)
867                         preprocess_groupclause(root);
868                 numGroupCols = list_length(parse->groupClause);
869
870                 /* Preprocess targetlist */
871                 tlist = preprocess_targetlist(root, tlist);
872
873                 /*
874                  * Generate appropriate target list for subplan; may be different from
875                  * tlist if grouping or aggregation is needed.
876                  */
877                 sub_tlist = make_subplanTargetList(root, tlist,
878                                                                                    &groupColIdx, &need_tlist_eval);
879
880                 /*
881                  * Calculate pathkeys that represent grouping/ordering requirements.
882                  * Stash them in PlannerInfo so that query_planner can canonicalize
883                  * them after EquivalenceClasses have been formed.  The sortClause
884                  * is certainly sort-able, but GROUP BY and DISTINCT might not be,
885                  * in which case we just leave their pathkeys empty.
886                  */
887                 if (parse->groupClause &&
888                         grouping_is_sortable(parse->groupClause))
889                         root->group_pathkeys =
890                                 make_pathkeys_for_sortclauses(root,
891                                                                                           parse->groupClause,
892                                                                                           tlist,
893                                                                                           false);
894                 else
895                         root->group_pathkeys = NIL;
896
897                 if (parse->distinctClause &&
898                         grouping_is_sortable(parse->distinctClause))
899                         root->distinct_pathkeys =
900                                 make_pathkeys_for_sortclauses(root,
901                                                                                           parse->distinctClause,
902                                                                                           tlist,
903                                                                                           false);
904                 else
905                         root->distinct_pathkeys = NIL;
906
907                 root->sort_pathkeys =
908                         make_pathkeys_for_sortclauses(root,
909                                                                                   parse->sortClause,
910                                                                                   tlist,
911                                                                                   false);
912
913                 /*
914                  * Will need actual number of aggregates for estimating costs.
915                  *
916                  * Note: we do not attempt to detect duplicate aggregates here; a
917                  * somewhat-overestimated count is okay for our present purposes.
918                  *
919                  * Note: think not that we can turn off hasAggs if we find no aggs. It
920                  * is possible for constant-expression simplification to remove all
921                  * explicit references to aggs, but we still have to follow the
922                  * aggregate semantics (eg, producing only one output row).
923                  */
924                 if (parse->hasAggs)
925                 {
926                         count_agg_clauses((Node *) tlist, &agg_counts);
927                         count_agg_clauses(parse->havingQual, &agg_counts);
928                 }
929
930                 /*
931                  * Figure out whether we want a sorted result from query_planner.
932                  *
933                  * If we have a sortable GROUP BY clause, then we want a result sorted
934                  * properly for grouping.  Otherwise, if there's a sortable DISTINCT
935                  * clause that's more rigorous than the ORDER BY clause, we try to
936                  * produce output that's sufficiently well sorted for the DISTINCT.
937                  * Otherwise, if there is an ORDER BY clause, we want to sort by the
938                  * ORDER BY clause.
939                  *
940                  * Note: if we have both ORDER BY and GROUP BY, and ORDER BY is a
941                  * superset of GROUP BY, it would be tempting to request sort by ORDER
942                  * BY --- but that might just leave us failing to exploit an available
943                  * sort order at all.  Needs more thought.  The choice for DISTINCT
944                  * versus ORDER BY is much easier, since we know that the parser
945                  * ensured that one is a superset of the other.
946                  */
947                 if (root->group_pathkeys)
948                         root->query_pathkeys = root->group_pathkeys;
949                 else if (list_length(root->distinct_pathkeys) >
950                                  list_length(root->sort_pathkeys))
951                         root->query_pathkeys = root->distinct_pathkeys;
952                 else if (root->sort_pathkeys)
953                         root->query_pathkeys = root->sort_pathkeys;
954                 else
955                         root->query_pathkeys = NIL;
956
957                 /*
958                  * Generate the best unsorted and presorted paths for this Query (but
959                  * note there may not be any presorted path).  query_planner will also
960                  * estimate the number of groups in the query, and canonicalize all
961                  * the pathkeys.
962                  */
963                 query_planner(root, sub_tlist, tuple_fraction, limit_tuples,
964                                           &cheapest_path, &sorted_path, &dNumGroups);
965
966                 /*
967                  * If grouping, decide whether to use sorted or hashed grouping.
968                  */
969                 if (parse->groupClause)
970                 {
971                         bool    can_hash;
972                         bool    can_sort;
973
974                         /*
975                          * Executor doesn't support hashed aggregation with DISTINCT
976                          * aggregates.  (Doing so would imply storing *all* the input
977                          * values in the hash table, which seems like a certain loser.)
978                          */
979                         can_hash = (agg_counts.numDistinctAggs == 0 &&
980                                                 grouping_is_hashable(parse->groupClause));
981                         can_sort = grouping_is_sortable(parse->groupClause);
982                         if (can_hash && can_sort)
983                         {
984                                 /* we have a meaningful choice to make ... */
985                                 use_hashed_grouping =
986                                         choose_hashed_grouping(root,
987                                                                                    tuple_fraction, limit_tuples,
988                                                                                    cheapest_path, sorted_path,
989                                                                                    dNumGroups, &agg_counts);
990                         }
991                         else if (can_hash)
992                                 use_hashed_grouping = true;
993                         else if (can_sort)
994                                 use_hashed_grouping = false;
995                         else
996                                 ereport(ERROR,
997                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
998                                                  errmsg("could not implement GROUP BY"),
999                                                  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
1000
1001                         /* Also convert # groups to long int --- but 'ware overflow! */
1002                         numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
1003                 }
1004
1005                 /*
1006                  * Select the best path.  If we are doing hashed grouping, we will
1007                  * always read all the input tuples, so use the cheapest-total path.
1008                  * Otherwise, trust query_planner's decision about which to use.
1009                  */
1010                 if (use_hashed_grouping || !sorted_path)
1011                         best_path = cheapest_path;
1012                 else
1013                         best_path = sorted_path;
1014
1015                 /*
1016                  * Check to see if it's possible to optimize MIN/MAX aggregates. If
1017                  * so, we will forget all the work we did so far to choose a "regular"
1018                  * path ... but we had to do it anyway to be able to tell which way is
1019                  * cheaper.
1020                  */
1021                 result_plan = optimize_minmax_aggregates(root,
1022                                                                                                  tlist,
1023                                                                                                  best_path);
1024                 if (result_plan != NULL)
1025                 {
1026                         /*
1027                          * optimize_minmax_aggregates generated the full plan, with the
1028                          * right tlist, and it has no sort order.
1029                          */
1030                         current_pathkeys = NIL;
1031                 }
1032                 else
1033                 {
1034                         /*
1035                          * Normal case --- create a plan according to query_planner's
1036                          * results.
1037                          */
1038                         bool    need_sort_for_grouping = false;
1039
1040                         result_plan = create_plan(root, best_path);
1041                         current_pathkeys = best_path->pathkeys;
1042
1043                         /* Detect if we'll need an explicit sort for grouping */
1044                         if (parse->groupClause && !use_hashed_grouping &&
1045                                 !pathkeys_contained_in(root->group_pathkeys, current_pathkeys))
1046                         {
1047                                 need_sort_for_grouping = true;
1048                                 /*
1049                                  * Always override query_planner's tlist, so that we don't
1050                                  * sort useless data from a "physical" tlist.
1051                                  */
1052                                 need_tlist_eval = true;
1053                         }
1054
1055                         /*
1056                          * create_plan() returns a plan with just a "flat" tlist of
1057                          * required Vars.  Usually we need to insert the sub_tlist as the
1058                          * tlist of the top plan node.  However, we can skip that if we
1059                          * determined that whatever query_planner chose to return will be
1060                          * good enough.
1061                          */
1062                         if (need_tlist_eval)
1063                         {
1064                                 /*
1065                                  * If the top-level plan node is one that cannot do expression
1066                                  * evaluation, we must insert a Result node to project the
1067                                  * desired tlist.
1068                                  */
1069                                 if (!is_projection_capable_plan(result_plan))
1070                                 {
1071                                         result_plan = (Plan *) make_result(root,
1072                                                                                                            sub_tlist,
1073                                                                                                            NULL,
1074                                                                                                            result_plan);
1075                                 }
1076                                 else
1077                                 {
1078                                         /*
1079                                          * Otherwise, just replace the subplan's flat tlist with
1080                                          * the desired tlist.
1081                                          */
1082                                         result_plan->targetlist = sub_tlist;
1083                                 }
1084
1085                                 /*
1086                                  * Also, account for the cost of evaluation of the sub_tlist.
1087                                  *
1088                                  * Up to now, we have only been dealing with "flat" tlists,
1089                                  * containing just Vars.  So their evaluation cost is zero
1090                                  * according to the model used by cost_qual_eval() (or if you
1091                                  * prefer, the cost is factored into cpu_tuple_cost).  Thus we
1092                                  * can avoid accounting for tlist cost throughout
1093                                  * query_planner() and subroutines.  But now we've inserted a
1094                                  * tlist that might contain actual operators, sub-selects, etc
1095                                  * --- so we'd better account for its cost.
1096                                  *
1097                                  * Below this point, any tlist eval cost for added-on nodes
1098                                  * should be accounted for as we create those nodes.
1099                                  * Presently, of the node types we can add on, only Agg and
1100                                  * Group project new tlists (the rest just copy their input
1101                                  * tuples) --- so make_agg() and make_group() are responsible
1102                                  * for computing the added cost.
1103                                  */
1104                                 cost_qual_eval(&tlist_cost, sub_tlist, root);
1105                                 result_plan->startup_cost += tlist_cost.startup;
1106                                 result_plan->total_cost += tlist_cost.startup +
1107                                         tlist_cost.per_tuple * result_plan->plan_rows;
1108                         }
1109                         else
1110                         {
1111                                 /*
1112                                  * Since we're using query_planner's tlist and not the one
1113                                  * make_subplanTargetList calculated, we have to refigure any
1114                                  * grouping-column indexes make_subplanTargetList computed.
1115                                  */
1116                                 locate_grouping_columns(root, tlist, result_plan->targetlist,
1117                                                                                 groupColIdx);
1118                         }
1119
1120                         /*
1121                          * Insert AGG or GROUP node if needed, plus an explicit sort step
1122                          * if necessary.
1123                          *
1124                          * HAVING clause, if any, becomes qual of the Agg or Group node.
1125                          */
1126                         if (use_hashed_grouping)
1127                         {
1128                                 /* Hashed aggregate plan --- no sort needed */
1129                                 result_plan = (Plan *) make_agg(root,
1130                                                                                                 tlist,
1131                                                                                                 (List *) parse->havingQual,
1132                                                                                                 AGG_HASHED,
1133                                                                                                 numGroupCols,
1134                                                                                                 groupColIdx,
1135                                                                         extract_grouping_ops(parse->groupClause),
1136                                                                                                 numGroups,
1137                                                                                                 agg_counts.numAggs,
1138                                                                                                 result_plan);
1139                                 /* Hashed aggregation produces randomly-ordered results */
1140                                 current_pathkeys = NIL;
1141                         }
1142                         else if (parse->hasAggs)
1143                         {
1144                                 /* Plain aggregate plan --- sort if needed */
1145                                 AggStrategy aggstrategy;
1146
1147                                 if (parse->groupClause)
1148                                 {
1149                                         if (need_sort_for_grouping)
1150                                         {
1151                                                 result_plan = (Plan *)
1152                                                         make_sort_from_groupcols(root,
1153                                                                                                          parse->groupClause,
1154                                                                                                          groupColIdx,
1155                                                                                                          result_plan);
1156                                                 current_pathkeys = root->group_pathkeys;
1157                                         }
1158                                         aggstrategy = AGG_SORTED;
1159
1160                                         /*
1161                                          * The AGG node will not change the sort ordering of its
1162                                          * groups, so current_pathkeys describes the result too.
1163                                          */
1164                                 }
1165                                 else
1166                                 {
1167                                         aggstrategy = AGG_PLAIN;
1168                                         /* Result will be only one row anyway; no sort order */
1169                                         current_pathkeys = NIL;
1170                                 }
1171
1172                                 result_plan = (Plan *) make_agg(root,
1173                                                                                                 tlist,
1174                                                                                                 (List *) parse->havingQual,
1175                                                                                                 aggstrategy,
1176                                                                                                 numGroupCols,
1177                                                                                                 groupColIdx,
1178                                                                         extract_grouping_ops(parse->groupClause),
1179                                                                                                 numGroups,
1180                                                                                                 agg_counts.numAggs,
1181                                                                                                 result_plan);
1182                         }
1183                         else if (parse->groupClause)
1184                         {
1185                                 /*
1186                                  * GROUP BY without aggregation, so insert a group node (plus
1187                                  * the appropriate sort node, if necessary).
1188                                  *
1189                                  * Add an explicit sort if we couldn't make the path come out
1190                                  * the way the GROUP node needs it.
1191                                  */
1192                                 if (need_sort_for_grouping)
1193                                 {
1194                                         result_plan = (Plan *)
1195                                                 make_sort_from_groupcols(root,
1196                                                                                                  parse->groupClause,
1197                                                                                                  groupColIdx,
1198                                                                                                  result_plan);
1199                                         current_pathkeys = root->group_pathkeys;
1200                                 }
1201
1202                                 result_plan = (Plan *) make_group(root,
1203                                                                                                   tlist,
1204                                                                                                   (List *) parse->havingQual,
1205                                                                                                   numGroupCols,
1206                                                                                                   groupColIdx,
1207                                                                         extract_grouping_ops(parse->groupClause),
1208                                                                                                   dNumGroups,
1209                                                                                                   result_plan);
1210                                 /* The Group node won't change sort ordering */
1211                         }
1212                         else if (root->hasHavingQual)
1213                         {
1214                                 /*
1215                                  * No aggregates, and no GROUP BY, but we have a HAVING qual.
1216                                  * This is a degenerate case in which we are supposed to emit
1217                                  * either 0 or 1 row depending on whether HAVING succeeds.
1218                                  * Furthermore, there cannot be any variables in either HAVING
1219                                  * or the targetlist, so we actually do not need the FROM
1220                                  * table at all!  We can just throw away the plan-so-far and
1221                                  * generate a Result node.      This is a sufficiently unusual
1222                                  * corner case that it's not worth contorting the structure of
1223                                  * this routine to avoid having to generate the plan in the
1224                                  * first place.
1225                                  */
1226                                 result_plan = (Plan *) make_result(root,
1227                                                                                                    tlist,
1228                                                                                                    parse->havingQual,
1229                                                                                                    NULL);
1230                         }
1231                 }                                               /* end of non-minmax-aggregate case */
1232         }                                                       /* end of if (setOperations) */
1233
1234         /*
1235          * If there is a DISTINCT clause, add the necessary node(s).
1236          */
1237         if (parse->distinctClause)
1238         {
1239                 double  dNumDistinctRows;
1240                 long    numDistinctRows;
1241                 bool    use_hashed_distinct;
1242                 bool    can_sort;
1243                 bool    can_hash;
1244
1245                 /*
1246                  * If there was grouping or aggregation, use the current number of
1247                  * rows as the estimated number of DISTINCT rows (ie, assume the
1248                  * result was already mostly unique).  If not, use the number of
1249                  * distinct-groups calculated by query_planner.
1250                  */
1251                 if (parse->groupClause || root->hasHavingQual || parse->hasAggs)
1252                         dNumDistinctRows = result_plan->plan_rows;
1253                 else
1254                         dNumDistinctRows = dNumGroups;
1255
1256                 /* Also convert to long int --- but 'ware overflow! */
1257                 numDistinctRows = (long) Min(dNumDistinctRows, (double) LONG_MAX);
1258
1259                 /*
1260                  * If we have a sortable DISTINCT ON clause, we always use sorting.
1261                  * This enforces the expected behavior of DISTINCT ON.
1262                  */
1263                 can_sort = grouping_is_sortable(parse->distinctClause);
1264                 if (can_sort && parse->hasDistinctOn)
1265                         use_hashed_distinct = false;
1266                 else
1267                 {
1268                         can_hash = grouping_is_hashable(parse->distinctClause);
1269                         if (can_hash && can_sort)
1270                         {
1271                                 /* we have a meaningful choice to make ... */
1272                                 use_hashed_distinct =
1273                                         choose_hashed_distinct(root,
1274                                                                                    result_plan, current_pathkeys,
1275                                                                                    tuple_fraction, limit_tuples,
1276                                                                                    dNumDistinctRows);
1277                         }
1278                         else if (can_hash)
1279                                 use_hashed_distinct = true;
1280                         else if (can_sort)
1281                                 use_hashed_distinct = false;
1282                         else
1283                         {
1284                                 ereport(ERROR,
1285                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1286                                                  errmsg("could not implement DISTINCT"),
1287                                                  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
1288                                 use_hashed_distinct = false; /* keep compiler quiet */
1289                         }
1290                 }
1291
1292                 if (use_hashed_distinct)
1293                 {
1294                         /* Hashed aggregate plan --- no sort needed */
1295                         result_plan = (Plan *) make_agg(root,
1296                                                                                         result_plan->targetlist,
1297                                                                                         NIL,
1298                                                                                         AGG_HASHED,
1299                                                                                         list_length(parse->distinctClause),
1300                                                                                         extract_grouping_cols(parse->distinctClause,
1301                                                                                                                                   result_plan->targetlist),
1302                                                                                         extract_grouping_ops(parse->distinctClause),
1303                                                                                         numDistinctRows,
1304                                                                                         0,
1305                                                                                         result_plan);
1306                         /* Hashed aggregation produces randomly-ordered results */
1307                         current_pathkeys = NIL;
1308                 }
1309                 else
1310                 {
1311                         /*
1312                          * Use a Unique node to implement DISTINCT.  Add an explicit sort
1313                          * if we couldn't make the path come out the way the Unique node
1314                          * needs it.  If we do have to sort, always sort by the more
1315                          * rigorous of DISTINCT and ORDER BY, to avoid a second sort
1316                          * below.  However, for regular DISTINCT, don't sort now if we
1317                          * don't have to --- sorting afterwards will likely be cheaper,
1318                          * and also has the possibility of optimizing via LIMIT.  But
1319                          * for DISTINCT ON, we *must* force the final sort now, else
1320                          * it won't have the desired behavior.
1321                          */
1322                         List   *needed_pathkeys;
1323
1324                         if (parse->hasDistinctOn &&
1325                                 list_length(root->distinct_pathkeys) <
1326                                 list_length(root->sort_pathkeys))
1327                                 needed_pathkeys = root->sort_pathkeys;
1328                         else
1329                                 needed_pathkeys = root->distinct_pathkeys;
1330
1331                         if (!pathkeys_contained_in(needed_pathkeys, current_pathkeys))
1332                         {
1333                                 if (list_length(root->distinct_pathkeys) >=
1334                                         list_length(root->sort_pathkeys))
1335                                         current_pathkeys = root->distinct_pathkeys;
1336                                 else
1337                                 {
1338                                         current_pathkeys = root->sort_pathkeys;
1339                                         /* Assert checks that parser didn't mess up... */
1340                                         Assert(pathkeys_contained_in(root->distinct_pathkeys,
1341                                                                                                  current_pathkeys));
1342                                 }
1343
1344                                 result_plan = (Plan *) make_sort_from_pathkeys(root,
1345                                                                                                                            result_plan,
1346                                                                                                                            current_pathkeys,
1347                                                                                                                            -1.0);
1348                         }
1349
1350                         result_plan = (Plan *) make_unique(result_plan,
1351                                                                                            parse->distinctClause);
1352                         result_plan->plan_rows = dNumDistinctRows;
1353                         /* The Unique node won't change sort ordering */
1354                 }
1355         }
1356
1357         /*
1358          * If ORDER BY was given and we were not able to make the plan come out in
1359          * the right order, add an explicit sort step.
1360          */
1361         if (parse->sortClause)
1362         {
1363                 if (!pathkeys_contained_in(root->sort_pathkeys, current_pathkeys))
1364                 {
1365                         result_plan = (Plan *) make_sort_from_pathkeys(root,
1366                                                                                                                    result_plan,
1367                                                                                                                    root->sort_pathkeys,
1368                                                                                                                    limit_tuples);
1369                         current_pathkeys = root->sort_pathkeys;
1370                 }
1371         }
1372
1373         /*
1374          * Finally, if there is a LIMIT/OFFSET clause, add the LIMIT node.
1375          */
1376         if (parse->limitCount || parse->limitOffset)
1377         {
1378                 result_plan = (Plan *) make_limit(result_plan,
1379                                                                                   parse->limitOffset,
1380                                                                                   parse->limitCount,
1381                                                                                   offset_est,
1382                                                                                   count_est);
1383         }
1384
1385         /*
1386          * Deal with the RETURNING clause if any.  It's convenient to pass the
1387          * returningList through setrefs.c now rather than at top level (if we
1388          * waited, handling inherited UPDATE/DELETE would be much harder).
1389          */
1390         if (parse->returningList)
1391         {
1392                 List       *rlist;
1393
1394                 Assert(parse->resultRelation);
1395                 rlist = set_returning_clause_references(root->glob,
1396                                                                                                 parse->returningList,
1397                                                                                                 result_plan,
1398                                                                                                 parse->resultRelation);
1399                 root->returningLists = list_make1(rlist);
1400         }
1401         else
1402                 root->returningLists = NIL;
1403
1404         /* Compute result-relations list if needed */
1405         if (parse->resultRelation)
1406                 root->resultRelations = list_make1_int(parse->resultRelation);
1407         else
1408                 root->resultRelations = NIL;
1409
1410         /*
1411          * Return the actual output ordering in query_pathkeys for possible use by
1412          * an outer query level.
1413          */
1414         root->query_pathkeys = current_pathkeys;
1415
1416         return result_plan;
1417 }
1418
1419 /*
1420  * Detect whether a plan node is a "dummy" plan created when a relation
1421  * is deemed not to need scanning due to constraint exclusion.
1422  *
1423  * Currently, such dummy plans are Result nodes with constant FALSE
1424  * filter quals.
1425  */
1426 static bool
1427 is_dummy_plan(Plan *plan)
1428 {
1429         if (IsA(plan, Result))
1430         {
1431                 List       *rcqual = (List *) ((Result *) plan)->resconstantqual;
1432
1433                 if (list_length(rcqual) == 1)
1434                 {
1435                         Const      *constqual = (Const *) linitial(rcqual);
1436
1437                         if (constqual && IsA(constqual, Const))
1438                         {
1439                                 if (!constqual->constisnull &&
1440                                         !DatumGetBool(constqual->constvalue))
1441                                         return true;
1442                         }
1443                 }
1444         }
1445         return false;
1446 }
1447
1448 /*
1449  * preprocess_limit - do pre-estimation for LIMIT and/or OFFSET clauses
1450  *
1451  * We try to estimate the values of the LIMIT/OFFSET clauses, and pass the
1452  * results back in *count_est and *offset_est.  These variables are set to
1453  * 0 if the corresponding clause is not present, and -1 if it's present
1454  * but we couldn't estimate the value for it.  (The "0" convention is OK
1455  * for OFFSET but a little bit bogus for LIMIT: effectively we estimate
1456  * LIMIT 0 as though it were LIMIT 1.  But this is in line with the planner's
1457  * usual practice of never estimating less than one row.)  These values will
1458  * be passed to make_limit, which see if you change this code.
1459  *
1460  * The return value is the suitably adjusted tuple_fraction to use for
1461  * planning the query.  This adjustment is not overridable, since it reflects
1462  * plan actions that grouping_planner() will certainly take, not assumptions
1463  * about context.
1464  */
1465 static double
1466 preprocess_limit(PlannerInfo *root, double tuple_fraction,
1467                                  int64 *offset_est, int64 *count_est)
1468 {
1469         Query      *parse = root->parse;
1470         Node       *est;
1471         double          limit_fraction;
1472
1473         /* Should not be called unless LIMIT or OFFSET */
1474         Assert(parse->limitCount || parse->limitOffset);
1475
1476         /*
1477          * Try to obtain the clause values.  We use estimate_expression_value
1478          * primarily because it can sometimes do something useful with Params.
1479          */
1480         if (parse->limitCount)
1481         {
1482                 est = estimate_expression_value(root, parse->limitCount);
1483                 if (est && IsA(est, Const))
1484                 {
1485                         if (((Const *) est)->constisnull)
1486                         {
1487                                 /* NULL indicates LIMIT ALL, ie, no limit */
1488                                 *count_est = 0; /* treat as not present */
1489                         }
1490                         else
1491                         {
1492                                 *count_est = DatumGetInt64(((Const *) est)->constvalue);
1493                                 if (*count_est <= 0)
1494                                         *count_est = 1;         /* force to at least 1 */
1495                         }
1496                 }
1497                 else
1498                         *count_est = -1;        /* can't estimate */
1499         }
1500         else
1501                 *count_est = 0;                 /* not present */
1502
1503         if (parse->limitOffset)
1504         {
1505                 est = estimate_expression_value(root, parse->limitOffset);
1506                 if (est && IsA(est, Const))
1507                 {
1508                         if (((Const *) est)->constisnull)
1509                         {
1510                                 /* Treat NULL as no offset; the executor will too */
1511                                 *offset_est = 0;        /* treat as not present */
1512                         }
1513                         else
1514                         {
1515                                 *offset_est = DatumGetInt64(((Const *) est)->constvalue);
1516                                 if (*offset_est < 0)
1517                                         *offset_est = 0;        /* less than 0 is same as 0 */
1518                         }
1519                 }
1520                 else
1521                         *offset_est = -1;       /* can't estimate */
1522         }
1523         else
1524                 *offset_est = 0;                /* not present */
1525
1526         if (*count_est != 0)
1527         {
1528                 /*
1529                  * A LIMIT clause limits the absolute number of tuples returned.
1530                  * However, if it's not a constant LIMIT then we have to guess; for
1531                  * lack of a better idea, assume 10% of the plan's result is wanted.
1532                  */
1533                 if (*count_est < 0 || *offset_est < 0)
1534                 {
1535                         /* LIMIT or OFFSET is an expression ... punt ... */
1536                         limit_fraction = 0.10;
1537                 }
1538                 else
1539                 {
1540                         /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
1541                         limit_fraction = (double) *count_est + (double) *offset_est;
1542                 }
1543
1544                 /*
1545                  * If we have absolute limits from both caller and LIMIT, use the
1546                  * smaller value; likewise if they are both fractional.  If one is
1547                  * fractional and the other absolute, we can't easily determine which
1548                  * is smaller, but we use the heuristic that the absolute will usually
1549                  * be smaller.
1550                  */
1551                 if (tuple_fraction >= 1.0)
1552                 {
1553                         if (limit_fraction >= 1.0)
1554                         {
1555                                 /* both absolute */
1556                                 tuple_fraction = Min(tuple_fraction, limit_fraction);
1557                         }
1558                         else
1559                         {
1560                                 /* caller absolute, limit fractional; use caller's value */
1561                         }
1562                 }
1563                 else if (tuple_fraction > 0.0)
1564                 {
1565                         if (limit_fraction >= 1.0)
1566                         {
1567                                 /* caller fractional, limit absolute; use limit */
1568                                 tuple_fraction = limit_fraction;
1569                         }
1570                         else
1571                         {
1572                                 /* both fractional */
1573                                 tuple_fraction = Min(tuple_fraction, limit_fraction);
1574                         }
1575                 }
1576                 else
1577                 {
1578                         /* no info from caller, just use limit */
1579                         tuple_fraction = limit_fraction;
1580                 }
1581         }
1582         else if (*offset_est != 0 && tuple_fraction > 0.0)
1583         {
1584                 /*
1585                  * We have an OFFSET but no LIMIT.      This acts entirely differently
1586                  * from the LIMIT case: here, we need to increase rather than decrease
1587                  * the caller's tuple_fraction, because the OFFSET acts to cause more
1588                  * tuples to be fetched instead of fewer.  This only matters if we got
1589                  * a tuple_fraction > 0, however.
1590                  *
1591                  * As above, use 10% if OFFSET is present but unestimatable.
1592                  */
1593                 if (*offset_est < 0)
1594                         limit_fraction = 0.10;
1595                 else
1596                         limit_fraction = (double) *offset_est;
1597
1598                 /*
1599                  * If we have absolute counts from both caller and OFFSET, add them
1600                  * together; likewise if they are both fractional.      If one is
1601                  * fractional and the other absolute, we want to take the larger, and
1602                  * we heuristically assume that's the fractional one.
1603                  */
1604                 if (tuple_fraction >= 1.0)
1605                 {
1606                         if (limit_fraction >= 1.0)
1607                         {
1608                                 /* both absolute, so add them together */
1609                                 tuple_fraction += limit_fraction;
1610                         }
1611                         else
1612                         {
1613                                 /* caller absolute, limit fractional; use limit */
1614                                 tuple_fraction = limit_fraction;
1615                         }
1616                 }
1617                 else
1618                 {
1619                         if (limit_fraction >= 1.0)
1620                         {
1621                                 /* caller fractional, limit absolute; use caller's value */
1622                         }
1623                         else
1624                         {
1625                                 /* both fractional, so add them together */
1626                                 tuple_fraction += limit_fraction;
1627                                 if (tuple_fraction >= 1.0)
1628                                         tuple_fraction = 0.0;           /* assume fetch all */
1629                         }
1630                 }
1631         }
1632
1633         return tuple_fraction;
1634 }
1635
1636
1637 /*
1638  * preprocess_groupclause - do preparatory work on GROUP BY clause
1639  *
1640  * The idea here is to adjust the ordering of the GROUP BY elements
1641  * (which in itself is semantically insignificant) to match ORDER BY,
1642  * thereby allowing a single sort operation to both implement the ORDER BY
1643  * requirement and set up for a Unique step that implements GROUP BY.
1644  *
1645  * In principle it might be interesting to consider other orderings of the
1646  * GROUP BY elements, which could match the sort ordering of other
1647  * possible plans (eg an indexscan) and thereby reduce cost.  We don't
1648  * bother with that, though.  Hashed grouping will frequently win anyway.
1649  *
1650  * Note: we need no comparable processing of the distinctClause because
1651  * the parser already enforced that that matches ORDER BY.
1652  */
1653 static void
1654 preprocess_groupclause(PlannerInfo *root)
1655 {
1656         Query      *parse = root->parse;
1657         List       *new_groupclause;
1658         bool            partial_match;
1659         ListCell   *sl;
1660         ListCell   *gl;
1661
1662         /* If no ORDER BY, nothing useful to do here */
1663         if (parse->sortClause == NIL)
1664                 return;
1665
1666         /*
1667          * Scan the ORDER BY clause and construct a list of matching GROUP BY
1668          * items, but only as far as we can make a matching prefix.
1669          *
1670          * This code assumes that the sortClause contains no duplicate items.
1671          */
1672         new_groupclause = NIL;
1673         foreach(sl, parse->sortClause)
1674         {
1675                 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
1676
1677                 foreach(gl, parse->groupClause)
1678                 {
1679                         SortGroupClause *gc = (SortGroupClause *) lfirst(gl);
1680
1681                         if (equal(gc, sc))
1682                         {
1683                                 new_groupclause = lappend(new_groupclause, gc);
1684                                 break;
1685                         }
1686                 }
1687                 if (gl == NULL)
1688                         break;                          /* no match, so stop scanning */
1689         }
1690
1691         /* Did we match all of the ORDER BY list, or just some of it? */
1692         partial_match = (sl != NULL);
1693
1694         /* If no match at all, no point in reordering GROUP BY */
1695         if (new_groupclause == NIL)
1696                 return;
1697
1698         /*
1699          * Add any remaining GROUP BY items to the new list, but only if we
1700          * were able to make a complete match.  In other words, we only
1701          * rearrange the GROUP BY list if the result is that one list is a
1702          * prefix of the other --- otherwise there's no possibility of a
1703          * common sort.  Also, give up if there are any non-sortable GROUP BY
1704          * items, since then there's no hope anyway.
1705          */
1706         foreach(gl, parse->groupClause)
1707         {
1708                 SortGroupClause *gc = (SortGroupClause *) lfirst(gl);
1709
1710                 if (list_member_ptr(new_groupclause, gc))
1711                         continue;                       /* it matched an ORDER BY item */
1712                 if (partial_match)
1713                         return;                         /* give up, no common sort possible */
1714                 if (!OidIsValid(gc->sortop))
1715                         return;                         /* give up, GROUP BY can't be sorted */
1716                 new_groupclause = lappend(new_groupclause, gc);
1717         }
1718
1719         /* Success --- install the rearranged GROUP BY list */
1720         Assert(list_length(parse->groupClause) == list_length(new_groupclause));
1721         parse->groupClause = new_groupclause;
1722 }
1723
1724 /*
1725  * choose_hashed_grouping - should we use hashed grouping?
1726  *
1727  * Note: this is only applied when both alternatives are actually feasible.
1728  */
1729 static bool
1730 choose_hashed_grouping(PlannerInfo *root,
1731                                            double tuple_fraction, double limit_tuples,
1732                                            Path *cheapest_path, Path *sorted_path,
1733                                            double dNumGroups, AggClauseCounts *agg_counts)
1734 {
1735         int                     numGroupCols = list_length(root->parse->groupClause);
1736         double          cheapest_path_rows;
1737         int                     cheapest_path_width;
1738         Size            hashentrysize;
1739         List       *target_pathkeys;
1740         List       *current_pathkeys;
1741         Path            hashed_p;
1742         Path            sorted_p;
1743
1744         /* Prefer sorting when enable_hashagg is off */
1745         if (!enable_hashagg)
1746                 return false;
1747
1748         /*
1749          * Don't do it if it doesn't look like the hashtable will fit into
1750          * work_mem.
1751          *
1752          * Beware here of the possibility that cheapest_path->parent is NULL. This
1753          * could happen if user does something silly like SELECT 'foo' GROUP BY 1;
1754          */
1755         if (cheapest_path->parent)
1756         {
1757                 cheapest_path_rows = cheapest_path->parent->rows;
1758                 cheapest_path_width = cheapest_path->parent->width;
1759         }
1760         else
1761         {
1762                 cheapest_path_rows = 1; /* assume non-set result */
1763                 cheapest_path_width = 100;              /* arbitrary */
1764         }
1765
1766         /* Estimate per-hash-entry space at tuple width... */
1767         hashentrysize = MAXALIGN(cheapest_path_width) + MAXALIGN(sizeof(MinimalTupleData));
1768         /* plus space for pass-by-ref transition values... */
1769         hashentrysize += agg_counts->transitionSpace;
1770         /* plus the per-hash-entry overhead */
1771         hashentrysize += hash_agg_entry_size(agg_counts->numAggs);
1772
1773         if (hashentrysize * dNumGroups > work_mem * 1024L)
1774                 return false;
1775
1776         /*
1777          * When we have both GROUP BY and DISTINCT, use the more-rigorous of
1778          * DISTINCT and ORDER BY as the assumed required output sort order.
1779          * This is an oversimplification because the DISTINCT might get
1780          * implemented via hashing, but it's not clear that the case is common
1781          * enough (or that our estimates are good enough) to justify trying to
1782          * solve it exactly.
1783          */
1784         if (list_length(root->distinct_pathkeys) >
1785                 list_length(root->sort_pathkeys))
1786                 target_pathkeys = root->distinct_pathkeys;
1787         else
1788                 target_pathkeys = root->sort_pathkeys;
1789
1790         /*
1791          * See if the estimated cost is no more than doing it the other way. While
1792          * avoiding the need for sorted input is usually a win, the fact that the
1793          * output won't be sorted may be a loss; so we need to do an actual cost
1794          * comparison.
1795          *
1796          * We need to consider cheapest_path + hashagg [+ final sort] versus
1797          * either cheapest_path [+ sort] + group or agg [+ final sort] or
1798          * presorted_path + group or agg [+ final sort] where brackets indicate a
1799          * step that may not be needed. We assume query_planner() will have
1800          * returned a presorted path only if it's a winner compared to
1801          * cheapest_path for this purpose.
1802          *
1803          * These path variables are dummies that just hold cost fields; we don't
1804          * make actual Paths for these steps.
1805          */
1806         cost_agg(&hashed_p, root, AGG_HASHED, agg_counts->numAggs,
1807                          numGroupCols, dNumGroups,
1808                          cheapest_path->startup_cost, cheapest_path->total_cost,
1809                          cheapest_path_rows);
1810         /* Result of hashed agg is always unsorted */
1811         if (target_pathkeys)
1812                 cost_sort(&hashed_p, root, target_pathkeys, hashed_p.total_cost,
1813                                   dNumGroups, cheapest_path_width, limit_tuples);
1814
1815         if (sorted_path)
1816         {
1817                 sorted_p.startup_cost = sorted_path->startup_cost;
1818                 sorted_p.total_cost = sorted_path->total_cost;
1819                 current_pathkeys = sorted_path->pathkeys;
1820         }
1821         else
1822         {
1823                 sorted_p.startup_cost = cheapest_path->startup_cost;
1824                 sorted_p.total_cost = cheapest_path->total_cost;
1825                 current_pathkeys = cheapest_path->pathkeys;
1826         }
1827         if (!pathkeys_contained_in(root->group_pathkeys, current_pathkeys))
1828         {
1829                 cost_sort(&sorted_p, root, root->group_pathkeys, sorted_p.total_cost,
1830                                   cheapest_path_rows, cheapest_path_width, -1.0);
1831                 current_pathkeys = root->group_pathkeys;
1832         }
1833
1834         if (root->parse->hasAggs)
1835                 cost_agg(&sorted_p, root, AGG_SORTED, agg_counts->numAggs,
1836                                  numGroupCols, dNumGroups,
1837                                  sorted_p.startup_cost, sorted_p.total_cost,
1838                                  cheapest_path_rows);
1839         else
1840                 cost_group(&sorted_p, root, numGroupCols, dNumGroups,
1841                                    sorted_p.startup_cost, sorted_p.total_cost,
1842                                    cheapest_path_rows);
1843         /* The Agg or Group node will preserve ordering */
1844         if (target_pathkeys &&
1845                 !pathkeys_contained_in(target_pathkeys, current_pathkeys))
1846                 cost_sort(&sorted_p, root, target_pathkeys, sorted_p.total_cost,
1847                                   dNumGroups, cheapest_path_width, limit_tuples);
1848
1849         /*
1850          * Now make the decision using the top-level tuple fraction.  First we
1851          * have to convert an absolute count (LIMIT) into fractional form.
1852          */
1853         if (tuple_fraction >= 1.0)
1854                 tuple_fraction /= dNumGroups;
1855
1856         if (compare_fractional_path_costs(&hashed_p, &sorted_p,
1857                                                                           tuple_fraction) < 0)
1858         {
1859                 /* Hashed is cheaper, so use it */
1860                 return true;
1861         }
1862         return false;
1863 }
1864
1865 /*
1866  * choose_hashed_distinct - should we use hashing for DISTINCT?
1867  *
1868  * This is fairly similar to choose_hashed_grouping, but there are enough
1869  * differences that it doesn't seem worth trying to unify the two functions.
1870  *
1871  * But note that making the two choices independently is a bit bogus in
1872  * itself.  If the two could be combined into a single choice operation
1873  * it'd probably be better, but that seems far too unwieldy to be practical,
1874  * especially considering that the combination of GROUP BY and DISTINCT
1875  * isn't very common in real queries.  By separating them, we are giving
1876  * extra preference to using a sorting implementation when a common sort key
1877  * is available ... and that's not necessarily wrong anyway.
1878  *
1879  * Note: this is only applied when both alternatives are actually feasible.
1880  */
1881 static bool
1882 choose_hashed_distinct(PlannerInfo *root,
1883                                            Plan *input_plan, List *input_pathkeys,
1884                                            double tuple_fraction, double limit_tuples,
1885                                            double dNumDistinctRows)
1886 {
1887         int                     numDistinctCols = list_length(root->parse->distinctClause);
1888         Size            hashentrysize;
1889         List       *current_pathkeys;
1890         List       *needed_pathkeys;
1891         Path            hashed_p;
1892         Path            sorted_p;
1893
1894         /* Prefer sorting when enable_hashagg is off */
1895         if (!enable_hashagg)
1896                 return false;
1897
1898         /*
1899          * Don't do it if it doesn't look like the hashtable will fit into
1900          * work_mem.
1901          */
1902         hashentrysize = MAXALIGN(input_plan->plan_width) + MAXALIGN(sizeof(MinimalTupleData));
1903
1904         if (hashentrysize * dNumDistinctRows > work_mem * 1024L)
1905                 return false;
1906
1907         /*
1908          * See if the estimated cost is no more than doing it the other way. While
1909          * avoiding the need for sorted input is usually a win, the fact that the
1910          * output won't be sorted may be a loss; so we need to do an actual cost
1911          * comparison.
1912          *
1913          * We need to consider input_plan + hashagg [+ final sort] versus
1914          * input_plan [+ sort] + group [+ final sort] where brackets indicate
1915          * a step that may not be needed.
1916          *
1917          * These path variables are dummies that just hold cost fields; we don't
1918          * make actual Paths for these steps.
1919          */
1920         cost_agg(&hashed_p, root, AGG_HASHED, 0,
1921                          numDistinctCols, dNumDistinctRows,
1922                          input_plan->startup_cost, input_plan->total_cost,
1923                          input_plan->plan_rows);
1924         /*
1925          * Result of hashed agg is always unsorted, so if ORDER BY is present
1926          * we need to charge for the final sort.
1927          */
1928         if (root->parse->sortClause)
1929                 cost_sort(&hashed_p, root, root->sort_pathkeys, hashed_p.total_cost,
1930                                   dNumDistinctRows, input_plan->plan_width, limit_tuples);
1931
1932         /*
1933          * Now for the GROUP case.  See comments in grouping_planner about the
1934          * sorting choices here --- this code should match that code.
1935          */
1936         sorted_p.startup_cost = input_plan->startup_cost;
1937         sorted_p.total_cost = input_plan->total_cost;
1938         current_pathkeys = input_pathkeys;
1939         if (root->parse->hasDistinctOn &&
1940                 list_length(root->distinct_pathkeys) <
1941                 list_length(root->sort_pathkeys))
1942                 needed_pathkeys = root->sort_pathkeys;
1943         else
1944                 needed_pathkeys = root->distinct_pathkeys;
1945         if (!pathkeys_contained_in(needed_pathkeys, current_pathkeys))
1946         {
1947                 if (list_length(root->distinct_pathkeys) >=
1948                         list_length(root->sort_pathkeys))
1949                         current_pathkeys = root->distinct_pathkeys;
1950                 else
1951                         current_pathkeys = root->sort_pathkeys;
1952                 cost_sort(&sorted_p, root, current_pathkeys, sorted_p.total_cost,
1953                                   input_plan->plan_rows, input_plan->plan_width, -1.0);
1954         }
1955         cost_group(&sorted_p, root, numDistinctCols, dNumDistinctRows,
1956                            sorted_p.startup_cost, sorted_p.total_cost,
1957                            input_plan->plan_rows);
1958         if (root->parse->sortClause &&
1959                 !pathkeys_contained_in(root->sort_pathkeys, current_pathkeys))
1960                 cost_sort(&sorted_p, root, root->sort_pathkeys, sorted_p.total_cost,
1961                                   dNumDistinctRows, input_plan->plan_width, limit_tuples);
1962
1963         /*
1964          * Now make the decision using the top-level tuple fraction.  First we
1965          * have to convert an absolute count (LIMIT) into fractional form.
1966          */
1967         if (tuple_fraction >= 1.0)
1968                 tuple_fraction /= dNumDistinctRows;
1969
1970         if (compare_fractional_path_costs(&hashed_p, &sorted_p,
1971                                                                           tuple_fraction) < 0)
1972         {
1973                 /* Hashed is cheaper, so use it */
1974                 return true;
1975         }
1976         return false;
1977 }
1978
1979 /*---------------
1980  * make_subplanTargetList
1981  *        Generate appropriate target list when grouping is required.
1982  *
1983  * When grouping_planner inserts Aggregate, Group, or Result plan nodes
1984  * above the result of query_planner, we typically want to pass a different
1985  * target list to query_planner than the outer plan nodes should have.
1986  * This routine generates the correct target list for the subplan.
1987  *
1988  * The initial target list passed from the parser already contains entries
1989  * for all ORDER BY and GROUP BY expressions, but it will not have entries
1990  * for variables used only in HAVING clauses; so we need to add those
1991  * variables to the subplan target list.  Also, we flatten all expressions
1992  * except GROUP BY items into their component variables; the other expressions
1993  * will be computed by the inserted nodes rather than by the subplan.
1994  * For example, given a query like
1995  *              SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
1996  * we want to pass this targetlist to the subplan:
1997  *              a,b,c,d,a+b
1998  * where the a+b target will be used by the Sort/Group steps, and the
1999  * other targets will be used for computing the final results.  (In the
2000  * above example we could theoretically suppress the a and b targets and
2001  * pass down only c,d,a+b, but it's not really worth the trouble to
2002  * eliminate simple var references from the subplan.  We will avoid doing
2003  * the extra computation to recompute a+b at the outer level; see
2004  * fix_upper_expr() in setrefs.c.)
2005  *
2006  * If we are grouping or aggregating, *and* there are no non-Var grouping
2007  * expressions, then the returned tlist is effectively dummy; we do not
2008  * need to force it to be evaluated, because all the Vars it contains
2009  * should be present in the output of query_planner anyway.
2010  *
2011  * 'tlist' is the query's target list.
2012  * 'groupColIdx' receives an array of column numbers for the GROUP BY
2013  *                      expressions (if there are any) in the subplan's target list.
2014  * 'need_tlist_eval' is set true if we really need to evaluate the
2015  *                      result tlist.
2016  *
2017  * The result is the targetlist to be passed to the subplan.
2018  *---------------
2019  */
2020 static List *
2021 make_subplanTargetList(PlannerInfo *root,
2022                                            List *tlist,
2023                                            AttrNumber **groupColIdx,
2024                                            bool *need_tlist_eval)
2025 {
2026         Query      *parse = root->parse;
2027         List       *sub_tlist;
2028         List       *extravars;
2029         int                     numCols;
2030
2031         *groupColIdx = NULL;
2032
2033         /*
2034          * If we're not grouping or aggregating, there's nothing to do here;
2035          * query_planner should receive the unmodified target list.
2036          */
2037         if (!parse->hasAggs && !parse->groupClause && !root->hasHavingQual)
2038         {
2039                 *need_tlist_eval = true;
2040                 return tlist;
2041         }
2042
2043         /*
2044          * Otherwise, start with a "flattened" tlist (having just the vars
2045          * mentioned in the targetlist and HAVING qual --- but not upper-level
2046          * Vars; they will be replaced by Params later on).
2047          */
2048         sub_tlist = flatten_tlist(tlist);
2049         extravars = pull_var_clause(parse->havingQual, false);
2050         sub_tlist = add_to_flat_tlist(sub_tlist, extravars);
2051         list_free(extravars);
2052         *need_tlist_eval = false;       /* only eval if not flat tlist */
2053
2054         /*
2055          * If grouping, create sub_tlist entries for all GROUP BY expressions
2056          * (GROUP BY items that are simple Vars should be in the list already),
2057          * and make an array showing where the group columns are in the sub_tlist.
2058          */
2059         numCols = list_length(parse->groupClause);
2060         if (numCols > 0)
2061         {
2062                 int                     keyno = 0;
2063                 AttrNumber *grpColIdx;
2064                 ListCell   *gl;
2065
2066                 grpColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
2067                 *groupColIdx = grpColIdx;
2068
2069                 foreach(gl, parse->groupClause)
2070                 {
2071                         SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2072                         Node       *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
2073                         TargetEntry *te = NULL;
2074
2075                         /*
2076                          * Find or make a matching sub_tlist entry.  If the groupexpr
2077                          * isn't a Var, no point in searching.  (Note that the parser
2078                          * won't make multiple groupClause entries for the same TLE.)
2079                          */
2080                         if (groupexpr && IsA(groupexpr, Var))
2081                         {
2082                                 ListCell   *sl;
2083
2084                                 foreach(sl, sub_tlist)
2085                                 {
2086                                         TargetEntry *lte = (TargetEntry *) lfirst(sl);
2087
2088                                         if (equal(groupexpr, lte->expr))
2089                                         {
2090                                                 te = lte;
2091                                                 break;
2092                                         }
2093                                 }
2094                         }
2095                         if (!te)
2096                         {
2097                                 te = makeTargetEntry((Expr *) groupexpr,
2098                                                                          list_length(sub_tlist) + 1,
2099                                                                          NULL,
2100                                                                          false);
2101                                 sub_tlist = lappend(sub_tlist, te);
2102                                 *need_tlist_eval = true;                /* it's not flat anymore */
2103                         }
2104
2105                         /* and save its resno */
2106                         grpColIdx[keyno++] = te->resno;
2107                 }
2108         }
2109
2110         return sub_tlist;
2111 }
2112
2113 /*
2114  * locate_grouping_columns
2115  *              Locate grouping columns in the tlist chosen by query_planner.
2116  *
2117  * This is only needed if we don't use the sub_tlist chosen by
2118  * make_subplanTargetList.      We have to forget the column indexes found
2119  * by that routine and re-locate the grouping vars in the real sub_tlist.
2120  */
2121 static void
2122 locate_grouping_columns(PlannerInfo *root,
2123                                                 List *tlist,
2124                                                 List *sub_tlist,
2125                                                 AttrNumber *groupColIdx)
2126 {
2127         int                     keyno = 0;
2128         ListCell   *gl;
2129
2130         /*
2131          * No work unless grouping.
2132          */
2133         if (!root->parse->groupClause)
2134         {
2135                 Assert(groupColIdx == NULL);
2136                 return;
2137         }
2138         Assert(groupColIdx != NULL);
2139
2140         foreach(gl, root->parse->groupClause)
2141         {
2142                 SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2143                 Node       *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
2144                 TargetEntry *te = NULL;
2145                 ListCell   *sl;
2146
2147                 foreach(sl, sub_tlist)
2148                 {
2149                         te = (TargetEntry *) lfirst(sl);
2150                         if (equal(groupexpr, te->expr))
2151                                 break;
2152                 }
2153                 if (!sl)
2154                         elog(ERROR, "failed to locate grouping columns");
2155
2156                 groupColIdx[keyno++] = te->resno;
2157         }
2158 }
2159
2160 /*
2161  * postprocess_setop_tlist
2162  *        Fix up targetlist returned by plan_set_operations().
2163  *
2164  * We need to transpose sort key info from the orig_tlist into new_tlist.
2165  * NOTE: this would not be good enough if we supported resjunk sort keys
2166  * for results of set operations --- then, we'd need to project a whole
2167  * new tlist to evaluate the resjunk columns.  For now, just ereport if we
2168  * find any resjunk columns in orig_tlist.
2169  */
2170 static List *
2171 postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
2172 {
2173         ListCell   *l;
2174         ListCell   *orig_tlist_item = list_head(orig_tlist);
2175
2176         foreach(l, new_tlist)
2177         {
2178                 TargetEntry *new_tle = (TargetEntry *) lfirst(l);
2179                 TargetEntry *orig_tle;
2180
2181                 /* ignore resjunk columns in setop result */
2182                 if (new_tle->resjunk)
2183                         continue;
2184
2185                 Assert(orig_tlist_item != NULL);
2186                 orig_tle = (TargetEntry *) lfirst(orig_tlist_item);
2187                 orig_tlist_item = lnext(orig_tlist_item);
2188                 if (orig_tle->resjunk)  /* should not happen */
2189                         elog(ERROR, "resjunk output columns are not implemented");
2190                 Assert(new_tle->resno == orig_tle->resno);
2191                 new_tle->ressortgroupref = orig_tle->ressortgroupref;
2192         }
2193         if (orig_tlist_item != NULL)
2194                 elog(ERROR, "resjunk output columns are not implemented");
2195         return new_tlist;
2196 }