PostgreSQL Source Code  git master
planner.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * planner.c
4  * The query optimizer external interface.
5  *
6  * Portions Copyright (c) 1996-2024, 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 #include <math.h>
20 
21 #include "access/genam.h"
22 #include "access/parallel.h"
23 #include "access/sysattr.h"
24 #include "access/table.h"
25 #include "catalog/pg_aggregate.h"
26 #include "catalog/pg_constraint.h"
27 #include "catalog/pg_inherits.h"
28 #include "catalog/pg_proc.h"
29 #include "catalog/pg_type.h"
30 #include "executor/executor.h"
31 #include "foreign/fdwapi.h"
32 #include "jit/jit.h"
33 #include "lib/bipartite_match.h"
34 #include "lib/knapsack.h"
35 #include "miscadmin.h"
36 #include "nodes/makefuncs.h"
37 #include "nodes/nodeFuncs.h"
38 #ifdef OPTIMIZER_DEBUG
39 #include "nodes/print.h"
40 #endif
41 #include "nodes/supportnodes.h"
42 #include "optimizer/appendinfo.h"
43 #include "optimizer/clauses.h"
44 #include "optimizer/cost.h"
45 #include "optimizer/optimizer.h"
46 #include "optimizer/paramassign.h"
47 #include "optimizer/pathnode.h"
48 #include "optimizer/paths.h"
49 #include "optimizer/plancat.h"
50 #include "optimizer/planmain.h"
51 #include "optimizer/planner.h"
52 #include "optimizer/prep.h"
53 #include "optimizer/subselect.h"
54 #include "optimizer/tlist.h"
55 #include "parser/analyze.h"
56 #include "parser/parse_agg.h"
57 #include "parser/parse_clause.h"
58 #include "parser/parse_relation.h"
59 #include "parser/parsetree.h"
60 #include "partitioning/partdesc.h"
61 #include "utils/lsyscache.h"
62 #include "utils/rel.h"
63 #include "utils/selfuncs.h"
64 
65 /* GUC parameters */
69 
70 /* Hook for plugins to get control in planner() */
72 
73 /* Hook for plugins to get control when grouping_planner() plans upper rels */
75 
76 
77 /* Expression kind codes for preprocess_expression */
78 #define EXPRKIND_QUAL 0
79 #define EXPRKIND_TARGET 1
80 #define EXPRKIND_RTFUNC 2
81 #define EXPRKIND_RTFUNC_LATERAL 3
82 #define EXPRKIND_VALUES 4
83 #define EXPRKIND_VALUES_LATERAL 5
84 #define EXPRKIND_LIMIT 6
85 #define EXPRKIND_APPINFO 7
86 #define EXPRKIND_PHV 8
87 #define EXPRKIND_TABLESAMPLE 9
88 #define EXPRKIND_ARBITER_ELEM 10
89 #define EXPRKIND_TABLEFUNC 11
90 #define EXPRKIND_TABLEFUNC_LATERAL 12
91 
92 /*
93  * Data specific to grouping sets
94  */
95 typedef struct
96 {
106 
107 /*
108  * Temporary structure for use during WindowClause reordering in order to be
109  * able to sort WindowClauses on partitioning/ordering prefix.
110  */
111 typedef struct
112 {
114  List *uniqueOrder; /* A List of unique ordering/partitioning
115  * clauses per Window */
117 
118 /* Passthrough data for standard_qp_callback */
119 typedef struct
120 {
121  List *activeWindows; /* active windows, if any */
122  grouping_sets_data *gset_data; /* grouping sets data, if any */
123  SetOperationStmt *setop; /* parent set operation or NULL if not a
124  * subquery belonging to a set operation */
126 
127 /* Local functions */
128 static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
129 static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
130 static void grouping_planner(PlannerInfo *root, double tuple_fraction,
131  SetOperationStmt *setops);
133 static List *remap_to_groupclause_idx(List *groupClause, List *gsets,
134  int *tleref_to_colnum_map);
135 static void preprocess_rowmarks(PlannerInfo *root);
136 static double preprocess_limit(PlannerInfo *root,
137  double tuple_fraction,
138  int64 *offset_est, int64 *count_est);
141 static List *extract_rollup_sets(List *groupingSets);
142 static List *reorder_grouping_sets(List *groupingSets, List *sortclause);
143 static void standard_qp_callback(PlannerInfo *root, void *extra);
144 static double get_number_of_groups(PlannerInfo *root,
145  double path_rows,
146  grouping_sets_data *gd,
147  List *target_list);
149  RelOptInfo *input_rel,
150  PathTarget *target,
151  bool target_parallel_safe,
152  grouping_sets_data *gd);
155  RelOptInfo *input_rel,
156  RelOptInfo *grouped_rel);
158  PathTarget *target, bool target_parallel_safe,
159  Node *havingQual);
161  RelOptInfo *input_rel,
162  RelOptInfo *grouped_rel,
163  const AggClauseCosts *agg_costs,
164  grouping_sets_data *gd,
165  GroupPathExtraData *extra,
166  RelOptInfo **partially_grouped_rel_p);
168  RelOptInfo *grouped_rel,
169  Path *path,
170  bool is_sorted,
171  bool can_hash,
172  grouping_sets_data *gd,
173  const AggClauseCosts *agg_costs,
174  double dNumGroups);
176  RelOptInfo *input_rel,
177  PathTarget *input_target,
178  PathTarget *output_target,
179  bool output_target_parallel_safe,
180  WindowFuncLists *wflists,
181  List *activeWindows);
183  RelOptInfo *window_rel,
184  Path *path,
185  PathTarget *input_target,
186  PathTarget *output_target,
187  WindowFuncLists *wflists,
188  List *activeWindows);
190  RelOptInfo *input_rel,
191  PathTarget *target);
193  RelOptInfo *input_rel,
194  RelOptInfo *final_distinct_rel,
195  PathTarget *target);
197  RelOptInfo *input_rel,
198  RelOptInfo *distinct_rel);
200  RelOptInfo *input_rel,
201  PathTarget *target,
202  bool target_parallel_safe,
203  double limit_tuples);
205  PathTarget *final_target);
207  PathTarget *grouping_target,
208  Node *havingQual);
209 static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
211  WindowFuncLists *wflists);
214  PathTarget *final_target,
215  List *activeWindows);
217  List *tlist);
219  PathTarget *final_target,
220  bool *have_postponed_srfs);
222  List *targets, List *targets_contain_srfs);
223 static void add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
224  RelOptInfo *grouped_rel,
225  RelOptInfo *partially_grouped_rel,
226  const AggClauseCosts *agg_costs,
227  grouping_sets_data *gd,
228  double dNumGroups,
229  GroupPathExtraData *extra);
231  RelOptInfo *grouped_rel,
232  RelOptInfo *input_rel,
233  grouping_sets_data *gd,
234  GroupPathExtraData *extra,
235  bool force_rel_creation);
237 static bool can_partial_agg(PlannerInfo *root);
239  RelOptInfo *rel,
240  List *scanjoin_targets,
241  List *scanjoin_targets_contain_srfs,
242  bool scanjoin_target_parallel_safe,
243  bool tlist_same_exprs);
245  RelOptInfo *input_rel,
246  RelOptInfo *grouped_rel,
247  RelOptInfo *partially_grouped_rel,
248  const AggClauseCosts *agg_costs,
249  grouping_sets_data *gd,
251  GroupPathExtraData *extra);
252 static bool group_by_has_partkey(RelOptInfo *input_rel,
253  List *targetList,
254  List *groupClause);
255 static int common_prefix_cmp(const void *a, const void *b);
257  List *targetlist);
258 
259 
260 /*****************************************************************************
261  *
262  * Query optimizer entry point
263  *
264  * To support loadable plugins that monitor or modify planner behavior,
265  * we provide a hook variable that lets a plugin get control before and
266  * after the standard planning process. The plugin would normally call
267  * standard_planner().
268  *
269  * Note to plugin authors: standard_planner() scribbles on its Query input,
270  * so you'd better copy that data structure if you want to plan more than once.
271  *
272  *****************************************************************************/
273 PlannedStmt *
274 planner(Query *parse, const char *query_string, int cursorOptions,
275  ParamListInfo boundParams)
276 {
277  PlannedStmt *result;
278 
279  if (planner_hook)
280  result = (*planner_hook) (parse, query_string, cursorOptions, boundParams);
281  else
282  result = standard_planner(parse, query_string, cursorOptions, boundParams);
283  return result;
284 }
285 
286 PlannedStmt *
287 standard_planner(Query *parse, const char *query_string, int cursorOptions,
288  ParamListInfo boundParams)
289 {
290  PlannedStmt *result;
291  PlannerGlobal *glob;
292  double tuple_fraction;
293  PlannerInfo *root;
294  RelOptInfo *final_rel;
295  Path *best_path;
296  Plan *top_plan;
297  ListCell *lp,
298  *lr;
299 
300  /*
301  * Set up global state for this planner invocation. This data is needed
302  * across all levels of sub-Query that might exist in the given command,
303  * so we keep it in a separate struct that's linked to by each per-Query
304  * PlannerInfo.
305  */
306  glob = makeNode(PlannerGlobal);
307 
308  glob->boundParams = boundParams;
309  glob->subplans = NIL;
310  glob->subpaths = NIL;
311  glob->subroots = NIL;
312  glob->rewindPlanIDs = NULL;
313  glob->finalrtable = NIL;
314  glob->finalrteperminfos = NIL;
315  glob->finalrowmarks = NIL;
316  glob->resultRelations = NIL;
317  glob->appendRelations = NIL;
318  glob->relationOids = NIL;
319  glob->invalItems = NIL;
320  glob->paramExecTypes = NIL;
321  glob->lastPHId = 0;
322  glob->lastRowMarkId = 0;
323  glob->lastPlanNodeId = 0;
324  glob->transientPlan = false;
325  glob->dependsOnRole = false;
326 
327  /*
328  * Assess whether it's feasible to use parallel mode for this query. We
329  * can't do this in a standalone backend, or if the command will try to
330  * modify any data, or if this is a cursor operation, or if GUCs are set
331  * to values that don't permit parallelism, or if parallel-unsafe
332  * functions are present in the query tree.
333  *
334  * (Note that we do allow CREATE TABLE AS, SELECT INTO, and CREATE
335  * MATERIALIZED VIEW to use parallel plans, but this is safe only because
336  * the command is writing into a completely new table which workers won't
337  * be able to see. If the workers could see the table, the fact that
338  * group locking would cause them to ignore the leader's heavyweight GIN
339  * page locks would make this unsafe. We'll have to fix that somehow if
340  * we want to allow parallel inserts in general; updates and deletes have
341  * additional problems especially around combo CIDs.)
342  *
343  * For now, we don't try to use parallel mode if we're running inside a
344  * parallel worker. We might eventually be able to relax this
345  * restriction, but for now it seems best not to have parallel workers
346  * trying to create their own parallel workers.
347  */
348  if ((cursorOptions & CURSOR_OPT_PARALLEL_OK) != 0 &&
350  parse->commandType == CMD_SELECT &&
351  !parse->hasModifyingCTE &&
353  !IsParallelWorker())
354  {
355  /* all the cheap tests pass, so scan the query tree */
357  glob->parallelModeOK = (glob->maxParallelHazard != PROPARALLEL_UNSAFE);
358  }
359  else
360  {
361  /* skip the query tree scan, just assume it's unsafe */
362  glob->maxParallelHazard = PROPARALLEL_UNSAFE;
363  glob->parallelModeOK = false;
364  }
365 
366  /*
367  * glob->parallelModeNeeded is normally set to false here and changed to
368  * true during plan creation if a Gather or Gather Merge plan is actually
369  * created (cf. create_gather_plan, create_gather_merge_plan).
370  *
371  * However, if debug_parallel_query = on or debug_parallel_query =
372  * regress, then we impose parallel mode whenever it's safe to do so, even
373  * if the final plan doesn't use parallelism. It's not safe to do so if
374  * the query contains anything parallel-unsafe; parallelModeOK will be
375  * false in that case. Note that parallelModeOK can't change after this
376  * point. Otherwise, everything in the query is either parallel-safe or
377  * parallel-restricted, and in either case it should be OK to impose
378  * parallel-mode restrictions. If that ends up breaking something, then
379  * either some function the user included in the query is incorrectly
380  * labeled as parallel-safe or parallel-restricted when in reality it's
381  * parallel-unsafe, or else the query planner itself has a bug.
382  */
383  glob->parallelModeNeeded = glob->parallelModeOK &&
385 
386  /* Determine what fraction of the plan is likely to be scanned */
387  if (cursorOptions & CURSOR_OPT_FAST_PLAN)
388  {
389  /*
390  * We have no real idea how many tuples the user will ultimately FETCH
391  * from a cursor, but it is often the case that he doesn't want 'em
392  * all, or would prefer a fast-start plan anyway so that he can
393  * process some of the tuples sooner. Use a GUC parameter to decide
394  * what fraction to optimize for.
395  */
396  tuple_fraction = cursor_tuple_fraction;
397 
398  /*
399  * We document cursor_tuple_fraction as simply being a fraction, which
400  * means the edge cases 0 and 1 have to be treated specially here. We
401  * convert 1 to 0 ("all the tuples") and 0 to a very small fraction.
402  */
403  if (tuple_fraction >= 1.0)
404  tuple_fraction = 0.0;
405  else if (tuple_fraction <= 0.0)
406  tuple_fraction = 1e-10;
407  }
408  else
409  {
410  /* Default assumption is we need all the tuples */
411  tuple_fraction = 0.0;
412  }
413 
414  /* primary planning entry point (may recurse for subqueries) */
415  root = subquery_planner(glob, parse, NULL, false, tuple_fraction, NULL);
416 
417  /* Select best Path and turn it into a Plan */
418  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
419  best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
420 
421  top_plan = create_plan(root, best_path);
422 
423  /*
424  * If creating a plan for a scrollable cursor, make sure it can run
425  * backwards on demand. Add a Material node at the top at need.
426  */
427  if (cursorOptions & CURSOR_OPT_SCROLL)
428  {
429  if (!ExecSupportsBackwardScan(top_plan))
430  top_plan = materialize_finished_plan(top_plan);
431  }
432 
433  /*
434  * Optionally add a Gather node for testing purposes, provided this is
435  * actually a safe thing to do.
436  *
437  * We can add Gather even when top_plan has parallel-safe initPlans, but
438  * then we have to move the initPlans to the Gather node because of
439  * SS_finalize_plan's limitations. That would cause cosmetic breakage of
440  * regression tests when debug_parallel_query = regress, because initPlans
441  * that would normally appear on the top_plan move to the Gather, causing
442  * them to disappear from EXPLAIN output. That doesn't seem worth kluging
443  * EXPLAIN to hide, so skip it when debug_parallel_query = regress.
444  */
446  top_plan->parallel_safe &&
447  (top_plan->initPlan == NIL ||
449  {
450  Gather *gather = makeNode(Gather);
451  Cost initplan_cost;
452  bool unsafe_initplans;
453 
454  gather->plan.targetlist = top_plan->targetlist;
455  gather->plan.qual = NIL;
456  gather->plan.lefttree = top_plan;
457  gather->plan.righttree = NULL;
458  gather->num_workers = 1;
459  gather->single_copy = true;
461 
462  /* Transfer any initPlans to the new top node */
463  gather->plan.initPlan = top_plan->initPlan;
464  top_plan->initPlan = NIL;
465 
466  /*
467  * Since this Gather has no parallel-aware descendants to signal to,
468  * we don't need a rescan Param.
469  */
470  gather->rescan_param = -1;
471 
472  /*
473  * Ideally we'd use cost_gather here, but setting up dummy path data
474  * to satisfy it doesn't seem much cleaner than knowing what it does.
475  */
476  gather->plan.startup_cost = top_plan->startup_cost +
478  gather->plan.total_cost = top_plan->total_cost +
480  gather->plan.plan_rows = top_plan->plan_rows;
481  gather->plan.plan_width = top_plan->plan_width;
482  gather->plan.parallel_aware = false;
483  gather->plan.parallel_safe = false;
484 
485  /*
486  * Delete the initplans' cost from top_plan. We needn't add it to the
487  * Gather node, since the above coding already included it there.
488  */
490  &initplan_cost, &unsafe_initplans);
491  top_plan->startup_cost -= initplan_cost;
492  top_plan->total_cost -= initplan_cost;
493 
494  /* use parallel mode for parallel plans. */
495  root->glob->parallelModeNeeded = true;
496 
497  top_plan = &gather->plan;
498  }
499 
500  /*
501  * If any Params were generated, run through the plan tree and compute
502  * each plan node's extParam/allParam sets. Ideally we'd merge this into
503  * set_plan_references' tree traversal, but for now it has to be separate
504  * because we need to visit subplans before not after main plan.
505  */
506  if (glob->paramExecTypes != NIL)
507  {
508  Assert(list_length(glob->subplans) == list_length(glob->subroots));
509  forboth(lp, glob->subplans, lr, glob->subroots)
510  {
511  Plan *subplan = (Plan *) lfirst(lp);
512  PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
513 
514  SS_finalize_plan(subroot, subplan);
515  }
516  SS_finalize_plan(root, top_plan);
517  }
518 
519  /* final cleanup of the plan */
520  Assert(glob->finalrtable == NIL);
521  Assert(glob->finalrteperminfos == NIL);
522  Assert(glob->finalrowmarks == NIL);
523  Assert(glob->resultRelations == NIL);
524  Assert(glob->appendRelations == NIL);
525  top_plan = set_plan_references(root, top_plan);
526  /* ... and the subplans (both regular subplans and initplans) */
527  Assert(list_length(glob->subplans) == list_length(glob->subroots));
528  forboth(lp, glob->subplans, lr, glob->subroots)
529  {
530  Plan *subplan = (Plan *) lfirst(lp);
531  PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
532 
533  lfirst(lp) = set_plan_references(subroot, subplan);
534  }
535 
536  /* build the PlannedStmt result */
537  result = makeNode(PlannedStmt);
538 
539  result->commandType = parse->commandType;
540  result->queryId = parse->queryId;
541  result->hasReturning = (parse->returningList != NIL);
542  result->hasModifyingCTE = parse->hasModifyingCTE;
543  result->canSetTag = parse->canSetTag;
544  result->transientPlan = glob->transientPlan;
545  result->dependsOnRole = glob->dependsOnRole;
546  result->parallelModeNeeded = glob->parallelModeNeeded;
547  result->planTree = top_plan;
548  result->rtable = glob->finalrtable;
549  result->permInfos = glob->finalrteperminfos;
550  result->resultRelations = glob->resultRelations;
551  result->appendRelations = glob->appendRelations;
552  result->subplans = glob->subplans;
553  result->rewindPlanIDs = glob->rewindPlanIDs;
554  result->rowMarks = glob->finalrowmarks;
555  result->relationOids = glob->relationOids;
556  result->invalItems = glob->invalItems;
557  result->paramExecTypes = glob->paramExecTypes;
558  /* utilityStmt should be null, but we might as well copy it */
559  result->utilityStmt = parse->utilityStmt;
560  result->stmt_location = parse->stmt_location;
561  result->stmt_len = parse->stmt_len;
562 
563  result->jitFlags = PGJIT_NONE;
564  if (jit_enabled && jit_above_cost >= 0 &&
565  top_plan->total_cost > jit_above_cost)
566  {
567  result->jitFlags |= PGJIT_PERFORM;
568 
569  /*
570  * Decide how much effort should be put into generating better code.
571  */
572  if (jit_optimize_above_cost >= 0 &&
574  result->jitFlags |= PGJIT_OPT3;
575  if (jit_inline_above_cost >= 0 &&
576  top_plan->total_cost > jit_inline_above_cost)
577  result->jitFlags |= PGJIT_INLINE;
578 
579  /*
580  * Decide which operations should be JITed.
581  */
582  if (jit_expressions)
583  result->jitFlags |= PGJIT_EXPR;
585  result->jitFlags |= PGJIT_DEFORM;
586  }
587 
588  if (glob->partition_directory != NULL)
589  DestroyPartitionDirectory(glob->partition_directory);
590 
591  return result;
592 }
593 
594 
595 /*--------------------
596  * subquery_planner
597  * Invokes the planner on a subquery. We recurse to here for each
598  * sub-SELECT found in the query tree.
599  *
600  * glob is the global state for the current planner run.
601  * parse is the querytree produced by the parser & rewriter.
602  * parent_root is the immediate parent Query's info (NULL at the top level).
603  * hasRecursion is true if this is a recursive WITH query.
604  * tuple_fraction is the fraction of tuples we expect will be retrieved.
605  * tuple_fraction is interpreted as explained for grouping_planner, below.
606  * setops is used for set operation subqueries to provide the subquery with
607  * the context in which it's being used so that Paths correctly sorted for the
608  * set operation can be generated. NULL when not planning a set operation
609  * child.
610  *
611  * Basically, this routine does the stuff that should only be done once
612  * per Query object. It then calls grouping_planner. At one time,
613  * grouping_planner could be invoked recursively on the same Query object;
614  * that's not currently true, but we keep the separation between the two
615  * routines anyway, in case we need it again someday.
616  *
617  * subquery_planner will be called recursively to handle sub-Query nodes
618  * found within the query's expressions and rangetable.
619  *
620  * Returns the PlannerInfo struct ("root") that contains all data generated
621  * while planning the subquery. In particular, the Path(s) attached to
622  * the (UPPERREL_FINAL, NULL) upperrel represent our conclusions about the
623  * cheapest way(s) to implement the query. The top level will select the
624  * best Path and pass it through createplan.c to produce a finished Plan.
625  *--------------------
626  */
627 PlannerInfo *
629  bool hasRecursion, double tuple_fraction,
630  SetOperationStmt *setops)
631 {
632  PlannerInfo *root;
633  List *newWithCheckOptions;
634  List *newHaving;
635  bool hasOuterJoins;
636  bool hasResultRTEs;
637  RelOptInfo *final_rel;
638  ListCell *l;
639 
640  /* Create a PlannerInfo data structure for this subquery */
642  root->parse = parse;
643  root->glob = glob;
644  root->query_level = parent_root ? parent_root->query_level + 1 : 1;
645  root->parent_root = parent_root;
646  root->plan_params = NIL;
647  root->outer_params = NULL;
648  root->planner_cxt = CurrentMemoryContext;
649  root->init_plans = NIL;
650  root->cte_plan_ids = NIL;
651  root->multiexpr_params = NIL;
652  root->join_domains = NIL;
653  root->eq_classes = NIL;
654  root->ec_merging_done = false;
655  root->last_rinfo_serial = 0;
656  root->all_result_relids =
657  parse->resultRelation ? bms_make_singleton(parse->resultRelation) : NULL;
658  root->leaf_result_relids = NULL; /* we'll find out leaf-ness later */
659  root->append_rel_list = NIL;
660  root->row_identity_vars = NIL;
661  root->rowMarks = NIL;
662  memset(root->upper_rels, 0, sizeof(root->upper_rels));
663  memset(root->upper_targets, 0, sizeof(root->upper_targets));
664  root->processed_groupClause = NIL;
665  root->processed_distinctClause = NIL;
666  root->processed_tlist = NIL;
667  root->update_colnos = NIL;
668  root->grouping_map = NULL;
669  root->minmax_aggs = NIL;
670  root->qual_security_level = 0;
671  root->hasPseudoConstantQuals = false;
672  root->hasAlternativeSubPlans = false;
673  root->placeholdersFrozen = false;
674  root->hasRecursion = hasRecursion;
675  if (hasRecursion)
676  root->wt_param_id = assign_special_exec_param(root);
677  else
678  root->wt_param_id = -1;
679  root->non_recursive_path = NULL;
680  root->partColsUpdated = false;
681 
682  /*
683  * Create the top-level join domain. This won't have valid contents until
684  * deconstruct_jointree fills it in, but the node needs to exist before
685  * that so we can build EquivalenceClasses referencing it.
686  */
687  root->join_domains = list_make1(makeNode(JoinDomain));
688 
689  /*
690  * If there is a WITH list, process each WITH query and either convert it
691  * to RTE_SUBQUERY RTE(s) or build an initplan SubPlan structure for it.
692  */
693  if (parse->cteList)
695 
696  /*
697  * If it's a MERGE command, transform the joinlist as appropriate.
698  */
700 
701  /*
702  * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
703  * that we don't need so many special cases to deal with that situation.
704  */
706 
707  /*
708  * Look for ANY and EXISTS SubLinks in WHERE and JOIN/ON clauses, and try
709  * to transform them into joins. Note that this step does not descend
710  * into subqueries; if we pull up any subqueries below, their SubLinks are
711  * processed just before pulling them up.
712  */
713  if (parse->hasSubLinks)
715 
716  /*
717  * Scan the rangetable for function RTEs, do const-simplification on them,
718  * and then inline them if possible (producing subqueries that might get
719  * pulled up next). Recursion issues here are handled in the same way as
720  * for SubLinks.
721  */
723 
724  /*
725  * Check to see if any subqueries in the jointree can be merged into this
726  * query.
727  */
729 
730  /*
731  * If this is a simple UNION ALL query, flatten it into an appendrel. We
732  * do this now because it requires applying pull_up_subqueries to the leaf
733  * queries of the UNION ALL, which weren't touched above because they
734  * weren't referenced by the jointree (they will be after we do this).
735  */
736  if (parse->setOperations)
738 
739  /*
740  * Survey the rangetable to see what kinds of entries are present. We can
741  * skip some later processing if relevant SQL features are not used; for
742  * example if there are no JOIN RTEs we can avoid the expense of doing
743  * flatten_join_alias_vars(). This must be done after we have finished
744  * adding rangetable entries, of course. (Note: actually, processing of
745  * inherited or partitioned rels can cause RTEs for their child tables to
746  * get added later; but those must all be RTE_RELATION entries, so they
747  * don't invalidate the conclusions drawn here.)
748  */
749  root->hasJoinRTEs = false;
750  root->hasLateralRTEs = false;
751  hasOuterJoins = false;
752  hasResultRTEs = false;
753  foreach(l, parse->rtable)
754  {
756 
757  switch (rte->rtekind)
758  {
759  case RTE_RELATION:
760  if (rte->inh)
761  {
762  /*
763  * Check to see if the relation actually has any children;
764  * if not, clear the inh flag so we can treat it as a
765  * plain base relation.
766  *
767  * Note: this could give a false-positive result, if the
768  * rel once had children but no longer does. We used to
769  * be able to clear rte->inh later on when we discovered
770  * that, but no more; we have to handle such cases as
771  * full-fledged inheritance.
772  */
773  rte->inh = has_subclass(rte->relid);
774  }
775  break;
776  case RTE_JOIN:
777  root->hasJoinRTEs = true;
778  if (IS_OUTER_JOIN(rte->jointype))
779  hasOuterJoins = true;
780  break;
781  case RTE_RESULT:
782  hasResultRTEs = true;
783  break;
784  default:
785  /* No work here for other RTE types */
786  break;
787  }
788 
789  if (rte->lateral)
790  root->hasLateralRTEs = true;
791 
792  /*
793  * We can also determine the maximum security level required for any
794  * securityQuals now. Addition of inheritance-child RTEs won't affect
795  * this, because child tables don't have their own securityQuals; see
796  * expand_single_inheritance_child().
797  */
798  if (rte->securityQuals)
799  root->qual_security_level = Max(root->qual_security_level,
800  list_length(rte->securityQuals));
801  }
802 
803  /*
804  * If we have now verified that the query target relation is
805  * non-inheriting, mark it as a leaf target.
806  */
807  if (parse->resultRelation)
808  {
809  RangeTblEntry *rte = rt_fetch(parse->resultRelation, parse->rtable);
810 
811  if (!rte->inh)
812  root->leaf_result_relids =
813  bms_make_singleton(parse->resultRelation);
814  }
815 
816  /*
817  * Preprocess RowMark information. We need to do this after subquery
818  * pullup, so that all base relations are present.
819  */
821 
822  /*
823  * Set hasHavingQual to remember if HAVING clause is present. Needed
824  * because preprocess_expression will reduce a constant-true condition to
825  * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
826  */
827  root->hasHavingQual = (parse->havingQual != NULL);
828 
829  /*
830  * Do expression preprocessing on targetlist and quals, as well as other
831  * random expressions in the querytree. Note that we do not need to
832  * handle sort/group expressions explicitly, because they are actually
833  * part of the targetlist.
834  */
835  parse->targetList = (List *)
836  preprocess_expression(root, (Node *) parse->targetList,
838 
839  /* Constant-folding might have removed all set-returning functions */
840  if (parse->hasTargetSRFs)
841  parse->hasTargetSRFs = expression_returns_set((Node *) parse->targetList);
842 
843  newWithCheckOptions = NIL;
844  foreach(l, parse->withCheckOptions)
845  {
847 
848  wco->qual = preprocess_expression(root, wco->qual,
849  EXPRKIND_QUAL);
850  if (wco->qual != NULL)
851  newWithCheckOptions = lappend(newWithCheckOptions, wco);
852  }
853  parse->withCheckOptions = newWithCheckOptions;
854 
855  parse->returningList = (List *)
856  preprocess_expression(root, (Node *) parse->returningList,
858 
859  preprocess_qual_conditions(root, (Node *) parse->jointree);
860 
861  parse->havingQual = preprocess_expression(root, parse->havingQual,
862  EXPRKIND_QUAL);
863 
864  foreach(l, parse->windowClause)
865  {
867 
868  /* partitionClause/orderClause are sort/group expressions */
873  }
874 
875  parse->limitOffset = preprocess_expression(root, parse->limitOffset,
877  parse->limitCount = preprocess_expression(root, parse->limitCount,
879 
880  if (parse->onConflict)
881  {
882  parse->onConflict->arbiterElems = (List *)
884  (Node *) parse->onConflict->arbiterElems,
886  parse->onConflict->arbiterWhere =
888  parse->onConflict->arbiterWhere,
889  EXPRKIND_QUAL);
890  parse->onConflict->onConflictSet = (List *)
892  (Node *) parse->onConflict->onConflictSet,
894  parse->onConflict->onConflictWhere =
896  parse->onConflict->onConflictWhere,
897  EXPRKIND_QUAL);
898  /* exclRelTlist contains only Vars, so no preprocessing needed */
899  }
900 
901  foreach(l, parse->mergeActionList)
902  {
904 
905  action->targetList = (List *)
907  (Node *) action->targetList,
909  action->qual =
911  (Node *) action->qual,
912  EXPRKIND_QUAL);
913  }
914 
915  parse->mergeJoinCondition =
916  preprocess_expression(root, parse->mergeJoinCondition, EXPRKIND_QUAL);
917 
918  root->append_rel_list = (List *)
919  preprocess_expression(root, (Node *) root->append_rel_list,
921 
922  /* Also need to preprocess expressions within RTEs */
923  foreach(l, parse->rtable)
924  {
926  int kind;
927  ListCell *lcsq;
928 
929  if (rte->rtekind == RTE_RELATION)
930  {
931  if (rte->tablesample)
932  rte->tablesample = (TableSampleClause *)
934  (Node *) rte->tablesample,
936  }
937  else if (rte->rtekind == RTE_SUBQUERY)
938  {
939  /*
940  * We don't want to do all preprocessing yet on the subquery's
941  * expressions, since that will happen when we plan it. But if it
942  * contains any join aliases of our level, those have to get
943  * expanded now, because planning of the subquery won't do it.
944  * That's only possible if the subquery is LATERAL.
945  */
946  if (rte->lateral && root->hasJoinRTEs)
947  rte->subquery = (Query *)
949  (Node *) rte->subquery);
950  }
951  else if (rte->rtekind == RTE_FUNCTION)
952  {
953  /* Preprocess the function expression(s) fully */
954  kind = rte->lateral ? EXPRKIND_RTFUNC_LATERAL : EXPRKIND_RTFUNC;
955  rte->functions = (List *)
956  preprocess_expression(root, (Node *) rte->functions, kind);
957  }
958  else if (rte->rtekind == RTE_TABLEFUNC)
959  {
960  /* Preprocess the function expression(s) fully */
961  kind = rte->lateral ? EXPRKIND_TABLEFUNC_LATERAL : EXPRKIND_TABLEFUNC;
962  rte->tablefunc = (TableFunc *)
963  preprocess_expression(root, (Node *) rte->tablefunc, kind);
964  }
965  else if (rte->rtekind == RTE_VALUES)
966  {
967  /* Preprocess the values lists fully */
968  kind = rte->lateral ? EXPRKIND_VALUES_LATERAL : EXPRKIND_VALUES;
969  rte->values_lists = (List *)
970  preprocess_expression(root, (Node *) rte->values_lists, kind);
971  }
972 
973  /*
974  * Process each element of the securityQuals list as if it were a
975  * separate qual expression (as indeed it is). We need to do it this
976  * way to get proper canonicalization of AND/OR structure. Note that
977  * this converts each element into an implicit-AND sublist.
978  */
979  foreach(lcsq, rte->securityQuals)
980  {
982  (Node *) lfirst(lcsq),
983  EXPRKIND_QUAL);
984  }
985  }
986 
987  /*
988  * Now that we are done preprocessing expressions, and in particular done
989  * flattening join alias variables, get rid of the joinaliasvars lists.
990  * They no longer match what expressions in the rest of the tree look
991  * like, because we have not preprocessed expressions in those lists (and
992  * do not want to; for example, expanding a SubLink there would result in
993  * a useless unreferenced subplan). Leaving them in place simply creates
994  * a hazard for later scans of the tree. We could try to prevent that by
995  * using QTW_IGNORE_JOINALIASES in every tree scan done after this point,
996  * but that doesn't sound very reliable.
997  */
998  if (root->hasJoinRTEs)
999  {
1000  foreach(l, parse->rtable)
1001  {
1003 
1004  rte->joinaliasvars = NIL;
1005  }
1006  }
1007 
1008  /*
1009  * In some cases we may want to transfer a HAVING clause into WHERE. We
1010  * cannot do so if the HAVING clause contains aggregates (obviously) or
1011  * volatile functions (since a HAVING clause is supposed to be executed
1012  * only once per group). We also can't do this if there are any nonempty
1013  * grouping sets; moving such a clause into WHERE would potentially change
1014  * the results, if any referenced column isn't present in all the grouping
1015  * sets. (If there are only empty grouping sets, then the HAVING clause
1016  * must be degenerate as discussed below.)
1017  *
1018  * Also, it may be that the clause is so expensive to execute that we're
1019  * better off doing it only once per group, despite the loss of
1020  * selectivity. This is hard to estimate short of doing the entire
1021  * planning process twice, so we use a heuristic: clauses containing
1022  * subplans are left in HAVING. Otherwise, we move or copy the HAVING
1023  * clause into WHERE, in hopes of eliminating tuples before aggregation
1024  * instead of after.
1025  *
1026  * If the query has explicit grouping then we can simply move such a
1027  * clause into WHERE; any group that fails the clause will not be in the
1028  * output because none of its tuples will reach the grouping or
1029  * aggregation stage. Otherwise we must have a degenerate (variable-free)
1030  * HAVING clause, which we put in WHERE so that query_planner() can use it
1031  * in a gating Result node, but also keep in HAVING to ensure that we
1032  * don't emit a bogus aggregated row. (This could be done better, but it
1033  * seems not worth optimizing.)
1034  *
1035  * Note that both havingQual and parse->jointree->quals are in
1036  * implicitly-ANDed-list form at this point, even though they are declared
1037  * as Node *.
1038  */
1039  newHaving = NIL;
1040  foreach(l, (List *) parse->havingQual)
1041  {
1042  Node *havingclause = (Node *) lfirst(l);
1043 
1044  if ((parse->groupClause && parse->groupingSets) ||
1045  contain_agg_clause(havingclause) ||
1046  contain_volatile_functions(havingclause) ||
1047  contain_subplans(havingclause))
1048  {
1049  /* keep it in HAVING */
1050  newHaving = lappend(newHaving, havingclause);
1051  }
1052  else if (parse->groupClause && !parse->groupingSets)
1053  {
1054  /* move it to WHERE */
1055  parse->jointree->quals = (Node *)
1056  lappend((List *) parse->jointree->quals, havingclause);
1057  }
1058  else
1059  {
1060  /* put a copy in WHERE, keep it in HAVING */
1061  parse->jointree->quals = (Node *)
1062  lappend((List *) parse->jointree->quals,
1063  copyObject(havingclause));
1064  newHaving = lappend(newHaving, havingclause);
1065  }
1066  }
1067  parse->havingQual = (Node *) newHaving;
1068 
1069  /*
1070  * If we have any outer joins, try to reduce them to plain inner joins.
1071  * This step is most easily done after we've done expression
1072  * preprocessing.
1073  */
1074  if (hasOuterJoins)
1076 
1077  /*
1078  * If we have any RTE_RESULT relations, see if they can be deleted from
1079  * the jointree. We also rely on this processing to flatten single-child
1080  * FromExprs underneath outer joins. This step is most effectively done
1081  * after we've done expression preprocessing and outer join reduction.
1082  */
1083  if (hasResultRTEs || hasOuterJoins)
1085 
1086  /*
1087  * Do the main planning.
1088  */
1089  grouping_planner(root, tuple_fraction, setops);
1090 
1091  /*
1092  * Capture the set of outer-level param IDs we have access to, for use in
1093  * extParam/allParam calculations later.
1094  */
1096 
1097  /*
1098  * If any initPlans were created in this query level, adjust the surviving
1099  * Paths' costs and parallel-safety flags to account for them. The
1100  * initPlans won't actually get attached to the plan tree till
1101  * create_plan() runs, but we must include their effects now.
1102  */
1103  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
1104  SS_charge_for_initplans(root, final_rel);
1105 
1106  /*
1107  * Make sure we've identified the cheapest Path for the final rel. (By
1108  * doing this here not in grouping_planner, we include initPlan costs in
1109  * the decision, though it's unlikely that will change anything.)
1110  */
1111  set_cheapest(final_rel);
1112 
1113  return root;
1114 }
1115 
1116 /*
1117  * preprocess_expression
1118  * Do subquery_planner's preprocessing work for an expression,
1119  * which can be a targetlist, a WHERE clause (including JOIN/ON
1120  * conditions), a HAVING clause, or a few other things.
1121  */
1122 static Node *
1124 {
1125  /*
1126  * Fall out quickly if expression is empty. This occurs often enough to
1127  * be worth checking. Note that null->null is the correct conversion for
1128  * implicit-AND result format, too.
1129  */
1130  if (expr == NULL)
1131  return NULL;
1132 
1133  /*
1134  * If the query has any join RTEs, replace join alias variables with
1135  * base-relation variables. We must do this first, since any expressions
1136  * we may extract from the joinaliasvars lists have not been preprocessed.
1137  * For example, if we did this after sublink processing, sublinks expanded
1138  * out from join aliases would not get processed. But we can skip this in
1139  * non-lateral RTE functions, VALUES lists, and TABLESAMPLE clauses, since
1140  * they can't contain any Vars of the current query level.
1141  */
1142  if (root->hasJoinRTEs &&
1143  !(kind == EXPRKIND_RTFUNC ||
1144  kind == EXPRKIND_VALUES ||
1145  kind == EXPRKIND_TABLESAMPLE ||
1146  kind == EXPRKIND_TABLEFUNC))
1147  expr = flatten_join_alias_vars(root, root->parse, expr);
1148 
1149  /*
1150  * Simplify constant expressions. For function RTEs, this was already
1151  * done by preprocess_function_rtes. (But note we must do it again for
1152  * EXPRKIND_RTFUNC_LATERAL, because those might by now contain
1153  * un-simplified subexpressions inserted by flattening of subqueries or
1154  * join alias variables.)
1155  *
1156  * Note: an essential effect of this is to convert named-argument function
1157  * calls to positional notation and insert the current actual values of
1158  * any default arguments for functions. To ensure that happens, we *must*
1159  * process all expressions here. Previous PG versions sometimes skipped
1160  * const-simplification if it didn't seem worth the trouble, but we can't
1161  * do that anymore.
1162  *
1163  * Note: this also flattens nested AND and OR expressions into N-argument
1164  * form. All processing of a qual expression after this point must be
1165  * careful to maintain AND/OR flatness --- that is, do not generate a tree
1166  * with AND directly under AND, nor OR directly under OR.
1167  */
1168  if (kind != EXPRKIND_RTFUNC)
1169  expr = eval_const_expressions(root, expr);
1170 
1171  /*
1172  * If it's a qual or havingQual, canonicalize it.
1173  */
1174  if (kind == EXPRKIND_QUAL)
1175  {
1176  expr = (Node *) canonicalize_qual((Expr *) expr, false);
1177 
1178 #ifdef OPTIMIZER_DEBUG
1179  printf("After canonicalize_qual()\n");
1180  pprint(expr);
1181 #endif
1182  }
1183 
1184  /*
1185  * Check for ANY ScalarArrayOpExpr with Const arrays and set the
1186  * hashfuncid of any that might execute more quickly by using hash lookups
1187  * instead of a linear search.
1188  */
1189  if (kind == EXPRKIND_QUAL || kind == EXPRKIND_TARGET)
1190  {
1192  }
1193 
1194  /* Expand SubLinks to SubPlans */
1195  if (root->parse->hasSubLinks)
1196  expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
1197 
1198  /*
1199  * XXX do not insert anything here unless you have grokked the comments in
1200  * SS_replace_correlation_vars ...
1201  */
1202 
1203  /* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
1204  if (root->query_level > 1)
1205  expr = SS_replace_correlation_vars(root, expr);
1206 
1207  /*
1208  * If it's a qual or havingQual, convert it to implicit-AND format. (We
1209  * don't want to do this before eval_const_expressions, since the latter
1210  * would be unable to simplify a top-level AND correctly. Also,
1211  * SS_process_sublinks expects explicit-AND format.)
1212  */
1213  if (kind == EXPRKIND_QUAL)
1214  expr = (Node *) make_ands_implicit((Expr *) expr);
1215 
1216  return expr;
1217 }
1218 
1219 /*
1220  * preprocess_qual_conditions
1221  * Recursively scan the query's jointree and do subquery_planner's
1222  * preprocessing work on each qual condition found therein.
1223  */
1224 static void
1226 {
1227  if (jtnode == NULL)
1228  return;
1229  if (IsA(jtnode, RangeTblRef))
1230  {
1231  /* nothing to do here */
1232  }
1233  else if (IsA(jtnode, FromExpr))
1234  {
1235  FromExpr *f = (FromExpr *) jtnode;
1236  ListCell *l;
1237 
1238  foreach(l, f->fromlist)
1240 
1242  }
1243  else if (IsA(jtnode, JoinExpr))
1244  {
1245  JoinExpr *j = (JoinExpr *) jtnode;
1246 
1249 
1250  j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
1251  }
1252  else
1253  elog(ERROR, "unrecognized node type: %d",
1254  (int) nodeTag(jtnode));
1255 }
1256 
1257 /*
1258  * preprocess_phv_expression
1259  * Do preprocessing on a PlaceHolderVar expression that's been pulled up.
1260  *
1261  * If a LATERAL subquery references an output of another subquery, and that
1262  * output must be wrapped in a PlaceHolderVar because of an intermediate outer
1263  * join, then we'll push the PlaceHolderVar expression down into the subquery
1264  * and later pull it back up during find_lateral_references, which runs after
1265  * subquery_planner has preprocessed all the expressions that were in the
1266  * current query level to start with. So we need to preprocess it then.
1267  */
1268 Expr *
1270 {
1271  return (Expr *) preprocess_expression(root, (Node *) expr, EXPRKIND_PHV);
1272 }
1273 
1274 /*--------------------
1275  * grouping_planner
1276  * Perform planning steps related to grouping, aggregation, etc.
1277  *
1278  * This function adds all required top-level processing to the scan/join
1279  * Path(s) produced by query_planner.
1280  *
1281  * tuple_fraction is the fraction of tuples we expect will be retrieved.
1282  * tuple_fraction is interpreted as follows:
1283  * 0: expect all tuples to be retrieved (normal case)
1284  * 0 < tuple_fraction < 1: expect the given fraction of tuples available
1285  * from the plan to be retrieved
1286  * tuple_fraction >= 1: tuple_fraction is the absolute number of tuples
1287  * expected to be retrieved (ie, a LIMIT specification).
1288  * setops is used for set operation subqueries to provide the subquery with
1289  * the context in which it's being used so that Paths correctly sorted for the
1290  * set operation can be generated. NULL when not planning a set operation
1291  * child.
1292  *
1293  * Returns nothing; the useful output is in the Paths we attach to the
1294  * (UPPERREL_FINAL, NULL) upperrel in *root. In addition,
1295  * root->processed_tlist contains the final processed targetlist.
1296  *
1297  * Note that we have not done set_cheapest() on the final rel; it's convenient
1298  * to leave this to the caller.
1299  *--------------------
1300  */
1301 static void
1302 grouping_planner(PlannerInfo *root, double tuple_fraction,
1303  SetOperationStmt *setops)
1304 {
1305  Query *parse = root->parse;
1306  int64 offset_est = 0;
1307  int64 count_est = 0;
1308  double limit_tuples = -1.0;
1309  bool have_postponed_srfs = false;
1310  PathTarget *final_target;
1311  List *final_targets;
1312  List *final_targets_contain_srfs;
1313  bool final_target_parallel_safe;
1314  RelOptInfo *current_rel;
1315  RelOptInfo *final_rel;
1316  FinalPathExtraData extra;
1317  ListCell *lc;
1318 
1319  /* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
1320  if (parse->limitCount || parse->limitOffset)
1321  {
1322  tuple_fraction = preprocess_limit(root, tuple_fraction,
1323  &offset_est, &count_est);
1324 
1325  /*
1326  * If we have a known LIMIT, and don't have an unknown OFFSET, we can
1327  * estimate the effects of using a bounded sort.
1328  */
1329  if (count_est > 0 && offset_est >= 0)
1330  limit_tuples = (double) count_est + (double) offset_est;
1331  }
1332 
1333  /* Make tuple_fraction accessible to lower-level routines */
1334  root->tuple_fraction = tuple_fraction;
1335 
1336  if (parse->setOperations)
1337  {
1338  /*
1339  * Construct Paths for set operations. The results will not need any
1340  * work except perhaps a top-level sort and/or LIMIT. Note that any
1341  * special work for recursive unions is the responsibility of
1342  * plan_set_operations.
1343  */
1344  current_rel = plan_set_operations(root);
1345 
1346  /*
1347  * We should not need to call preprocess_targetlist, since we must be
1348  * in a SELECT query node. Instead, use the processed_tlist returned
1349  * by plan_set_operations (since this tells whether it returned any
1350  * resjunk columns!), and transfer any sort key information from the
1351  * original tlist.
1352  */
1353  Assert(parse->commandType == CMD_SELECT);
1354 
1355  /* for safety, copy processed_tlist instead of modifying in-place */
1356  root->processed_tlist =
1357  postprocess_setop_tlist(copyObject(root->processed_tlist),
1358  parse->targetList);
1359 
1360  /* Also extract the PathTarget form of the setop result tlist */
1361  final_target = current_rel->cheapest_total_path->pathtarget;
1362 
1363  /* And check whether it's parallel safe */
1364  final_target_parallel_safe =
1365  is_parallel_safe(root, (Node *) final_target->exprs);
1366 
1367  /* The setop result tlist couldn't contain any SRFs */
1368  Assert(!parse->hasTargetSRFs);
1369  final_targets = final_targets_contain_srfs = NIL;
1370 
1371  /*
1372  * Can't handle FOR [KEY] UPDATE/SHARE here (parser should have
1373  * checked already, but let's make sure).
1374  */
1375  if (parse->rowMarks)
1376  ereport(ERROR,
1377  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1378  /*------
1379  translator: %s is a SQL row locking clause such as FOR UPDATE */
1380  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1382  parse->rowMarks)->strength))));
1383 
1384  /*
1385  * Calculate pathkeys that represent result ordering requirements
1386  */
1387  Assert(parse->distinctClause == NIL);
1388  root->sort_pathkeys = make_pathkeys_for_sortclauses(root,
1389  parse->sortClause,
1390  root->processed_tlist);
1391  }
1392  else
1393  {
1394  /* No set operations, do regular planning */
1395  PathTarget *sort_input_target;
1396  List *sort_input_targets;
1397  List *sort_input_targets_contain_srfs;
1398  bool sort_input_target_parallel_safe;
1399  PathTarget *grouping_target;
1400  List *grouping_targets;
1401  List *grouping_targets_contain_srfs;
1402  bool grouping_target_parallel_safe;
1403  PathTarget *scanjoin_target;
1404  List *scanjoin_targets;
1405  List *scanjoin_targets_contain_srfs;
1406  bool scanjoin_target_parallel_safe;
1407  bool scanjoin_target_same_exprs;
1408  bool have_grouping;
1409  WindowFuncLists *wflists = NULL;
1410  List *activeWindows = NIL;
1411  grouping_sets_data *gset_data = NULL;
1412  standard_qp_extra qp_extra;
1413 
1414  /* A recursive query should always have setOperations */
1415  Assert(!root->hasRecursion);
1416 
1417  /* Preprocess grouping sets and GROUP BY clause, if any */
1418  if (parse->groupingSets)
1419  {
1420  gset_data = preprocess_grouping_sets(root);
1421  }
1422  else if (parse->groupClause)
1423  {
1424  /* Preprocess regular GROUP BY clause, if any */
1425  root->processed_groupClause = preprocess_groupclause(root, NIL);
1426  /* Remove any redundant GROUP BY columns */
1428  }
1429 
1430  /*
1431  * Preprocess targetlist. Note that much of the remaining planning
1432  * work will be done with the PathTarget representation of tlists, but
1433  * we must also maintain the full representation of the final tlist so
1434  * that we can transfer its decoration (resnames etc) to the topmost
1435  * tlist of the finished Plan. This is kept in processed_tlist.
1436  */
1438 
1439  /*
1440  * Mark all the aggregates with resolved aggtranstypes, and detect
1441  * aggregates that are duplicates or can share transition state. We
1442  * must do this before slicing and dicing the tlist into various
1443  * pathtargets, else some copies of the Aggref nodes might escape
1444  * being marked.
1445  */
1446  if (parse->hasAggs)
1447  {
1448  preprocess_aggrefs(root, (Node *) root->processed_tlist);
1449  preprocess_aggrefs(root, (Node *) parse->havingQual);
1450  }
1451 
1452  /*
1453  * Locate any window functions in the tlist. (We don't need to look
1454  * anywhere else, since expressions used in ORDER BY will be in there
1455  * too.) Note that they could all have been eliminated by constant
1456  * folding, in which case we don't need to do any more work.
1457  */
1458  if (parse->hasWindowFuncs)
1459  {
1460  wflists = find_window_functions((Node *) root->processed_tlist,
1461  list_length(parse->windowClause));
1462  if (wflists->numWindowFuncs > 0)
1463  {
1464  /*
1465  * See if any modifications can be made to each WindowClause
1466  * to allow the executor to execute the WindowFuncs more
1467  * quickly.
1468  */
1469  optimize_window_clauses(root, wflists);
1470 
1471  activeWindows = select_active_windows(root, wflists);
1472  }
1473  else
1474  parse->hasWindowFuncs = false;
1475  }
1476 
1477  /*
1478  * Preprocess MIN/MAX aggregates, if any. Note: be careful about
1479  * adding logic between here and the query_planner() call. Anything
1480  * that is needed in MIN/MAX-optimizable cases will have to be
1481  * duplicated in planagg.c.
1482  */
1483  if (parse->hasAggs)
1485 
1486  /*
1487  * Figure out whether there's a hard limit on the number of rows that
1488  * query_planner's result subplan needs to return. Even if we know a
1489  * hard limit overall, it doesn't apply if the query has any
1490  * grouping/aggregation operations, or SRFs in the tlist.
1491  */
1492  if (parse->groupClause ||
1493  parse->groupingSets ||
1494  parse->distinctClause ||
1495  parse->hasAggs ||
1496  parse->hasWindowFuncs ||
1497  parse->hasTargetSRFs ||
1498  root->hasHavingQual)
1499  root->limit_tuples = -1.0;
1500  else
1501  root->limit_tuples = limit_tuples;
1502 
1503  /* Set up data needed by standard_qp_callback */
1504  qp_extra.activeWindows = activeWindows;
1505  qp_extra.gset_data = gset_data;
1506 
1507  /*
1508  * If we're a subquery for a set operation, store the SetOperationStmt
1509  * in qp_extra.
1510  */
1511  qp_extra.setop = setops;
1512 
1513  /*
1514  * Generate the best unsorted and presorted paths for the scan/join
1515  * portion of this Query, ie the processing represented by the
1516  * FROM/WHERE clauses. (Note there may not be any presorted paths.)
1517  * We also generate (in standard_qp_callback) pathkey representations
1518  * of the query's sort clause, distinct clause, etc.
1519  */
1520  current_rel = query_planner(root, standard_qp_callback, &qp_extra);
1521 
1522  /*
1523  * Convert the query's result tlist into PathTarget format.
1524  *
1525  * Note: this cannot be done before query_planner() has performed
1526  * appendrel expansion, because that might add resjunk entries to
1527  * root->processed_tlist. Waiting till afterwards is also helpful
1528  * because the target width estimates can use per-Var width numbers
1529  * that were obtained within query_planner().
1530  */
1531  final_target = create_pathtarget(root, root->processed_tlist);
1532  final_target_parallel_safe =
1533  is_parallel_safe(root, (Node *) final_target->exprs);
1534 
1535  /*
1536  * If ORDER BY was given, consider whether we should use a post-sort
1537  * projection, and compute the adjusted target for preceding steps if
1538  * so.
1539  */
1540  if (parse->sortClause)
1541  {
1542  sort_input_target = make_sort_input_target(root,
1543  final_target,
1544  &have_postponed_srfs);
1545  sort_input_target_parallel_safe =
1546  is_parallel_safe(root, (Node *) sort_input_target->exprs);
1547  }
1548  else
1549  {
1550  sort_input_target = final_target;
1551  sort_input_target_parallel_safe = final_target_parallel_safe;
1552  }
1553 
1554  /*
1555  * If we have window functions to deal with, the output from any
1556  * grouping step needs to be what the window functions want;
1557  * otherwise, it should be sort_input_target.
1558  */
1559  if (activeWindows)
1560  {
1561  grouping_target = make_window_input_target(root,
1562  final_target,
1563  activeWindows);
1564  grouping_target_parallel_safe =
1565  is_parallel_safe(root, (Node *) grouping_target->exprs);
1566  }
1567  else
1568  {
1569  grouping_target = sort_input_target;
1570  grouping_target_parallel_safe = sort_input_target_parallel_safe;
1571  }
1572 
1573  /*
1574  * If we have grouping or aggregation to do, the topmost scan/join
1575  * plan node must emit what the grouping step wants; otherwise, it
1576  * should emit grouping_target.
1577  */
1578  have_grouping = (parse->groupClause || parse->groupingSets ||
1579  parse->hasAggs || root->hasHavingQual);
1580  if (have_grouping)
1581  {
1582  scanjoin_target = make_group_input_target(root, final_target);
1583  scanjoin_target_parallel_safe =
1584  is_parallel_safe(root, (Node *) scanjoin_target->exprs);
1585  }
1586  else
1587  {
1588  scanjoin_target = grouping_target;
1589  scanjoin_target_parallel_safe = grouping_target_parallel_safe;
1590  }
1591 
1592  /*
1593  * If there are any SRFs in the targetlist, we must separate each of
1594  * these PathTargets into SRF-computing and SRF-free targets. Replace
1595  * each of the named targets with a SRF-free version, and remember the
1596  * list of additional projection steps we need to add afterwards.
1597  */
1598  if (parse->hasTargetSRFs)
1599  {
1600  /* final_target doesn't recompute any SRFs in sort_input_target */
1601  split_pathtarget_at_srfs(root, final_target, sort_input_target,
1602  &final_targets,
1603  &final_targets_contain_srfs);
1604  final_target = linitial_node(PathTarget, final_targets);
1605  Assert(!linitial_int(final_targets_contain_srfs));
1606  /* likewise for sort_input_target vs. grouping_target */
1607  split_pathtarget_at_srfs(root, sort_input_target, grouping_target,
1608  &sort_input_targets,
1609  &sort_input_targets_contain_srfs);
1610  sort_input_target = linitial_node(PathTarget, sort_input_targets);
1611  Assert(!linitial_int(sort_input_targets_contain_srfs));
1612  /* likewise for grouping_target vs. scanjoin_target */
1613  split_pathtarget_at_srfs(root, grouping_target, scanjoin_target,
1614  &grouping_targets,
1615  &grouping_targets_contain_srfs);
1616  grouping_target = linitial_node(PathTarget, grouping_targets);
1617  Assert(!linitial_int(grouping_targets_contain_srfs));
1618  /* scanjoin_target will not have any SRFs precomputed for it */
1619  split_pathtarget_at_srfs(root, scanjoin_target, NULL,
1620  &scanjoin_targets,
1621  &scanjoin_targets_contain_srfs);
1622  scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
1623  Assert(!linitial_int(scanjoin_targets_contain_srfs));
1624  }
1625  else
1626  {
1627  /* initialize lists; for most of these, dummy values are OK */
1628  final_targets = final_targets_contain_srfs = NIL;
1629  sort_input_targets = sort_input_targets_contain_srfs = NIL;
1630  grouping_targets = grouping_targets_contain_srfs = NIL;
1631  scanjoin_targets = list_make1(scanjoin_target);
1632  scanjoin_targets_contain_srfs = NIL;
1633  }
1634 
1635  /* Apply scan/join target. */
1636  scanjoin_target_same_exprs = list_length(scanjoin_targets) == 1
1637  && equal(scanjoin_target->exprs, current_rel->reltarget->exprs);
1638  apply_scanjoin_target_to_paths(root, current_rel, scanjoin_targets,
1639  scanjoin_targets_contain_srfs,
1640  scanjoin_target_parallel_safe,
1641  scanjoin_target_same_exprs);
1642 
1643  /*
1644  * Save the various upper-rel PathTargets we just computed into
1645  * root->upper_targets[]. The core code doesn't use this, but it
1646  * provides a convenient place for extensions to get at the info. For
1647  * consistency, we save all the intermediate targets, even though some
1648  * of the corresponding upperrels might not be needed for this query.
1649  */
1650  root->upper_targets[UPPERREL_FINAL] = final_target;
1651  root->upper_targets[UPPERREL_ORDERED] = final_target;
1652  root->upper_targets[UPPERREL_DISTINCT] = sort_input_target;
1653  root->upper_targets[UPPERREL_PARTIAL_DISTINCT] = sort_input_target;
1654  root->upper_targets[UPPERREL_WINDOW] = sort_input_target;
1655  root->upper_targets[UPPERREL_GROUP_AGG] = grouping_target;
1656 
1657  /*
1658  * If we have grouping and/or aggregation, consider ways to implement
1659  * that. We build a new upperrel representing the output of this
1660  * phase.
1661  */
1662  if (have_grouping)
1663  {
1664  current_rel = create_grouping_paths(root,
1665  current_rel,
1666  grouping_target,
1667  grouping_target_parallel_safe,
1668  gset_data);
1669  /* Fix things up if grouping_target contains SRFs */
1670  if (parse->hasTargetSRFs)
1671  adjust_paths_for_srfs(root, current_rel,
1672  grouping_targets,
1673  grouping_targets_contain_srfs);
1674  }
1675 
1676  /*
1677  * If we have window functions, consider ways to implement those. We
1678  * build a new upperrel representing the output of this phase.
1679  */
1680  if (activeWindows)
1681  {
1682  current_rel = create_window_paths(root,
1683  current_rel,
1684  grouping_target,
1685  sort_input_target,
1686  sort_input_target_parallel_safe,
1687  wflists,
1688  activeWindows);
1689  /* Fix things up if sort_input_target contains SRFs */
1690  if (parse->hasTargetSRFs)
1691  adjust_paths_for_srfs(root, current_rel,
1692  sort_input_targets,
1693  sort_input_targets_contain_srfs);
1694  }
1695 
1696  /*
1697  * If there is a DISTINCT clause, consider ways to implement that. We
1698  * build a new upperrel representing the output of this phase.
1699  */
1700  if (parse->distinctClause)
1701  {
1702  current_rel = create_distinct_paths(root,
1703  current_rel,
1704  sort_input_target);
1705  }
1706  } /* end of if (setOperations) */
1707 
1708  /*
1709  * If ORDER BY was given, consider ways to implement that, and generate a
1710  * new upperrel containing only paths that emit the correct ordering and
1711  * project the correct final_target. We can apply the original
1712  * limit_tuples limit in sort costing here, but only if there are no
1713  * postponed SRFs.
1714  */
1715  if (parse->sortClause)
1716  {
1717  current_rel = create_ordered_paths(root,
1718  current_rel,
1719  final_target,
1720  final_target_parallel_safe,
1721  have_postponed_srfs ? -1.0 :
1722  limit_tuples);
1723  /* Fix things up if final_target contains SRFs */
1724  if (parse->hasTargetSRFs)
1725  adjust_paths_for_srfs(root, current_rel,
1726  final_targets,
1727  final_targets_contain_srfs);
1728  }
1729 
1730  /*
1731  * Now we are prepared to build the final-output upperrel.
1732  */
1733  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
1734 
1735  /*
1736  * If the input rel is marked consider_parallel and there's nothing that's
1737  * not parallel-safe in the LIMIT clause, then the final_rel can be marked
1738  * consider_parallel as well. Note that if the query has rowMarks or is
1739  * not a SELECT, consider_parallel will be false for every relation in the
1740  * query.
1741  */
1742  if (current_rel->consider_parallel &&
1743  is_parallel_safe(root, parse->limitOffset) &&
1744  is_parallel_safe(root, parse->limitCount))
1745  final_rel->consider_parallel = true;
1746 
1747  /*
1748  * If the current_rel belongs to a single FDW, so does the final_rel.
1749  */
1750  final_rel->serverid = current_rel->serverid;
1751  final_rel->userid = current_rel->userid;
1752  final_rel->useridiscurrent = current_rel->useridiscurrent;
1753  final_rel->fdwroutine = current_rel->fdwroutine;
1754 
1755  /*
1756  * Generate paths for the final_rel. Insert all surviving paths, with
1757  * LockRows, Limit, and/or ModifyTable steps added if needed.
1758  */
1759  foreach(lc, current_rel->pathlist)
1760  {
1761  Path *path = (Path *) lfirst(lc);
1762 
1763  /*
1764  * If there is a FOR [KEY] UPDATE/SHARE clause, add the LockRows node.
1765  * (Note: we intentionally test parse->rowMarks not root->rowMarks
1766  * here. If there are only non-locking rowmarks, they should be
1767  * handled by the ModifyTable node instead. However, root->rowMarks
1768  * is what goes into the LockRows node.)
1769  */
1770  if (parse->rowMarks)
1771  {
1772  path = (Path *) create_lockrows_path(root, final_rel, path,
1773  root->rowMarks,
1775  }
1776 
1777  /*
1778  * If there is a LIMIT/OFFSET clause, add the LIMIT node.
1779  */
1780  if (limit_needed(parse))
1781  {
1782  path = (Path *) create_limit_path(root, final_rel, path,
1783  parse->limitOffset,
1784  parse->limitCount,
1785  parse->limitOption,
1786  offset_est, count_est);
1787  }
1788 
1789  /*
1790  * If this is an INSERT/UPDATE/DELETE/MERGE, add the ModifyTable node.
1791  */
1792  if (parse->commandType != CMD_SELECT)
1793  {
1794  Index rootRelation;
1795  List *resultRelations = NIL;
1796  List *updateColnosLists = NIL;
1797  List *withCheckOptionLists = NIL;
1798  List *returningLists = NIL;
1799  List *mergeActionLists = NIL;
1800  List *mergeJoinConditions = NIL;
1801  List *rowMarks;
1802 
1803  if (bms_membership(root->all_result_relids) == BMS_MULTIPLE)
1804  {
1805  /* Inherited UPDATE/DELETE/MERGE */
1806  RelOptInfo *top_result_rel = find_base_rel(root,
1807  parse->resultRelation);
1808  int resultRelation = -1;
1809 
1810  /* Pass the root result rel forward to the executor. */
1811  rootRelation = parse->resultRelation;
1812 
1813  /* Add only leaf children to ModifyTable. */
1814  while ((resultRelation = bms_next_member(root->leaf_result_relids,
1815  resultRelation)) >= 0)
1816  {
1817  RelOptInfo *this_result_rel = find_base_rel(root,
1818  resultRelation);
1819 
1820  /*
1821  * Also exclude any leaf rels that have turned dummy since
1822  * being added to the list, for example, by being excluded
1823  * by constraint exclusion.
1824  */
1825  if (IS_DUMMY_REL(this_result_rel))
1826  continue;
1827 
1828  /* Build per-target-rel lists needed by ModifyTable */
1829  resultRelations = lappend_int(resultRelations,
1830  resultRelation);
1831  if (parse->commandType == CMD_UPDATE)
1832  {
1833  List *update_colnos = root->update_colnos;
1834 
1835  if (this_result_rel != top_result_rel)
1836  update_colnos =
1838  update_colnos,
1839  this_result_rel->relid,
1840  top_result_rel->relid);
1841  updateColnosLists = lappend(updateColnosLists,
1842  update_colnos);
1843  }
1844  if (parse->withCheckOptions)
1845  {
1846  List *withCheckOptions = parse->withCheckOptions;
1847 
1848  if (this_result_rel != top_result_rel)
1849  withCheckOptions = (List *)
1851  (Node *) withCheckOptions,
1852  this_result_rel,
1853  top_result_rel);
1854  withCheckOptionLists = lappend(withCheckOptionLists,
1855  withCheckOptions);
1856  }
1857  if (parse->returningList)
1858  {
1859  List *returningList = parse->returningList;
1860 
1861  if (this_result_rel != top_result_rel)
1862  returningList = (List *)
1864  (Node *) returningList,
1865  this_result_rel,
1866  top_result_rel);
1867  returningLists = lappend(returningLists,
1868  returningList);
1869  }
1870  if (parse->mergeActionList)
1871  {
1872  ListCell *l;
1873  List *mergeActionList = NIL;
1874 
1875  /*
1876  * Copy MergeActions and translate stuff that
1877  * references attribute numbers.
1878  */
1879  foreach(l, parse->mergeActionList)
1880  {
1881  MergeAction *action = lfirst(l),
1882  *leaf_action = copyObject(action);
1883 
1884  leaf_action->qual =
1886  (Node *) action->qual,
1887  this_result_rel,
1888  top_result_rel);
1889  leaf_action->targetList = (List *)
1891  (Node *) action->targetList,
1892  this_result_rel,
1893  top_result_rel);
1894  if (leaf_action->commandType == CMD_UPDATE)
1895  leaf_action->updateColnos =
1897  action->updateColnos,
1898  this_result_rel->relid,
1899  top_result_rel->relid);
1900  mergeActionList = lappend(mergeActionList,
1901  leaf_action);
1902  }
1903 
1904  mergeActionLists = lappend(mergeActionLists,
1905  mergeActionList);
1906  }
1907  if (parse->commandType == CMD_MERGE)
1908  {
1909  Node *mergeJoinCondition = parse->mergeJoinCondition;
1910 
1911  if (this_result_rel != top_result_rel)
1912  mergeJoinCondition =
1914  mergeJoinCondition,
1915  this_result_rel,
1916  top_result_rel);
1917  mergeJoinConditions = lappend(mergeJoinConditions,
1918  mergeJoinCondition);
1919  }
1920  }
1921 
1922  if (resultRelations == NIL)
1923  {
1924  /*
1925  * We managed to exclude every child rel, so generate a
1926  * dummy one-relation plan using info for the top target
1927  * rel (even though that may not be a leaf target).
1928  * Although it's clear that no data will be updated or
1929  * deleted, we still need to have a ModifyTable node so
1930  * that any statement triggers will be executed. (This
1931  * could be cleaner if we fixed nodeModifyTable.c to allow
1932  * zero target relations, but that probably wouldn't be a
1933  * net win.)
1934  */
1935  resultRelations = list_make1_int(parse->resultRelation);
1936  if (parse->commandType == CMD_UPDATE)
1937  updateColnosLists = list_make1(root->update_colnos);
1938  if (parse->withCheckOptions)
1939  withCheckOptionLists = list_make1(parse->withCheckOptions);
1940  if (parse->returningList)
1941  returningLists = list_make1(parse->returningList);
1942  if (parse->mergeActionList)
1943  mergeActionLists = list_make1(parse->mergeActionList);
1944  if (parse->commandType == CMD_MERGE)
1945  mergeJoinConditions = list_make1(parse->mergeJoinCondition);
1946  }
1947  }
1948  else
1949  {
1950  /* Single-relation INSERT/UPDATE/DELETE/MERGE. */
1951  rootRelation = 0; /* there's no separate root rel */
1952  resultRelations = list_make1_int(parse->resultRelation);
1953  if (parse->commandType == CMD_UPDATE)
1954  updateColnosLists = list_make1(root->update_colnos);
1955  if (parse->withCheckOptions)
1956  withCheckOptionLists = list_make1(parse->withCheckOptions);
1957  if (parse->returningList)
1958  returningLists = list_make1(parse->returningList);
1959  if (parse->mergeActionList)
1960  mergeActionLists = list_make1(parse->mergeActionList);
1961  if (parse->commandType == CMD_MERGE)
1962  mergeJoinConditions = list_make1(parse->mergeJoinCondition);
1963  }
1964 
1965  /*
1966  * If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node
1967  * will have dealt with fetching non-locked marked rows, else we
1968  * need to have ModifyTable do that.
1969  */
1970  if (parse->rowMarks)
1971  rowMarks = NIL;
1972  else
1973  rowMarks = root->rowMarks;
1974 
1975  path = (Path *)
1976  create_modifytable_path(root, final_rel,
1977  path,
1978  parse->commandType,
1979  parse->canSetTag,
1980  parse->resultRelation,
1981  rootRelation,
1982  root->partColsUpdated,
1983  resultRelations,
1984  updateColnosLists,
1985  withCheckOptionLists,
1986  returningLists,
1987  rowMarks,
1988  parse->onConflict,
1989  mergeActionLists,
1990  mergeJoinConditions,
1992  }
1993 
1994  /* And shove it into final_rel */
1995  add_path(final_rel, path);
1996  }
1997 
1998  /*
1999  * Generate partial paths for final_rel, too, if outer query levels might
2000  * be able to make use of them.
2001  */
2002  if (final_rel->consider_parallel && root->query_level > 1 &&
2003  !limit_needed(parse))
2004  {
2005  Assert(!parse->rowMarks && parse->commandType == CMD_SELECT);
2006  foreach(lc, current_rel->partial_pathlist)
2007  {
2008  Path *partial_path = (Path *) lfirst(lc);
2009 
2010  add_partial_path(final_rel, partial_path);
2011  }
2012  }
2013 
2014  extra.limit_needed = limit_needed(parse);
2015  extra.limit_tuples = limit_tuples;
2016  extra.count_est = count_est;
2017  extra.offset_est = offset_est;
2018 
2019  /*
2020  * If there is an FDW that's responsible for all baserels of the query,
2021  * let it consider adding ForeignPaths.
2022  */
2023  if (final_rel->fdwroutine &&
2024  final_rel->fdwroutine->GetForeignUpperPaths)
2025  final_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_FINAL,
2026  current_rel, final_rel,
2027  &extra);
2028 
2029  /* Let extensions possibly add some more paths */
2031  (*create_upper_paths_hook) (root, UPPERREL_FINAL,
2032  current_rel, final_rel, &extra);
2033 
2034  /* Note: currently, we leave it to callers to do set_cheapest() */
2035 }
2036 
2037 /*
2038  * Do preprocessing for groupingSets clause and related data. This handles the
2039  * preliminary steps of expanding the grouping sets, organizing them into lists
2040  * of rollups, and preparing annotations which will later be filled in with
2041  * size estimates.
2042  */
2043 static grouping_sets_data *
2045 {
2046  Query *parse = root->parse;
2047  List *sets;
2048  int maxref = 0;
2049  ListCell *lc_set;
2051 
2052  parse->groupingSets = expand_grouping_sets(parse->groupingSets, parse->groupDistinct, -1);
2053 
2054  gd->any_hashable = false;
2055  gd->unhashable_refs = NULL;
2056  gd->unsortable_refs = NULL;
2057  gd->unsortable_sets = NIL;
2058 
2059  /*
2060  * We don't currently make any attempt to optimize the groupClause when
2061  * there are grouping sets, so just duplicate it in processed_groupClause.
2062  */
2063  root->processed_groupClause = parse->groupClause;
2064 
2065  if (parse->groupClause)
2066  {
2067  ListCell *lc;
2068 
2069  foreach(lc, parse->groupClause)
2070  {
2072  Index ref = gc->tleSortGroupRef;
2073 
2074  if (ref > maxref)
2075  maxref = ref;
2076 
2077  if (!gc->hashable)
2079 
2080  if (!OidIsValid(gc->sortop))
2082  }
2083  }
2084 
2085  /* Allocate workspace array for remapping */
2086  gd->tleref_to_colnum_map = (int *) palloc((maxref + 1) * sizeof(int));
2087 
2088  /*
2089  * If we have any unsortable sets, we must extract them before trying to
2090  * prepare rollups. Unsortable sets don't go through
2091  * reorder_grouping_sets, so we must apply the GroupingSetData annotation
2092  * here.
2093  */
2094  if (!bms_is_empty(gd->unsortable_refs))
2095  {
2096  List *sortable_sets = NIL;
2097  ListCell *lc;
2098 
2099  foreach(lc, parse->groupingSets)
2100  {
2101  List *gset = (List *) lfirst(lc);
2102 
2103  if (bms_overlap_list(gd->unsortable_refs, gset))
2104  {
2106 
2107  gs->set = gset;
2108  gd->unsortable_sets = lappend(gd->unsortable_sets, gs);
2109 
2110  /*
2111  * We must enforce here that an unsortable set is hashable;
2112  * later code assumes this. Parse analysis only checks that
2113  * every individual column is either hashable or sortable.
2114  *
2115  * Note that passing this test doesn't guarantee we can
2116  * generate a plan; there might be other showstoppers.
2117  */
2118  if (bms_overlap_list(gd->unhashable_refs, gset))
2119  ereport(ERROR,
2120  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2121  errmsg("could not implement GROUP BY"),
2122  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
2123  }
2124  else
2125  sortable_sets = lappend(sortable_sets, gset);
2126  }
2127 
2128  if (sortable_sets)
2129  sets = extract_rollup_sets(sortable_sets);
2130  else
2131  sets = NIL;
2132  }
2133  else
2134  sets = extract_rollup_sets(parse->groupingSets);
2135 
2136  foreach(lc_set, sets)
2137  {
2138  List *current_sets = (List *) lfirst(lc_set);
2139  RollupData *rollup = makeNode(RollupData);
2140  GroupingSetData *gs;
2141 
2142  /*
2143  * Reorder the current list of grouping sets into correct prefix
2144  * order. If only one aggregation pass is needed, try to make the
2145  * list match the ORDER BY clause; if more than one pass is needed, we
2146  * don't bother with that.
2147  *
2148  * Note that this reorders the sets from smallest-member-first to
2149  * largest-member-first, and applies the GroupingSetData annotations,
2150  * though the data will be filled in later.
2151  */
2152  current_sets = reorder_grouping_sets(current_sets,
2153  (list_length(sets) == 1
2154  ? parse->sortClause
2155  : NIL));
2156 
2157  /*
2158  * Get the initial (and therefore largest) grouping set.
2159  */
2160  gs = linitial_node(GroupingSetData, current_sets);
2161 
2162  /*
2163  * Order the groupClause appropriately. If the first grouping set is
2164  * empty, then the groupClause must also be empty; otherwise we have
2165  * to force the groupClause to match that grouping set's order.
2166  *
2167  * (The first grouping set can be empty even though parse->groupClause
2168  * is not empty only if all non-empty grouping sets are unsortable.
2169  * The groupClauses for hashed grouping sets are built later on.)
2170  */
2171  if (gs->set)
2172  rollup->groupClause = preprocess_groupclause(root, gs->set);
2173  else
2174  rollup->groupClause = NIL;
2175 
2176  /*
2177  * Is it hashable? We pretend empty sets are hashable even though we
2178  * actually force them not to be hashed later. But don't bother if
2179  * there's nothing but empty sets (since in that case we can't hash
2180  * anything).
2181  */
2182  if (gs->set &&
2184  {
2185  rollup->hashable = true;
2186  gd->any_hashable = true;
2187  }
2188 
2189  /*
2190  * Now that we've pinned down an order for the groupClause for this
2191  * list of grouping sets, we need to remap the entries in the grouping
2192  * sets from sortgrouprefs to plain indices (0-based) into the
2193  * groupClause for this collection of grouping sets. We keep the
2194  * original form for later use, though.
2195  */
2196  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
2197  current_sets,
2198  gd->tleref_to_colnum_map);
2199  rollup->gsets_data = current_sets;
2200 
2201  gd->rollups = lappend(gd->rollups, rollup);
2202  }
2203 
2204  if (gd->unsortable_sets)
2205  {
2206  /*
2207  * We have not yet pinned down a groupclause for this, but we will
2208  * need index-based lists for estimation purposes. Construct
2209  * hash_sets_idx based on the entire original groupclause for now.
2210  */
2211  gd->hash_sets_idx = remap_to_groupclause_idx(parse->groupClause,
2212  gd->unsortable_sets,
2213  gd->tleref_to_colnum_map);
2214  gd->any_hashable = true;
2215  }
2216 
2217  return gd;
2218 }
2219 
2220 /*
2221  * Given a groupclause and a list of GroupingSetData, return equivalent sets
2222  * (without annotation) mapped to indexes into the given groupclause.
2223  */
2224 static List *
2226  List *gsets,
2227  int *tleref_to_colnum_map)
2228 {
2229  int ref = 0;
2230  List *result = NIL;
2231  ListCell *lc;
2232 
2233  foreach(lc, groupClause)
2234  {
2236 
2237  tleref_to_colnum_map[gc->tleSortGroupRef] = ref++;
2238  }
2239 
2240  foreach(lc, gsets)
2241  {
2242  List *set = NIL;
2243  ListCell *lc2;
2245 
2246  foreach(lc2, gs->set)
2247  {
2248  set = lappend_int(set, tleref_to_colnum_map[lfirst_int(lc2)]);
2249  }
2250 
2251  result = lappend(result, set);
2252  }
2253 
2254  return result;
2255 }
2256 
2257 
2258 /*
2259  * preprocess_rowmarks - set up PlanRowMarks if needed
2260  */
2261 static void
2263 {
2264  Query *parse = root->parse;
2265  Bitmapset *rels;
2266  List *prowmarks;
2267  ListCell *l;
2268  int i;
2269 
2270  if (parse->rowMarks)
2271  {
2272  /*
2273  * We've got trouble if FOR [KEY] UPDATE/SHARE appears inside
2274  * grouping, since grouping renders a reference to individual tuple
2275  * CTIDs invalid. This is also checked at parse time, but that's
2276  * insufficient because of rule substitution, query pullup, etc.
2277  */
2279  parse->rowMarks)->strength);
2280  }
2281  else
2282  {
2283  /*
2284  * We only need rowmarks for UPDATE, DELETE, MERGE, or FOR [KEY]
2285  * UPDATE/SHARE.
2286  */
2287  if (parse->commandType != CMD_UPDATE &&
2288  parse->commandType != CMD_DELETE &&
2289  parse->commandType != CMD_MERGE)
2290  return;
2291  }
2292 
2293  /*
2294  * We need to have rowmarks for all base relations except the target. We
2295  * make a bitmapset of all base rels and then remove the items we don't
2296  * need or have FOR [KEY] UPDATE/SHARE marks for.
2297  */
2298  rels = get_relids_in_jointree((Node *) parse->jointree, false, false);
2299  if (parse->resultRelation)
2300  rels = bms_del_member(rels, parse->resultRelation);
2301 
2302  /*
2303  * Convert RowMarkClauses to PlanRowMark representation.
2304  */
2305  prowmarks = NIL;
2306  foreach(l, parse->rowMarks)
2307  {
2309  RangeTblEntry *rte = rt_fetch(rc->rti, parse->rtable);
2310  PlanRowMark *newrc;
2311 
2312  /*
2313  * Currently, it is syntactically impossible to have FOR UPDATE et al
2314  * applied to an update/delete target rel. If that ever becomes
2315  * possible, we should drop the target from the PlanRowMark list.
2316  */
2317  Assert(rc->rti != parse->resultRelation);
2318 
2319  /*
2320  * Ignore RowMarkClauses for subqueries; they aren't real tables and
2321  * can't support true locking. Subqueries that got flattened into the
2322  * main query should be ignored completely. Any that didn't will get
2323  * ROW_MARK_COPY items in the next loop.
2324  */
2325  if (rte->rtekind != RTE_RELATION)
2326  continue;
2327 
2328  rels = bms_del_member(rels, rc->rti);
2329 
2330  newrc = makeNode(PlanRowMark);
2331  newrc->rti = newrc->prti = rc->rti;
2332  newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2333  newrc->markType = select_rowmark_type(rte, rc->strength);
2334  newrc->allMarkTypes = (1 << newrc->markType);
2335  newrc->strength = rc->strength;
2336  newrc->waitPolicy = rc->waitPolicy;
2337  newrc->isParent = false;
2338 
2339  prowmarks = lappend(prowmarks, newrc);
2340  }
2341 
2342  /*
2343  * Now, add rowmarks for any non-target, non-locked base relations.
2344  */
2345  i = 0;
2346  foreach(l, parse->rtable)
2347  {
2349  PlanRowMark *newrc;
2350 
2351  i++;
2352  if (!bms_is_member(i, rels))
2353  continue;
2354 
2355  newrc = makeNode(PlanRowMark);
2356  newrc->rti = newrc->prti = i;
2357  newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2358  newrc->markType = select_rowmark_type(rte, LCS_NONE);
2359  newrc->allMarkTypes = (1 << newrc->markType);
2360  newrc->strength = LCS_NONE;
2361  newrc->waitPolicy = LockWaitBlock; /* doesn't matter */
2362  newrc->isParent = false;
2363 
2364  prowmarks = lappend(prowmarks, newrc);
2365  }
2366 
2367  root->rowMarks = prowmarks;
2368 }
2369 
2370 /*
2371  * Select RowMarkType to use for a given table
2372  */
2375 {
2376  if (rte->rtekind != RTE_RELATION)
2377  {
2378  /* If it's not a table at all, use ROW_MARK_COPY */
2379  return ROW_MARK_COPY;
2380  }
2381  else if (rte->relkind == RELKIND_FOREIGN_TABLE)
2382  {
2383  /* Let the FDW select the rowmark type, if it wants to */
2384  FdwRoutine *fdwroutine = GetFdwRoutineByRelId(rte->relid);
2385 
2386  if (fdwroutine->GetForeignRowMarkType != NULL)
2387  return fdwroutine->GetForeignRowMarkType(rte, strength);
2388  /* Otherwise, use ROW_MARK_COPY by default */
2389  return ROW_MARK_COPY;
2390  }
2391  else
2392  {
2393  /* Regular table, apply the appropriate lock type */
2394  switch (strength)
2395  {
2396  case LCS_NONE:
2397 
2398  /*
2399  * We don't need a tuple lock, only the ability to re-fetch
2400  * the row.
2401  */
2402  return ROW_MARK_REFERENCE;
2403  break;
2404  case LCS_FORKEYSHARE:
2405  return ROW_MARK_KEYSHARE;
2406  break;
2407  case LCS_FORSHARE:
2408  return ROW_MARK_SHARE;
2409  break;
2410  case LCS_FORNOKEYUPDATE:
2411  return ROW_MARK_NOKEYEXCLUSIVE;
2412  break;
2413  case LCS_FORUPDATE:
2414  return ROW_MARK_EXCLUSIVE;
2415  break;
2416  }
2417  elog(ERROR, "unrecognized LockClauseStrength %d", (int) strength);
2418  return ROW_MARK_EXCLUSIVE; /* keep compiler quiet */
2419  }
2420 }
2421 
2422 /*
2423  * preprocess_limit - do pre-estimation for LIMIT and/or OFFSET clauses
2424  *
2425  * We try to estimate the values of the LIMIT/OFFSET clauses, and pass the
2426  * results back in *count_est and *offset_est. These variables are set to
2427  * 0 if the corresponding clause is not present, and -1 if it's present
2428  * but we couldn't estimate the value for it. (The "0" convention is OK
2429  * for OFFSET but a little bit bogus for LIMIT: effectively we estimate
2430  * LIMIT 0 as though it were LIMIT 1. But this is in line with the planner's
2431  * usual practice of never estimating less than one row.) These values will
2432  * be passed to create_limit_path, which see if you change this code.
2433  *
2434  * The return value is the suitably adjusted tuple_fraction to use for
2435  * planning the query. This adjustment is not overridable, since it reflects
2436  * plan actions that grouping_planner() will certainly take, not assumptions
2437  * about context.
2438  */
2439 static double
2440 preprocess_limit(PlannerInfo *root, double tuple_fraction,
2441  int64 *offset_est, int64 *count_est)
2442 {
2443  Query *parse = root->parse;
2444  Node *est;
2445  double limit_fraction;
2446 
2447  /* Should not be called unless LIMIT or OFFSET */
2448  Assert(parse->limitCount || parse->limitOffset);
2449 
2450  /*
2451  * Try to obtain the clause values. We use estimate_expression_value
2452  * primarily because it can sometimes do something useful with Params.
2453  */
2454  if (parse->limitCount)
2455  {
2456  est = estimate_expression_value(root, parse->limitCount);
2457  if (est && IsA(est, Const))
2458  {
2459  if (((Const *) est)->constisnull)
2460  {
2461  /* NULL indicates LIMIT ALL, ie, no limit */
2462  *count_est = 0; /* treat as not present */
2463  }
2464  else
2465  {
2466  *count_est = DatumGetInt64(((Const *) est)->constvalue);
2467  if (*count_est <= 0)
2468  *count_est = 1; /* force to at least 1 */
2469  }
2470  }
2471  else
2472  *count_est = -1; /* can't estimate */
2473  }
2474  else
2475  *count_est = 0; /* not present */
2476 
2477  if (parse->limitOffset)
2478  {
2479  est = estimate_expression_value(root, parse->limitOffset);
2480  if (est && IsA(est, Const))
2481  {
2482  if (((Const *) est)->constisnull)
2483  {
2484  /* Treat NULL as no offset; the executor will too */
2485  *offset_est = 0; /* treat as not present */
2486  }
2487  else
2488  {
2489  *offset_est = DatumGetInt64(((Const *) est)->constvalue);
2490  if (*offset_est < 0)
2491  *offset_est = 0; /* treat as not present */
2492  }
2493  }
2494  else
2495  *offset_est = -1; /* can't estimate */
2496  }
2497  else
2498  *offset_est = 0; /* not present */
2499 
2500  if (*count_est != 0)
2501  {
2502  /*
2503  * A LIMIT clause limits the absolute number of tuples returned.
2504  * However, if it's not a constant LIMIT then we have to guess; for
2505  * lack of a better idea, assume 10% of the plan's result is wanted.
2506  */
2507  if (*count_est < 0 || *offset_est < 0)
2508  {
2509  /* LIMIT or OFFSET is an expression ... punt ... */
2510  limit_fraction = 0.10;
2511  }
2512  else
2513  {
2514  /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
2515  limit_fraction = (double) *count_est + (double) *offset_est;
2516  }
2517 
2518  /*
2519  * If we have absolute limits from both caller and LIMIT, use the
2520  * smaller value; likewise if they are both fractional. If one is
2521  * fractional and the other absolute, we can't easily determine which
2522  * is smaller, but we use the heuristic that the absolute will usually
2523  * be smaller.
2524  */
2525  if (tuple_fraction >= 1.0)
2526  {
2527  if (limit_fraction >= 1.0)
2528  {
2529  /* both absolute */
2530  tuple_fraction = Min(tuple_fraction, limit_fraction);
2531  }
2532  else
2533  {
2534  /* caller absolute, limit fractional; use caller's value */
2535  }
2536  }
2537  else if (tuple_fraction > 0.0)
2538  {
2539  if (limit_fraction >= 1.0)
2540  {
2541  /* caller fractional, limit absolute; use limit */
2542  tuple_fraction = limit_fraction;
2543  }
2544  else
2545  {
2546  /* both fractional */
2547  tuple_fraction = Min(tuple_fraction, limit_fraction);
2548  }
2549  }
2550  else
2551  {
2552  /* no info from caller, just use limit */
2553  tuple_fraction = limit_fraction;
2554  }
2555  }
2556  else if (*offset_est != 0 && tuple_fraction > 0.0)
2557  {
2558  /*
2559  * We have an OFFSET but no LIMIT. This acts entirely differently
2560  * from the LIMIT case: here, we need to increase rather than decrease
2561  * the caller's tuple_fraction, because the OFFSET acts to cause more
2562  * tuples to be fetched instead of fewer. This only matters if we got
2563  * a tuple_fraction > 0, however.
2564  *
2565  * As above, use 10% if OFFSET is present but unestimatable.
2566  */
2567  if (*offset_est < 0)
2568  limit_fraction = 0.10;
2569  else
2570  limit_fraction = (double) *offset_est;
2571 
2572  /*
2573  * If we have absolute counts from both caller and OFFSET, add them
2574  * together; likewise if they are both fractional. If one is
2575  * fractional and the other absolute, we want to take the larger, and
2576  * we heuristically assume that's the fractional one.
2577  */
2578  if (tuple_fraction >= 1.0)
2579  {
2580  if (limit_fraction >= 1.0)
2581  {
2582  /* both absolute, so add them together */
2583  tuple_fraction += limit_fraction;
2584  }
2585  else
2586  {
2587  /* caller absolute, limit fractional; use limit */
2588  tuple_fraction = limit_fraction;
2589  }
2590  }
2591  else
2592  {
2593  if (limit_fraction >= 1.0)
2594  {
2595  /* caller fractional, limit absolute; use caller's value */
2596  }
2597  else
2598  {
2599  /* both fractional, so add them together */
2600  tuple_fraction += limit_fraction;
2601  if (tuple_fraction >= 1.0)
2602  tuple_fraction = 0.0; /* assume fetch all */
2603  }
2604  }
2605  }
2606 
2607  return tuple_fraction;
2608 }
2609 
2610 /*
2611  * limit_needed - do we actually need a Limit plan node?
2612  *
2613  * If we have constant-zero OFFSET and constant-null LIMIT, we can skip adding
2614  * a Limit node. This is worth checking for because "OFFSET 0" is a common
2615  * locution for an optimization fence. (Because other places in the planner
2616  * merely check whether parse->limitOffset isn't NULL, it will still work as
2617  * an optimization fence --- we're just suppressing unnecessary run-time
2618  * overhead.)
2619  *
2620  * This might look like it could be merged into preprocess_limit, but there's
2621  * a key distinction: here we need hard constants in OFFSET/LIMIT, whereas
2622  * in preprocess_limit it's good enough to consider estimated values.
2623  */
2624 bool
2626 {
2627  Node *node;
2628 
2629  node = parse->limitCount;
2630  if (node)
2631  {
2632  if (IsA(node, Const))
2633  {
2634  /* NULL indicates LIMIT ALL, ie, no limit */
2635  if (!((Const *) node)->constisnull)
2636  return true; /* LIMIT with a constant value */
2637  }
2638  else
2639  return true; /* non-constant LIMIT */
2640  }
2641 
2642  node = parse->limitOffset;
2643  if (node)
2644  {
2645  if (IsA(node, Const))
2646  {
2647  /* Treat NULL as no offset; the executor would too */
2648  if (!((Const *) node)->constisnull)
2649  {
2650  int64 offset = DatumGetInt64(((Const *) node)->constvalue);
2651 
2652  if (offset != 0)
2653  return true; /* OFFSET with a nonzero value */
2654  }
2655  }
2656  else
2657  return true; /* non-constant OFFSET */
2658  }
2659 
2660  return false; /* don't need a Limit plan node */
2661 }
2662 
2663 
2664 /*
2665  * remove_useless_groupby_columns
2666  * Remove any columns in the GROUP BY clause that are redundant due to
2667  * being functionally dependent on other GROUP BY columns.
2668  *
2669  * Since some other DBMSes do not allow references to ungrouped columns, it's
2670  * not unusual to find all columns listed in GROUP BY even though listing the
2671  * primary-key columns would be sufficient. Deleting such excess columns
2672  * avoids redundant sorting work, so it's worth doing.
2673  *
2674  * Relcache invalidations will ensure that cached plans become invalidated
2675  * when the underlying index of the pkey constraint is dropped.
2676  *
2677  * Currently, we only make use of pkey constraints for this, however, we may
2678  * wish to take this further in the future and also use unique constraints
2679  * which have NOT NULL columns. In that case, plan invalidation will still
2680  * work since relations will receive a relcache invalidation when a NOT NULL
2681  * constraint is dropped.
2682  */
2683 static void
2685 {
2686  Query *parse = root->parse;
2687  Bitmapset **groupbyattnos;
2688  Bitmapset **surplusvars;
2689  ListCell *lc;
2690  int relid;
2691 
2692  /* No chance to do anything if there are less than two GROUP BY items */
2693  if (list_length(root->processed_groupClause) < 2)
2694  return;
2695 
2696  /* Don't fiddle with the GROUP BY clause if the query has grouping sets */
2697  if (parse->groupingSets)
2698  return;
2699 
2700  /*
2701  * Scan the GROUP BY clause to find GROUP BY items that are simple Vars.
2702  * Fill groupbyattnos[k] with a bitmapset of the column attnos of RTE k
2703  * that are GROUP BY items.
2704  */
2705  groupbyattnos = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
2706  (list_length(parse->rtable) + 1));
2707  foreach(lc, root->processed_groupClause)
2708  {
2710  TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
2711  Var *var = (Var *) tle->expr;
2712 
2713  /*
2714  * Ignore non-Vars and Vars from other query levels.
2715  *
2716  * XXX in principle, stable expressions containing Vars could also be
2717  * removed, if all the Vars are functionally dependent on other GROUP
2718  * BY items. But it's not clear that such cases occur often enough to
2719  * be worth troubling over.
2720  */
2721  if (!IsA(var, Var) ||
2722  var->varlevelsup > 0)
2723  continue;
2724 
2725  /* OK, remember we have this Var */
2726  relid = var->varno;
2727  Assert(relid <= list_length(parse->rtable));
2728  groupbyattnos[relid] = bms_add_member(groupbyattnos[relid],
2730  }
2731 
2732  /*
2733  * Consider each relation and see if it is possible to remove some of its
2734  * Vars from GROUP BY. For simplicity and speed, we do the actual removal
2735  * in a separate pass. Here, we just fill surplusvars[k] with a bitmapset
2736  * of the column attnos of RTE k that are removable GROUP BY items.
2737  */
2738  surplusvars = NULL; /* don't allocate array unless required */
2739  relid = 0;
2740  foreach(lc, parse->rtable)
2741  {
2743  Bitmapset *relattnos;
2744  Bitmapset *pkattnos;
2745  Oid constraintOid;
2746 
2747  relid++;
2748 
2749  /* Only plain relations could have primary-key constraints */
2750  if (rte->rtekind != RTE_RELATION)
2751  continue;
2752 
2753  /*
2754  * We must skip inheritance parent tables as some of the child rels
2755  * may cause duplicate rows. This cannot happen with partitioned
2756  * tables, however.
2757  */
2758  if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
2759  continue;
2760 
2761  /* Nothing to do unless this rel has multiple Vars in GROUP BY */
2762  relattnos = groupbyattnos[relid];
2763  if (bms_membership(relattnos) != BMS_MULTIPLE)
2764  continue;
2765 
2766  /*
2767  * Can't remove any columns for this rel if there is no suitable
2768  * (i.e., nondeferrable) primary key constraint.
2769  */
2770  pkattnos = get_primary_key_attnos(rte->relid, false, &constraintOid);
2771  if (pkattnos == NULL)
2772  continue;
2773 
2774  /*
2775  * If the primary key is a proper subset of relattnos then we have
2776  * some items in the GROUP BY that can be removed.
2777  */
2778  if (bms_subset_compare(pkattnos, relattnos) == BMS_SUBSET1)
2779  {
2780  /*
2781  * To easily remember whether we've found anything to do, we don't
2782  * allocate the surplusvars[] array until we find something.
2783  */
2784  if (surplusvars == NULL)
2785  surplusvars = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
2786  (list_length(parse->rtable) + 1));
2787 
2788  /* Remember the attnos of the removable columns */
2789  surplusvars[relid] = bms_difference(relattnos, pkattnos);
2790  }
2791  }
2792 
2793  /*
2794  * If we found any surplus Vars, build a new GROUP BY clause without them.
2795  * (Note: this may leave some TLEs with unreferenced ressortgroupref
2796  * markings, but that's harmless.)
2797  */
2798  if (surplusvars != NULL)
2799  {
2800  List *new_groupby = NIL;
2801 
2802  foreach(lc, root->processed_groupClause)
2803  {
2805  TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
2806  Var *var = (Var *) tle->expr;
2807 
2808  /*
2809  * New list must include non-Vars, outer Vars, and anything not
2810  * marked as surplus.
2811  */
2812  if (!IsA(var, Var) ||
2813  var->varlevelsup > 0 ||
2815  surplusvars[var->varno]))
2816  new_groupby = lappend(new_groupby, sgc);
2817  }
2818 
2819  root->processed_groupClause = new_groupby;
2820  }
2821 }
2822 
2823 /*
2824  * preprocess_groupclause - do preparatory work on GROUP BY clause
2825  *
2826  * The idea here is to adjust the ordering of the GROUP BY elements
2827  * (which in itself is semantically insignificant) to match ORDER BY,
2828  * thereby allowing a single sort operation to both implement the ORDER BY
2829  * requirement and set up for a Unique step that implements GROUP BY.
2830  * We also consider partial match between GROUP BY and ORDER BY elements,
2831  * which could allow to implement ORDER BY using the incremental sort.
2832  *
2833  * We also consider other orderings of the GROUP BY elements, which could
2834  * match the sort ordering of other possible plans (eg an indexscan) and
2835  * thereby reduce cost. This is implemented during the generation of grouping
2836  * paths. See get_useful_group_keys_orderings() for details.
2837  *
2838  * Note: we need no comparable processing of the distinctClause because
2839  * the parser already enforced that that matches ORDER BY.
2840  *
2841  * Note: we return a fresh List, but its elements are the same
2842  * SortGroupClauses appearing in parse->groupClause. This is important
2843  * because later processing may modify the processed_groupClause list.
2844  *
2845  * For grouping sets, the order of items is instead forced to agree with that
2846  * of the grouping set (and items not in the grouping set are skipped). The
2847  * work of sorting the order of grouping set elements to match the ORDER BY if
2848  * possible is done elsewhere.
2849  */
2850 static List *
2852 {
2853  Query *parse = root->parse;
2854  List *new_groupclause = NIL;
2855  ListCell *sl;
2856  ListCell *gl;
2857 
2858  /* For grouping sets, we need to force the ordering */
2859  if (force)
2860  {
2861  foreach(sl, force)
2862  {
2863  Index ref = lfirst_int(sl);
2864  SortGroupClause *cl = get_sortgroupref_clause(ref, parse->groupClause);
2865 
2866  new_groupclause = lappend(new_groupclause, cl);
2867  }
2868 
2869  return new_groupclause;
2870  }
2871 
2872  /* If no ORDER BY, nothing useful to do here */
2873  if (parse->sortClause == NIL)
2874  return list_copy(parse->groupClause);
2875 
2876  /*
2877  * Scan the ORDER BY clause and construct a list of matching GROUP BY
2878  * items, but only as far as we can make a matching prefix.
2879  *
2880  * This code assumes that the sortClause contains no duplicate items.
2881  */
2882  foreach(sl, parse->sortClause)
2883  {
2885 
2886  foreach(gl, parse->groupClause)
2887  {
2889 
2890  if (equal(gc, sc))
2891  {
2892  new_groupclause = lappend(new_groupclause, gc);
2893  break;
2894  }
2895  }
2896  if (gl == NULL)
2897  break; /* no match, so stop scanning */
2898  }
2899 
2900 
2901  /* If no match at all, no point in reordering GROUP BY */
2902  if (new_groupclause == NIL)
2903  return list_copy(parse->groupClause);
2904 
2905  /*
2906  * Add any remaining GROUP BY items to the new list. We don't require a
2907  * complete match, because even partial match allows ORDER BY to be
2908  * implemented using incremental sort. Also, give up if there are any
2909  * non-sortable GROUP BY items, since then there's no hope anyway.
2910  */
2911  foreach(gl, parse->groupClause)
2912  {
2914 
2915  if (list_member_ptr(new_groupclause, gc))
2916  continue; /* it matched an ORDER BY item */
2917  if (!OidIsValid(gc->sortop)) /* give up, GROUP BY can't be sorted */
2918  return list_copy(parse->groupClause);
2919  new_groupclause = lappend(new_groupclause, gc);
2920  }
2921 
2922  /* Success --- install the rearranged GROUP BY list */
2923  Assert(list_length(parse->groupClause) == list_length(new_groupclause));
2924  return new_groupclause;
2925 }
2926 
2927 /*
2928  * Extract lists of grouping sets that can be implemented using a single
2929  * rollup-type aggregate pass each. Returns a list of lists of grouping sets.
2930  *
2931  * Input must be sorted with smallest sets first. Result has each sublist
2932  * sorted with smallest sets first.
2933  *
2934  * We want to produce the absolute minimum possible number of lists here to
2935  * avoid excess sorts. Fortunately, there is an algorithm for this; the problem
2936  * of finding the minimal partition of a partially-ordered set into chains
2937  * (which is what we need, taking the list of grouping sets as a poset ordered
2938  * by set inclusion) can be mapped to the problem of finding the maximum
2939  * cardinality matching on a bipartite graph, which is solvable in polynomial
2940  * time with a worst case of no worse than O(n^2.5) and usually much
2941  * better. Since our N is at most 4096, we don't need to consider fallbacks to
2942  * heuristic or approximate methods. (Planning time for a 12-d cube is under
2943  * half a second on my modest system even with optimization off and assertions
2944  * on.)
2945  */
2946 static List *
2948 {
2949  int num_sets_raw = list_length(groupingSets);
2950  int num_empty = 0;
2951  int num_sets = 0; /* distinct sets */
2952  int num_chains = 0;
2953  List *result = NIL;
2954  List **results;
2955  List **orig_sets;
2956  Bitmapset **set_masks;
2957  int *chains;
2958  short **adjacency;
2959  short *adjacency_buf;
2961  int i;
2962  int j;
2963  int j_size;
2964  ListCell *lc1 = list_head(groupingSets);
2965  ListCell *lc;
2966 
2967  /*
2968  * Start by stripping out empty sets. The algorithm doesn't require this,
2969  * but the planner currently needs all empty sets to be returned in the
2970  * first list, so we strip them here and add them back after.
2971  */
2972  while (lc1 && lfirst(lc1) == NIL)
2973  {
2974  ++num_empty;
2975  lc1 = lnext(groupingSets, lc1);
2976  }
2977 
2978  /* bail out now if it turns out that all we had were empty sets. */
2979  if (!lc1)
2980  return list_make1(groupingSets);
2981 
2982  /*----------
2983  * We don't strictly need to remove duplicate sets here, but if we don't,
2984  * they tend to become scattered through the result, which is a bit
2985  * confusing (and irritating if we ever decide to optimize them out).
2986  * So we remove them here and add them back after.
2987  *
2988  * For each non-duplicate set, we fill in the following:
2989  *
2990  * orig_sets[i] = list of the original set lists
2991  * set_masks[i] = bitmapset for testing inclusion
2992  * adjacency[i] = array [n, v1, v2, ... vn] of adjacency indices
2993  *
2994  * chains[i] will be the result group this set is assigned to.
2995  *
2996  * We index all of these from 1 rather than 0 because it is convenient
2997  * to leave 0 free for the NIL node in the graph algorithm.
2998  *----------
2999  */
3000  orig_sets = palloc0((num_sets_raw + 1) * sizeof(List *));
3001  set_masks = palloc0((num_sets_raw + 1) * sizeof(Bitmapset *));
3002  adjacency = palloc0((num_sets_raw + 1) * sizeof(short *));
3003  adjacency_buf = palloc((num_sets_raw + 1) * sizeof(short));
3004 
3005  j_size = 0;
3006  j = 0;
3007  i = 1;
3008 
3009  for_each_cell(lc, groupingSets, lc1)
3010  {
3011  List *candidate = (List *) lfirst(lc);
3012  Bitmapset *candidate_set = NULL;
3013  ListCell *lc2;
3014  int dup_of = 0;
3015 
3016  foreach(lc2, candidate)
3017  {
3018  candidate_set = bms_add_member(candidate_set, lfirst_int(lc2));
3019  }
3020 
3021  /* we can only be a dup if we're the same length as a previous set */
3022  if (j_size == list_length(candidate))
3023  {
3024  int k;
3025 
3026  for (k = j; k < i; ++k)
3027  {
3028  if (bms_equal(set_masks[k], candidate_set))
3029  {
3030  dup_of = k;
3031  break;
3032  }
3033  }
3034  }
3035  else if (j_size < list_length(candidate))
3036  {
3037  j_size = list_length(candidate);
3038  j = i;
3039  }
3040 
3041  if (dup_of > 0)
3042  {
3043  orig_sets[dup_of] = lappend(orig_sets[dup_of], candidate);
3044  bms_free(candidate_set);
3045  }
3046  else
3047  {
3048  int k;
3049  int n_adj = 0;
3050 
3051  orig_sets[i] = list_make1(candidate);
3052  set_masks[i] = candidate_set;
3053 
3054  /* fill in adjacency list; no need to compare equal-size sets */
3055 
3056  for (k = j - 1; k > 0; --k)
3057  {
3058  if (bms_is_subset(set_masks[k], candidate_set))
3059  adjacency_buf[++n_adj] = k;
3060  }
3061 
3062  if (n_adj > 0)
3063  {
3064  adjacency_buf[0] = n_adj;
3065  adjacency[i] = palloc((n_adj + 1) * sizeof(short));
3066  memcpy(adjacency[i], adjacency_buf, (n_adj + 1) * sizeof(short));
3067  }
3068  else
3069  adjacency[i] = NULL;
3070 
3071  ++i;
3072  }
3073  }
3074 
3075  num_sets = i - 1;
3076 
3077  /*
3078  * Apply the graph matching algorithm to do the work.
3079  */
3080  state = BipartiteMatch(num_sets, num_sets, adjacency);
3081 
3082  /*
3083  * Now, the state->pair* fields have the info we need to assign sets to
3084  * chains. Two sets (u,v) belong to the same chain if pair_uv[u] = v or
3085  * pair_vu[v] = u (both will be true, but we check both so that we can do
3086  * it in one pass)
3087  */
3088  chains = palloc0((num_sets + 1) * sizeof(int));
3089 
3090  for (i = 1; i <= num_sets; ++i)
3091  {
3092  int u = state->pair_vu[i];
3093  int v = state->pair_uv[i];
3094 
3095  if (u > 0 && u < i)
3096  chains[i] = chains[u];
3097  else if (v > 0 && v < i)
3098  chains[i] = chains[v];
3099  else
3100  chains[i] = ++num_chains;
3101  }
3102 
3103  /* build result lists. */
3104  results = palloc0((num_chains + 1) * sizeof(List *));
3105 
3106  for (i = 1; i <= num_sets; ++i)
3107  {
3108  int c = chains[i];
3109 
3110  Assert(c > 0);
3111 
3112  results[c] = list_concat(results[c], orig_sets[i]);
3113  }
3114 
3115  /* push any empty sets back on the first list. */
3116  while (num_empty-- > 0)
3117  results[1] = lcons(NIL, results[1]);
3118 
3119  /* make result list */
3120  for (i = 1; i <= num_chains; ++i)
3121  result = lappend(result, results[i]);
3122 
3123  /*
3124  * Free all the things.
3125  *
3126  * (This is over-fussy for small sets but for large sets we could have
3127  * tied up a nontrivial amount of memory.)
3128  */
3130  pfree(results);
3131  pfree(chains);
3132  for (i = 1; i <= num_sets; ++i)
3133  if (adjacency[i])
3134  pfree(adjacency[i]);
3135  pfree(adjacency);
3136  pfree(adjacency_buf);
3137  pfree(orig_sets);
3138  for (i = 1; i <= num_sets; ++i)
3139  bms_free(set_masks[i]);
3140  pfree(set_masks);
3141 
3142  return result;
3143 }
3144 
3145 /*
3146  * Reorder the elements of a list of grouping sets such that they have correct
3147  * prefix relationships. Also inserts the GroupingSetData annotations.
3148  *
3149  * The input must be ordered with smallest sets first; the result is returned
3150  * with largest sets first. Note that the result shares no list substructure
3151  * with the input, so it's safe for the caller to modify it later.
3152  *
3153  * If we're passed in a sortclause, we follow its order of columns to the
3154  * extent possible, to minimize the chance that we add unnecessary sorts.
3155  * (We're trying here to ensure that GROUPING SETS ((a,b,c),(c)) ORDER BY c,b,a
3156  * gets implemented in one pass.)
3157  */
3158 static List *
3159 reorder_grouping_sets(List *groupingSets, List *sortclause)
3160 {
3161  ListCell *lc;
3162  List *previous = NIL;
3163  List *result = NIL;
3164 
3165  foreach(lc, groupingSets)
3166  {
3167  List *candidate = (List *) lfirst(lc);
3168  List *new_elems = list_difference_int(candidate, previous);
3170 
3171  while (list_length(sortclause) > list_length(previous) &&
3172  new_elems != NIL)
3173  {
3174  SortGroupClause *sc = list_nth(sortclause, list_length(previous));
3175  int ref = sc->tleSortGroupRef;
3176 
3177  if (list_member_int(new_elems, ref))
3178  {
3179  previous = lappend_int(previous, ref);
3180  new_elems = list_delete_int(new_elems, ref);
3181  }
3182  else
3183  {
3184  /* diverged from the sortclause; give up on it */
3185  sortclause = NIL;
3186  break;
3187  }
3188  }
3189 
3190  previous = list_concat(previous, new_elems);
3191 
3192  gs->set = list_copy(previous);
3193  result = lcons(gs, result);
3194  }
3195 
3196  list_free(previous);
3197 
3198  return result;
3199 }
3200 
3201 /*
3202  * has_volatile_pathkey
3203  * Returns true if any PathKey in 'keys' has an EquivalenceClass
3204  * containing a volatile function. Otherwise returns false.
3205  */
3206 static bool
3208 {
3209  ListCell *lc;
3210 
3211  foreach(lc, keys)
3212  {
3213  PathKey *pathkey = lfirst_node(PathKey, lc);
3214 
3215  if (pathkey->pk_eclass->ec_has_volatile)
3216  return true;
3217  }
3218 
3219  return false;
3220 }
3221 
3222 /*
3223  * adjust_group_pathkeys_for_groupagg
3224  * Add pathkeys to root->group_pathkeys to reflect the best set of
3225  * pre-ordered input for ordered aggregates.
3226  *
3227  * We define "best" as the pathkeys that suit the largest number of
3228  * aggregate functions. We find these by looking at the first ORDER BY /
3229  * DISTINCT aggregate and take the pathkeys for that before searching for
3230  * other aggregates that require the same or a more strict variation of the
3231  * same pathkeys. We then repeat that process for any remaining aggregates
3232  * with different pathkeys and if we find another set of pathkeys that suits a
3233  * larger number of aggregates then we select those pathkeys instead.
3234  *
3235  * When the best pathkeys are found we also mark each Aggref that can use
3236  * those pathkeys as aggpresorted = true.
3237  *
3238  * Note: When an aggregate function's ORDER BY / DISTINCT clause contains any
3239  * volatile functions, we never make use of these pathkeys. We want to ensure
3240  * that sorts using volatile functions are done independently in each Aggref
3241  * rather than once at the query level. If we were to allow this then Aggrefs
3242  * with compatible sort orders would all transition their rows in the same
3243  * order if those pathkeys were deemed to be the best pathkeys to sort on.
3244  * Whereas, if some other set of Aggref's pathkeys happened to be deemed
3245  * better pathkeys to sort on, then the volatile function Aggrefs would be
3246  * left to perform their sorts individually. To avoid this inconsistent
3247  * behavior which could make Aggref results depend on what other Aggrefs the
3248  * query contains, we always force Aggrefs with volatile functions to perform
3249  * their own sorts.
3250  */
3251 static void
3253 {
3254  List *grouppathkeys = root->group_pathkeys;
3255  List *bestpathkeys;
3256  Bitmapset *bestaggs;
3257  Bitmapset *unprocessed_aggs;
3258  ListCell *lc;
3259  int i;
3260 
3261  /* Shouldn't be here if there are grouping sets */
3262  Assert(root->parse->groupingSets == NIL);
3263  /* Shouldn't be here unless there are some ordered aggregates */
3264  Assert(root->numOrderedAggs > 0);
3265 
3266  /* Do nothing if disabled */
3268  return;
3269 
3270  /*
3271  * Make a first pass over all AggInfos to collect a Bitmapset containing
3272  * the indexes of all AggInfos to be processed below.
3273  */
3274  unprocessed_aggs = NULL;
3275  foreach(lc, root->agginfos)
3276  {
3277  AggInfo *agginfo = lfirst_node(AggInfo, lc);
3278  Aggref *aggref = linitial_node(Aggref, agginfo->aggrefs);
3279 
3280  if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
3281  continue;
3282 
3283  /* only add aggregates with a DISTINCT or ORDER BY */
3284  if (aggref->aggdistinct != NIL || aggref->aggorder != NIL)
3285  unprocessed_aggs = bms_add_member(unprocessed_aggs,
3286  foreach_current_index(lc));
3287  }
3288 
3289  /*
3290  * Now process all the unprocessed_aggs to find the best set of pathkeys
3291  * for the given set of aggregates.
3292  *
3293  * On the first outer loop here 'bestaggs' will be empty. We'll populate
3294  * this during the first loop using the pathkeys for the very first
3295  * AggInfo then taking any stronger pathkeys from any other AggInfos with
3296  * a more strict set of compatible pathkeys. Once the outer loop is
3297  * complete, we mark off all the aggregates with compatible pathkeys then
3298  * remove those from the unprocessed_aggs and repeat the process to try to
3299  * find another set of pathkeys that are suitable for a larger number of
3300  * aggregates. The outer loop will stop when there are not enough
3301  * unprocessed aggregates for it to be possible to find a set of pathkeys
3302  * to suit a larger number of aggregates.
3303  */
3304  bestpathkeys = NIL;
3305  bestaggs = NULL;
3306  while (bms_num_members(unprocessed_aggs) > bms_num_members(bestaggs))
3307  {
3308  Bitmapset *aggindexes = NULL;
3309  List *currpathkeys = NIL;
3310 
3311  i = -1;
3312  while ((i = bms_next_member(unprocessed_aggs, i)) >= 0)
3313  {
3314  AggInfo *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3315  Aggref *aggref = linitial_node(Aggref, agginfo->aggrefs);
3316  List *sortlist;
3317  List *pathkeys;
3318 
3319  if (aggref->aggdistinct != NIL)
3320  sortlist = aggref->aggdistinct;
3321  else
3322  sortlist = aggref->aggorder;
3323 
3324  pathkeys = make_pathkeys_for_sortclauses(root, sortlist,
3325  aggref->args);
3326 
3327  /*
3328  * Ignore Aggrefs which have volatile functions in their ORDER BY
3329  * or DISTINCT clause.
3330  */
3331  if (has_volatile_pathkey(pathkeys))
3332  {
3333  unprocessed_aggs = bms_del_member(unprocessed_aggs, i);
3334  continue;
3335  }
3336 
3337  /*
3338  * When not set yet, take the pathkeys from the first unprocessed
3339  * aggregate.
3340  */
3341  if (currpathkeys == NIL)
3342  {
3343  currpathkeys = pathkeys;
3344 
3345  /* include the GROUP BY pathkeys, if they exist */
3346  if (grouppathkeys != NIL)
3347  currpathkeys = append_pathkeys(list_copy(grouppathkeys),
3348  currpathkeys);
3349 
3350  /* record that we found pathkeys for this aggregate */
3351  aggindexes = bms_add_member(aggindexes, i);
3352  }
3353  else
3354  {
3355  /* now look for a stronger set of matching pathkeys */
3356 
3357  /* include the GROUP BY pathkeys, if they exist */
3358  if (grouppathkeys != NIL)
3359  pathkeys = append_pathkeys(list_copy(grouppathkeys),
3360  pathkeys);
3361 
3362  /* are 'pathkeys' compatible or better than 'currpathkeys'? */
3363  switch (compare_pathkeys(currpathkeys, pathkeys))
3364  {
3365  case PATHKEYS_BETTER2:
3366  /* 'pathkeys' are stronger, use these ones instead */
3367  currpathkeys = pathkeys;
3368  /* FALLTHROUGH */
3369 
3370  case PATHKEYS_BETTER1:
3371  /* 'pathkeys' are less strict */
3372  /* FALLTHROUGH */
3373 
3374  case PATHKEYS_EQUAL:
3375  /* mark this aggregate as covered by 'currpathkeys' */
3376  aggindexes = bms_add_member(aggindexes, i);
3377  break;
3378 
3379  case PATHKEYS_DIFFERENT:
3380  break;
3381  }
3382  }
3383  }
3384 
3385  /* remove the aggregates that we've just processed */
3386  unprocessed_aggs = bms_del_members(unprocessed_aggs, aggindexes);
3387 
3388  /*
3389  * If this pass included more aggregates than the previous best then
3390  * use these ones as the best set.
3391  */
3392  if (bms_num_members(aggindexes) > bms_num_members(bestaggs))
3393  {
3394  bestaggs = aggindexes;
3395  bestpathkeys = currpathkeys;
3396  }
3397  }
3398 
3399  /*
3400  * If we found any ordered aggregates, update root->group_pathkeys to add
3401  * the best set of aggregate pathkeys. Note that bestpathkeys includes
3402  * the original GROUP BY pathkeys already.
3403  */
3404  if (bestpathkeys != NIL)
3405  root->group_pathkeys = bestpathkeys;
3406 
3407  /*
3408  * Now that we've found the best set of aggregates we can set the
3409  * presorted flag to indicate to the executor that it needn't bother
3410  * performing a sort for these Aggrefs. We're able to do this now as
3411  * there's no chance of a Hash Aggregate plan as create_grouping_paths
3412  * will not mark the GROUP BY as GROUPING_CAN_USE_HASH due to the presence
3413  * of ordered aggregates.
3414  */
3415  i = -1;
3416  while ((i = bms_next_member(bestaggs, i)) >= 0)
3417  {
3418  AggInfo *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3419 
3420  foreach(lc, agginfo->aggrefs)
3421  {
3422  Aggref *aggref = lfirst_node(Aggref, lc);
3423 
3424  aggref->aggpresorted = true;
3425  }
3426  }
3427 }
3428 
3429 /*
3430  * Compute query_pathkeys and other pathkeys during plan generation
3431  */
3432 static void
3434 {
3435  Query *parse = root->parse;
3436  standard_qp_extra *qp_extra = (standard_qp_extra *) extra;
3437  List *tlist = root->processed_tlist;
3438  List *activeWindows = qp_extra->activeWindows;
3439 
3440  /*
3441  * Calculate pathkeys that represent grouping/ordering and/or ordered
3442  * aggregate requirements.
3443  */
3444  if (qp_extra->gset_data)
3445  {
3446  /*
3447  * With grouping sets, just use the first RollupData's groupClause. We
3448  * don't make any effort to optimize grouping clauses when there are
3449  * grouping sets, nor can we combine aggregate ordering keys with
3450  * grouping.
3451  */
3452  List *rollups = qp_extra->gset_data->rollups;
3453  List *groupClause = (rollups ? linitial_node(RollupData, rollups)->groupClause : NIL);
3454 
3455  if (grouping_is_sortable(groupClause))
3456  {
3457  root->group_pathkeys = make_pathkeys_for_sortclauses(root,
3458  groupClause,
3459  tlist);
3460  root->num_groupby_pathkeys = list_length(root->group_pathkeys);
3461  }
3462  else
3463  {
3464  root->group_pathkeys = NIL;
3465  root->num_groupby_pathkeys = 0;
3466  }
3467  }
3468  else if (parse->groupClause || root->numOrderedAggs > 0)
3469  {
3470  /*
3471  * With a plain GROUP BY list, we can remove any grouping items that
3472  * are proven redundant by EquivalenceClass processing. For example,
3473  * we can remove y given "WHERE x = y GROUP BY x, y". These aren't
3474  * especially common cases, but they're nearly free to detect. Note
3475  * that we remove redundant items from processed_groupClause but not
3476  * the original parse->groupClause.
3477  */
3478  bool sortable;
3479 
3480  /*
3481  * Convert group clauses into pathkeys. Set the ec_sortref field of
3482  * EquivalenceClass'es if it's not set yet.
3483  */
3484  root->group_pathkeys =
3486  &root->processed_groupClause,
3487  tlist,
3488  true,
3489  &sortable,
3490  true);
3491  if (!sortable)
3492  {
3493  /* Can't sort; no point in considering aggregate ordering either */
3494  root->group_pathkeys = NIL;
3495  root->num_groupby_pathkeys = 0;
3496  }
3497  else
3498  {
3499  root->num_groupby_pathkeys = list_length(root->group_pathkeys);
3500  /* If we have ordered aggs, consider adding onto group_pathkeys */
3501  if (root->numOrderedAggs > 0)
3503  }
3504  }
3505  else
3506  {
3507  root->group_pathkeys = NIL;
3508  root->num_groupby_pathkeys = 0;
3509  }
3510 
3511  /* We consider only the first (bottom) window in pathkeys logic */
3512  if (activeWindows != NIL)
3513  {
3514  WindowClause *wc = linitial_node(WindowClause, activeWindows);
3515 
3516  root->window_pathkeys = make_pathkeys_for_window(root,
3517  wc,
3518  tlist);
3519  }
3520  else
3521  root->window_pathkeys = NIL;
3522 
3523  /*
3524  * As with GROUP BY, we can discard any DISTINCT items that are proven
3525  * redundant by EquivalenceClass processing. The non-redundant list is
3526  * kept in root->processed_distinctClause, leaving the original
3527  * parse->distinctClause alone.
3528  */
3529  if (parse->distinctClause)
3530  {
3531  bool sortable;
3532 
3533  /* Make a copy since pathkey processing can modify the list */
3534  root->processed_distinctClause = list_copy(parse->distinctClause);
3535  root->distinct_pathkeys =
3537  &root->processed_distinctClause,
3538  tlist,
3539  true,
3540  &sortable,
3541  false);
3542  if (!sortable)
3543  root->distinct_pathkeys = NIL;
3544  }
3545  else
3546  root->distinct_pathkeys = NIL;
3547 
3548  root->sort_pathkeys =
3550  parse->sortClause,
3551  tlist);
3552 
3553  /* setting setop_pathkeys might be useful to the union planner */
3554  if (qp_extra->setop != NULL &&
3556  {
3557  List *groupClauses;
3558  bool sortable;
3559 
3560  groupClauses = generate_setop_child_grouplist(qp_extra->setop, tlist);
3561 
3562  root->setop_pathkeys =
3564  &groupClauses,
3565  tlist,
3566  false,
3567  &sortable,
3568  false);
3569  if (!sortable)
3570  root->setop_pathkeys = NIL;
3571  }
3572  else
3573  root->setop_pathkeys = NIL;
3574 
3575  /*
3576  * Figure out whether we want a sorted result from query_planner.
3577  *
3578  * If we have a sortable GROUP BY clause, then we want a result sorted
3579  * properly for grouping. Otherwise, if we have window functions to
3580  * evaluate, we try to sort for the first window. Otherwise, if there's a
3581  * sortable DISTINCT clause that's more rigorous than the ORDER BY clause,
3582  * we try to produce output that's sufficiently well sorted for the
3583  * DISTINCT. Otherwise, if there is an ORDER BY clause, we want to sort
3584  * by the ORDER BY clause. Otherwise, if we're a subquery being planned
3585  * for a set operation which can benefit from presorted results and have a
3586  * sortable targetlist, we want to sort by the target list.
3587  *
3588  * Note: if we have both ORDER BY and GROUP BY, and ORDER BY is a superset
3589  * of GROUP BY, it would be tempting to request sort by ORDER BY --- but
3590  * that might just leave us failing to exploit an available sort order at
3591  * all. Needs more thought. The choice for DISTINCT versus ORDER BY is
3592  * much easier, since we know that the parser ensured that one is a
3593  * superset of the other.
3594  */
3595  if (root->group_pathkeys)
3596  root->query_pathkeys = root->group_pathkeys;
3597  else if (root->window_pathkeys)
3598  root->query_pathkeys = root->window_pathkeys;
3599  else if (list_length(root->distinct_pathkeys) >
3600  list_length(root->sort_pathkeys))
3601  root->query_pathkeys = root->distinct_pathkeys;
3602  else if (root->sort_pathkeys)
3603  root->query_pathkeys = root->sort_pathkeys;
3604  else if (root->setop_pathkeys != NIL)
3605  root->query_pathkeys = root->setop_pathkeys;
3606  else
3607  root->query_pathkeys = NIL;
3608 }
3609 
3610 /*
3611  * Estimate number of groups produced by grouping clauses (1 if not grouping)
3612  *
3613  * path_rows: number of output rows from scan/join step
3614  * gd: grouping sets data including list of grouping sets and their clauses
3615  * target_list: target list containing group clause references
3616  *
3617  * If doing grouping sets, we also annotate the gsets data with the estimates
3618  * for each set and each individual rollup list, with a view to later
3619  * determining whether some combination of them could be hashed instead.
3620  */
3621 static double
3623  double path_rows,
3624  grouping_sets_data *gd,
3625  List *target_list)
3626 {
3627  Query *parse = root->parse;
3628  double dNumGroups;
3629 
3630  if (parse->groupClause)
3631  {
3632  List *groupExprs;
3633 
3634  if (parse->groupingSets)
3635  {
3636  /* Add up the estimates for each grouping set */
3637  ListCell *lc;
3638 
3639  Assert(gd); /* keep Coverity happy */
3640 
3641  dNumGroups = 0;
3642 
3643  foreach(lc, gd->rollups)
3644  {
3645  RollupData *rollup = lfirst_node(RollupData, lc);
3646  ListCell *lc2;
3647  ListCell *lc3;
3648 
3649  groupExprs = get_sortgrouplist_exprs(rollup->groupClause,
3650  target_list);
3651 
3652  rollup->numGroups = 0.0;
3653 
3654  forboth(lc2, rollup->gsets, lc3, rollup->gsets_data)
3655  {
3656  List *gset = (List *) lfirst(lc2);
3658  double numGroups = estimate_num_groups(root,
3659  groupExprs,
3660  path_rows,
3661  &gset,
3662  NULL);
3663 
3664  gs->numGroups = numGroups;
3665  rollup->numGroups += numGroups;
3666  }
3667 
3668  dNumGroups += rollup->numGroups;
3669  }
3670 
3671  if (gd->hash_sets_idx)
3672  {
3673  ListCell *lc2;
3674 
3675  gd->dNumHashGroups = 0;
3676 
3677  groupExprs = get_sortgrouplist_exprs(parse->groupClause,
3678  target_list);
3679 
3680  forboth(lc, gd->hash_sets_idx, lc2, gd->unsortable_sets)
3681  {
3682  List *gset = (List *) lfirst(lc);
3684  double numGroups = estimate_num_groups(root,
3685  groupExprs,
3686  path_rows,
3687  &gset,
3688  NULL);
3689 
3690  gs->numGroups = numGroups;
3691  gd->dNumHashGroups += numGroups;
3692  }
3693 
3694  dNumGroups += gd->dNumHashGroups;
3695  }
3696  }
3697  else
3698  {
3699  /* Plain GROUP BY -- estimate based on optimized groupClause */
3700  groupExprs = get_sortgrouplist_exprs(root->processed_groupClause,
3701  target_list);
3702 
3703  dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
3704  NULL, NULL);
3705  }
3706  }
3707  else if (parse->groupingSets)
3708  {
3709  /* Empty grouping sets ... one result row for each one */
3710  dNumGroups = list_length(parse->groupingSets);
3711  }
3712  else if (parse->hasAggs || root->hasHavingQual)
3713  {
3714  /* Plain aggregation, one result row */
3715  dNumGroups = 1;
3716  }
3717  else
3718  {
3719  /* Not grouping */
3720  dNumGroups = 1;
3721  }
3722 
3723  return dNumGroups;
3724 }
3725 
3726 /*
3727  * create_grouping_paths
3728  *
3729  * Build a new upperrel containing Paths for grouping and/or aggregation.
3730  * Along the way, we also build an upperrel for Paths which are partially
3731  * grouped and/or aggregated. A partially grouped and/or aggregated path
3732  * needs a FinalizeAggregate node to complete the aggregation. Currently,
3733  * the only partially grouped paths we build are also partial paths; that
3734  * is, they need a Gather and then a FinalizeAggregate.
3735  *
3736  * input_rel: contains the source-data Paths
3737  * target: the pathtarget for the result Paths to compute
3738  * gd: grouping sets data including list of grouping sets and their clauses
3739  *
3740  * Note: all Paths in input_rel are expected to return the target computed
3741  * by make_group_input_target.
3742  */
3743 static RelOptInfo *
3745  RelOptInfo *input_rel,
3746  PathTarget *target,
3747  bool target_parallel_safe,
3748  grouping_sets_data *gd)
3749 {
3750  Query *parse = root->parse;
3751  RelOptInfo *grouped_rel;
3752  RelOptInfo *partially_grouped_rel;
3753  AggClauseCosts agg_costs;
3754 
3755  MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
3757 
3758  /*
3759  * Create grouping relation to hold fully aggregated grouping and/or
3760  * aggregation paths.
3761  */
3762  grouped_rel = make_grouping_rel(root, input_rel, target,
3763  target_parallel_safe, parse->havingQual);
3764 
3765  /*
3766  * Create either paths for a degenerate grouping or paths for ordinary
3767  * grouping, as appropriate.
3768  */
3770  create_degenerate_grouping_paths(root, input_rel, grouped_rel);
3771  else
3772  {
3773  int flags = 0;
3774  GroupPathExtraData extra;
3775 
3776  /*
3777  * Determine whether it's possible to perform sort-based
3778  * implementations of grouping. (Note that if processed_groupClause
3779  * is empty, grouping_is_sortable() is trivially true, and all the
3780  * pathkeys_contained_in() tests will succeed too, so that we'll
3781  * consider every surviving input path.)
3782  *
3783  * If we have grouping sets, we might be able to sort some but not all
3784  * of them; in this case, we need can_sort to be true as long as we
3785  * must consider any sorted-input plan.
3786  */
3787  if ((gd && gd->rollups != NIL)
3788  || grouping_is_sortable(root->processed_groupClause))
3789  flags |= GROUPING_CAN_USE_SORT;
3790 
3791  /*
3792  * Determine whether we should consider hash-based implementations of
3793  * grouping.
3794  *
3795  * Hashed aggregation only applies if we're grouping. If we have
3796  * grouping sets, some groups might be hashable but others not; in
3797  * this case we set can_hash true as long as there is nothing globally
3798  * preventing us from hashing (and we should therefore consider plans
3799  * with hashes).
3800  *
3801  * Executor doesn't support hashed aggregation with DISTINCT or ORDER
3802  * BY aggregates. (Doing so would imply storing *all* the input
3803  * values in the hash table, and/or running many sorts in parallel,
3804  * either of which seems like a certain loser.) We similarly don't
3805  * support ordered-set aggregates in hashed aggregation, but that case
3806  * is also included in the numOrderedAggs count.
3807  *
3808  * Note: grouping_is_hashable() is much more expensive to check than
3809  * the other gating conditions, so we want to do it last.
3810  */
3811  if ((parse->groupClause != NIL &&
3812  root->numOrderedAggs == 0 &&
3813  (gd ? gd->any_hashable : grouping_is_hashable(root->processed_groupClause))))
3814  flags |= GROUPING_CAN_USE_HASH;
3815 
3816  /*
3817  * Determine whether partial aggregation is possible.
3818  */
3819  if (can_partial_agg(root))
3820  flags |= GROUPING_CAN_PARTIAL_AGG;
3821 
3822  extra.flags = flags;
3823  extra.target_parallel_safe = target_parallel_safe;
3824  extra.havingQual = parse->havingQual;
3825  extra.targetList = parse->targetList;
3826  extra.partial_costs_set = false;
3827 
3828  /*
3829  * Determine whether partitionwise aggregation is in theory possible.
3830  * It can be disabled by the user, and for now, we don't try to
3831  * support grouping sets. create_ordinary_grouping_paths() will check
3832  * additional conditions, such as whether input_rel is partitioned.
3833  */
3834  if (enable_partitionwise_aggregate && !parse->groupingSets)
3836  else
3838 
3839  create_ordinary_grouping_paths(root, input_rel, grouped_rel,
3840  &agg_costs, gd, &extra,
3841  &partially_grouped_rel);
3842  }
3843 
3844  set_cheapest(grouped_rel);
3845  return grouped_rel;
3846 }
3847 
3848 /*
3849  * make_grouping_rel
3850  *
3851  * Create a new grouping rel and set basic properties.
3852  *
3853  * input_rel represents the underlying scan/join relation.
3854  * target is the output expected from the grouping relation.
3855  */
3856 static RelOptInfo *
3858  PathTarget *target, bool target_parallel_safe,
3859  Node *havingQual)
3860 {
3861  RelOptInfo *grouped_rel;
3862 
3863  if (IS_OTHER_REL(input_rel))
3864  {
3865  grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG,
3866  input_rel->relids);
3867  grouped_rel->reloptkind = RELOPT_OTHER_UPPER_REL;
3868  }
3869  else
3870  {
3871  /*
3872  * By tradition, the relids set for the main grouping relation is
3873  * NULL. (This could be changed, but might require adjustments
3874  * elsewhere.)
3875  */
3876  grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
3877  }
3878 
3879  /* Set target. */
3880  grouped_rel->reltarget = target;
3881 
3882  /*
3883  * If the input relation is not parallel-safe, then the grouped relation
3884  * can't be parallel-safe, either. Otherwise, it's parallel-safe if the
3885  * target list and HAVING quals are parallel-safe.
3886  */
3887  if (input_rel->consider_parallel && target_parallel_safe &&
3888  is_parallel_safe(root, (Node *) havingQual))
3889  grouped_rel->consider_parallel = true;
3890 
3891  /*
3892  * If the input rel belongs to a single FDW, so does the grouped rel.
3893  */
3894  grouped_rel->serverid = input_rel->serverid;
3895  grouped_rel->userid = input_rel->userid;
3896  grouped_rel->useridiscurrent = input_rel->useridiscurrent;
3897  grouped_rel->fdwroutine = input_rel->fdwroutine;
3898 
3899  return grouped_rel;
3900 }
3901 
3902 /*
3903  * is_degenerate_grouping
3904  *
3905  * A degenerate grouping is one in which the query has a HAVING qual and/or
3906  * grouping sets, but no aggregates and no GROUP BY (which implies that the
3907  * grouping sets are all empty).
3908  */
3909 static bool
3911 {
3912  Query *parse = root->parse;
3913 
3914  return (root->hasHavingQual || parse->groupingSets) &&
3915  !parse->hasAggs && parse->groupClause == NIL;
3916 }
3917 
3918 /*
3919  * create_degenerate_grouping_paths
3920  *
3921  * When the grouping is degenerate (see is_degenerate_grouping), we are
3922  * supposed to emit either zero or one row for each grouping set depending on
3923  * whether HAVING succeeds. Furthermore, there cannot be any variables in
3924  * either HAVING or the targetlist, so we actually do not need the FROM table
3925  * at all! We can just throw away the plan-so-far and generate a Result node.
3926  * This is a sufficiently unusual corner case that it's not worth contorting
3927  * the structure of this module to avoid having to generate the earlier paths
3928  * in the first place.
3929  */
3930 static void
3932  RelOptInfo *grouped_rel)
3933 {
3934  Query *parse = root->parse;
3935  int nrows;
3936  Path *path;
3937 
3938  nrows = list_length(parse->groupingSets);
3939  if (nrows > 1)
3940  {
3941  /*
3942  * Doesn't seem worthwhile writing code to cons up a generate_series
3943  * or a values scan to emit multiple rows. Instead just make N clones
3944  * and append them. (With a volatile HAVING clause, this means you
3945  * might get between 0 and N output rows. Offhand I think that's
3946  * desired.)
3947  */
3948  List *paths = NIL;
3949 
3950  while (--nrows >= 0)
3951  {
3952  path = (Path *)
3953  create_group_result_path(root, grouped_rel,
3954  grouped_rel->reltarget,
3955  (List *) parse->havingQual);
3956  paths = lappend(paths, path);
3957  }
3958  path = (Path *)
3960  grouped_rel,
3961  paths,
3962  NIL,
3963  NIL,
3964  NULL,
3965  0,
3966  false,
3967  -1);
3968  }
3969  else
3970  {
3971  /* No grouping sets, or just one, so one output row */
3972  path = (Path *)
3973  create_group_result_path(root, grouped_rel,
3974  grouped_rel->reltarget,
3975  (List *) parse->havingQual);
3976  }
3977 
3978  add_path(grouped_rel, path);
3979 }
3980 
3981 /*
3982  * create_ordinary_grouping_paths
3983  *
3984  * Create grouping paths for the ordinary (that is, non-degenerate) case.
3985  *
3986  * We need to consider sorted and hashed aggregation in the same function,
3987  * because otherwise (1) it would be harder to throw an appropriate error
3988  * message if neither way works, and (2) we should not allow hashtable size
3989  * considerations to dissuade us from using hashing if sorting is not possible.
3990  *
3991  * *partially_grouped_rel_p will be set to the partially grouped rel which this
3992  * function creates, or to NULL if it doesn't create one.
3993  */
3994 static void
3996  RelOptInfo *grouped_rel,
3997  const AggClauseCosts *agg_costs,
3998  grouping_sets_data *gd,
3999  GroupPathExtraData *extra,
4000  RelOptInfo **partially_grouped_rel_p)
4001 {
4002  Path *cheapest_path = input_rel->cheapest_total_path;
4003  RelOptInfo *partially_grouped_rel = NULL;
4004  double dNumGroups;
4006 
4007  /*
4008  * If this is the topmost grouping relation or if the parent relation is
4009  * doing some form of partitionwise aggregation, then we may be able to do
4010  * it at this level also. However, if the input relation is not
4011  * partitioned, partitionwise aggregate is impossible.
4012  */
4013  if (extra->patype != PARTITIONWISE_AGGREGATE_NONE &&
4014  IS_PARTITIONED_REL(input_rel))
4015  {
4016  /*
4017  * If this is the topmost relation or if the parent relation is doing
4018  * full partitionwise aggregation, then we can do full partitionwise
4019  * aggregation provided that the GROUP BY clause contains all of the
4020  * partitioning columns at this level. Otherwise, we can do at most
4021  * partial partitionwise aggregation. But if partial aggregation is
4022  * not supported in general then we can't use it for partitionwise
4023  * aggregation either.
4024  *
4025  * Check parse->groupClause not processed_groupClause, because it's
4026  * okay if some of the partitioning columns were proved redundant.
4027  */
4028  if (extra->patype == PARTITIONWISE_AGGREGATE_FULL &&
4029  group_by_has_partkey(input_rel, extra->targetList,
4030  root->parse->groupClause))
4032  else if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
4034  else
4036  }
4037 
4038  /*
4039  * Before generating paths for grouped_rel, we first generate any possible
4040  * partially grouped paths; that way, later code can easily consider both
4041  * parallel and non-parallel approaches to grouping.
4042  */
4043  if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
4044  {
4045  bool force_rel_creation;
4046 
4047  /*
4048  * If we're doing partitionwise aggregation at this level, force
4049  * creation of a partially_grouped_rel so we can add partitionwise
4050  * paths to it.
4051  */
4052  force_rel_creation = (patype == PARTITIONWISE_AGGREGATE_PARTIAL);
4053 
4054  partially_grouped_rel =
4056  grouped_rel,
4057  input_rel,
4058  gd,
4059  extra,
4060  force_rel_creation);
4061  }
4062 
4063  /* Set out parameter. */
4064  *partially_grouped_rel_p = partially_grouped_rel;
4065 
4066  /* Apply partitionwise aggregation technique, if possible. */
4067  if (patype != PARTITIONWISE_AGGREGATE_NONE)
4068  create_partitionwise_grouping_paths(root, input_rel, grouped_rel,
4069  partially_grouped_rel, agg_costs,
4070  gd, patype, extra);
4071 
4072  /* If we are doing partial aggregation only, return. */
4074  {
4075  Assert(partially_grouped_rel);
4076 
4077  if (partially_grouped_rel->pathlist)
4078  set_cheapest(partially_grouped_rel);
4079 
4080  return;
4081  }
4082 
4083  /* Gather any partially grouped partial paths. */
4084  if (partially_grouped_rel && partially_grouped_rel->partial_pathlist)
4085  {
4086  gather_grouping_paths(root, partially_grouped_rel);
4087  set_cheapest(partially_grouped_rel);
4088  }
4089 
4090  /*
4091  * Estimate number of groups.
4092  */
4093  dNumGroups = get_number_of_groups(root,
4094  cheapest_path->rows,
4095  gd,
4096  extra->targetList);
4097 
4098  /* Build final grouping paths */
4099  add_paths_to_grouping_rel(root, input_rel, grouped_rel,
4100  partially_grouped_rel, agg_costs, gd,
4101  dNumGroups, extra);
4102 
4103  /* Give a helpful error if we failed to find any implementation */
4104  if (grouped_rel->pathlist == NIL)
4105  ereport(ERROR,
4106  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4107  errmsg("could not implement GROUP BY"),
4108  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
4109 
4110  /*
4111  * If there is an FDW that's responsible for all baserels of the query,
4112  * let it consider adding ForeignPaths.
4113  */
4114  if (grouped_rel->fdwroutine &&
4115  grouped_rel->fdwroutine->GetForeignUpperPaths)
4116  grouped_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_GROUP_AGG,
4117  input_rel, grouped_rel,
4118  extra);
4119 
4120  /* Let extensions possibly add some more paths */
4122  (*create_upper_paths_hook) (root, UPPERREL_GROUP_AGG,
4123  input_rel, grouped_rel,
4124  extra);
4125 }
4126 
4127 /*
4128  * For a given input path, consider the possible ways of doing grouping sets on
4129  * it, by combinations of hashing and sorting. This can be called multiple
4130  * times, so it's important that it not scribble on input. No result is
4131  * returned, but any generated paths are added to grouped_rel.
4132  */
4133 static void
4135  RelOptInfo *grouped_rel,
4136  Path *path,
4137  bool is_sorted,
4138  bool can_hash,
4139  grouping_sets_data *gd,
4140  const AggClauseCosts *agg_costs,
4141  double dNumGroups)
4142 {
4143  Query *parse = root->parse;
4144  Size hash_mem_limit = get_hash_memory_limit();
4145 
4146  /*
4147  * If we're not being offered sorted input, then only consider plans that
4148  * can be done entirely by hashing.
4149  *
4150  * We can hash everything if it looks like it'll fit in hash_mem. But if
4151  * the input is actually sorted despite not being advertised as such, we
4152  * prefer to make use of that in order to use less memory.
4153  *
4154  * If none of the grouping sets are sortable, then ignore the hash_mem
4155  * limit and generate a path anyway, since otherwise we'll just fail.
4156  */
4157  if (!is_sorted)
4158  {
4159  List *new_rollups = NIL;
4160  RollupData *unhashed_rollup = NULL;
4161  List *sets_data;
4162  List *empty_sets_data = NIL;
4163  List *empty_sets = NIL;
4164  ListCell *lc;
4165  ListCell *l_start = list_head(gd->rollups);
4166  AggStrategy strat = AGG_HASHED;
4167  double hashsize;
4168  double exclude_groups = 0.0;
4169 
4170  Assert(can_hash);
4171 
4172  /*
4173  * If the input is coincidentally sorted usefully (which can happen
4174  * even if is_sorted is false, since that only means that our caller
4175  * has set up the sorting for us), then save some hashtable space by
4176  * making use of that. But we need to watch out for degenerate cases:
4177  *
4178  * 1) If there are any empty grouping sets, then group_pathkeys might
4179  * be NIL if all non-empty grouping sets are unsortable. In this case,
4180  * there will be a rollup containing only empty groups, and the
4181  * pathkeys_contained_in test is vacuously true; this is ok.
4182  *
4183  * XXX: the above relies on the fact that group_pathkeys is generated
4184  * from the first rollup. If we add the ability to consider multiple
4185  * sort orders for grouping input, this assumption might fail.
4186  *
4187  * 2) If there are no empty sets and only unsortable sets, then the
4188  * rollups list will be empty (and thus l_start == NULL), and
4189  * group_pathkeys will be NIL; we must ensure that the vacuously-true
4190  * pathkeys_contained_in test doesn't cause us to crash.
4191  */
4192  if (l_start != NULL &&
4193  pathkeys_contained_in(root->group_pathkeys, path->pathkeys))
4194  {
4195  unhashed_rollup = lfirst_node(RollupData, l_start);
4196  exclude_groups = unhashed_rollup->numGroups;
4197  l_start = lnext(gd->rollups, l_start);
4198  }
4199 
4200  hashsize = estimate_hashagg_tablesize(root,
4201  path,
4202  agg_costs,
4203  dNumGroups - exclude_groups);
4204 
4205  /*
4206  * gd->rollups is empty if we have only unsortable columns to work
4207  * with. Override hash_mem in that case; otherwise, we'll rely on the
4208  * sorted-input case to generate usable mixed paths.
4209  */
4210  if (hashsize > hash_mem_limit && gd->rollups)
4211  return; /* nope, won't fit */
4212 
4213  /*
4214  * We need to burst the existing rollups list into individual grouping
4215  * sets and recompute a groupClause for each set.
4216  */
4217  sets_data = list_copy(gd->unsortable_sets);
4218 
4219  for_each_cell(lc, gd->rollups, l_start)
4220  {
4221  RollupData *rollup = lfirst_node(RollupData, lc);
4222 
4223  /*
4224  * If we find an unhashable rollup that's not been skipped by the
4225  * "actually sorted" check above, we can't cope; we'd need sorted
4226  * input (with a different sort order) but we can't get that here.
4227  * So bail out; we'll get a valid path from the is_sorted case
4228  * instead.
4229  *
4230  * The mere presence of empty grouping sets doesn't make a rollup
4231  * unhashable (see preprocess_grouping_sets), we handle those
4232  * specially below.
4233  */
4234  if (!rollup->hashable)
4235  return;
4236 
4237  sets_data = list_concat(sets_data, rollup->gsets_data);
4238  }
4239  foreach(lc, sets_data)
4240  {
4242  List *gset = gs->set;
4243  RollupData *rollup;
4244 
4245  if (gset == NIL)
4246  {
4247  /* Empty grouping sets can't be hashed. */
4248  empty_sets_data = lappend(empty_sets_data, gs);
4249  empty_sets = lappend(empty_sets, NIL);
4250  }
4251  else
4252  {
4253  rollup = makeNode(RollupData);
4254 
4255  rollup->groupClause = preprocess_groupclause(root, gset);
4256  rollup->gsets_data = list_make1(gs);
4257  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4258  rollup->gsets_data,
4259  gd->tleref_to_colnum_map);
4260  rollup->numGroups = gs->numGroups;
4261  rollup->hashable = true;
4262  rollup->is_hashed = true;
4263  new_rollups = lappend(new_rollups, rollup);
4264  }
4265  }
4266 
4267  /*
4268  * If we didn't find anything nonempty to hash, then bail. We'll
4269  * generate a path from the is_sorted case.
4270  */
4271  if (new_rollups == NIL)
4272  return;
4273 
4274  /*
4275  * If there were empty grouping sets they should have been in the
4276  * first rollup.
4277  */
4278  Assert(!unhashed_rollup || !empty_sets);
4279 
4280  if (unhashed_rollup)
4281  {
4282  new_rollups = lappend(new_rollups, unhashed_rollup);
4283  strat = AGG_MIXED;
4284  }
4285  else if (empty_sets)
4286  {
4287  RollupData *rollup = makeNode(RollupData);
4288 
4289  rollup->groupClause = NIL;
4290  rollup->gsets_data = empty_sets_data;
4291  rollup->gsets = empty_sets;
4292  rollup->numGroups = list_length(empty_sets);
4293  rollup->hashable = false;
4294  rollup->is_hashed = false;
4295  new_rollups = lappend(new_rollups, rollup);
4296  strat = AGG_MIXED;
4297  }
4298 
4299  add_path(grouped_rel, (Path *)
4301  grouped_rel,
4302  path,
4303  (List *) parse->havingQual,
4304  strat,
4305  new_rollups,
4306  agg_costs));
4307  return;
4308  }
4309 
4310  /*
4311  * If we have sorted input but nothing we can do with it, bail.
4312  */
4313  if (gd->rollups == NIL)
4314  return;
4315 
4316  /*
4317  * Given sorted input, we try and make two paths: one sorted and one mixed
4318  * sort/hash. (We need to try both because hashagg might be disabled, or
4319  * some columns might not be sortable.)
4320  *
4321  * can_hash is passed in as false if some obstacle elsewhere (such as
4322  * ordered aggs) means that we shouldn't consider hashing at all.
4323  */
4324  if (can_hash && gd->any_hashable)
4325  {
4326  List *rollups = NIL;
4327  List *hash_sets = list_copy(gd->unsortable_sets);
4328  double availspace = hash_mem_limit;
4329  ListCell *lc;
4330 
4331  /*
4332  * Account first for space needed for groups we can't sort at all.
4333  */
4334  availspace -= estimate_hashagg_tablesize(root,
4335  path,
4336  agg_costs,
4337  gd->dNumHashGroups);
4338 
4339  if (availspace > 0 && list_length(gd->rollups) > 1)
4340  {
4341  double scale;
4342  int num_rollups = list_length(gd->rollups);
4343  int k_capacity;
4344  int *k_weights = palloc(num_rollups * sizeof(int));
4345  Bitmapset *hash_items = NULL;
4346  int i;
4347 
4348  /*
4349  * We treat this as a knapsack problem: the knapsack capacity
4350  * represents hash_mem, the item weights are the estimated memory
4351  * usage of the hashtables needed to implement a single rollup,
4352  * and we really ought to use the cost saving as the item value;
4353  * however, currently the costs assigned to sort nodes don't
4354  * reflect the comparison costs well, and so we treat all items as
4355  * of equal value (each rollup we hash instead saves us one sort).
4356  *
4357  * To use the discrete knapsack, we need to scale the values to a
4358  * reasonably small bounded range. We choose to allow a 5% error
4359  * margin; we have no more than 4096 rollups in the worst possible
4360  * case, which with a 5% error margin will require a bit over 42MB
4361  * of workspace. (Anyone wanting to plan queries that complex had
4362  * better have the memory for it. In more reasonable cases, with
4363  * no more than a couple of dozen rollups, the memory usage will
4364  * be negligible.)
4365  *
4366  * k_capacity is naturally bounded, but we clamp the values for
4367  * scale and weight (below) to avoid overflows or underflows (or
4368  * uselessly trying to use a scale factor less than 1 byte).
4369  */
4370  scale = Max(availspace / (20.0 * num_rollups), 1.0);
4371  k_capacity = (int) floor(availspace / scale);
4372 
4373  /*
4374  * We leave the first rollup out of consideration since it's the
4375  * one that matches the input sort order. We assign indexes "i"
4376  * to only those entries considered for hashing; the second loop,
4377  * below, must use the same condition.
4378  */
4379  i = 0;
4380  for_each_from(lc, gd->rollups, 1)
4381  {
4382  RollupData *rollup = lfirst_node(RollupData, lc);
4383 
4384  if (rollup->hashable)
4385  {
4386  double sz = estimate_hashagg_tablesize(root,
4387  path,
4388  agg_costs,
4389  rollup->numGroups);
4390 
4391  /*
4392  * If sz is enormous, but hash_mem (and hence scale) is
4393  * small, avoid integer overflow here.
4394  */
4395  k_weights[i] = (int) Min(floor(sz / scale),
4396  k_capacity + 1.0);
4397  ++i;
4398  }
4399  }
4400 
4401  /*
4402  * Apply knapsack algorithm; compute the set of items which
4403  * maximizes the value stored (in this case the number of sorts
4404  * saved) while keeping the total size (approximately) within
4405  * capacity.
4406  */
4407  if (i > 0)
4408  hash_items = DiscreteKnapsack(k_capacity, i, k_weights, NULL);
4409 
4410  if (!bms_is_empty(hash_items))
4411  {
4412  rollups = list_make1(linitial(gd->rollups));
4413 
4414  i = 0;
4415  for_each_from(lc, gd->rollups, 1)
4416  {
4417  RollupData *rollup = lfirst_node(RollupData, lc);
4418 
4419  if (rollup->hashable)
4420  {
4421  if (bms_is_member(i, hash_items))
4422  hash_sets = list_concat(hash_sets,
4423  rollup->gsets_data);
4424  else
4425  rollups = lappend(rollups, rollup);
4426  ++i;
4427  }
4428  else
4429  rollups = lappend(rollups, rollup);
4430  }
4431  }
4432  }
4433 
4434  if (!rollups && hash_sets)
4435  rollups = list_copy(gd->rollups);
4436 
4437  foreach(lc, hash_sets)
4438  {
4440  RollupData *rollup = makeNode(RollupData);
4441 
4442  Assert(gs->set != NIL);
4443 
4444  rollup->groupClause = preprocess_groupclause(root, gs->set);
4445  rollup->gsets_data = list_make1(gs);
4446  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4447  rollup->gsets_data,
4448  gd->tleref_to_colnum_map);
4449  rollup->numGroups = gs->numGroups;
4450  rollup->hashable = true;
4451  rollup->is_hashed = true;
4452  rollups = lcons(rollup, rollups);
4453  }
4454 
4455  if (rollups)
4456  {
4457  add_path(grouped_rel, (Path *)
4459  grouped_rel,
4460  path,
4461  (List *) parse->havingQual,
4462  AGG_MIXED,
4463  rollups,
4464  agg_costs));
4465  }
4466  }
4467 
4468  /*
4469  * Now try the simple sorted case.
4470  */
4471  if (!gd->unsortable_sets)
4472  add_path(grouped_rel, (Path *)
4474  grouped_rel,
4475  path,
4476  (List *) parse->havingQual,
4477  AGG_SORTED,
4478  gd->rollups,
4479  agg_costs));
4480 }
4481 
4482 /*
4483  * create_window_paths
4484  *
4485  * Build a new upperrel containing Paths for window-function evaluation.
4486  *
4487  * input_rel: contains the source-data Paths
4488  * input_target: result of make_window_input_target
4489  * output_target: what the topmost WindowAggPath should return
4490  * wflists: result of find_window_functions
4491  * activeWindows: result of select_active_windows
4492  *
4493  * Note: all Paths in input_rel are expected to return input_target.
4494  */
4495 static RelOptInfo *
4497  RelOptInfo *input_rel,
4498  PathTarget *input_target,
4499  PathTarget *output_target,
4500  bool output_target_parallel_safe,
4501  WindowFuncLists *wflists,
4502  List *activeWindows)
4503 {
4504  RelOptInfo *window_rel;
4505  ListCell *lc;
4506 
4507  /* For now, do all work in the (WINDOW, NULL) upperrel */
4508  window_rel = fetch_upper_rel(root, UPPERREL_WINDOW, NULL);
4509 
4510  /*
4511  * If the input relation is not parallel-safe, then the window relation
4512  * can't be parallel-safe, either. Otherwise, we need to examine the
4513  * target list and active windows for non-parallel-safe constructs.
4514  */
4515  if (input_rel->consider_parallel && output_target_parallel_safe &&
4516  is_parallel_safe(root, (Node *) activeWindows))
4517  window_rel->consider_parallel = true;
4518 
4519  /*
4520  * If the input rel belongs to a single FDW, so does the window rel.
4521  */
4522  window_rel->serverid = input_rel->serverid;
4523  window_rel->userid = input_rel->userid;
4524  window_rel->useridiscurrent = input_rel->useridiscurrent;
4525  window_rel->fdwroutine = input_rel->fdwroutine;
4526 
4527  /*
4528  * Consider computing window functions starting from the existing
4529  * cheapest-total path (which will likely require a sort) as well as any
4530  * existing paths that satisfy or partially satisfy root->window_pathkeys.
4531  */
4532  foreach(lc, input_rel->pathlist)
4533  {
4534  Path *path = (Path *) lfirst(lc);
4535  int presorted_keys;
4536 
4537  if (path == input_rel->cheapest_total_path ||
4538  pathkeys_count_contained_in(root->window_pathkeys, path->pathkeys,
4539  &presorted_keys) ||
4540  presorted_keys > 0)
4542  window_rel,
4543  path,
4544  input_target,
4545  output_target,
4546  wflists,
4547  activeWindows);
4548  }
4549 
4550  /*
4551  * If there is an FDW that's responsible for all baserels of the query,
4552  * let it consider adding ForeignPaths.
4553  */
4554  if (window_rel->fdwroutine &&
4555  window_rel->fdwroutine->GetForeignUpperPaths)
4556  window_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_WINDOW,
4557  input_rel, window_rel,
4558  NULL);
4559 
4560  /* Let extensions possibly add some more paths */
4562  (*create_upper_paths_hook) (root, UPPERREL_WINDOW,
4563  input_rel, window_rel, NULL);
4564 
4565  /* Now choose the best path(s) */
4566  set_cheapest(window_rel);
4567 
4568  return window_rel;
4569 }
4570 
4571 /*
4572  * Stack window-function implementation steps atop the given Path, and
4573  * add the result to window_rel.
4574  *
4575  * window_rel: upperrel to contain result
4576  * path: input Path to use (must return input_target)
4577  * input_target: result of make_window_input_target
4578  * output_target: what the topmost WindowAggPath should return
4579  * wflists: result of find_window_functions
4580  * activeWindows: result of select_active_windows
4581  */
4582 static void
4584  RelOptInfo *window_rel,
4585  Path *path,
4586  PathTarget *input_target,
4587  PathTarget *output_target,
4588  WindowFuncLists *wflists,
4589  List *activeWindows)
4590 {
4591  PathTarget *window_target;
4592  ListCell *l;
4593  List *topqual = NIL;
4594 
4595  /*
4596  * Since each window clause could require a different sort order, we stack
4597  * up a WindowAgg node for each clause, with sort steps between them as
4598  * needed. (We assume that select_active_windows chose a good order for
4599  * executing the clauses in.)
4600  *
4601  * input_target should contain all Vars and Aggs needed for the result.
4602  * (In some cases we wouldn't need to propagate all of these all the way
4603  * to the top, since they might only be needed as inputs to WindowFuncs.
4604  * It's probably not worth trying to optimize that though.) It must also
4605  * contain all window partitioning and sorting expressions, to ensure
4606  * they're computed only once at the bottom of the stack (that's critical
4607  * for volatile functions). As we climb up the stack, we'll add outputs
4608  * for the WindowFuncs computed at each level.
4609  */
4610  window_target = input_target;
4611 
4612  foreach(l, activeWindows)
4613  {
4615  List *window_pathkeys;
4616  List *runcondition = NIL;
4617  int presorted_keys;
4618  bool is_sorted;
4619  bool topwindow;
4620  ListCell *lc2;
4621 
4622  window_pathkeys = make_pathkeys_for_window(root,
4623  wc,
4624  root->processed_tlist);
4625 
4626  is_sorted = pathkeys_count_contained_in(window_pathkeys,
4627  path->pathkeys,
4628  &presorted_keys);
4629 
4630  /* Sort if necessary */
4631  if (!is_sorted)
4632  {
4633  /*
4634  * No presorted keys or incremental sort disabled, just perform a
4635  * complete sort.
4636  */
4637  if (presorted_keys == 0 || !enable_incremental_sort)
4638  path = (Path *) create_sort_path(root, window_rel,
4639  path,
4640  window_pathkeys,
4641  -1.0);
4642  else
4643  {
4644  /*
4645  * Since we have presorted keys and incremental sort is
4646  * enabled, just use incremental sort.
4647  */
4649  window_rel,
4650  path,
4651  window_pathkeys,
4652  presorted_keys,
4653  -1.0);
4654  }
4655  }
4656 
4657  if (lnext(activeWindows, l))
4658  {
4659  /*
4660  * Add the current WindowFuncs to the output target for this
4661  * intermediate WindowAggPath. We must copy window_target to
4662  * avoid changing the previous path's target.
4663  *
4664  * Note: a WindowFunc adds nothing to the target's eval costs; but
4665  * we do need to account for the increase in tlist width.
4666  */
4667  int64 tuple_width = window_target->width;
4668 
4669  window_target = copy_pathtarget(window_target);
4670  foreach(lc2, wflists->windowFuncs[wc->winref])
4671  {
4672  WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
4673 
4674  add_column_to_pathtarget(window_target, (Expr *) wfunc, 0);
4675  tuple_width += get_typavgwidth(wfunc->wintype, -1);
4676  }
4677  window_target->width = clamp_width_est(tuple_width);
4678  }
4679  else
4680  {
4681  /* Install the goal target in the topmost WindowAgg */
4682  window_target = output_target;
4683  }
4684 
4685  /* mark the final item in the list as the top-level window */
4686  topwindow = foreach_current_index(l) == list_length(activeWindows) - 1;
4687 
4688  /*
4689  * Collect the WindowFuncRunConditions from each WindowFunc and
4690  * convert them into OpExprs
4691  */
4692  foreach(lc2, wflists->windowFuncs[wc->winref])
4693  {
4694  ListCell *lc3;
4695  WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
4696 
4697  foreach(lc3, wfunc->runCondition)
4698  {
4699  WindowFuncRunCondition *wfuncrc =
4701  Expr *opexpr;
4702  Expr *leftop;
4703  Expr *rightop;
4704 
4705  if (wfuncrc->wfunc_left)
4706  {
4707  leftop = (Expr *) copyObject(wfunc);
4708  rightop = copyObject(wfuncrc->arg);
4709  }
4710  else
4711  {
4712  leftop = copyObject(wfuncrc->arg);
4713  rightop = (Expr *) copyObject(wfunc);
4714  }
4715 
4716  opexpr = make_opclause(wfuncrc->opno,
4717  BOOLOID,
4718  false,
4719  leftop,
4720  rightop,
4721  InvalidOid,
4722  wfuncrc->inputcollid);
4723 
4724  runcondition = lappend(runcondition, opexpr);
4725 
4726  if (!topwindow)
4727  topqual = lappend(topqual, opexpr);
4728  }
4729  }
4730 
4731  path = (Path *)
4732  create_windowagg_path(root, window_rel, path, window_target,
4733  wflists->windowFuncs[wc->winref],
4734  runcondition, wc,
4735  topwindow ? topqual : NIL, topwindow);
4736  }
4737 
4738  add_path(window_rel, path);
4739 }
4740 
4741 /*
4742  * create_distinct_paths
4743  *
4744  * Build a new upperrel containing Paths for SELECT DISTINCT evaluation.
4745  *
4746  * input_rel: contains the source-data Paths
4747  * target: the pathtarget for the result Paths to compute
4748  *
4749  * Note: input paths should already compute the desired pathtarget, since
4750  * Sort/Unique won't project anything.
4751  */
4752 static RelOptInfo *
4754  PathTarget *target)
4755 {
4756  RelOptInfo *distinct_rel;
4757 
4758  /* For now, do all work in the (DISTINCT, NULL) upperrel */
4759  distinct_rel = fetch_upper_rel(root, UPPERREL_DISTINCT, NULL);
4760 
4761  /*
4762  * We don't compute anything at this level, so distinct_rel will be
4763  * parallel-safe if the input rel is parallel-safe. In particular, if
4764  * there is a DISTINCT ON (...) clause, any path for the input_rel will
4765  * output those expressions, and will not be parallel-safe unless those
4766  * expressions are parallel-safe.
4767  */
4768  distinct_rel->consider_parallel = input_rel->consider_parallel;
4769 
4770  /*
4771  * If the input rel belongs to a single FDW, so does the distinct_rel.
4772  */
4773  distinct_rel->serverid = input_rel->serverid;
4774  distinct_rel->userid = input_rel->userid;
4775  distinct_rel->useridiscurrent = input_rel->useridiscurrent;
4776  distinct_rel->fdwroutine = input_rel->fdwroutine;
4777 
4778  /* build distinct paths based on input_rel's pathlist */
4779  create_final_distinct_paths(root, input_rel, distinct_rel);
4780 
4781  /* now build distinct paths based on input_rel's partial_pathlist */
4782  create_partial_distinct_paths(root, input_rel, distinct_rel, target);
4783 
4784  /* Give a helpful error if we failed to create any paths */
4785  if (distinct_rel->pathlist == NIL)
4786  ereport(ERROR,
4787  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4788  errmsg("could not implement DISTINCT"),
4789  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
4790 
4791  /*
4792  * If there is an FDW that's responsible for all baserels of the query,
4793  * let it consider adding ForeignPaths.
4794  */
4795  if (distinct_rel->fdwroutine &&
4796  distinct_rel->fdwroutine->GetForeignUpperPaths)
4797  distinct_rel->fdwroutine->GetForeignUpperPaths(root,
4799  input_rel,
4800  distinct_rel,
4801  NULL);
4802 
4803  /* Let extensions possibly add some more paths */
4805  (*create_upper_paths_hook) (root, UPPERREL_DISTINCT, input_rel,
4806  distinct_rel, NULL);
4807 
4808  /* Now choose the best path(s) */
4809  set_cheapest(distinct_rel);
4810 
4811  return distinct_rel;
4812 }
4813 
4814 /*
4815  * create_partial_distinct_paths
4816  *
4817  * Process 'input_rel' partial paths and add unique/aggregate paths to the
4818  * UPPERREL_PARTIAL_DISTINCT rel. For paths created, add Gather/GatherMerge
4819  * paths on top and add a final unique/aggregate path to remove any duplicate
4820  * produced from combining rows from parallel workers.
4821  */
4822 static void
4824  RelOptInfo *final_distinct_rel,
4825  PathTarget *target)
4826 {
4827  RelOptInfo *partial_distinct_rel;
4828  Query *parse;
4829  List *distinctExprs;
4830  double numDistinctRows;
4831  Path *cheapest_partial_path;
4832  ListCell *lc;
4833 
4834  /* nothing to do when there are no partial paths in the input rel */
4835  if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
4836  return;
4837 
4838  parse = root->parse;
4839 
4840  /* can't do parallel DISTINCT ON */
4841  if (parse->hasDistinctOn)
4842  return;
4843 
4844  partial_distinct_rel = fetch_upper_rel(root, UPPERREL_PARTIAL_DISTINCT,
4845  NULL);
4846  partial_distinct_rel->reltarget = target;
4847  partial_distinct_rel->consider_parallel = input_rel->consider_parallel;
4848 
4849  /*
4850  * If input_rel belongs to a single FDW, so does the partial_distinct_rel.
4851  */
4852  partial_distinct_rel->serverid = input_rel->serverid;
4853  partial_distinct_rel->userid = input_rel->userid;
4854  partial_distinct_rel->useridiscurrent = input_rel->useridiscurrent;
4855  partial_distinct_rel->fdwroutine = input_rel->fdwroutine;
4856 
4857  cheapest_partial_path = linitial(input_rel->partial_pathlist);
4858 
4859  distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
4860  parse->targetList);
4861 
4862  /* estimate how many distinct rows we'll get from each worker */
4863  numDistinctRows = estimate_num_groups(root, distinctExprs,
4864  cheapest_partial_path->rows,
4865  NULL, NULL);
4866 
4867  /*
4868  * Try sorting the cheapest path and incrementally sorting any paths with
4869  * presorted keys and put a unique paths atop of those.
4870  */
4871  if (grouping_is_sortable(root->processed_distinctClause))
4872  {
4873  foreach(lc, input_rel->partial_pathlist)
4874  {
4875  Path *input_path = (Path *) lfirst(lc);
4876  Path *sorted_path;
4877  bool is_sorted;
4878  int presorted_keys;
4879 
4880  is_sorted = pathkeys_count_contained_in(root->distinct_pathkeys,
4881  input_path->pathkeys,
4882  &presorted_keys);
4883 
4884  if (is_sorted)
4885  sorted_path = input_path;
4886  else
4887  {
4888  /*
4889  * Try at least sorting the cheapest path and also try
4890  * incrementally sorting any path which is partially sorted
4891  * already (no need to deal with paths which have presorted
4892  * keys when incremental sort is disabled unless it's the
4893  * cheapest partial path).
4894  */
4895  if (input_path != cheapest_partial_path &&
4896  (presorted_keys == 0 || !enable_incremental_sort))
4897  continue;
4898 
4899  /*
4900  * We've no need to consider both a sort and incremental sort.
4901  * We'll just do a sort if there are no presorted keys and an
4902  * incremental sort when there are presorted keys.
4903  */
4904  if (presorted_keys == 0 || !enable_incremental_sort)
4905  sorted_path = (Path *) create_sort_path(root,
4906  partial_distinct_rel,
4907  input_path,
4908  root->distinct_pathkeys,
4909  -1.0);
4910  else
4911  sorted_path = (Path *) create_incremental_sort_path(root,
4912  partial_distinct_rel,
4913  input_path,
4914  root->distinct_pathkeys,
4915  presorted_keys,
4916  -1.0);
4917  }
4918 
4919  /*
4920  * An empty distinct_pathkeys means all tuples have the same value
4921  * for the DISTINCT clause. See create_final_distinct_paths()
4922  */
4923  if (root->distinct_pathkeys == NIL)
4924  {
4925  Node *limitCount;
4926 
4927  limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
4928  sizeof(int64),
4929  Int64GetDatum(1), false,
4930  FLOAT8PASSBYVAL);
4931 
4932  /*
4933  * Apply a LimitPath onto the partial path to restrict the
4934  * tuples from each worker to 1. create_final_distinct_paths
4935  * will need to apply an additional LimitPath to restrict this
4936  * to a single row after the Gather node. If the query
4937  * already has a LIMIT clause, then we could end up with three
4938  * Limit nodes in the final plan. Consolidating the top two
4939  * of these could be done, but does not seem worth troubling
4940  * over.
4941  */
4942  add_partial_path(partial_distinct_rel, (Path *)
4943  create_limit_path(root, partial_distinct_rel,
4944  sorted_path,
4945  NULL,
4946  limitCount,
4948  0, 1));
4949  }
4950  else
4951  {
4952  add_partial_path(partial_distinct_rel, (Path *)
4953  create_upper_unique_path(root, partial_distinct_rel,
4954  sorted_path,
4955  list_length(root->distinct_pathkeys),
4956  numDistinctRows));
4957  }
4958  }
4959  }
4960 
4961  /*
4962  * Now try hash aggregate paths, if enabled and hashing is possible. Since
4963  * we're not on the hook to ensure we do our best to create at least one
4964  * path here, we treat enable_hashagg as a hard off-switch rather than the
4965  * slightly softer variant in create_final_distinct_paths.
4966  */
4967  if (enable_hashagg && grouping_is_hashable(root->processed_distinctClause))
4968  {
4969  add_partial_path(partial_distinct_rel, (Path *)
4971  partial_distinct_rel,
4972  cheapest_partial_path,
4973  cheapest_partial_path->pathtarget,
4974  AGG_HASHED,
4976  root->processed_distinctClause,
4977  NIL,
4978  NULL,
4979  numDistinctRows));
4980  }
4981 
4982  /*
4983  * If there is an FDW that's responsible for all baserels of the query,
4984  * let it consider adding ForeignPaths.
4985  */
4986  if (partial_distinct_rel->fdwroutine &&
4987  partial_distinct_rel->fdwroutine->GetForeignUpperPaths)
4988  partial_distinct_rel->fdwroutine->GetForeignUpperPaths(root,
4990  input_rel,
4991  partial_distinct_rel,
4992  NULL);
4993 
4994  /* Let extensions possibly add some more partial paths */
4996  (*create_upper_paths_hook) (root, UPPERREL_PARTIAL_DISTINCT,
4997  input_rel, partial_distinct_rel, NULL);
4998 
4999  if (partial_distinct_rel->partial_pathlist != NIL)
5000  {
5001  generate_useful_gather_paths(root, partial_distinct_rel, true);
5002  set_cheapest(partial_distinct_rel);
5003 
5004  /*
5005  * Finally, create paths to distinctify the final result. This step
5006  * is needed to remove any duplicates due to combining rows from
5007  * parallel workers.
5008  */
5009  create_final_distinct_paths(root, partial_distinct_rel,
5010  final_distinct_rel);
5011  }
5012 }
5013 
5014 /*
5015  * create_final_distinct_paths
5016  * Create distinct paths in 'distinct_rel' based on 'input_rel' pathlist
5017  *
5018  * input_rel: contains the source-data paths
5019  * distinct_rel: destination relation for storing created paths
5020  */
5021 static RelOptInfo *
5023  RelOptInfo *distinct_rel)
5024 {
5025  Query *parse = root->parse;
5026  Path *cheapest_input_path = input_rel->cheapest_total_path;
5027  double numDistinctRows;
5028  bool allow_hash;
5029 
5030  /* Estimate number of distinct rows there will be */
5031  if (parse->groupClause || parse->groupingSets || parse->hasAggs ||
5032  root->hasHavingQual)
5033  {
5034  /*
5035  * If there was grouping or aggregation, use the number of input rows
5036  * as the estimated number of DISTINCT rows (ie, assume the input is
5037  * already mostly unique).
5038  */
5039  numDistinctRows = cheapest_input_path->rows;
5040  }
5041  else
5042  {
5043  /*
5044  * Otherwise, the UNIQUE filter has effects comparable to GROUP BY.
5045  */
5046  List *distinctExprs;
5047 
5048  distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
5049  parse->targetList);
5050  numDistinctRows = estimate_num_groups(root, distinctExprs,
5051  cheapest_input_path->rows,
5052  NULL, NULL);
5053  }
5054 
5055  /*
5056  * Consider sort-based implementations of DISTINCT, if possible.
5057  */
5058  if (grouping_is_sortable(root->processed_distinctClause))
5059  {
5060  /*
5061  * Firstly, if we have any adequately-presorted paths, just stick a
5062  * Unique node on those. We also, consider doing an explicit sort of
5063  * the cheapest input path and Unique'ing that. If any paths have
5064  * presorted keys then we'll create an incremental sort atop of those
5065  * before adding a unique node on the top.
5066  *
5067  * When we have DISTINCT ON, we must sort by the more rigorous of
5068  * DISTINCT and ORDER BY, else it won't have the desired behavior.
5069  * Also, if we do have to do an explicit sort, we might as well use
5070  * the more rigorous ordering to avoid a second sort later. (Note
5071  * that the parser will have ensured that one clause is a prefix of
5072  * the other.)
5073  */
5074  List *needed_pathkeys;
5075  ListCell *lc;
5076  double limittuples = root->distinct_pathkeys == NIL ? 1.0 : -1.0;
5077 
5078  if (parse->hasDistinctOn &&
5079  list_length(root->distinct_pathkeys) <
5080  list_length(root->sort_pathkeys))
5081  needed_pathkeys = root->sort_pathkeys;
5082  else
5083  needed_pathkeys = root->distinct_pathkeys;
5084 
5085  foreach(lc, input_rel->pathlist)
5086  {
5087  Path *input_path = (Path *) lfirst(lc);
5088  Path *sorted_path;
5089  bool is_sorted;
5090  int presorted_keys;
5091 
5092  is_sorted = pathkeys_count_contained_in(needed_pathkeys,
5093  input_path->pathkeys,
5094  &presorted_keys);
5095 
5096  if (is_sorted)
5097  sorted_path = input_path;
5098  else
5099  {
5100  /*
5101  * Try at least sorting the cheapest path and also try
5102  * incrementally sorting any path which is partially sorted
5103  * already (no need to deal with paths which have presorted
5104  * keys when incremental sort is disabled unless it's the
5105  * cheapest input path).
5106  */
5107  if (input_path != cheapest_input_path &&
5108  (presorted_keys == 0 || !enable_incremental_sort))
5109  continue;
5110 
5111  /*
5112  * We've no need to consider both a sort and incremental sort.
5113  * We'll just do a sort if there are no presorted keys and an
5114  * incremental sort when there are presorted keys.
5115  */
5116  if (presorted_keys == 0 || !enable_incremental_sort)
5117  sorted_path = (Path *) create_sort_path(root,
5118  distinct_rel,
5119  input_path,
5120  needed_pathkeys,
5121  limittuples);
5122  else
5123  sorted_path = (Path *) create_incremental_sort_path(root,
5124  distinct_rel,
5125  input_path,
5126  needed_pathkeys,
5127  presorted_keys,
5128  limittuples);
5129  }
5130 
5131  /*
5132  * distinct_pathkeys may have become empty if all of the pathkeys
5133  * were determined to be redundant. If all of the pathkeys are
5134  * redundant then each DISTINCT target must only allow a single
5135  * value, therefore all resulting tuples must be identical (or at
5136  * least indistinguishable by an equality check). We can uniquify
5137  * these tuples simply by just taking the first tuple. All we do
5138  * here is add a path to do "LIMIT 1" atop of 'sorted_path'. When
5139  * doing a DISTINCT ON we may still have a non-NIL sort_pathkeys
5140  * list, so we must still only do this with paths which are
5141  * correctly sorted by sort_pathkeys.
5142  */
5143  if (root->distinct_pathkeys == NIL)
5144  {
5145  Node *limitCount;
5146 
5147  limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
5148  sizeof(int64),
5149  Int64GetDatum(1), false,
5150  FLOAT8PASSBYVAL);
5151 
5152  /*
5153  * If the query already has a LIMIT clause, then we could end
5154  * up with a duplicate LimitPath in the final plan. That does
5155  * not seem worth troubling over too much.
5156  */
5157  add_path(distinct_rel, (Path *)
5158  create_limit_path(root, distinct_rel, sorted_path,
5159  NULL, limitCount,
5160  LIMIT_OPTION_COUNT, 0, 1));
5161  }
5162  else
5163  {
5164  add_path(distinct_rel, (Path *)
5165  create_upper_unique_path(root, distinct_rel,
5166  sorted_path,
5167  list_length(root->distinct_pathkeys),
5168  numDistinctRows));
5169  }
5170  }
5171  }
5172 
5173  /*
5174  * Consider hash-based implementations of DISTINCT, if possible.
5175  *
5176  * If we were not able to make any other types of path, we *must* hash or
5177  * die trying. If we do have other choices, there are two things that
5178  * should prevent selection of hashing: if the query uses DISTINCT ON
5179  * (because it won't really have the expected behavior if we hash), or if
5180  * enable_hashagg is off.
5181  *
5182  * Note: grouping_is_hashable() is much more expensive to check than the
5183  * other gating conditions, so we want to do it last.
5184  */
5185  if (distinct_rel->pathlist == NIL)
5186  allow_hash = true; /* we have no alternatives */
5187  else if (parse->hasDistinctOn || !enable_hashagg)
5188  allow_hash = false; /* policy-based decision not to hash */
5189  else
5190  allow_hash = true; /* default */
5191 
5192  if (allow_hash && grouping_is_hashable(root->processed_distinctClause))
5193  {
5194  /* Generate hashed aggregate path --- no sort needed */
5195  add_path(distinct_rel, (Path *)
5197  distinct_rel,
5198  cheapest_input_path,
5199  cheapest_input_path->pathtarget,
5200  AGG_HASHED,
5202  root->processed_distinctClause,
5203  NIL,
5204  NULL,
5205  numDistinctRows));
5206  }
5207 
5208  return distinct_rel;
5209 }
5210 
5211 /*
5212  * create_ordered_paths
5213  *
5214  * Build a new upperrel containing Paths for ORDER BY evaluation.
5215  *
5216  * All paths in the result must satisfy the ORDER BY ordering.
5217  * The only new paths we need consider are an explicit full sort
5218  * and incremental sort on the cheapest-total existing path.
5219  *
5220  * input_rel: contains the source-data Paths
5221  * target: the output tlist the result Paths must emit
5222  * limit_tuples: estimated bound on the number of output tuples,
5223  * or -1 if no LIMIT or couldn't estimate
5224  *
5225  * XXX This only looks at sort_pathkeys. I wonder if it needs to look at the
5226  * other pathkeys (grouping, ...) like generate_useful_gather_paths.
5227  */
5228 static RelOptInfo *
5230  RelOptInfo *input_rel,
5231  PathTarget *target,
5232  bool target_parallel_safe,
5233  double limit_tuples)
5234 {
5235  Path *cheapest_input_path = input_rel->cheapest_total_path;
5236  RelOptInfo *ordered_rel;
5237  ListCell *lc;
5238 
5239  /* For now, do all work in the (ORDERED, NULL) upperrel */
5240  ordered_rel = fetch_upper_rel(root, UPPERREL_ORDERED, NULL);
5241 
5242  /*
5243  * If the input relation is not parallel-safe, then the ordered relation
5244  * can't be parallel-safe, either. Otherwise, it's parallel-safe if the
5245  * target list is parallel-safe.
5246  */
5247  if (input_rel->consider_parallel && target_parallel_safe)
5248  ordered_rel->consider_parallel = true;
5249 
5250  /*
5251  * If the input rel belongs to a single FDW, so does the ordered_rel.
5252  */
5253  ordered_rel->serverid = input_rel->serverid;
5254  ordered_rel->userid = input_rel->userid;
5255  ordered_rel->useridiscurrent = input_rel->useridiscurrent;
5256  ordered_rel->fdwroutine = input_rel->fdwroutine;
5257 
5258  foreach(lc, input_rel->pathlist)
5259  {
5260  Path *input_path = (Path *) lfirst(lc);
5261  Path *sorted_path;
5262  bool is_sorted;
5263  int presorted_keys;
5264 
5265  is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5266  input_path->pathkeys, &presorted_keys);
5267 
5268  if (is_sorted)
5269  sorted_path = input_path;
5270  else
5271  {
5272  /*
5273  * Try at least sorting the cheapest path and also try
5274  * incrementally sorting any path which is partially sorted
5275  * already (no need to deal with paths which have presorted keys
5276  * when incremental sort is disabled unless it's the cheapest
5277  * input path).
5278  */
5279  if (input_path != cheapest_input_path &&
5280  (presorted_keys == 0 || !enable_incremental_sort))
5281  continue;
5282 
5283  /*
5284  * We've no need to consider both a sort and incremental sort.
5285  * We'll just do a sort if there are no presorted keys and an
5286  * incremental sort when there are presorted keys.
5287  */
5288  if (presorted_keys == 0 || !enable_incremental_sort)
5289  sorted_path = (Path *) create_sort_path(root,
5290  ordered_rel,
5291  input_path,
5292  root->sort_pathkeys,
5293  limit_tuples);
5294  else
5295  sorted_path = (Path *) create_incremental_sort_path(root,
5296  ordered_rel,
5297  input_path,
5298  root->sort_pathkeys,
5299  presorted_keys,
5300  limit_tuples);
5301  }
5302 
5303  /* Add projection step if needed */
5304  if (sorted_path->pathtarget != target)
5305  sorted_path = apply_projection_to_path(root, ordered_rel,
5306  sorted_path, target);
5307 
5308  add_path(ordered_rel, sorted_path);
5309  }
5310 
5311  /*
5312  * generate_gather_paths() will have already generated a simple Gather
5313  * path for the best parallel path, if any, and the loop above will have
5314  * considered sorting it. Similarly, generate_gather_paths() will also
5315  * have generated order-preserving Gather Merge plans which can be used
5316  * without sorting if they happen to match the sort_pathkeys, and the loop
5317  * above will have handled those as well. However, there's one more
5318  * possibility: it may make sense to sort the cheapest partial path or
5319  * incrementally sort any partial path that is partially sorted according
5320  * to the required output order and then use Gather Merge.
5321  */
5322  if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL &&
5323  input_rel->partial_pathlist != NIL)
5324  {
5325  Path *cheapest_partial_path;
5326 
5327  cheapest_partial_path = linitial(input_rel->partial_pathlist);
5328 
5329  foreach(lc, input_rel->partial_pathlist)
5330  {
5331  Path *input_path = (Path *) lfirst(lc);
5332  Path *sorted_path;
5333  bool is_sorted;
5334  int presorted_keys;
5335  double total_groups;
5336 
5337  is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5338  input_path->pathkeys,
5339  &presorted_keys);
5340 
5341  if (is_sorted)
5342  continue;
5343 
5344  /*
5345  * Try at least sorting the cheapest path and also try
5346  * incrementally sorting any path which is partially sorted
5347  * already (no need to deal with paths which have presorted keys
5348  * when incremental sort is disabled unless it's the cheapest
5349  * partial path).
5350  */
5351  if (input_path != cheapest_partial_path &&
5352  (presorted_keys == 0 || !enable_incremental_sort))
5353  continue;
5354 
5355  /*
5356  * We've no need to consider both a sort and incremental sort.
5357  * We'll just do a sort if there are no presorted keys and an
5358  * incremental sort when there are presorted keys.
5359  */
5360  if (presorted_keys == 0 || !enable_incremental_sort)
5361  sorted_path = (Path *) create_sort_path(root,
5362  ordered_rel,
5363  input_path,
5364  root->sort_pathkeys,
5365  limit_tuples);
5366  else
5367  sorted_path = (Path *) create_incremental_sort_path(root,
5368  ordered_rel,
5369  input_path,
5370  root->sort_pathkeys,
5371  presorted_keys,
5372  limit_tuples);
5373  total_groups = compute_gather_rows(sorted_path);
5374  sorted_path = (Path *)
5375  create_gather_merge_path(root, ordered_rel,
5376  sorted_path,
5377  sorted_path->pathtarget,
5378  root->sort_pathkeys, NULL,
5379  &total_groups);
5380 
5381  /* Add projection step if needed */
5382  if (sorted_path->pathtarget != target)
5383  sorted_path = apply_projection_to_path(root, ordered_rel,
5384  sorted_path, target);
5385 
5386  add_path(ordered_rel, sorted_path);
5387  }
5388  }
5389 
5390  /*
5391  * If there is an FDW that's responsible for all baserels of the query,
5392  * let it consider adding ForeignPaths.
5393  */
5394  if (ordered_rel->fdwroutine &&
5395  ordered_rel->fdwroutine->GetForeignUpperPaths)
5396  ordered_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_ORDERED,
5397  input_rel, ordered_rel,
5398  NULL);
5399 
5400  /* Let extensions possibly add some more paths */
5402  (*create_upper_paths_hook) (root, UPPERREL_ORDERED,
5403  input_rel, ordered_rel, NULL);
5404 
5405  /*
5406  * No need to bother with set_cheapest here; grouping_planner does not
5407  * need us to do it.
5408  */
5409  Assert(ordered_rel->pathlist != NIL);
5410 
5411  return ordered_rel;
5412 }
5413 
5414 
5415 /*
5416  * make_group_input_target
5417  * Generate appropriate PathTarget for initial input to grouping nodes.
5418  *
5419  * If there is grouping or aggregation, the scan/join subplan cannot emit
5420  * the query's final targetlist; for example, it certainly can't emit any
5421  * aggregate function calls. This routine generates the correct target
5422  * for the scan/join subplan.
5423  *
5424  * The query target list passed from the parser already contains entries
5425  * for all ORDER BY and GROUP BY expressions, but it will not have entries
5426  * for variables used only in HAVING clauses; so we need to add those
5427  * variables to the subplan target list. Also, we flatten all expressions
5428  * except GROUP BY items into their component variables; other expressions
5429  * will be computed by the upper plan nodes rather than by the subplan.
5430  * For example, given a query like
5431  * SELECT a+b,SUM(c+d) FROM table GROUP BY a+b;
5432  * we want to pass this targetlist to the subplan:
5433  * a+b,c,d
5434  * where the a+b target will be used by the Sort/Group steps, and the
5435  * other targets will be used for computing the final results.
5436  *
5437  * 'final_target' is the query's final target list (in PathTarget form)
5438  *
5439  * The result is the PathTarget to be computed by the Paths returned from
5440  * query_planner().
5441  */
5442 static PathTarget *
5444 {
5445  Query *parse = root->parse;
5446  PathTarget *input_target;
5447  List *non_group_cols;
5448  List *non_group_vars;
5449  int i;
5450  ListCell *lc;
5451 
5452  /*
5453  * We must build a target containing all grouping columns, plus any other
5454  * Vars mentioned in the query's targetlist and HAVING qual.
5455  */
5456  input_target = create_empty_pathtarget();
5457  non_group_cols = NIL;
5458 
5459  i = 0;
5460  foreach(lc, final_target->exprs)
5461  {
5462  Expr *expr = (Expr *) lfirst(lc);
5463  Index sgref = get_pathtarget_sortgroupref(final_target, i);
5464 
5465  if (sgref && root->processed_groupClause &&
5467  root->processed_groupClause) != NULL)
5468  {
5469  /*
5470  * It's a grouping column, so add it to the input target as-is.
5471  */
5472  add_column_to_pathtarget(input_target, expr, sgref);
5473  }
5474  else
5475  {
5476  /*
5477  * Non-grouping column, so just remember the expression for later
5478  * call to pull_var_clause.
5479  */
5480  non_group_cols = lappend(non_group_cols, expr);
5481  }
5482 
5483  i++;
5484  }
5485 
5486  /*
5487  * If there's a HAVING clause, we'll need the Vars it uses, too.
5488  */
5489  if (parse->havingQual)
5490  non_group_cols = lappend(non_group_cols, parse->havingQual);
5491 
5492  /*
5493  * Pull out all the Vars mentioned in non-group cols (plus HAVING), and
5494  * add them to the input target if not already present. (A Var used
5495  * directly as a GROUP BY item will be present already.) Note this
5496  * includes Vars used in resjunk items, so we are covering the needs of
5497  * ORDER BY and window specifications. Vars used within Aggrefs and
5498  * WindowFuncs will be pulled out here, too.
5499  */
5500  non_group_vars = pull_var_clause((Node *) non_group_cols,
5504  add_new_columns_to_pathtarget(input_target, non_group_vars);
5505 
5506  /* clean up cruft */
5507  list_free(non_group_vars);
5508  list_free(non_group_cols);
5509 
5510  /* XXX this causes some redundant cost calculation ... */
5511  return set_pathtarget_cost_width(root, input_target);
5512 }
5513 
5514 /*
5515  * make_partial_grouping_target
5516  * Generate appropriate PathTarget for output of partial aggregate
5517  * (or partial grouping, if there are no aggregates) nodes.
5518  *
5519  * A partial aggregation node needs to emit all the same aggregates that
5520  * a regular aggregation node would, plus any aggregates used in HAVING;
5521  * except that the Aggref nodes should be marked as partial aggregates.
5522  *
5523  * In addition, we'd better emit any Vars and PlaceHolderVars that are
5524  * used outside of Aggrefs in the aggregation tlist and HAVING. (Presumably,
5525  * these would be Vars that are grouped by or used in grouping expressions.)
5526  *
5527  * grouping_target is the tlist to be emitted by the topmost aggregation step.
5528  * havingQual represents the HAVING clause.
5529  */
5530 static PathTarget *
5532  PathTarget *grouping_target,
5533  Node *havingQual)
5534 {
5535  PathTarget *partial_target;
5536  List *non_group_cols;
5537  List *non_group_exprs;
5538  int i;
5539  ListCell *lc;
5540 
5541  partial_target = create_empty_pathtarget();
5542  non_group_cols = NIL;
5543 
5544  i = 0;
5545  foreach(lc, grouping_target->exprs)
5546  {
5547  Expr *expr = (Expr *) lfirst(lc);
5548  Index sgref = get_pathtarget_sortgroupref(grouping_target, i);
5549 
5550  if (sgref && root->processed_groupClause &&
5552  root->processed_groupClause) != NULL)
5553  {
5554  /*
5555  * It's a grouping column, so add it to the partial_target as-is.
5556  * (This allows the upper agg step to repeat the grouping calcs.)
5557  */
5558  add_column_to_pathtarget(partial_target, expr, sgref);
5559  }
5560  else
5561  {
5562  /*
5563  * Non-grouping column, so just remember the expression for later
5564  * call to pull_var_clause.
5565  */
5566  non_group_cols = lappend(non_group_cols, expr);
5567  }
5568 
5569  i++;
5570  }
5571 
5572  /*
5573  * If there's a HAVING clause, we'll need the Vars/Aggrefs it uses, too.
5574  */
5575  if (havingQual)
5576  non_group_cols = lappend(non_group_cols, havingQual);
5577 
5578  /*
5579  * Pull out all the Vars, PlaceHolderVars, and Aggrefs mentioned in
5580  * non-group cols (plus HAVING), and add them to the partial_target if not
5581  * already present. (An expression used directly as a GROUP BY item will
5582  * be present already.) Note this includes Vars used in resjunk items, so
5583  * we are covering the needs of ORDER BY and window specifications.
5584  */
5585  non_group_exprs = pull_var_clause((Node *) non_group_cols,
5589 
5590  add_new_columns_to_pathtarget(partial_target, non_group_exprs);
5591 
5592  /*
5593  * Adjust Aggrefs to put them in partial mode. At this point all Aggrefs
5594  * are at the top level of the target list, so we can just scan the list
5595  * rather than recursing through the expression trees.
5596  */
5597  foreach(lc, partial_target->exprs)
5598  {
5599  Aggref *aggref = (Aggref *) lfirst(lc);
5600 
5601  if (IsA(aggref, Aggref))
5602  {
5603  Aggref *newaggref;
5604 
5605  /*
5606  * We shouldn't need to copy the substructure of the Aggref node,
5607  * but flat-copy the node itself to avoid damaging other trees.
5608  */
5609  newaggref = makeNode(Aggref);
5610  memcpy(newaggref, aggref, sizeof(Aggref));
5611 
5612  /* For now, assume serialization is required */
5614 
5615  lfirst(lc) = newaggref;
5616  }
5617  }
5618 
5619  /* clean up cruft */
5620  list_free(non_group_exprs);
5621  list_free(non_group_cols);
5622 
5623  /* XXX this causes some redundant cost calculation ... */
5624  return set_pathtarget_cost_width(root, partial_target);
5625 }
5626 
5627 /*
5628  * mark_partial_aggref
5629  * Adjust an Aggref to make it represent a partial-aggregation step.
5630  *
5631  * The Aggref node is modified in-place; caller must do any copying required.
5632  */
5633 void
5635 {
5636  /* aggtranstype should be computed by this point */
5637  Assert(OidIsValid(agg->aggtranstype));
5638  /* ... but aggsplit should still be as the parser left it */
5639  Assert(agg->aggsplit == AGGSPLIT_SIMPLE);
5640 
5641  /* Mark the Aggref with the intended partial-aggregation mode */
5642  agg->aggsplit = aggsplit;
5643 
5644  /*
5645  * Adjust result type if needed. Normally, a partial aggregate returns
5646  * the aggregate's transition type; but if that's INTERNAL and we're
5647  * serializing, it returns BYTEA instead.
5648  */
5649  if (DO_AGGSPLIT_SKIPFINAL(aggsplit))
5650  {
5651  if (agg->aggtranstype == INTERNALOID && DO_AGGSPLIT_SERIALIZE(aggsplit))
5652  agg->aggtype = BYTEAOID;
5653  else
5654  agg->aggtype = agg->aggtranstype;
5655  }
5656 }
5657 
5658 /*
5659  * postprocess_setop_tlist
5660  * Fix up targetlist returned by plan_set_operations().
5661  *
5662  * We need to transpose sort key info from the orig_tlist into new_tlist.
5663  * NOTE: this would not be good enough if we supported resjunk sort keys
5664  * for results of set operations --- then, we'd need to project a whole
5665  * new tlist to evaluate the resjunk columns. For now, just ereport if we
5666  * find any resjunk columns in orig_tlist.
5667  */
5668 static List *
5669 postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
5670 {
5671  ListCell *l;
5672  ListCell *orig_tlist_item = list_head(orig_tlist);
5673 
5674  foreach(l, new_tlist)
5675  {
5676  TargetEntry *new_tle = lfirst_node(TargetEntry, l);
5677  TargetEntry *orig_tle;
5678 
5679  /* ignore resjunk columns in setop result */
5680  if (new_tle->resjunk)
5681  continue;
5682 
5683  Assert(orig_tlist_item != NULL);
5684  orig_tle = lfirst_node(TargetEntry, orig_tlist_item);
5685  orig_tlist_item = lnext(orig_tlist, orig_tlist_item);
5686  if (orig_tle->resjunk) /* should not happen */
5687  elog(ERROR, "resjunk output columns are not implemented");
5688  Assert(new_tle->resno == orig_tle->resno);
5689  new_tle->ressortgroupref = orig_tle->ressortgroupref;
5690  }
5691  if (orig_tlist_item != NULL)
5692  elog(ERROR, "resjunk output columns are not implemented");
5693  return new_tlist;
5694 }
5695 
5696 /*
5697  * optimize_window_clauses
5698  * Call each WindowFunc's prosupport function to see if we're able to
5699  * make any adjustments to any of the WindowClause's so that the executor
5700  * can execute the window functions in a more optimal way.
5701  *
5702  * Currently we only allow adjustments to the WindowClause's frameOptions. We
5703  * may allow more things to be done here in the future.
5704  */
5705 static void
5707 {
5708  List *windowClause = root->parse->windowClause;
5709  ListCell *lc;
5710 
5711  foreach(lc, windowClause)
5712  {
5714  ListCell *lc2;
5715  int optimizedFrameOptions = 0;
5716 
5717  Assert(wc->winref <= wflists->maxWinRef);
5718 
5719  /* skip any WindowClauses that have no WindowFuncs */
5720  if (wflists->windowFuncs[wc->winref] == NIL)
5721  continue;
5722 
5723  foreach(lc2, wflists->windowFuncs[wc->winref])
5724  {
5727  WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
5728  Oid prosupport;
5729 
5730  prosupport = get_func_support(wfunc->winfnoid);
5731 
5732  /* Check if there's a support function for 'wfunc' */
5733  if (!OidIsValid(prosupport))
5734  break; /* can't optimize this WindowClause */
5735 
5736  req.type = T_SupportRequestOptimizeWindowClause;
5737  req.window_clause = wc;
5738  req.window_func = wfunc;
5739  req.frameOptions = wc->frameOptions;
5740 
5741  /* call the support function */
5743  DatumGetPointer(OidFunctionCall1(prosupport,
5744  PointerGetDatum(&req)));
5745 
5746  /*
5747  * Skip to next WindowClause if the support function does not
5748  * support this request type.
5749  */
5750  if (res == NULL)
5751  break;
5752 
5753  /*
5754  * Save these frameOptions for the first WindowFunc for this
5755  * WindowClause.
5756  */
5757  if (foreach_current_index(lc2) == 0)
5758  optimizedFrameOptions = res->frameOptions;
5759 
5760  /*
5761  * On subsequent WindowFuncs, if the frameOptions are not the same
5762  * then we're unable to optimize the frameOptions for this
5763  * WindowClause.
5764  */
5765  else if (optimizedFrameOptions != res->frameOptions)
5766  break; /* skip to the next WindowClause, if any */
5767  }
5768 
5769  /* adjust the frameOptions if all WindowFunc's agree that it's ok */
5770  if (lc2 == NULL && wc->frameOptions != optimizedFrameOptions)
5771  {
5772  ListCell *lc3;
5773 
5774  /* apply the new frame options */
5775  wc->frameOptions = optimizedFrameOptions;
5776 
5777  /*
5778  * We now check to see if changing the frameOptions has caused
5779  * this WindowClause to be a duplicate of some other WindowClause.
5780  * This can only happen if we have multiple WindowClauses, so
5781  * don't bother if there's only 1.
5782  */
5783  if (list_length(windowClause) == 1)
5784  continue;
5785 
5786  /*
5787  * Do the duplicate check and reuse the existing WindowClause if
5788  * we find a duplicate.
5789  */
5790  foreach(lc3, windowClause)
5791  {
5792  WindowClause *existing_wc = lfirst_node(WindowClause, lc3);
5793 
5794  /* skip over the WindowClause we're currently editing */
5795  if (existing_wc == wc)
5796  continue;
5797 
5798  /*
5799  * Perform the same duplicate check that is done in
5800  * transformWindowFuncCall.
5801  */
5802  if (equal(wc->partitionClause, existing_wc->partitionClause) &&
5803  equal(wc->orderClause, existing_wc->orderClause) &&
5804  wc->frameOptions == existing_wc->frameOptions &&
5805  equal(wc->startOffset, existing_wc->startOffset) &&
5806  equal(wc->endOffset, existing_wc->endOffset))
5807  {
5808  ListCell *lc4;
5809 
5810  /*
5811  * Now move each WindowFunc in 'wc' into 'existing_wc'.
5812  * This required adjusting each WindowFunc's winref and
5813  * moving the WindowFuncs in 'wc' to the list of
5814  * WindowFuncs in 'existing_wc'.
5815  */
5816  foreach(lc4, wflists->windowFuncs[wc->winref])
5817  {
5818  WindowFunc *wfunc = lfirst_node(WindowFunc, lc4);
5819 
5820  wfunc->winref = existing_wc->winref;
5821  }
5822 
5823  /* move list items */
5824  wflists->windowFuncs[existing_wc->winref] = list_concat(wflists->windowFuncs[existing_wc->winref],
5825  wflists->windowFuncs[wc->winref]);
5826  wflists->windowFuncs[wc->winref] = NIL;
5827 
5828  /*
5829  * transformWindowFuncCall() should have made sure there
5830  * are no other duplicates, so we needn't bother looking
5831  * any further.
5832  */
5833  break;
5834  }
5835  }
5836  }
5837  }
5838 }
5839 
5840 /*
5841  * select_active_windows
5842  * Create a list of the "active" window clauses (ie, those referenced
5843  * by non-deleted WindowFuncs) in the order they are to be executed.
5844  */
5845 static List *
5847 {
5848  List *windowClause = root->parse->windowClause;
5849  List *result = NIL;
5850  ListCell *lc;
5851  int nActive = 0;
5853  * list_length(windowClause));
5854 
5855  /* First, construct an array of the active windows */
5856  foreach(lc, windowClause)
5857  {
5859 
5860  /* It's only active if wflists shows some related WindowFuncs */
5861  Assert(wc->winref <= wflists->maxWinRef);
5862  if (wflists->windowFuncs[wc->winref] == NIL)
5863  continue;
5864 
5865  actives[nActive].wc = wc; /* original clause */
5866 
5867  /*
5868  * For sorting, we want the list of partition keys followed by the
5869  * list of sort keys. But pathkeys construction will remove duplicates
5870  * between the two, so we can as well (even though we can't detect all
5871  * of the duplicates, since some may come from ECs - that might mean
5872  * we miss optimization chances here). We must, however, ensure that
5873  * the order of entries is preserved with respect to the ones we do
5874  * keep.
5875  *
5876  * partitionClause and orderClause had their own duplicates removed in
5877  * parse analysis, so we're only concerned here with removing
5878  * orderClause entries that also appear in partitionClause.
5879  */
5880  actives[nActive].uniqueOrder =
5882  wc->orderClause);
5883  nActive++;
5884  }
5885 
5886  /*
5887  * Sort active windows by their partitioning/ordering clauses, ignoring
5888  * any framing clauses, so that the windows that need the same sorting are
5889  * adjacent in the list. When we come to generate paths, this will avoid
5890  * inserting additional Sort nodes.
5891  *
5892  * This is how we implement a specific requirement from the SQL standard,
5893  * which says that when two or more windows are order-equivalent (i.e.
5894  * have matching partition and order clauses, even if their names or
5895  * framing clauses differ), then all peer rows must be presented in the
5896  * same order in all of them. If we allowed multiple sort nodes for such
5897  * cases, we'd risk having the peer rows end up in different orders in
5898  * equivalent windows due to sort instability. (See General Rule 4 of
5899  * <window clause> in SQL2008 - SQL2016.)
5900  *
5901  * Additionally, if the entire list of clauses of one window is a prefix
5902  * of another, put first the window with stronger sorting requirements.
5903  * This way we will first sort for stronger window, and won't have to sort
5904  * again for the weaker one.
5905  */
5906  qsort(actives, nActive, sizeof(WindowClauseSortData), common_prefix_cmp);
5907 
5908  /* build ordered list of the original WindowClause nodes */
5909  for (int i = 0; i < nActive; i++)
5910  result = lappend(result, actives[i].wc);
5911 
5912  pfree(actives);
5913 
5914  return result;
5915 }
5916 
5917 /*
5918  * common_prefix_cmp
5919  * QSort comparison function for WindowClauseSortData
5920  *
5921  * Sort the windows by the required sorting clauses. First, compare the sort
5922  * clauses themselves. Second, if one window's clauses are a prefix of another
5923  * one's clauses, put the window with more sort clauses first.
5924  *
5925  * We purposefully sort by the highest tleSortGroupRef first. Since
5926  * tleSortGroupRefs are assigned for the query's DISTINCT and ORDER BY first
5927  * and because here we sort the lowest tleSortGroupRefs last, if a
5928  * WindowClause is sharing a tleSortGroupRef with the query's DISTINCT or
5929  * ORDER BY clause, this makes it more likely that the final WindowAgg will
5930  * provide presorted input for the query's DISTINCT or ORDER BY clause, thus
5931  * reducing the total number of sorts required for the query.
5932  */
5933 static int
5934 common_prefix_cmp(const void *a, const void *b)
5935 {
5936  const WindowClauseSortData *wcsa = a;
5937  const WindowClauseSortData *wcsb = b;
5938  ListCell *item_a;
5939  ListCell *item_b;
5940 
5941  forboth(item_a, wcsa->uniqueOrder, item_b, wcsb->uniqueOrder)
5942  {
5945 
5946  if (sca->tleSortGroupRef > scb->tleSortGroupRef)
5947  return -1;
5948  else if (sca->tleSortGroupRef < scb->tleSortGroupRef)
5949  return 1;
5950  else if (sca->sortop > scb->sortop)
5951  return -1;
5952  else if (sca->sortop < scb->sortop)
5953  return 1;
5954  else if (sca->nulls_first && !scb->nulls_first)
5955  return -1;
5956  else if (!sca->nulls_first && scb->nulls_first)
5957  return 1;
5958  /* no need to compare eqop, since it is fully determined by sortop */
5959  }
5960 
5961  if (list_length(wcsa->uniqueOrder) > list_length(wcsb->uniqueOrder))
5962  return -1;
5963  else if (list_length(wcsa->uniqueOrder) < list_length(wcsb->uniqueOrder))
5964  return 1;
5965 
5966  return 0;
5967 }
5968 
5969 /*
5970  * make_window_input_target
5971  * Generate appropriate PathTarget for initial input to WindowAgg nodes.
5972  *
5973  * When the query has window functions, this function computes the desired
5974  * target to be computed by the node just below the first WindowAgg.
5975  * This tlist must contain all values needed to evaluate the window functions,
5976  * compute the final target list, and perform any required final sort step.
5977  * If multiple WindowAggs are needed, each intermediate one adds its window
5978  * function results onto this base tlist; only the topmost WindowAgg computes
5979  * the actual desired target list.
5980  *
5981  * This function is much like make_group_input_target, though not quite enough
5982  * like it to share code. As in that function, we flatten most expressions
5983  * into their component variables. But we do not want to flatten window
5984  * PARTITION BY/ORDER BY clauses, since that might result in multiple
5985  * evaluations of them, which would be bad (possibly even resulting in
5986  * inconsistent answers, if they contain volatile functions).
5987  * Also, we must not flatten GROUP BY clauses that were left unflattened by
5988  * make_group_input_target, because we may no longer have access to the
5989  * individual Vars in them.
5990  *
5991  * Another key difference from make_group_input_target is that we don't
5992  * flatten Aggref expressions, since those are to be computed below the
5993  * window functions and just referenced like Vars above that.
5994  *
5995  * 'final_target' is the query's final target list (in PathTarget form)
5996  * 'activeWindows' is the list of active windows previously identified by
5997  * select_active_windows.
5998  *
5999  * The result is the PathTarget to be computed by the plan node immediately
6000  * below the first WindowAgg node.
6001  */
6002 static PathTarget *
6004  PathTarget *final_target,
6005  List *activeWindows)
6006 {
6007  PathTarget *input_target;
6008  Bitmapset *sgrefs;
6009  List *flattenable_cols;
6010  List *flattenable_vars;
6011  int i;
6012  ListCell *lc;
6013 
6014  Assert(root->parse->hasWindowFuncs);
6015 
6016  /*
6017  * Collect the sortgroupref numbers of window PARTITION/ORDER BY clauses
6018  * into a bitmapset for convenient reference below.
6019  */
6020  sgrefs = NULL;
6021  foreach(lc, activeWindows)
6022  {
6024  ListCell *lc2;
6025 
6026  foreach(lc2, wc->partitionClause)
6027  {
6029 
6030  sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
6031  }
6032  foreach(lc2, wc->orderClause)
6033  {
6035 
6036  sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
6037  }
6038  }
6039 
6040  /* Add in sortgroupref numbers of GROUP BY clauses, too */
6041  foreach(lc, root->processed_groupClause)
6042  {
6044 
6045  sgrefs = bms_add_member(sgrefs, grpcl->tleSortGroupRef);
6046  }
6047 
6048  /*
6049  * Construct a target containing all the non-flattenable targetlist items,
6050  * and save aside the others for a moment.
6051  */
6052  input_target = create_empty_pathtarget();
6053  flattenable_cols = NIL;
6054 
6055  i = 0;
6056  foreach(lc, final_target->exprs)
6057  {
6058  Expr *expr = (Expr *) lfirst(lc);
6059  Index sgref = get_pathtarget_sortgroupref(final_target, i);
6060 
6061  /*
6062  * Don't want to deconstruct window clauses or GROUP BY items. (Note
6063  * that such items can't contain window functions, so it's okay to
6064  * compute them below the WindowAgg nodes.)
6065  */
6066  if (sgref != 0 && bms_is_member(sgref, sgrefs))
6067  {
6068  /*
6069  * Don't want to deconstruct this value, so add it to the input
6070  * target as-is.
6071  */
6072  add_column_to_pathtarget(input_target, expr, sgref);
6073  }
6074  else
6075  {
6076  /*
6077  * Column is to be flattened, so just remember the expression for
6078  * later call to pull_var_clause.
6079  */
6080  flattenable_cols = lappend(flattenable_cols, expr);
6081  }
6082 
6083  i++;
6084  }
6085 
6086  /*
6087  * Pull out all the Vars and Aggrefs mentioned in flattenable columns, and
6088  * add them to the input target if not already present. (Some might be
6089  * there already because they're used directly as window/group clauses.)
6090  *
6091  * Note: it's essential to use PVC_INCLUDE_AGGREGATES here, so that any
6092  * Aggrefs are placed in the Agg node's tlist and not left to be computed
6093  * at higher levels. On the other hand, we should recurse into
6094  * WindowFuncs to make sure their input expressions are available.
6095  */
6096  flattenable_vars = pull_var_clause((Node *) flattenable_cols,
6100  add_new_columns_to_pathtarget(input_target, flattenable_vars);
6101 
6102  /* clean up cruft */
6103  list_free(flattenable_vars);
6104  list_free(flattenable_cols);
6105 
6106  /* XXX this causes some redundant cost calculation ... */
6107  return set_pathtarget_cost_width(root, input_target);
6108 }
6109 
6110 /*
6111  * make_pathkeys_for_window
6112  * Create a pathkeys list describing the required input ordering
6113  * for the given WindowClause.
6114  *
6115  * Modifies wc's partitionClause to remove any clauses which are deemed
6116  * redundant by the pathkey logic.
6117  *
6118  * The required ordering is first the PARTITION keys, then the ORDER keys.
6119  * In the future we might try to implement windowing using hashing, in which
6120  * case the ordering could be relaxed, but for now we always sort.
6121  */
6122 static List *
6124  List *tlist)
6125 {
6126  List *window_pathkeys = NIL;
6127 
6128  /* Throw error if can't sort */
6130  ereport(ERROR,
6131  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6132  errmsg("could not implement window PARTITION BY"),
6133  errdetail("Window partitioning columns must be of sortable datatypes.")));
6135  ereport(ERROR,
6136  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6137  errmsg("could not implement window ORDER BY"),
6138  errdetail("Window ordering columns must be of sortable datatypes.")));
6139 
6140  /*
6141  * First fetch the pathkeys for the PARTITION BY clause. We can safely
6142  * remove any clauses from the wc->partitionClause for redundant pathkeys.
6143  */
6144  if (wc->partitionClause != NIL)
6145  {
6146  bool sortable;
6147 
6148  window_pathkeys = make_pathkeys_for_sortclauses_extended(root,
6149  &wc->partitionClause,
6150  tlist,
6151  true,
6152  &sortable,
6153  false);
6154 
6155  Assert(sortable);
6156  }
6157 
6158  /*
6159  * In principle, we could also consider removing redundant ORDER BY items
6160  * too as doing so does not alter the result of peer row checks done by
6161  * the executor. However, we must *not* remove the ordering column for
6162  * RANGE OFFSET cases, as the executor needs that for in_range tests even
6163  * if it's known to be equal to some partitioning column.
6164  */
6165  if (wc->orderClause != NIL)
6166  {
6167  List *orderby_pathkeys;
6168 
6169  orderby_pathkeys = make_pathkeys_for_sortclauses(root,
6170  wc->orderClause,
6171  tlist);
6172 
6173  /* Okay, make the combined pathkeys */
6174  if (window_pathkeys != NIL)
6175  window_pathkeys = append_pathkeys(window_pathkeys, orderby_pathkeys);
6176  else
6177  window_pathkeys = orderby_pathkeys;
6178  }
6179 
6180  return window_pathkeys;
6181 }
6182 
6183 /*
6184  * make_sort_input_target
6185  * Generate appropriate PathTarget for initial input to Sort step.
6186  *
6187  * If the query has ORDER BY, this function chooses the target to be computed
6188  * by the node just below the Sort (and DISTINCT, if any, since Unique can't
6189  * project) steps. This might or might not be identical to the query's final
6190  * output target.
6191  *
6192  * The main argument for keeping the sort-input tlist the same as the final
6193  * is that we avoid a separate projection node (which will be needed if
6194  * they're different, because Sort can't project). However, there are also
6195  * advantages to postponing tlist evaluation till after the Sort: it ensures
6196  * a consistent order of evaluation for any volatile functions in the tlist,
6197  * and if there's also a LIMIT, we can stop the query without ever computing
6198  * tlist functions for later rows, which is beneficial for both volatile and
6199  * expensive functions.
6200  *
6201  * Our current policy is to postpone volatile expressions till after the sort
6202  * unconditionally (assuming that that's possible, ie they are in plain tlist
6203  * columns and not ORDER BY/GROUP BY/DISTINCT columns). We also prefer to
6204  * postpone set-returning expressions, because running them beforehand would
6205  * bloat the sort dataset, and because it might cause unexpected output order
6206  * if the sort isn't stable. However there's a constraint on that: all SRFs
6207  * in the tlist should be evaluated at the same plan step, so that they can
6208  * run in sync in nodeProjectSet. So if any SRFs are in sort columns, we
6209  * mustn't postpone any SRFs. (Note that in principle that policy should
6210  * probably get applied to the group/window input targetlists too, but we
6211  * have not done that historically.) Lastly, expensive expressions are
6212  * postponed if there is a LIMIT, or if root->tuple_fraction shows that
6213  * partial evaluation of the query is possible (if neither is true, we expect
6214  * to have to evaluate the expressions for every row anyway), or if there are
6215  * any volatile or set-returning expressions (since once we've put in a
6216  * projection at all, it won't cost any more to postpone more stuff).
6217  *
6218  * Another issue that could potentially be considered here is that
6219  * evaluating tlist expressions could result in data that's either wider
6220  * or narrower than the input Vars, thus changing the volume of data that
6221  * has to go through the Sort. However, we usually have only a very bad
6222  * idea of the output width of any expression more complex than a Var,
6223  * so for now it seems too risky to try to optimize on that basis.
6224  *
6225  * Note that if we do produce a modified sort-input target, and then the
6226  * query ends up not using an explicit Sort, no particular harm is done:
6227  * we'll initially use the modified target for the preceding path nodes,
6228  * but then change them to the final target with apply_projection_to_path.
6229  * Moreover, in such a case the guarantees about evaluation order of
6230  * volatile functions still hold, since the rows are sorted already.
6231  *
6232  * This function has some things in common with make_group_input_target and
6233  * make_window_input_target, though the detailed rules for what to do are
6234  * different. We never flatten/postpone any grouping or ordering columns;
6235  * those are needed before the sort. If we do flatten a particular
6236  * expression, we leave Aggref and WindowFunc nodes alone, since those were
6237  * computed earlier.
6238  *
6239  * 'final_target' is the query's final target list (in PathTarget form)
6240  * 'have_postponed_srfs' is an output argument, see below
6241  *
6242  * The result is the PathTarget to be computed by the plan node immediately
6243  * below the Sort step (and the Distinct step, if any). This will be
6244  * exactly final_target if we decide a projection step wouldn't be helpful.
6245  *
6246  * In addition, *have_postponed_srfs is set to true if we choose to postpone
6247  * any set-returning functions to after the Sort.
6248  */
6249 static PathTarget *
6251  PathTarget *final_target,
6252  bool *have_postponed_srfs)
6253 {
6254  Query *parse = root->parse;
6255  PathTarget *input_target;
6256  int ncols;
6257  bool *col_is_srf;
6258  bool *postpone_col;
6259  bool have_srf;
6260  bool have_volatile;
6261  bool have_expensive;
6262  bool have_srf_sortcols;
6263  bool postpone_srfs;
6264  List *postponable_cols;
6265  List *postponable_vars;
6266  int i;
6267  ListCell *lc;
6268 
6269  /* Shouldn't get here unless query has ORDER BY */
6270  Assert(parse->sortClause);
6271 
6272  *have_postponed_srfs = false; /* default result */
6273 
6274  /* Inspect tlist and collect per-column information */
6275  ncols = list_length(final_target->exprs);
6276  col_is_srf = (bool *) palloc0(ncols * sizeof(bool));
6277  postpone_col = (bool *) palloc0(ncols * sizeof(bool));
6278  have_srf = have_volatile = have_expensive = have_srf_sortcols = false;
6279 
6280  i = 0;
6281  foreach(lc, final_target->exprs)
6282  {
6283  Expr *expr = (Expr *) lfirst(lc);
6284 
6285  /*
6286  * If the column has a sortgroupref, assume it has to be evaluated
6287  * before sorting. Generally such columns would be ORDER BY, GROUP
6288  * BY, etc targets. One exception is columns that were removed from
6289  * GROUP BY by remove_useless_groupby_columns() ... but those would
6290  * only be Vars anyway. There don't seem to be any cases where it
6291  * would be worth the trouble to double-check.
6292  */
6293  if (get_pathtarget_sortgroupref(final_target, i) == 0)
6294  {
6295  /*
6296  * Check for SRF or volatile functions. Check the SRF case first
6297  * because we must know whether we have any postponed SRFs.
6298  */
6299  if (parse->hasTargetSRFs &&
6300  expression_returns_set((Node *) expr))
6301  {
6302  /* We'll decide below whether these are postponable */
6303  col_is_srf[i] = true;
6304  have_srf = true;
6305  }
6306  else if (contain_volatile_functions((Node *) expr))
6307  {
6308  /* Unconditionally postpone */
6309  postpone_col[i] = true;
6310  have_volatile = true;
6311  }
6312  else
6313  {
6314  /*
6315  * Else check the cost. XXX it's annoying to have to do this
6316  * when set_pathtarget_cost_width() just did it. Refactor to
6317  * allow sharing the work?
6318  */
6319  QualCost cost;
6320 
6321  cost_qual_eval_node(&cost, (Node *) expr, root);
6322 
6323  /*
6324  * We arbitrarily define "expensive" as "more than 10X
6325  * cpu_operator_cost". Note this will take in any PL function
6326  * with default cost.
6327  */
6328  if (cost.per_tuple > 10 * cpu_operator_cost)
6329  {
6330  postpone_col[i] = true;
6331  have_expensive = true;
6332  }
6333  }
6334  }
6335  else
6336  {
6337  /* For sortgroupref cols, just check if any contain SRFs */
6338  if (!have_srf_sortcols &&
6339  parse->hasTargetSRFs &&
6340  expression_returns_set((Node *) expr))
6341  have_srf_sortcols = true;
6342  }
6343 
6344  i++;
6345  }
6346 
6347  /*
6348  * We can postpone SRFs if we have some but none are in sortgroupref cols.
6349  */
6350  postpone_srfs = (have_srf && !have_srf_sortcols);
6351 
6352  /*
6353  * If we don't need a post-sort projection, just return final_target.
6354  */
6355  if (!(postpone_srfs || have_volatile ||
6356  (have_expensive &&
6357  (parse->limitCount || root->tuple_fraction > 0))))
6358  return final_target;
6359 
6360  /*
6361  * Report whether the post-sort projection will contain set-returning
6362  * functions. This is important because it affects whether the Sort can
6363  * rely on the query's LIMIT (if any) to bound the number of rows it needs
6364  * to return.
6365  */
6366  *have_postponed_srfs = postpone_srfs;
6367 
6368  /*
6369  * Construct the sort-input target, taking all non-postponable columns and
6370  * then adding Vars, PlaceHolderVars, Aggrefs, and WindowFuncs found in
6371  * the postponable ones.
6372  */
6373  input_target = create_empty_pathtarget();
6374  postponable_cols = NIL;
6375 
6376  i = 0;
6377  foreach(lc, final_target->exprs)
6378  {
6379  Expr *expr = (Expr *) lfirst(lc);
6380 
6381  if (postpone_col[i] || (postpone_srfs && col_is_srf[i]))
6382  postponable_cols = lappend(postponable_cols, expr);
6383  else
6384  add_column_to_pathtarget(input_target, expr,
6385  get_pathtarget_sortgroupref(final_target, i));
6386 
6387  i++;
6388  }
6389 
6390  /*
6391  * Pull out all the Vars, Aggrefs, and WindowFuncs mentioned in
6392  * postponable columns, and add them to the sort-input target if not
6393  * already present. (Some might be there already.) We mustn't
6394  * deconstruct Aggrefs or WindowFuncs here, since the projection node
6395  * would be unable to recompute them.
6396  */
6397  postponable_vars = pull_var_clause((Node *) postponable_cols,
6401  add_new_columns_to_pathtarget(input_target, postponable_vars);
6402 
6403  /* clean up cruft */
6404  list_free(postponable_vars);
6405  list_free(postponable_cols);
6406 
6407  /* XXX this represents even more redundant cost calculation ... */
6408  return set_pathtarget_cost_width(root, input_target);
6409 }
6410 
6411 /*
6412  * get_cheapest_fractional_path
6413  * Find the cheapest path for retrieving a specified fraction of all
6414  * the tuples expected to be returned by the given relation.
6415  *
6416  * We interpret tuple_fraction the same way as grouping_planner.
6417  *
6418  * We assume set_cheapest() has been run on the given rel.
6419  */
6420 Path *
6421 get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
6422 {
6423  Path *best_path = rel->cheapest_total_path;
6424  ListCell *l;
6425 
6426  /* If all tuples will be retrieved, just return the cheapest-total path */
6427  if (tuple_fraction <= 0.0)
6428  return best_path;
6429 
6430  /* Convert absolute # of tuples to a fraction; no need to clamp to 0..1 */
6431  if (tuple_fraction >= 1.0 && best_path->rows > 0)
6432  tuple_fraction /= best_path->rows;
6433 
6434  foreach(l, rel->pathlist)
6435  {
6436  Path *path = (Path *) lfirst(l);
6437 
6438  if (path == rel->cheapest_total_path ||
6439  compare_fractional_path_costs(best_path, path, tuple_fraction) <= 0)
6440  continue;
6441 
6442  best_path = path;
6443  }
6444 
6445  return best_path;
6446 }
6447 
6448 /*
6449  * adjust_paths_for_srfs
6450  * Fix up the Paths of the given upperrel to handle tSRFs properly.
6451  *
6452  * The executor can only handle set-returning functions that appear at the
6453  * top level of the targetlist of a ProjectSet plan node. If we have any SRFs
6454  * that are not at top level, we need to split up the evaluation into multiple
6455  * plan levels in which each level satisfies this constraint. This function
6456  * modifies each Path of an upperrel that (might) compute any SRFs in its
6457  * output tlist to insert appropriate projection steps.
6458  *
6459  * The given targets and targets_contain_srfs lists are from
6460  * split_pathtarget_at_srfs(). We assume the existing Paths emit the first
6461  * target in targets.
6462  */
6463 static void
6465  List *targets, List *targets_contain_srfs)
6466 {
6467  ListCell *lc;
6468 
6469  Assert(list_length(targets) == list_length(targets_contain_srfs));
6470  Assert(!linitial_int(targets_contain_srfs));
6471 
6472  /* If no SRFs appear at this plan level, nothing to do */
6473  if (list_length(targets) == 1)
6474  return;
6475 
6476  /*
6477  * Stack SRF-evaluation nodes atop each path for the rel.
6478  *
6479  * In principle we should re-run set_cheapest() here to identify the
6480  * cheapest path, but it seems unlikely that adding the same tlist eval
6481  * costs to all the paths would change that, so we don't bother. Instead,
6482  * just assume that the cheapest-startup and cheapest-total paths remain
6483  * so. (There should be no parameterized paths anymore, so we needn't
6484  * worry about updating cheapest_parameterized_paths.)
6485  */
6486  foreach(lc, rel->pathlist)
6487  {
6488  Path *subpath = (Path *) lfirst(lc);
6489  Path *newpath = subpath;
6490  ListCell *lc1,
6491  *lc2;
6492 
6493  Assert(subpath->param_info == NULL);
6494  forboth(lc1, targets, lc2, targets_contain_srfs)
6495  {
6496  PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6497  bool contains_srfs = (bool) lfirst_int(lc2);
6498 
6499  /* If this level doesn't contain SRFs, do regular projection */
6500  if (contains_srfs)
6501  newpath = (Path *) create_set_projection_path(root,
6502  rel,
6503  newpath,
6504  thistarget);
6505  else
6506  newpath = (Path *) apply_projection_to_path(root,
6507  rel,
6508  newpath,
6509  thistarget);
6510  }
6511  lfirst(lc) = newpath;
6512  if (subpath == rel->cheapest_startup_path)
6513  rel->cheapest_startup_path = newpath;
6514  if (subpath == rel->cheapest_total_path)
6515  rel->cheapest_total_path = newpath;
6516  }
6517 
6518  /* Likewise for partial paths, if any */
6519  foreach(lc, rel->partial_pathlist)
6520  {
6521  Path *subpath = (Path *) lfirst(lc);
6522  Path *newpath = subpath;
6523  ListCell *lc1,
6524  *lc2;
6525 
6526  Assert(subpath->param_info == NULL);
6527  forboth(lc1, targets, lc2, targets_contain_srfs)
6528  {
6529  PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6530  bool contains_srfs = (bool) lfirst_int(lc2);
6531 
6532  /* If this level doesn't contain SRFs, do regular projection */
6533  if (contains_srfs)
6534  newpath = (Path *) create_set_projection_path(root,
6535  rel,
6536  newpath,
6537  thistarget);
6538  else
6539  {
6540  /* avoid apply_projection_to_path, in case of multiple refs */
6541  newpath = (Path *) create_projection_path(root,
6542  rel,
6543  newpath,
6544  thistarget);
6545  }
6546  }
6547  lfirst(lc) = newpath;
6548  }
6549 }
6550 
6551 /*
6552  * expression_planner
6553  * Perform planner's transformations on a standalone expression.
6554  *
6555  * Various utility commands need to evaluate expressions that are not part
6556  * of a plannable query. They can do so using the executor's regular
6557  * expression-execution machinery, but first the expression has to be fed
6558  * through here to transform it from parser output to something executable.
6559  *
6560  * Currently, we disallow sublinks in standalone expressions, so there's no
6561  * real "planning" involved here. (That might not always be true though.)
6562  * What we must do is run eval_const_expressions to ensure that any function
6563  * calls are converted to positional notation and function default arguments
6564  * get inserted. The fact that constant subexpressions get simplified is a
6565  * side-effect that is useful when the expression will get evaluated more than
6566  * once. Also, we must fix operator function IDs.
6567  *
6568  * This does not return any information about dependencies of the expression.
6569  * Hence callers should use the results only for the duration of the current
6570  * query. Callers that would like to cache the results for longer should use
6571  * expression_planner_with_deps, probably via the plancache.
6572  *
6573  * Note: this must not make any damaging changes to the passed-in expression
6574  * tree. (It would actually be okay to apply fix_opfuncids to it, but since
6575  * we first do an expression_tree_mutator-based walk, what is returned will
6576  * be a new node tree.) The result is constructed in the current memory
6577  * context; beware that this can leak a lot of additional stuff there, too.
6578  */
6579 Expr *
6581 {
6582  Node *result;
6583 
6584  /*
6585  * Convert named-argument function calls, insert default arguments and
6586  * simplify constant subexprs
6587  */
6588  result = eval_const_expressions(NULL, (Node *) expr);
6589 
6590  /* Fill in opfuncid values if missing */
6591  fix_opfuncids(result);
6592 
6593  return (Expr *) result;
6594 }
6595 
6596 /*
6597  * expression_planner_with_deps
6598  * Perform planner's transformations on a standalone expression,
6599  * returning expression dependency information along with the result.
6600  *
6601  * This is identical to expression_planner() except that it also returns
6602  * information about possible dependencies of the expression, ie identities of
6603  * objects whose definitions affect the result. As in a PlannedStmt, these
6604  * are expressed as a list of relation Oids and a list of PlanInvalItems.
6605  */
6606 Expr *
6608  List **relationOids,
6609  List **invalItems)
6610 {
6611  Node *result;
6612  PlannerGlobal glob;
6613  PlannerInfo root;
6614 
6615  /* Make up dummy planner state so we can use setrefs machinery */
6616  MemSet(&glob, 0, sizeof(glob));
6617  glob.type = T_PlannerGlobal;
6618  glob.relationOids = NIL;
6619  glob.invalItems = NIL;
6620 
6621  MemSet(&root, 0, sizeof(root));
6622  root.type = T_PlannerInfo;
6623  root.glob = &glob;
6624 
6625  /*
6626  * Convert named-argument function calls, insert default arguments and
6627  * simplify constant subexprs. Collect identities of inlined functions
6628  * and elided domains, too.
6629  */
6630  result = eval_const_expressions(&root, (Node *) expr);
6631 
6632  /* Fill in opfuncid values if missing */
6633  fix_opfuncids(result);
6634 
6635  /*
6636  * Now walk the finished expression to find anything else we ought to
6637  * record as an expression dependency.
6638  */
6639  (void) extract_query_dependencies_walker(result, &root);
6640 
6641  *relationOids = glob.relationOids;
6642  *invalItems = glob.invalItems;
6643 
6644  return (Expr *) result;
6645 }
6646 
6647 
6648 /*
6649  * plan_cluster_use_sort
6650  * Use the planner to decide how CLUSTER should implement sorting
6651  *
6652  * tableOid is the OID of a table to be clustered on its index indexOid
6653  * (which is already known to be a btree index). Decide whether it's
6654  * cheaper to do an indexscan or a seqscan-plus-sort to execute the CLUSTER.
6655  * Return true to use sorting, false to use an indexscan.
6656  *
6657  * Note: caller had better already hold some type of lock on the table.
6658  */
6659 bool
6660 plan_cluster_use_sort(Oid tableOid, Oid indexOid)
6661 {
6662  PlannerInfo *root;
6663  Query *query;
6664  PlannerGlobal *glob;
6665  RangeTblEntry *rte;
6666  RelOptInfo *rel;
6667  IndexOptInfo *indexInfo;
6668  QualCost indexExprCost;
6669  Cost comparisonCost;
6670  Path *seqScanPath;
6671  Path seqScanAndSortPath;
6672  IndexPath *indexScanPath;
6673  ListCell *lc;
6674 
6675  /* We can short-circuit the cost comparison if indexscans are disabled */
6676  if (!enable_indexscan)
6677  return true; /* use sort */
6678 
6679  /* Set up mostly-dummy planner state */
6680  query = makeNode(Query);
6681  query->commandType = CMD_SELECT;
6682 
6683  glob = makeNode(PlannerGlobal);
6684 
6686  root->parse = query;
6687  root->glob = glob;
6688  root->query_level = 1;
6689  root->planner_cxt = CurrentMemoryContext;
6690  root->wt_param_id = -1;
6691  root->join_domains = list_make1(makeNode(JoinDomain));
6692 
6693  /* Build a minimal RTE for the rel */
6694  rte = makeNode(RangeTblEntry);
6695  rte->rtekind = RTE_RELATION;
6696  rte->relid = tableOid;
6697  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6698  rte->rellockmode = AccessShareLock;
6699  rte->lateral = false;
6700  rte->inh = false;
6701  rte->inFromCl = true;
6702  query->rtable = list_make1(rte);
6703  addRTEPermissionInfo(&query->rteperminfos, rte);
6704 
6705  /* Set up RTE/RelOptInfo arrays */
6707 
6708  /* Build RelOptInfo */
6709  rel = build_simple_rel(root, 1, NULL);
6710 
6711  /* Locate IndexOptInfo for the target index */
6712  indexInfo = NULL;
6713  foreach(lc, rel->indexlist)
6714  {
6715  indexInfo = lfirst_node(IndexOptInfo, lc);
6716  if (indexInfo->indexoid == indexOid)
6717  break;
6718  }
6719 
6720  /*
6721  * It's possible that get_relation_info did not generate an IndexOptInfo
6722  * for the desired index; this could happen if it's not yet reached its
6723  * indcheckxmin usability horizon, or if it's a system index and we're
6724  * ignoring system indexes. In such cases we should tell CLUSTER to not
6725  * trust the index contents but use seqscan-and-sort.
6726  */
6727  if (lc == NULL) /* not in the list? */
6728  return true; /* use sort */
6729 
6730  /*
6731  * Rather than doing all the pushups that would be needed to use
6732  * set_baserel_size_estimates, just do a quick hack for rows and width.
6733  */
6734  rel->rows = rel->tuples;
6735  rel->reltarget->width = get_relation_data_width(tableOid, NULL);
6736 
6737  root->total_table_pages = rel->pages;
6738 
6739  /*
6740  * Determine eval cost of the index expressions, if any. We need to
6741  * charge twice that amount for each tuple comparison that happens during
6742  * the sort, since tuplesort.c will have to re-evaluate the index
6743  * expressions each time. (XXX that's pretty inefficient...)
6744  */
6745  cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
6746  comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
6747 
6748  /* Estimate the cost of seq scan + sort */
6749  seqScanPath = create_seqscan_path(root, rel, NULL, 0);
6750  cost_sort(&seqScanAndSortPath, root, NIL,
6751  seqScanPath->total_cost, rel->tuples, rel->reltarget->width,
6752  comparisonCost, maintenance_work_mem, -1.0);
6753 
6754  /* Estimate the cost of index scan */
6755  indexScanPath = create_index_path(root, indexInfo,
6756  NIL, NIL, NIL, NIL,
6757  ForwardScanDirection, false,
6758  NULL, 1.0, false);
6759 
6760  return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
6761 }
6762 
6763 /*
6764  * plan_create_index_workers
6765  * Use the planner to decide how many parallel worker processes
6766  * CREATE INDEX should request for use
6767  *
6768  * tableOid is the table on which the index is to be built. indexOid is the
6769  * OID of an index to be created or reindexed (which must be a btree index).
6770  *
6771  * Return value is the number of parallel worker processes to request. It
6772  * may be unsafe to proceed if this is 0. Note that this does not include the
6773  * leader participating as a worker (value is always a number of parallel
6774  * worker processes).
6775  *
6776  * Note: caller had better already hold some type of lock on the table and
6777  * index.
6778  */
6779 int
6780 plan_create_index_workers(Oid tableOid, Oid indexOid)
6781 {
6782  PlannerInfo *root;
6783  Query *query;
6784  PlannerGlobal *glob;
6785  RangeTblEntry *rte;
6786  Relation heap;
6787  Relation index;
6788  RelOptInfo *rel;
6789  int parallel_workers;
6790  BlockNumber heap_blocks;
6791  double reltuples;
6792  double allvisfrac;
6793 
6794  /*
6795  * We don't allow performing parallel operation in standalone backend or
6796  * when parallelism is disabled.
6797  */
6799  return 0;
6800 
6801  /* Set up largely-dummy planner state */
6802  query = makeNode(Query);
6803  query->commandType = CMD_SELECT;
6804 
6805  glob = makeNode(PlannerGlobal);
6806 
6808  root->parse = query;
6809  root->glob = glob;
6810  root->query_level = 1;
6811  root->planner_cxt = CurrentMemoryContext;
6812  root->wt_param_id = -1;
6813  root->join_domains = list_make1(makeNode(JoinDomain));
6814 
6815  /*
6816  * Build a minimal RTE.
6817  *
6818  * Mark the RTE with inh = true. This is a kludge to prevent
6819  * get_relation_info() from fetching index info, which is necessary
6820  * because it does not expect that any IndexOptInfo is currently
6821  * undergoing REINDEX.
6822  */
6823  rte = makeNode(RangeTblEntry);
6824  rte->rtekind = RTE_RELATION;
6825  rte->relid = tableOid;
6826  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6827  rte->rellockmode = AccessShareLock;
6828  rte->lateral = false;
6829  rte->inh = true;
6830  rte->inFromCl = true;
6831  query->rtable = list_make1(rte);
6832  addRTEPermissionInfo(&query->rteperminfos, rte);
6833 
6834  /* Set up RTE/RelOptInfo arrays */
6836 
6837  /* Build RelOptInfo */
6838  rel = build_simple_rel(root, 1, NULL);
6839 
6840  /* Rels are assumed already locked by the caller */
6841  heap = table_open(tableOid, NoLock);
6842  index = index_open(indexOid, NoLock);
6843 
6844  /*
6845  * Determine if it's safe to proceed.
6846  *
6847  * Currently, parallel workers can't access the leader's temporary tables.
6848  * Furthermore, any index predicate or index expressions must be parallel
6849  * safe.
6850  */
6851  if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
6854  {
6855  parallel_workers = 0;
6856  goto done;
6857  }
6858 
6859  /*
6860  * If parallel_workers storage parameter is set for the table, accept that
6861  * as the number of parallel worker processes to launch (though still cap
6862  * at max_parallel_maintenance_workers). Note that we deliberately do not
6863  * consider any other factor when parallel_workers is set. (e.g., memory
6864  * use by workers.)
6865  */
6866  if (rel->rel_parallel_workers != -1)
6867  {
6868  parallel_workers = Min(rel->rel_parallel_workers,
6870  goto done;
6871  }
6872 
6873  /*
6874  * Estimate heap relation size ourselves, since rel->pages cannot be
6875  * trusted (heap RTE was marked as inheritance parent)
6876  */
6877  estimate_rel_size(heap, NULL, &heap_blocks, &reltuples, &allvisfrac);
6878 
6879  /*
6880  * Determine number of workers to scan the heap relation using generic
6881  * model
6882  */
6883  parallel_workers = compute_parallel_worker(rel, heap_blocks, -1,
6885 
6886  /*
6887  * Cap workers based on available maintenance_work_mem as needed.
6888  *
6889  * Note that each tuplesort participant receives an even share of the
6890  * total maintenance_work_mem budget. Aim to leave participants
6891  * (including the leader as a participant) with no less than 32MB of
6892  * memory. This leaves cases where maintenance_work_mem is set to 64MB
6893  * immediately past the threshold of being capable of launching a single
6894  * parallel worker to sort.
6895  */
6896  while (parallel_workers > 0 &&
6897  maintenance_work_mem / (parallel_workers + 1) < 32768L)
6898  parallel_workers--;
6899 
6900 done:
6902  table_close(heap, NoLock);
6903 
6904  return parallel_workers;
6905 }
6906 
6907 /*
6908  * make_ordered_path
6909  * Return a path ordered by 'pathkeys' based on the given 'path'. May
6910  * return NULL if it doesn't make sense to generate an ordered path in
6911  * this case.
6912  */
6913 static Path *
6915  Path *cheapest_path, List *pathkeys)
6916 {
6917  bool is_sorted;
6918  int presorted_keys;
6919 
6920  is_sorted = pathkeys_count_contained_in(pathkeys,
6921  path->pathkeys,
6922  &presorted_keys);
6923 
6924  if (!is_sorted)
6925  {
6926  /*
6927  * Try at least sorting the cheapest path and also try incrementally
6928  * sorting any path which is partially sorted already (no need to deal
6929  * with paths which have presorted keys when incremental sort is
6930  * disabled unless it's the cheapest input path).
6931  */
6932  if (path != cheapest_path &&
6933  (presorted_keys == 0 || !enable_incremental_sort))
6934  return NULL;
6935 
6936  /*
6937  * We've no need to consider both a sort and incremental sort. We'll
6938  * just do a sort if there are no presorted keys and an incremental
6939  * sort when there are presorted keys.
6940  */
6941  if (presorted_keys == 0 || !enable_incremental_sort)
6942  path = (Path *) create_sort_path(root,
6943  rel,
6944  path,
6945  pathkeys,
6946  -1.0);
6947  else
6949  rel,
6950  path,
6951  pathkeys,
6952  presorted_keys,
6953  -1.0);
6954  }
6955 
6956  return path;
6957 }
6958 
6959 /*
6960  * add_paths_to_grouping_rel
6961  *
6962  * Add non-partial paths to grouping relation.
6963  */
6964 static void
6966  RelOptInfo *grouped_rel,
6967  RelOptInfo *partially_grouped_rel,
6968  const AggClauseCosts *agg_costs,
6969  grouping_sets_data *gd, double dNumGroups,
6970  GroupPathExtraData *extra)
6971 {
6972  Query *parse = root->parse;
6973  Path *cheapest_path = input_rel->cheapest_total_path;
6974  ListCell *lc;
6975  bool can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
6976  bool can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
6977  List *havingQual = (List *) extra->havingQual;
6978  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
6979 
6980  if (can_sort)
6981  {
6982  /*
6983  * Use any available suitably-sorted path as input, and also consider
6984  * sorting the cheapest-total path and incremental sort on any paths
6985  * with presorted keys.
6986  */
6987  foreach(lc, input_rel->pathlist)
6988  {
6989  ListCell *lc2;
6990  Path *path = (Path *) lfirst(lc);
6991  Path *path_save = path;
6992  List *pathkey_orderings = NIL;
6993 
6994  /* generate alternative group orderings that might be useful */
6995  pathkey_orderings = get_useful_group_keys_orderings(root, path);
6996 
6997  Assert(list_length(pathkey_orderings) > 0);
6998 
6999  foreach(lc2, pathkey_orderings)
7000  {
7001  GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7002 
7003  /* restore the path (we replace it in the loop) */
7004  path = path_save;
7005 
7006  path = make_ordered_path(root,
7007  grouped_rel,
7008  path,
7009  cheapest_path,
7010  info->pathkeys);
7011  if (path == NULL)
7012  continue;
7013 
7014  /* Now decide what to stick atop it */
7015  if (parse->groupingSets)
7016  {
7017  consider_groupingsets_paths(root, grouped_rel,
7018  path, true, can_hash,
7019  gd, agg_costs, dNumGroups);
7020  }
7021  else if (parse->hasAggs)
7022  {
7023  /*
7024  * We have aggregation, possibly with plain GROUP BY. Make
7025  * an AggPath.
7026  */
7027  add_path(grouped_rel, (Path *)
7029  grouped_rel,
7030  path,
7031  grouped_rel->reltarget,
7032  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7034  info->clauses,
7035  havingQual,
7036  agg_costs,
7037  dNumGroups));
7038  }
7039  else if (parse->groupClause)
7040  {
7041  /*
7042  * We have GROUP BY without aggregation or grouping sets.
7043  * Make a GroupPath.
7044  */
7045  add_path(grouped_rel, (Path *)
7047  grouped_rel,
7048  path,
7049  info->clauses,
7050  havingQual,
7051  dNumGroups));
7052  }
7053  else
7054  {
7055  /* Other cases should have been handled above */
7056  Assert(false);
7057  }
7058  }
7059  }
7060 
7061  /*
7062  * Instead of operating directly on the input relation, we can
7063  * consider finalizing a partially aggregated path.
7064  */
7065  if (partially_grouped_rel != NULL)
7066  {
7067  foreach(lc, partially_grouped_rel->pathlist)
7068  {
7069  ListCell *lc2;
7070  Path *path = (Path *) lfirst(lc);
7071  Path *path_save = path;
7072  List *pathkey_orderings = NIL;
7073 
7074  /* generate alternative group orderings that might be useful */
7075  pathkey_orderings = get_useful_group_keys_orderings(root, path);
7076 
7077  Assert(list_length(pathkey_orderings) > 0);
7078 
7079  /* process all potentially interesting grouping reorderings */
7080  foreach(lc2, pathkey_orderings)
7081  {
7082  GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7083 
7084  /* restore the path (we replace it in the loop) */
7085  path = path_save;
7086 
7087  path = make_ordered_path(root,
7088  grouped_rel,
7089  path,
7090  partially_grouped_rel->cheapest_total_path,
7091  info->pathkeys);
7092 
7093  if (path == NULL)
7094  continue;
7095 
7096  if (parse->hasAggs)
7097  add_path(grouped_rel, (Path *)
7099  grouped_rel,
7100  path,
7101  grouped_rel->reltarget,
7102  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7104  info->clauses,
7105  havingQual,
7106  agg_final_costs,
7107  dNumGroups));
7108  else
7109  add_path(grouped_rel, (Path *)
7111  grouped_rel,
7112  path,
7113  info->clauses,
7114  havingQual,
7115  dNumGroups));
7116 
7117  }
7118  }
7119  }
7120  }
7121 
7122  if (can_hash)
7123  {
7124  if (parse->groupingSets)
7125  {
7126  /*
7127  * Try for a hash-only groupingsets path over unsorted input.
7128  */
7129  consider_groupingsets_paths(root, grouped_rel,
7130  cheapest_path, false, true,
7131  gd, agg_costs, dNumGroups);
7132  }
7133  else
7134  {
7135  /*
7136  * Generate a HashAgg Path. We just need an Agg over the
7137  * cheapest-total input path, since input order won't matter.
7138  */
7139  add_path(grouped_rel, (Path *)
7140  create_agg_path(root, grouped_rel,
7141  cheapest_path,
7142  grouped_rel->reltarget,
7143  AGG_HASHED,
7145  root->processed_groupClause,
7146  havingQual,
7147  agg_costs,
7148  dNumGroups));
7149  }
7150 
7151  /*
7152  * Generate a Finalize HashAgg Path atop of the cheapest partially
7153  * grouped path, assuming there is one
7154  */
7155  if (partially_grouped_rel && partially_grouped_rel->pathlist)
7156  {
7157  Path *path = partially_grouped_rel->cheapest_total_path;
7158 
7159  add_path(grouped_rel, (Path *)
7161  grouped_rel,
7162  path,
7163  grouped_rel->reltarget,
7164  AGG_HASHED,
7166  root->processed_groupClause,
7167  havingQual,
7168  agg_final_costs,
7169  dNumGroups));
7170  }
7171  }
7172 
7173  /*
7174  * When partitionwise aggregate is used, we might have fully aggregated
7175  * paths in the partial pathlist, because add_paths_to_append_rel() will
7176  * consider a path for grouped_rel consisting of a Parallel Append of
7177  * non-partial paths from each child.
7178  */
7179  if (grouped_rel->partial_pathlist != NIL)
7180  gather_grouping_paths(root, grouped_rel);
7181 }
7182 
7183 /*
7184  * create_partial_grouping_paths
7185  *
7186  * Create a new upper relation representing the result of partial aggregation
7187  * and populate it with appropriate paths. Note that we don't finalize the
7188  * lists of paths here, so the caller can add additional partial or non-partial
7189  * paths and must afterward call gather_grouping_paths and set_cheapest on
7190  * the returned upper relation.
7191  *
7192  * All paths for this new upper relation -- both partial and non-partial --
7193  * have been partially aggregated but require a subsequent FinalizeAggregate
7194  * step.
7195  *
7196  * NB: This function is allowed to return NULL if it determines that there is
7197  * no real need to create a new RelOptInfo.
7198  */
7199 static RelOptInfo *
7201  RelOptInfo *grouped_rel,
7202  RelOptInfo *input_rel,
7203  grouping_sets_data *gd,
7204  GroupPathExtraData *extra,
7205  bool force_rel_creation)
7206 {
7207  Query *parse = root->parse;
7208  RelOptInfo *partially_grouped_rel;
7209  AggClauseCosts *agg_partial_costs = &extra->agg_partial_costs;
7210  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
7211  Path *cheapest_partial_path = NULL;
7212  Path *cheapest_total_path = NULL;
7213  double dNumPartialGroups = 0;
7214  double dNumPartialPartialGroups = 0;
7215  ListCell *lc;
7216  bool can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
7217  bool can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
7218 
7219  /*
7220  * Consider whether we should generate partially aggregated non-partial
7221  * paths. We can only do this if we have a non-partial path, and only if
7222  * the parent of the input rel is performing partial partitionwise
7223  * aggregation. (Note that extra->patype is the type of partitionwise
7224  * aggregation being used at the parent level, not this level.)
7225  */
7226  if (input_rel->pathlist != NIL &&
7228  cheapest_total_path = input_rel->cheapest_total_path;
7229 
7230  /*
7231  * If parallelism is possible for grouped_rel, then we should consider
7232  * generating partially-grouped partial paths. However, if the input rel
7233  * has no partial paths, then we can't.
7234  */
7235  if (grouped_rel->consider_parallel && input_rel->partial_pathlist != NIL)
7236  cheapest_partial_path = linitial(input_rel->partial_pathlist);
7237 
7238  /*
7239  * If we can't partially aggregate partial paths, and we can't partially
7240  * aggregate non-partial paths, then don't bother creating the new
7241  * RelOptInfo at all, unless the caller specified force_rel_creation.
7242  */
7243  if (cheapest_total_path == NULL &&
7244  cheapest_partial_path == NULL &&
7245  !force_rel_creation)
7246  return NULL;
7247 
7248  /*
7249  * Build a new upper relation to represent the result of partially
7250  * aggregating the rows from the input relation.
7251  */
7252  partially_grouped_rel = fetch_upper_rel(root,
7254  grouped_rel->relids);
7255  partially_grouped_rel->consider_parallel =
7256  grouped_rel->consider_parallel;
7257  partially_grouped_rel->reloptkind = grouped_rel->reloptkind;
7258  partially_grouped_rel->serverid = grouped_rel->serverid;
7259  partially_grouped_rel->userid = grouped_rel->userid;
7260  partially_grouped_rel->useridiscurrent = grouped_rel->useridiscurrent;
7261  partially_grouped_rel->fdwroutine = grouped_rel->fdwroutine;
7262 
7263  /*
7264  * Build target list for partial aggregate paths. These paths cannot just
7265  * emit the same tlist as regular aggregate paths, because (1) we must
7266  * include Vars and Aggrefs needed in HAVING, which might not appear in
7267  * the result tlist, and (2) the Aggrefs must be set in partial mode.
7268  */
7269  partially_grouped_rel->reltarget =
7271  extra->havingQual);
7272 
7273  if (!extra->partial_costs_set)
7274  {
7275  /*
7276  * Collect statistics about aggregates for estimating costs of
7277  * performing aggregation in parallel.
7278  */
7279  MemSet(agg_partial_costs, 0, sizeof(AggClauseCosts));
7280  MemSet(agg_final_costs, 0, sizeof(AggClauseCosts));
7281  if (parse->hasAggs)
7282  {
7283  /* partial phase */
7285  agg_partial_costs);
7286 
7287  /* final phase */
7289  agg_final_costs);
7290  }
7291 
7292  extra->partial_costs_set = true;
7293  }
7294 
7295  /* Estimate number of partial groups. */
7296  if (cheapest_total_path != NULL)
7297  dNumPartialGroups =
7299  cheapest_total_path->rows,
7300  gd,
7301  extra->targetList);
7302  if (cheapest_partial_path != NULL)
7303  dNumPartialPartialGroups =
7305  cheapest_partial_path->rows,
7306  gd,
7307  extra->targetList);
7308 
7309  if (can_sort && cheapest_total_path != NULL)
7310  {
7311  /* This should have been checked previously */
7312  Assert(parse->hasAggs || parse->groupClause);
7313 
7314  /*
7315  * Use any available suitably-sorted path as input, and also consider
7316  * sorting the cheapest partial path.
7317  */
7318  foreach(lc, input_rel->pathlist)
7319  {
7320  ListCell *lc2;
7321  Path *path = (Path *) lfirst(lc);
7322  Path *path_save = path;
7323  List *pathkey_orderings = NIL;
7324 
7325  /* generate alternative group orderings that might be useful */
7326  pathkey_orderings = get_useful_group_keys_orderings(root, path);
7327 
7328  Assert(list_length(pathkey_orderings) > 0);
7329 
7330  /* process all potentially interesting grouping reorderings */
7331  foreach(lc2, pathkey_orderings)
7332  {
7333  GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7334 
7335  /* restore the path (we replace it in the loop) */
7336  path = path_save;
7337 
7338  path = make_ordered_path(root,
7339  partially_grouped_rel,
7340  path,
7341  cheapest_total_path,
7342  info->pathkeys);
7343 
7344  if (path == NULL)
7345  continue;
7346 
7347  if (parse->hasAggs)
7348  add_path(partially_grouped_rel, (Path *)
7350  partially_grouped_rel,
7351  path,
7352  partially_grouped_rel->reltarget,
7353  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7355  info->clauses,
7356  NIL,
7357  agg_partial_costs,
7358  dNumPartialGroups));
7359  else
7360  add_path(partially_grouped_rel, (Path *)
7362  partially_grouped_rel,
7363  path,
7364  info->clauses,
7365  NIL,
7366  dNumPartialGroups));
7367  }
7368  }
7369  }
7370 
7371  if (can_sort && cheapest_partial_path != NULL)
7372  {
7373  /* Similar to above logic, but for partial paths. */
7374  foreach(lc, input_rel->partial_pathlist)
7375  {
7376  ListCell *lc2;
7377  Path *path = (Path *) lfirst(lc);
7378  Path *path_save = path;
7379  List *pathkey_orderings = NIL;
7380 
7381  /* generate alternative group orderings that might be useful */
7382  pathkey_orderings = get_useful_group_keys_orderings(root, path);
7383 
7384  Assert(list_length(pathkey_orderings) > 0);
7385 
7386  /* process all potentially interesting grouping reorderings */
7387  foreach(lc2, pathkey_orderings)
7388  {
7389  GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
7390 
7391 
7392  /* restore the path (we replace it in the loop) */
7393  path = path_save;
7394 
7395  path = make_ordered_path(root,
7396  partially_grouped_rel,
7397  path,
7398  cheapest_partial_path,
7399  info->pathkeys);
7400 
7401  if (path == NULL)
7402  continue;
7403 
7404  if (parse->hasAggs)
7405  add_partial_path(partially_grouped_rel, (Path *)
7407  partially_grouped_rel,
7408  path,
7409  partially_grouped_rel->reltarget,
7410  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7412  info->clauses,
7413  NIL,
7414  agg_partial_costs,
7415  dNumPartialPartialGroups));
7416  else
7417  add_partial_path(partially_grouped_rel, (Path *)
7419  partially_grouped_rel,
7420  path,
7421  info->clauses,
7422  NIL,
7423  dNumPartialPartialGroups));
7424  }
7425  }
7426  }
7427 
7428  /*
7429  * Add a partially-grouped HashAgg Path where possible
7430  */
7431  if (can_hash && cheapest_total_path != NULL)
7432  {
7433  /* Checked above */
7434  Assert(parse->hasAggs || parse->groupClause);
7435 
7436  add_path(partially_grouped_rel, (Path *)
7438  partially_grouped_rel,
7439  cheapest_total_path,
7440  partially_grouped_rel->reltarget,
7441  AGG_HASHED,
7443  root->processed_groupClause,
7444  NIL,
7445  agg_partial_costs,
7446  dNumPartialGroups));
7447  }
7448 
7449  /*
7450  * Now add a partially-grouped HashAgg partial Path where possible
7451  */
7452  if (can_hash && cheapest_partial_path != NULL)
7453  {
7454  add_partial_path(partially_grouped_rel, (Path *)
7456  partially_grouped_rel,
7457  cheapest_partial_path,
7458  partially_grouped_rel->reltarget,
7459  AGG_HASHED,
7461  root->processed_groupClause,
7462  NIL,
7463  agg_partial_costs,
7464  dNumPartialPartialGroups));
7465  }
7466 
7467  /*
7468  * If there is an FDW that's responsible for all baserels of the query,
7469  * let it consider adding partially grouped ForeignPaths.
7470  */
7471  if (partially_grouped_rel->fdwroutine &&
7472  partially_grouped_rel->fdwroutine->GetForeignUpperPaths)
7473  {
7474  FdwRoutine *fdwroutine = partially_grouped_rel->fdwroutine;
7475 
7476  fdwroutine->GetForeignUpperPaths(root,
7478  input_rel, partially_grouped_rel,
7479  extra);
7480  }
7481 
7482  return partially_grouped_rel;
7483 }
7484 
7485 /*
7486  * Generate Gather and Gather Merge paths for a grouping relation or partial
7487  * grouping relation.
7488  *
7489  * generate_useful_gather_paths does most of the work, but we also consider a
7490  * special case: we could try sorting the data by the group_pathkeys and then
7491  * applying Gather Merge.
7492  *
7493  * NB: This function shouldn't be used for anything other than a grouped or
7494  * partially grouped relation not only because of the fact that it explicitly
7495  * references group_pathkeys but we pass "true" as the third argument to
7496  * generate_useful_gather_paths().
7497  */
7498 static void
7500 {
7501  ListCell *lc;
7502  Path *cheapest_partial_path;
7503  List *groupby_pathkeys;
7504 
7505  /*
7506  * This occurs after any partial aggregation has taken place, so trim off
7507  * any pathkeys added for ORDER BY / DISTINCT aggregates.
7508  */
7509  if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys)
7510  groupby_pathkeys = list_copy_head(root->group_pathkeys,
7511  root->num_groupby_pathkeys);
7512  else
7513  groupby_pathkeys = root->group_pathkeys;
7514 
7515  /* Try Gather for unordered paths and Gather Merge for ordered ones. */
7516  generate_useful_gather_paths(root, rel, true);
7517 
7518  cheapest_partial_path = linitial(rel->partial_pathlist);
7519 
7520  /* XXX Shouldn't this also consider the group-key-reordering? */
7521  foreach(lc, rel->partial_pathlist)
7522  {
7523  Path *path = (Path *) lfirst(lc);
7524  bool is_sorted;
7525  int presorted_keys;
7526  double total_groups;
7527 
7528  is_sorted = pathkeys_count_contained_in(groupby_pathkeys,
7529  path->pathkeys,
7530  &presorted_keys);
7531 
7532  if (is_sorted)
7533  continue;
7534 
7535  /*
7536  * Try at least sorting the cheapest path and also try incrementally
7537  * sorting any path which is partially sorted already (no need to deal
7538  * with paths which have presorted keys when incremental sort is
7539  * disabled unless it's the cheapest input path).
7540  */
7541  if (path != cheapest_partial_path &&
7542  (presorted_keys == 0 || !enable_incremental_sort))
7543  continue;
7544 
7545  /*
7546  * We've no need to consider both a sort and incremental sort. We'll
7547  * just do a sort if there are no presorted keys and an incremental
7548  * sort when there are presorted keys.
7549  */
7550  if (presorted_keys == 0 || !enable_incremental_sort)
7551  path = (Path *) create_sort_path(root, rel, path,
7552  groupby_pathkeys,
7553  -1.0);
7554  else
7556  rel,
7557  path,
7558  groupby_pathkeys,
7559  presorted_keys,
7560  -1.0);
7561  total_groups = compute_gather_rows(path);
7562  path = (Path *)
7564  rel,
7565  path,
7566  rel->reltarget,
7567  groupby_pathkeys,
7568  NULL,
7569  &total_groups);
7570 
7571  add_path(rel, path);
7572  }
7573 }
7574 
7575 /*
7576  * can_partial_agg
7577  *
7578  * Determines whether or not partial grouping and/or aggregation is possible.
7579  * Returns true when possible, false otherwise.
7580  */
7581 static bool
7583 {
7584  Query *parse = root->parse;
7585 
7586  if (!parse->hasAggs && parse->groupClause == NIL)
7587  {
7588  /*
7589  * We don't know how to do parallel aggregation unless we have either
7590  * some aggregates or a grouping clause.
7591  */
7592  return false;
7593  }
7594  else if (parse->groupingSets)
7595  {
7596  /* We don't know how to do grouping sets in parallel. */
7597  return false;
7598  }
7599  else if (root->hasNonPartialAggs || root->hasNonSerialAggs)
7600  {
7601  /* Insufficient support for partial mode. */
7602  return false;
7603  }
7604 
7605  /* Everything looks good. */
7606  return true;
7607 }
7608 
7609 /*
7610  * apply_scanjoin_target_to_paths
7611  *
7612  * Adjust the final scan/join relation, and recursively all of its children,
7613  * to generate the final scan/join target. It would be more correct to model
7614  * this as a separate planning step with a new RelOptInfo at the toplevel and
7615  * for each child relation, but doing it this way is noticeably cheaper.
7616  * Maybe that problem can be solved at some point, but for now we do this.
7617  *
7618  * If tlist_same_exprs is true, then the scan/join target to be applied has
7619  * the same expressions as the existing reltarget, so we need only insert the
7620  * appropriate sortgroupref information. By avoiding the creation of
7621  * projection paths we save effort both immediately and at plan creation time.
7622  */
7623 static void
7625  RelOptInfo *rel,
7626  List *scanjoin_targets,
7627  List *scanjoin_targets_contain_srfs,
7628  bool scanjoin_target_parallel_safe,
7629  bool tlist_same_exprs)
7630 {
7631  bool rel_is_partitioned = IS_PARTITIONED_REL(rel);
7632  PathTarget *scanjoin_target;
7633  ListCell *lc;
7634 
7635  /* This recurses, so be paranoid. */
7637 
7638  /*
7639  * If the rel is partitioned, we want to drop its existing paths and
7640  * generate new ones. This function would still be correct if we kept the
7641  * existing paths: we'd modify them to generate the correct target above
7642  * the partitioning Append, and then they'd compete on cost with paths
7643  * generating the target below the Append. However, in our current cost
7644  * model the latter way is always the same or cheaper cost, so modifying
7645  * the existing paths would just be useless work. Moreover, when the cost
7646  * is the same, varying roundoff errors might sometimes allow an existing
7647  * path to be picked, resulting in undesirable cross-platform plan
7648  * variations. So we drop old paths and thereby force the work to be done
7649  * below the Append, except in the case of a non-parallel-safe target.
7650  *
7651  * Some care is needed, because we have to allow
7652  * generate_useful_gather_paths to see the old partial paths in the next
7653  * stanza. Hence, zap the main pathlist here, then allow
7654  * generate_useful_gather_paths to add path(s) to the main list, and
7655  * finally zap the partial pathlist.
7656  */
7657  if (rel_is_partitioned)
7658  rel->pathlist = NIL;
7659 
7660  /*
7661  * If the scan/join target is not parallel-safe, partial paths cannot
7662  * generate it.
7663  */
7664  if (!scanjoin_target_parallel_safe)
7665  {
7666  /*
7667  * Since we can't generate the final scan/join target in parallel
7668  * workers, this is our last opportunity to use any partial paths that
7669  * exist; so build Gather path(s) that use them and emit whatever the
7670  * current reltarget is. We don't do this in the case where the
7671  * target is parallel-safe, since we will be able to generate superior
7672  * paths by doing it after the final scan/join target has been
7673  * applied.
7674  */
7675  generate_useful_gather_paths(root, rel, false);
7676 
7677  /* Can't use parallel query above this level. */
7678  rel->partial_pathlist = NIL;
7679  rel->consider_parallel = false;
7680  }
7681 
7682  /* Finish dropping old paths for a partitioned rel, per comment above */
7683  if (rel_is_partitioned)
7684  rel->partial_pathlist = NIL;
7685 
7686  /* Extract SRF-free scan/join target. */
7687  scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
7688 
7689  /*
7690  * Apply the SRF-free scan/join target to each existing path.
7691  *
7692  * If the tlist exprs are the same, we can just inject the sortgroupref
7693  * information into the existing pathtargets. Otherwise, replace each
7694  * path with a projection path that generates the SRF-free scan/join
7695  * target. This can't change the ordering of paths within rel->pathlist,
7696  * so we just modify the list in place.
7697  */
7698  foreach(lc, rel->pathlist)
7699  {
7700  Path *subpath = (Path *) lfirst(lc);
7701 
7702  /* Shouldn't have any parameterized paths anymore */
7703  Assert(subpath->param_info == NULL);
7704 
7705  if (tlist_same_exprs)
7706  subpath->pathtarget->sortgrouprefs =
7707  scanjoin_target->sortgrouprefs;
7708  else
7709  {
7710  Path *newpath;
7711 
7712  newpath = (Path *) create_projection_path(root, rel, subpath,
7713  scanjoin_target);
7714  lfirst(lc) = newpath;
7715  }
7716  }
7717 
7718  /* Likewise adjust the targets for any partial paths. */
7719  foreach(lc, rel->partial_pathlist)
7720  {
7721  Path *subpath = (Path *) lfirst(lc);
7722 
7723  /* Shouldn't have any parameterized paths anymore */
7724  Assert(subpath->param_info == NULL);
7725 
7726  if (tlist_same_exprs)
7727  subpath->pathtarget->sortgrouprefs =
7728  scanjoin_target->sortgrouprefs;
7729  else
7730  {
7731  Path *newpath;
7732 
7733  newpath = (Path *) create_projection_path(root, rel, subpath,
7734  scanjoin_target);
7735  lfirst(lc) = newpath;
7736  }
7737  }
7738 
7739  /*
7740  * Now, if final scan/join target contains SRFs, insert ProjectSetPath(s)
7741  * atop each existing path. (Note that this function doesn't look at the
7742  * cheapest-path fields, which is a good thing because they're bogus right
7743  * now.)
7744  */
7745  if (root->parse->hasTargetSRFs)
7747  scanjoin_targets,
7748  scanjoin_targets_contain_srfs);
7749 
7750  /*
7751  * Update the rel's target to be the final (with SRFs) scan/join target.
7752  * This now matches the actual output of all the paths, and we might get
7753  * confused in createplan.c if they don't agree. We must do this now so
7754  * that any append paths made in the next part will use the correct
7755  * pathtarget (cf. create_append_path).
7756  *
7757  * Note that this is also necessary if GetForeignUpperPaths() gets called
7758  * on the final scan/join relation or on any of its children, since the
7759  * FDW might look at the rel's target to create ForeignPaths.
7760  */
7761  rel->reltarget = llast_node(PathTarget, scanjoin_targets);
7762 
7763  /*
7764  * If the relation is partitioned, recursively apply the scan/join target
7765  * to all partitions, and generate brand-new Append paths in which the
7766  * scan/join target is computed below the Append rather than above it.
7767  * Since Append is not projection-capable, that might save a separate
7768  * Result node, and it also is important for partitionwise aggregate.
7769  */
7770  if (rel_is_partitioned)
7771  {
7772  List *live_children = NIL;
7773  int i;
7774 
7775  /* Adjust each partition. */
7776  i = -1;
7777  while ((i = bms_next_member(rel->live_parts, i)) >= 0)
7778  {
7779  RelOptInfo *child_rel = rel->part_rels[i];
7780  AppendRelInfo **appinfos;
7781  int nappinfos;
7782  List *child_scanjoin_targets = NIL;
7783 
7784  Assert(child_rel != NULL);
7785 
7786  /* Dummy children can be ignored. */
7787  if (IS_DUMMY_REL(child_rel))
7788  continue;
7789 
7790  /* Translate scan/join targets for this child. */
7791  appinfos = find_appinfos_by_relids(root, child_rel->relids,
7792  &nappinfos);
7793  foreach(lc, scanjoin_targets)
7794  {
7795  PathTarget *target = lfirst_node(PathTarget, lc);
7796 
7797  target = copy_pathtarget(target);
7798  target->exprs = (List *)
7800  (Node *) target->exprs,
7801  nappinfos, appinfos);
7802  child_scanjoin_targets = lappend(child_scanjoin_targets,
7803  target);
7804  }
7805  pfree(appinfos);
7806 
7807  /* Recursion does the real work. */
7809  child_scanjoin_targets,
7810  scanjoin_targets_contain_srfs,
7811  scanjoin_target_parallel_safe,
7813 
7814  /* Save non-dummy children for Append paths. */
7815  if (!IS_DUMMY_REL(child_rel))
7816  live_children = lappend(live_children, child_rel);
7817  }
7818 
7819  /* Build new paths for this relation by appending child paths. */
7820  add_paths_to_append_rel(root, rel, live_children);
7821  }
7822 
7823  /*
7824  * Consider generating Gather or Gather Merge paths. We must only do this
7825  * if the relation is parallel safe, and we don't do it for child rels to
7826  * avoid creating multiple Gather nodes within the same plan. We must do
7827  * this after all paths have been generated and before set_cheapest, since
7828  * one of the generated paths may turn out to be the cheapest one.
7829  */
7830  if (rel->consider_parallel && !IS_OTHER_REL(rel))
7831  generate_useful_gather_paths(root, rel, false);
7832 
7833  /*
7834  * Reassess which paths are the cheapest, now that we've potentially added
7835  * new Gather (or Gather Merge) and/or Append (or MergeAppend) paths to
7836  * this relation.
7837  */
7838  set_cheapest(rel);
7839 }
7840 
7841 /*
7842  * create_partitionwise_grouping_paths
7843  *
7844  * If the partition keys of input relation are part of the GROUP BY clause, all
7845  * the rows belonging to a given group come from a single partition. This
7846  * allows aggregation/grouping over a partitioned relation to be broken down
7847  * into aggregation/grouping on each partition. This should be no worse, and
7848  * often better, than the normal approach.
7849  *
7850  * However, if the GROUP BY clause does not contain all the partition keys,
7851  * rows from a given group may be spread across multiple partitions. In that
7852  * case, we perform partial aggregation for each group, append the results,
7853  * and then finalize aggregation. This is less certain to win than the
7854  * previous case. It may win if the PartialAggregate stage greatly reduces
7855  * the number of groups, because fewer rows will pass through the Append node.
7856  * It may lose if we have lots of small groups.
7857  */
7858 static void
7860  RelOptInfo *input_rel,
7861  RelOptInfo *grouped_rel,
7862  RelOptInfo *partially_grouped_rel,
7863  const AggClauseCosts *agg_costs,
7864  grouping_sets_data *gd,
7866  GroupPathExtraData *extra)
7867 {
7868  List *grouped_live_children = NIL;
7869  List *partially_grouped_live_children = NIL;
7870  PathTarget *target = grouped_rel->reltarget;
7871  bool partial_grouping_valid = true;
7872  int i;
7873 
7876  partially_grouped_rel != NULL);
7877 
7878  /* Add paths for partitionwise aggregation/grouping. */
7879  i = -1;
7880  while ((i = bms_next_member(input_rel->live_parts, i)) >= 0)
7881  {
7882  RelOptInfo *child_input_rel = input_rel->part_rels[i];
7883  PathTarget *child_target;
7884  AppendRelInfo **appinfos;
7885  int nappinfos;
7886  GroupPathExtraData child_extra;
7887  RelOptInfo *child_grouped_rel;
7888  RelOptInfo *child_partially_grouped_rel;
7889 
7890  Assert(child_input_rel != NULL);
7891 
7892  /* Dummy children can be ignored. */
7893  if (IS_DUMMY_REL(child_input_rel))
7894  continue;
7895 
7896  child_target = copy_pathtarget(target);
7897 
7898  /*
7899  * Copy the given "extra" structure as is and then override the
7900  * members specific to this child.
7901  */
7902  memcpy(&child_extra, extra, sizeof(child_extra));
7903 
7904  appinfos = find_appinfos_by_relids(root, child_input_rel->relids,
7905  &nappinfos);
7906 
7907  child_target->exprs = (List *)
7909  (Node *) target->exprs,
7910  nappinfos, appinfos);
7911 
7912  /* Translate havingQual and targetList. */
7913  child_extra.havingQual = (Node *)
7915  extra->havingQual,
7916  nappinfos, appinfos);
7917  child_extra.targetList = (List *)
7919  (Node *) extra->targetList,
7920  nappinfos, appinfos);
7921 
7922  /*
7923  * extra->patype was the value computed for our parent rel; patype is
7924  * the value for this relation. For the child, our value is its
7925  * parent rel's value.
7926  */
7927  child_extra.patype = patype;
7928 
7929  /*
7930  * Create grouping relation to hold fully aggregated grouping and/or
7931  * aggregation paths for the child.
7932  */
7933  child_grouped_rel = make_grouping_rel(root, child_input_rel,
7934  child_target,
7935  extra->target_parallel_safe,
7936  child_extra.havingQual);
7937 
7938  /* Create grouping paths for this child relation. */
7939  create_ordinary_grouping_paths(root, child_input_rel,
7940  child_grouped_rel,
7941  agg_costs, gd, &child_extra,
7942  &child_partially_grouped_rel);
7943 
7944  if (child_partially_grouped_rel)
7945  {
7946  partially_grouped_live_children =
7947  lappend(partially_grouped_live_children,
7948  child_partially_grouped_rel);
7949  }
7950  else
7951  partial_grouping_valid = false;
7952 
7953  if (patype == PARTITIONWISE_AGGREGATE_FULL)
7954  {
7955  set_cheapest(child_grouped_rel);
7956  grouped_live_children = lappend(grouped_live_children,
7957  child_grouped_rel);
7958  }
7959 
7960  pfree(appinfos);
7961  }
7962 
7963  /*
7964  * Try to create append paths for partially grouped children. For full
7965  * partitionwise aggregation, we might have paths in the partial_pathlist
7966  * if parallel aggregation is possible. For partial partitionwise
7967  * aggregation, we may have paths in both pathlist and partial_pathlist.
7968  *
7969  * NB: We must have a partially grouped path for every child in order to
7970  * generate a partially grouped path for this relation.
7971  */
7972  if (partially_grouped_rel && partial_grouping_valid)
7973  {
7974  Assert(partially_grouped_live_children != NIL);
7975 
7976  add_paths_to_append_rel(root, partially_grouped_rel,
7977  partially_grouped_live_children);
7978 
7979  /*
7980  * We need call set_cheapest, since the finalization step will use the
7981  * cheapest path from the rel.
7982  */
7983  if (partially_grouped_rel->pathlist)
7984  set_cheapest(partially_grouped_rel);
7985  }
7986 
7987  /* If possible, create append paths for fully grouped children. */
7988  if (patype == PARTITIONWISE_AGGREGATE_FULL)
7989  {
7990  Assert(grouped_live_children != NIL);
7991 
7992  add_paths_to_append_rel(root, grouped_rel, grouped_live_children);
7993  }
7994 }
7995 
7996 /*
7997  * group_by_has_partkey
7998  *
7999  * Returns true, if all the partition keys of the given relation are part of
8000  * the GROUP BY clauses, false otherwise.
8001  */
8002 static bool
8004  List *targetList,
8005  List *groupClause)
8006 {
8007  List *groupexprs = get_sortgrouplist_exprs(groupClause, targetList);
8008  int cnt = 0;
8009  int partnatts;
8010 
8011  /* Input relation should be partitioned. */
8012  Assert(input_rel->part_scheme);
8013 
8014  /* Rule out early, if there are no partition keys present. */
8015  if (!input_rel->partexprs)
8016  return false;
8017 
8018  partnatts = input_rel->part_scheme->partnatts;
8019 
8020  for (cnt = 0; cnt < partnatts; cnt++)
8021  {
8022  List *partexprs = input_rel->partexprs[cnt];
8023  ListCell *lc;
8024  bool found = false;
8025 
8026  foreach(lc, partexprs)
8027  {
8028  Expr *partexpr = lfirst(lc);
8029 
8030  if (list_member(groupexprs, partexpr))
8031  {
8032  found = true;
8033  break;
8034  }
8035  }
8036 
8037  /*
8038  * If none of the partition key expressions match with any of the
8039  * GROUP BY expression, return false.
8040  */
8041  if (!found)
8042  return false;
8043  }
8044 
8045  return true;
8046 }
8047 
8048 /*
8049  * generate_setop_child_grouplist
8050  * Build a SortGroupClause list defining the sort/grouping properties
8051  * of the child of a set operation.
8052  *
8053  * This is similar to generate_setop_grouplist() but differs as the setop
8054  * child query's targetlist entries may already have a tleSortGroupRef
8055  * assigned for other purposes, such as GROUP BYs. Here we keep the
8056  * SortGroupClause list in the same order as 'op' groupClauses and just adjust
8057  * the tleSortGroupRef to reference the TargetEntry's 'ressortgroupref'.
8058  */
8059 static List *
8061 {
8062  List *grouplist = copyObject(op->groupClauses);
8063  ListCell *lg;
8064  ListCell *lt;
8065 
8066  lg = list_head(grouplist);
8067  foreach(lt, targetlist)
8068  {
8069  TargetEntry *tle = (TargetEntry *) lfirst(lt);
8070  SortGroupClause *sgc;
8071 
8072  /* resjunk columns could have sortgrouprefs. Leave these alone */
8073  if (tle->resjunk)
8074  continue;
8075 
8076  /* we expect every non-resjunk target to have a SortGroupClause */
8077  Assert(lg != NULL);
8078  sgc = (SortGroupClause *) lfirst(lg);
8079  lg = lnext(grouplist, lg);
8080 
8081  /* assign a tleSortGroupRef, or reuse the existing one */
8082  sgc->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
8083  }
8084  Assert(lg == NULL);
8085  return grouplist;
8086 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:4210
void generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:3197
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1310
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:737
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:200
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition: appendinfo.c:525
List * adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
Definition: appendinfo.c:666
void pprint(const void *obj)
Definition: print.c:54
void BipartiteMatchFree(BipartiteMatchState *state)
BipartiteMatchState * BipartiteMatch(int u_size, int v_size, short **adjacency)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:445
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
bool bms_overlap_list(const Bitmapset *a, const List *b)
Definition: bitmapset.c:608
#define bms_is_empty(a)
Definition: bitmapset.h:118
@ BMS_SUBSET1
Definition: bitmapset.h:63
@ BMS_MULTIPLE
Definition: bitmapset.h:73
uint32 BlockNumber
Definition: block.h:31
#define Min(x, y)
Definition: c.h:1004
#define Max(x, y)
Definition: c.h:998
#define Assert(condition)
Definition: c.h:858
unsigned char bool
Definition: c.h:456
#define FLOAT8PASSBYVAL
Definition: c.h:635
unsigned int Index
Definition: c.h:614
#define MemSet(start, val, len)
Definition: c.h:1020
#define OidIsValid(objectId)
Definition: c.h:775
size_t Size
Definition: c.h:605
WindowFuncLists * find_window_functions(Node *clause, Index maxWinRef)
Definition: clauses.c:227
bool contain_agg_clause(Node *clause)
Definition: clauses.c:177
void convert_saop_to_hashed_saop(Node *node)
Definition: clauses.c:2287
char max_parallel_hazard(Query *parse)
Definition: clauses.c:734
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:753
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2254
bool contain_subplans(Node *clause)
Definition: clauses.c:330
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:538
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2395
double cpu_operator_cost
Definition: costsize.c:123
bool enable_partitionwise_aggregate
Definition: costsize.c:149
int max_parallel_workers_per_gather
Definition: costsize.c:132
double parallel_setup_cost
Definition: costsize.c:125
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition: costsize.c:6230
double parallel_tuple_cost
Definition: costsize.c:124
double compute_gather_rows(Path *path)
Definition: costsize.c:6488
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4645
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:4619
bool enable_presorted_aggregate
Definition: costsize.c:153
bool enable_hashagg
Definition: costsize.c:141
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:231
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:2103
bool enable_indexscan
Definition: costsize.c:135
bool enable_incremental_sort
Definition: costsize.c:140
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:337
Plan * materialize_finished_plan(Plan *subplan)
Definition: createplan.c:6522
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:510
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:680
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition: foreign.c:409
int max_parallel_maintenance_workers
Definition: globals.c:132
bool IsUnderPostmaster
Definition: globals.c:118
int maintenance_work_mem
Definition: globals.c:131
#define IsParallelWorker()
Definition: parallel.h:60
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int j
Definition: isn.c:74
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
double jit_optimize_above_cost
Definition: jit.c:41
bool jit_enabled
Definition: jit.c:32
bool jit_expressions
Definition: jit.c:36
bool jit_tuple_deforming
Definition: jit.c:38
double jit_above_cost
Definition: jit.c:39
double jit_inline_above_cost
Definition: jit.c:40
#define PGJIT_OPT3
Definition: jit.h:21
#define PGJIT_NONE
Definition: jit.h:19
#define PGJIT_EXPR
Definition: jit.h:23
#define PGJIT_DEFORM
Definition: jit.h:24
#define PGJIT_INLINE
Definition: jit.h:22
#define PGJIT_PERFORM
Definition: jit.h:20
Bitmapset * DiscreteKnapsack(int max_weight, int num_items, int *item_weights, double *item_values)
Definition: knapsack.c:52
List * list_delete_int(List *list, int datum)
Definition: list.c:891
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1405
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_difference_int(const List *list1, const List *list2)
Definition: list.c:1288
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1593
List * lappend_int(List *list, int datum)
Definition: list.c:357
List * list_copy(const List *oldlist)
Definition: list.c:1573
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:682
void list_free(List *list)
Definition: list.c:1546
bool list_member_int(const List *list, int datum)
Definition: list.c:702
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
bool list_member(const List *list, const void *datum)
Definition: list.c:661
List * lcons(void *datum, List *list)
Definition: list.c:495
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
@ LockWaitBlock
Definition: lockoptions.h:39
LockClauseStrength
Definition: lockoptions.h:22
@ LCS_FORUPDATE
Definition: lockoptions.h:27
@ LCS_NONE
Definition: lockoptions.h:23
@ LCS_FORSHARE
Definition: lockoptions.h:25
@ LCS_FORKEYSHARE
Definition: lockoptions.h:24
@ LCS_FORNOKEYUPDATE
Definition: lockoptions.h:26
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1858
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2578
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:310
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:628
List * make_ands_implicit(Expr *clause)
Definition: makefuncs.c:737
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void * palloc(Size size)
Definition: mcxt.c:1317
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:758
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1831
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3595
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:386
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define copyObject(obj)
Definition: nodes.h:224
double Cost
Definition: nodes.h:251
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:338
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UPDATE
Definition: nodes.h:266
@ CMD_SELECT
Definition: nodes.h:265
AggStrategy
Definition: nodes.h:353
@ AGG_SORTED
Definition: nodes.h:355
@ AGG_HASHED
Definition: nodes.h:356
@ AGG_MIXED
Definition: nodes.h:357
@ AGG_PLAIN
Definition: nodes.h:354
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:387
AggSplit
Definition: nodes.h:375
@ AGGSPLIT_FINAL_DESERIAL
Definition: nodes.h:381
@ AGGSPLIT_SIMPLE
Definition: nodes.h:377
@ AGGSPLIT_INITIAL_SERIAL
Definition: nodes.h:379
@ LIMIT_OPTION_COUNT
Definition: nodes.h:431
#define makeNode(_type_)
Definition: nodes.h:155
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:187
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:189
@ DEBUG_PARALLEL_REGRESS
Definition: optimizer.h:108
@ DEBUG_PARALLEL_OFF
Definition: optimizer.h:106
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:188
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:190
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:186
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:664
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition: parse_agg.c:1805
Index assignSortGroupRef(TargetEntry *tle, List *tlist)
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:3287
#define CURSOR_OPT_FAST_PLAN
Definition: parsenodes.h:3293
@ RTE_JOIN
Definition: parsenodes.h:1030
@ RTE_VALUES
Definition: parsenodes.h:1033
@ RTE_SUBQUERY
Definition: parsenodes.h:1029
@ RTE_RESULT
Definition: parsenodes.h:1036
@ RTE_FUNCTION
Definition: parsenodes.h:1031
@ RTE_TABLEFUNC
Definition: parsenodes.h:1032
@ RTE_RELATION
Definition: parsenodes.h:1028
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3296
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition: analyze.c:3238
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:3213
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
void DestroyPartitionDirectory(PartitionDirectory pdir)
Definition: partdesc.c:470
List * get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
Definition: pathkeys.c:465
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:556
List * append_pathkeys(List *target, List *source)
Definition: pathkeys.c:106
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
Definition: pathkeys.c:1333
List * make_pathkeys_for_sortclauses_extended(PlannerInfo *root, List **sortclauses, List *tlist, bool remove_redundant, bool *sortable, bool set_ec_sortref)
Definition: pathkeys.c:1374
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:341
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:302
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, double rows)
Definition: pathnode.c:1244
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
Definition: pathnode.c:2988
GroupResultPath * create_group_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *havingqual)
Definition: pathnode.c:1518
IndexPath * create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, List *indexorderbys, List *indexorderbycols, List *pathkeys, ScanDirection indexscandir, bool indexonly, Relids required_outer, double loop_count, bool partial_path)
Definition: pathnode.c:993
LimitPath * create_limit_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, Node *limitOffset, Node *limitCount, LimitOption limitOption, int64 offset_est, int64 count_est)
Definition: pathnode.c:3814
ProjectionPath * create_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2673
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1881
UpperUniquePath * create_upper_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, int numCols, double numGroups)
Definition: pathnode.c:3091
LockRowsPath * create_lockrows_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *rowMarks, int epqParam)
Definition: pathnode.c:3650
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:242
ProjectSetPath * create_set_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2870
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:747
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:115
GroupPath * create_group_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *groupClause, List *qual, double numGroups)
Definition: pathnode.c:3032
GroupingSetsPath * create_groupingsets_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *having_qual, AggStrategy aggstrategy, List *rollups, const AggClauseCosts *agg_costs)
Definition: pathnode.c:3225
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
Definition: pathnode.c:2939
WindowAggPath * create_windowagg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *windowFuncs, List *runCondition, WindowClause *winclause, List *qual, bool topwindow)
Definition: pathnode.c:3473
AggPath * create_agg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, AggStrategy aggstrategy, AggSplit aggsplit, List *groupClause, List *qual, const AggClauseCosts *aggcosts, double numGroups)
Definition: pathnode.c:3143
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:420
ModifyTablePath * create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, List *mergeJoinConditions, int epqParam)
Definition: pathnode.c:3713
Path * apply_projection_to_path(PlannerInfo *root, RelOptInfo *rel, Path *path, PathTarget *target)
Definition: pathnode.c:2781
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:927
PartitionwiseAggregateType
Definition: pathnodes.h:3269
@ PARTITIONWISE_AGGREGATE_PARTIAL
Definition: pathnodes.h:3272
@ PARTITIONWISE_AGGREGATE_FULL
Definition: pathnodes.h:3271
@ PARTITIONWISE_AGGREGATE_NONE
Definition: pathnodes.h:3270
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1946
#define GROUPING_CAN_USE_HASH
Definition: pathnodes.h:3254
#define get_pathtarget_sortgroupref(target, colno)
Definition: pathnodes.h:1549
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1056
#define GROUPING_CAN_USE_SORT
Definition: pathnodes.h:3253
#define GROUPING_CAN_PARTIAL_AGG
Definition: pathnodes.h:3255
@ UPPERREL_GROUP_AGG
Definition: pathnodes.h:74
@ UPPERREL_FINAL
Definition: pathnodes.h:79
@ UPPERREL_DISTINCT
Definition: pathnodes.h:77
@ UPPERREL_PARTIAL_GROUP_AGG
Definition: pathnodes.h:72
@ UPPERREL_ORDERED
Definition: pathnodes.h:78
@ UPPERREL_WINDOW
Definition: pathnodes.h:75
@ UPPERREL_PARTIAL_DISTINCT
Definition: pathnodes.h:76
@ RELOPT_OTHER_UPPER_REL
Definition: pathnodes.h:826
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:848
@ PATHKEYS_BETTER2
Definition: paths.h:204
@ PATHKEYS_BETTER1
Definition: paths.h:203
@ PATHKEYS_DIFFERENT
Definition: paths.h:205
@ PATHKEYS_EQUAL
Definition: paths.h:202
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
bool has_subclass(Oid relationId)
Definition: pg_inherits.c:355
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define lfirst_int(lc)
Definition: pg_list.h:173
#define list_make1(x1)
Definition: pg_list.h:212
#define linitial_int(l)
Definition: pg_list.h:179
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define list_make1_int(x1)
Definition: pg_list.h:227
#define llast_node(type, l)
Definition: pg_list.h:202
static int scale
Definition: pgbench.c:181
void preprocess_minmax_aggregates(PlannerInfo *root)
Definition: planagg.c:72
void estimate_rel_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
Definition: plancat.c:1048
int32 get_relation_data_width(Oid relid, int32 *attr_widths)
Definition: plancat.c:1215
RelOptInfo * query_planner(PlannerInfo *root, query_pathkeys_callback qp_callback, void *qp_extra)
Definition: planmain.c:54
#define DEFAULT_CURSOR_TUPLE_FRACTION
Definition: planmain.h:21
#define EXPRKIND_TABLEFUNC_LATERAL
Definition: planner.c:90
static RelOptInfo * create_final_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *distinct_rel)
Definition: planner.c:5022
static List * postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
Definition: planner.c:5669
Expr * expression_planner(Expr *expr)
Definition: planner.c:6580
static PathTarget * make_partial_grouping_target(PlannerInfo *root, PathTarget *grouping_target, Node *havingQual)
Definition: planner.c:5531
#define EXPRKIND_TARGET
Definition: planner.c:79
#define EXPRKIND_APPINFO
Definition: planner.c:85
static void gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: planner.c:7499
static void preprocess_rowmarks(PlannerInfo *root)
Definition: planner.c:2262
#define EXPRKIND_TABLESAMPLE
Definition: planner.c:87
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction, SetOperationStmt *setops)
Definition: planner.c:628
static void create_degenerate_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel)
Definition: planner.c:3931
planner_hook_type planner_hook
Definition: planner.c:71
double cursor_tuple_fraction
Definition: planner.c:66
static bool is_degenerate_grouping(PlannerInfo *root)
Definition: planner.c:3910
static void remove_useless_groupby_columns(PlannerInfo *root)
Definition: planner.c:2684
bool plan_cluster_use_sort(Oid tableOid, Oid indexOid)
Definition: planner.c:6660
static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
Definition: planner.c:1225
int plan_create_index_workers(Oid tableOid, Oid indexOid)
Definition: planner.c:6780
#define EXPRKIND_PHV
Definition: planner.c:86
#define EXPRKIND_RTFUNC_LATERAL
Definition: planner.c:81
#define EXPRKIND_VALUES_LATERAL
Definition: planner.c:83
static void create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, GroupPathExtraData *extra, RelOptInfo **partially_grouped_rel_p)
Definition: planner.c:3995
#define EXPRKIND_LIMIT
Definition: planner.c:84
#define EXPRKIND_VALUES
Definition: planner.c:82
static bool can_partial_agg(PlannerInfo *root)
Definition: planner.c:7582
static double preprocess_limit(PlannerInfo *root, double tuple_fraction, int64 *offset_est, int64 *count_est)
Definition: planner.c:2440
bool parallel_leader_participation
Definition: planner.c:68
static PathTarget * make_window_input_target(PlannerInfo *root, PathTarget *final_target, List *activeWindows)
Definition: planner.c:6003
static void apply_scanjoin_target_to_paths(PlannerInfo *root, RelOptInfo *rel, List *scanjoin_targets, List *scanjoin_targets_contain_srfs, bool scanjoin_target_parallel_safe, bool tlist_same_exprs)
Definition: planner.c:7624
static RelOptInfo * create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target)
Definition: planner.c:4753
static void optimize_window_clauses(PlannerInfo *root, WindowFuncLists *wflists)
Definition: planner.c:5706
RowMarkType select_rowmark_type(RangeTblEntry *rte, LockClauseStrength strength)
Definition: planner.c:2374
static void adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel, List *targets, List *targets_contain_srfs)
Definition: planner.c:6464
static void create_partial_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *final_distinct_rel, PathTarget *target)
Definition: planner.c:4823
#define EXPRKIND_QUAL
Definition: planner.c:78
static List * preprocess_groupclause(PlannerInfo *root, List *force)
Definition: planner.c:2851
static Node * preprocess_expression(PlannerInfo *root, Node *expr, int kind)
Definition: planner.c:1123
static bool has_volatile_pathkey(List *keys)
Definition: planner.c:3207
static RelOptInfo * create_partial_grouping_paths(PlannerInfo *root, RelOptInfo *grouped_rel, RelOptInfo *input_rel, grouping_sets_data *gd, GroupPathExtraData *extra, bool force_rel_creation)
Definition: planner.c:7200
PlannedStmt * planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.c:274
static PathTarget * make_sort_input_target(PlannerInfo *root, PathTarget *final_target, bool *have_postponed_srfs)
Definition: planner.c:6250
static void create_one_window_path(PlannerInfo *root, RelOptInfo *window_rel, Path *path, PathTarget *input_target, PathTarget *output_target, WindowFuncLists *wflists, List *activeWindows)
Definition: planner.c:4583
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:5634
static grouping_sets_data * preprocess_grouping_sets(PlannerInfo *root)
Definition: planner.c:2044
int debug_parallel_query
Definition: planner.c:67
static List * remap_to_groupclause_idx(List *groupClause, List *gsets, int *tleref_to_colnum_map)
Definition: planner.c:2225
static void adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
Definition: planner.c:3252
static PathTarget * make_group_input_target(PlannerInfo *root, PathTarget *final_target)
Definition: planner.c:5443
static List * reorder_grouping_sets(List *groupingSets, List *sortclause)
Definition: planner.c:3159
static int common_prefix_cmp(const void *a, const void *b)
Definition: planner.c:5934
static void grouping_planner(PlannerInfo *root, double tuple_fraction, SetOperationStmt *setops)
Definition: planner.c:1302
static RelOptInfo * make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, Node *havingQual)
Definition: planner.c:3857
static List * generate_setop_child_grouplist(SetOperationStmt *op, List *targetlist)
Definition: planner.c:8060
static List * select_active_windows(PlannerInfo *root, WindowFuncLists *wflists)
Definition: planner.c:5846
Expr * preprocess_phv_expression(PlannerInfo *root, Expr *expr)
Definition: planner.c:1269
bool limit_needed(Query *parse)
Definition: planner.c:2625
create_upper_paths_hook_type create_upper_paths_hook
Definition: planner.c:74
#define EXPRKIND_TABLEFUNC
Definition: planner.c:89
static void consider_groupingsets_paths(PlannerInfo *root, RelOptInfo *grouped_rel, Path *path, bool is_sorted, bool can_hash, grouping_sets_data *gd, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: planner.c:4134
Expr * expression_planner_with_deps(Expr *expr, List **relationOids, List **invalItems)
Definition: planner.c:6607
static Path * make_ordered_path(PlannerInfo *root, RelOptInfo *rel, Path *path, Path *cheapest_path, List *pathkeys)
Definition: planner.c:6914
PlannedStmt * standard_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.c:287
static List * make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc, List *tlist)
Definition: planner.c:6123
static void add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, double dNumGroups, GroupPathExtraData *extra)
Definition: planner.c:6965
static RelOptInfo * create_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, double limit_tuples)
Definition: planner.c:5229
#define EXPRKIND_RTFUNC
Definition: planner.c:80
static double get_number_of_groups(PlannerInfo *root, double path_rows, grouping_sets_data *gd, List *target_list)
Definition: planner.c:3622
static List * extract_rollup_sets(List *groupingSets)
Definition: planner.c:2947
static RelOptInfo * create_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, grouping_sets_data *gd)
Definition: planner.c:3744
static void create_partitionwise_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, PartitionwiseAggregateType patype, GroupPathExtraData *extra)
Definition: planner.c:7859
Path * get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
Definition: planner.c:6421
#define EXPRKIND_ARBITER_ELEM
Definition: planner.c:88
static bool group_by_has_partkey(RelOptInfo *input_rel, List *targetList, List *groupClause)
Definition: planner.c:8003
static void standard_qp_callback(PlannerInfo *root, void *extra)
Definition: planner.c:3433
static RelOptInfo * create_window_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *input_target, PathTarget *output_target, bool output_target_parallel_safe, WindowFuncLists *wflists, List *activeWindows)
Definition: planner.c:4496
PlannedStmt *(* planner_hook_type)(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.h:26
void(* create_upper_paths_hook_type)(PlannerInfo *root, UpperRelationKind stage, RelOptInfo *input_rel, RelOptInfo *output_rel, void *extra)
Definition: planner.h:33
RowMarkType
Definition: plannodes.h:1328
@ ROW_MARK_COPY
Definition: plannodes.h:1334
@ ROW_MARK_REFERENCE
Definition: plannodes.h:1333
@ ROW_MARK_SHARE
Definition: plannodes.h:1331
@ ROW_MARK_EXCLUSIVE
Definition: plannodes.h:1329
@ ROW_MARK_NOKEYEXCLUSIVE
Definition: plannodes.h:1330
@ ROW_MARK_KEYSHARE
Definition: plannodes.h:1332
#define qsort(a, b, c, d)
Definition: port.h:453
#define printf(...)
Definition: port.h:244
void check_stack_depth(void)
Definition: postgres.c:3530
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void get_agg_clause_costs(PlannerInfo *root, AggSplit aggsplit, AggClauseCosts *costs)
Definition: prepagg.c:560
void preprocess_aggrefs(PlannerInfo *root, Node *clause)
Definition: prepagg.c:110
void preprocess_function_rtes(PlannerInfo *root)
Definition: prepjointree.c:776
void flatten_simple_union_all(PlannerInfo *root)
void transform_MERGE_to_join(Query *parse)
Definition: prepjointree.c:152
void remove_useless_result_rtes(PlannerInfo *root)
void pull_up_sublinks(PlannerInfo *root)
Definition: prepjointree.c:342
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:284
void pull_up_subqueries(PlannerInfo *root)
Definition: prepjointree.c:817
Relids get_relids_in_jointree(Node *jtnode, bool include_outer_joins, bool include_inner_joins)
void reduce_outer_joins(PlannerInfo *root)
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition: prepqual.c:293
char * c
e
Definition: preproc-init.c:82
void preprocess_targetlist(PlannerInfo *root)
Definition: preptlist.c:62
bool set_operation_ordered_results_useful(SetOperationStmt *setop)
Definition: prepunion.c:188
RelOptInfo * plan_set_operations(PlannerInfo *root)
Definition: prepunion.c:99
tree ctl root
Definition: radixtree.h:1886
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
List * RelationGetIndexPredicate(Relation relation)
Definition: relcache.c:5151
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:5038
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:94
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:192
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1470
@ ForwardScanDirection
Definition: sdir.h:28
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition: selfuncs.c:3416
double estimate_hashagg_tablesize(PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: selfuncs.c:3917
Plan * set_plan_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:287
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3577
List * aggrefs
Definition: pathnodes.h:3375
List * aggdistinct
Definition: primnodes.h:474
List * args
Definition: primnodes.h:468
List * aggorder
Definition: primnodes.h:471
GetForeignRowMarkType_function GetForeignRowMarkType
Definition: fdwapi.h:247
GetForeignUpperPaths_function GetForeignUpperPaths
Definition: fdwapi.h:226
Cardinality limit_tuples
Definition: pathnodes.h:3316
Node * quals
Definition: primnodes.h:2305
List * fromlist
Definition: primnodes.h:2304
int num_workers
Definition: plannodes.h:1143
bool invisible
Definition: plannodes.h:1146
bool single_copy
Definition: plannodes.h:1145
Plan plan
Definition: plannodes.h:1142
int rescan_param
Definition: plannodes.h:1144
PartitionwiseAggregateType patype
Definition: pathnodes.h:3300
AggClauseCosts agg_final_costs
Definition: pathnodes.h:3294
AggClauseCosts agg_partial_costs
Definition: pathnodes.h:3293
Cardinality numGroups
Definition: pathnodes.h:2273
Path path
Definition: pathnodes.h:1709
Definition: pg_list.h:54
Definition: nodes.h:129
List * exprs
Definition: pathnodes.h:1533
List * pathkeys
Definition: pathnodes.h:1665
Cardinality rows
Definition: pathnodes.h:1660
Cost total_cost
Definition: pathnodes.h:1662
LockClauseStrength strength
Definition: plannodes.h:1387
Index prti
Definition: plannodes.h:1383
RowMarkType markType
Definition: plannodes.h:1385
LockWaitPolicy waitPolicy
Definition: plannodes.h:1388
bool isParent
Definition: plannodes.h:1389
Index rowmarkId
Definition: plannodes.h:1384
int allMarkTypes
Definition: plannodes.h:1386
struct Plan * lefttree
Definition: plannodes.h:154
Cost total_cost
Definition: plannodes.h:129
struct Plan * righttree
Definition: plannodes.h:155
bool parallel_aware
Definition: plannodes.h:140
Cost startup_cost
Definition: plannodes.h:128
List * qual
Definition: plannodes.h:153
int plan_width
Definition: plannodes.h:135
bool parallel_safe
Definition: plannodes.h:141
Cardinality plan_rows
Definition: plannodes.h:134
List * targetlist
Definition: plannodes.h:152
List * initPlan
Definition: plannodes.h:156
struct Plan * planTree
Definition: plannodes.h:70
bool hasModifyingCTE
Definition: plannodes.h:58
List * appendRelations
Definition: plannodes.h:80
List * permInfos
Definition: plannodes.h:74
bool canSetTag
Definition: plannodes.h:60
List * rowMarks
Definition: plannodes.h:87
int jitFlags
Definition: plannodes.h:68
Bitmapset * rewindPlanIDs
Definition: plannodes.h:85
ParseLoc stmt_len
Definition: plannodes.h:99
bool hasReturning
Definition: plannodes.h:56
ParseLoc stmt_location
Definition: plannodes.h:98
List * invalItems
Definition: plannodes.h:91
bool transientPlan
Definition: plannodes.h:62
List * resultRelations
Definition: plannodes.h:78
List * subplans
Definition: plannodes.h:82
List * relationOids
Definition: plannodes.h:89
bool dependsOnRole
Definition: plannodes.h:64
CmdType commandType
Definition: plannodes.h:52
Node * utilityStmt
Definition: plannodes.h:95
List * rtable
Definition: plannodes.h:72
List * paramExecTypes
Definition: plannodes.h:93
bool parallelModeNeeded
Definition: plannodes.h:66
uint64 queryId
Definition: plannodes.h:54
int lastPlanNodeId
Definition: pathnodes.h:147
char maxParallelHazard
Definition: pathnodes.h:162
List * subplans
Definition: pathnodes.h:105
bool dependsOnRole
Definition: pathnodes.h:153
List * appendRelations
Definition: pathnodes.h:129
List * finalrowmarks
Definition: pathnodes.h:123
List * invalItems
Definition: pathnodes.h:135
List * relationOids
Definition: pathnodes.h:132
List * paramExecTypes
Definition: pathnodes.h:138
bool parallelModeOK
Definition: pathnodes.h:156
bool transientPlan
Definition: pathnodes.h:150
Bitmapset * rewindPlanIDs
Definition: pathnodes.h:114
List * finalrteperminfos
Definition: pathnodes.h:120
List * subpaths
Definition: pathnodes.h:108
Index lastPHId
Definition: pathnodes.h:141
Index lastRowMarkId
Definition: pathnodes.h:144
List * resultRelations
Definition: pathnodes.h:126
List * finalrtable
Definition: pathnodes.h:117
bool parallelModeNeeded
Definition: pathnodes.h:159
Index query_level
Definition: pathnodes.h:208
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47
List * rtable
Definition: parsenodes.h:168
CmdType commandType
Definition: parsenodes.h:121
TableFunc * tablefunc
Definition: parsenodes.h:1194
struct TableSampleClause * tablesample
Definition: parsenodes.h:1108
Query * subquery
Definition: parsenodes.h:1114
List * values_lists
Definition: parsenodes.h:1200
JoinType jointype
Definition: parsenodes.h:1161
List * functions
Definition: parsenodes.h:1187
RTEKind rtekind
Definition: parsenodes.h:1057
bool useridiscurrent
Definition: pathnodes.h:962
Relids relids
Definition: pathnodes.h:865
struct PathTarget * reltarget
Definition: pathnodes.h:887
Index relid
Definition: pathnodes.h:912
Cardinality tuples
Definition: pathnodes.h:943
bool consider_parallel
Definition: pathnodes.h:881
BlockNumber pages
Definition: pathnodes.h:942
List * pathlist
Definition: pathnodes.h:892
RelOptKind reloptkind
Definition: pathnodes.h:859
List * indexlist
Definition: pathnodes.h:938
struct Path * cheapest_startup_path
Definition: pathnodes.h:895
struct Path * cheapest_total_path
Definition: pathnodes.h:896
Oid userid
Definition: pathnodes.h:960
Oid serverid
Definition: pathnodes.h:958
Bitmapset * live_parts
Definition: pathnodes.h:1033
int rel_parallel_workers
Definition: pathnodes.h:950
List * partial_pathlist
Definition: pathnodes.h:894
Cardinality rows
Definition: pathnodes.h:871
Form_pg_class rd_rel
Definition: rel.h:111
Cardinality numGroups
Definition: pathnodes.h:2284
List * groupClause
Definition: pathnodes.h:2281
List * gsets_data
Definition: pathnodes.h:2283
bool hashable
Definition: pathnodes.h:2285
List * gsets
Definition: pathnodes.h:2282
bool is_hashed
Definition: pathnodes.h:2286
LockClauseStrength strength
Definition: parsenodes.h:1583
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1584
Index tleSortGroupRef
Definition: parsenodes.h:1442
struct WindowClause * window_clause
Definition: supportnodes.h:339
Expr * expr
Definition: primnodes.h:2186
AttrNumber resno
Definition: primnodes.h:2188
Index ressortgroupref
Definition: primnodes.h:2192
Definition: primnodes.h:248
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280
WindowClause * wc
Definition: planner.c:113
Node * startOffset
Definition: parsenodes.h:1550
List * partitionClause
Definition: parsenodes.h:1546
Node * endOffset
Definition: parsenodes.h:1551
List * orderClause
Definition: parsenodes.h:1548
List ** windowFuncs
Definition: clauses.h:23
Index maxWinRef
Definition: clauses.h:22
int numWindowFuncs
Definition: clauses.h:21
Index winref
Definition: primnodes.h:581
Oid winfnoid
Definition: primnodes.h:567
int * tleref_to_colnum_map
Definition: planner.c:104
List * rollups
Definition: planner.c:97
Bitmapset * unhashable_refs
Definition: planner.c:102
List * unsortable_sets
Definition: planner.c:103
List * hash_sets_idx
Definition: planner.c:98
double dNumHashGroups
Definition: planner.c:99
Bitmapset * unsortable_refs
Definition: planner.c:101
Definition: type.h:95
List * activeWindows
Definition: planner.c:121
grouping_sets_data * gset_data
Definition: planner.c:122
SetOperationStmt * setop
Definition: planner.c:123
Definition: regguts.h:323
void SS_process_ctes(PlannerInfo *root)
Definition: subselect.c:880
void SS_identify_outer_params(PlannerInfo *root)
Definition: subselect.c:2072
void SS_finalize_plan(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2254
Node * SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
Definition: subselect.c:1868
Node * SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
Definition: subselect.c:1919
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition: subselect.c:2198
void SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
Definition: subselect.c:2134
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
List * get_sortgrouplist_exprs(List *sgClauses, List *targetList)
Definition: tlist.c:392
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:218
PathTarget * copy_pathtarget(PathTarget *src)
Definition: tlist.c:657
SortGroupClause * get_sortgroupref_clause_noerr(Index sortref, List *clauses)
Definition: tlist.c:443
bool grouping_is_sortable(List *groupClause)
Definition: tlist.c:540
void add_new_columns_to_pathtarget(PathTarget *target, List *exprs)
Definition: tlist.c:752
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367
SortGroupClause * get_sortgroupref_clause(Index sortref, List *clauses)
Definition: tlist.c:422
void split_pathtarget_at_srfs(PlannerInfo *root, PathTarget *target, PathTarget *input_target, List **targets, List **targets_contain_srfs)
Definition: tlist.c:881
bool grouping_is_hashable(List *groupClause)
Definition: tlist.c:560
void add_column_to_pathtarget(PathTarget *target, Expr *expr, Index sortgroupref)
Definition: tlist.c:695
#define create_pathtarget(root, tlist)
Definition: tlist.h:53
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607
Node * flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
Definition: var.c:744