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