Debian Codegraph @

src/backend/optimizer/plan/planner.c

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