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