]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/planner.c
Fix PARAM_EXEC assignment mechanism to be safe in the presence of WITH.
[postgresql] / src / backend / optimizer / plan / planner.c
1 /*-------------------------------------------------------------------------
2  *
3  * planner.c
4  *        The query optimizer external interface.
5  *
6  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/optimizer/plan/planner.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include <limits.h>
19
20 #include "access/htup_details.h"
21 #include "executor/executor.h"
22 #include "executor/nodeAgg.h"
23 #include "miscadmin.h"
24 #include "nodes/makefuncs.h"
25 #ifdef OPTIMIZER_DEBUG
26 #include "nodes/print.h"
27 #endif
28 #include "optimizer/clauses.h"
29 #include "optimizer/cost.h"
30 #include "optimizer/pathnode.h"
31 #include "optimizer/paths.h"
32 #include "optimizer/plancat.h"
33 #include "optimizer/planmain.h"
34 #include "optimizer/planner.h"
35 #include "optimizer/prep.h"
36 #include "optimizer/subselect.h"
37 #include "optimizer/tlist.h"
38 #include "parser/analyze.h"
39 #include "parser/parsetree.h"
40 #include "rewrite/rewriteManip.h"
41 #include "utils/rel.h"
42
43
44 /* GUC parameter */
45 double          cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;
46
47 /* Hook for plugins to get control in planner() */
48 planner_hook_type planner_hook = NULL;
49
50
51 /* Expression kind codes for preprocess_expression */
52 #define EXPRKIND_QUAL                   0
53 #define EXPRKIND_TARGET                 1
54 #define EXPRKIND_RTFUNC                 2
55 #define EXPRKIND_RTFUNC_LATERAL 3
56 #define EXPRKIND_VALUES                 4
57 #define EXPRKIND_VALUES_LATERAL 5
58 #define EXPRKIND_LIMIT                  6
59 #define EXPRKIND_APPINFO                7
60 #define EXPRKIND_PHV                    8
61
62
63 static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
64 static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
65 static Plan *inheritance_planner(PlannerInfo *root);
66 static Plan *grouping_planner(PlannerInfo *root, double tuple_fraction);
67 static void preprocess_rowmarks(PlannerInfo *root);
68 static double preprocess_limit(PlannerInfo *root,
69                                  double tuple_fraction,
70                                  int64 *offset_est, int64 *count_est);
71 static void preprocess_groupclause(PlannerInfo *root);
72 static bool choose_hashed_grouping(PlannerInfo *root,
73                                            double tuple_fraction, double limit_tuples,
74                                            double path_rows, int path_width,
75                                            Path *cheapest_path, Path *sorted_path,
76                                            double dNumGroups, AggClauseCosts *agg_costs);
77 static bool choose_hashed_distinct(PlannerInfo *root,
78                                            double tuple_fraction, double limit_tuples,
79                                            double path_rows, int path_width,
80                                            Cost cheapest_startup_cost, Cost cheapest_total_cost,
81                                            Cost sorted_startup_cost, Cost sorted_total_cost,
82                                            List *sorted_pathkeys,
83                                            double dNumDistinctRows);
84 static List *make_subplanTargetList(PlannerInfo *root, List *tlist,
85                                            AttrNumber **groupColIdx, bool *need_tlist_eval);
86 static int      get_grouping_column_index(Query *parse, TargetEntry *tle);
87 static void locate_grouping_columns(PlannerInfo *root,
88                                                 List *tlist,
89                                                 List *sub_tlist,
90                                                 AttrNumber *groupColIdx);
91 static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
92 static List *select_active_windows(PlannerInfo *root, WindowFuncLists *wflists);
93 static List *add_volatile_sort_exprs(List *window_tlist, List *tlist,
94                                                 List *activeWindows);
95 static List *make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
96                                                  List *tlist, bool canonicalize);
97 static void get_column_info_for_window(PlannerInfo *root, WindowClause *wc,
98                                                    List *tlist,
99                                                    int numSortCols, AttrNumber *sortColIdx,
100                                                    int *partNumCols,
101                                                    AttrNumber **partColIdx,
102                                                    Oid **partOperators,
103                                                    int *ordNumCols,
104                                                    AttrNumber **ordColIdx,
105                                                    Oid **ordOperators);
106
107
108 /*****************************************************************************
109  *
110  *         Query optimizer entry point
111  *
112  * To support loadable plugins that monitor or modify planner behavior,
113  * we provide a hook variable that lets a plugin get control before and
114  * after the standard planning process.  The plugin would normally call
115  * standard_planner().
116  *
117  * Note to plugin authors: standard_planner() scribbles on its Query input,
118  * so you'd better copy that data structure if you want to plan more than once.
119  *
120  *****************************************************************************/
121 PlannedStmt *
122 planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
123 {
124         PlannedStmt *result;
125
126         if (planner_hook)
127                 result = (*planner_hook) (parse, cursorOptions, boundParams);
128         else
129                 result = standard_planner(parse, cursorOptions, boundParams);
130         return result;
131 }
132
133 PlannedStmt *
134 standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
135 {
136         PlannedStmt *result;
137         PlannerGlobal *glob;
138         double          tuple_fraction;
139         PlannerInfo *root;
140         Plan       *top_plan;
141         ListCell   *lp,
142                            *lr;
143
144         /* Cursor options may come from caller or from DECLARE CURSOR stmt */
145         if (parse->utilityStmt &&
146                 IsA(parse->utilityStmt, DeclareCursorStmt))
147                 cursorOptions |= ((DeclareCursorStmt *) parse->utilityStmt)->options;
148
149         /*
150          * Set up global state for this planner invocation.  This data is needed
151          * across all levels of sub-Query that might exist in the given command,
152          * so we keep it in a separate struct that's linked to by each per-Query
153          * PlannerInfo.
154          */
155         glob = makeNode(PlannerGlobal);
156
157         glob->boundParams = boundParams;
158         glob->subplans = NIL;
159         glob->subroots = NIL;
160         glob->rewindPlanIDs = NULL;
161         glob->finalrtable = NIL;
162         glob->finalrowmarks = NIL;
163         glob->resultRelations = NIL;
164         glob->relationOids = NIL;
165         glob->invalItems = NIL;
166         glob->nParamExec = 0;
167         glob->lastPHId = 0;
168         glob->lastRowMarkId = 0;
169         glob->transientPlan = false;
170
171         /* Determine what fraction of the plan is likely to be scanned */
172         if (cursorOptions & CURSOR_OPT_FAST_PLAN)
173         {
174                 /*
175                  * We have no real idea how many tuples the user will ultimately FETCH
176                  * from a cursor, but it is often the case that he doesn't want 'em
177                  * all, or would prefer a fast-start plan anyway so that he can
178                  * process some of the tuples sooner.  Use a GUC parameter to decide
179                  * what fraction to optimize for.
180                  */
181                 tuple_fraction = cursor_tuple_fraction;
182
183                 /*
184                  * We document cursor_tuple_fraction as simply being a fraction, which
185                  * means the edge cases 0 and 1 have to be treated specially here.      We
186                  * convert 1 to 0 ("all the tuples") and 0 to a very small fraction.
187                  */
188                 if (tuple_fraction >= 1.0)
189                         tuple_fraction = 0.0;
190                 else if (tuple_fraction <= 0.0)
191                         tuple_fraction = 1e-10;
192         }
193         else
194         {
195                 /* Default assumption is we need all the tuples */
196                 tuple_fraction = 0.0;
197         }
198
199         /* primary planning entry point (may recurse for subqueries) */
200         top_plan = subquery_planner(glob, parse, NULL,
201                                                                 false, tuple_fraction, &root);
202
203         /*
204          * If creating a plan for a scrollable cursor, make sure it can run
205          * backwards on demand.  Add a Material node at the top at need.
206          */
207         if (cursorOptions & CURSOR_OPT_SCROLL)
208         {
209                 if (!ExecSupportsBackwardScan(top_plan))
210                         top_plan = materialize_finished_plan(top_plan);
211         }
212
213         /* final cleanup of the plan */
214         Assert(glob->finalrtable == NIL);
215         Assert(glob->finalrowmarks == NIL);
216         Assert(glob->resultRelations == NIL);
217         top_plan = set_plan_references(root, top_plan);
218         /* ... and the subplans (both regular subplans and initplans) */
219         Assert(list_length(glob->subplans) == list_length(glob->subroots));
220         forboth(lp, glob->subplans, lr, glob->subroots)
221         {
222                 Plan       *subplan = (Plan *) lfirst(lp);
223                 PlannerInfo *subroot = (PlannerInfo *) lfirst(lr);
224
225                 lfirst(lp) = set_plan_references(subroot, subplan);
226         }
227
228         /* build the PlannedStmt result */
229         result = makeNode(PlannedStmt);
230
231         result->commandType = parse->commandType;
232         result->queryId = parse->queryId;
233         result->hasReturning = (parse->returningList != NIL);
234         result->hasModifyingCTE = parse->hasModifyingCTE;
235         result->canSetTag = parse->canSetTag;
236         result->transientPlan = glob->transientPlan;
237         result->planTree = top_plan;
238         result->rtable = glob->finalrtable;
239         result->resultRelations = glob->resultRelations;
240         result->utilityStmt = parse->utilityStmt;
241         result->subplans = glob->subplans;
242         result->rewindPlanIDs = glob->rewindPlanIDs;
243         result->rowMarks = glob->finalrowmarks;
244         result->relationOids = glob->relationOids;
245         result->invalItems = glob->invalItems;
246         result->nParamExec = glob->nParamExec;
247
248         return result;
249 }
250
251
252 /*--------------------
253  * subquery_planner
254  *        Invokes the planner on a subquery.  We recurse to here for each
255  *        sub-SELECT found in the query tree.
256  *
257  * glob is the global state for the current planner run.
258  * parse is the querytree produced by the parser & rewriter.
259  * parent_root is the immediate parent Query's info (NULL at the top level).
260  * hasRecursion is true if this is a recursive WITH query.
261  * tuple_fraction is the fraction of tuples we expect will be retrieved.
262  * tuple_fraction is interpreted as explained for grouping_planner, below.
263  *
264  * If subroot isn't NULL, we pass back the query's final PlannerInfo struct;
265  * among other things this tells the output sort ordering of the plan.
266  *
267  * Basically, this routine does the stuff that should only be done once
268  * per Query object.  It then calls grouping_planner.  At one time,
269  * grouping_planner could be invoked recursively on the same Query object;
270  * that's not currently true, but we keep the separation between the two
271  * routines anyway, in case we need it again someday.
272  *
273  * subquery_planner will be called recursively to handle sub-Query nodes
274  * found within the query's expressions and rangetable.
275  *
276  * Returns a query plan.
277  *--------------------
278  */
279 Plan *
280 subquery_planner(PlannerGlobal *glob, Query *parse,
281                                  PlannerInfo *parent_root,
282                                  bool hasRecursion, double tuple_fraction,
283                                  PlannerInfo **subroot)
284 {
285         int                     num_old_subplans = list_length(glob->subplans);
286         PlannerInfo *root;
287         Plan       *plan;
288         List       *newHaving;
289         bool            hasOuterJoins;
290         ListCell   *l;
291
292         /* Create a PlannerInfo data structure for this subquery */
293         root = makeNode(PlannerInfo);
294         root->parse = parse;
295         root->glob = glob;
296         root->query_level = parent_root ? parent_root->query_level + 1 : 1;
297         root->parent_root = parent_root;
298         root->plan_params = NIL;
299         root->planner_cxt = CurrentMemoryContext;
300         root->init_plans = NIL;
301         root->cte_plan_ids = NIL;
302         root->eq_classes = NIL;
303         root->append_rel_list = NIL;
304         root->rowMarks = NIL;
305         root->hasInheritedTarget = false;
306
307         root->hasRecursion = hasRecursion;
308         if (hasRecursion)
309                 root->wt_param_id = SS_assign_special_param(root);
310         else
311                 root->wt_param_id = -1;
312         root->non_recursive_plan = NULL;
313
314         /*
315          * If there is a WITH list, process each WITH query and build an initplan
316          * SubPlan structure for it.
317          */
318         if (parse->cteList)
319                 SS_process_ctes(root);
320
321         /*
322          * Look for ANY and EXISTS SubLinks in WHERE and JOIN/ON clauses, and try
323          * to transform them into joins.  Note that this step does not descend
324          * into subqueries; if we pull up any subqueries below, their SubLinks are
325          * processed just before pulling them up.
326          */
327         if (parse->hasSubLinks)
328                 pull_up_sublinks(root);
329
330         /*
331          * Scan the rangetable for set-returning functions, and inline them if
332          * possible (producing subqueries that might get pulled up next).
333          * Recursion issues here are handled in the same way as for SubLinks.
334          */
335         inline_set_returning_functions(root);
336
337         /*
338          * Check to see if any subqueries in the jointree can be merged into this
339          * query.
340          */
341         parse->jointree = (FromExpr *)
342                 pull_up_subqueries(root, (Node *) parse->jointree);
343
344         /*
345          * If this is a simple UNION ALL query, flatten it into an appendrel. We
346          * do this now because it requires applying pull_up_subqueries to the leaf
347          * queries of the UNION ALL, which weren't touched above because they
348          * weren't referenced by the jointree (they will be after we do this).
349          */
350         if (parse->setOperations)
351                 flatten_simple_union_all(root);
352
353         /*
354          * Detect whether any rangetable entries are RTE_JOIN kind; if not, we can
355          * avoid the expense of doing flatten_join_alias_vars().  Also check for
356          * outer joins --- if none, we can skip reduce_outer_joins().  And check
357          * for LATERAL RTEs, too.  This must be done after we have done
358          * pull_up_subqueries(), of course.
359          */
360         root->hasJoinRTEs = false;
361         root->hasLateralRTEs = false;
362         hasOuterJoins = false;
363         foreach(l, parse->rtable)
364         {
365                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
366
367                 if (rte->rtekind == RTE_JOIN)
368                 {
369                         root->hasJoinRTEs = true;
370                         if (IS_OUTER_JOIN(rte->jointype))
371                                 hasOuterJoins = true;
372                 }
373                 if (rte->lateral)
374                         root->hasLateralRTEs = true;
375         }
376
377         /*
378          * Preprocess RowMark information.      We need to do this after subquery
379          * pullup (so that all non-inherited RTEs are present) and before
380          * inheritance expansion (so that the info is available for
381          * expand_inherited_tables to examine and modify).
382          */
383         preprocess_rowmarks(root);
384
385         /*
386          * Expand any rangetable entries that are inheritance sets into "append
387          * relations".  This can add entries to the rangetable, but they must be
388          * plain base relations not joins, so it's OK (and marginally more
389          * efficient) to do it after checking for join RTEs.  We must do it after
390          * pulling up subqueries, else we'd fail to handle inherited tables in
391          * subqueries.
392          */
393         expand_inherited_tables(root);
394
395         /*
396          * Set hasHavingQual to remember if HAVING clause is present.  Needed
397          * because preprocess_expression will reduce a constant-true condition to
398          * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
399          */
400         root->hasHavingQual = (parse->havingQual != NULL);
401
402         /* Clear this flag; might get set in distribute_qual_to_rels */
403         root->hasPseudoConstantQuals = false;
404
405         /*
406          * Do expression preprocessing on targetlist and quals, as well as other
407          * random expressions in the querytree.  Note that we do not need to
408          * handle sort/group expressions explicitly, because they are actually
409          * part of the targetlist.
410          */
411         parse->targetList = (List *)
412                 preprocess_expression(root, (Node *) parse->targetList,
413                                                           EXPRKIND_TARGET);
414
415         parse->returningList = (List *)
416                 preprocess_expression(root, (Node *) parse->returningList,
417                                                           EXPRKIND_TARGET);
418
419         preprocess_qual_conditions(root, (Node *) parse->jointree);
420
421         parse->havingQual = preprocess_expression(root, parse->havingQual,
422                                                                                           EXPRKIND_QUAL);
423
424         foreach(l, parse->windowClause)
425         {
426                 WindowClause *wc = (WindowClause *) lfirst(l);
427
428                 /* partitionClause/orderClause are sort/group expressions */
429                 wc->startOffset = preprocess_expression(root, wc->startOffset,
430                                                                                                 EXPRKIND_LIMIT);
431                 wc->endOffset = preprocess_expression(root, wc->endOffset,
432                                                                                           EXPRKIND_LIMIT);
433         }
434
435         parse->limitOffset = preprocess_expression(root, parse->limitOffset,
436                                                                                            EXPRKIND_LIMIT);
437         parse->limitCount = preprocess_expression(root, parse->limitCount,
438                                                                                           EXPRKIND_LIMIT);
439
440         root->append_rel_list = (List *)
441                 preprocess_expression(root, (Node *) root->append_rel_list,
442                                                           EXPRKIND_APPINFO);
443
444         /* Also need to preprocess expressions within RTEs */
445         foreach(l, parse->rtable)
446         {
447                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
448                 int                     kind;
449
450                 if (rte->rtekind == RTE_SUBQUERY)
451                 {
452                         /*
453                          * We don't want to do all preprocessing yet on the subquery's
454                          * expressions, since that will happen when we plan it.  But if it
455                          * contains any join aliases of our level, those have to get
456                          * expanded now, because planning of the subquery won't do it.
457                          * That's only possible if the subquery is LATERAL.
458                          */
459                         if (rte->lateral && root->hasJoinRTEs)
460                                 rte->subquery = (Query *)
461                                         flatten_join_alias_vars(root, (Node *) rte->subquery);
462                 }
463                 else if (rte->rtekind == RTE_FUNCTION)
464                 {
465                         /* Preprocess the function expression fully */
466                         kind = rte->lateral ? EXPRKIND_RTFUNC_LATERAL : EXPRKIND_RTFUNC;
467                         rte->funcexpr = preprocess_expression(root, rte->funcexpr, kind);
468                 }
469                 else if (rte->rtekind == RTE_VALUES)
470                 {
471                         /* Preprocess the values lists fully */
472                         kind = rte->lateral ? EXPRKIND_VALUES_LATERAL : EXPRKIND_VALUES;
473                         rte->values_lists = (List *)
474                                 preprocess_expression(root, (Node *) rte->values_lists, kind);
475                 }
476         }
477
478         /*
479          * In some cases we may want to transfer a HAVING clause into WHERE. We
480          * cannot do so if the HAVING clause contains aggregates (obviously) or
481          * volatile functions (since a HAVING clause is supposed to be executed
482          * only once per group).  Also, it may be that the clause is so expensive
483          * to execute that we're better off doing it only once per group, despite
484          * the loss of selectivity.  This is hard to estimate short of doing the
485          * entire planning process twice, so we use a heuristic: clauses
486          * containing subplans are left in HAVING.      Otherwise, we move or copy the
487          * HAVING clause into WHERE, in hopes of eliminating tuples before
488          * aggregation instead of after.
489          *
490          * If the query has explicit grouping then we can simply move such a
491          * clause into WHERE; any group that fails the clause will not be in the
492          * output because none of its tuples will reach the grouping or
493          * aggregation stage.  Otherwise we must have a degenerate (variable-free)
494          * HAVING clause, which we put in WHERE so that query_planner() can use it
495          * in a gating Result node, but also keep in HAVING to ensure that we
496          * don't emit a bogus aggregated row. (This could be done better, but it
497          * seems not worth optimizing.)
498          *
499          * Note that both havingQual and parse->jointree->quals are in
500          * implicitly-ANDed-list form at this point, even though they are declared
501          * as Node *.
502          */
503         newHaving = NIL;
504         foreach(l, (List *) parse->havingQual)
505         {
506                 Node       *havingclause = (Node *) lfirst(l);
507
508                 if (contain_agg_clause(havingclause) ||
509                         contain_volatile_functions(havingclause) ||
510                         contain_subplans(havingclause))
511                 {
512                         /* keep it in HAVING */
513                         newHaving = lappend(newHaving, havingclause);
514                 }
515                 else if (parse->groupClause)
516                 {
517                         /* move it to WHERE */
518                         parse->jointree->quals = (Node *)
519                                 lappend((List *) parse->jointree->quals, havingclause);
520                 }
521                 else
522                 {
523                         /* put a copy in WHERE, keep it in HAVING */
524                         parse->jointree->quals = (Node *)
525                                 lappend((List *) parse->jointree->quals,
526                                                 copyObject(havingclause));
527                         newHaving = lappend(newHaving, havingclause);
528                 }
529         }
530         parse->havingQual = (Node *) newHaving;
531
532         /*
533          * If we have any outer joins, try to reduce them to plain inner joins.
534          * This step is most easily done after we've done expression
535          * preprocessing.
536          */
537         if (hasOuterJoins)
538                 reduce_outer_joins(root);
539
540         /*
541          * Do the main planning.  If we have an inherited target relation, that
542          * needs special processing, else go straight to grouping_planner.
543          */
544         if (parse->resultRelation &&
545                 rt_fetch(parse->resultRelation, parse->rtable)->inh)
546                 plan = inheritance_planner(root);
547         else
548         {
549                 plan = grouping_planner(root, tuple_fraction);
550                 /* If it's not SELECT, we need a ModifyTable node */
551                 if (parse->commandType != CMD_SELECT)
552                 {
553                         List       *returningLists;
554                         List       *rowMarks;
555
556                         /*
557                          * Set up the RETURNING list-of-lists, if needed.
558                          */
559                         if (parse->returningList)
560                                 returningLists = list_make1(parse->returningList);
561                         else
562                                 returningLists = NIL;
563
564                         /*
565                          * If there was a FOR UPDATE/SHARE clause, the LockRows node will
566                          * have dealt with fetching non-locked marked rows, else we need
567                          * to have ModifyTable do that.
568                          */
569                         if (parse->rowMarks)
570                                 rowMarks = NIL;
571                         else
572                                 rowMarks = root->rowMarks;
573
574                         plan = (Plan *) make_modifytable(parse->commandType,
575                                                                                          parse->canSetTag,
576                                                                            list_make1_int(parse->resultRelation),
577                                                                                          list_make1(plan),
578                                                                                          returningLists,
579                                                                                          rowMarks,
580                                                                                          SS_assign_special_param(root));
581                 }
582         }
583
584         /*
585          * If any subplans were generated, or if there are any parameters to worry
586          * about, build initPlan list and extParam/allParam sets for plan nodes,
587          * and attach the initPlans to the top plan node.
588          */
589         if (list_length(glob->subplans) != num_old_subplans ||
590                 root->glob->nParamExec > 0)
591                 SS_finalize_plan(root, plan, true);
592
593         /* Return internal info if caller wants it */
594         if (subroot)
595                 *subroot = root;
596
597         return plan;
598 }
599
600 /*
601  * preprocess_expression
602  *              Do subquery_planner's preprocessing work for an expression,
603  *              which can be a targetlist, a WHERE clause (including JOIN/ON
604  *              conditions), a HAVING clause, or a few other things.
605  */
606 static Node *
607 preprocess_expression(PlannerInfo *root, Node *expr, int kind)
608 {
609         /*
610          * Fall out quickly if expression is empty.  This occurs often enough to
611          * be worth checking.  Note that null->null is the correct conversion for
612          * implicit-AND result format, too.
613          */
614         if (expr == NULL)
615                 return NULL;
616
617         /*
618          * If the query has any join RTEs, replace join alias variables with
619          * base-relation variables.  We must do this before sublink processing,
620          * else sublinks expanded out from join aliases would not get processed.
621          * We can skip it in non-lateral RTE functions and VALUES lists, however,
622          * since they can't contain any Vars of the current query level.
623          */
624         if (root->hasJoinRTEs &&
625                 !(kind == EXPRKIND_RTFUNC || kind == EXPRKIND_VALUES))
626                 expr = flatten_join_alias_vars(root, expr);
627
628         /*
629          * Simplify constant expressions.
630          *
631          * Note: an essential effect of this is to convert named-argument function
632          * calls to positional notation and insert the current actual values of
633          * any default arguments for functions.  To ensure that happens, we *must*
634          * process all expressions here.  Previous PG versions sometimes skipped
635          * const-simplification if it didn't seem worth the trouble, but we can't
636          * do that anymore.
637          *
638          * Note: this also flattens nested AND and OR expressions into N-argument
639          * form.  All processing of a qual expression after this point must be
640          * careful to maintain AND/OR flatness --- that is, do not generate a tree
641          * with AND directly under AND, nor OR directly under OR.
642          */
643         expr = eval_const_expressions(root, expr);
644
645         /*
646          * If it's a qual or havingQual, canonicalize it.
647          */
648         if (kind == EXPRKIND_QUAL)
649         {
650                 expr = (Node *) canonicalize_qual((Expr *) expr);
651
652 #ifdef OPTIMIZER_DEBUG
653                 printf("After canonicalize_qual()\n");
654                 pprint(expr);
655 #endif
656         }
657
658         /* Expand SubLinks to SubPlans */
659         if (root->parse->hasSubLinks)
660                 expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
661
662         /*
663          * XXX do not insert anything here unless you have grokked the comments in
664          * SS_replace_correlation_vars ...
665          */
666
667         /* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
668         if (root->query_level > 1)
669                 expr = SS_replace_correlation_vars(root, expr);
670
671         /*
672          * If it's a qual or havingQual, convert it to implicit-AND format. (We
673          * don't want to do this before eval_const_expressions, since the latter
674          * would be unable to simplify a top-level AND correctly. Also,
675          * SS_process_sublinks expects explicit-AND format.)
676          */
677         if (kind == EXPRKIND_QUAL)
678                 expr = (Node *) make_ands_implicit((Expr *) expr);
679
680         return expr;
681 }
682
683 /*
684  * preprocess_qual_conditions
685  *              Recursively scan the query's jointree and do subquery_planner's
686  *              preprocessing work on each qual condition found therein.
687  */
688 static void
689 preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
690 {
691         if (jtnode == NULL)
692                 return;
693         if (IsA(jtnode, RangeTblRef))
694         {
695                 /* nothing to do here */
696         }
697         else if (IsA(jtnode, FromExpr))
698         {
699                 FromExpr   *f = (FromExpr *) jtnode;
700                 ListCell   *l;
701
702                 foreach(l, f->fromlist)
703                         preprocess_qual_conditions(root, lfirst(l));
704
705                 f->quals = preprocess_expression(root, f->quals, EXPRKIND_QUAL);
706         }
707         else if (IsA(jtnode, JoinExpr))
708         {
709                 JoinExpr   *j = (JoinExpr *) jtnode;
710
711                 preprocess_qual_conditions(root, j->larg);
712                 preprocess_qual_conditions(root, j->rarg);
713
714                 j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
715         }
716         else
717                 elog(ERROR, "unrecognized node type: %d",
718                          (int) nodeTag(jtnode));
719 }
720
721 /*
722  * preprocess_phv_expression
723  *        Do preprocessing on a PlaceHolderVar expression that's been pulled up.
724  *
725  * If a LATERAL subquery references an output of another subquery, and that
726  * output must be wrapped in a PlaceHolderVar because of an intermediate outer
727  * join, then we'll push the PlaceHolderVar expression down into the subquery
728  * and later pull it back up during find_lateral_references, which runs after
729  * subquery_planner has preprocessed all the expressions that were in the
730  * current query level to start with.  So we need to preprocess it then.
731  */
732 Expr *
733 preprocess_phv_expression(PlannerInfo *root, Expr *expr)
734 {
735         return (Expr *) preprocess_expression(root, (Node *) expr, EXPRKIND_PHV);
736 }
737
738 /*
739  * inheritance_planner
740  *        Generate a plan in the case where the result relation is an
741  *        inheritance set.
742  *
743  * We have to handle this case differently from cases where a source relation
744  * is an inheritance set. Source inheritance is expanded at the bottom of the
745  * plan tree (see allpaths.c), but target inheritance has to be expanded at
746  * the top.  The reason is that for UPDATE, each target relation needs a
747  * different targetlist matching its own column set.  Fortunately,
748  * the UPDATE/DELETE target can never be the nullable side of an outer join,
749  * so it's OK to generate the plan this way.
750  *
751  * Returns a query plan.
752  */
753 static Plan *
754 inheritance_planner(PlannerInfo *root)
755 {
756         Query      *parse = root->parse;
757         int                     parentRTindex = parse->resultRelation;
758         List       *final_rtable = NIL;
759         int                     save_rel_array_size = 0;
760         RelOptInfo **save_rel_array = NULL;
761         List       *subplans = NIL;
762         List       *resultRelations = NIL;
763         List       *returningLists = NIL;
764         List       *rowMarks;
765         ListCell   *lc;
766
767         /*
768          * We generate a modified instance of the original Query for each target
769          * relation, plan that, and put all the plans into a list that will be
770          * controlled by a single ModifyTable node.  All the instances share the
771          * same rangetable, but each instance must have its own set of subquery
772          * RTEs within the finished rangetable because (1) they are likely to get
773          * scribbled on during planning, and (2) it's not inconceivable that
774          * subqueries could get planned differently in different cases.  We need
775          * not create duplicate copies of other RTE kinds, in particular not the
776          * target relations, because they don't have either of those issues.  Not
777          * having to duplicate the target relations is important because doing so
778          * (1) would result in a rangetable of length O(N^2) for N targets, with
779          * at least O(N^3) work expended here; and (2) would greatly complicate
780          * management of the rowMarks list.
781          */
782         foreach(lc, root->append_rel_list)
783         {
784                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
785                 PlannerInfo subroot;
786                 Plan       *subplan;
787                 Index           rti;
788
789                 /* append_rel_list contains all append rels; ignore others */
790                 if (appinfo->parent_relid != parentRTindex)
791                         continue;
792
793                 /*
794                  * We need a working copy of the PlannerInfo so that we can control
795                  * propagation of information back to the main copy.
796                  */
797                 memcpy(&subroot, root, sizeof(PlannerInfo));
798
799                 /*
800                  * Generate modified query with this rel as target.  We first apply
801                  * adjust_appendrel_attrs, which copies the Query and changes
802                  * references to the parent RTE to refer to the current child RTE,
803                  * then fool around with subquery RTEs.
804                  */
805                 subroot.parse = (Query *)
806                         adjust_appendrel_attrs(root,
807                                                                    (Node *) parse,
808                                                                    appinfo);
809
810                 /*
811                  * The rowMarks list might contain references to subquery RTEs, so
812                  * make a copy that we can apply ChangeVarNodes to.  (Fortunately, the
813                  * executor doesn't need to see the modified copies --- we can just
814                  * pass it the original rowMarks list.)
815                  */
816                 subroot.rowMarks = (List *) copyObject(root->rowMarks);
817
818                 /*
819                  * Add placeholders to the child Query's rangetable list to fill the
820                  * RT indexes already reserved for subqueries in previous children.
821                  * These won't be referenced, so there's no need to make them very
822                  * valid-looking.
823                  */
824                 while (list_length(subroot.parse->rtable) < list_length(final_rtable))
825                         subroot.parse->rtable = lappend(subroot.parse->rtable,
826                                                                                         makeNode(RangeTblEntry));
827
828                 /*
829                  * If this isn't the first child Query, generate duplicates of all
830                  * subquery RTEs, and adjust Var numbering to reference the
831                  * duplicates. To simplify the loop logic, we scan the original rtable
832                  * not the copy just made by adjust_appendrel_attrs; that should be OK
833                  * since subquery RTEs couldn't contain any references to the target
834                  * rel.
835                  */
836                 if (final_rtable != NIL)
837                 {
838                         ListCell   *lr;
839
840                         rti = 1;
841                         foreach(lr, parse->rtable)
842                         {
843                                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lr);
844
845                                 if (rte->rtekind == RTE_SUBQUERY)
846                                 {
847                                         Index           newrti;
848
849                                         /*
850                                          * The RTE can't contain any references to its own RT
851                                          * index, so we can save a few cycles by applying
852                                          * ChangeVarNodes before we append the RTE to the
853                                          * rangetable.
854                                          */
855                                         newrti = list_length(subroot.parse->rtable) + 1;
856                                         ChangeVarNodes((Node *) subroot.parse, rti, newrti, 0);
857                                         ChangeVarNodes((Node *) subroot.rowMarks, rti, newrti, 0);
858                                         rte = copyObject(rte);
859                                         subroot.parse->rtable = lappend(subroot.parse->rtable,
860                                                                                                         rte);
861                                 }
862                                 rti++;
863                         }
864                 }
865
866                 /* We needn't modify the child's append_rel_list */
867                 /* There shouldn't be any OJ or LATERAL info to translate, as yet */
868                 Assert(subroot.join_info_list == NIL);
869                 Assert(subroot.lateral_info_list == NIL);
870                 /* and we haven't created PlaceHolderInfos, either */
871                 Assert(subroot.placeholder_list == NIL);
872                 /* hack to mark target relation as an inheritance partition */
873                 subroot.hasInheritedTarget = true;
874
875                 /* Generate plan */
876                 subplan = grouping_planner(&subroot, 0.0 /* retrieve all tuples */ );
877
878                 /*
879                  * If this child rel was excluded by constraint exclusion, exclude it
880                  * from the result plan.
881                  */
882                 if (is_dummy_plan(subplan))
883                         continue;
884
885                 subplans = lappend(subplans, subplan);
886
887                 /*
888                  * If this is the first non-excluded child, its post-planning rtable
889                  * becomes the initial contents of final_rtable; otherwise, append
890                  * just its modified subquery RTEs to final_rtable.
891                  */
892                 if (final_rtable == NIL)
893                         final_rtable = subroot.parse->rtable;
894                 else
895                         final_rtable = list_concat(final_rtable,
896                                                                            list_copy_tail(subroot.parse->rtable,
897                                                                                                  list_length(final_rtable)));
898
899                 /*
900                  * We need to collect all the RelOptInfos from all child plans into
901                  * the main PlannerInfo, since setrefs.c will need them.  We use the
902                  * last child's simple_rel_array (previous ones are too short), so we
903                  * have to propagate forward the RelOptInfos that were already built
904                  * in previous children.
905                  */
906                 Assert(subroot.simple_rel_array_size >= save_rel_array_size);
907                 for (rti = 1; rti < save_rel_array_size; rti++)
908                 {
909                         RelOptInfo *brel = save_rel_array[rti];
910
911                         if (brel)
912                                 subroot.simple_rel_array[rti] = brel;
913                 }
914                 save_rel_array_size = subroot.simple_rel_array_size;
915                 save_rel_array = subroot.simple_rel_array;
916
917                 /* Make sure any initplans from this rel get into the outer list */
918                 root->init_plans = subroot.init_plans;
919
920                 /* Build list of target-relation RT indexes */
921                 resultRelations = lappend_int(resultRelations, appinfo->child_relid);
922
923                 /* Build list of per-relation RETURNING targetlists */
924                 if (parse->returningList)
925                         returningLists = lappend(returningLists,
926                                                                          subroot.parse->returningList);
927         }
928
929         /* Mark result as unordered (probably unnecessary) */
930         root->query_pathkeys = NIL;
931
932         /*
933          * If we managed to exclude every child rel, return a dummy plan; it
934          * doesn't even need a ModifyTable node.
935          */
936         if (subplans == NIL)
937         {
938                 /* although dummy, it must have a valid tlist for executor */
939                 List       *tlist;
940
941                 tlist = preprocess_targetlist(root, parse->targetList);
942                 return (Plan *) make_result(root,
943                                                                         tlist,
944                                                                         (Node *) list_make1(makeBoolConst(false,
945                                                                                                                                           false)),
946                                                                         NULL);
947         }
948
949         /*
950          * Put back the final adjusted rtable into the master copy of the Query.
951          */
952         parse->rtable = final_rtable;
953         root->simple_rel_array_size = save_rel_array_size;
954         root->simple_rel_array = save_rel_array;
955
956         /*
957          * If there was a FOR UPDATE/SHARE clause, the LockRows node will have
958          * dealt with fetching non-locked marked rows, else we need to have
959          * ModifyTable do that.
960          */
961         if (parse->rowMarks)
962                 rowMarks = NIL;
963         else
964                 rowMarks = root->rowMarks;
965
966         /* And last, tack on a ModifyTable node to do the UPDATE/DELETE work */
967         return (Plan *) make_modifytable(parse->commandType,
968                                                                          parse->canSetTag,
969                                                                          resultRelations,
970                                                                          subplans,
971                                                                          returningLists,
972                                                                          rowMarks,
973                                                                          SS_assign_special_param(root));
974 }
975
976 /*--------------------
977  * grouping_planner
978  *        Perform planning steps related to grouping, aggregation, etc.
979  *        This primarily means adding top-level processing to the basic
980  *        query plan produced by query_planner.
981  *
982  * tuple_fraction is the fraction of tuples we expect will be retrieved
983  *
984  * tuple_fraction is interpreted as follows:
985  *        0: expect all tuples to be retrieved (normal case)
986  *        0 < tuple_fraction < 1: expect the given fraction of tuples available
987  *              from the plan to be retrieved
988  *        tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
989  *              expected to be retrieved (ie, a LIMIT specification)
990  *
991  * Returns a query plan.  Also, root->query_pathkeys is returned as the
992  * actual output ordering of the plan (in pathkey format).
993  *--------------------
994  */
995 static Plan *
996 grouping_planner(PlannerInfo *root, double tuple_fraction)
997 {
998         Query      *parse = root->parse;
999         List       *tlist = parse->targetList;
1000         int64           offset_est = 0;
1001         int64           count_est = 0;
1002         double          limit_tuples = -1.0;
1003         Plan       *result_plan;
1004         List       *current_pathkeys;
1005         double          dNumGroups = 0;
1006         bool            use_hashed_distinct = false;
1007         bool            tested_hashed_distinct = false;
1008
1009         /* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
1010         if (parse->limitCount || parse->limitOffset)
1011         {
1012                 tuple_fraction = preprocess_limit(root, tuple_fraction,
1013                                                                                   &offset_est, &count_est);
1014
1015                 /*
1016                  * If we have a known LIMIT, and don't have an unknown OFFSET, we can
1017                  * estimate the effects of using a bounded sort.
1018                  */
1019                 if (count_est > 0 && offset_est >= 0)
1020                         limit_tuples = (double) count_est + (double) offset_est;
1021         }
1022
1023         if (parse->setOperations)
1024         {
1025                 List       *set_sortclauses;
1026
1027                 /*
1028                  * If there's a top-level ORDER BY, assume we have to fetch all the
1029                  * tuples.      This might be too simplistic given all the hackery below
1030                  * to possibly avoid the sort; but the odds of accurate estimates here
1031                  * are pretty low anyway.
1032                  */
1033                 if (parse->sortClause)
1034                         tuple_fraction = 0.0;
1035
1036                 /*
1037                  * Construct the plan for set operations.  The result will not need
1038                  * any work except perhaps a top-level sort and/or LIMIT.  Note that
1039                  * any special work for recursive unions is the responsibility of
1040                  * plan_set_operations.
1041                  */
1042                 result_plan = plan_set_operations(root, tuple_fraction,
1043                                                                                   &set_sortclauses);
1044
1045                 /*
1046                  * Calculate pathkeys representing the sort order (if any) of the set
1047                  * operation's result.  We have to do this before overwriting the sort
1048                  * key information...
1049                  */
1050                 current_pathkeys = make_pathkeys_for_sortclauses(root,
1051                                                                                                                  set_sortclauses,
1052                                                                                                          result_plan->targetlist,
1053                                                                                                                  true);
1054
1055                 /*
1056                  * We should not need to call preprocess_targetlist, since we must be
1057                  * in a SELECT query node.      Instead, use the targetlist returned by
1058                  * plan_set_operations (since this tells whether it returned any
1059                  * resjunk columns!), and transfer any sort key information from the
1060                  * original tlist.
1061                  */
1062                 Assert(parse->commandType == CMD_SELECT);
1063
1064                 tlist = postprocess_setop_tlist(copyObject(result_plan->targetlist),
1065                                                                                 tlist);
1066
1067                 /*
1068                  * Can't handle FOR UPDATE/SHARE here (parser should have checked
1069                  * already, but let's make sure).
1070                  */
1071                 if (parse->rowMarks)
1072                         ereport(ERROR,
1073                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1074                                          errmsg("SELECT FOR UPDATE/SHARE is not allowed with UNION/INTERSECT/EXCEPT")));
1075
1076                 /*
1077                  * Calculate pathkeys that represent result ordering requirements
1078                  */
1079                 Assert(parse->distinctClause == NIL);
1080                 root->sort_pathkeys = make_pathkeys_for_sortclauses(root,
1081                                                                                                                         parse->sortClause,
1082                                                                                                                         tlist,
1083                                                                                                                         true);
1084         }
1085         else
1086         {
1087                 /* No set operations, do regular planning */
1088                 List       *sub_tlist;
1089                 double          sub_limit_tuples;
1090                 AttrNumber *groupColIdx = NULL;
1091                 bool            need_tlist_eval = true;
1092                 Path       *cheapest_path;
1093                 Path       *sorted_path;
1094                 Path       *best_path;
1095                 long            numGroups = 0;
1096                 AggClauseCosts agg_costs;
1097                 int                     numGroupCols;
1098                 double          path_rows;
1099                 int                     path_width;
1100                 bool            use_hashed_grouping = false;
1101                 WindowFuncLists *wflists = NULL;
1102                 List       *activeWindows = NIL;
1103
1104                 MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
1105
1106                 /* A recursive query should always have setOperations */
1107                 Assert(!root->hasRecursion);
1108
1109                 /* Preprocess GROUP BY clause, if any */
1110                 if (parse->groupClause)
1111                         preprocess_groupclause(root);
1112                 numGroupCols = list_length(parse->groupClause);
1113
1114                 /* Preprocess targetlist */
1115                 tlist = preprocess_targetlist(root, tlist);
1116
1117                 /*
1118                  * Locate any window functions in the tlist.  (We don't need to look
1119                  * anywhere else, since expressions used in ORDER BY will be in there
1120                  * too.)  Note that they could all have been eliminated by constant
1121                  * folding, in which case we don't need to do any more work.
1122                  */
1123                 if (parse->hasWindowFuncs)
1124                 {
1125                         wflists = find_window_functions((Node *) tlist,
1126                                                                                         list_length(parse->windowClause));
1127                         if (wflists->numWindowFuncs > 0)
1128                                 activeWindows = select_active_windows(root, wflists);
1129                         else
1130                                 parse->hasWindowFuncs = false;
1131                 }
1132
1133                 /*
1134                  * Generate appropriate target list for subplan; may be different from
1135                  * tlist if grouping or aggregation is needed.
1136                  */
1137                 sub_tlist = make_subplanTargetList(root, tlist,
1138                                                                                    &groupColIdx, &need_tlist_eval);
1139
1140                 /*
1141                  * Do aggregate preprocessing, if the query has any aggs.
1142                  *
1143                  * Note: think not that we can turn off hasAggs if we find no aggs. It
1144                  * is possible for constant-expression simplification to remove all
1145                  * explicit references to aggs, but we still have to follow the
1146                  * aggregate semantics (eg, producing only one output row).
1147                  */
1148                 if (parse->hasAggs)
1149                 {
1150                         /*
1151                          * Collect statistics about aggregates for estimating costs. Note:
1152                          * we do not attempt to detect duplicate aggregates here; a
1153                          * somewhat-overestimated cost is okay for our present purposes.
1154                          */
1155                         count_agg_clauses(root, (Node *) tlist, &agg_costs);
1156                         count_agg_clauses(root, parse->havingQual, &agg_costs);
1157
1158                         /*
1159                          * Preprocess MIN/MAX aggregates, if any.  Note: be careful about
1160                          * adding logic between here and the optimize_minmax_aggregates
1161                          * call.  Anything that is needed in MIN/MAX-optimizable cases
1162                          * will have to be duplicated in planagg.c.
1163                          */
1164                         preprocess_minmax_aggregates(root, tlist);
1165                 }
1166
1167                 /*
1168                  * Calculate pathkeys that represent grouping/ordering requirements.
1169                  * Stash them in PlannerInfo so that query_planner can canonicalize
1170                  * them after EquivalenceClasses have been formed.      The sortClause is
1171                  * certainly sort-able, but GROUP BY and DISTINCT might not be, in
1172                  * which case we just leave their pathkeys empty.
1173                  */
1174                 if (parse->groupClause &&
1175                         grouping_is_sortable(parse->groupClause))
1176                         root->group_pathkeys =
1177                                 make_pathkeys_for_sortclauses(root,
1178                                                                                           parse->groupClause,
1179                                                                                           tlist,
1180                                                                                           false);
1181                 else
1182                         root->group_pathkeys = NIL;
1183
1184                 /* We consider only the first (bottom) window in pathkeys logic */
1185                 if (activeWindows != NIL)
1186                 {
1187                         WindowClause *wc = (WindowClause *) linitial(activeWindows);
1188
1189                         root->window_pathkeys = make_pathkeys_for_window(root,
1190                                                                                                                          wc,
1191                                                                                                                          tlist,
1192                                                                                                                          false);
1193                 }
1194                 else
1195                         root->window_pathkeys = NIL;
1196
1197                 if (parse->distinctClause &&
1198                         grouping_is_sortable(parse->distinctClause))
1199                         root->distinct_pathkeys =
1200                                 make_pathkeys_for_sortclauses(root,
1201                                                                                           parse->distinctClause,
1202                                                                                           tlist,
1203                                                                                           false);
1204                 else
1205                         root->distinct_pathkeys = NIL;
1206
1207                 root->sort_pathkeys =
1208                         make_pathkeys_for_sortclauses(root,
1209                                                                                   parse->sortClause,
1210                                                                                   tlist,
1211                                                                                   false);
1212
1213                 /*
1214                  * Figure out whether we want a sorted result from query_planner.
1215                  *
1216                  * If we have a sortable GROUP BY clause, then we want a result sorted
1217                  * properly for grouping.  Otherwise, if we have window functions to
1218                  * evaluate, we try to sort for the first window.  Otherwise, if
1219                  * there's a sortable DISTINCT clause that's more rigorous than the
1220                  * ORDER BY clause, we try to produce output that's sufficiently well
1221                  * sorted for the DISTINCT.  Otherwise, if there is an ORDER BY
1222                  * clause, we want to sort by the ORDER BY clause.
1223                  *
1224                  * Note: if we have both ORDER BY and GROUP BY, and ORDER BY is a
1225                  * superset of GROUP BY, it would be tempting to request sort by ORDER
1226                  * BY --- but that might just leave us failing to exploit an available
1227                  * sort order at all.  Needs more thought.      The choice for DISTINCT
1228                  * versus ORDER BY is much easier, since we know that the parser
1229                  * ensured that one is a superset of the other.
1230                  */
1231                 if (root->group_pathkeys)
1232                         root->query_pathkeys = root->group_pathkeys;
1233                 else if (root->window_pathkeys)
1234                         root->query_pathkeys = root->window_pathkeys;
1235                 else if (list_length(root->distinct_pathkeys) >
1236                                  list_length(root->sort_pathkeys))
1237                         root->query_pathkeys = root->distinct_pathkeys;
1238                 else if (root->sort_pathkeys)
1239                         root->query_pathkeys = root->sort_pathkeys;
1240                 else
1241                         root->query_pathkeys = NIL;
1242
1243                 /*
1244                  * Figure out whether there's a hard limit on the number of rows that
1245                  * query_planner's result subplan needs to return.  Even if we know a
1246                  * hard limit overall, it doesn't apply if the query has any
1247                  * grouping/aggregation operations.
1248                  */
1249                 if (parse->groupClause ||
1250                         parse->distinctClause ||
1251                         parse->hasAggs ||
1252                         parse->hasWindowFuncs ||
1253                         root->hasHavingQual)
1254                         sub_limit_tuples = -1.0;
1255                 else
1256                         sub_limit_tuples = limit_tuples;
1257
1258                 /*
1259                  * Generate the best unsorted and presorted paths for this Query (but
1260                  * note there may not be any presorted path).  query_planner will also
1261                  * estimate the number of groups in the query, and canonicalize all
1262                  * the pathkeys.
1263                  */
1264                 query_planner(root, sub_tlist, tuple_fraction, sub_limit_tuples,
1265                                           &cheapest_path, &sorted_path, &dNumGroups);
1266
1267                 /*
1268                  * Extract rowcount and width estimates for possible use in grouping
1269                  * decisions.  Beware here of the possibility that
1270                  * cheapest_path->parent is NULL (ie, there is no FROM clause).
1271                  */
1272                 if (cheapest_path->parent)
1273                 {
1274                         path_rows = cheapest_path->parent->rows;
1275                         path_width = cheapest_path->parent->width;
1276                 }
1277                 else
1278                 {
1279                         path_rows = 1;          /* assume non-set result */
1280                         path_width = 100;       /* arbitrary */
1281                 }
1282
1283                 if (parse->groupClause)
1284                 {
1285                         /*
1286                          * If grouping, decide whether to use sorted or hashed grouping.
1287                          */
1288                         use_hashed_grouping =
1289                                 choose_hashed_grouping(root,
1290                                                                            tuple_fraction, limit_tuples,
1291                                                                            path_rows, path_width,
1292                                                                            cheapest_path, sorted_path,
1293                                                                            dNumGroups, &agg_costs);
1294                         /* Also convert # groups to long int --- but 'ware overflow! */
1295                         numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
1296                 }
1297                 else if (parse->distinctClause && sorted_path &&
1298                                  !root->hasHavingQual && !parse->hasAggs && !activeWindows)
1299                 {
1300                         /*
1301                          * We'll reach the DISTINCT stage without any intermediate
1302                          * processing, so figure out whether we will want to hash or not
1303                          * so we can choose whether to use cheapest or sorted path.
1304                          */
1305                         use_hashed_distinct =
1306                                 choose_hashed_distinct(root,
1307                                                                            tuple_fraction, limit_tuples,
1308                                                                            path_rows, path_width,
1309                                                                            cheapest_path->startup_cost,
1310                                                                            cheapest_path->total_cost,
1311                                                                            sorted_path->startup_cost,
1312                                                                            sorted_path->total_cost,
1313                                                                            sorted_path->pathkeys,
1314                                                                            dNumGroups);
1315                         tested_hashed_distinct = true;
1316                 }
1317
1318                 /*
1319                  * Select the best path.  If we are doing hashed grouping, we will
1320                  * always read all the input tuples, so use the cheapest-total path.
1321                  * Otherwise, trust query_planner's decision about which to use.
1322                  */
1323                 if (use_hashed_grouping || use_hashed_distinct || !sorted_path)
1324                         best_path = cheapest_path;
1325                 else
1326                         best_path = sorted_path;
1327
1328                 /*
1329                  * Check to see if it's possible to optimize MIN/MAX aggregates. If
1330                  * so, we will forget all the work we did so far to choose a "regular"
1331                  * path ... but we had to do it anyway to be able to tell which way is
1332                  * cheaper.
1333                  */
1334                 result_plan = optimize_minmax_aggregates(root,
1335                                                                                                  tlist,
1336                                                                                                  &agg_costs,
1337                                                                                                  best_path);
1338                 if (result_plan != NULL)
1339                 {
1340                         /*
1341                          * optimize_minmax_aggregates generated the full plan, with the
1342                          * right tlist, and it has no sort order.
1343                          */
1344                         current_pathkeys = NIL;
1345                 }
1346                 else
1347                 {
1348                         /*
1349                          * Normal case --- create a plan according to query_planner's
1350                          * results.
1351                          */
1352                         bool            need_sort_for_grouping = false;
1353
1354                         result_plan = create_plan(root, best_path);
1355                         current_pathkeys = best_path->pathkeys;
1356
1357                         /* Detect if we'll need an explicit sort for grouping */
1358                         if (parse->groupClause && !use_hashed_grouping &&
1359                           !pathkeys_contained_in(root->group_pathkeys, current_pathkeys))
1360                         {
1361                                 need_sort_for_grouping = true;
1362
1363                                 /*
1364                                  * Always override create_plan's tlist, so that we don't sort
1365                                  * useless data from a "physical" tlist.
1366                                  */
1367                                 need_tlist_eval = true;
1368                         }
1369
1370                         /*
1371                          * create_plan returns a plan with just a "flat" tlist of required
1372                          * Vars.  Usually we need to insert the sub_tlist as the tlist of
1373                          * the top plan node.  However, we can skip that if we determined
1374                          * that whatever create_plan chose to return will be good enough.
1375                          */
1376                         if (need_tlist_eval)
1377                         {
1378                                 /*
1379                                  * If the top-level plan node is one that cannot do expression
1380                                  * evaluation, we must insert a Result node to project the
1381                                  * desired tlist.
1382                                  */
1383                                 if (!is_projection_capable_plan(result_plan))
1384                                 {
1385                                         result_plan = (Plan *) make_result(root,
1386                                                                                                            sub_tlist,
1387                                                                                                            NULL,
1388                                                                                                            result_plan);
1389                                 }
1390                                 else
1391                                 {
1392                                         /*
1393                                          * Otherwise, just replace the subplan's flat tlist with
1394                                          * the desired tlist.
1395                                          */
1396                                         result_plan->targetlist = sub_tlist;
1397                                 }
1398
1399                                 /*
1400                                  * Also, account for the cost of evaluation of the sub_tlist.
1401                                  * See comments for add_tlist_costs_to_plan() for more info.
1402                                  */
1403                                 add_tlist_costs_to_plan(root, result_plan, sub_tlist);
1404                         }
1405                         else
1406                         {
1407                                 /*
1408                                  * Since we're using create_plan's tlist and not the one
1409                                  * make_subplanTargetList calculated, we have to refigure any
1410                                  * grouping-column indexes make_subplanTargetList computed.
1411                                  */
1412                                 locate_grouping_columns(root, tlist, result_plan->targetlist,
1413                                                                                 groupColIdx);
1414                         }
1415
1416                         /*
1417                          * Insert AGG or GROUP node if needed, plus an explicit sort step
1418                          * if necessary.
1419                          *
1420                          * HAVING clause, if any, becomes qual of the Agg or Group node.
1421                          */
1422                         if (use_hashed_grouping)
1423                         {
1424                                 /* Hashed aggregate plan --- no sort needed */
1425                                 result_plan = (Plan *) make_agg(root,
1426                                                                                                 tlist,
1427                                                                                                 (List *) parse->havingQual,
1428                                                                                                 AGG_HASHED,
1429                                                                                                 &agg_costs,
1430                                                                                                 numGroupCols,
1431                                                                                                 groupColIdx,
1432                                                                         extract_grouping_ops(parse->groupClause),
1433                                                                                                 numGroups,
1434                                                                                                 result_plan);
1435                                 /* Hashed aggregation produces randomly-ordered results */
1436                                 current_pathkeys = NIL;
1437                         }
1438                         else if (parse->hasAggs)
1439                         {
1440                                 /* Plain aggregate plan --- sort if needed */
1441                                 AggStrategy aggstrategy;
1442
1443                                 if (parse->groupClause)
1444                                 {
1445                                         if (need_sort_for_grouping)
1446                                         {
1447                                                 result_plan = (Plan *)
1448                                                         make_sort_from_groupcols(root,
1449                                                                                                          parse->groupClause,
1450                                                                                                          groupColIdx,
1451                                                                                                          result_plan);
1452                                                 current_pathkeys = root->group_pathkeys;
1453                                         }
1454                                         aggstrategy = AGG_SORTED;
1455
1456                                         /*
1457                                          * The AGG node will not change the sort ordering of its
1458                                          * groups, so current_pathkeys describes the result too.
1459                                          */
1460                                 }
1461                                 else
1462                                 {
1463                                         aggstrategy = AGG_PLAIN;
1464                                         /* Result will be only one row anyway; no sort order */
1465                                         current_pathkeys = NIL;
1466                                 }
1467
1468                                 result_plan = (Plan *) make_agg(root,
1469                                                                                                 tlist,
1470                                                                                                 (List *) parse->havingQual,
1471                                                                                                 aggstrategy,
1472                                                                                                 &agg_costs,
1473                                                                                                 numGroupCols,
1474                                                                                                 groupColIdx,
1475                                                                         extract_grouping_ops(parse->groupClause),
1476                                                                                                 numGroups,
1477                                                                                                 result_plan);
1478                         }
1479                         else if (parse->groupClause)
1480                         {
1481                                 /*
1482                                  * GROUP BY without aggregation, so insert a group node (plus
1483                                  * the appropriate sort node, if necessary).
1484                                  *
1485                                  * Add an explicit sort if we couldn't make the path come out
1486                                  * the way the GROUP node needs it.
1487                                  */
1488                                 if (need_sort_for_grouping)
1489                                 {
1490                                         result_plan = (Plan *)
1491                                                 make_sort_from_groupcols(root,
1492                                                                                                  parse->groupClause,
1493                                                                                                  groupColIdx,
1494                                                                                                  result_plan);
1495                                         current_pathkeys = root->group_pathkeys;
1496                                 }
1497
1498                                 result_plan = (Plan *) make_group(root,
1499                                                                                                   tlist,
1500                                                                                                   (List *) parse->havingQual,
1501                                                                                                   numGroupCols,
1502                                                                                                   groupColIdx,
1503                                                                         extract_grouping_ops(parse->groupClause),
1504                                                                                                   dNumGroups,
1505                                                                                                   result_plan);
1506                                 /* The Group node won't change sort ordering */
1507                         }
1508                         else if (root->hasHavingQual)
1509                         {
1510                                 /*
1511                                  * No aggregates, and no GROUP BY, but we have a HAVING qual.
1512                                  * This is a degenerate case in which we are supposed to emit
1513                                  * either 0 or 1 row depending on whether HAVING succeeds.
1514                                  * Furthermore, there cannot be any variables in either HAVING
1515                                  * or the targetlist, so we actually do not need the FROM
1516                                  * table at all!  We can just throw away the plan-so-far and
1517                                  * generate a Result node.      This is a sufficiently unusual
1518                                  * corner case that it's not worth contorting the structure of
1519                                  * this routine to avoid having to generate the plan in the
1520                                  * first place.
1521                                  */
1522                                 result_plan = (Plan *) make_result(root,
1523                                                                                                    tlist,
1524                                                                                                    parse->havingQual,
1525                                                                                                    NULL);
1526                         }
1527                 }                                               /* end of non-minmax-aggregate case */
1528
1529                 /*
1530                  * Since each window function could require a different sort order, we
1531                  * stack up a WindowAgg node for each window, with sort steps between
1532                  * them as needed.
1533                  */
1534                 if (activeWindows)
1535                 {
1536                         List       *window_tlist;
1537                         ListCell   *l;
1538
1539                         /*
1540                          * If the top-level plan node is one that cannot do expression
1541                          * evaluation, we must insert a Result node to project the desired
1542                          * tlist.  (In some cases this might not really be required, but
1543                          * it's not worth trying to avoid it.)  Note that on second and
1544                          * subsequent passes through the following loop, the top-level
1545                          * node will be a WindowAgg which we know can project; so we only
1546                          * need to check once.
1547                          */
1548                         if (!is_projection_capable_plan(result_plan))
1549                         {
1550                                 result_plan = (Plan *) make_result(root,
1551                                                                                                    NIL,
1552                                                                                                    NULL,
1553                                                                                                    result_plan);
1554                         }
1555
1556                         /*
1557                          * The "base" targetlist for all steps of the windowing process is
1558                          * a flat tlist of all Vars and Aggs needed in the result. (In
1559                          * some cases we wouldn't need to propagate all of these all the
1560                          * way to the top, since they might only be needed as inputs to
1561                          * WindowFuncs.  It's probably not worth trying to optimize that
1562                          * though.)  We also need any volatile sort expressions, because
1563                          * make_sort_from_pathkeys won't add those on its own, and anyway
1564                          * we want them evaluated only once at the bottom of the stack. As
1565                          * we climb up the stack, we add outputs for the WindowFuncs
1566                          * computed at each level.      Also, each input tlist has to present
1567                          * all the columns needed to sort the data for the next WindowAgg
1568                          * step.  That's handled internally by make_sort_from_pathkeys,
1569                          * but we need the copyObject steps here to ensure that each plan
1570                          * node has a separately modifiable tlist.
1571                          *
1572                          * Note: it's essential here to use PVC_INCLUDE_AGGREGATES so that
1573                          * Vars mentioned only in aggregate expressions aren't pulled out
1574                          * as separate targetlist entries.      Otherwise we could be putting
1575                          * ungrouped Vars directly into an Agg node's tlist, resulting in
1576                          * undefined behavior.
1577                          */
1578                         window_tlist = flatten_tlist(tlist,
1579                                                                                  PVC_INCLUDE_AGGREGATES,
1580                                                                                  PVC_INCLUDE_PLACEHOLDERS);
1581                         window_tlist = add_volatile_sort_exprs(window_tlist, tlist,
1582                                                                                                    activeWindows);
1583                         result_plan->targetlist = (List *) copyObject(window_tlist);
1584
1585                         foreach(l, activeWindows)
1586                         {
1587                                 WindowClause *wc = (WindowClause *) lfirst(l);
1588                                 List       *window_pathkeys;
1589                                 int                     partNumCols;
1590                                 AttrNumber *partColIdx;
1591                                 Oid                *partOperators;
1592                                 int                     ordNumCols;
1593                                 AttrNumber *ordColIdx;
1594                                 Oid                *ordOperators;
1595
1596                                 window_pathkeys = make_pathkeys_for_window(root,
1597                                                                                                                    wc,
1598                                                                                                                    tlist,
1599                                                                                                                    true);
1600
1601                                 /*
1602                                  * This is a bit tricky: we build a sort node even if we don't
1603                                  * really have to sort.  Even when no explicit sort is needed,
1604                                  * we need to have suitable resjunk items added to the input
1605                                  * plan's tlist for any partitioning or ordering columns that
1606                                  * aren't plain Vars.  Furthermore, this way we can use
1607                                  * existing infrastructure to identify which input columns are
1608                                  * the interesting ones.
1609                                  */
1610                                 if (window_pathkeys)
1611                                 {
1612                                         Sort       *sort_plan;
1613
1614                                         sort_plan = make_sort_from_pathkeys(root,
1615                                                                                                                 result_plan,
1616                                                                                                                 window_pathkeys,
1617                                                                                                                 -1.0);
1618                                         if (!pathkeys_contained_in(window_pathkeys,
1619                                                                                            current_pathkeys))
1620                                         {
1621                                                 /* we do indeed need to sort */
1622                                                 result_plan = (Plan *) sort_plan;
1623                                                 current_pathkeys = window_pathkeys;
1624                                         }
1625                                         /* In either case, extract the per-column information */
1626                                         get_column_info_for_window(root, wc, tlist,
1627                                                                                            sort_plan->numCols,
1628                                                                                            sort_plan->sortColIdx,
1629                                                                                            &partNumCols,
1630                                                                                            &partColIdx,
1631                                                                                            &partOperators,
1632                                                                                            &ordNumCols,
1633                                                                                            &ordColIdx,
1634                                                                                            &ordOperators);
1635                                 }
1636                                 else
1637                                 {
1638                                         /* empty window specification, nothing to sort */
1639                                         partNumCols = 0;
1640                                         partColIdx = NULL;
1641                                         partOperators = NULL;
1642                                         ordNumCols = 0;
1643                                         ordColIdx = NULL;
1644                                         ordOperators = NULL;
1645                                 }
1646
1647                                 if (lnext(l))
1648                                 {
1649                                         /* Add the current WindowFuncs to the running tlist */
1650                                         window_tlist = add_to_flat_tlist(window_tlist,
1651                                                                                    wflists->windowFuncs[wc->winref]);
1652                                 }
1653                                 else
1654                                 {
1655                                         /* Install the original tlist in the topmost WindowAgg */
1656                                         window_tlist = tlist;
1657                                 }
1658
1659                                 /* ... and make the WindowAgg plan node */
1660                                 result_plan = (Plan *)
1661                                         make_windowagg(root,
1662                                                                    (List *) copyObject(window_tlist),
1663                                                                    wflists->windowFuncs[wc->winref],
1664                                                                    wc->winref,
1665                                                                    partNumCols,
1666                                                                    partColIdx,
1667                                                                    partOperators,
1668                                                                    ordNumCols,
1669                                                                    ordColIdx,
1670                                                                    ordOperators,
1671                                                                    wc->frameOptions,
1672                                                                    wc->startOffset,
1673                                                                    wc->endOffset,
1674                                                                    result_plan);
1675                         }
1676                 }
1677         }                                                       /* end of if (setOperations) */
1678
1679         /*
1680          * If there is a DISTINCT clause, add the necessary node(s).
1681          */
1682         if (parse->distinctClause)
1683         {
1684                 double          dNumDistinctRows;
1685                 long            numDistinctRows;
1686
1687                 /*
1688                  * If there was grouping or aggregation, use the current number of
1689                  * rows as the estimated number of DISTINCT rows (ie, assume the
1690                  * result was already mostly unique).  If not, use the number of
1691                  * distinct-groups calculated by query_planner.
1692                  */
1693                 if (parse->groupClause || root->hasHavingQual || parse->hasAggs)
1694                         dNumDistinctRows = result_plan->plan_rows;
1695                 else
1696                         dNumDistinctRows = dNumGroups;
1697
1698                 /* Also convert to long int --- but 'ware overflow! */
1699                 numDistinctRows = (long) Min(dNumDistinctRows, (double) LONG_MAX);
1700
1701                 /* Choose implementation method if we didn't already */
1702                 if (!tested_hashed_distinct)
1703                 {
1704                         /*
1705                          * At this point, either hashed or sorted grouping will have to
1706                          * work from result_plan, so we pass that as both "cheapest" and
1707                          * "sorted".
1708                          */
1709                         use_hashed_distinct =
1710                                 choose_hashed_distinct(root,
1711                                                                            tuple_fraction, limit_tuples,
1712                                                                            result_plan->plan_rows,
1713                                                                            result_plan->plan_width,
1714                                                                            result_plan->startup_cost,
1715                                                                            result_plan->total_cost,
1716                                                                            result_plan->startup_cost,
1717                                                                            result_plan->total_cost,
1718                                                                            current_pathkeys,
1719                                                                            dNumDistinctRows);
1720                 }
1721
1722                 if (use_hashed_distinct)
1723                 {
1724                         /* Hashed aggregate plan --- no sort needed */
1725                         result_plan = (Plan *) make_agg(root,
1726                                                                                         result_plan->targetlist,
1727                                                                                         NIL,
1728                                                                                         AGG_HASHED,
1729                                                                                         NULL,
1730                                                                                   list_length(parse->distinctClause),
1731                                                                  extract_grouping_cols(parse->distinctClause,
1732                                                                                                         result_plan->targetlist),
1733                                                                  extract_grouping_ops(parse->distinctClause),
1734                                                                                         numDistinctRows,
1735                                                                                         result_plan);
1736                         /* Hashed aggregation produces randomly-ordered results */
1737                         current_pathkeys = NIL;
1738                 }
1739                 else
1740                 {
1741                         /*
1742                          * Use a Unique node to implement DISTINCT.  Add an explicit sort
1743                          * if we couldn't make the path come out the way the Unique node
1744                          * needs it.  If we do have to sort, always sort by the more
1745                          * rigorous of DISTINCT and ORDER BY, to avoid a second sort
1746                          * below.  However, for regular DISTINCT, don't sort now if we
1747                          * don't have to --- sorting afterwards will likely be cheaper,
1748                          * and also has the possibility of optimizing via LIMIT.  But for
1749                          * DISTINCT ON, we *must* force the final sort now, else it won't
1750                          * have the desired behavior.
1751                          */
1752                         List       *needed_pathkeys;
1753
1754                         if (parse->hasDistinctOn &&
1755                                 list_length(root->distinct_pathkeys) <
1756                                 list_length(root->sort_pathkeys))
1757                                 needed_pathkeys = root->sort_pathkeys;
1758                         else
1759                                 needed_pathkeys = root->distinct_pathkeys;
1760
1761                         if (!pathkeys_contained_in(needed_pathkeys, current_pathkeys))
1762                         {
1763                                 if (list_length(root->distinct_pathkeys) >=
1764                                         list_length(root->sort_pathkeys))
1765                                         current_pathkeys = root->distinct_pathkeys;
1766                                 else
1767                                 {
1768                                         current_pathkeys = root->sort_pathkeys;
1769                                         /* Assert checks that parser didn't mess up... */
1770                                         Assert(pathkeys_contained_in(root->distinct_pathkeys,
1771                                                                                                  current_pathkeys));
1772                                 }
1773
1774                                 result_plan = (Plan *) make_sort_from_pathkeys(root,
1775                                                                                                                            result_plan,
1776                                                                                                                         current_pathkeys,
1777                                                                                                                            -1.0);
1778                         }
1779
1780                         result_plan = (Plan *) make_unique(result_plan,
1781                                                                                            parse->distinctClause);
1782                         result_plan->plan_rows = dNumDistinctRows;
1783                         /* The Unique node won't change sort ordering */
1784                 }
1785         }
1786
1787         /*
1788          * If ORDER BY was given and we were not able to make the plan come out in
1789          * the right order, add an explicit sort step.
1790          */
1791         if (parse->sortClause)
1792         {
1793                 if (!pathkeys_contained_in(root->sort_pathkeys, current_pathkeys))
1794                 {
1795                         result_plan = (Plan *) make_sort_from_pathkeys(root,
1796                                                                                                                    result_plan,
1797                                                                                                                  root->sort_pathkeys,
1798                                                                                                                    limit_tuples);
1799                         current_pathkeys = root->sort_pathkeys;
1800                 }
1801         }
1802
1803         /*
1804          * If there is a FOR UPDATE/SHARE clause, add the LockRows node. (Note: we
1805          * intentionally test parse->rowMarks not root->rowMarks here. If there
1806          * are only non-locking rowmarks, they should be handled by the
1807          * ModifyTable node instead.)
1808          */
1809         if (parse->rowMarks)
1810         {
1811                 result_plan = (Plan *) make_lockrows(result_plan,
1812                                                                                          root->rowMarks,
1813                                                                                          SS_assign_special_param(root));
1814
1815                 /*
1816                  * The result can no longer be assumed sorted, since locking might
1817                  * cause the sort key columns to be replaced with new values.
1818                  */
1819                 current_pathkeys = NIL;
1820         }
1821
1822         /*
1823          * Finally, if there is a LIMIT/OFFSET clause, add the LIMIT node.
1824          */
1825         if (parse->limitCount || parse->limitOffset)
1826         {
1827                 result_plan = (Plan *) make_limit(result_plan,
1828                                                                                   parse->limitOffset,
1829                                                                                   parse->limitCount,
1830                                                                                   offset_est,
1831                                                                                   count_est);
1832         }
1833
1834         /*
1835          * Return the actual output ordering in query_pathkeys for possible use by
1836          * an outer query level.
1837          */
1838         root->query_pathkeys = current_pathkeys;
1839
1840         return result_plan;
1841 }
1842
1843 /*
1844  * add_tlist_costs_to_plan
1845  *
1846  * Estimate the execution costs associated with evaluating the targetlist
1847  * expressions, and add them to the cost estimates for the Plan node.
1848  *
1849  * If the tlist contains set-returning functions, also inflate the Plan's cost
1850  * and plan_rows estimates accordingly.  (Hence, this must be called *after*
1851  * any logic that uses plan_rows to, eg, estimate qual evaluation costs.)
1852  *
1853  * Note: during initial stages of planning, we mostly consider plan nodes with
1854  * "flat" tlists, containing just Vars.  So their evaluation cost is zero
1855  * according to the model used by cost_qual_eval() (or if you prefer, the cost
1856  * is factored into cpu_tuple_cost).  Thus we can avoid accounting for tlist
1857  * cost throughout query_planner() and subroutines.  But once we apply a
1858  * tlist that might contain actual operators, sub-selects, etc, we'd better
1859  * account for its cost.  Any set-returning functions in the tlist must also
1860  * affect the estimated rowcount.
1861  *
1862  * Once grouping_planner() has applied a general tlist to the topmost
1863  * scan/join plan node, any tlist eval cost for added-on nodes should be
1864  * accounted for as we create those nodes.      Presently, of the node types we
1865  * can add on later, only Agg, WindowAgg, and Group project new tlists (the
1866  * rest just copy their input tuples) --- so make_agg(), make_windowagg() and
1867  * make_group() are responsible for calling this function to account for their
1868  * tlist costs.
1869  */
1870 void
1871 add_tlist_costs_to_plan(PlannerInfo *root, Plan *plan, List *tlist)
1872 {
1873         QualCost        tlist_cost;
1874         double          tlist_rows;
1875
1876         cost_qual_eval(&tlist_cost, tlist, root);
1877         plan->startup_cost += tlist_cost.startup;
1878         plan->total_cost += tlist_cost.startup +
1879                 tlist_cost.per_tuple * plan->plan_rows;
1880
1881         tlist_rows = tlist_returns_set_rows(tlist);
1882         if (tlist_rows > 1)
1883         {
1884                 /*
1885                  * We assume that execution costs of the tlist proper were all
1886                  * accounted for by cost_qual_eval.  However, it still seems
1887                  * appropriate to charge something more for the executor's general
1888                  * costs of processing the added tuples.  The cost is probably less
1889                  * than cpu_tuple_cost, though, so we arbitrarily use half of that.
1890                  */
1891                 plan->total_cost += plan->plan_rows * (tlist_rows - 1) *
1892                         cpu_tuple_cost / 2;
1893
1894                 plan->plan_rows *= tlist_rows;
1895         }
1896 }
1897
1898 /*
1899  * Detect whether a plan node is a "dummy" plan created when a relation
1900  * is deemed not to need scanning due to constraint exclusion.
1901  *
1902  * Currently, such dummy plans are Result nodes with constant FALSE
1903  * filter quals (see set_dummy_rel_pathlist and create_append_plan).
1904  *
1905  * XXX this probably ought to be somewhere else, but not clear where.
1906  */
1907 bool
1908 is_dummy_plan(Plan *plan)
1909 {
1910         if (IsA(plan, Result))
1911         {
1912                 List       *rcqual = (List *) ((Result *) plan)->resconstantqual;
1913
1914                 if (list_length(rcqual) == 1)
1915                 {
1916                         Const      *constqual = (Const *) linitial(rcqual);
1917
1918                         if (constqual && IsA(constqual, Const))
1919                         {
1920                                 if (!constqual->constisnull &&
1921                                         !DatumGetBool(constqual->constvalue))
1922                                         return true;
1923                         }
1924                 }
1925         }
1926         return false;
1927 }
1928
1929 /*
1930  * Create a bitmapset of the RT indexes of live base relations
1931  *
1932  * Helper for preprocess_rowmarks ... at this point in the proceedings,
1933  * the only good way to distinguish baserels from appendrel children
1934  * is to see what is in the join tree.
1935  */
1936 static Bitmapset *
1937 get_base_rel_indexes(Node *jtnode)
1938 {
1939         Bitmapset  *result;
1940
1941         if (jtnode == NULL)
1942                 return NULL;
1943         if (IsA(jtnode, RangeTblRef))
1944         {
1945                 int                     varno = ((RangeTblRef *) jtnode)->rtindex;
1946
1947                 result = bms_make_singleton(varno);
1948         }
1949         else if (IsA(jtnode, FromExpr))
1950         {
1951                 FromExpr   *f = (FromExpr *) jtnode;
1952                 ListCell   *l;
1953
1954                 result = NULL;
1955                 foreach(l, f->fromlist)
1956                         result = bms_join(result,
1957                                                           get_base_rel_indexes(lfirst(l)));
1958         }
1959         else if (IsA(jtnode, JoinExpr))
1960         {
1961                 JoinExpr   *j = (JoinExpr *) jtnode;
1962
1963                 result = bms_join(get_base_rel_indexes(j->larg),
1964                                                   get_base_rel_indexes(j->rarg));
1965         }
1966         else
1967         {
1968                 elog(ERROR, "unrecognized node type: %d",
1969                          (int) nodeTag(jtnode));
1970                 result = NULL;                  /* keep compiler quiet */
1971         }
1972         return result;
1973 }
1974
1975 /*
1976  * preprocess_rowmarks - set up PlanRowMarks if needed
1977  */
1978 static void
1979 preprocess_rowmarks(PlannerInfo *root)
1980 {
1981         Query      *parse = root->parse;
1982         Bitmapset  *rels;
1983         List       *prowmarks;
1984         ListCell   *l;
1985         int                     i;
1986
1987         if (parse->rowMarks)
1988         {
1989                 /*
1990                  * We've got trouble if FOR UPDATE/SHARE appears inside grouping,
1991                  * since grouping renders a reference to individual tuple CTIDs
1992                  * invalid.  This is also checked at parse time, but that's
1993                  * insufficient because of rule substitution, query pullup, etc.
1994                  */
1995                 CheckSelectLocking(parse);
1996         }
1997         else
1998         {
1999                 /*
2000                  * We only need rowmarks for UPDATE, DELETE, or FOR UPDATE/SHARE.
2001                  */
2002                 if (parse->commandType != CMD_UPDATE &&
2003                         parse->commandType != CMD_DELETE)
2004                         return;
2005         }
2006
2007         /*
2008          * We need to have rowmarks for all base relations except the target. We
2009          * make a bitmapset of all base rels and then remove the items we don't
2010          * need or have FOR UPDATE/SHARE marks for.
2011          */
2012         rels = get_base_rel_indexes((Node *) parse->jointree);
2013         if (parse->resultRelation)
2014                 rels = bms_del_member(rels, parse->resultRelation);
2015
2016         /*
2017          * Convert RowMarkClauses to PlanRowMark representation.
2018          */
2019         prowmarks = NIL;
2020         foreach(l, parse->rowMarks)
2021         {
2022                 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
2023                 RangeTblEntry *rte = rt_fetch(rc->rti, parse->rtable);
2024                 PlanRowMark *newrc;
2025
2026                 /*
2027                  * Currently, it is syntactically impossible to have FOR UPDATE
2028                  * applied to an update/delete target rel.      If that ever becomes
2029                  * possible, we should drop the target from the PlanRowMark list.
2030                  */
2031                 Assert(rc->rti != parse->resultRelation);
2032
2033                 /*
2034                  * Ignore RowMarkClauses for subqueries; they aren't real tables and
2035                  * can't support true locking.  Subqueries that got flattened into the
2036                  * main query should be ignored completely.  Any that didn't will get
2037                  * ROW_MARK_COPY items in the next loop.
2038                  */
2039                 if (rte->rtekind != RTE_RELATION)
2040                         continue;
2041
2042                 rels = bms_del_member(rels, rc->rti);
2043
2044                 newrc = makeNode(PlanRowMark);
2045                 newrc->rti = newrc->prti = rc->rti;
2046                 newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2047                 if (rc->forUpdate)
2048                         newrc->markType = ROW_MARK_EXCLUSIVE;
2049                 else
2050                         newrc->markType = ROW_MARK_SHARE;
2051                 newrc->noWait = rc->noWait;
2052                 newrc->isParent = false;
2053
2054                 prowmarks = lappend(prowmarks, newrc);
2055         }
2056
2057         /*
2058          * Now, add rowmarks for any non-target, non-locked base relations.
2059          */
2060         i = 0;
2061         foreach(l, parse->rtable)
2062         {
2063                 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
2064                 PlanRowMark *newrc;
2065
2066                 i++;
2067                 if (!bms_is_member(i, rels))
2068                         continue;
2069
2070                 newrc = makeNode(PlanRowMark);
2071                 newrc->rti = newrc->prti = i;
2072                 newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2073                 /* real tables support REFERENCE, anything else needs COPY */
2074                 if (rte->rtekind == RTE_RELATION &&
2075                         rte->relkind != RELKIND_FOREIGN_TABLE)
2076                         newrc->markType = ROW_MARK_REFERENCE;
2077                 else
2078                         newrc->markType = ROW_MARK_COPY;
2079                 newrc->noWait = false;  /* doesn't matter */
2080                 newrc->isParent = false;
2081
2082                 prowmarks = lappend(prowmarks, newrc);
2083         }
2084
2085         root->rowMarks = prowmarks;
2086 }
2087
2088 /*
2089  * preprocess_limit - do pre-estimation for LIMIT and/or OFFSET clauses
2090  *
2091  * We try to estimate the values of the LIMIT/OFFSET clauses, and pass the
2092  * results back in *count_est and *offset_est.  These variables are set to
2093  * 0 if the corresponding clause is not present, and -1 if it's present
2094  * but we couldn't estimate the value for it.  (The "0" convention is OK
2095  * for OFFSET but a little bit bogus for LIMIT: effectively we estimate
2096  * LIMIT 0 as though it were LIMIT 1.  But this is in line with the planner's
2097  * usual practice of never estimating less than one row.)  These values will
2098  * be passed to make_limit, which see if you change this code.
2099  *
2100  * The return value is the suitably adjusted tuple_fraction to use for
2101  * planning the query.  This adjustment is not overridable, since it reflects
2102  * plan actions that grouping_planner() will certainly take, not assumptions
2103  * about context.
2104  */
2105 static double
2106 preprocess_limit(PlannerInfo *root, double tuple_fraction,
2107                                  int64 *offset_est, int64 *count_est)
2108 {
2109         Query      *parse = root->parse;
2110         Node       *est;
2111         double          limit_fraction;
2112
2113         /* Should not be called unless LIMIT or OFFSET */
2114         Assert(parse->limitCount || parse->limitOffset);
2115
2116         /*
2117          * Try to obtain the clause values.  We use estimate_expression_value
2118          * primarily because it can sometimes do something useful with Params.
2119          */
2120         if (parse->limitCount)
2121         {
2122                 est = estimate_expression_value(root, parse->limitCount);
2123                 if (est && IsA(est, Const))
2124                 {
2125                         if (((Const *) est)->constisnull)
2126                         {
2127                                 /* NULL indicates LIMIT ALL, ie, no limit */
2128                                 *count_est = 0; /* treat as not present */
2129                         }
2130                         else
2131                         {
2132                                 *count_est = DatumGetInt64(((Const *) est)->constvalue);
2133                                 if (*count_est <= 0)
2134                                         *count_est = 1;         /* force to at least 1 */
2135                         }
2136                 }
2137                 else
2138                         *count_est = -1;        /* can't estimate */
2139         }
2140         else
2141                 *count_est = 0;                 /* not present */
2142
2143         if (parse->limitOffset)
2144         {
2145                 est = estimate_expression_value(root, parse->limitOffset);
2146                 if (est && IsA(est, Const))
2147                 {
2148                         if (((Const *) est)->constisnull)
2149                         {
2150                                 /* Treat NULL as no offset; the executor will too */
2151                                 *offset_est = 0;        /* treat as not present */
2152                         }
2153                         else
2154                         {
2155                                 *offset_est = DatumGetInt64(((Const *) est)->constvalue);
2156                                 if (*offset_est < 0)
2157                                         *offset_est = 0;        /* less than 0 is same as 0 */
2158                         }
2159                 }
2160                 else
2161                         *offset_est = -1;       /* can't estimate */
2162         }
2163         else
2164                 *offset_est = 0;                /* not present */
2165
2166         if (*count_est != 0)
2167         {
2168                 /*
2169                  * A LIMIT clause limits the absolute number of tuples returned.
2170                  * However, if it's not a constant LIMIT then we have to guess; for
2171                  * lack of a better idea, assume 10% of the plan's result is wanted.
2172                  */
2173                 if (*count_est < 0 || *offset_est < 0)
2174                 {
2175                         /* LIMIT or OFFSET is an expression ... punt ... */
2176                         limit_fraction = 0.10;
2177                 }
2178                 else
2179                 {
2180                         /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
2181                         limit_fraction = (double) *count_est + (double) *offset_est;
2182                 }
2183
2184                 /*
2185                  * If we have absolute limits from both caller and LIMIT, use the
2186                  * smaller value; likewise if they are both fractional.  If one is
2187                  * fractional and the other absolute, we can't easily determine which
2188                  * is smaller, but we use the heuristic that the absolute will usually
2189                  * be smaller.
2190                  */
2191                 if (tuple_fraction >= 1.0)
2192                 {
2193                         if (limit_fraction >= 1.0)
2194                         {
2195                                 /* both absolute */
2196                                 tuple_fraction = Min(tuple_fraction, limit_fraction);
2197                         }
2198                         else
2199                         {
2200                                 /* caller absolute, limit fractional; use caller's value */
2201                         }
2202                 }
2203                 else if (tuple_fraction > 0.0)
2204                 {
2205                         if (limit_fraction >= 1.0)
2206                         {
2207                                 /* caller fractional, limit absolute; use limit */
2208                                 tuple_fraction = limit_fraction;
2209                         }
2210                         else
2211                         {
2212                                 /* both fractional */
2213                                 tuple_fraction = Min(tuple_fraction, limit_fraction);
2214                         }
2215                 }
2216                 else
2217                 {
2218                         /* no info from caller, just use limit */
2219                         tuple_fraction = limit_fraction;
2220                 }
2221         }
2222         else if (*offset_est != 0 && tuple_fraction > 0.0)
2223         {
2224                 /*
2225                  * We have an OFFSET but no LIMIT.      This acts entirely differently
2226                  * from the LIMIT case: here, we need to increase rather than decrease
2227                  * the caller's tuple_fraction, because the OFFSET acts to cause more
2228                  * tuples to be fetched instead of fewer.  This only matters if we got
2229                  * a tuple_fraction > 0, however.
2230                  *
2231                  * As above, use 10% if OFFSET is present but unestimatable.
2232                  */
2233                 if (*offset_est < 0)
2234                         limit_fraction = 0.10;
2235                 else
2236                         limit_fraction = (double) *offset_est;
2237
2238                 /*
2239                  * If we have absolute counts from both caller and OFFSET, add them
2240                  * together; likewise if they are both fractional.      If one is
2241                  * fractional and the other absolute, we want to take the larger, and
2242                  * we heuristically assume that's the fractional one.
2243                  */
2244                 if (tuple_fraction >= 1.0)
2245                 {
2246                         if (limit_fraction >= 1.0)
2247                         {
2248                                 /* both absolute, so add them together */
2249                                 tuple_fraction += limit_fraction;
2250                         }
2251                         else
2252                         {
2253                                 /* caller absolute, limit fractional; use limit */
2254                                 tuple_fraction = limit_fraction;
2255                         }
2256                 }
2257                 else
2258                 {
2259                         if (limit_fraction >= 1.0)
2260                         {
2261                                 /* caller fractional, limit absolute; use caller's value */
2262                         }
2263                         else
2264                         {
2265                                 /* both fractional, so add them together */
2266                                 tuple_fraction += limit_fraction;
2267                                 if (tuple_fraction >= 1.0)
2268                                         tuple_fraction = 0.0;           /* assume fetch all */
2269                         }
2270                 }
2271         }
2272
2273         return tuple_fraction;
2274 }
2275
2276
2277 /*
2278  * preprocess_groupclause - do preparatory work on GROUP BY clause
2279  *
2280  * The idea here is to adjust the ordering of the GROUP BY elements
2281  * (which in itself is semantically insignificant) to match ORDER BY,
2282  * thereby allowing a single sort operation to both implement the ORDER BY
2283  * requirement and set up for a Unique step that implements GROUP BY.
2284  *
2285  * In principle it might be interesting to consider other orderings of the
2286  * GROUP BY elements, which could match the sort ordering of other
2287  * possible plans (eg an indexscan) and thereby reduce cost.  We don't
2288  * bother with that, though.  Hashed grouping will frequently win anyway.
2289  *
2290  * Note: we need no comparable processing of the distinctClause because
2291  * the parser already enforced that that matches ORDER BY.
2292  */
2293 static void
2294 preprocess_groupclause(PlannerInfo *root)
2295 {
2296         Query      *parse = root->parse;
2297         List       *new_groupclause;
2298         bool            partial_match;
2299         ListCell   *sl;
2300         ListCell   *gl;
2301
2302         /* If no ORDER BY, nothing useful to do here */
2303         if (parse->sortClause == NIL)
2304                 return;
2305
2306         /*
2307          * Scan the ORDER BY clause and construct a list of matching GROUP BY
2308          * items, but only as far as we can make a matching prefix.
2309          *
2310          * This code assumes that the sortClause contains no duplicate items.
2311          */
2312         new_groupclause = NIL;
2313         foreach(sl, parse->sortClause)
2314         {
2315                 SortGroupClause *sc = (SortGroupClause *) lfirst(sl);
2316
2317                 foreach(gl, parse->groupClause)
2318                 {
2319                         SortGroupClause *gc = (SortGroupClause *) lfirst(gl);
2320
2321                         if (equal(gc, sc))
2322                         {
2323                                 new_groupclause = lappend(new_groupclause, gc);
2324                                 break;
2325                         }
2326                 }
2327                 if (gl == NULL)
2328                         break;                          /* no match, so stop scanning */
2329         }
2330
2331         /* Did we match all of the ORDER BY list, or just some of it? */
2332         partial_match = (sl != NULL);
2333
2334         /* If no match at all, no point in reordering GROUP BY */
2335         if (new_groupclause == NIL)
2336                 return;
2337
2338         /*
2339          * Add any remaining GROUP BY items to the new list, but only if we were
2340          * able to make a complete match.  In other words, we only rearrange the
2341          * GROUP BY list if the result is that one list is a prefix of the other
2342          * --- otherwise there's no possibility of a common sort.  Also, give up
2343          * if there are any non-sortable GROUP BY items, since then there's no
2344          * hope anyway.
2345          */
2346         foreach(gl, parse->groupClause)
2347         {
2348                 SortGroupClause *gc = (SortGroupClause *) lfirst(gl);
2349
2350                 if (list_member_ptr(new_groupclause, gc))
2351                         continue;                       /* it matched an ORDER BY item */
2352                 if (partial_match)
2353                         return;                         /* give up, no common sort possible */
2354                 if (!OidIsValid(gc->sortop))
2355                         return;                         /* give up, GROUP BY can't be sorted */
2356                 new_groupclause = lappend(new_groupclause, gc);
2357         }
2358
2359         /* Success --- install the rearranged GROUP BY list */
2360         Assert(list_length(parse->groupClause) == list_length(new_groupclause));
2361         parse->groupClause = new_groupclause;
2362 }
2363
2364 /*
2365  * choose_hashed_grouping - should we use hashed grouping?
2366  *
2367  * Returns TRUE to select hashing, FALSE to select sorting.
2368  */
2369 static bool
2370 choose_hashed_grouping(PlannerInfo *root,
2371                                            double tuple_fraction, double limit_tuples,
2372                                            double path_rows, int path_width,
2373                                            Path *cheapest_path, Path *sorted_path,
2374                                            double dNumGroups, AggClauseCosts *agg_costs)
2375 {
2376         Query      *parse = root->parse;
2377         int                     numGroupCols = list_length(parse->groupClause);
2378         bool            can_hash;
2379         bool            can_sort;
2380         Size            hashentrysize;
2381         List       *target_pathkeys;
2382         List       *current_pathkeys;
2383         Path            hashed_p;
2384         Path            sorted_p;
2385
2386         /*
2387          * Executor doesn't support hashed aggregation with DISTINCT or ORDER BY
2388          * aggregates.  (Doing so would imply storing *all* the input values in
2389          * the hash table, and/or running many sorts in parallel, either of which
2390          * seems like a certain loser.)
2391          */
2392         can_hash = (agg_costs->numOrderedAggs == 0 &&
2393                                 grouping_is_hashable(parse->groupClause));
2394         can_sort = grouping_is_sortable(parse->groupClause);
2395
2396         /* Quick out if only one choice is workable */
2397         if (!(can_hash && can_sort))
2398         {
2399                 if (can_hash)
2400                         return true;
2401                 else if (can_sort)
2402                         return false;
2403                 else
2404                         ereport(ERROR,
2405                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2406                                          errmsg("could not implement GROUP BY"),
2407                                          errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
2408         }
2409
2410         /* Prefer sorting when enable_hashagg is off */
2411         if (!enable_hashagg)
2412                 return false;
2413
2414         /*
2415          * Don't do it if it doesn't look like the hashtable will fit into
2416          * work_mem.
2417          */
2418
2419         /* Estimate per-hash-entry space at tuple width... */
2420         hashentrysize = MAXALIGN(path_width) + MAXALIGN(sizeof(MinimalTupleData));
2421         /* plus space for pass-by-ref transition values... */
2422         hashentrysize += agg_costs->transitionSpace;
2423         /* plus the per-hash-entry overhead */
2424         hashentrysize += hash_agg_entry_size(agg_costs->numAggs);
2425
2426         if (hashentrysize * dNumGroups > work_mem * 1024L)
2427                 return false;
2428
2429         /*
2430          * When we have both GROUP BY and DISTINCT, use the more-rigorous of
2431          * DISTINCT and ORDER BY as the assumed required output sort order. This
2432          * is an oversimplification because the DISTINCT might get implemented via
2433          * hashing, but it's not clear that the case is common enough (or that our
2434          * estimates are good enough) to justify trying to solve it exactly.
2435          */
2436         if (list_length(root->distinct_pathkeys) >
2437                 list_length(root->sort_pathkeys))
2438                 target_pathkeys = root->distinct_pathkeys;
2439         else
2440                 target_pathkeys = root->sort_pathkeys;
2441
2442         /*
2443          * See if the estimated cost is no more than doing it the other way. While
2444          * avoiding the need for sorted input is usually a win, the fact that the
2445          * output won't be sorted may be a loss; so we need to do an actual cost
2446          * comparison.
2447          *
2448          * We need to consider cheapest_path + hashagg [+ final sort] versus
2449          * either cheapest_path [+ sort] + group or agg [+ final sort] or
2450          * presorted_path + group or agg [+ final sort] where brackets indicate a
2451          * step that may not be needed. We assume query_planner() will have
2452          * returned a presorted path only if it's a winner compared to
2453          * cheapest_path for this purpose.
2454          *
2455          * These path variables are dummies that just hold cost fields; we don't
2456          * make actual Paths for these steps.
2457          */
2458         cost_agg(&hashed_p, root, AGG_HASHED, agg_costs,
2459                          numGroupCols, dNumGroups,
2460                          cheapest_path->startup_cost, cheapest_path->total_cost,
2461                          path_rows);
2462         /* Result of hashed agg is always unsorted */
2463         if (target_pathkeys)
2464                 cost_sort(&hashed_p, root, target_pathkeys, hashed_p.total_cost,
2465                                   dNumGroups, path_width,
2466                                   0.0, work_mem, limit_tuples);
2467
2468         if (sorted_path)
2469         {
2470                 sorted_p.startup_cost = sorted_path->startup_cost;
2471                 sorted_p.total_cost = sorted_path->total_cost;
2472                 current_pathkeys = sorted_path->pathkeys;
2473         }
2474         else
2475         {
2476                 sorted_p.startup_cost = cheapest_path->startup_cost;
2477                 sorted_p.total_cost = cheapest_path->total_cost;
2478                 current_pathkeys = cheapest_path->pathkeys;
2479         }
2480         if (!pathkeys_contained_in(root->group_pathkeys, current_pathkeys))
2481         {
2482                 cost_sort(&sorted_p, root, root->group_pathkeys, sorted_p.total_cost,
2483                                   path_rows, path_width,
2484                                   0.0, work_mem, -1.0);
2485                 current_pathkeys = root->group_pathkeys;
2486         }
2487
2488         if (parse->hasAggs)
2489                 cost_agg(&sorted_p, root, AGG_SORTED, agg_costs,
2490                                  numGroupCols, dNumGroups,
2491                                  sorted_p.startup_cost, sorted_p.total_cost,
2492                                  path_rows);
2493         else
2494                 cost_group(&sorted_p, root, numGroupCols, dNumGroups,
2495                                    sorted_p.startup_cost, sorted_p.total_cost,
2496                                    path_rows);
2497         /* The Agg or Group node will preserve ordering */
2498         if (target_pathkeys &&
2499                 !pathkeys_contained_in(target_pathkeys, current_pathkeys))
2500                 cost_sort(&sorted_p, root, target_pathkeys, sorted_p.total_cost,
2501                                   dNumGroups, path_width,
2502                                   0.0, work_mem, limit_tuples);
2503
2504         /*
2505          * Now make the decision using the top-level tuple fraction.  First we
2506          * have to convert an absolute count (LIMIT) into fractional form.
2507          */
2508         if (tuple_fraction >= 1.0)
2509                 tuple_fraction /= dNumGroups;
2510
2511         if (compare_fractional_path_costs(&hashed_p, &sorted_p,
2512                                                                           tuple_fraction) < 0)
2513         {
2514                 /* Hashed is cheaper, so use it */
2515                 return true;
2516         }
2517         return false;
2518 }
2519
2520 /*
2521  * choose_hashed_distinct - should we use hashing for DISTINCT?
2522  *
2523  * This is fairly similar to choose_hashed_grouping, but there are enough
2524  * differences that it doesn't seem worth trying to unify the two functions.
2525  * (One difference is that we sometimes apply this after forming a Plan,
2526  * so the input alternatives can't be represented as Paths --- instead we
2527  * pass in the costs as individual variables.)
2528  *
2529  * But note that making the two choices independently is a bit bogus in
2530  * itself.      If the two could be combined into a single choice operation
2531  * it'd probably be better, but that seems far too unwieldy to be practical,
2532  * especially considering that the combination of GROUP BY and DISTINCT
2533  * isn't very common in real queries.  By separating them, we are giving
2534  * extra preference to using a sorting implementation when a common sort key
2535  * is available ... and that's not necessarily wrong anyway.
2536  *
2537  * Returns TRUE to select hashing, FALSE to select sorting.
2538  */
2539 static bool
2540 choose_hashed_distinct(PlannerInfo *root,
2541                                            double tuple_fraction, double limit_tuples,
2542                                            double path_rows, int path_width,
2543                                            Cost cheapest_startup_cost, Cost cheapest_total_cost,
2544                                            Cost sorted_startup_cost, Cost sorted_total_cost,
2545                                            List *sorted_pathkeys,
2546                                            double dNumDistinctRows)
2547 {
2548         Query      *parse = root->parse;
2549         int                     numDistinctCols = list_length(parse->distinctClause);
2550         bool            can_sort;
2551         bool            can_hash;
2552         Size            hashentrysize;
2553         List       *current_pathkeys;
2554         List       *needed_pathkeys;
2555         Path            hashed_p;
2556         Path            sorted_p;
2557
2558         /*
2559          * If we have a sortable DISTINCT ON clause, we always use sorting. This
2560          * enforces the expected behavior of DISTINCT ON.
2561          */
2562         can_sort = grouping_is_sortable(parse->distinctClause);
2563         if (can_sort && parse->hasDistinctOn)
2564                 return false;
2565
2566         can_hash = grouping_is_hashable(parse->distinctClause);
2567
2568         /* Quick out if only one choice is workable */
2569         if (!(can_hash && can_sort))
2570         {
2571                 if (can_hash)
2572                         return true;
2573                 else if (can_sort)
2574                         return false;
2575                 else
2576                         ereport(ERROR,
2577                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2578                                          errmsg("could not implement DISTINCT"),
2579                                          errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
2580         }
2581
2582         /* Prefer sorting when enable_hashagg is off */
2583         if (!enable_hashagg)
2584                 return false;
2585
2586         /*
2587          * Don't do it if it doesn't look like the hashtable will fit into
2588          * work_mem.
2589          */
2590         hashentrysize = MAXALIGN(path_width) + MAXALIGN(sizeof(MinimalTupleData));
2591
2592         if (hashentrysize * dNumDistinctRows > work_mem * 1024L)
2593                 return false;
2594
2595         /*
2596          * See if the estimated cost is no more than doing it the other way. While
2597          * avoiding the need for sorted input is usually a win, the fact that the
2598          * output won't be sorted may be a loss; so we need to do an actual cost
2599          * comparison.
2600          *
2601          * We need to consider cheapest_path + hashagg [+ final sort] versus
2602          * sorted_path [+ sort] + group [+ final sort] where brackets indicate a
2603          * step that may not be needed.
2604          *
2605          * These path variables are dummies that just hold cost fields; we don't
2606          * make actual Paths for these steps.
2607          */
2608         cost_agg(&hashed_p, root, AGG_HASHED, NULL,
2609                          numDistinctCols, dNumDistinctRows,
2610                          cheapest_startup_cost, cheapest_total_cost,
2611                          path_rows);
2612
2613         /*
2614          * Result of hashed agg is always unsorted, so if ORDER BY is present we
2615          * need to charge for the final sort.
2616          */
2617         if (parse->sortClause)
2618                 cost_sort(&hashed_p, root, root->sort_pathkeys, hashed_p.total_cost,
2619                                   dNumDistinctRows, path_width,
2620                                   0.0, work_mem, limit_tuples);
2621
2622         /*
2623          * Now for the GROUP case.      See comments in grouping_planner about the
2624          * sorting choices here --- this code should match that code.
2625          */
2626         sorted_p.startup_cost = sorted_startup_cost;
2627         sorted_p.total_cost = sorted_total_cost;
2628         current_pathkeys = sorted_pathkeys;
2629         if (parse->hasDistinctOn &&
2630                 list_length(root->distinct_pathkeys) <
2631                 list_length(root->sort_pathkeys))
2632                 needed_pathkeys = root->sort_pathkeys;
2633         else
2634                 needed_pathkeys = root->distinct_pathkeys;
2635         if (!pathkeys_contained_in(needed_pathkeys, current_pathkeys))
2636         {
2637                 if (list_length(root->distinct_pathkeys) >=
2638                         list_length(root->sort_pathkeys))
2639                         current_pathkeys = root->distinct_pathkeys;
2640                 else
2641                         current_pathkeys = root->sort_pathkeys;
2642                 cost_sort(&sorted_p, root, current_pathkeys, sorted_p.total_cost,
2643                                   path_rows, path_width,
2644                                   0.0, work_mem, -1.0);
2645         }
2646         cost_group(&sorted_p, root, numDistinctCols, dNumDistinctRows,
2647                            sorted_p.startup_cost, sorted_p.total_cost,
2648                            path_rows);
2649         if (parse->sortClause &&
2650                 !pathkeys_contained_in(root->sort_pathkeys, current_pathkeys))
2651                 cost_sort(&sorted_p, root, root->sort_pathkeys, sorted_p.total_cost,
2652                                   dNumDistinctRows, path_width,
2653                                   0.0, work_mem, limit_tuples);
2654
2655         /*
2656          * Now make the decision using the top-level tuple fraction.  First we
2657          * have to convert an absolute count (LIMIT) into fractional form.
2658          */
2659         if (tuple_fraction >= 1.0)
2660                 tuple_fraction /= dNumDistinctRows;
2661
2662         if (compare_fractional_path_costs(&hashed_p, &sorted_p,
2663                                                                           tuple_fraction) < 0)
2664         {
2665                 /* Hashed is cheaper, so use it */
2666                 return true;
2667         }
2668         return false;
2669 }
2670
2671 /*
2672  * make_subplanTargetList
2673  *        Generate appropriate target list when grouping is required.
2674  *
2675  * When grouping_planner inserts grouping or aggregation plan nodes
2676  * above the scan/join plan constructed by query_planner+create_plan,
2677  * we typically want the scan/join plan to emit a different target list
2678  * than the outer plan nodes should have.  This routine generates the
2679  * correct target list for the scan/join subplan.
2680  *
2681  * The initial target list passed from the parser already contains entries
2682  * for all ORDER BY and GROUP BY expressions, but it will not have entries
2683  * for variables used only in HAVING clauses; so we need to add those
2684  * variables to the subplan target list.  Also, we flatten all expressions
2685  * except GROUP BY items into their component variables; the other expressions
2686  * will be computed by the inserted nodes rather than by the subplan.
2687  * For example, given a query like
2688  *              SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
2689  * we want to pass this targetlist to the subplan:
2690  *              a+b,c,d
2691  * where the a+b target will be used by the Sort/Group steps, and the
2692  * other targets will be used for computing the final results.
2693  *
2694  * If we are grouping or aggregating, *and* there are no non-Var grouping
2695  * expressions, then the returned tlist is effectively dummy; we do not
2696  * need to force it to be evaluated, because all the Vars it contains
2697  * should be present in the "flat" tlist generated by create_plan, though
2698  * possibly in a different order.  In that case we'll use create_plan's tlist,
2699  * and the tlist made here is only needed as input to query_planner to tell
2700  * it which Vars are needed in the output of the scan/join plan.
2701  *
2702  * 'tlist' is the query's target list.
2703  * 'groupColIdx' receives an array of column numbers for the GROUP BY
2704  *                      expressions (if there are any) in the returned target list.
2705  * 'need_tlist_eval' is set true if we really need to evaluate the
2706  *                      returned tlist as-is.
2707  *
2708  * The result is the targetlist to be passed to query_planner.
2709  */
2710 static List *
2711 make_subplanTargetList(PlannerInfo *root,
2712                                            List *tlist,
2713                                            AttrNumber **groupColIdx,
2714                                            bool *need_tlist_eval)
2715 {
2716         Query      *parse = root->parse;
2717         List       *sub_tlist;
2718         List       *non_group_cols;
2719         List       *non_group_vars;
2720         int                     numCols;
2721
2722         *groupColIdx = NULL;
2723
2724         /*
2725          * If we're not grouping or aggregating, there's nothing to do here;
2726          * query_planner should receive the unmodified target list.
2727          */
2728         if (!parse->hasAggs && !parse->groupClause && !root->hasHavingQual &&
2729                 !parse->hasWindowFuncs)
2730         {
2731                 *need_tlist_eval = true;
2732                 return tlist;
2733         }
2734
2735         /*
2736          * Otherwise, we must build a tlist containing all grouping columns, plus
2737          * any other Vars mentioned in the targetlist and HAVING qual.
2738          */
2739         sub_tlist = NIL;
2740         non_group_cols = NIL;
2741         *need_tlist_eval = false;       /* only eval if not flat tlist */
2742
2743         numCols = list_length(parse->groupClause);
2744         if (numCols > 0)
2745         {
2746                 /*
2747                  * If grouping, create sub_tlist entries for all GROUP BY columns, and
2748                  * make an array showing where the group columns are in the sub_tlist.
2749                  *
2750                  * Note: with this implementation, the array entries will always be
2751                  * 1..N, but we don't want callers to assume that.
2752                  */
2753                 AttrNumber *grpColIdx;
2754                 ListCell   *tl;
2755
2756                 grpColIdx = (AttrNumber *) palloc0(sizeof(AttrNumber) * numCols);
2757                 *groupColIdx = grpColIdx;
2758
2759                 foreach(tl, tlist)
2760                 {
2761                         TargetEntry *tle = (TargetEntry *) lfirst(tl);
2762                         int                     colno;
2763
2764                         colno = get_grouping_column_index(parse, tle);
2765                         if (colno >= 0)
2766                         {
2767                                 /*
2768                                  * It's a grouping column, so add it to the result tlist and
2769                                  * remember its resno in grpColIdx[].
2770                                  */
2771                                 TargetEntry *newtle;
2772
2773                                 newtle = makeTargetEntry(tle->expr,
2774                                                                                  list_length(sub_tlist) + 1,
2775                                                                                  NULL,
2776                                                                                  false);
2777                                 sub_tlist = lappend(sub_tlist, newtle);
2778
2779                                 Assert(grpColIdx[colno] == 0);  /* no dups expected */
2780                                 grpColIdx[colno] = newtle->resno;
2781
2782                                 if (!(newtle->expr && IsA(newtle->expr, Var)))
2783                                         *need_tlist_eval = true;        /* tlist contains non Vars */
2784                         }
2785                         else
2786                         {
2787                                 /*
2788                                  * Non-grouping column, so just remember the expression for
2789                                  * later call to pull_var_clause.  There's no need for
2790                                  * pull_var_clause to examine the TargetEntry node itself.
2791                                  */
2792                                 non_group_cols = lappend(non_group_cols, tle->expr);
2793                         }
2794                 }
2795         }
2796         else
2797         {
2798                 /*
2799                  * With no grouping columns, just pass whole tlist to pull_var_clause.
2800                  * Need (shallow) copy to avoid damaging input tlist below.
2801                  */
2802                 non_group_cols = list_copy(tlist);
2803         }
2804
2805         /*
2806          * If there's a HAVING clause, we'll need the Vars it uses, too.
2807          */
2808         if (parse->havingQual)
2809                 non_group_cols = lappend(non_group_cols, parse->havingQual);
2810
2811         /*
2812          * Pull out all the Vars mentioned in non-group cols (plus HAVING), and
2813          * add them to the result tlist if not already present.  (A Var used
2814          * directly as a GROUP BY item will be present already.)  Note this
2815          * includes Vars used in resjunk items, so we are covering the needs of
2816          * ORDER BY and window specifications.  Vars used within Aggrefs will be
2817          * pulled out here, too.
2818          */
2819         non_group_vars = pull_var_clause((Node *) non_group_cols,
2820                                                                          PVC_RECURSE_AGGREGATES,
2821                                                                          PVC_INCLUDE_PLACEHOLDERS);
2822         sub_tlist = add_to_flat_tlist(sub_tlist, non_group_vars);
2823
2824         /* clean up cruft */
2825         list_free(non_group_vars);
2826         list_free(non_group_cols);
2827
2828         return sub_tlist;
2829 }
2830
2831 /*
2832  * get_grouping_column_index
2833  *              Get the GROUP BY column position, if any, of a targetlist entry.
2834  *
2835  * Returns the index (counting from 0) of the TLE in the GROUP BY list, or -1
2836  * if it's not a grouping column.  Note: the result is unique because the
2837  * parser won't make multiple groupClause entries for the same TLE.
2838  */
2839 static int
2840 get_grouping_column_index(Query *parse, TargetEntry *tle)
2841 {
2842         int                     colno = 0;
2843         Index           ressortgroupref = tle->ressortgroupref;
2844         ListCell   *gl;
2845
2846         /* No need to search groupClause if TLE hasn't got a sortgroupref */
2847         if (ressortgroupref == 0)
2848                 return -1;
2849
2850         foreach(gl, parse->groupClause)
2851         {
2852                 SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2853
2854                 if (grpcl->tleSortGroupRef == ressortgroupref)
2855                         return colno;
2856                 colno++;
2857         }
2858
2859         return -1;
2860 }
2861
2862 /*
2863  * locate_grouping_columns
2864  *              Locate grouping columns in the tlist chosen by create_plan.
2865  *
2866  * This is only needed if we don't use the sub_tlist chosen by
2867  * make_subplanTargetList.      We have to forget the column indexes found
2868  * by that routine and re-locate the grouping exprs in the real sub_tlist.
2869  */
2870 static void
2871 locate_grouping_columns(PlannerInfo *root,
2872                                                 List *tlist,
2873                                                 List *sub_tlist,
2874                                                 AttrNumber *groupColIdx)
2875 {
2876         int                     keyno = 0;
2877         ListCell   *gl;
2878
2879         /*
2880          * No work unless grouping.
2881          */
2882         if (!root->parse->groupClause)
2883         {
2884                 Assert(groupColIdx == NULL);
2885                 return;
2886         }
2887         Assert(groupColIdx != NULL);
2888
2889         foreach(gl, root->parse->groupClause)
2890         {
2891                 SortGroupClause *grpcl = (SortGroupClause *) lfirst(gl);
2892                 Node       *groupexpr = get_sortgroupclause_expr(grpcl, tlist);
2893                 TargetEntry *te = tlist_member(groupexpr, sub_tlist);
2894
2895                 if (!te)
2896                         elog(ERROR, "failed to locate grouping columns");
2897                 groupColIdx[keyno++] = te->resno;
2898         }
2899 }
2900
2901 /*
2902  * postprocess_setop_tlist
2903  *        Fix up targetlist returned by plan_set_operations().
2904  *
2905  * We need to transpose sort key info from the orig_tlist into new_tlist.
2906  * NOTE: this would not be good enough if we supported resjunk sort keys
2907  * for results of set operations --- then, we'd need to project a whole
2908  * new tlist to evaluate the resjunk columns.  For now, just ereport if we
2909  * find any resjunk columns in orig_tlist.
2910  */
2911 static List *
2912 postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
2913 {
2914         ListCell   *l;
2915         ListCell   *orig_tlist_item = list_head(orig_tlist);
2916
2917         foreach(l, new_tlist)
2918         {
2919                 TargetEntry *new_tle = (TargetEntry *) lfirst(l);
2920                 TargetEntry *orig_tle;
2921
2922                 /* ignore resjunk columns in setop result */
2923                 if (new_tle->resjunk)
2924                         continue;
2925
2926                 Assert(orig_tlist_item != NULL);
2927                 orig_tle = (TargetEntry *) lfirst(orig_tlist_item);
2928                 orig_tlist_item = lnext(orig_tlist_item);
2929                 if (orig_tle->resjunk)  /* should not happen */
2930                         elog(ERROR, "resjunk output columns are not implemented");
2931                 Assert(new_tle->resno == orig_tle->resno);
2932                 new_tle->ressortgroupref = orig_tle->ressortgroupref;
2933         }
2934         if (orig_tlist_item != NULL)
2935                 elog(ERROR, "resjunk output columns are not implemented");
2936         return new_tlist;
2937 }
2938
2939 /*
2940  * select_active_windows
2941  *              Create a list of the "active" window clauses (ie, those referenced
2942  *              by non-deleted WindowFuncs) in the order they are to be executed.
2943  */
2944 static List *
2945 select_active_windows(PlannerInfo *root, WindowFuncLists *wflists)
2946 {
2947         List       *result;
2948         List       *actives;
2949         ListCell   *lc;
2950
2951         /* First, make a list of the active windows */
2952         actives = NIL;
2953         foreach(lc, root->parse->windowClause)
2954         {
2955                 WindowClause *wc = (WindowClause *) lfirst(lc);
2956
2957                 /* It's only active if wflists shows some related WindowFuncs */
2958                 Assert(wc->winref <= wflists->maxWinRef);
2959                 if (wflists->windowFuncs[wc->winref] != NIL)
2960                         actives = lappend(actives, wc);
2961         }
2962
2963         /*
2964          * Now, ensure that windows with identical partitioning/ordering clauses
2965          * are adjacent in the list.  This is required by the SQL standard, which
2966          * says that only one sort is to be used for such windows, even if they
2967          * are otherwise distinct (eg, different names or framing clauses).
2968          *
2969          * There is room to be much smarter here, for example detecting whether
2970          * one window's sort keys are a prefix of another's (so that sorting for
2971          * the latter would do for the former), or putting windows first that
2972          * match a sort order available for the underlying query.  For the moment
2973          * we are content with meeting the spec.
2974          */
2975         result = NIL;
2976         while (actives != NIL)
2977         {
2978                 WindowClause *wc = (WindowClause *) linitial(actives);
2979                 ListCell   *prev;
2980                 ListCell   *next;
2981
2982                 /* Move wc from actives to result */
2983                 actives = list_delete_first(actives);
2984                 result = lappend(result, wc);
2985
2986                 /* Now move any matching windows from actives to result */
2987                 prev = NULL;
2988                 for (lc = list_head(actives); lc; lc = next)
2989                 {
2990                         WindowClause *wc2 = (WindowClause *) lfirst(lc);
2991
2992                         next = lnext(lc);
2993                         /* framing options are NOT to be compared here! */
2994                         if (equal(wc->partitionClause, wc2->partitionClause) &&
2995                                 equal(wc->orderClause, wc2->orderClause))
2996                         {
2997                                 actives = list_delete_cell(actives, lc, prev);
2998                                 result = lappend(result, wc2);
2999                         }
3000                         else
3001                                 prev = lc;
3002                 }
3003         }
3004
3005         return result;
3006 }
3007
3008 /*
3009  * add_volatile_sort_exprs
3010  *              Identify any volatile sort/group expressions used by the active
3011  *              windows, and add them to window_tlist if not already present.
3012  *              Return the modified window_tlist.
3013  */
3014 static List *
3015 add_volatile_sort_exprs(List *window_tlist, List *tlist, List *activeWindows)
3016 {
3017         Bitmapset  *sgrefs = NULL;
3018         ListCell   *lc;
3019
3020         /* First, collect the sortgrouprefs of the windows into a bitmapset */
3021         foreach(lc, activeWindows)
3022         {
3023                 WindowClause *wc = (WindowClause *) lfirst(lc);
3024                 ListCell   *lc2;
3025
3026                 foreach(lc2, wc->partitionClause)
3027                 {
3028                         SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc2);
3029
3030                         sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
3031                 }
3032                 foreach(lc2, wc->orderClause)
3033                 {
3034                         SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc2);
3035
3036                         sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
3037                 }
3038         }
3039
3040         /*
3041          * Now scan the original tlist to find the referenced expressions. Any
3042          * that are volatile must be added to window_tlist.
3043          *
3044          * Note: we know that the input window_tlist contains no items marked with
3045          * ressortgrouprefs, so we don't have to worry about collisions of the
3046          * reference numbers.
3047          */
3048         foreach(lc, tlist)
3049         {
3050                 TargetEntry *tle = (TargetEntry *) lfirst(lc);
3051
3052                 if (tle->ressortgroupref != 0 &&
3053                         bms_is_member(tle->ressortgroupref, sgrefs) &&
3054                         contain_volatile_functions((Node *) tle->expr))
3055                 {
3056                         TargetEntry *newtle;
3057
3058                         newtle = makeTargetEntry(tle->expr,
3059                                                                          list_length(window_tlist) + 1,
3060                                                                          NULL,
3061                                                                          false);
3062                         newtle->ressortgroupref = tle->ressortgroupref;
3063                         window_tlist = lappend(window_tlist, newtle);
3064                 }
3065         }
3066
3067         return window_tlist;
3068 }
3069
3070 /*
3071  * make_pathkeys_for_window
3072  *              Create a pathkeys list describing the required input ordering
3073  *              for the given WindowClause.
3074  *
3075  * The required ordering is first the PARTITION keys, then the ORDER keys.
3076  * In the future we might try to implement windowing using hashing, in which
3077  * case the ordering could be relaxed, but for now we always sort.
3078  */
3079 static List *
3080 make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
3081                                                  List *tlist, bool canonicalize)
3082 {
3083         List       *window_pathkeys;
3084         List       *window_sortclauses;
3085
3086         /* Throw error if can't sort */
3087         if (!grouping_is_sortable(wc->partitionClause))
3088                 ereport(ERROR,
3089                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3090                                  errmsg("could not implement window PARTITION BY"),
3091                                  errdetail("Window partitioning columns must be of sortable datatypes.")));
3092         if (!grouping_is_sortable(wc->orderClause))
3093                 ereport(ERROR,
3094                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3095                                  errmsg("could not implement window ORDER BY"),
3096                 errdetail("Window ordering columns must be of sortable datatypes.")));
3097
3098         /* Okay, make the combined pathkeys */
3099         window_sortclauses = list_concat(list_copy(wc->partitionClause),
3100                                                                          list_copy(wc->orderClause));
3101         window_pathkeys = make_pathkeys_for_sortclauses(root,
3102                                                                                                         window_sortclauses,
3103                                                                                                         tlist,
3104                                                                                                         canonicalize);
3105         list_free(window_sortclauses);
3106         return window_pathkeys;
3107 }
3108
3109 /*----------
3110  * get_column_info_for_window
3111  *              Get the partitioning/ordering column numbers and equality operators
3112  *              for a WindowAgg node.
3113  *
3114  * This depends on the behavior of make_pathkeys_for_window()!
3115  *
3116  * We are given the target WindowClause and an array of the input column
3117  * numbers associated with the resulting pathkeys.      In the easy case, there
3118  * are the same number of pathkey columns as partitioning + ordering columns
3119  * and we just have to copy some data around.  However, it's possible that
3120  * some of the original partitioning + ordering columns were eliminated as
3121  * redundant during the transformation to pathkeys.  (This can happen even
3122  * though the parser gets rid of obvious duplicates.  A typical scenario is a
3123  * window specification "PARTITION BY x ORDER BY y" coupled with a clause
3124  * "WHERE x = y" that causes the two sort columns to be recognized as
3125  * redundant.)  In that unusual case, we have to work a lot harder to
3126  * determine which keys are significant.
3127  *
3128  * The method used here is a bit brute-force: add the sort columns to a list
3129  * one at a time and note when the resulting pathkey list gets longer.  But
3130  * it's a sufficiently uncommon case that a faster way doesn't seem worth
3131  * the amount of code refactoring that'd be needed.
3132  *----------
3133  */
3134 static void
3135 get_column_info_for_window(PlannerInfo *root, WindowClause *wc, List *tlist,
3136                                                    int numSortCols, AttrNumber *sortColIdx,
3137                                                    int *partNumCols,
3138                                                    AttrNumber **partColIdx,
3139                                                    Oid **partOperators,
3140                                                    int *ordNumCols,
3141                                                    AttrNumber **ordColIdx,
3142                                                    Oid **ordOperators)
3143 {
3144         int                     numPart = list_length(wc->partitionClause);
3145         int                     numOrder = list_length(wc->orderClause);
3146
3147         if (numSortCols == numPart + numOrder)
3148         {
3149                 /* easy case */
3150                 *partNumCols = numPart;
3151                 *partColIdx = sortColIdx;
3152                 *partOperators = extract_grouping_ops(wc->partitionClause);
3153                 *ordNumCols = numOrder;
3154                 *ordColIdx = sortColIdx + numPart;
3155                 *ordOperators = extract_grouping_ops(wc->orderClause);
3156         }
3157         else
3158         {
3159                 List       *sortclauses;
3160                 List       *pathkeys;
3161                 int                     scidx;
3162                 ListCell   *lc;
3163
3164                 /* first, allocate what's certainly enough space for the arrays */
3165                 *partNumCols = 0;
3166                 *partColIdx = (AttrNumber *) palloc(numPart * sizeof(AttrNumber));
3167                 *partOperators = (Oid *) palloc(numPart * sizeof(Oid));
3168                 *ordNumCols = 0;
3169                 *ordColIdx = (AttrNumber *) palloc(numOrder * sizeof(AttrNumber));
3170                 *ordOperators = (Oid *) palloc(numOrder * sizeof(Oid));
3171                 sortclauses = NIL;
3172                 pathkeys = NIL;
3173                 scidx = 0;
3174                 foreach(lc, wc->partitionClause)
3175                 {
3176                         SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
3177                         List       *new_pathkeys;
3178
3179                         sortclauses = lappend(sortclauses, sgc);
3180                         new_pathkeys = make_pathkeys_for_sortclauses(root,
3181                                                                                                                  sortclauses,
3182                                                                                                                  tlist,
3183                                                                                                                  true);
3184                         if (list_length(new_pathkeys) > list_length(pathkeys))
3185                         {
3186                                 /* this sort clause is actually significant */
3187                                 (*partColIdx)[*partNumCols] = sortColIdx[scidx++];
3188                                 (*partOperators)[*partNumCols] = sgc->eqop;
3189                                 (*partNumCols)++;
3190                                 pathkeys = new_pathkeys;
3191                         }
3192                 }
3193                 foreach(lc, wc->orderClause)
3194                 {
3195                         SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
3196                         List       *new_pathkeys;
3197
3198                         sortclauses = lappend(sortclauses, sgc);
3199                         new_pathkeys = make_pathkeys_for_sortclauses(root,
3200                                                                                                                  sortclauses,
3201                                                                                                                  tlist,
3202                                                                                                                  true);
3203                         if (list_length(new_pathkeys) > list_length(pathkeys))
3204                         {
3205                                 /* this sort clause is actually significant */
3206                                 (*ordColIdx)[*ordNumCols] = sortColIdx[scidx++];
3207                                 (*ordOperators)[*ordNumCols] = sgc->eqop;
3208                                 (*ordNumCols)++;
3209                                 pathkeys = new_pathkeys;
3210                         }
3211                 }
3212                 /* complain if we didn't eat exactly the right number of sort cols */
3213                 if (scidx != numSortCols)
3214                         elog(ERROR, "failed to deconstruct sort operators into partitioning/ordering operators");
3215         }
3216 }
3217
3218
3219 /*
3220  * expression_planner
3221  *              Perform planner's transformations on a standalone expression.
3222  *
3223  * Various utility commands need to evaluate expressions that are not part
3224  * of a plannable query.  They can do so using the executor's regular
3225  * expression-execution machinery, but first the expression has to be fed
3226  * through here to transform it from parser output to something executable.
3227  *
3228  * Currently, we disallow sublinks in standalone expressions, so there's no
3229  * real "planning" involved here.  (That might not always be true though.)
3230  * What we must do is run eval_const_expressions to ensure that any function
3231  * calls are converted to positional notation and function default arguments
3232  * get inserted.  The fact that constant subexpressions get simplified is a
3233  * side-effect that is useful when the expression will get evaluated more than
3234  * once.  Also, we must fix operator function IDs.
3235  *
3236  * Note: this must not make any damaging changes to the passed-in expression
3237  * tree.  (It would actually be okay to apply fix_opfuncids to it, but since
3238  * we first do an expression_tree_mutator-based walk, what is returned will
3239  * be a new node tree.)
3240  */
3241 Expr *
3242 expression_planner(Expr *expr)
3243 {
3244         Node       *result;
3245
3246         /*
3247          * Convert named-argument function calls, insert default arguments and
3248          * simplify constant subexprs
3249          */
3250         result = eval_const_expressions(NULL, (Node *) expr);
3251
3252         /* Fill in opfuncid values if missing */
3253         fix_opfuncids(result);
3254
3255         return (Expr *) result;
3256 }
3257
3258
3259 /*
3260  * plan_cluster_use_sort
3261  *              Use the planner to decide how CLUSTER should implement sorting
3262  *
3263  * tableOid is the OID of a table to be clustered on its index indexOid
3264  * (which is already known to be a btree index).  Decide whether it's
3265  * cheaper to do an indexscan or a seqscan-plus-sort to execute the CLUSTER.
3266  * Return TRUE to use sorting, FALSE to use an indexscan.
3267  *
3268  * Note: caller had better already hold some type of lock on the table.
3269  */
3270 bool
3271 plan_cluster_use_sort(Oid tableOid, Oid indexOid)
3272 {
3273         PlannerInfo *root;
3274         Query      *query;
3275         PlannerGlobal *glob;
3276         RangeTblEntry *rte;
3277         RelOptInfo *rel;
3278         IndexOptInfo *indexInfo;
3279         QualCost        indexExprCost;
3280         Cost            comparisonCost;
3281         Path       *seqScanPath;
3282         Path            seqScanAndSortPath;
3283         IndexPath  *indexScanPath;
3284         ListCell   *lc;
3285
3286         /* Set up mostly-dummy planner state */
3287         query = makeNode(Query);
3288         query->commandType = CMD_SELECT;
3289
3290         glob = makeNode(PlannerGlobal);
3291
3292         root = makeNode(PlannerInfo);
3293         root->parse = query;
3294         root->glob = glob;
3295         root->query_level = 1;
3296         root->planner_cxt = CurrentMemoryContext;
3297         root->wt_param_id = -1;
3298
3299         /* Build a minimal RTE for the rel */
3300         rte = makeNode(RangeTblEntry);
3301         rte->rtekind = RTE_RELATION;
3302         rte->relid = tableOid;
3303         rte->relkind = RELKIND_RELATION;
3304         rte->lateral = false;
3305         rte->inh = false;
3306         rte->inFromCl = true;
3307         query->rtable = list_make1(rte);
3308
3309         /* Set up RTE/RelOptInfo arrays */
3310         setup_simple_rel_arrays(root);
3311
3312         /* Build RelOptInfo */
3313         rel = build_simple_rel(root, 1, RELOPT_BASEREL);
3314
3315         /* Locate IndexOptInfo for the target index */
3316         indexInfo = NULL;
3317         foreach(lc, rel->indexlist)
3318         {
3319                 indexInfo = (IndexOptInfo *) lfirst(lc);
3320                 if (indexInfo->indexoid == indexOid)
3321                         break;
3322         }
3323
3324         /*
3325          * It's possible that get_relation_info did not generate an IndexOptInfo
3326          * for the desired index; this could happen if it's not yet reached its
3327          * indcheckxmin usability horizon, or if it's a system index and we're
3328          * ignoring system indexes.  In such cases we should tell CLUSTER to not
3329          * trust the index contents but use seqscan-and-sort.
3330          */
3331         if (lc == NULL)                         /* not in the list? */
3332                 return true;                    /* use sort */
3333
3334         /*
3335          * Rather than doing all the pushups that would be needed to use
3336          * set_baserel_size_estimates, just do a quick hack for rows and width.
3337          */
3338         rel->rows = rel->tuples;
3339         rel->width = get_relation_data_width(tableOid, NULL);
3340
3341         root->total_table_pages = rel->pages;
3342
3343         /*
3344          * Determine eval cost of the index expressions, if any.  We need to
3345          * charge twice that amount for each tuple comparison that happens during
3346          * the sort, since tuplesort.c will have to re-evaluate the index
3347          * expressions each time.  (XXX that's pretty inefficient...)
3348          */
3349         cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
3350         comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
3351
3352         /* Estimate the cost of seq scan + sort */
3353         seqScanPath = create_seqscan_path(root, rel, NULL);
3354         cost_sort(&seqScanAndSortPath, root, NIL,
3355                           seqScanPath->total_cost, rel->tuples, rel->width,
3356                           comparisonCost, maintenance_work_mem, -1.0);
3357
3358         /* Estimate the cost of index scan */
3359         indexScanPath = create_index_path(root, indexInfo,
3360                                                                           NIL, NIL, NIL, NIL, NIL,
3361                                                                           ForwardScanDirection, false,
3362                                                                           NULL, 1.0);
3363
3364         return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
3365 }