LCOV - code coverage report
Current view: top level - src/backend/optimizer/plan - planner.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 2409 2492 96.7 %
Date: 2025-10-23 18:17:25 Functions: 63 63 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * planner.c
       4             :  *    The query optimizer external interface.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/optimizer/plan/planner.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : 
      16             : #include "postgres.h"
      17             : 
      18             : #include <limits.h>
      19             : #include <math.h>
      20             : 
      21             : #include "access/genam.h"
      22             : #include "access/parallel.h"
      23             : #include "access/sysattr.h"
      24             : #include "access/table.h"
      25             : #include "catalog/pg_aggregate.h"
      26             : #include "catalog/pg_inherits.h"
      27             : #include "catalog/pg_proc.h"
      28             : #include "catalog/pg_type.h"
      29             : #include "executor/executor.h"
      30             : #include "foreign/fdwapi.h"
      31             : #include "jit/jit.h"
      32             : #include "lib/bipartite_match.h"
      33             : #include "lib/knapsack.h"
      34             : #include "miscadmin.h"
      35             : #include "nodes/makefuncs.h"
      36             : #include "nodes/nodeFuncs.h"
      37             : #ifdef OPTIMIZER_DEBUG
      38             : #include "nodes/print.h"
      39             : #endif
      40             : #include "nodes/supportnodes.h"
      41             : #include "optimizer/appendinfo.h"
      42             : #include "optimizer/clauses.h"
      43             : #include "optimizer/cost.h"
      44             : #include "optimizer/optimizer.h"
      45             : #include "optimizer/paramassign.h"
      46             : #include "optimizer/pathnode.h"
      47             : #include "optimizer/paths.h"
      48             : #include "optimizer/plancat.h"
      49             : #include "optimizer/planmain.h"
      50             : #include "optimizer/planner.h"
      51             : #include "optimizer/prep.h"
      52             : #include "optimizer/subselect.h"
      53             : #include "optimizer/tlist.h"
      54             : #include "parser/analyze.h"
      55             : #include "parser/parse_agg.h"
      56             : #include "parser/parse_clause.h"
      57             : #include "parser/parse_relation.h"
      58             : #include "parser/parsetree.h"
      59             : #include "partitioning/partdesc.h"
      60             : #include "rewrite/rewriteManip.h"
      61             : #include "utils/acl.h"
      62             : #include "utils/backend_status.h"
      63             : #include "utils/lsyscache.h"
      64             : #include "utils/rel.h"
      65             : #include "utils/selfuncs.h"
      66             : 
      67             : /* GUC parameters */
      68             : double      cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION;
      69             : int         debug_parallel_query = DEBUG_PARALLEL_OFF;
      70             : bool        parallel_leader_participation = true;
      71             : bool        enable_distinct_reordering = true;
      72             : 
      73             : /* Hook for plugins to get control in planner() */
      74             : planner_hook_type planner_hook = NULL;
      75             : 
      76             : /* Hook for plugins to get control after PlannerGlobal is initialized */
      77             : planner_setup_hook_type planner_setup_hook = NULL;
      78             : 
      79             : /* Hook for plugins to get control before PlannerGlobal is discarded */
      80             : planner_shutdown_hook_type planner_shutdown_hook = NULL;
      81             : 
      82             : /* Hook for plugins to get control when grouping_planner() plans upper rels */
      83             : create_upper_paths_hook_type create_upper_paths_hook = NULL;
      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             :  */
     105             : typedef struct
     106             : {
     107             :     List       *rollups;
     108             :     List       *hash_sets_idx;
     109             :     double      dNumHashGroups;
     110             :     bool        any_hashable;
     111             :     Bitmapset  *unsortable_refs;
     112             :     Bitmapset  *unhashable_refs;
     113             :     List       *unsortable_sets;
     114             :     int        *tleref_to_colnum_map;
     115             : } grouping_sets_data;
     116             : 
     117             : /*
     118             :  * Temporary structure for use during WindowClause reordering in order to be
     119             :  * able to sort WindowClauses on partitioning/ordering prefix.
     120             :  */
     121             : typedef struct
     122             : {
     123             :     WindowClause *wc;
     124             :     List       *uniqueOrder;    /* A List of unique ordering/partitioning
     125             :                                  * clauses per Window */
     126             : } WindowClauseSortData;
     127             : 
     128             : /* Passthrough data for standard_qp_callback */
     129             : typedef 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 */
     135             : } standard_qp_extra;
     136             : 
     137             : /* Local functions */
     138             : static Node *preprocess_expression(PlannerInfo *root, Node *expr, int kind);
     139             : static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode);
     140             : static void grouping_planner(PlannerInfo *root, double tuple_fraction,
     141             :                              SetOperationStmt *setops);
     142             : static grouping_sets_data *preprocess_grouping_sets(PlannerInfo *root);
     143             : static List *remap_to_groupclause_idx(List *groupClause, List *gsets,
     144             :                                       int *tleref_to_colnum_map);
     145             : static void preprocess_rowmarks(PlannerInfo *root);
     146             : static double preprocess_limit(PlannerInfo *root,
     147             :                                double tuple_fraction,
     148             :                                int64 *offset_est, int64 *count_est);
     149             : static List *preprocess_groupclause(PlannerInfo *root, List *force);
     150             : static List *extract_rollup_sets(List *groupingSets);
     151             : static List *reorder_grouping_sets(List *groupingSets, List *sortclause);
     152             : static void standard_qp_callback(PlannerInfo *root, void *extra);
     153             : static double get_number_of_groups(PlannerInfo *root,
     154             :                                    double path_rows,
     155             :                                    grouping_sets_data *gd,
     156             :                                    List *target_list);
     157             : static RelOptInfo *create_grouping_paths(PlannerInfo *root,
     158             :                                          RelOptInfo *input_rel,
     159             :                                          PathTarget *target,
     160             :                                          bool target_parallel_safe,
     161             :                                          grouping_sets_data *gd);
     162             : static bool is_degenerate_grouping(PlannerInfo *root);
     163             : static void create_degenerate_grouping_paths(PlannerInfo *root,
     164             :                                              RelOptInfo *input_rel,
     165             :                                              RelOptInfo *grouped_rel);
     166             : static RelOptInfo *make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
     167             :                                      PathTarget *target, bool target_parallel_safe,
     168             :                                      Node *havingQual);
     169             : static void create_ordinary_grouping_paths(PlannerInfo *root,
     170             :                                            RelOptInfo *input_rel,
     171             :                                            RelOptInfo *grouped_rel,
     172             :                                            const AggClauseCosts *agg_costs,
     173             :                                            grouping_sets_data *gd,
     174             :                                            GroupPathExtraData *extra,
     175             :                                            RelOptInfo **partially_grouped_rel_p);
     176             : static void consider_groupingsets_paths(PlannerInfo *root,
     177             :                                         RelOptInfo *grouped_rel,
     178             :                                         Path *path,
     179             :                                         bool is_sorted,
     180             :                                         bool can_hash,
     181             :                                         grouping_sets_data *gd,
     182             :                                         const AggClauseCosts *agg_costs,
     183             :                                         double dNumGroups);
     184             : static RelOptInfo *create_window_paths(PlannerInfo *root,
     185             :                                        RelOptInfo *input_rel,
     186             :                                        PathTarget *input_target,
     187             :                                        PathTarget *output_target,
     188             :                                        bool output_target_parallel_safe,
     189             :                                        WindowFuncLists *wflists,
     190             :                                        List *activeWindows);
     191             : static void create_one_window_path(PlannerInfo *root,
     192             :                                    RelOptInfo *window_rel,
     193             :                                    Path *path,
     194             :                                    PathTarget *input_target,
     195             :                                    PathTarget *output_target,
     196             :                                    WindowFuncLists *wflists,
     197             :                                    List *activeWindows);
     198             : static RelOptInfo *create_distinct_paths(PlannerInfo *root,
     199             :                                          RelOptInfo *input_rel,
     200             :                                          PathTarget *target);
     201             : static void create_partial_distinct_paths(PlannerInfo *root,
     202             :                                           RelOptInfo *input_rel,
     203             :                                           RelOptInfo *final_distinct_rel,
     204             :                                           PathTarget *target);
     205             : static RelOptInfo *create_final_distinct_paths(PlannerInfo *root,
     206             :                                                RelOptInfo *input_rel,
     207             :                                                RelOptInfo *distinct_rel);
     208             : static List *get_useful_pathkeys_for_distinct(PlannerInfo *root,
     209             :                                               List *needed_pathkeys,
     210             :                                               List *path_pathkeys);
     211             : static RelOptInfo *create_ordered_paths(PlannerInfo *root,
     212             :                                         RelOptInfo *input_rel,
     213             :                                         PathTarget *target,
     214             :                                         bool target_parallel_safe,
     215             :                                         double limit_tuples);
     216             : static PathTarget *make_group_input_target(PlannerInfo *root,
     217             :                                            PathTarget *final_target);
     218             : static PathTarget *make_partial_grouping_target(PlannerInfo *root,
     219             :                                                 PathTarget *grouping_target,
     220             :                                                 Node *havingQual);
     221             : static List *postprocess_setop_tlist(List *new_tlist, List *orig_tlist);
     222             : static void optimize_window_clauses(PlannerInfo *root,
     223             :                                     WindowFuncLists *wflists);
     224             : static List *select_active_windows(PlannerInfo *root, WindowFuncLists *wflists);
     225             : static void name_active_windows(List *activeWindows);
     226             : static PathTarget *make_window_input_target(PlannerInfo *root,
     227             :                                             PathTarget *final_target,
     228             :                                             List *activeWindows);
     229             : static List *make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
     230             :                                       List *tlist);
     231             : static PathTarget *make_sort_input_target(PlannerInfo *root,
     232             :                                           PathTarget *final_target,
     233             :                                           bool *have_postponed_srfs);
     234             : static void adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel,
     235             :                                   List *targets, List *targets_contain_srfs);
     236             : static void add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
     237             :                                       RelOptInfo *grouped_rel,
     238             :                                       RelOptInfo *partially_grouped_rel,
     239             :                                       const AggClauseCosts *agg_costs,
     240             :                                       grouping_sets_data *gd,
     241             :                                       GroupPathExtraData *extra);
     242             : static RelOptInfo *create_partial_grouping_paths(PlannerInfo *root,
     243             :                                                  RelOptInfo *grouped_rel,
     244             :                                                  RelOptInfo *input_rel,
     245             :                                                  grouping_sets_data *gd,
     246             :                                                  GroupPathExtraData *extra,
     247             :                                                  bool force_rel_creation);
     248             : static Path *make_ordered_path(PlannerInfo *root,
     249             :                                RelOptInfo *rel,
     250             :                                Path *path,
     251             :                                Path *cheapest_path,
     252             :                                List *pathkeys,
     253             :                                double limit_tuples);
     254             : static void gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel);
     255             : static bool can_partial_agg(PlannerInfo *root);
     256             : static void apply_scanjoin_target_to_paths(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);
     262             : static void create_partitionwise_grouping_paths(PlannerInfo *root,
     263             :                                                 RelOptInfo *input_rel,
     264             :                                                 RelOptInfo *grouped_rel,
     265             :                                                 RelOptInfo *partially_grouped_rel,
     266             :                                                 const AggClauseCosts *agg_costs,
     267             :                                                 grouping_sets_data *gd,
     268             :                                                 PartitionwiseAggregateType patype,
     269             :                                                 GroupPathExtraData *extra);
     270             : static bool group_by_has_partkey(RelOptInfo *input_rel,
     271             :                                  List *targetList,
     272             :                                  List *groupClause);
     273             : static int  common_prefix_cmp(const void *a, const void *b);
     274             : static List *generate_setop_child_grouplist(SetOperationStmt *op,
     275             :                                             List *targetlist);
     276             : static void create_final_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
     277             :                                       List *sortPathkeys, List *groupClause,
     278             :                                       SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel);
     279             : static void create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_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             :  *****************************************************************************/
     314             : PlannedStmt *
     315      450226 : planner(Query *parse, const char *query_string, int cursorOptions,
     316             :         ParamListInfo boundParams, ExplainState *es)
     317             : {
     318             :     PlannedStmt *result;
     319             : 
     320      450226 :     if (planner_hook)
     321       94932 :         result = (*planner_hook) (parse, query_string, cursorOptions,
     322             :                                   boundParams, es);
     323             :     else
     324      355294 :         result = standard_planner(parse, query_string, cursorOptions,
     325             :                                   boundParams, es);
     326             : 
     327      445410 :     pgstat_report_plan_id(result->planId, false);
     328             : 
     329      445410 :     return result;
     330             : }
     331             : 
     332             : PlannedStmt *
     333      450226 : standard_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;
     339             :     PlannerInfo *root;
     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      450226 :     glob = makeNode(PlannerGlobal);
     353             : 
     354      450226 :     glob->boundParams = boundParams;
     355      450226 :     glob->subplans = NIL;
     356      450226 :     glob->subpaths = NIL;
     357      450226 :     glob->subroots = NIL;
     358      450226 :     glob->rewindPlanIDs = NULL;
     359      450226 :     glob->finalrtable = NIL;
     360      450226 :     glob->allRelids = NULL;
     361      450226 :     glob->prunableRelids = NULL;
     362      450226 :     glob->finalrteperminfos = NIL;
     363      450226 :     glob->finalrowmarks = NIL;
     364      450226 :     glob->resultRelations = NIL;
     365      450226 :     glob->appendRelations = NIL;
     366      450226 :     glob->partPruneInfos = NIL;
     367      450226 :     glob->relationOids = NIL;
     368      450226 :     glob->invalItems = NIL;
     369      450226 :     glob->paramExecTypes = NIL;
     370      450226 :     glob->lastPHId = 0;
     371      450226 :     glob->lastRowMarkId = 0;
     372      450226 :     glob->lastPlanNodeId = 0;
     373      450226 :     glob->transientPlan = false;
     374      450226 :     glob->dependsOnRole = false;
     375      450226 :     glob->partition_directory = NULL;
     376      450226 :     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      450226 :     if ((cursorOptions & CURSOR_OPT_PARALLEL_OK) != 0 &&
     400      422012 :         IsUnderPostmaster &&
     401      422012 :         parse->commandType == CMD_SELECT &&
     402      339512 :         !parse->hasModifyingCTE &&
     403      339368 :         max_parallel_workers_per_gather > 0 &&
     404      338754 :         !IsParallelWorker())
     405             :     {
     406             :         /* all the cheap tests pass, so scan the query tree */
     407      338706 :         glob->maxParallelHazard = max_parallel_hazard(parse);
     408      338706 :         glob->parallelModeOK = (glob->maxParallelHazard != PROPARALLEL_UNSAFE);
     409             :     }
     410             :     else
     411             :     {
     412             :         /* skip the query tree scan, just assume it's unsafe */
     413      111520 :         glob->maxParallelHazard = PROPARALLEL_UNSAFE;
     414      111520 :         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      739828 :     glob->parallelModeNeeded = glob->parallelModeOK &&
     435      289602 :         (debug_parallel_query != DEBUG_PARALLEL_OFF);
     436             : 
     437             :     /* Determine what fraction of the plan is likely to be scanned */
     438      450226 :     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        4700 :         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        4700 :         if (tuple_fraction >= 1.0)
     455           0 :             tuple_fraction = 0.0;
     456        4700 :         else if (tuple_fraction <= 0.0)
     457           0 :             tuple_fraction = 1e-10;
     458             :     }
     459             :     else
     460             :     {
     461             :         /* Default assumption is we need all the tuples */
     462      445526 :         tuple_fraction = 0.0;
     463             :     }
     464             : 
     465             :     /* Allow plugins to take control after we've initialized "glob" */
     466      450226 :     if (planner_setup_hook)
     467           0 :         (*planner_setup_hook) (glob, parse, query_string, &tuple_fraction, es);
     468             : 
     469             :     /* primary planning entry point (may recurse for subqueries) */
     470      450226 :     root = subquery_planner(glob, parse, NULL, NULL, false, tuple_fraction,
     471             :                             NULL);
     472             : 
     473             :     /* Select best Path and turn it into a Plan */
     474      445806 :     final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
     475      445806 :     best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
     476             : 
     477      445806 :     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      445410 :     if (cursorOptions & CURSOR_OPT_SCROLL)
     484             :     {
     485         266 :         if (!ExecSupportsBackwardScan(top_plan))
     486          32 :             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             :      */
     501      445410 :     if (debug_parallel_query != DEBUG_PARALLEL_OFF &&
     502         194 :         top_plan->parallel_safe &&
     503         128 :         (top_plan->initPlan == NIL ||
     504           0 :          debug_parallel_query != DEBUG_PARALLEL_REGRESS))
     505             :     {
     506         128 :         Gather     *gather = makeNode(Gather);
     507             :         Cost        initplan_cost;
     508             :         bool        unsafe_initplans;
     509             : 
     510         128 :         gather->plan.targetlist = top_plan->targetlist;
     511         128 :         gather->plan.qual = NIL;
     512         128 :         gather->plan.lefttree = top_plan;
     513         128 :         gather->plan.righttree = NULL;
     514         128 :         gather->num_workers = 1;
     515         128 :         gather->single_copy = true;
     516         128 :         gather->invisible = (debug_parallel_query == DEBUG_PARALLEL_REGRESS);
     517             : 
     518             :         /* Transfer any initPlans to the new top node */
     519         128 :         gather->plan.initPlan = top_plan->initPlan;
     520         128 :         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         128 :         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         128 :         gather->plan.startup_cost = top_plan->startup_cost +
     533             :             parallel_setup_cost;
     534         128 :         gather->plan.total_cost = top_plan->total_cost +
     535         128 :             parallel_setup_cost + parallel_tuple_cost * top_plan->plan_rows;
     536         128 :         gather->plan.plan_rows = top_plan->plan_rows;
     537         128 :         gather->plan.plan_width = top_plan->plan_width;
     538         128 :         gather->plan.parallel_aware = false;
     539         128 :         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             :          */
     545         128 :         SS_compute_initplan_cost(gather->plan.initPlan,
     546             :                                  &initplan_cost, &unsafe_initplans);
     547         128 :         top_plan->startup_cost -= initplan_cost;
     548         128 :         top_plan->total_cost -= initplan_cost;
     549             : 
     550             :         /* use parallel mode for parallel plans. */
     551         128 :         root->glob->parallelModeNeeded = true;
     552             : 
     553         128 :         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      445410 :     if (glob->paramExecTypes != NIL)
     563             :     {
     564             :         Assert(list_length(glob->subplans) == list_length(glob->subroots));
     565      197148 :         forboth(lp, glob->subplans, lr, glob->subroots)
     566             :         {
     567       43510 :             Plan       *subplan = (Plan *) lfirst(lp);
     568       43510 :             PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
     569             : 
     570       43510 :             SS_finalize_plan(subroot, subplan);
     571             :         }
     572      153638 :         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      445410 :     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      488920 :     forboth(lp, glob->subplans, lr, glob->subroots)
     585             :     {
     586       43510 :         Plan       *subplan = (Plan *) lfirst(lp);
     587       43510 :         PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
     588             : 
     589       43510 :         lfirst(lp) = set_plan_references(subroot, subplan);
     590             :     }
     591             : 
     592             :     /* build the PlannedStmt result */
     593      445410 :     result = makeNode(PlannedStmt);
     594             : 
     595      445410 :     result->commandType = parse->commandType;
     596      445410 :     result->queryId = parse->queryId;
     597      445410 :     result->planOrigin = PLAN_STMT_STANDARD;
     598      445410 :     result->hasReturning = (parse->returningList != NIL);
     599      445410 :     result->hasModifyingCTE = parse->hasModifyingCTE;
     600      445410 :     result->canSetTag = parse->canSetTag;
     601      445410 :     result->transientPlan = glob->transientPlan;
     602      445410 :     result->dependsOnRole = glob->dependsOnRole;
     603      445410 :     result->parallelModeNeeded = glob->parallelModeNeeded;
     604      445410 :     result->planTree = top_plan;
     605      445410 :     result->partPruneInfos = glob->partPruneInfos;
     606      445410 :     result->rtable = glob->finalrtable;
     607      890820 :     result->unprunableRelids = bms_difference(glob->allRelids,
     608      445410 :                                               glob->prunableRelids);
     609      445410 :     result->permInfos = glob->finalrteperminfos;
     610      445410 :     result->resultRelations = glob->resultRelations;
     611      445410 :     result->appendRelations = glob->appendRelations;
     612      445410 :     result->subplans = glob->subplans;
     613      445410 :     result->rewindPlanIDs = glob->rewindPlanIDs;
     614      445410 :     result->rowMarks = glob->finalrowmarks;
     615      445410 :     result->relationOids = glob->relationOids;
     616      445410 :     result->invalItems = glob->invalItems;
     617      445410 :     result->paramExecTypes = glob->paramExecTypes;
     618             :     /* utilityStmt should be null, but we might as well copy it */
     619      445410 :     result->utilityStmt = parse->utilityStmt;
     620      445410 :     result->stmt_location = parse->stmt_location;
     621      445410 :     result->stmt_len = parse->stmt_len;
     622             : 
     623      445410 :     result->jitFlags = PGJIT_NONE;
     624      445410 :     if (jit_enabled && jit_above_cost >= 0 &&
     625      444718 :         top_plan->total_cost > jit_above_cost)
     626             :     {
     627         954 :         result->jitFlags |= PGJIT_PERFORM;
     628             : 
     629             :         /*
     630             :          * Decide how much effort should be put into generating better code.
     631             :          */
     632         954 :         if (jit_optimize_above_cost >= 0 &&
     633         954 :             top_plan->total_cost > jit_optimize_above_cost)
     634         436 :             result->jitFlags |= PGJIT_OPT3;
     635         954 :         if (jit_inline_above_cost >= 0 &&
     636         954 :             top_plan->total_cost > jit_inline_above_cost)
     637         436 :             result->jitFlags |= PGJIT_INLINE;
     638             : 
     639             :         /*
     640             :          * Decide which operations should be JITed.
     641             :          */
     642         954 :         if (jit_expressions)
     643         954 :             result->jitFlags |= PGJIT_EXPR;
     644         954 :         if (jit_tuple_deforming)
     645         954 :             result->jitFlags |= PGJIT_DEFORM;
     646             :     }
     647             : 
     648             :     /* Allow plugins to take control before we discard "glob" */
     649      445410 :     if (planner_shutdown_hook)
     650           0 :         (*planner_shutdown_hook) (glob, parse, query_string, result);
     651             : 
     652      445410 :     if (glob->partition_directory != NULL)
     653       11780 :         DestroyPartitionDirectory(glob->partition_directory);
     654             : 
     655      445410 :     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             :  */
     692             : PlannerInfo *
     693      526750 : subquery_planner(PlannerGlobal *glob, Query *parse, char *plan_name,
     694             :                  PlannerInfo *parent_root, bool hasRecursion,
     695             :                  double tuple_fraction, SetOperationStmt *setops)
     696             : {
     697             :     PlannerInfo *root;
     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 */
     706      526750 :     root = makeNode(PlannerInfo);
     707      526750 :     root->parse = parse;
     708      526750 :     root->glob = glob;
     709      526750 :     root->query_level = parent_root ? parent_root->query_level + 1 : 1;
     710      526750 :     root->plan_name = plan_name;
     711      526750 :     root->parent_root = parent_root;
     712      526750 :     root->plan_params = NIL;
     713      526750 :     root->outer_params = NULL;
     714      526750 :     root->planner_cxt = CurrentMemoryContext;
     715      526750 :     root->init_plans = NIL;
     716      526750 :     root->cte_plan_ids = NIL;
     717      526750 :     root->multiexpr_params = NIL;
     718      526750 :     root->join_domains = NIL;
     719      526750 :     root->eq_classes = NIL;
     720      526750 :     root->ec_merging_done = false;
     721      526750 :     root->last_rinfo_serial = 0;
     722      526750 :     root->all_result_relids =
     723      526750 :         parse->resultRelation ? bms_make_singleton(parse->resultRelation) : NULL;
     724      526750 :     root->leaf_result_relids = NULL; /* we'll find out leaf-ness later */
     725      526750 :     root->append_rel_list = NIL;
     726      526750 :     root->row_identity_vars = NIL;
     727      526750 :     root->rowMarks = NIL;
     728      526750 :     memset(root->upper_rels, 0, sizeof(root->upper_rels));
     729      526750 :     memset(root->upper_targets, 0, sizeof(root->upper_targets));
     730      526750 :     root->processed_groupClause = NIL;
     731      526750 :     root->processed_distinctClause = NIL;
     732      526750 :     root->processed_tlist = NIL;
     733      526750 :     root->update_colnos = NIL;
     734      526750 :     root->grouping_map = NULL;
     735      526750 :     root->minmax_aggs = NIL;
     736      526750 :     root->qual_security_level = 0;
     737      526750 :     root->hasPseudoConstantQuals = false;
     738      526750 :     root->hasAlternativeSubPlans = false;
     739      526750 :     root->placeholdersFrozen = false;
     740      526750 :     root->hasRecursion = hasRecursion;
     741      526750 :     root->assumeReplanning = false;
     742      526750 :     if (hasRecursion)
     743         930 :         root->wt_param_id = assign_special_exec_param(root);
     744             :     else
     745      525820 :         root->wt_param_id = -1;
     746      526750 :     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      526750 :     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      526750 :     if (parse->cteList)
     760        2874 :         SS_process_ctes(root);
     761             : 
     762             :     /*
     763             :      * If it's a MERGE command, transform the joinlist as appropriate.
     764             :      */
     765      526744 :     transform_MERGE_to_join(parse);
     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             :      */
     777      526744 :     parse = root->parse = preprocess_relation_rtes(root);
     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             :      */
     783      526744 :     replace_empty_jointree(parse);
     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      526744 :     if (parse->hasSubLinks)
     792       36282 :         pull_up_sublinks(root);
     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             :      */
     800      526744 :     preprocess_function_rtes(root);
     801             : 
     802             :     /*
     803             :      * Check to see if any subqueries in the jointree can be merged into this
     804             :      * query.
     805             :      */
     806      526738 :     pull_up_subqueries(root);
     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      526732 :     if (parse->setOperations)
     815        6878 :         flatten_simple_union_all(root);
     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      526732 :     root->hasJoinRTEs = false;
     828      526732 :     root->hasLateralRTEs = false;
     829      526732 :     root->group_rtindex = 0;
     830      526732 :     hasOuterJoins = false;
     831      526732 :     hasResultRTEs = false;
     832     1441832 :     foreach(l, parse->rtable)
     833             :     {
     834      915100 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
     835             : 
     836      915100 :         switch (rte->rtekind)
     837             :         {
     838       92882 :             case RTE_JOIN:
     839       92882 :                 root->hasJoinRTEs = true;
     840       92882 :                 if (IS_OUTER_JOIN(rte->jointype))
     841       47912 :                     hasOuterJoins = true;
     842       92882 :                 break;
     843      201998 :             case RTE_RESULT:
     844      201998 :                 hasResultRTEs = true;
     845      201998 :                 break;
     846        4782 :             case RTE_GROUP:
     847             :                 Assert(parse->hasGroupRTE);
     848        4782 :                 root->group_rtindex = list_cell_number(parse->rtable, l) + 1;
     849        4782 :                 break;
     850      615438 :             default:
     851             :                 /* No work here for other RTE types */
     852      615438 :                 break;
     853             :         }
     854             : 
     855      915100 :         if (rte->lateral)
     856       10968 :             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      915100 :         if (rte->securityQuals)
     865        2622 :             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      526732 :     if (parse->resultRelation)
     874             :     {
     875       89372 :         RangeTblEntry *rte = rt_fetch(parse->resultRelation, parse->rtable);
     876             : 
     877       89372 :         if (!rte->inh)
     878       86474 :             root->leaf_result_relids =
     879       86474 :                 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     1440712 :     foreach(l, parse->rtable)
     897             :     {
     898      914366 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
     899             : 
     900      914366 :         if (rte->perminfoindex != 0 &&
     901      489654 :             rte->relkind == RELKIND_VIEW)
     902             :         {
     903             :             RTEPermissionInfo *perminfo;
     904             :             bool        result;
     905             : 
     906       20758 :             perminfo = getRTEPermissionInfo(parse->rteperminfos, rte);
     907       20758 :             result = ExecCheckOneRelPerms(perminfo);
     908       20758 :             if (!result)
     909         386 :                 aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_VIEW,
     910         386 :                                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             :      */
     918      526346 :     preprocess_rowmarks(root);
     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      526346 :     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      522396 :     parse->targetList = (List *)
     934      526346 :         preprocess_expression(root, (Node *) parse->targetList,
     935             :                               EXPRKIND_TARGET);
     936             : 
     937      522396 :     newWithCheckOptions = NIL;
     938      524884 :     foreach(l, parse->withCheckOptions)
     939             :     {
     940        2488 :         WithCheckOption *wco = lfirst_node(WithCheckOption, l);
     941             : 
     942        2488 :         wco->qual = preprocess_expression(root, wco->qual,
     943             :                                           EXPRKIND_QUAL);
     944        2488 :         if (wco->qual != NULL)
     945        2088 :             newWithCheckOptions = lappend(newWithCheckOptions, wco);
     946             :     }
     947      522396 :     parse->withCheckOptions = newWithCheckOptions;
     948             : 
     949      522396 :     parse->returningList = (List *)
     950      522396 :         preprocess_expression(root, (Node *) parse->returningList,
     951             :                               EXPRKIND_TARGET);
     952             : 
     953      522396 :     preprocess_qual_conditions(root, (Node *) parse->jointree);
     954             : 
     955      522396 :     parse->havingQual = preprocess_expression(root, parse->havingQual,
     956             :                                               EXPRKIND_QUAL);
     957             : 
     958      525218 :     foreach(l, parse->windowClause)
     959             :     {
     960        2822 :         WindowClause *wc = lfirst_node(WindowClause, l);
     961             : 
     962             :         /* partitionClause/orderClause are sort/group expressions */
     963        2822 :         wc->startOffset = preprocess_expression(root, wc->startOffset,
     964             :                                                 EXPRKIND_LIMIT);
     965        2822 :         wc->endOffset = preprocess_expression(root, wc->endOffset,
     966             :                                               EXPRKIND_LIMIT);
     967             :     }
     968             : 
     969      522396 :     parse->limitOffset = preprocess_expression(root, parse->limitOffset,
     970             :                                                EXPRKIND_LIMIT);
     971      522396 :     parse->limitCount = preprocess_expression(root, parse->limitCount,
     972             :                                               EXPRKIND_LIMIT);
     973             : 
     974      522396 :     if (parse->onConflict)
     975             :     {
     976        3640 :         parse->onConflict->arbiterElems = (List *)
     977        1820 :             preprocess_expression(root,
     978        1820 :                                   (Node *) parse->onConflict->arbiterElems,
     979             :                                   EXPRKIND_ARBITER_ELEM);
     980        3640 :         parse->onConflict->arbiterWhere =
     981        1820 :             preprocess_expression(root,
     982        1820 :                                   parse->onConflict->arbiterWhere,
     983             :                                   EXPRKIND_QUAL);
     984        3640 :         parse->onConflict->onConflictSet = (List *)
     985        1820 :             preprocess_expression(root,
     986        1820 :                                   (Node *) parse->onConflict->onConflictSet,
     987             :                                   EXPRKIND_TARGET);
     988        1820 :         parse->onConflict->onConflictWhere =
     989        1820 :             preprocess_expression(root,
     990        1820 :                                   parse->onConflict->onConflictWhere,
     991             :                                   EXPRKIND_QUAL);
     992             :         /* exclRelTlist contains only Vars, so no preprocessing needed */
     993             :     }
     994             : 
     995      525192 :     foreach(l, parse->mergeActionList)
     996             :     {
     997        2796 :         MergeAction *action = (MergeAction *) lfirst(l);
     998             : 
     999        2796 :         action->targetList = (List *)
    1000        2796 :             preprocess_expression(root,
    1001        2796 :                                   (Node *) action->targetList,
    1002             :                                   EXPRKIND_TARGET);
    1003        2796 :         action->qual =
    1004        2796 :             preprocess_expression(root,
    1005             :                                   (Node *) action->qual,
    1006             :                                   EXPRKIND_QUAL);
    1007             :     }
    1008             : 
    1009      522396 :     parse->mergeJoinCondition =
    1010      522396 :         preprocess_expression(root, parse->mergeJoinCondition, EXPRKIND_QUAL);
    1011             : 
    1012      522396 :     root->append_rel_list = (List *)
    1013      522396 :         preprocess_expression(root, (Node *) root->append_rel_list,
    1014             :                               EXPRKIND_APPINFO);
    1015             : 
    1016             :     /* Also need to preprocess expressions within RTEs */
    1017     1431830 :     foreach(l, parse->rtable)
    1018             :     {
    1019      909434 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
    1020             :         int         kind;
    1021             :         ListCell   *lcsq;
    1022             : 
    1023      909434 :         if (rte->rtekind == RTE_RELATION)
    1024             :         {
    1025      473078 :             if (rte->tablesample)
    1026         228 :                 rte->tablesample = (TableSampleClause *)
    1027         228 :                     preprocess_expression(root,
    1028         228 :                                           (Node *) rte->tablesample,
    1029             :                                           EXPRKIND_TABLESAMPLE);
    1030             :         }
    1031      436356 :         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       75226 :             if (rte->lateral && root->hasJoinRTEs)
    1041        1434 :                 rte->subquery = (Query *)
    1042        1434 :                     flatten_join_alias_vars(root, root->parse,
    1043        1434 :                                             (Node *) rte->subquery);
    1044             :         }
    1045      361130 :         else if (rte->rtekind == RTE_FUNCTION)
    1046             :         {
    1047             :             /* Preprocess the function expression(s) fully */
    1048       51144 :             kind = rte->lateral ? EXPRKIND_RTFUNC_LATERAL : EXPRKIND_RTFUNC;
    1049       51144 :             rte->functions = (List *)
    1050       51144 :                 preprocess_expression(root, (Node *) rte->functions, kind);
    1051             :         }
    1052      309986 :         else if (rte->rtekind == RTE_TABLEFUNC)
    1053             :         {
    1054             :             /* Preprocess the function expression(s) fully */
    1055         626 :             kind = rte->lateral ? EXPRKIND_TABLEFUNC_LATERAL : EXPRKIND_TABLEFUNC;
    1056         626 :             rte->tablefunc = (TableFunc *)
    1057         626 :                 preprocess_expression(root, (Node *) rte->tablefunc, kind);
    1058             :         }
    1059      309360 :         else if (rte->rtekind == RTE_VALUES)
    1060             :         {
    1061             :             /* Preprocess the values lists fully */
    1062        8272 :             kind = rte->lateral ? EXPRKIND_VALUES_LATERAL : EXPRKIND_VALUES;
    1063        8272 :             rte->values_lists = (List *)
    1064        8272 :                 preprocess_expression(root, (Node *) rte->values_lists, kind);
    1065             :         }
    1066      301088 :         else if (rte->rtekind == RTE_GROUP)
    1067             :         {
    1068             :             /* Preprocess the groupexprs list fully */
    1069        4782 :             rte->groupexprs = (List *)
    1070        4782 :                 preprocess_expression(root, (Node *) rte->groupexprs,
    1071             :                                       EXPRKIND_GROUPEXPR);
    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      912402 :         foreach(lcsq, rte->securityQuals)
    1081             :         {
    1082        2968 :             lfirst(lcsq) = preprocess_expression(root,
    1083        2968 :                                                  (Node *) lfirst(lcsq),
    1084             :                                                  EXPRKIND_QUAL);
    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      522396 :     if (root->hasJoinRTEs)
    1100             :     {
    1101      322316 :         foreach(l, parse->rtable)
    1102             :         {
    1103      265164 :             RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
    1104             : 
    1105      265164 :             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      522396 :     if (parse->hasGroupRTE)
    1119             :     {
    1120        4782 :         parse->targetList = (List *)
    1121        4782 :             flatten_group_exprs(root, root->parse, (Node *) parse->targetList);
    1122        4782 :         parse->havingQual =
    1123        4782 :             flatten_group_exprs(root, root->parse, parse->havingQual);
    1124             :     }
    1125             : 
    1126             :     /* Constant-folding might have removed all set-returning functions */
    1127      522396 :     if (parse->hasTargetSRFs)
    1128       11704 :         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      522396 :     if (parse->groupingSets)
    1137             :     {
    1138         938 :         parse->groupingSets =
    1139         938 :             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      522396 :     newHaving = NIL;
    1185      523658 :     foreach(l, (List *) parse->havingQual)
    1186             :     {
    1187        1262 :         Node       *havingclause = (Node *) lfirst(l);
    1188             : 
    1189        1658 :         if (contain_agg_clause(havingclause) ||
    1190         792 :             contain_volatile_functions(havingclause) ||
    1191         396 :             contain_subplans(havingclause) ||
    1192         516 :             (parse->groupClause && parse->groupingSets &&
    1193         120 :              bms_is_member(root->group_rtindex, pull_varnos(root, havingclause))))
    1194             :         {
    1195             :             /* keep it in HAVING */
    1196         938 :             newHaving = lappend(newHaving, havingclause);
    1197             :         }
    1198         324 :         else if (parse->groupClause &&
    1199         288 :                  (parse->groupingSets == NIL ||
    1200          48 :                   (List *) linitial(parse->groupingSets) != NIL))
    1201         276 :         {
    1202             :             /* There is GROUP BY, but no empty grouping set */
    1203             :             Node       *whereclause;
    1204             : 
    1205             :             /* Preprocess the HAVING clause fully */
    1206         276 :             whereclause = preprocess_expression(root, havingclause,
    1207             :                                                 EXPRKIND_QUAL);
    1208             :             /* ... and move it to WHERE */
    1209         276 :             parse->jointree->quals = (Node *)
    1210         276 :                 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          48 :             whereclause = preprocess_expression(root, copyObject(havingclause),
    1220             :                                                 EXPRKIND_QUAL);
    1221             :             /* ... and put a copy in WHERE */
    1222          96 :             parse->jointree->quals = (Node *)
    1223          48 :                 list_concat((List *) parse->jointree->quals,
    1224             :                             (List *) whereclause);
    1225             :             /* ... and also keep it in HAVING */
    1226          48 :             newHaving = lappend(newHaving, havingclause);
    1227             :         }
    1228             :     }
    1229      522396 :     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      522396 :     if (hasOuterJoins)
    1237       33548 :         reduce_outer_joins(root);
    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      522396 :     if (hasResultRTEs || hasOuterJoins)
    1246      230076 :         remove_useless_result_rtes(root);
    1247             : 
    1248             :     /*
    1249             :      * Do the main planning.
    1250             :      */
    1251      522396 :     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             :      */
    1257      522324 :     SS_identify_outer_params(root);
    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      522324 :     final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
    1266      522324 :     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      522324 :     set_cheapest(final_rel);
    1274             : 
    1275      522324 :     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             :  */
    1284             : static Node *
    1285     4388142 : preprocess_expression(PlannerInfo *root, Node *expr, int kind)
    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     4388142 :     if (expr == NULL)
    1293     3462482 :         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      925660 :     if (root->hasJoinRTEs &&
    1305      408870 :         !(kind == EXPRKIND_RTFUNC ||
    1306      204262 :           kind == EXPRKIND_VALUES ||
    1307             :           kind == EXPRKIND_TABLESAMPLE ||
    1308             :           kind == EXPRKIND_TABLEFUNC))
    1309      204244 :         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      925660 :     if (kind != EXPRKIND_RTFUNC)
    1331      883126 :         expr = eval_const_expressions(root, expr);
    1332             : 
    1333             :     /*
    1334             :      * If it's a qual or havingQual, canonicalize it.
    1335             :      */
    1336      921710 :     if (kind == EXPRKIND_QUAL)
    1337             :     {
    1338      333072 :         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      921710 :     if (kind == EXPRKIND_QUAL || kind == EXPRKIND_TARGET)
    1352             :     {
    1353      842618 :         convert_saop_to_hashed_saop(expr);
    1354             :     }
    1355             : 
    1356             :     /* Expand SubLinks to SubPlans */
    1357      921710 :     if (root->parse->hasSubLinks)
    1358      104554 :         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      921710 :     if (root->query_level > 1)
    1367      172868 :         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      921710 :     if (kind == EXPRKIND_QUAL)
    1376      333072 :         expr = (Node *) make_ands_implicit((Expr *) expr);
    1377             : 
    1378      921710 :     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             :  */
    1386             : static void
    1387     1305206 : preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
    1388             : {
    1389     1305206 :     if (jtnode == NULL)
    1390           0 :         return;
    1391     1305206 :     if (IsA(jtnode, RangeTblRef))
    1392             :     {
    1393             :         /* nothing to do here */
    1394             :     }
    1395      637982 :     else if (IsA(jtnode, FromExpr))
    1396             :     {
    1397      537442 :         FromExpr   *f = (FromExpr *) jtnode;
    1398             :         ListCell   *l;
    1399             : 
    1400     1119172 :         foreach(l, f->fromlist)
    1401      581730 :             preprocess_qual_conditions(root, lfirst(l));
    1402             : 
    1403      537442 :         f->quals = preprocess_expression(root, f->quals, EXPRKIND_QUAL);
    1404             :     }
    1405      100540 :     else if (IsA(jtnode, JoinExpr))
    1406             :     {
    1407      100540 :         JoinExpr   *j = (JoinExpr *) jtnode;
    1408             : 
    1409      100540 :         preprocess_qual_conditions(root, j->larg);
    1410      100540 :         preprocess_qual_conditions(root, j->rarg);
    1411             : 
    1412      100540 :         j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
    1413             :     }
    1414             :     else
    1415           0 :         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             :  */
    1430             : Expr *
    1431          90 : preprocess_phv_expression(PlannerInfo *root, Expr *expr)
    1432             : {
    1433          90 :     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             :  */
    1463             : static void
    1464      522396 : grouping_planner(PlannerInfo *root, double tuple_fraction,
    1465             :                  SetOperationStmt *setops)
    1466             : {
    1467      522396 :     Query      *parse = root->parse;
    1468      522396 :     int64       offset_est = 0;
    1469      522396 :     int64       count_est = 0;
    1470      522396 :     double      limit_tuples = -1.0;
    1471      522396 :     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      522396 :     if (parse->limitCount || parse->limitOffset)
    1483             :     {
    1484        5022 :         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        5022 :         if (count_est > 0 && offset_est >= 0)
    1492        4472 :             limit_tuples = (double) count_est + (double) offset_est;
    1493             :     }
    1494             : 
    1495             :     /* Make tuple_fraction accessible to lower-level routines */
    1496      522396 :     root->tuple_fraction = tuple_fraction;
    1497             : 
    1498      522396 :     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        6102 :         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        6096 :         root->processed_tlist =
    1519        6096 :             postprocess_setop_tlist(copyObject(root->processed_tlist),
    1520             :                                     parse->targetList);
    1521             : 
    1522             :         /* Also extract the PathTarget form of the setop result tlist */
    1523        6096 :         final_target = current_rel->cheapest_total_path->pathtarget;
    1524             : 
    1525             :         /* And check whether it's parallel safe */
    1526             :         final_target_parallel_safe =
    1527        6096 :             is_parallel_safe(root, (Node *) final_target->exprs);
    1528             : 
    1529             :         /* The setop result tlist couldn't contain any SRFs */
    1530             :         Assert(!parse->hasTargetSRFs);
    1531        6096 :         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        6096 :         if (parse->rowMarks)
    1538           0 :             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",
    1543             :                             LCS_asString(linitial_node(RowMarkClause,
    1544             :                                                        parse->rowMarks)->strength))));
    1545             : 
    1546             :         /*
    1547             :          * Calculate pathkeys that represent result ordering requirements
    1548             :          */
    1549             :         Assert(parse->distinctClause == NIL);
    1550        6096 :         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      516294 :         WindowFuncLists *wflists = NULL;
    1572      516294 :         List       *activeWindows = NIL;
    1573      516294 :         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      516294 :         if (parse->groupingSets)
    1581             :         {
    1582         938 :             gset_data = preprocess_grouping_sets(root);
    1583             :         }
    1584      515356 :         else if (parse->groupClause)
    1585             :         {
    1586             :             /* Preprocess regular GROUP BY clause, if any */
    1587        3910 :             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             :          */
    1597      516288 :         preprocess_targetlist(root);
    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      516288 :         if (parse->hasAggs)
    1607             :         {
    1608       38592 :             preprocess_aggrefs(root, (Node *) root->processed_tlist);
    1609       38592 :             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      516288 :         if (parse->hasWindowFuncs)
    1619             :         {
    1620        2570 :             wflists = find_window_functions((Node *) root->processed_tlist,
    1621        2570 :                                             list_length(parse->windowClause));
    1622        2570 :             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        2564 :                 optimize_window_clauses(root, wflists);
    1630             : 
    1631             :                 /* Extract the list of windows actually in use. */
    1632        2564 :                 activeWindows = select_active_windows(root, wflists);
    1633             : 
    1634             :                 /* Make sure they all have names, for EXPLAIN's use. */
    1635        2564 :                 name_active_windows(activeWindows);
    1636             :             }
    1637             :             else
    1638           6 :                 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      516288 :         if (parse->hasAggs)
    1648       38592 :             preprocess_minmax_aggregates(root);
    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      516288 :         if (parse->groupClause ||
    1657      511512 :             parse->groupingSets ||
    1658      511446 :             parse->distinctClause ||
    1659      508524 :             parse->hasAggs ||
    1660      474144 :             parse->hasWindowFuncs ||
    1661      471724 :             parse->hasTargetSRFs ||
    1662      460494 :             root->hasHavingQual)
    1663       55818 :             root->limit_tuples = -1.0;
    1664             :         else
    1665      460470 :             root->limit_tuples = limit_tuples;
    1666             : 
    1667             :         /* Set up data needed by standard_qp_callback */
    1668      516288 :         qp_extra.activeWindows = activeWindows;
    1669      516288 :         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      516288 :         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      516288 :         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      516234 :         final_target = create_pathtarget(root, root->processed_tlist);
    1696             :         final_target_parallel_safe =
    1697      516234 :             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      516234 :         if (parse->sortClause)
    1705             :         {
    1706       70148 :             sort_input_target = make_sort_input_target(root,
    1707             :                                                        final_target,
    1708             :                                                        &have_postponed_srfs);
    1709             :             sort_input_target_parallel_safe =
    1710       70148 :                 is_parallel_safe(root, (Node *) sort_input_target->exprs);
    1711             :         }
    1712             :         else
    1713             :         {
    1714      446086 :             sort_input_target = final_target;
    1715      446086 :             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      516234 :         if (activeWindows)
    1724             :         {
    1725        2564 :             grouping_target = make_window_input_target(root,
    1726             :                                                        final_target,
    1727             :                                                        activeWindows);
    1728             :             grouping_target_parallel_safe =
    1729        2564 :                 is_parallel_safe(root, (Node *) grouping_target->exprs);
    1730             :         }
    1731             :         else
    1732             :         {
    1733      513670 :             grouping_target = sort_input_target;
    1734      513670 :             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      511458 :         have_grouping = (parse->groupClause || parse->groupingSets ||
    1743     1027692 :                          parse->hasAggs || root->hasHavingQual);
    1744      516234 :         if (have_grouping)
    1745             :         {
    1746       39284 :             scanjoin_target = make_group_input_target(root, final_target);
    1747             :             scanjoin_target_parallel_safe =
    1748       39284 :                 is_parallel_safe(root, (Node *) scanjoin_target->exprs);
    1749             :         }
    1750             :         else
    1751             :         {
    1752      476950 :             scanjoin_target = grouping_target;
    1753      476950 :             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      516234 :         if (parse->hasTargetSRFs)
    1763             :         {
    1764             :             /* final_target doesn't recompute any SRFs in sort_input_target */
    1765       11704 :             split_pathtarget_at_srfs(root, final_target, sort_input_target,
    1766             :                                      &final_targets,
    1767             :                                      &final_targets_contain_srfs);
    1768       11704 :             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       11704 :             split_pathtarget_at_srfs(root, sort_input_target, grouping_target,
    1772             :                                      &sort_input_targets,
    1773             :                                      &sort_input_targets_contain_srfs);
    1774       11704 :             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       11704 :             split_pathtarget_at_srfs(root, grouping_target, scanjoin_target,
    1778             :                                      &grouping_targets,
    1779             :                                      &grouping_targets_contain_srfs);
    1780       11704 :             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       11704 :             split_pathtarget_at_srfs(root, scanjoin_target, NULL,
    1784             :                                      &scanjoin_targets,
    1785             :                                      &scanjoin_targets_contain_srfs);
    1786       11704 :             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      504530 :             final_targets = final_targets_contain_srfs = NIL;
    1793      504530 :             sort_input_targets = sort_input_targets_contain_srfs = NIL;
    1794      504530 :             grouping_targets = grouping_targets_contain_srfs = NIL;
    1795      504530 :             scanjoin_targets = list_make1(scanjoin_target);
    1796      504530 :             scanjoin_targets_contain_srfs = NIL;
    1797             :         }
    1798             : 
    1799             :         /* Apply scan/join target. */
    1800      516234 :         scanjoin_target_same_exprs = list_length(scanjoin_targets) == 1
    1801      516234 :             && equal(scanjoin_target->exprs, current_rel->reltarget->exprs);
    1802      516234 :         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      516234 :         root->upper_targets[UPPERREL_FINAL] = final_target;
    1815      516234 :         root->upper_targets[UPPERREL_ORDERED] = final_target;
    1816      516234 :         root->upper_targets[UPPERREL_DISTINCT] = sort_input_target;
    1817      516234 :         root->upper_targets[UPPERREL_PARTIAL_DISTINCT] = sort_input_target;
    1818      516234 :         root->upper_targets[UPPERREL_WINDOW] = sort_input_target;
    1819      516234 :         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      516234 :         if (have_grouping)
    1827             :         {
    1828       39284 :             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       39278 :             if (parse->hasTargetSRFs)
    1835         432 :                 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      516228 :         if (activeWindows)
    1845             :         {
    1846        2564 :             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        2564 :             if (parse->hasTargetSRFs)
    1855          12 :                 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      516228 :         if (parse->distinctClause)
    1865             :         {
    1866        2956 :             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      522324 :     if (parse->sortClause)
    1880             :     {
    1881       74036 :         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       74036 :         if (parse->hasTargetSRFs)
    1889         196 :             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      522324 :     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      697954 :     if (current_rel->consider_parallel &&
    1907      351236 :         is_parallel_safe(root, parse->limitOffset) &&
    1908      175606 :         is_parallel_safe(root, parse->limitCount))
    1909      175600 :         final_rel->consider_parallel = true;
    1910             : 
    1911             :     /*
    1912             :      * If the current_rel belongs to a single FDW, so does the final_rel.
    1913             :      */
    1914      522324 :     final_rel->serverid = current_rel->serverid;
    1915      522324 :     final_rel->userid = current_rel->userid;
    1916      522324 :     final_rel->useridiscurrent = current_rel->useridiscurrent;
    1917      522324 :     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     1065534 :     foreach(lc, current_rel->pathlist)
    1924             :     {
    1925      543210 :         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      543210 :         if (parse->rowMarks)
    1935             :         {
    1936        8350 :             path = (Path *) create_lockrows_path(root, final_rel, path,
    1937             :                                                  root->rowMarks,
    1938             :                                                  assign_special_exec_param(root));
    1939             :         }
    1940             : 
    1941             :         /*
    1942             :          * If there is a LIMIT/OFFSET clause, add the LIMIT node.
    1943             :          */
    1944      543210 :         if (limit_needed(parse))
    1945             :         {
    1946        5950 :             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      543210 :         if (parse->commandType != CMD_SELECT)
    1957             :         {
    1958             :             Index       rootRelation;
    1959       88842 :             List       *resultRelations = NIL;
    1960       88842 :             List       *updateColnosLists = NIL;
    1961       88842 :             List       *withCheckOptionLists = NIL;
    1962       88842 :             List       *returningLists = NIL;
    1963       88842 :             List       *mergeActionLists = NIL;
    1964       88842 :             List       *mergeJoinConditions = NIL;
    1965             :             List       *rowMarks;
    1966             : 
    1967       88842 :             if (bms_membership(root->all_result_relids) == BMS_MULTIPLE)
    1968             :             {
    1969             :                 /* Inherited UPDATE/DELETE/MERGE */
    1970        2868 :                 RelOptInfo *top_result_rel = find_base_rel(root,
    1971             :                                                            parse->resultRelation);
    1972        2868 :                 int         resultRelation = -1;
    1973             : 
    1974             :                 /* Pass the root result rel forward to the executor. */
    1975        2868 :                 rootRelation = parse->resultRelation;
    1976             : 
    1977             :                 /* Add only leaf children to ModifyTable. */
    1978        8374 :                 while ((resultRelation = bms_next_member(root->leaf_result_relids,
    1979        8374 :                                                          resultRelation)) >= 0)
    1980             :                 {
    1981        5506 :                     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        5506 :                     if (IS_DUMMY_REL(this_result_rel))
    1990         174 :                         continue;
    1991             : 
    1992             :                     /* Build per-target-rel lists needed by ModifyTable */
    1993        5332 :                     resultRelations = lappend_int(resultRelations,
    1994             :                                                   resultRelation);
    1995        5332 :                     if (parse->commandType == CMD_UPDATE)
    1996             :                     {
    1997        3660 :                         List       *update_colnos = root->update_colnos;
    1998             : 
    1999        3660 :                         if (this_result_rel != top_result_rel)
    2000             :                             update_colnos =
    2001        3660 :                                 adjust_inherited_attnums_multilevel(root,
    2002             :                                                                     update_colnos,
    2003             :                                                                     this_result_rel->relid,
    2004             :                                                                     top_result_rel->relid);
    2005        3660 :                         updateColnosLists = lappend(updateColnosLists,
    2006             :                                                     update_colnos);
    2007             :                     }
    2008        5332 :                     if (parse->withCheckOptions)
    2009             :                     {
    2010         504 :                         List       *withCheckOptions = parse->withCheckOptions;
    2011             : 
    2012         504 :                         if (this_result_rel != top_result_rel)
    2013             :                             withCheckOptions = (List *)
    2014         504 :                                 adjust_appendrel_attrs_multilevel(root,
    2015             :                                                                   (Node *) withCheckOptions,
    2016             :                                                                   this_result_rel,
    2017             :                                                                   top_result_rel);
    2018         504 :                         withCheckOptionLists = lappend(withCheckOptionLists,
    2019             :                                                        withCheckOptions);
    2020             :                     }
    2021        5332 :                     if (parse->returningList)
    2022             :                     {
    2023         840 :                         List       *returningList = parse->returningList;
    2024             : 
    2025         840 :                         if (this_result_rel != top_result_rel)
    2026             :                             returningList = (List *)
    2027         840 :                                 adjust_appendrel_attrs_multilevel(root,
    2028             :                                                                   (Node *) returningList,
    2029             :                                                                   this_result_rel,
    2030             :                                                                   top_result_rel);
    2031         840 :                         returningLists = lappend(returningLists,
    2032             :                                                  returningList);
    2033             :                     }
    2034        5332 :                     if (parse->mergeActionList)
    2035             :                     {
    2036             :                         ListCell   *l;
    2037         534 :                         List       *mergeActionList = NIL;
    2038             : 
    2039             :                         /*
    2040             :                          * Copy MergeActions and translate stuff that
    2041             :                          * references attribute numbers.
    2042             :                          */
    2043        1662 :                         foreach(l, parse->mergeActionList)
    2044             :                         {
    2045        1128 :                             MergeAction *action = lfirst(l),
    2046        1128 :                                        *leaf_action = copyObject(action);
    2047             : 
    2048        1128 :                             leaf_action->qual =
    2049        1128 :                                 adjust_appendrel_attrs_multilevel(root,
    2050             :                                                                   (Node *) action->qual,
    2051             :                                                                   this_result_rel,
    2052             :                                                                   top_result_rel);
    2053        1128 :                             leaf_action->targetList = (List *)
    2054        1128 :                                 adjust_appendrel_attrs_multilevel(root,
    2055        1128 :                                                                   (Node *) action->targetList,
    2056             :                                                                   this_result_rel,
    2057             :                                                                   top_result_rel);
    2058        1128 :                             if (leaf_action->commandType == CMD_UPDATE)
    2059         628 :                                 leaf_action->updateColnos =
    2060         628 :                                     adjust_inherited_attnums_multilevel(root,
    2061             :                                                                         action->updateColnos,
    2062             :                                                                         this_result_rel->relid,
    2063             :                                                                         top_result_rel->relid);
    2064        1128 :                             mergeActionList = lappend(mergeActionList,
    2065             :                                                       leaf_action);
    2066             :                         }
    2067             : 
    2068         534 :                         mergeActionLists = lappend(mergeActionLists,
    2069             :                                                    mergeActionList);
    2070             :                     }
    2071        5332 :                     if (parse->commandType == CMD_MERGE)
    2072             :                     {
    2073         534 :                         Node       *mergeJoinCondition = parse->mergeJoinCondition;
    2074             : 
    2075         534 :                         if (this_result_rel != top_result_rel)
    2076             :                             mergeJoinCondition =
    2077         534 :                                 adjust_appendrel_attrs_multilevel(root,
    2078             :                                                                   mergeJoinCondition,
    2079             :                                                                   this_result_rel,
    2080             :                                                                   top_result_rel);
    2081         534 :                         mergeJoinConditions = lappend(mergeJoinConditions,
    2082             :                                                       mergeJoinCondition);
    2083             :                     }
    2084             :                 }
    2085             : 
    2086        2868 :                 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          30 :                     resultRelations = list_make1_int(parse->resultRelation);
    2100          30 :                     if (parse->commandType == CMD_UPDATE)
    2101          30 :                         updateColnosLists = list_make1(root->update_colnos);
    2102          30 :                     if (parse->withCheckOptions)
    2103           0 :                         withCheckOptionLists = list_make1(parse->withCheckOptions);
    2104          30 :                     if (parse->returningList)
    2105          18 :                         returningLists = list_make1(parse->returningList);
    2106          30 :                     if (parse->mergeActionList)
    2107           0 :                         mergeActionLists = list_make1(parse->mergeActionList);
    2108          30 :                     if (parse->commandType == CMD_MERGE)
    2109           0 :                         mergeJoinConditions = list_make1(parse->mergeJoinCondition);
    2110             :                 }
    2111             :             }
    2112             :             else
    2113             :             {
    2114             :                 /* Single-relation INSERT/UPDATE/DELETE/MERGE. */
    2115       85974 :                 rootRelation = 0;   /* there's no separate root rel */
    2116       85974 :                 resultRelations = list_make1_int(parse->resultRelation);
    2117       85974 :                 if (parse->commandType == CMD_UPDATE)
    2118       12092 :                     updateColnosLists = list_make1(root->update_colnos);
    2119       85974 :                 if (parse->withCheckOptions)
    2120         926 :                     withCheckOptionLists = list_make1(parse->withCheckOptions);
    2121       85974 :                 if (parse->returningList)
    2122        2438 :                     returningLists = list_make1(parse->returningList);
    2123       85974 :                 if (parse->mergeActionList)
    2124        1542 :                     mergeActionLists = list_make1(parse->mergeActionList);
    2125       85974 :                 if (parse->commandType == CMD_MERGE)
    2126        1542 :                     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       88842 :             if (parse->rowMarks)
    2135           0 :                 rowMarks = NIL;
    2136             :             else
    2137       88842 :                 rowMarks = root->rowMarks;
    2138             : 
    2139             :             path = (Path *)
    2140       88842 :                 create_modifytable_path(root, final_rel,
    2141             :                                         path,
    2142             :                                         parse->commandType,
    2143       88842 :                                         parse->canSetTag,
    2144       88842 :                                         parse->resultRelation,
    2145             :                                         rootRelation,
    2146             :                                         resultRelations,
    2147             :                                         updateColnosLists,
    2148             :                                         withCheckOptionLists,
    2149             :                                         returningLists,
    2150             :                                         rowMarks,
    2151             :                                         parse->onConflict,
    2152             :                                         mergeActionLists,
    2153             :                                         mergeJoinConditions,
    2154             :                                         assign_special_exec_param(root));
    2155             :         }
    2156             : 
    2157             :         /* And shove it into final_rel */
    2158      543210 :         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      522324 :     if (final_rel->consider_parallel && root->query_level > 1 &&
    2166       30108 :         !limit_needed(parse))
    2167             :     {
    2168             :         Assert(!parse->rowMarks && parse->commandType == CMD_SELECT);
    2169       30030 :         foreach(lc, current_rel->partial_pathlist)
    2170             :         {
    2171         108 :             Path       *partial_path = (Path *) lfirst(lc);
    2172             : 
    2173         108 :             add_partial_path(final_rel, partial_path);
    2174             :         }
    2175             :     }
    2176             : 
    2177      522324 :     extra.limit_needed = limit_needed(parse);
    2178      522324 :     extra.limit_tuples = limit_tuples;
    2179      522324 :     extra.count_est = count_est;
    2180      522324 :     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      522324 :     if (final_rel->fdwroutine &&
    2187        1260 :         final_rel->fdwroutine->GetForeignUpperPaths)
    2188        1192 :         final_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_FINAL,
    2189             :                                                     current_rel, final_rel,
    2190             :                                                     &extra);
    2191             : 
    2192             :     /* Let extensions possibly add some more paths */
    2193      522324 :     if (create_upper_paths_hook)
    2194           0 :         (*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      522324 : }
    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             :  */
    2209             : static grouping_sets_data *
    2210         938 : preprocess_grouping_sets(PlannerInfo *root)
    2211             : {
    2212         938 :     Query      *parse = root->parse;
    2213             :     List       *sets;
    2214         938 :     int         maxref = 0;
    2215             :     ListCell   *lc_set;
    2216         938 :     grouping_sets_data *gd = palloc0(sizeof(grouping_sets_data));
    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         938 :     root->processed_groupClause = parse->groupClause;
    2223             : 
    2224             :     /* Detect unhashable and unsortable grouping expressions */
    2225         938 :     gd->any_hashable = false;
    2226         938 :     gd->unhashable_refs = NULL;
    2227         938 :     gd->unsortable_refs = NULL;
    2228         938 :     gd->unsortable_sets = NIL;
    2229             : 
    2230         938 :     if (parse->groupClause)
    2231             :     {
    2232             :         ListCell   *lc;
    2233             : 
    2234        2780 :         foreach(lc, parse->groupClause)
    2235             :         {
    2236        1908 :             SortGroupClause *gc = lfirst_node(SortGroupClause, lc);
    2237        1908 :             Index       ref = gc->tleSortGroupRef;
    2238             : 
    2239        1908 :             if (ref > maxref)
    2240        1872 :                 maxref = ref;
    2241             : 
    2242        1908 :             if (!gc->hashable)
    2243          30 :                 gd->unhashable_refs = bms_add_member(gd->unhashable_refs, ref);
    2244             : 
    2245        1908 :             if (!OidIsValid(gc->sortop))
    2246          42 :                 gd->unsortable_refs = bms_add_member(gd->unsortable_refs, ref);
    2247             :         }
    2248             :     }
    2249             : 
    2250             :     /* Allocate workspace array for remapping */
    2251         938 :     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         938 :     if (!bms_is_empty(gd->unsortable_refs))
    2260             :     {
    2261          42 :         List       *sortable_sets = NIL;
    2262             :         ListCell   *lc;
    2263             : 
    2264         126 :         foreach(lc, parse->groupingSets)
    2265             :         {
    2266          90 :             List       *gset = (List *) lfirst(lc);
    2267             : 
    2268          90 :             if (bms_overlap_list(gd->unsortable_refs, gset))
    2269             :             {
    2270          48 :                 GroupingSetData *gs = makeNode(GroupingSetData);
    2271             : 
    2272          48 :                 gs->set = gset;
    2273          48 :                 gd->unsortable_sets = lappend(gd->unsortable_sets, gs);
    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          48 :                 if (bms_overlap_list(gd->unhashable_refs, gset))
    2284           6 :                     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          42 :                 sortable_sets = lappend(sortable_sets, gset);
    2291             :         }
    2292             : 
    2293          36 :         if (sortable_sets)
    2294          30 :             sets = extract_rollup_sets(sortable_sets);
    2295             :         else
    2296           6 :             sets = NIL;
    2297             :     }
    2298             :     else
    2299         896 :         sets = extract_rollup_sets(parse->groupingSets);
    2300             : 
    2301        2442 :     foreach(lc_set, sets)
    2302             :     {
    2303        1510 :         List       *current_sets = (List *) lfirst(lc_set);
    2304        1510 :         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        1510 :         current_sets = reorder_grouping_sets(current_sets,
    2318        1510 :                                              (list_length(sets) == 1
    2319             :                                               ? parse->sortClause
    2320             :                                               : NIL));
    2321             : 
    2322             :         /*
    2323             :          * Get the initial (and therefore largest) grouping set.
    2324             :          */
    2325        1510 :         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        1510 :         if (gs->set)
    2337        1444 :             rollup->groupClause = preprocess_groupclause(root, gs->set);
    2338             :         else
    2339          66 :             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        1510 :         if (gs->set &&
    2348        1444 :             !bms_overlap_list(gd->unhashable_refs, gs->set))
    2349             :         {
    2350        1420 :             rollup->hashable = true;
    2351        1420 :             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        1510 :         rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
    2362             :                                                  current_sets,
    2363             :                                                  gd->tleref_to_colnum_map);
    2364        1510 :         rollup->gsets_data = current_sets;
    2365             : 
    2366        1510 :         gd->rollups = lappend(gd->rollups, rollup);
    2367             :     }
    2368             : 
    2369         932 :     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          36 :         gd->hash_sets_idx = remap_to_groupclause_idx(parse->groupClause,
    2377             :                                                      gd->unsortable_sets,
    2378             :                                                      gd->tleref_to_colnum_map);
    2379          36 :         gd->any_hashable = true;
    2380             :     }
    2381             : 
    2382         932 :     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             :  */
    2389             : static List *
    2390        4344 : remap_to_groupclause_idx(List *groupClause,
    2391             :                          List *gsets,
    2392             :                          int *tleref_to_colnum_map)
    2393             : {
    2394        4344 :     int         ref = 0;
    2395        4344 :     List       *result = NIL;
    2396             :     ListCell   *lc;
    2397             : 
    2398       10552 :     foreach(lc, groupClause)
    2399             :     {
    2400        6208 :         SortGroupClause *gc = lfirst_node(SortGroupClause, lc);
    2401             : 
    2402        6208 :         tleref_to_colnum_map[gc->tleSortGroupRef] = ref++;
    2403             :     }
    2404             : 
    2405       10014 :     foreach(lc, gsets)
    2406             :     {
    2407        5670 :         List       *set = NIL;
    2408             :         ListCell   *lc2;
    2409        5670 :         GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
    2410             : 
    2411       12728 :         foreach(lc2, gs->set)
    2412             :         {
    2413        7058 :             set = lappend_int(set, tleref_to_colnum_map[lfirst_int(lc2)]);
    2414             :         }
    2415             : 
    2416        5670 :         result = lappend(result, set);
    2417             :     }
    2418             : 
    2419        4344 :     return result;
    2420             : }
    2421             : 
    2422             : 
    2423             : /*
    2424             :  * preprocess_rowmarks - set up PlanRowMarks if needed
    2425             :  */
    2426             : static void
    2427      526346 : preprocess_rowmarks(PlannerInfo *root)
    2428             : {
    2429      526346 :     Query      *parse = root->parse;
    2430             :     Bitmapset  *rels;
    2431             :     List       *prowmarks;
    2432             :     ListCell   *l;
    2433             :     int         i;
    2434             : 
    2435      526346 :     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             :          */
    2443        7862 :         CheckSelectLocking(parse, linitial_node(RowMarkClause,
    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      518484 :         if (parse->commandType != CMD_UPDATE &&
    2453      504386 :             parse->commandType != CMD_DELETE &&
    2454      500038 :             parse->commandType != CMD_MERGE)
    2455      498250 :             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       28096 :     rels = get_relids_in_jointree((Node *) parse->jointree, false, false);
    2464       28096 :     if (parse->resultRelation)
    2465       20234 :         rels = bms_del_member(rels, parse->resultRelation);
    2466             : 
    2467             :     /*
    2468             :      * Convert RowMarkClauses to PlanRowMark representation.
    2469             :      */
    2470       28096 :     prowmarks = NIL;
    2471       36188 :     foreach(l, parse->rowMarks)
    2472             :     {
    2473        8092 :         RowMarkClause *rc = lfirst_node(RowMarkClause, l);
    2474        8092 :         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        8092 :         if (rte->rtekind != RTE_RELATION)
    2491          60 :             continue;
    2492             : 
    2493        8032 :         rels = bms_del_member(rels, rc->rti);
    2494             : 
    2495        8032 :         newrc = makeNode(PlanRowMark);
    2496        8032 :         newrc->rti = newrc->prti = rc->rti;
    2497        8032 :         newrc->rowmarkId = ++(root->glob->lastRowMarkId);
    2498        8032 :         newrc->markType = select_rowmark_type(rte, rc->strength);
    2499        8032 :         newrc->allMarkTypes = (1 << newrc->markType);
    2500        8032 :         newrc->strength = rc->strength;
    2501        8032 :         newrc->waitPolicy = rc->waitPolicy;
    2502        8032 :         newrc->isParent = false;
    2503             : 
    2504        8032 :         prowmarks = lappend(prowmarks, newrc);
    2505             :     }
    2506             : 
    2507             :     /*
    2508             :      * Now, add rowmarks for any non-target, non-locked base relations.
    2509             :      */
    2510       28096 :     i = 0;
    2511       66870 :     foreach(l, parse->rtable)
    2512             :     {
    2513       38774 :         RangeTblEntry *rte = lfirst_node(RangeTblEntry, l);
    2514             :         PlanRowMark *newrc;
    2515             : 
    2516       38774 :         i++;
    2517       38774 :         if (!bms_is_member(i, rels))
    2518       35096 :             continue;
    2519             : 
    2520        3678 :         newrc = makeNode(PlanRowMark);
    2521        3678 :         newrc->rti = newrc->prti = i;
    2522        3678 :         newrc->rowmarkId = ++(root->glob->lastRowMarkId);
    2523        3678 :         newrc->markType = select_rowmark_type(rte, LCS_NONE);
    2524        3678 :         newrc->allMarkTypes = (1 << newrc->markType);
    2525        3678 :         newrc->strength = LCS_NONE;
    2526        3678 :         newrc->waitPolicy = LockWaitBlock;   /* doesn't matter */
    2527        3678 :         newrc->isParent = false;
    2528             : 
    2529        3678 :         prowmarks = lappend(prowmarks, newrc);
    2530             :     }
    2531             : 
    2532       28096 :     root->rowMarks = prowmarks;
    2533             : }
    2534             : 
    2535             : /*
    2536             :  * Select RowMarkType to use for a given table
    2537             :  */
    2538             : RowMarkType
    2539       14042 : select_rowmark_type(RangeTblEntry *rte, LockClauseStrength strength)
    2540             : {
    2541       14042 :     if (rte->rtekind != RTE_RELATION)
    2542             :     {
    2543             :         /* If it's not a table at all, use ROW_MARK_COPY */
    2544        1404 :         return ROW_MARK_COPY;
    2545             :     }
    2546       12638 :     else if (rte->relkind == RELKIND_FOREIGN_TABLE)
    2547             :     {
    2548             :         /* Let the FDW select the rowmark type, if it wants to */
    2549         212 :         FdwRoutine *fdwroutine = GetFdwRoutineByRelId(rte->relid);
    2550             : 
    2551         212 :         if (fdwroutine->GetForeignRowMarkType != NULL)
    2552           0 :             return fdwroutine->GetForeignRowMarkType(rte, strength);
    2553             :         /* Otherwise, use ROW_MARK_COPY by default */
    2554         212 :         return ROW_MARK_COPY;
    2555             :     }
    2556             :     else
    2557             :     {
    2558             :         /* Regular table, apply the appropriate lock type */
    2559       12426 :         switch (strength)
    2560             :         {
    2561        2504 :             case LCS_NONE:
    2562             : 
    2563             :                 /*
    2564             :                  * We don't need a tuple lock, only the ability to re-fetch
    2565             :                  * the row.
    2566             :                  */
    2567        2504 :                 return ROW_MARK_REFERENCE;
    2568             :                 break;
    2569        8038 :             case LCS_FORKEYSHARE:
    2570        8038 :                 return ROW_MARK_KEYSHARE;
    2571             :                 break;
    2572         300 :             case LCS_FORSHARE:
    2573         300 :                 return ROW_MARK_SHARE;
    2574             :                 break;
    2575          72 :             case LCS_FORNOKEYUPDATE:
    2576          72 :                 return ROW_MARK_NOKEYEXCLUSIVE;
    2577             :                 break;
    2578        1512 :             case LCS_FORUPDATE:
    2579        1512 :                 return ROW_MARK_EXCLUSIVE;
    2580             :                 break;
    2581             :         }
    2582           0 :         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             :  */
    2604             : static double
    2605        5022 : preprocess_limit(PlannerInfo *root, double tuple_fraction,
    2606             :                  int64 *offset_est, int64 *count_est)
    2607             : {
    2608        5022 :     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        5022 :     if (parse->limitCount)
    2620             :     {
    2621        4496 :         est = estimate_expression_value(root, parse->limitCount);
    2622        4496 :         if (est && IsA(est, Const))
    2623             :         {
    2624        4490 :             if (((Const *) est)->constisnull)
    2625             :             {
    2626             :                 /* NULL indicates LIMIT ALL, ie, no limit */
    2627           0 :                 *count_est = 0; /* treat as not present */
    2628             :             }
    2629             :             else
    2630             :             {
    2631        4490 :                 *count_est = DatumGetInt64(((Const *) est)->constvalue);
    2632        4490 :                 if (*count_est <= 0)
    2633         150 :                     *count_est = 1; /* force to at least 1 */
    2634             :             }
    2635             :         }
    2636             :         else
    2637           6 :             *count_est = -1;    /* can't estimate */
    2638             :     }
    2639             :     else
    2640         526 :         *count_est = 0;         /* not present */
    2641             : 
    2642        5022 :     if (parse->limitOffset)
    2643             :     {
    2644         898 :         est = estimate_expression_value(root, parse->limitOffset);
    2645         898 :         if (est && IsA(est, Const))
    2646             :         {
    2647         874 :             if (((Const *) est)->constisnull)
    2648             :             {
    2649             :                 /* Treat NULL as no offset; the executor will too */
    2650           0 :                 *offset_est = 0;    /* treat as not present */
    2651             :             }
    2652             :             else
    2653             :             {
    2654         874 :                 *offset_est = DatumGetInt64(((Const *) est)->constvalue);
    2655         874 :                 if (*offset_est < 0)
    2656           0 :                     *offset_est = 0;    /* treat as not present */
    2657             :             }
    2658             :         }
    2659             :         else
    2660          24 :             *offset_est = -1;   /* can't estimate */
    2661             :     }
    2662             :     else
    2663        4124 :         *offset_est = 0;        /* not present */
    2664             : 
    2665        5022 :     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        4496 :         if (*count_est < 0 || *offset_est < 0)
    2673             :         {
    2674             :             /* LIMIT or OFFSET is an expression ... punt ... */
    2675          24 :             limit_fraction = 0.10;
    2676             :         }
    2677             :         else
    2678             :         {
    2679             :             /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
    2680        4472 :             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        4496 :         if (tuple_fraction >= 1.0)
    2691             :         {
    2692           6 :             if (limit_fraction >= 1.0)
    2693             :             {
    2694             :                 /* both absolute */
    2695           6 :                 tuple_fraction = Min(tuple_fraction, limit_fraction);
    2696             :             }
    2697             :             else
    2698             :             {
    2699             :                 /* caller absolute, limit fractional; use caller's value */
    2700             :             }
    2701             :         }
    2702        4490 :         else if (tuple_fraction > 0.0)
    2703             :         {
    2704         148 :             if (limit_fraction >= 1.0)
    2705             :             {
    2706             :                 /* caller fractional, limit absolute; use limit */
    2707         148 :                 tuple_fraction = limit_fraction;
    2708             :             }
    2709             :             else
    2710             :             {
    2711             :                 /* both fractional */
    2712           0 :                 tuple_fraction = Min(tuple_fraction, limit_fraction);
    2713             :             }
    2714             :         }
    2715             :         else
    2716             :         {
    2717             :             /* no info from caller, just use limit */
    2718        4342 :             tuple_fraction = limit_fraction;
    2719             :         }
    2720             :     }
    2721         526 :     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          14 :         if (*offset_est < 0)
    2733           0 :             limit_fraction = 0.10;
    2734             :         else
    2735          14 :             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          14 :         if (tuple_fraction >= 1.0)
    2744             :         {
    2745           0 :             if (limit_fraction >= 1.0)
    2746             :             {
    2747             :                 /* both absolute, so add them together */
    2748           0 :                 tuple_fraction += limit_fraction;
    2749             :             }
    2750             :             else
    2751             :             {
    2752             :                 /* caller absolute, limit fractional; use limit */
    2753           0 :                 tuple_fraction = limit_fraction;
    2754             :             }
    2755             :         }
    2756             :         else
    2757             :         {
    2758          14 :             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           0 :                 tuple_fraction += limit_fraction;
    2766           0 :                 if (tuple_fraction >= 1.0)
    2767           0 :                     tuple_fraction = 0.0;   /* assume fetch all */
    2768             :             }
    2769             :         }
    2770             :     }
    2771             : 
    2772        5022 :     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             :  */
    2789             : bool
    2790     1114742 : limit_needed(Query *parse)
    2791             : {
    2792             :     Node       *node;
    2793             : 
    2794     1114742 :     node = parse->limitCount;
    2795     1114742 :     if (node)
    2796             :     {
    2797       10764 :         if (IsA(node, Const))
    2798             :         {
    2799             :             /* NULL indicates LIMIT ALL, ie, no limit */
    2800       10528 :             if (!((Const *) node)->constisnull)
    2801       10528 :                 return true;    /* LIMIT with a constant value */
    2802             :         }
    2803             :         else
    2804         236 :             return true;        /* non-constant LIMIT */
    2805             :     }
    2806             : 
    2807     1103978 :     node = parse->limitOffset;
    2808     1103978 :     if (node)
    2809             :     {
    2810        1530 :         if (IsA(node, Const))
    2811             :         {
    2812             :             /* Treat NULL as no offset; the executor would too */
    2813        1222 :             if (!((Const *) node)->constisnull)
    2814             :             {
    2815        1222 :                 int64       offset = DatumGetInt64(((Const *) node)->constvalue);
    2816             : 
    2817        1222 :                 if (offset != 0)
    2818         142 :                     return true;    /* OFFSET with a nonzero value */
    2819             :             }
    2820             :         }
    2821             :         else
    2822         308 :             return true;        /* non-constant OFFSET */
    2823             :     }
    2824             : 
    2825     1103528 :     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             :  */
    2855             : static List *
    2856        8152 : preprocess_groupclause(PlannerInfo *root, List *force)
    2857             : {
    2858        8152 :     Query      *parse = root->parse;
    2859        8152 :     List       *new_groupclause = NIL;
    2860             :     ListCell   *sl;
    2861             :     ListCell   *gl;
    2862             : 
    2863             :     /* For grouping sets, we need to force the ordering */
    2864        8152 :     if (force)
    2865             :     {
    2866       10372 :         foreach(sl, force)
    2867             :         {
    2868        6130 :             Index       ref = lfirst_int(sl);
    2869        6130 :             SortGroupClause *cl = get_sortgroupref_clause(ref, parse->groupClause);
    2870             : 
    2871        6130 :             new_groupclause = lappend(new_groupclause, cl);
    2872             :         }
    2873             : 
    2874        4242 :         return new_groupclause;
    2875             :     }
    2876             : 
    2877             :     /* If no ORDER BY, nothing useful to do here */
    2878        3910 :     if (parse->sortClause == NIL)
    2879        2106 :         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        3514 :     foreach(sl, parse->sortClause)
    2888             :     {
    2889        2356 :         SortGroupClause *sc = lfirst_node(SortGroupClause, sl);
    2890             : 
    2891        3452 :         foreach(gl, parse->groupClause)
    2892             :         {
    2893        2806 :             SortGroupClause *gc = lfirst_node(SortGroupClause, gl);
    2894             : 
    2895        2806 :             if (equal(gc, sc))
    2896             :             {
    2897        1710 :                 new_groupclause = lappend(new_groupclause, gc);
    2898        1710 :                 break;
    2899             :             }
    2900             :         }
    2901        2356 :         if (gl == NULL)
    2902         646 :             break;              /* no match, so stop scanning */
    2903             :     }
    2904             : 
    2905             : 
    2906             :     /* If no match at all, no point in reordering GROUP BY */
    2907        1804 :     if (new_groupclause == NIL)
    2908         298 :         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        3382 :     foreach(gl, parse->groupClause)
    2917             :     {
    2918        1876 :         SortGroupClause *gc = lfirst_node(SortGroupClause, gl);
    2919             : 
    2920        1876 :         if (list_member_ptr(new_groupclause, gc))
    2921        1710 :             continue;           /* it matched an ORDER BY item */
    2922         166 :         if (!OidIsValid(gc->sortop)) /* give up, GROUP BY can't be sorted */
    2923           0 :             return list_copy(parse->groupClause);
    2924         166 :         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        1506 :     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             :  */
    2951             : static List *
    2952         926 : extract_rollup_sets(List *groupingSets)
    2953             : {
    2954         926 :     int         num_sets_raw = list_length(groupingSets);
    2955         926 :     int         num_empty = 0;
    2956         926 :     int         num_sets = 0;   /* distinct sets */
    2957         926 :     int         num_chains = 0;
    2958         926 :     List       *result = NIL;
    2959             :     List      **results;
    2960             :     List      **orig_sets;
    2961             :     Bitmapset **set_masks;
    2962             :     int        *chains;
    2963             :     short     **adjacency;
    2964             :     short      *adjacency_buf;
    2965             :     BipartiteMatchState *state;
    2966             :     int         i;
    2967             :     int         j;
    2968             :     int         j_size;
    2969         926 :     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        1572 :     while (lc1 && lfirst(lc1) == NIL)
    2978             :     {
    2979         646 :         ++num_empty;
    2980         646 :         lc1 = lnext(groupingSets, lc1);
    2981             :     }
    2982             : 
    2983             :     /* bail out now if it turns out that all we had were empty sets. */
    2984         926 :     if (!lc1)
    2985          66 :         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         860 :     orig_sets = palloc0((num_sets_raw + 1) * sizeof(List *));
    3006         860 :     set_masks = palloc0((num_sets_raw + 1) * sizeof(Bitmapset *));
    3007         860 :     adjacency = palloc0((num_sets_raw + 1) * sizeof(short *));
    3008         860 :     adjacency_buf = palloc((num_sets_raw + 1) * sizeof(short));
    3009             : 
    3010         860 :     j_size = 0;
    3011         860 :     j = 0;
    3012         860 :     i = 1;
    3013             : 
    3014        3044 :     for_each_cell(lc, groupingSets, lc1)
    3015             :     {
    3016        2184 :         List       *candidate = (List *) lfirst(lc);
    3017        2184 :         Bitmapset  *candidate_set = NULL;
    3018             :         ListCell   *lc2;
    3019        2184 :         int         dup_of = 0;
    3020             : 
    3021        5274 :         foreach(lc2, candidate)
    3022             :         {
    3023        3090 :             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        2184 :         if (j_size == list_length(candidate))
    3028             :         {
    3029             :             int         k;
    3030             : 
    3031        1928 :             for (k = j; k < i; ++k)
    3032             :             {
    3033        1248 :                 if (bms_equal(set_masks[k], candidate_set))
    3034             :                 {
    3035         158 :                     dup_of = k;
    3036         158 :                     break;
    3037             :                 }
    3038             :             }
    3039             :         }
    3040        1346 :         else if (j_size < list_length(candidate))
    3041             :         {
    3042        1346 :             j_size = list_length(candidate);
    3043        1346 :             j = i;
    3044             :         }
    3045             : 
    3046        2184 :         if (dup_of > 0)
    3047             :         {
    3048         158 :             orig_sets[dup_of] = lappend(orig_sets[dup_of], candidate);
    3049         158 :             bms_free(candidate_set);
    3050             :         }
    3051             :         else
    3052             :         {
    3053             :             int         k;
    3054        2026 :             int         n_adj = 0;
    3055             : 
    3056        2026 :             orig_sets[i] = list_make1(candidate);
    3057        2026 :             set_masks[i] = candidate_set;
    3058             : 
    3059             :             /* fill in adjacency list; no need to compare equal-size sets */
    3060             : 
    3061        3310 :             for (k = j - 1; k > 0; --k)
    3062             :             {
    3063        1284 :                 if (bms_is_subset(set_masks[k], candidate_set))
    3064        1122 :                     adjacency_buf[++n_adj] = k;
    3065             :             }
    3066             : 
    3067        2026 :             if (n_adj > 0)
    3068             :             {
    3069         610 :                 adjacency_buf[0] = n_adj;
    3070         610 :                 adjacency[i] = palloc((n_adj + 1) * sizeof(short));
    3071         610 :                 memcpy(adjacency[i], adjacency_buf, (n_adj + 1) * sizeof(short));
    3072             :             }
    3073             :             else
    3074        1416 :                 adjacency[i] = NULL;
    3075             : 
    3076        2026 :             ++i;
    3077             :         }
    3078             :     }
    3079             : 
    3080         860 :     num_sets = i - 1;
    3081             : 
    3082             :     /*
    3083             :      * Apply the graph matching algorithm to do the work.
    3084             :      */
    3085         860 :     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         860 :     chains = palloc0((num_sets + 1) * sizeof(int));
    3094             : 
    3095        2886 :     for (i = 1; i <= num_sets; ++i)
    3096             :     {
    3097        2026 :         int         u = state->pair_vu[i];
    3098        2026 :         int         v = state->pair_uv[i];
    3099             : 
    3100        2026 :         if (u > 0 && u < i)
    3101           0 :             chains[i] = chains[u];
    3102        2026 :         else if (v > 0 && v < i)
    3103         582 :             chains[i] = chains[v];
    3104             :         else
    3105        1444 :             chains[i] = ++num_chains;
    3106             :     }
    3107             : 
    3108             :     /* build result lists. */
    3109         860 :     results = palloc0((num_chains + 1) * sizeof(List *));
    3110             : 
    3111        2886 :     for (i = 1; i <= num_sets; ++i)
    3112             :     {
    3113        2026 :         int         c = chains[i];
    3114             : 
    3115             :         Assert(c > 0);
    3116             : 
    3117        2026 :         results[c] = list_concat(results[c], orig_sets[i]);
    3118             :     }
    3119             : 
    3120             :     /* push any empty sets back on the first list. */
    3121        1392 :     while (num_empty-- > 0)
    3122         532 :         results[1] = lcons(NIL, results[1]);
    3123             : 
    3124             :     /* make result list */
    3125        2304 :     for (i = 1; i <= num_chains; ++i)
    3126        1444 :         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             :      */
    3134         860 :     BipartiteMatchFree(state);
    3135         860 :     pfree(results);
    3136         860 :     pfree(chains);
    3137        2886 :     for (i = 1; i <= num_sets; ++i)
    3138        2026 :         if (adjacency[i])
    3139         610 :             pfree(adjacency[i]);
    3140         860 :     pfree(adjacency);
    3141         860 :     pfree(adjacency_buf);
    3142         860 :     pfree(orig_sets);
    3143        2886 :     for (i = 1; i <= num_sets; ++i)
    3144        2026 :         bms_free(set_masks[i]);
    3145         860 :     pfree(set_masks);
    3146             : 
    3147         860 :     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             :  */
    3163             : static List *
    3164        1510 : reorder_grouping_sets(List *groupingSets, List *sortclause)
    3165             : {
    3166             :     ListCell   *lc;
    3167        1510 :     List       *previous = NIL;
    3168        1510 :     List       *result = NIL;
    3169             : 
    3170        4340 :     foreach(lc, groupingSets)
    3171             :     {
    3172        2830 :         List       *candidate = (List *) lfirst(lc);
    3173        2830 :         List       *new_elems = list_difference_int(candidate, previous);
    3174        2830 :         GroupingSetData *gs = makeNode(GroupingSetData);
    3175             : 
    3176        2994 :         while (list_length(sortclause) > list_length(previous) &&
    3177             :                new_elems != NIL)
    3178             :         {
    3179         272 :             SortGroupClause *sc = list_nth(sortclause, list_length(previous));
    3180         272 :             int         ref = sc->tleSortGroupRef;
    3181             : 
    3182         272 :             if (list_member_int(new_elems, ref))
    3183             :             {
    3184         164 :                 previous = lappend_int(previous, ref);
    3185         164 :                 new_elems = list_delete_int(new_elems, ref);
    3186             :             }
    3187             :             else
    3188             :             {
    3189             :                 /* diverged from the sortclause; give up on it */
    3190         108 :                 sortclause = NIL;
    3191         108 :                 break;
    3192             :             }
    3193             :         }
    3194             : 
    3195        2830 :         previous = list_concat(previous, new_elems);
    3196             : 
    3197        2830 :         gs->set = list_copy(previous);
    3198        2830 :         result = lcons(gs, result);
    3199             :     }
    3200             : 
    3201        1510 :     list_free(previous);
    3202             : 
    3203        1510 :     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             :  */
    3211             : static bool
    3212        2814 : has_volatile_pathkey(List *keys)
    3213             : {
    3214             :     ListCell   *lc;
    3215             : 
    3216        5772 :     foreach(lc, keys)
    3217             :     {
    3218        2976 :         PathKey    *pathkey = lfirst_node(PathKey, lc);
    3219             : 
    3220        2976 :         if (pathkey->pk_eclass->ec_has_volatile)
    3221          18 :             return true;
    3222             :     }
    3223             : 
    3224        2796 :     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             :  */
    3256             : static void
    3257        2418 : adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
    3258             : {
    3259        2418 :     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 */
    3272        2418 :     if (!enable_presorted_aggregate)
    3273           6 :         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        2412 :     unprocessed_aggs = NULL;
    3280        5508 :     foreach(lc, root->agginfos)
    3281             :     {
    3282        3096 :         AggInfo    *agginfo = lfirst_node(AggInfo, lc);
    3283        3096 :         Aggref     *aggref = linitial_node(Aggref, agginfo->aggrefs);
    3284             : 
    3285        3096 :         if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
    3286         264 :             continue;
    3287             : 
    3288             :         /* Skip unless there's a DISTINCT or ORDER BY clause */
    3289        2832 :         if (aggref->aggdistinct == NIL && aggref->aggorder == NIL)
    3290         300 :             continue;
    3291             : 
    3292             :         /* Additional safety checks are needed if there's a FILTER clause */
    3293        2532 :         if (aggref->aggfilter != NULL)
    3294             :         {
    3295             :             ListCell   *lc2;
    3296          54 :             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         102 :             foreach(lc2, aggref->args)
    3312             :             {
    3313          72 :                 TargetEntry *tle = (TargetEntry *) lfirst(lc2);
    3314          72 :                 Expr       *expr = tle->expr;
    3315             : 
    3316          84 :                 while (IsA(expr, RelabelType))
    3317          12 :                     expr = (Expr *) (castNode(RelabelType, expr))->arg;
    3318             : 
    3319             :                 /* Common case, Vars and Consts are ok */
    3320          72 :                 if (IsA(expr, Var) || IsA(expr, Const))
    3321          48 :                     continue;
    3322             : 
    3323             :                 /* Unsupported.  Don't try to presort for this Aggref */
    3324          24 :                 allow_presort = false;
    3325          24 :                 break;
    3326             :             }
    3327             : 
    3328             :             /* Skip unsupported Aggrefs */
    3329          54 :             if (!allow_presort)
    3330          24 :                 continue;
    3331             :         }
    3332             : 
    3333        2508 :         unprocessed_aggs = bms_add_member(unprocessed_aggs,
    3334             :                                           foreach_current_index(lc));
    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        2412 :     bestpathkeys = NIL;
    3353        2412 :     bestaggs = NULL;
    3354        4758 :     while (bms_num_members(unprocessed_aggs) > bms_num_members(bestaggs))
    3355             :     {
    3356        2346 :         Bitmapset  *aggindexes = NULL;
    3357        2346 :         List       *currpathkeys = NIL;
    3358             : 
    3359        2346 :         i = -1;
    3360        5160 :         while ((i = bms_next_member(unprocessed_aggs, i)) >= 0)
    3361             :         {
    3362        2814 :             AggInfo    *agginfo = list_nth_node(AggInfo, root->agginfos, i);
    3363        2814 :             Aggref     *aggref = linitial_node(Aggref, agginfo->aggrefs);
    3364             :             List       *sortlist;
    3365             :             List       *pathkeys;
    3366             : 
    3367        2814 :             if (aggref->aggdistinct != NIL)
    3368         718 :                 sortlist = aggref->aggdistinct;
    3369             :             else
    3370        2096 :                 sortlist = aggref->aggorder;
    3371             : 
    3372        2814 :             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        2814 :             if (has_volatile_pathkey(pathkeys))
    3380             :             {
    3381          18 :                 unprocessed_aggs = bms_del_member(unprocessed_aggs, i);
    3382          18 :                 continue;
    3383             :             }
    3384             : 
    3385             :             /*
    3386             :              * When not set yet, take the pathkeys from the first unprocessed
    3387             :              * aggregate.
    3388             :              */
    3389        2796 :             if (currpathkeys == NIL)
    3390             :             {
    3391        2340 :                 currpathkeys = pathkeys;
    3392             : 
    3393             :                 /* include the GROUP BY pathkeys, if they exist */
    3394        2340 :                 if (grouppathkeys != NIL)
    3395         276 :                     currpathkeys = append_pathkeys(list_copy(grouppathkeys),
    3396             :                                                    currpathkeys);
    3397             : 
    3398             :                 /* record that we found pathkeys for this aggregate */
    3399        2340 :                 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         456 :                 if (grouppathkeys != NIL)
    3407         288 :                     pathkeys = append_pathkeys(list_copy(grouppathkeys),
    3408             :                                                pathkeys);
    3409             : 
    3410             :                 /* are 'pathkeys' compatible or better than 'currpathkeys'? */
    3411         456 :                 switch (compare_pathkeys(currpathkeys, pathkeys))
    3412             :                 {
    3413          12 :                     case PATHKEYS_BETTER2:
    3414             :                         /* 'pathkeys' are stronger, use these ones instead */
    3415          12 :                         currpathkeys = pathkeys;
    3416             :                         /* FALLTHROUGH */
    3417             : 
    3418          66 :                     case PATHKEYS_BETTER1:
    3419             :                         /* 'pathkeys' are less strict */
    3420             :                         /* FALLTHROUGH */
    3421             : 
    3422             :                     case PATHKEYS_EQUAL:
    3423             :                         /* mark this aggregate as covered by 'currpathkeys' */
    3424          66 :                         aggindexes = bms_add_member(aggindexes, i);
    3425          66 :                         break;
    3426             : 
    3427         390 :                     case PATHKEYS_DIFFERENT:
    3428         390 :                         break;
    3429             :                 }
    3430             :             }
    3431             :         }
    3432             : 
    3433             :         /* remove the aggregates that we've just processed */
    3434        2346 :         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        2346 :         if (bms_num_members(aggindexes) > bms_num_members(bestaggs))
    3441             :         {
    3442        2238 :             bestaggs = aggindexes;
    3443        2238 :             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        2412 :     if (bestpathkeys != NIL)
    3453        2178 :         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        2412 :     i = -1;
    3464        4686 :     while ((i = bms_next_member(bestaggs, i)) >= 0)
    3465             :     {
    3466        2274 :         AggInfo    *agginfo = list_nth_node(AggInfo, root->agginfos, i);
    3467             : 
    3468        4566 :         foreach(lc, agginfo->aggrefs)
    3469             :         {
    3470        2292 :             Aggref     *aggref = lfirst_node(Aggref, lc);
    3471             : 
    3472        2292 :             aggref->aggpresorted = true;
    3473             :         }
    3474             :     }
    3475             : }
    3476             : 
    3477             : /*
    3478             :  * Compute query_pathkeys and other pathkeys during plan generation
    3479             :  */
    3480             : static void
    3481      516270 : standard_qp_callback(PlannerInfo *root, void *extra)
    3482             : {
    3483      516270 :     Query      *parse = root->parse;
    3484      516270 :     standard_qp_extra *qp_extra = (standard_qp_extra *) extra;
    3485      516270 :     List       *tlist = root->processed_tlist;
    3486      516270 :     List       *activeWindows = qp_extra->activeWindows;
    3487             : 
    3488             :     /*
    3489             :      * Calculate pathkeys that represent grouping/ordering and/or ordered
    3490             :      * aggregate requirements.
    3491             :      */
    3492      516270 :     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         932 :         List       *rollups = qp_extra->gset_data->rollups;
    3501         932 :         List       *groupClause = (rollups ? linitial_node(RollupData, rollups)->groupClause : NIL);
    3502             : 
    3503         932 :         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         932 :             root->group_pathkeys =
    3514         932 :                 make_pathkeys_for_sortclauses_extended(root,
    3515             :                                                        &groupClause,
    3516             :                                                        tlist,
    3517             :                                                        false,
    3518         932 :                                                        parse->hasGroupRTE,
    3519             :                                                        &sortable,
    3520             :                                                        false);
    3521             :             Assert(sortable);
    3522         932 :             root->num_groupby_pathkeys = list_length(root->group_pathkeys);
    3523             :         }
    3524             :         else
    3525             :         {
    3526           0 :             root->group_pathkeys = NIL;
    3527           0 :             root->num_groupby_pathkeys = 0;
    3528             :         }
    3529             :     }
    3530      515338 :     else if (parse->groupClause || root->numOrderedAggs > 0)
    3531        6084 :     {
    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        6084 :         root->group_pathkeys =
    3547        6084 :             make_pathkeys_for_sortclauses_extended(root,
    3548             :                                                    &root->processed_groupClause,
    3549             :                                                    tlist,
    3550             :                                                    true,
    3551             :                                                    false,
    3552             :                                                    &sortable,
    3553             :                                                    true);
    3554        6084 :         if (!sortable)
    3555             :         {
    3556             :             /* Can't sort; no point in considering aggregate ordering either */
    3557           0 :             root->group_pathkeys = NIL;
    3558           0 :             root->num_groupby_pathkeys = 0;
    3559             :         }
    3560             :         else
    3561             :         {
    3562        6084 :             root->num_groupby_pathkeys = list_length(root->group_pathkeys);
    3563             :             /* If we have ordered aggs, consider adding onto group_pathkeys */
    3564        6084 :             if (root->numOrderedAggs > 0)
    3565        2418 :                 adjust_group_pathkeys_for_groupagg(root);
    3566             :         }
    3567             :     }
    3568             :     else
    3569             :     {
    3570      509254 :         root->group_pathkeys = NIL;
    3571      509254 :         root->num_groupby_pathkeys = 0;
    3572             :     }
    3573             : 
    3574             :     /* We consider only the first (bottom) window in pathkeys logic */
    3575      516270 :     if (activeWindows != NIL)
    3576             :     {
    3577        2564 :         WindowClause *wc = linitial_node(WindowClause, activeWindows);
    3578             : 
    3579        2564 :         root->window_pathkeys = make_pathkeys_for_window(root,
    3580             :                                                          wc,
    3581             :                                                          tlist);
    3582             :     }
    3583             :     else
    3584      513706 :         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      516270 :     if (parse->distinctClause)
    3593             :     {
    3594             :         bool        sortable;
    3595             : 
    3596             :         /* Make a copy since pathkey processing can modify the list */
    3597        2956 :         root->processed_distinctClause = list_copy(parse->distinctClause);
    3598        2956 :         root->distinct_pathkeys =
    3599        2956 :             make_pathkeys_for_sortclauses_extended(root,
    3600             :                                                    &root->processed_distinctClause,
    3601             :                                                    tlist,
    3602             :                                                    true,
    3603             :                                                    false,
    3604             :                                                    &sortable,
    3605             :                                                    false);
    3606        2956 :         if (!sortable)
    3607           6 :             root->distinct_pathkeys = NIL;
    3608             :     }
    3609             :     else
    3610      513314 :         root->distinct_pathkeys = NIL;
    3611             : 
    3612      516270 :     root->sort_pathkeys =
    3613      516270 :         make_pathkeys_for_sortclauses(root,
    3614             :                                       parse->sortClause,
    3615             :                                       tlist);
    3616             : 
    3617             :     /* setting setop_pathkeys might be useful to the union planner */
    3618      516270 :     if (qp_extra->setop != NULL)
    3619             :     {
    3620             :         List       *groupClauses;
    3621             :         bool        sortable;
    3622             : 
    3623       12488 :         groupClauses = generate_setop_child_grouplist(qp_extra->setop, tlist);
    3624             : 
    3625       12488 :         root->setop_pathkeys =
    3626       12488 :             make_pathkeys_for_sortclauses_extended(root,
    3627             :                                                    &groupClauses,
    3628             :                                                    tlist,
    3629             :                                                    false,
    3630             :                                                    false,
    3631             :                                                    &sortable,
    3632             :                                                    false);
    3633       12488 :         if (!sortable)
    3634         204 :             root->setop_pathkeys = NIL;
    3635             :     }
    3636             :     else
    3637      503782 :         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      516270 :     if (root->group_pathkeys)
    3660        6636 :         root->query_pathkeys = root->group_pathkeys;
    3661      509634 :     else if (root->window_pathkeys)
    3662        2098 :         root->query_pathkeys = root->window_pathkeys;
    3663     1015072 :     else if (list_length(root->distinct_pathkeys) >
    3664      507536 :              list_length(root->sort_pathkeys))
    3665        2472 :         root->query_pathkeys = root->distinct_pathkeys;
    3666      505064 :     else if (root->sort_pathkeys)
    3667       67414 :         root->query_pathkeys = root->sort_pathkeys;
    3668      437650 :     else if (root->setop_pathkeys != NIL)
    3669       11092 :         root->query_pathkeys = root->setop_pathkeys;
    3670             :     else
    3671      426558 :         root->query_pathkeys = NIL;
    3672      516270 : }
    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             :  */
    3685             : static double
    3686       46132 : get_number_of_groups(PlannerInfo *root,
    3687             :                      double path_rows,
    3688             :                      grouping_sets_data *gd,
    3689             :                      List *target_list)
    3690             : {
    3691       46132 :     Query      *parse = root->parse;
    3692             :     double      dNumGroups;
    3693             : 
    3694       46132 :     if (parse->groupClause)
    3695             :     {
    3696             :         List       *groupExprs;
    3697             : 
    3698       10066 :         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         866 :             dNumGroups = 0;
    3706             : 
    3707        2310 :             foreach(lc, gd->rollups)
    3708             :             {
    3709        1444 :                 RollupData *rollup = lfirst_node(RollupData, lc);
    3710             :                 ListCell   *lc2;
    3711             :                 ListCell   *lc3;
    3712             : 
    3713        1444 :                 groupExprs = get_sortgrouplist_exprs(rollup->groupClause,
    3714             :                                                      target_list);
    3715             : 
    3716        1444 :                 rollup->numGroups = 0.0;
    3717             : 
    3718        4160 :                 forboth(lc2, rollup->gsets, lc3, rollup->gsets_data)
    3719             :                 {
    3720        2716 :                     List       *gset = (List *) lfirst(lc2);
    3721        2716 :                     GroupingSetData *gs = lfirst_node(GroupingSetData, lc3);
    3722        2716 :                     double      numGroups = estimate_num_groups(root,
    3723             :                                                                 groupExprs,
    3724             :                                                                 path_rows,
    3725             :                                                                 &gset,
    3726             :                                                                 NULL);
    3727             : 
    3728        2716 :                     gs->numGroups = numGroups;
    3729        2716 :                     rollup->numGroups += numGroups;
    3730             :                 }
    3731             : 
    3732        1444 :                 dNumGroups += rollup->numGroups;
    3733             :             }
    3734             : 
    3735         866 :             if (gd->hash_sets_idx)
    3736             :             {
    3737             :                 ListCell   *lc2;
    3738             : 
    3739          36 :                 gd->dNumHashGroups = 0;
    3740             : 
    3741          36 :                 groupExprs = get_sortgrouplist_exprs(parse->groupClause,
    3742             :                                                      target_list);
    3743             : 
    3744          78 :                 forboth(lc, gd->hash_sets_idx, lc2, gd->unsortable_sets)
    3745             :                 {
    3746          42 :                     List       *gset = (List *) lfirst(lc);
    3747          42 :                     GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
    3748          42 :                     double      numGroups = estimate_num_groups(root,
    3749             :                                                                 groupExprs,
    3750             :                                                                 path_rows,
    3751             :                                                                 &gset,
    3752             :                                                                 NULL);
    3753             : 
    3754          42 :                     gs->numGroups = numGroups;
    3755          42 :                     gd->dNumHashGroups += numGroups;
    3756             :                 }
    3757             : 
    3758          36 :                 dNumGroups += gd->dNumHashGroups;
    3759             :             }
    3760             :         }
    3761             :         else
    3762             :         {
    3763             :             /* Plain GROUP BY -- estimate based on optimized groupClause */
    3764        9200 :             groupExprs = get_sortgrouplist_exprs(root->processed_groupClause,
    3765             :                                                  target_list);
    3766             : 
    3767        9200 :             dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
    3768             :                                              NULL, NULL);
    3769             :         }
    3770             :     }
    3771       36066 :     else if (parse->groupingSets)
    3772             :     {
    3773             :         /* Empty grouping sets ... one result row for each one */
    3774          60 :         dNumGroups = list_length(parse->groupingSets);
    3775             :     }
    3776       36006 :     else if (parse->hasAggs || root->hasHavingQual)
    3777             :     {
    3778             :         /* Plain aggregation, one result row */
    3779       36006 :         dNumGroups = 1;
    3780             :     }
    3781             :     else
    3782             :     {
    3783             :         /* Not grouping */
    3784           0 :         dNumGroups = 1;
    3785             :     }
    3786             : 
    3787       46132 :     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             :  */
    3807             : static RelOptInfo *
    3808       39284 : create_grouping_paths(PlannerInfo *root,
    3809             :                       RelOptInfo *input_rel,
    3810             :                       PathTarget *target,
    3811             :                       bool target_parallel_safe,
    3812             :                       grouping_sets_data *gd)
    3813             : {
    3814       39284 :     Query      *parse = root->parse;
    3815             :     RelOptInfo *grouped_rel;
    3816             :     RelOptInfo *partially_grouped_rel;
    3817             :     AggClauseCosts agg_costs;
    3818             : 
    3819      235704 :     MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
    3820       39284 :     get_agg_clause_costs(root, AGGSPLIT_SIMPLE, &agg_costs);
    3821             : 
    3822             :     /*
    3823             :      * Create grouping relation to hold fully aggregated grouping and/or
    3824             :      * aggregation paths.
    3825             :      */
    3826       39284 :     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             :      */
    3833       39284 :     if (is_degenerate_grouping(root))
    3834          30 :         create_degenerate_grouping_paths(root, input_rel, grouped_rel);
    3835             :     else
    3836             :     {
    3837       39254 :         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       39254 :         if ((gd && gd->rollups != NIL)
    3852       38334 :             || grouping_is_sortable(root->processed_groupClause))
    3853       39248 :             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       39254 :         if ((parse->groupClause != NIL &&
    3876        9272 :              root->numOrderedAggs == 0 &&
    3877        4496 :              (gd ? gd->any_hashable : grouping_is_hashable(root->processed_groupClause))))
    3878        4492 :             flags |= GROUPING_CAN_USE_HASH;
    3879             : 
    3880             :         /*
    3881             :          * Determine whether partial aggregation is possible.
    3882             :          */
    3883       39254 :         if (can_partial_agg(root))
    3884       34456 :             flags |= GROUPING_CAN_PARTIAL_AGG;
    3885             : 
    3886       39254 :         extra.flags = flags;
    3887       39254 :         extra.target_parallel_safe = target_parallel_safe;
    3888       39254 :         extra.havingQual = parse->havingQual;
    3889       39254 :         extra.targetList = parse->targetList;
    3890       39254 :         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       39254 :         if (enable_partitionwise_aggregate && !parse->groupingSets)
    3899         688 :             extra.patype = PARTITIONWISE_AGGREGATE_FULL;
    3900             :         else
    3901       38566 :             extra.patype = PARTITIONWISE_AGGREGATE_NONE;
    3902             : 
    3903       39254 :         create_ordinary_grouping_paths(root, input_rel, grouped_rel,
    3904             :                                        &agg_costs, gd, &extra,
    3905             :                                        &partially_grouped_rel);
    3906             :     }
    3907             : 
    3908       39278 :     set_cheapest(grouped_rel);
    3909       39278 :     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             :  */
    3920             : static RelOptInfo *
    3921       41414 : make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
    3922             :                   PathTarget *target, bool target_parallel_safe,
    3923             :                   Node *havingQual)
    3924             : {
    3925             :     RelOptInfo *grouped_rel;
    3926             : 
    3927       41414 :     if (IS_OTHER_REL(input_rel))
    3928             :     {
    3929        2130 :         grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG,
    3930             :                                       input_rel->relids);
    3931        2130 :         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       39284 :         grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
    3941             :     }
    3942             : 
    3943             :     /* Set target. */
    3944       41414 :     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       70442 :     if (input_rel->consider_parallel && target_parallel_safe &&
    3952       29028 :         is_parallel_safe(root, (Node *) havingQual))
    3953       29010 :         grouped_rel->consider_parallel = true;
    3954             : 
    3955             :     /*
    3956             :      * If the input rel belongs to a single FDW, so does the grouped rel.
    3957             :      */
    3958       41414 :     grouped_rel->serverid = input_rel->serverid;
    3959       41414 :     grouped_rel->userid = input_rel->userid;
    3960       41414 :     grouped_rel->useridiscurrent = input_rel->useridiscurrent;
    3961       41414 :     grouped_rel->fdwroutine = input_rel->fdwroutine;
    3962             : 
    3963       41414 :     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             :  */
    3973             : static bool
    3974       39284 : is_degenerate_grouping(PlannerInfo *root)
    3975             : {
    3976       39284 :     Query      *parse = root->parse;
    3977             : 
    3978       38160 :     return (root->hasHavingQual || parse->groupingSets) &&
    3979       77444 :         !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             :  */
    3994             : static void
    3995          30 : create_degenerate_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
    3996             :                                  RelOptInfo *grouped_rel)
    3997             : {
    3998          30 :     Query      *parse = root->parse;
    3999             :     int         nrows;
    4000             :     Path       *path;
    4001             : 
    4002          30 :     nrows = list_length(parse->groupingSets);
    4003          30 :     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           0 :         List       *paths = NIL;
    4013             : 
    4014           0 :         while (--nrows >= 0)
    4015             :         {
    4016             :             path = (Path *)
    4017           0 :                 create_group_result_path(root, grouped_rel,
    4018           0 :                                          grouped_rel->reltarget,
    4019           0 :                                          (List *) parse->havingQual);
    4020           0 :             paths = lappend(paths, path);
    4021             :         }
    4022             :         path = (Path *)
    4023           0 :             create_append_path(root,
    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          30 :             create_group_result_path(root, grouped_rel,
    4038          30 :                                      grouped_rel->reltarget,
    4039          30 :                                      (List *) parse->havingQual);
    4040             :     }
    4041             : 
    4042          30 :     add_path(grouped_rel, path);
    4043          30 : }
    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             :  */
    4058             : static void
    4059       41384 : create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
    4060             :                                RelOptInfo *grouped_rel,
    4061             :                                const AggClauseCosts *agg_costs,
    4062             :                                grouping_sets_data *gd,
    4063             :                                GroupPathExtraData *extra,
    4064             :                                RelOptInfo **partially_grouped_rel_p)
    4065             : {
    4066       41384 :     RelOptInfo *partially_grouped_rel = NULL;
    4067       41384 :     PartitionwiseAggregateType patype = PARTITIONWISE_AGGREGATE_NONE;
    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       41384 :     if (extra->patype != PARTITIONWISE_AGGREGATE_NONE &&
    4076        2818 :         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        1616 :         if (extra->patype == PARTITIONWISE_AGGREGATE_FULL &&
    4092         760 :             group_by_has_partkey(input_rel, extra->targetList,
    4093         760 :                                  root->parse->groupClause))
    4094         476 :             patype = PARTITIONWISE_AGGREGATE_FULL;
    4095         380 :         else if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
    4096         338 :             patype = PARTITIONWISE_AGGREGATE_PARTIAL;
    4097             :         else
    4098          42 :             patype = PARTITIONWISE_AGGREGATE_NONE;
    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       41384 :     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       36514 :         force_rel_creation = (patype == PARTITIONWISE_AGGREGATE_PARTIAL);
    4116             : 
    4117             :         partially_grouped_rel =
    4118       36514 :             create_partial_grouping_paths(root,
    4119             :                                           grouped_rel,
    4120             :                                           input_rel,
    4121             :                                           gd,
    4122             :                                           extra,
    4123             :                                           force_rel_creation);
    4124             :     }
    4125             : 
    4126             :     /* Set out parameter. */
    4127       41384 :     *partially_grouped_rel_p = partially_grouped_rel;
    4128             : 
    4129             :     /* Apply partitionwise aggregation technique, if possible. */
    4130       41384 :     if (patype != PARTITIONWISE_AGGREGATE_NONE)
    4131         814 :         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. */
    4136       41384 :     if (extra->patype == PARTITIONWISE_AGGREGATE_PARTIAL)
    4137             :     {
    4138             :         Assert(partially_grouped_rel);
    4139             : 
    4140         858 :         if (partially_grouped_rel->pathlist)
    4141         858 :             set_cheapest(partially_grouped_rel);
    4142             : 
    4143         858 :         return;
    4144             :     }
    4145             : 
    4146             :     /* Gather any partially grouped partial paths. */
    4147       40526 :     if (partially_grouped_rel && partially_grouped_rel->partial_pathlist)
    4148        2008 :         gather_grouping_paths(root, partially_grouped_rel);
    4149             : 
    4150             :     /* Now choose the best path(s) for partially_grouped_rel. */
    4151       40526 :     if (partially_grouped_rel && partially_grouped_rel->pathlist)
    4152        2236 :         set_cheapest(partially_grouped_rel);
    4153             : 
    4154             :     /* Build final grouping paths */
    4155       40526 :     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       40526 :     if (grouped_rel->pathlist == NIL)
    4161           6 :         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       40520 :     if (grouped_rel->fdwroutine &&
    4171         336 :         grouped_rel->fdwroutine->GetForeignUpperPaths)
    4172         336 :         grouped_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_GROUP_AGG,
    4173             :                                                       input_rel, grouped_rel,
    4174             :                                                       extra);
    4175             : 
    4176             :     /* Let extensions possibly add some more paths */
    4177       40520 :     if (create_upper_paths_hook)
    4178           0 :         (*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             :  */
    4189             : static void
    4190        1822 : consider_groupingsets_paths(PlannerInfo *root,
    4191             :                             RelOptInfo *grouped_rel,
    4192             :                             Path *path,
    4193             :                             bool is_sorted,
    4194             :                             bool can_hash,
    4195             :                             grouping_sets_data *gd,
    4196             :                             const AggClauseCosts *agg_costs,
    4197             :                             double dNumGroups)
    4198             : {
    4199        1822 :     Query      *parse = root->parse;
    4200        1822 :     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        1822 :     if (!is_sorted)
    4214             :     {
    4215         830 :         List       *new_rollups = NIL;
    4216         830 :         RollupData *unhashed_rollup = NULL;
    4217             :         List       *sets_data;
    4218         830 :         List       *empty_sets_data = NIL;
    4219         830 :         List       *empty_sets = NIL;
    4220             :         ListCell   *lc;
    4221         830 :         ListCell   *l_start = list_head(gd->rollups);
    4222         830 :         AggStrategy strat = AGG_HASHED;
    4223             :         double      hashsize;
    4224         830 :         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        1654 :         if (l_start != NULL &&
    4249         824 :             pathkeys_contained_in(root->group_pathkeys, path->pathkeys))
    4250             :         {
    4251          12 :             unhashed_rollup = lfirst_node(RollupData, l_start);
    4252          12 :             exclude_groups = unhashed_rollup->numGroups;
    4253          12 :             l_start = lnext(gd->rollups, l_start);
    4254             :         }
    4255             : 
    4256         830 :         hashsize = estimate_hashagg_tablesize(root,
    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         830 :         if (hashsize > hash_mem_limit && gd->rollups)
    4267          18 :             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         812 :         sets_data = list_copy(gd->unsortable_sets);
    4274             : 
    4275        2064 :         for_each_cell(lc, gd->rollups, l_start)
    4276             :         {
    4277        1276 :             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        1276 :             if (!rollup->hashable)
    4291          24 :                 return;
    4292             : 
    4293        1252 :             sets_data = list_concat(sets_data, rollup->gsets_data);
    4294             :         }
    4295        3270 :         foreach(lc, sets_data)
    4296             :         {
    4297        2482 :             GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
    4298        2482 :             List       *gset = gs->set;
    4299             :             RollupData *rollup;
    4300             : 
    4301        2482 :             if (gset == NIL)
    4302             :             {
    4303             :                 /* Empty grouping sets can't be hashed. */
    4304         496 :                 empty_sets_data = lappend(empty_sets_data, gs);
    4305         496 :                 empty_sets = lappend(empty_sets, NIL);
    4306             :             }
    4307             :             else
    4308             :             {
    4309        1986 :                 rollup = makeNode(RollupData);
    4310             : 
    4311        1986 :                 rollup->groupClause = preprocess_groupclause(root, gset);
    4312        1986 :                 rollup->gsets_data = list_make1(gs);
    4313        1986 :                 rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
    4314             :                                                          rollup->gsets_data,
    4315             :                                                          gd->tleref_to_colnum_map);
    4316        1986 :                 rollup->numGroups = gs->numGroups;
    4317        1986 :                 rollup->hashable = true;
    4318        1986 :                 rollup->is_hashed = true;
    4319        1986 :                 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         788 :         if (new_rollups == NIL)
    4328           0 :             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         788 :         if (unhashed_rollup)
    4337             :         {
    4338          12 :             new_rollups = lappend(new_rollups, unhashed_rollup);
    4339          12 :             strat = AGG_MIXED;
    4340             :         }
    4341         776 :         else if (empty_sets)
    4342             :         {
    4343         448 :             RollupData *rollup = makeNode(RollupData);
    4344             : 
    4345         448 :             rollup->groupClause = NIL;
    4346         448 :             rollup->gsets_data = empty_sets_data;
    4347         448 :             rollup->gsets = empty_sets;
    4348         448 :             rollup->numGroups = list_length(empty_sets);
    4349         448 :             rollup->hashable = false;
    4350         448 :             rollup->is_hashed = false;
    4351         448 :             new_rollups = lappend(new_rollups, rollup);
    4352         448 :             strat = AGG_MIXED;
    4353             :         }
    4354             : 
    4355         788 :         add_path(grouped_rel, (Path *)
    4356         788 :                  create_groupingsets_path(root,
    4357             :                                           grouped_rel,
    4358             :                                           path,
    4359         788 :                                           (List *) parse->havingQual,
    4360             :                                           strat,
    4361             :                                           new_rollups,
    4362             :                                           agg_costs));
    4363         788 :         return;
    4364             :     }
    4365             : 
    4366             :     /*
    4367             :      * If we have sorted input but nothing we can do with it, bail.
    4368             :      */
    4369         992 :     if (gd->rollups == NIL)
    4370           0 :         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         992 :     if (can_hash && gd->any_hashable)
    4381             :     {
    4382         896 :         List       *rollups = NIL;
    4383         896 :         List       *hash_sets = list_copy(gd->unsortable_sets);
    4384         896 :         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         896 :         availspace -= estimate_hashagg_tablesize(root,
    4391             :                                                  path,
    4392             :                                                  agg_costs,
    4393             :                                                  gd->dNumHashGroups);
    4394             : 
    4395         896 :         if (availspace > 0 && list_length(gd->rollups) > 1)
    4396             :         {
    4397             :             double      scale;
    4398         456 :             int         num_rollups = list_length(gd->rollups);
    4399             :             int         k_capacity;
    4400         456 :             int        *k_weights = palloc(num_rollups * sizeof(int));
    4401         456 :             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         456 :             scale = Max(availspace / (20.0 * num_rollups), 1.0);
    4427         456 :             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         456 :             i = 0;
    4436        1164 :             for_each_from(lc, gd->rollups, 1)
    4437             :             {
    4438         708 :                 RollupData *rollup = lfirst_node(RollupData, lc);
    4439             : 
    4440         708 :                 if (rollup->hashable)
    4441             :                 {
    4442         708 :                     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         708 :                     k_weights[i] = (int) Min(floor(sz / scale),
    4452             :                                              k_capacity + 1.0);
    4453         708 :                     ++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         456 :             if (i > 0)
    4464         456 :                 hash_items = DiscreteKnapsack(k_capacity, i, k_weights, NULL);
    4465             : 
    4466         456 :             if (!bms_is_empty(hash_items))
    4467             :             {
    4468         456 :                 rollups = list_make1(linitial(gd->rollups));
    4469             : 
    4470         456 :                 i = 0;
    4471        1164 :                 for_each_from(lc, gd->rollups, 1)
    4472             :                 {
    4473         708 :                     RollupData *rollup = lfirst_node(RollupData, lc);
    4474             : 
    4475         708 :                     if (rollup->hashable)
    4476             :                     {
    4477         708 :                         if (bms_is_member(i, hash_items))
    4478         672 :                             hash_sets = list_concat(hash_sets,
    4479         672 :                                                     rollup->gsets_data);
    4480             :                         else
    4481          36 :                             rollups = lappend(rollups, rollup);
    4482         708 :                         ++i;
    4483             :                     }
    4484             :                     else
    4485           0 :                         rollups = lappend(rollups, rollup);
    4486             :                 }
    4487             :             }
    4488             :         }
    4489             : 
    4490         896 :         if (!rollups && hash_sets)
    4491          24 :             rollups = list_copy(gd->rollups);
    4492             : 
    4493        1708 :         foreach(lc, hash_sets)
    4494             :         {
    4495         812 :             GroupingSetData *gs = lfirst_node(GroupingSetData, lc);
    4496         812 :             RollupData *rollup = makeNode(RollupData);
    4497             : 
    4498             :             Assert(gs->set != NIL);
    4499             : 
    4500         812 :             rollup->groupClause = preprocess_groupclause(root, gs->set);
    4501         812 :             rollup->gsets_data = list_make1(gs);
    4502         812 :             rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
    4503             :                                                      rollup->gsets_data,
    4504             :                                                      gd->tleref_to_colnum_map);
    4505         812 :             rollup->numGroups = gs->numGroups;
    4506         812 :             rollup->hashable = true;
    4507         812 :             rollup->is_hashed = true;
    4508         812 :             rollups = lcons(rollup, rollups);
    4509             :         }
    4510             : 
    4511         896 :         if (rollups)
    4512             :         {
    4513         480 :             add_path(grouped_rel, (Path *)
    4514         480 :                      create_groupingsets_path(root,
    4515             :                                               grouped_rel,
    4516             :                                               path,
    4517         480 :                                               (List *) parse->havingQual,
    4518             :                                               AGG_MIXED,
    4519             :                                               rollups,
    4520             :                                               agg_costs));
    4521             :         }
    4522             :     }
    4523             : 
    4524             :     /*
    4525             :      * Now try the simple sorted case.
    4526             :      */
    4527         992 :     if (!gd->unsortable_sets)
    4528         962 :         add_path(grouped_rel, (Path *)
    4529         962 :                  create_groupingsets_path(root,
    4530             :                                           grouped_rel,
    4531             :                                           path,
    4532         962 :                                           (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             :  */
    4551             : static RelOptInfo *
    4552        2564 : create_window_paths(PlannerInfo *root,
    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        2564 :     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        2564 :     if (input_rel->consider_parallel && output_target_parallel_safe &&
    4572           0 :         is_parallel_safe(root, (Node *) activeWindows))
    4573           0 :         window_rel->consider_parallel = true;
    4574             : 
    4575             :     /*
    4576             :      * If the input rel belongs to a single FDW, so does the window rel.
    4577             :      */
    4578        2564 :     window_rel->serverid = input_rel->serverid;
    4579        2564 :     window_rel->userid = input_rel->userid;
    4580        2564 :     window_rel->useridiscurrent = input_rel->useridiscurrent;
    4581        2564 :     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        5468 :     foreach(lc, input_rel->pathlist)
    4589             :     {
    4590        2904 :         Path       *path = (Path *) lfirst(lc);
    4591             :         int         presorted_keys;
    4592             : 
    4593        3244 :         if (path == input_rel->cheapest_total_path ||
    4594         340 :             pathkeys_count_contained_in(root->window_pathkeys, path->pathkeys,
    4595         152 :                                         &presorted_keys) ||
    4596         152 :             presorted_keys > 0)
    4597        2778 :             create_one_window_path(root,
    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        2564 :     if (window_rel->fdwroutine &&
    4611          12 :         window_rel->fdwroutine->GetForeignUpperPaths)
    4612          12 :         window_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_WINDOW,
    4613             :                                                      input_rel, window_rel,
    4614             :                                                      NULL);
    4615             : 
    4616             :     /* Let extensions possibly add some more paths */
    4617        2564 :     if (create_upper_paths_hook)
    4618           0 :         (*create_upper_paths_hook) (root, UPPERREL_WINDOW,
    4619             :                                     input_rel, window_rel, NULL);
    4620             : 
    4621             :     /* Now choose the best path(s) */
    4622        2564 :     set_cheapest(window_rel);
    4623             : 
    4624        2564 :     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             :  */
    4638             : static void
    4639        2778 : create_one_window_path(PlannerInfo *root,
    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        2778 :     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        2778 :     window_target = input_target;
    4667             : 
    4668        5742 :     foreach(l, activeWindows)
    4669             :     {
    4670        2964 :         WindowClause *wc = lfirst_node(WindowClause, l);
    4671             :         List       *window_pathkeys;
    4672        2964 :         List       *runcondition = NIL;
    4673             :         int         presorted_keys;
    4674             :         bool        is_sorted;
    4675             :         bool        topwindow;
    4676             :         ListCell   *lc2;
    4677             : 
    4678        2964 :         window_pathkeys = make_pathkeys_for_window(root,
    4679             :                                                    wc,
    4680             :                                                    root->processed_tlist);
    4681             : 
    4682        2964 :         is_sorted = pathkeys_count_contained_in(window_pathkeys,
    4683             :                                                 path->pathkeys,
    4684             :                                                 &presorted_keys);
    4685             : 
    4686             :         /* Sort if necessary */
    4687        2964 :         if (!is_sorted)
    4688             :         {
    4689             :             /*
    4690             :              * No presorted keys or incremental sort disabled, just perform a
    4691             :              * complete sort.
    4692             :              */
    4693        2172 :             if (presorted_keys == 0 || !enable_incremental_sort)
    4694        2110 :                 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             :                  */
    4704          62 :                 path = (Path *) create_incremental_sort_path(root,
    4705             :                                                              window_rel,
    4706             :                                                              path,
    4707             :                                                              window_pathkeys,
    4708             :                                                              presorted_keys,
    4709             :                                                              -1.0);
    4710             :             }
    4711             :         }
    4712             : 
    4713        2964 :         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         186 :             int64       tuple_width = window_target->width;
    4724             : 
    4725         186 :             window_target = copy_pathtarget(window_target);
    4726         438 :             foreach(lc2, wflists->windowFuncs[wc->winref])
    4727             :             {
    4728         252 :                 WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
    4729             : 
    4730         252 :                 add_column_to_pathtarget(window_target, (Expr *) wfunc, 0);
    4731         252 :                 tuple_width += get_typavgwidth(wfunc->wintype, -1);
    4732             :             }
    4733         186 :             window_target->width = clamp_width_est(tuple_width);
    4734             :         }
    4735             :         else
    4736             :         {
    4737             :             /* Install the goal target in the topmost WindowAgg */
    4738        2778 :             window_target = output_target;
    4739             :         }
    4740             : 
    4741             :         /* mark the final item in the list as the top-level window */
    4742        2964 :         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        6798 :         foreach(lc2, wflists->windowFuncs[wc->winref])
    4749             :         {
    4750             :             ListCell   *lc3;
    4751        3834 :             WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
    4752             : 
    4753        4014 :             foreach(lc3, wfunc->runCondition)
    4754             :             {
    4755         180 :                 WindowFuncRunCondition *wfuncrc =
    4756             :                     lfirst_node(WindowFuncRunCondition, lc3);
    4757             :                 Expr       *opexpr;
    4758             :                 Expr       *leftop;
    4759             :                 Expr       *rightop;
    4760             : 
    4761         180 :                 if (wfuncrc->wfunc_left)
    4762             :                 {
    4763         162 :                     leftop = (Expr *) copyObject(wfunc);
    4764         162 :                     rightop = copyObject(wfuncrc->arg);
    4765             :                 }
    4766             :                 else
    4767             :                 {
    4768          18 :                     leftop = copyObject(wfuncrc->arg);
    4769          18 :                     rightop = (Expr *) copyObject(wfunc);
    4770             :                 }
    4771             : 
    4772         180 :                 opexpr = make_opclause(wfuncrc->opno,
    4773             :                                        BOOLOID,
    4774             :                                        false,
    4775             :                                        leftop,
    4776             :                                        rightop,
    4777             :                                        InvalidOid,
    4778             :                                        wfuncrc->inputcollid);
    4779             : 
    4780         180 :                 runcondition = lappend(runcondition, opexpr);
    4781             : 
    4782         180 :                 if (!topwindow)
    4783          24 :                     topqual = lappend(topqual, opexpr);
    4784             :             }
    4785             :         }
    4786             : 
    4787             :         path = (Path *)
    4788        2964 :             create_windowagg_path(root, window_rel, path, window_target,
    4789        2964 :                                   wflists->windowFuncs[wc->winref],
    4790             :                                   runcondition, wc,
    4791             :                                   topwindow ? topqual : NIL, topwindow);
    4792             :     }
    4793             : 
    4794        2778 :     add_path(window_rel, path);
    4795        2778 : }
    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             :  */
    4808             : static RelOptInfo *
    4809        2956 : create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
    4810             :                       PathTarget *target)
    4811             : {
    4812             :     RelOptInfo *distinct_rel;
    4813             : 
    4814             :     /* For now, do all work in the (DISTINCT, NULL) upperrel */
    4815        2956 :     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        2956 :     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        2956 :     distinct_rel->serverid = input_rel->serverid;
    4830        2956 :     distinct_rel->userid = input_rel->userid;
    4831        2956 :     distinct_rel->useridiscurrent = input_rel->useridiscurrent;
    4832        2956 :     distinct_rel->fdwroutine = input_rel->fdwroutine;
    4833             : 
    4834             :     /* build distinct paths based on input_rel's pathlist */
    4835        2956 :     create_final_distinct_paths(root, input_rel, distinct_rel);
    4836             : 
    4837             :     /* now build distinct paths based on input_rel's partial_pathlist */
    4838        2956 :     create_partial_distinct_paths(root, input_rel, distinct_rel, target);
    4839             : 
    4840             :     /* Give a helpful error if we failed to create any paths */
    4841        2956 :     if (distinct_rel->pathlist == NIL)
    4842           0 :         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        2956 :     if (distinct_rel->fdwroutine &&
    4852          16 :         distinct_rel->fdwroutine->GetForeignUpperPaths)
    4853          16 :         distinct_rel->fdwroutine->GetForeignUpperPaths(root,
    4854             :                                                        UPPERREL_DISTINCT,
    4855             :                                                        input_rel,
    4856             :                                                        distinct_rel,
    4857             :                                                        NULL);
    4858             : 
    4859             :     /* Let extensions possibly add some more paths */
    4860        2956 :     if (create_upper_paths_hook)
    4861           0 :         (*create_upper_paths_hook) (root, UPPERREL_DISTINCT, input_rel,
    4862             :                                     distinct_rel, NULL);
    4863             : 
    4864             :     /* Now choose the best path(s) */
    4865        2956 :     set_cheapest(distinct_rel);
    4866             : 
    4867        2956 :     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             :  */
    4878             : static void
    4879        2956 : create_partial_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
    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        2956 :     if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
    4892        2848 :         return;
    4893             : 
    4894         108 :     parse = root->parse;
    4895             : 
    4896             :     /* can't do parallel DISTINCT ON */
    4897         108 :     if (parse->hasDistinctOn)
    4898           0 :         return;
    4899             : 
    4900         108 :     partial_distinct_rel = fetch_upper_rel(root, UPPERREL_PARTIAL_DISTINCT,
    4901             :                                            NULL);
    4902         108 :     partial_distinct_rel->reltarget = target;
    4903         108 :     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         108 :     partial_distinct_rel->serverid = input_rel->serverid;
    4909         108 :     partial_distinct_rel->userid = input_rel->userid;
    4910         108 :     partial_distinct_rel->useridiscurrent = input_rel->useridiscurrent;
    4911         108 :     partial_distinct_rel->fdwroutine = input_rel->fdwroutine;
    4912             : 
    4913         108 :     cheapest_partial_path = linitial(input_rel->partial_pathlist);
    4914             : 
    4915         108 :     distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
    4916             :                                             parse->targetList);
    4917             : 
    4918             :     /* estimate how many distinct rows we'll get from each worker */
    4919         108 :     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         108 :     if (grouping_is_sortable(root->processed_distinctClause))
    4931             :     {
    4932         234 :         foreach(lc, input_rel->partial_pathlist)
    4933             :         {
    4934         126 :             Path       *input_path = (Path *) lfirst(lc);
    4935             :             Path       *sorted_path;
    4936         126 :             List       *useful_pathkeys_list = NIL;
    4937             : 
    4938             :             useful_pathkeys_list =
    4939         126 :                 get_useful_pathkeys_for_distinct(root,
    4940             :                                                  root->distinct_pathkeys,
    4941             :                                                  input_path->pathkeys);
    4942             :             Assert(list_length(useful_pathkeys_list) > 0);
    4943             : 
    4944         390 :             foreach_node(List, useful_pathkeys, useful_pathkeys_list)
    4945             :             {
    4946         138 :                 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         138 :                 if (sorted_path == NULL)
    4954          12 :                     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         126 :                 if (root->distinct_pathkeys == NIL)
    4962             :                 {
    4963             :                     Node       *limitCount;
    4964             : 
    4965           6 :                     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           6 :                     add_partial_path(partial_distinct_rel, (Path *)
    4982           6 :                                      create_limit_path(root, partial_distinct_rel,
    4983             :                                                        sorted_path,
    4984             :                                                        NULL,
    4985             :                                                        limitCount,
    4986             :                                                        LIMIT_OPTION_COUNT,
    4987             :                                                        0, 1));
    4988             :                 }
    4989             :                 else
    4990             :                 {
    4991         120 :                     add_partial_path(partial_distinct_rel, (Path *)
    4992         120 :                                      create_unique_path(root, partial_distinct_rel,
    4993             :                                                         sorted_path,
    4994         120 :                                                         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         108 :     if (enable_hashagg && grouping_is_hashable(root->processed_distinctClause))
    5008             :     {
    5009          78 :         add_partial_path(partial_distinct_rel, (Path *)
    5010          78 :                          create_agg_path(root,
    5011             :                                          partial_distinct_rel,
    5012             :                                          cheapest_partial_path,
    5013             :                                          cheapest_partial_path->pathtarget,
    5014             :                                          AGG_HASHED,
    5015             :                                          AGGSPLIT_SIMPLE,
    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         108 :     if (partial_distinct_rel->fdwroutine &&
    5027           0 :         partial_distinct_rel->fdwroutine->GetForeignUpperPaths)
    5028           0 :         partial_distinct_rel->fdwroutine->GetForeignUpperPaths(root,
    5029             :                                                                UPPERREL_PARTIAL_DISTINCT,
    5030             :                                                                input_rel,
    5031             :                                                                partial_distinct_rel,
    5032             :                                                                NULL);
    5033             : 
    5034             :     /* Let extensions possibly add some more partial paths */
    5035         108 :     if (create_upper_paths_hook)
    5036           0 :         (*create_upper_paths_hook) (root, UPPERREL_PARTIAL_DISTINCT,
    5037             :                                     input_rel, partial_distinct_rel, NULL);
    5038             : 
    5039         108 :     if (partial_distinct_rel->partial_pathlist != NIL)
    5040             :     {
    5041         108 :         generate_useful_gather_paths(root, partial_distinct_rel, true);
    5042         108 :         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         108 :         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             :  */
    5061             : static RelOptInfo *
    5062        3064 : create_final_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel,
    5063             :                             RelOptInfo *distinct_rel)
    5064             : {
    5065        3064 :     Query      *parse = root->parse;
    5066        3064 :     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        3064 :     if (parse->groupClause || parse->groupingSets || parse->hasAggs ||
    5072        2990 :         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          74 :         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        2990 :         distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
    5089             :                                                 parse->targetList);
    5090        2990 :         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        3064 :     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        3058 :         double      limittuples = root->distinct_pathkeys == NIL ? 1.0 : -1.0;
    5119             : 
    5120        3306 :         if (parse->hasDistinctOn &&
    5121         248 :             list_length(root->distinct_pathkeys) <
    5122         248 :             list_length(root->sort_pathkeys))
    5123          54 :             needed_pathkeys = root->sort_pathkeys;
    5124             :         else
    5125        3004 :             needed_pathkeys = root->distinct_pathkeys;
    5126             : 
    5127        8256 :         foreach(lc, input_rel->pathlist)
    5128             :         {
    5129        5198 :             Path       *input_path = (Path *) lfirst(lc);
    5130             :             Path       *sorted_path;
    5131        5198 :             List       *useful_pathkeys_list = NIL;
    5132             : 
    5133             :             useful_pathkeys_list =
    5134        5198 :                 get_useful_pathkeys_for_distinct(root,
    5135             :                                                  needed_pathkeys,
    5136             :                                                  input_path->pathkeys);
    5137             :             Assert(list_length(useful_pathkeys_list) > 0);
    5138             : 
    5139       16398 :             foreach_node(List, useful_pathkeys, useful_pathkeys_list)
    5140             :             {
    5141        6002 :                 sorted_path = make_ordered_path(root,
    5142             :                                                 distinct_rel,
    5143             :                                                 input_path,
    5144             :                                                 cheapest_input_path,
    5145             :                                                 useful_pathkeys,
    5146             :                                                 limittuples);
    5147             : 
    5148        6002 :                 if (sorted_path == NULL)
    5149         862 :                     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        5140 :                 if (root->distinct_pathkeys == NIL)
    5165             :                 {
    5166             :                     Node       *limitCount;
    5167             : 
    5168         138 :                     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         138 :                     add_path(distinct_rel, (Path *)
    5179         138 :                              create_limit_path(root, distinct_rel, sorted_path,
    5180             :                                                NULL, limitCount,
    5181             :                                                LIMIT_OPTION_COUNT, 0, 1));
    5182             :                 }
    5183             :                 else
    5184             :                 {
    5185        5002 :                     add_path(distinct_rel, (Path *)
    5186        5002 :                              create_unique_path(root, distinct_rel,
    5187             :                                                 sorted_path,
    5188        5002 :                                                 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        3064 :     if (distinct_rel->pathlist == NIL)
    5208           6 :         allow_hash = true;      /* we have no alternatives */
    5209        3058 :     else if (parse->hasDistinctOn || !enable_hashagg)
    5210         398 :         allow_hash = false;     /* policy-based decision not to hash */
    5211             :     else
    5212        2660 :         allow_hash = true;      /* default */
    5213             : 
    5214        3064 :     if (allow_hash && grouping_is_hashable(root->processed_distinctClause))
    5215             :     {
    5216             :         /* Generate hashed aggregate path --- no sort needed */
    5217        2666 :         add_path(distinct_rel, (Path *)
    5218        2666 :                  create_agg_path(root,
    5219             :                                  distinct_rel,
    5220             :                                  cheapest_input_path,
    5221             :                                  cheapest_input_path->pathtarget,
    5222             :                                  AGG_HASHED,
    5223             :                                  AGGSPLIT_SIMPLE,
    5224             :                                  root->processed_distinctClause,
    5225             :                                  NIL,
    5226             :                                  NULL,
    5227             :                                  numDistinctRows));
    5228             :     }
    5229             : 
    5230        3064 :     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             :  */
    5241             : static List *
    5242        5324 : get_useful_pathkeys_for_distinct(PlannerInfo *root, List *needed_pathkeys,
    5243             :                                  List *path_pathkeys)
    5244             : {
    5245        5324 :     List       *useful_pathkeys_list = NIL;
    5246        5324 :     List       *useful_pathkeys = NIL;
    5247             : 
    5248             :     /* always include the given 'needed_pathkeys' */
    5249        5324 :     useful_pathkeys_list = lappend(useful_pathkeys_list,
    5250             :                                    needed_pathkeys);
    5251             : 
    5252        5324 :     if (!enable_distinct_reordering)
    5253           0 :         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       13192 :     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        2572 :         if (!list_member_ptr(needed_pathkeys, pathkey))
    5271          10 :             break;
    5272        2562 :         if (root->parse->hasDistinctOn &&
    5273         200 :             !list_member_ptr(root->distinct_pathkeys, pathkey))
    5274          18 :             break;
    5275             : 
    5276        2544 :         useful_pathkeys = lappend(useful_pathkeys, pathkey);
    5277             :     }
    5278             : 
    5279             :     /* If no match at all, no point in reordering needed_pathkeys */
    5280        5324 :     if (useful_pathkeys == NIL)
    5281        3044 :         return useful_pathkeys_list;
    5282             : 
    5283             :     /*
    5284             :      * If not full match, the resulting pathkey list is not useful without
    5285             :      * incremental sort.
    5286             :      */
    5287        2280 :     if (list_length(useful_pathkeys) < list_length(needed_pathkeys) &&
    5288        1528 :         !enable_incremental_sort)
    5289          60 :         return useful_pathkeys_list;
    5290             : 
    5291             :     /* Append the remaining PathKey nodes in needed_pathkeys */
    5292        2220 :     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        2220 :     if (compare_pathkeys(needed_pathkeys,
    5300             :                          useful_pathkeys) == PATHKEYS_EQUAL)
    5301        1404 :         return useful_pathkeys_list;
    5302             : 
    5303         816 :     useful_pathkeys_list = lappend(useful_pathkeys_list,
    5304             :                                    useful_pathkeys);
    5305             : 
    5306         816 :     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             :  */
    5326             : static RelOptInfo *
    5327       74036 : create_ordered_paths(PlannerInfo *root,
    5328             :                      RelOptInfo *input_rel,
    5329             :                      PathTarget *target,
    5330             :                      bool target_parallel_safe,
    5331             :                      double limit_tuples)
    5332             : {
    5333       74036 :     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       74036 :     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       74036 :     if (input_rel->consider_parallel && target_parallel_safe)
    5346       51578 :         ordered_rel->consider_parallel = true;
    5347             : 
    5348             :     /*
    5349             :      * If the input rel belongs to a single FDW, so does the ordered_rel.
    5350             :      */
    5351       74036 :     ordered_rel->serverid = input_rel->serverid;
    5352       74036 :     ordered_rel->userid = input_rel->userid;
    5353       74036 :     ordered_rel->useridiscurrent = input_rel->useridiscurrent;
    5354       74036 :     ordered_rel->fdwroutine = input_rel->fdwroutine;
    5355             : 
    5356      187694 :     foreach(lc, input_rel->pathlist)
    5357             :     {
    5358      113658 :         Path       *input_path = (Path *) lfirst(lc);
    5359             :         Path       *sorted_path;
    5360             :         bool        is_sorted;
    5361             :         int         presorted_keys;
    5362             : 
    5363      113658 :         is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
    5364             :                                                 input_path->pathkeys, &presorted_keys);
    5365             : 
    5366      113658 :         if (is_sorted)
    5367       42528 :             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       71130 :             if (input_path != cheapest_input_path &&
    5378        5976 :                 (presorted_keys == 0 || !enable_incremental_sort))
    5379        1920 :                 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       69210 :             if (presorted_keys == 0 || !enable_incremental_sort)
    5387       64532 :                 sorted_path = (Path *) create_sort_path(root,
    5388             :                                                         ordered_rel,
    5389             :                                                         input_path,
    5390             :                                                         root->sort_pathkeys,
    5391             :                                                         limit_tuples);
    5392             :             else
    5393        4678 :                 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      111738 :         if (!equal(sorted_path->pathtarget->exprs, target->exprs))
    5406         294 :             sorted_path = apply_projection_to_path(root, ordered_rel,
    5407             :                                                    sorted_path, target);
    5408             : 
    5409      111738 :         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       74036 :     if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL &&
    5424       51374 :         input_rel->partial_pathlist != NIL)
    5425             :     {
    5426             :         Path       *cheapest_partial_path;
    5427             : 
    5428        2686 :         cheapest_partial_path = linitial(input_rel->partial_pathlist);
    5429             : 
    5430        5578 :         foreach(lc, input_rel->partial_pathlist)
    5431             :         {
    5432        2892 :             Path       *input_path = (Path *) lfirst(lc);
    5433             :             Path       *sorted_path;
    5434             :             bool        is_sorted;
    5435             :             int         presorted_keys;
    5436             :             double      total_groups;
    5437             : 
    5438        2892 :             is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
    5439             :                                                     input_path->pathkeys,
    5440             :                                                     &presorted_keys);
    5441             : 
    5442        2892 :             if (is_sorted)
    5443         182 :                 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        2710 :             if (input_path != cheapest_partial_path &&
    5453          42 :                 (presorted_keys == 0 || !enable_incremental_sort))
    5454           0 :                 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        2710 :             if (presorted_keys == 0 || !enable_incremental_sort)
    5462        2650 :                 sorted_path = (Path *) create_sort_path(root,
    5463             :                                                         ordered_rel,
    5464             :                                                         input_path,
    5465             :                                                         root->sort_pathkeys,
    5466             :                                                         limit_tuples);
    5467             :             else
    5468          60 :                 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        2710 :             total_groups = compute_gather_rows(sorted_path);
    5475             :             sorted_path = (Path *)
    5476        2710 :                 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        2710 :             if (!equal(sorted_path->pathtarget->exprs, target->exprs))
    5487           6 :                 sorted_path = apply_projection_to_path(root, ordered_rel,
    5488             :                                                        sorted_path, target);
    5489             : 
    5490        2710 :             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       74036 :     if (ordered_rel->fdwroutine &&
    5499         384 :         ordered_rel->fdwroutine->GetForeignUpperPaths)
    5500         370 :         ordered_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_ORDERED,
    5501             :                                                       input_rel, ordered_rel,
    5502             :                                                       NULL);
    5503             : 
    5504             :     /* Let extensions possibly add some more paths */
    5505       74036 :     if (create_upper_paths_hook)
    5506           0 :         (*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       74036 :     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             :  */
    5546             : static PathTarget *
    5547       39284 : make_group_input_target(PlannerInfo *root, PathTarget *final_target)
    5548             : {
    5549       39284 :     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       39284 :     input_target = create_empty_pathtarget();
    5561       39284 :     non_group_cols = NIL;
    5562             : 
    5563       39284 :     i = 0;
    5564       96390 :     foreach(lc, final_target->exprs)
    5565             :     {
    5566       57106 :         Expr       *expr = (Expr *) lfirst(lc);
    5567       57106 :         Index       sgref = get_pathtarget_sortgroupref(final_target, i);
    5568             : 
    5569       66238 :         if (sgref && root->processed_groupClause &&
    5570        9132 :             get_sortgroupref_clause_noerr(sgref,
    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        7376 :             if (parse->hasGroupRTE && parse->groupingSets != NIL)
    5581             :             {
    5582             :                 Assert(root->group_rtindex > 0);
    5583             :                 expr = (Expr *)
    5584        1896 :                     remove_nulling_relids((Node *) expr,
    5585        1896 :                                           bms_make_singleton(root->group_rtindex),
    5586             :                                           NULL);
    5587             :             }
    5588        7376 :             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       49730 :             non_group_cols = lappend(non_group_cols, expr);
    5597             :         }
    5598             : 
    5599       57106 :         i++;
    5600             :     }
    5601             : 
    5602             :     /*
    5603             :      * If there's a HAVING clause, we'll need the Vars it uses, too.
    5604             :      */
    5605       39284 :     if (parse->havingQual)
    5606         956 :         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       39284 :     non_group_vars = pull_var_clause((Node *) non_group_cols,
    5621             :                                      PVC_RECURSE_AGGREGATES |
    5622             :                                      PVC_RECURSE_WINDOWFUNCS |
    5623             :                                      PVC_INCLUDE_PLACEHOLDERS);
    5624       39284 :     if (parse->hasGroupRTE && parse->groupingSets != NIL)
    5625             :     {
    5626             :         Assert(root->group_rtindex > 0);
    5627             :         non_group_vars = (List *)
    5628         866 :             remove_nulling_relids((Node *) non_group_vars,
    5629         866 :                                   bms_make_singleton(root->group_rtindex),
    5630             :                                   NULL);
    5631             :     }
    5632       39284 :     add_new_columns_to_pathtarget(input_target, non_group_vars);
    5633             : 
    5634             :     /* clean up cruft */
    5635       39284 :     list_free(non_group_vars);
    5636       39284 :     list_free(non_group_cols);
    5637             : 
    5638             :     /* XXX this causes some redundant cost calculation ... */
    5639       39284 :     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             :  */
    5658             : static PathTarget *
    5659        3094 : make_partial_grouping_target(PlannerInfo *root,
    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        3094 :     partial_target = create_empty_pathtarget();
    5670        3094 :     non_group_cols = NIL;
    5671             : 
    5672        3094 :     i = 0;
    5673       11218 :     foreach(lc, grouping_target->exprs)
    5674             :     {
    5675        8124 :         Expr       *expr = (Expr *) lfirst(lc);
    5676        8124 :         Index       sgref = get_pathtarget_sortgroupref(grouping_target, i);
    5677             : 
    5678       12888 :         if (sgref && root->processed_groupClause &&
    5679        4764 :             get_sortgroupref_clause_noerr(sgref,
    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        2822 :             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        5302 :             non_group_cols = lappend(non_group_cols, expr);
    5695             :         }
    5696             : 
    5697        8124 :         i++;
    5698             :     }
    5699             : 
    5700             :     /*
    5701             :      * If there's a HAVING clause, we'll need the Vars/Aggrefs it uses, too.
    5702             :      */
    5703        3094 :     if (havingQual)
    5704         878 :         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        3094 :     non_group_exprs = pull_var_clause((Node *) non_group_cols,
    5714             :                                       PVC_INCLUDE_AGGREGATES |
    5715             :                                       PVC_RECURSE_WINDOWFUNCS |
    5716             :                                       PVC_INCLUDE_PLACEHOLDERS);
    5717             : 
    5718        3094 :     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       11814 :     foreach(lc, partial_target->exprs)
    5726             :     {
    5727        8720 :         Aggref     *aggref = (Aggref *) lfirst(lc);
    5728             : 
    5729        8720 :         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        5868 :             newaggref = makeNode(Aggref);
    5738        5868 :             memcpy(newaggref, aggref, sizeof(Aggref));
    5739             : 
    5740             :             /* For now, assume serialization is required */
    5741        5868 :             mark_partial_aggref(newaggref, AGGSPLIT_INITIAL_SERIAL);
    5742             : 
    5743        5868 :             lfirst(lc) = newaggref;
    5744             :         }
    5745             :     }
    5746             : 
    5747             :     /* clean up cruft */
    5748        3094 :     list_free(non_group_exprs);
    5749        3094 :     list_free(non_group_cols);
    5750             : 
    5751             :     /* XXX this causes some redundant cost calculation ... */
    5752        3094 :     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             :  */
    5761             : void
    5762       17692 : mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
    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       17692 :     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       17692 :     if (DO_AGGSPLIT_SKIPFINAL(aggsplit))
    5778             :     {
    5779       15446 :         if (agg->aggtranstype == INTERNALOID && DO_AGGSPLIT_SERIALIZE(aggsplit))
    5780         314 :             agg->aggtype = BYTEAOID;
    5781             :         else
    5782       15132 :             agg->aggtype = agg->aggtranstype;
    5783             :     }
    5784       17692 : }
    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             :  */
    5796             : static List *
    5797        6096 : postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
    5798             : {
    5799             :     ListCell   *l;
    5800        6096 :     ListCell   *orig_tlist_item = list_head(orig_tlist);
    5801             : 
    5802       23370 :     foreach(l, new_tlist)
    5803             :     {
    5804       17274 :         TargetEntry *new_tle = lfirst_node(TargetEntry, l);
    5805             :         TargetEntry *orig_tle;
    5806             : 
    5807             :         /* ignore resjunk columns in setop result */
    5808       17274 :         if (new_tle->resjunk)
    5809           0 :             continue;
    5810             : 
    5811             :         Assert(orig_tlist_item != NULL);
    5812       17274 :         orig_tle = lfirst_node(TargetEntry, orig_tlist_item);
    5813       17274 :         orig_tlist_item = lnext(orig_tlist, orig_tlist_item);
    5814       17274 :         if (orig_tle->resjunk)   /* should not happen */
    5815           0 :             elog(ERROR, "resjunk output columns are not implemented");
    5816             :         Assert(new_tle->resno == orig_tle->resno);
    5817       17274 :         new_tle->ressortgroupref = orig_tle->ressortgroupref;
    5818             :     }
    5819        6096 :     if (orig_tlist_item != NULL)
    5820           0 :         elog(ERROR, "resjunk output columns are not implemented");
    5821        6096 :     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             :  */
    5833             : static void
    5834        2564 : optimize_window_clauses(PlannerInfo *root, WindowFuncLists *wflists)
    5835             : {
    5836        2564 :     List       *windowClause = root->parse->windowClause;
    5837             :     ListCell   *lc;
    5838             : 
    5839        5374 :     foreach(lc, windowClause)
    5840             :     {
    5841        2810 :         WindowClause *wc = lfirst_node(WindowClause, lc);
    5842             :         ListCell   *lc2;
    5843        2810 :         int         optimizedFrameOptions = 0;
    5844             : 
    5845             :         Assert(wc->winref <= wflists->maxWinRef);
    5846             : 
    5847             :         /* skip any WindowClauses that have no WindowFuncs */
    5848        2810 :         if (wflists->windowFuncs[wc->winref] == NIL)
    5849          24 :             continue;
    5850             : 
    5851        3440 :         foreach(lc2, wflists->windowFuncs[wc->winref])
    5852             :         {
    5853             :             SupportRequestOptimizeWindowClause req;
    5854             :             SupportRequestOptimizeWindowClause *res;
    5855        2828 :             WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
    5856             :             Oid         prosupport;
    5857             : 
    5858        2828 :             prosupport = get_func_support(wfunc->winfnoid);
    5859             : 
    5860             :             /* Check if there's a support function for 'wfunc' */
    5861        2828 :             if (!OidIsValid(prosupport))
    5862        2174 :                 break;          /* can't optimize this WindowClause */
    5863             : 
    5864         880 :             req.type = T_SupportRequestOptimizeWindowClause;
    5865         880 :             req.window_clause = wc;
    5866         880 :             req.window_func = wfunc;
    5867         880 :             req.frameOptions = wc->frameOptions;
    5868             : 
    5869             :             /* call the support function */
    5870             :             res = (SupportRequestOptimizeWindowClause *)
    5871         880 :                 DatumGetPointer(OidFunctionCall1(prosupport,
    5872             :                                                  PointerGetDatum(&req)));
    5873             : 
    5874             :             /*
    5875             :              * Skip to next WindowClause if the support function does not
    5876             :              * support this request type.
    5877             :              */
    5878         880 :             if (res == NULL)
    5879         226 :                 break;
    5880             : 
    5881             :             /*
    5882             :              * Save these frameOptions for the first WindowFunc for this
    5883             :              * WindowClause.
    5884             :              */
    5885         654 :             if (foreach_current_index(lc2) == 0)
    5886         630 :                 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          24 :             else if (optimizedFrameOptions != res->frameOptions)
    5894           0 :                 break;          /* skip to the next WindowClause, if any */
    5895             :         }
    5896             : 
    5897             :         /* adjust the frameOptions if all WindowFunc's agree that it's ok */
    5898        2786 :         if (lc2 == NULL && wc->frameOptions != optimizedFrameOptions)
    5899             :         {
    5900             :             ListCell   *lc3;
    5901             : 
    5902             :             /* apply the new frame options */
    5903         612 :             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         612 :             if (list_length(windowClause) == 1)
    5912         522 :                 continue;
    5913             : 
    5914             :             /*
    5915             :              * Do the duplicate check and reuse the existing WindowClause if
    5916             :              * we find a duplicate.
    5917             :              */
    5918         228 :             foreach(lc3, windowClause)
    5919             :             {
    5920         174 :                 WindowClause *existing_wc = lfirst_node(WindowClause, lc3);
    5921             : 
    5922             :                 /* skip over the WindowClause we're currently editing */
    5923         174 :                 if (existing_wc == wc)
    5924          54 :                     continue;
    5925             : 
    5926             :                 /*
    5927             :                  * Perform the same duplicate check that is done in
    5928             :                  * transformWindowFuncCall.
    5929             :                  */
    5930         240 :                 if (equal(wc->partitionClause, existing_wc->partitionClause) &&
    5931         120 :                     equal(wc->orderClause, existing_wc->orderClause) &&
    5932         120 :                     wc->frameOptions == existing_wc->frameOptions &&
    5933          72 :                     equal(wc->startOffset, existing_wc->startOffset) &&
    5934          36 :                     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          78 :                     foreach(lc4, wflists->windowFuncs[wc->winref])
    5945             :                     {
    5946          42 :                         WindowFunc *wfunc = lfirst_node(WindowFunc, lc4);
    5947             : 
    5948          42 :                         wfunc->winref = existing_wc->winref;
    5949             :                     }
    5950             : 
    5951             :                     /* move list items */
    5952          72 :                     wflists->windowFuncs[existing_wc->winref] = list_concat(wflists->windowFuncs[existing_wc->winref],
    5953          36 :                                                                             wflists->windowFuncs[wc->winref]);
    5954          36 :                     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          36 :                     break;
    5962             :                 }
    5963             :             }
    5964             :         }
    5965             :     }
    5966        2564 : }
    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             :  */
    5973             : static List *
    5974        2564 : select_active_windows(PlannerInfo *root, WindowFuncLists *wflists)
    5975             : {
    5976        2564 :     List       *windowClause = root->parse->windowClause;
    5977        2564 :     List       *result = NIL;
    5978             :     ListCell   *lc;
    5979        2564 :     int         nActive = 0;
    5980        2564 :     WindowClauseSortData *actives = palloc(sizeof(WindowClauseSortData)
    5981        2564 :                                            * list_length(windowClause));
    5982             : 
    5983             :     /* First, construct an array of the active windows */
    5984        5374 :     foreach(lc, windowClause)
    5985             :     {
    5986        2810 :         WindowClause *wc = lfirst_node(WindowClause, lc);
    5987             : 
    5988             :         /* It's only active if wflists shows some related WindowFuncs */
    5989             :         Assert(wc->winref <= wflists->maxWinRef);
    5990        2810 :         if (wflists->windowFuncs[wc->winref] == NIL)
    5991          60 :             continue;
    5992             : 
    5993        2750 :         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        5500 :         actives[nActive].uniqueOrder =
    6009        2750 :             list_concat_unique(list_copy(wc->partitionClause),
    6010        2750 :                                wc->orderClause);
    6011        2750 :         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        2564 :     qsort(actives, nActive, sizeof(WindowClauseSortData), common_prefix_cmp);
    6035             : 
    6036             :     /* build ordered list of the original WindowClause nodes */
    6037        5314 :     for (int i = 0; i < nActive; i++)
    6038        2750 :         result = lappend(result, actives[i].wc);
    6039             : 
    6040        2564 :     pfree(actives);
    6041             : 
    6042        2564 :     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             :  */
    6056             : static void
    6057        2564 : name_active_windows(List *activeWindows)
    6058             : {
    6059        2564 :     int         next_n = 1;
    6060             :     char        newname[16];
    6061             :     ListCell   *lc;
    6062             : 
    6063        5314 :     foreach(lc, activeWindows)
    6064             :     {
    6065        2750 :         WindowClause *wc = lfirst_node(WindowClause, lc);
    6066             : 
    6067             :         /* Nothing to do if it has a name already. */
    6068        2750 :         if (wc->name)
    6069         576 :             continue;
    6070             : 
    6071             :         /* Select a name not currently present in the list. */
    6072             :         for (;;)
    6073           6 :         {
    6074             :             ListCell   *lc2;
    6075             : 
    6076        2180 :             snprintf(newname, sizeof(newname), "w%d", next_n++);
    6077        4708 :             foreach(lc2, activeWindows)
    6078             :             {
    6079        2534 :                 WindowClause *wc2 = lfirst_node(WindowClause, lc2);
    6080             : 
    6081        2534 :                 if (wc2->name && strcmp(wc2->name, newname) == 0)
    6082           6 :                     break;      /* matched */
    6083             :             }
    6084        2180 :             if (lc2 == NULL)
    6085        2174 :                 break;          /* reached the end with no match */
    6086             :         }
    6087        2174 :         wc->name = pstrdup(newname);
    6088             :     }
    6089        2564 : }
    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             :  */
    6107             : static int
    6108         204 : common_prefix_cmp(const void *a, const void *b)
    6109             : {
    6110         204 :     const WindowClauseSortData *wcsa = a;
    6111         204 :     const WindowClauseSortData *wcsb = b;
    6112             :     ListCell   *item_a;
    6113             :     ListCell   *item_b;
    6114             : 
    6115         366 :     forboth(item_a, wcsa->uniqueOrder, item_b, wcsb->uniqueOrder)
    6116             :     {
    6117         264 :         SortGroupClause *sca = lfirst_node(SortGroupClause, item_a);
    6118         264 :         SortGroupClause *scb = lfirst_node(SortGroupClause, item_b);
    6119             : 
    6120         264 :         if (sca->tleSortGroupRef > scb->tleSortGroupRef)
    6121         102 :             return -1;
    6122         252 :         else if (sca->tleSortGroupRef < scb->tleSortGroupRef)
    6123          66 :             return 1;
    6124         186 :         else if (sca->sortop > scb->sortop)
    6125           0 :             return -1;
    6126         186 :         else if (sca->sortop < scb->sortop)
    6127          24 :             return 1;
    6128         162 :         else if (sca->nulls_first && !scb->nulls_first)
    6129           0 :             return -1;
    6130         162 :         else if (!sca->nulls_first && scb->nulls_first)
    6131           0 :             return 1;
    6132             :         /* no need to compare eqop, since it is fully determined by sortop */
    6133             :     }
    6134             : 
    6135         102 :     if (list_length(wcsa->uniqueOrder) > list_length(wcsb->uniqueOrder))
    6136           6 :         return -1;
    6137          96 :     else if (list_length(wcsa->uniqueOrder) < list_length(wcsb->uniqueOrder))
    6138          30 :         return 1;
    6139             : 
    6140          66 :     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             :  */
    6176             : static PathTarget *
    6177        2564 : make_window_input_target(PlannerInfo *root,
    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        2564 :     sgrefs = NULL;
    6195        5314 :     foreach(lc, activeWindows)
    6196             :     {
    6197        2750 :         WindowClause *wc = lfirst_node(WindowClause, lc);
    6198             :         ListCell   *lc2;
    6199             : 
    6200        3506 :         foreach(lc2, wc->partitionClause)
    6201             :         {
    6202         756 :             SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
    6203             : 
    6204         756 :             sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
    6205             :         }
    6206        5010 :         foreach(lc2, wc->orderClause)
    6207             :         {
    6208        2260 :             SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
    6209             : 
    6210        2260 :             sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
    6211             :         }
    6212             :     }
    6213             : 
    6214             :     /* Add in sortgroupref numbers of GROUP BY clauses, too */
    6215        2756 :     foreach(lc, root->processed_groupClause)
    6216             :     {
    6217         192 :         SortGroupClause *grpcl = lfirst_node(SortGroupClause, lc);
    6218             : 
    6219         192 :         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        2564 :     input_target = create_empty_pathtarget();
    6227        2564 :     flattenable_cols = NIL;
    6228             : 
    6229        2564 :     i = 0;
    6230       10892 :     foreach(lc, final_target->exprs)
    6231             :     {
    6232        8328 :         Expr       *expr = (Expr *) lfirst(lc);
    6233        8328 :         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        8328 :         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        2852 :             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        5476 :             flattenable_cols = lappend(flattenable_cols, expr);
    6255             :         }
    6256             : 
    6257        8328 :         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        2564 :     flattenable_vars = pull_var_clause((Node *) flattenable_cols,
    6271             :                                        PVC_INCLUDE_AGGREGATES |
    6272             :                                        PVC_RECURSE_WINDOWFUNCS |
    6273             :                                        PVC_INCLUDE_PLACEHOLDERS);
    6274        2564 :     add_new_columns_to_pathtarget(input_target, flattenable_vars);
    6275             : 
    6276             :     /* clean up cruft */
    6277        2564 :     list_free(flattenable_vars);
    6278        2564 :     list_free(flattenable_cols);
    6279             : 
    6280             :     /* XXX this causes some redundant cost calculation ... */
    6281        2564 :     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             :  */
    6296             : static List *
    6297        5528 : make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
    6298             :                          List *tlist)
    6299             : {
    6300        5528 :     List       *window_pathkeys = NIL;
    6301             : 
    6302             :     /* Throw error if can't sort */
    6303        5528 :     if (!grouping_is_sortable(wc->partitionClause))
    6304           0 :         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.")));
    6308        5528 :     if (!grouping_is_sortable(wc->orderClause))
    6309           0 :         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        5528 :     if (wc->partitionClause != NIL)
    6319             :     {
    6320             :         bool        sortable;
    6321             : 
    6322        1320 :         window_pathkeys = make_pathkeys_for_sortclauses_extended(root,
    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        5528 :     if (wc->orderClause != NIL)
    6341             :     {
    6342             :         List       *orderby_pathkeys;
    6343             : 
    6344        4426 :         orderby_pathkeys = make_pathkeys_for_sortclauses(root,
    6345             :                                                          wc->orderClause,
    6346             :                                                          tlist);
    6347             : 
    6348             :         /* Okay, make the combined pathkeys */
    6349        4426 :         if (window_pathkeys != NIL)
    6350         946 :             window_pathkeys = append_pathkeys(window_pathkeys, orderby_pathkeys);
    6351             :         else
    6352        3480 :             window_pathkeys = orderby_pathkeys;
    6353             :     }
    6354             : 
    6355        5528 :     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             :  */
    6424             : static PathTarget *
    6425       70148 : make_sort_input_target(PlannerInfo *root,
    6426             :                        PathTarget *final_target,
    6427             :                        bool *have_postponed_srfs)
    6428             : {
    6429       70148 :     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       70148 :     *have_postponed_srfs = false;   /* default result */
    6448             : 
    6449             :     /* Inspect tlist and collect per-column information */
    6450       70148 :     ncols = list_length(final_target->exprs);
    6451       70148 :     col_is_srf = (bool *) palloc0(ncols * sizeof(bool));
    6452       70148 :     postpone_col = (bool *) palloc0(ncols * sizeof(bool));
    6453       70148 :     have_srf = have_volatile = have_expensive = have_srf_sortcols = false;
    6454             : 
    6455       70148 :     i = 0;
    6456      423292 :     foreach(lc, final_target->exprs)
    6457             :     {
    6458      353144 :         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      353144 :         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      254504 :             if (parse->hasTargetSRFs &&
    6475         216 :                 expression_returns_set((Node *) expr))
    6476             :             {
    6477             :                 /* We'll decide below whether these are postponable */
    6478          96 :                 col_is_srf[i] = true;
    6479          96 :                 have_srf = true;
    6480             :             }
    6481      254192 :             else if (contain_volatile_functions((Node *) expr))
    6482             :             {
    6483             :                 /* Unconditionally postpone */
    6484         148 :                 postpone_col[i] = true;
    6485         148 :                 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      254044 :                 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      254044 :                 if (cost.per_tuple > 10 * cpu_operator_cost)
    6504             :                 {
    6505       16512 :                     postpone_col[i] = true;
    6506       16512 :                     have_expensive = true;
    6507             :                 }
    6508             :             }
    6509             :         }
    6510             :         else
    6511             :         {
    6512             :             /* For sortgroupref cols, just check if any contain SRFs */
    6513       98856 :             if (!have_srf_sortcols &&
    6514       99166 :                 parse->hasTargetSRFs &&
    6515         310 :                 expression_returns_set((Node *) expr))
    6516         124 :                 have_srf_sortcols = true;
    6517             :         }
    6518             : 
    6519      353144 :         i++;
    6520             :     }
    6521             : 
    6522             :     /*
    6523             :      * We can postpone SRFs if we have some but none are in sortgroupref cols.
    6524             :      */
    6525       70148 :     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       70148 :     if (!(postpone_srfs || have_volatile ||
    6531       69944 :           (have_expensive &&
    6532        9708 :            (parse->limitCount || root->tuple_fraction > 0))))
    6533       69908 :         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         240 :     *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         240 :     input_target = create_empty_pathtarget();
    6549         240 :     postponable_cols = NIL;
    6550             : 
    6551         240 :     i = 0;
    6552        1990 :     foreach(lc, final_target->exprs)
    6553             :     {
    6554        1750 :         Expr       *expr = (Expr *) lfirst(lc);
    6555             : 
    6556        1750 :         if (postpone_col[i] || (postpone_srfs && col_is_srf[i]))
    6557         298 :             postponable_cols = lappend(postponable_cols, expr);
    6558             :         else
    6559        1452 :             add_column_to_pathtarget(input_target, expr,
    6560        1452 :                                      get_pathtarget_sortgroupref(final_target, i));
    6561             : 
    6562        1750 :         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         240 :     postponable_vars = pull_var_clause((Node *) postponable_cols,
    6573             :                                        PVC_INCLUDE_AGGREGATES |
    6574             :                                        PVC_INCLUDE_WINDOWFUNCS |
    6575             :                                        PVC_INCLUDE_PLACEHOLDERS);
    6576         240 :     add_new_columns_to_pathtarget(input_target, postponable_vars);
    6577             : 
    6578             :     /* clean up cruft */
    6579         240 :     list_free(postponable_vars);
    6580         240 :     list_free(postponable_cols);
    6581             : 
    6582             :     /* XXX this represents even more redundant cost calculation ... */
    6583         240 :     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             :  */
    6600             : Path *
    6601      486364 : get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
    6602             : {
    6603      486364 :     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      486364 :     if (tuple_fraction <= 0.0)
    6608      476972 :         return best_path;
    6609             : 
    6610             :     /* Convert absolute # of tuples to a fraction; no need to clamp to 0..1 */
    6611        9392 :     if (tuple_fraction >= 1.0 && best_path->rows > 0)
    6612        3806 :         tuple_fraction /= best_path->rows;
    6613             : 
    6614       24862 :     foreach(l, rel->pathlist)
    6615             :     {
    6616       15470 :         Path       *path = (Path *) lfirst(l);
    6617             : 
    6618       15470 :         if (path->param_info)
    6619         200 :             continue;
    6620             : 
    6621       21148 :         if (path == rel->cheapest_total_path ||
    6622        5878 :             compare_fractional_path_costs(best_path, path, tuple_fraction) <= 0)
    6623       14754 :             continue;
    6624             : 
    6625         516 :         best_path = path;
    6626             :     }
    6627             : 
    6628        9392 :     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             :  */
    6646             : static void
    6647       12344 : adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel,
    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       12344 :     if (list_length(targets) == 1)
    6657         640 :         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       23432 :     foreach(lc, rel->pathlist)
    6670             :     {
    6671       11728 :         Path       *subpath = (Path *) lfirst(lc);
    6672       11728 :         Path       *newpath = subpath;
    6673             :         ListCell   *lc1,
    6674             :                    *lc2;
    6675             : 
    6676             :         Assert(subpath->param_info == NULL);
    6677       36372 :         forboth(lc1, targets, lc2, targets_contain_srfs)
    6678             :         {
    6679       24644 :             PathTarget *thistarget = lfirst_node(PathTarget, lc1);
    6680       24644 :             bool        contains_srfs = (bool) lfirst_int(lc2);
    6681             : 
    6682             :             /* If this level doesn't contain SRFs, do regular projection */
    6683       24644 :             if (contains_srfs)
    6684       11788 :                 newpath = (Path *) create_set_projection_path(root,
    6685             :                                                               rel,
    6686             :                                                               newpath,
    6687             :                                                               thistarget);
    6688             :             else
    6689       12856 :                 newpath = (Path *) apply_projection_to_path(root,
    6690             :                                                             rel,
    6691             :                                                             newpath,
    6692             :                                                             thistarget);
    6693             :         }
    6694       11728 :         lfirst(lc) = newpath;
    6695       11728 :         if (subpath == rel->cheapest_startup_path)
    6696         384 :             rel->cheapest_startup_path = newpath;
    6697       11728 :         if (subpath == rel->cheapest_total_path)
    6698         384 :             rel->cheapest_total_path = newpath;
    6699             :     }
    6700             : 
    6701             :     /* Likewise for partial paths, if any */
    6702       11710 :     foreach(lc, rel->partial_pathlist)
    6703             :     {
    6704           6 :         Path       *subpath = (Path *) lfirst(lc);
    6705           6 :         Path       *newpath = subpath;
    6706             :         ListCell   *lc1,
    6707             :                    *lc2;
    6708             : 
    6709             :         Assert(subpath->param_info == NULL);
    6710          24 :         forboth(lc1, targets, lc2, targets_contain_srfs)
    6711             :         {
    6712          18 :             PathTarget *thistarget = lfirst_node(PathTarget, lc1);
    6713          18 :             bool        contains_srfs = (bool) lfirst_int(lc2);
    6714             : 
    6715             :             /* If this level doesn't contain SRFs, do regular projection */
    6716          18 :             if (contains_srfs)
    6717           6 :                 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          12 :                 newpath = (Path *) create_projection_path(root,
    6725             :                                                           rel,
    6726             :                                                           newpath,
    6727             :                                                           thistarget);
    6728             :             }
    6729             :         }
    6730           6 :         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             :  */
    6762             : Expr *
    6763      240670 : expression_planner(Expr *expr)
    6764             : {
    6765             :     Node       *result;
    6766             : 
    6767             :     /*
    6768             :      * Convert named-argument function calls, insert default arguments and
    6769             :      * simplify constant subexprs
    6770             :      */
    6771      240670 :     result = eval_const_expressions(NULL, (Node *) expr);
    6772             : 
    6773             :     /* Fill in opfuncid values if missing */
    6774      240652 :     fix_opfuncids(result);
    6775             : 
    6776      240652 :     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             :  */
    6789             : Expr *
    6790         346 : expression_planner_with_deps(Expr *expr,
    6791             :                              List **relationOids,
    6792             :                              List **invalItems)
    6793             : {
    6794             :     Node       *result;
    6795             :     PlannerGlobal glob;
    6796             :     PlannerInfo root;
    6797             : 
    6798             :     /* Make up dummy planner state so we can use setrefs machinery */
    6799        8996 :     MemSet(&glob, 0, sizeof(glob));
    6800         346 :     glob.type = T_PlannerGlobal;
    6801         346 :     glob.relationOids = NIL;
    6802         346 :     glob.invalItems = NIL;
    6803             : 
    6804       32178 :     MemSet(&root, 0, sizeof(root));
    6805         346 :     root.type = T_PlannerInfo;
    6806         346 :     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         346 :     result = eval_const_expressions(&root, (Node *) expr);
    6814             : 
    6815             :     /* Fill in opfuncid values if missing */
    6816         346 :     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         346 :     (void) extract_query_dependencies_walker(result, &root);
    6823             : 
    6824         346 :     *relationOids = glob.relationOids;
    6825         346 :     *invalItems = glob.invalItems;
    6826             : 
    6827         346 :     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             :  */
    6842             : bool
    6843         188 : plan_cluster_use_sort(Oid tableOid, Oid indexOid)
    6844             : {
    6845             :     PlannerInfo *root;
    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         188 :     if (!enable_indexscan)
    6860          30 :         return true;            /* use sort */
    6861             : 
    6862             :     /* Set up mostly-dummy planner state */
    6863         158 :     query = makeNode(Query);
    6864         158 :     query->commandType = CMD_SELECT;
    6865             : 
    6866         158 :     glob = makeNode(PlannerGlobal);
    6867             : 
    6868         158 :     root = makeNode(PlannerInfo);
    6869         158 :     root->parse = query;
    6870         158 :     root->glob = glob;
    6871         158 :     root->query_level = 1;
    6872         158 :     root->planner_cxt = CurrentMemoryContext;
    6873         158 :     root->wt_param_id = -1;
    6874         158 :     root->join_domains = list_make1(makeNode(JoinDomain));
    6875             : 
    6876             :     /* Build a minimal RTE for the rel */
    6877         158 :     rte = makeNode(RangeTblEntry);
    6878         158 :     rte->rtekind = RTE_RELATION;
    6879         158 :     rte->relid = tableOid;
    6880         158 :     rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
    6881         158 :     rte->rellockmode = AccessShareLock;
    6882         158 :     rte->lateral = false;
    6883         158 :     rte->inh = false;
    6884         158 :     rte->inFromCl = true;
    6885         158 :     query->rtable = list_make1(rte);
    6886         158 :     addRTEPermissionInfo(&query->rteperminfos, rte);
    6887             : 
    6888             :     /* Set up RTE/RelOptInfo arrays */
    6889         158 :     setup_simple_rel_arrays(root);
    6890             : 
    6891             :     /* Build RelOptInfo */
    6892         158 :     rel = build_simple_rel(root, 1, NULL);
    6893             : 
    6894             :     /* Locate IndexOptInfo for the target index */
    6895         158 :     indexInfo = NULL;
    6896         196 :     foreach(lc, rel->indexlist)
    6897             :     {
    6898         196 :         indexInfo = lfirst_node(IndexOptInfo, lc);
    6899         196 :         if (indexInfo->indexoid == indexOid)
    6900         158 :             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         158 :     if (lc == NULL)             /* not in the list? */
    6911           0 :         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         158 :     rel->rows = rel->tuples;
    6918         158 :     rel->reltarget->width = get_relation_data_width(tableOid, NULL);
    6919             : 
    6920         158 :     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         158 :     cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
    6929         158 :     comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
    6930             : 
    6931             :     /* Estimate the cost of seq scan + sort */
    6932         158 :     seqScanPath = create_seqscan_path(root, rel, NULL, 0);
    6933         158 :     cost_sort(&seqScanAndSortPath, root, NIL,
    6934             :               seqScanPath->disabled_nodes,
    6935         158 :               seqScanPath->total_cost, rel->tuples, rel->reltarget->width,
    6936             :               comparisonCost, maintenance_work_mem, -1.0);
    6937             : 
    6938             :     /* Estimate the cost of index scan */
    6939         158 :     indexScanPath = create_index_path(root, indexInfo,
    6940             :                                       NIL, NIL, NIL, NIL,
    6941             :                                       ForwardScanDirection, false,
    6942             :                                       NULL, 1.0, false);
    6943             : 
    6944         158 :     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             :  */
    6964             : int
    6965       35256 : plan_create_index_workers(Oid tableOid, Oid indexOid)
    6966             : {
    6967             :     PlannerInfo *root;
    6968             :     Query      *query;
    6969             :     PlannerGlobal *glob;
    6970             :     RangeTblEntry *rte;
    6971             :     Relation    heap;
    6972             :     Relation    index;
    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             :      */
    6983       35256 :     if (!IsUnderPostmaster || max_parallel_maintenance_workers == 0)
    6984         506 :         return 0;
    6985             : 
    6986             :     /* Set up largely-dummy planner state */
    6987       34750 :     query = makeNode(Query);
    6988       34750 :     query->commandType = CMD_SELECT;
    6989             : 
    6990       34750 :     glob = makeNode(PlannerGlobal);
    6991             : 
    6992       34750 :     root = makeNode(PlannerInfo);
    6993       34750 :     root->parse = query;
    6994       34750 :     root->glob = glob;
    6995       34750 :     root->query_level = 1;
    6996       34750 :     root->planner_cxt = CurrentMemoryContext;
    6997       34750 :     root->wt_param_id = -1;
    6998       34750 :     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       34750 :     rte = makeNode(RangeTblEntry);
    7009       34750 :     rte->rtekind = RTE_RELATION;
    7010       34750 :     rte->relid = tableOid;
    7011       34750 :     rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
    7012       34750 :     rte->rellockmode = AccessShareLock;
    7013       34750 :     rte->lateral = false;
    7014       34750 :     rte->inh = true;
    7015       34750 :     rte->inFromCl = true;
    7016       34750 :     query->rtable = list_make1(rte);
    7017       34750 :     addRTEPermissionInfo(&query->rteperminfos, rte);
    7018             : 
    7019             :     /* Set up RTE/RelOptInfo arrays */
    7020       34750 :     setup_simple_rel_arrays(root);
    7021             : 
    7022             :     /* Build RelOptInfo */
    7023       34750 :     rel = build_simple_rel(root, 1, NULL);
    7024             : 
    7025             :     /* Rels are assumed already locked by the caller */
    7026       34750 :     heap = table_open(tableOid, NoLock);
    7027       34750 :     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       34750 :     if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
    7037       32678 :         !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
    7038       32558 :         !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
    7039             :     {
    7040        2192 :         parallel_workers = 0;
    7041        2192 :         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       32558 :     if (rel->rel_parallel_workers != -1)
    7052             :     {
    7053          20 :         parallel_workers = Min(rel->rel_parallel_workers,
    7054             :                                max_parallel_maintenance_workers);
    7055          20 :         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       32538 :     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       32538 :     parallel_workers = compute_parallel_worker(rel, heap_blocks, -1,
    7069             :                                                max_parallel_maintenance_workers);
    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       32694 :     while (parallel_workers > 0 &&
    7082         314 :            maintenance_work_mem / (parallel_workers + 1) < 32 * 1024)
    7083         156 :         parallel_workers--;
    7084             : 
    7085       32538 : done:
    7086       34750 :     index_close(index, NoLock);
    7087       34750 :     table_close(heap, NoLock);
    7088             : 
    7089       34750 :     return parallel_workers;
    7090             : }
    7091             : 
    7092             : /*
    7093             :  * add_paths_to_grouping_rel
    7094             :  *
    7095             :  * Add non-partial paths to grouping relation.
    7096             :  */
    7097             : static void
    7098       40526 : add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
    7099             :                           RelOptInfo *grouped_rel,
    7100             :                           RelOptInfo *partially_grouped_rel,
    7101             :                           const AggClauseCosts *agg_costs,
    7102             :                           grouping_sets_data *gd,
    7103             :                           GroupPathExtraData *extra)
    7104             : {
    7105       40526 :     Query      *parse = root->parse;
    7106       40526 :     Path       *cheapest_path = input_rel->cheapest_total_path;
    7107       40526 :     Path       *cheapest_partially_grouped_path = NULL;
    7108             :     ListCell   *lc;
    7109       40526 :     bool        can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
    7110       40526 :     bool        can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
    7111       40526 :     List       *havingQual = (List *) extra->havingQual;
    7112       40526 :     AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
    7113       40526 :     double      dNumGroups = 0;
    7114       40526 :     double      dNumFinalGroups = 0;
    7115             : 
    7116             :     /*
    7117             :      * Estimate number of groups for non-split aggregation.
    7118             :      */
    7119       40526 :     dNumGroups = get_number_of_groups(root,
    7120             :                                       cheapest_path->rows,
    7121             :                                       gd,
    7122             :                                       extra->targetList);
    7123             : 
    7124       40526 :     if (partially_grouped_rel && partially_grouped_rel->pathlist)
    7125             :     {
    7126        2236 :         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 =
    7133        2236 :             get_number_of_groups(root,
    7134             :                                  cheapest_partially_grouped_path->rows,
    7135             :                                  gd,
    7136             :                                  extra->targetList);
    7137             :     }
    7138             : 
    7139       40526 :     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       84300 :         foreach(lc, input_rel->pathlist)
    7147             :         {
    7148             :             ListCell   *lc2;
    7149       43780 :             Path       *path = (Path *) lfirst(lc);
    7150       43780 :             Path       *path_save = path;
    7151       43780 :             List       *pathkey_orderings = NIL;
    7152             : 
    7153             :             /* generate alternative group orderings that might be useful */
    7154       43780 :             pathkey_orderings = get_useful_group_keys_orderings(root, path);
    7155             : 
    7156             :             Assert(list_length(pathkey_orderings) > 0);
    7157             : 
    7158       87704 :             foreach(lc2, pathkey_orderings)
    7159             :             {
    7160       43924 :                 GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
    7161             : 
    7162             :                 /* restore the path (we replace it in the loop) */
    7163       43924 :                 path = path_save;
    7164             : 
    7165       43924 :                 path = make_ordered_path(root,
    7166             :                                          grouped_rel,
    7167             :                                          path,
    7168             :                                          cheapest_path,
    7169             :                                          info->pathkeys,
    7170             :                                          -1.0);
    7171       43924 :                 if (path == NULL)
    7172         404 :                     continue;
    7173             : 
    7174             :                 /* Now decide what to stick atop it */
    7175       43520 :                 if (parse->groupingSets)
    7176             :                 {
    7177         992 :                     consider_groupingsets_paths(root, grouped_rel,
    7178             :                                                 path, true, can_hash,
    7179             :                                                 gd, agg_costs, dNumGroups);
    7180             :                 }
    7181       42528 :                 else if (parse->hasAggs)
    7182             :                 {
    7183             :                     /*
    7184             :                      * We have aggregation, possibly with plain GROUP BY. Make
    7185             :                      * an AggPath.
    7186             :                      */
    7187       41744 :                     add_path(grouped_rel, (Path *)
    7188       41744 :                              create_agg_path(root,
    7189             :                                              grouped_rel,
    7190             :                                              path,
    7191       41744 :                                              grouped_rel->reltarget,
    7192       41744 :                                              parse->groupClause ? AGG_SORTED : AGG_PLAIN,
    7193             :                                              AGGSPLIT_SIMPLE,
    7194             :                                              info->clauses,
    7195             :                                              havingQual,
    7196             :                                              agg_costs,
    7197             :                                              dNumGroups));
    7198             :                 }
    7199         784 :                 else if (parse->groupClause)
    7200             :                 {
    7201             :                     /*
    7202             :                      * We have GROUP BY without aggregation or grouping sets.
    7203             :                      * Make a GroupPath.
    7204             :                      */
    7205         784 :                     add_path(grouped_rel, (Path *)
    7206         784 :                              create_group_path(root,
    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       40520 :         if (partially_grouped_rel != NULL)
    7226             :         {
    7227        6154 :             foreach(lc, partially_grouped_rel->pathlist)
    7228             :             {
    7229             :                 ListCell   *lc2;
    7230        3918 :                 Path       *path = (Path *) lfirst(lc);
    7231        3918 :                 Path       *path_save = path;
    7232        3918 :                 List       *pathkey_orderings = NIL;
    7233             : 
    7234             :                 /* generate alternative group orderings that might be useful */
    7235        3918 :                 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        7836 :                 foreach(lc2, pathkey_orderings)
    7241             :                 {
    7242        3918 :                     GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
    7243             : 
    7244             :                     /* restore the path (we replace it in the loop) */
    7245        3918 :                     path = path_save;
    7246             : 
    7247        3918 :                     path = make_ordered_path(root,
    7248             :                                              grouped_rel,
    7249             :                                              path,
    7250             :                                              cheapest_partially_grouped_path,
    7251             :                                              info->pathkeys,
    7252             :                                              -1.0);
    7253             : 
    7254        3918 :                     if (path == NULL)
    7255         204 :                         continue;
    7256             : 
    7257        3714 :                     if (parse->hasAggs)
    7258        3466 :                         add_path(grouped_rel, (Path *)
    7259        3466 :                                  create_agg_path(root,
    7260             :                                                  grouped_rel,
    7261             :                                                  path,
    7262        3466 :                                                  grouped_rel->reltarget,
    7263        3466 :                                                  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
    7264             :                                                  AGGSPLIT_FINAL_DESERIAL,
    7265             :                                                  info->clauses,
    7266             :                                                  havingQual,
    7267             :                                                  agg_final_costs,
    7268             :                                                  dNumFinalGroups));
    7269             :                     else
    7270         248 :                         add_path(grouped_rel, (Path *)
    7271         248 :                                  create_group_path(root,
    7272             :                                                    grouped_rel,
    7273             :                                                    path,
    7274             :                                                    info->clauses,
    7275             :                                                    havingQual,
    7276             :                                                    dNumFinalGroups));
    7277             : 
    7278             :                 }
    7279             :             }
    7280             :         }
    7281             :     }
    7282             : 
    7283       40526 :     if (can_hash)
    7284             :     {
    7285        5692 :         if (parse->groupingSets)
    7286             :         {
    7287             :             /*
    7288             :              * Try for a hash-only groupingsets path over unsorted input.
    7289             :              */
    7290         830 :             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        4862 :             add_path(grouped_rel, (Path *)
    7301        4862 :                      create_agg_path(root, grouped_rel,
    7302             :                                      cheapest_path,
    7303        4862 :                                      grouped_rel->reltarget,
    7304             :                                      AGG_HASHED,
    7305             :                                      AGGSPLIT_SIMPLE,
    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        5692 :         if (partially_grouped_rel && partially_grouped_rel->pathlist)
    7317             :         {
    7318        1442 :             add_path(grouped_rel, (Path *)
    7319        1442 :                      create_agg_path(root,
    7320             :                                      grouped_rel,
    7321             :                                      cheapest_partially_grouped_path,
    7322        1442 :                                      grouped_rel->reltarget,
    7323             :                                      AGG_HASHED,
    7324             :                                      AGGSPLIT_FINAL_DESERIAL,
    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       40526 :     if (grouped_rel->partial_pathlist != NIL)
    7339         318 :         gather_grouping_paths(root, grouped_rel);
    7340       40526 : }
    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             :  */
    7358             : static RelOptInfo *
    7359       36514 : create_partial_grouping_paths(PlannerInfo *root,
    7360             :                               RelOptInfo *grouped_rel,
    7361             :                               RelOptInfo *input_rel,
    7362             :                               grouping_sets_data *gd,
    7363             :                               GroupPathExtraData *extra,
    7364             :                               bool force_rel_creation)
    7365             : {
    7366       36514 :     Query      *parse = root->parse;
    7367             :     RelOptInfo *partially_grouped_rel;
    7368       36514 :     RelOptInfo *eager_agg_rel = NULL;
    7369       36514 :     AggClauseCosts *agg_partial_costs = &extra->agg_partial_costs;
    7370       36514 :     AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
    7371       36514 :     Path       *cheapest_partial_path = NULL;
    7372       36514 :     Path       *cheapest_total_path = NULL;
    7373       36514 :     double      dNumPartialGroups = 0;
    7374       36514 :     double      dNumPartialPartialGroups = 0;
    7375             :     ListCell   *lc;
    7376       36514 :     bool        can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
    7377       36514 :     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       36514 :     if (input_rel->grouped_rel &&
    7384         958 :         !IS_DUMMY_REL(input_rel->grouped_rel) &&
    7385         958 :         input_rel->grouped_rel->pathlist != NIL)
    7386         898 :         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       36514 :     if (input_rel->pathlist != NIL &&
    7396       36514 :         extra->patype == PARTITIONWISE_AGGREGATE_PARTIAL)
    7397         858 :         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       36514 :     if (grouped_rel->consider_parallel && input_rel->partial_pathlist != NIL)
    7405        2512 :         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       36514 :     if (cheapest_total_path == NULL &&
    7414       33648 :         cheapest_partial_path == NULL &&
    7415       33518 :         eager_agg_rel == NULL &&
    7416       33518 :         !force_rel_creation)
    7417       33420 :         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        3094 :     partially_grouped_rel = fetch_upper_rel(root,
    7424             :                                             UPPERREL_PARTIAL_GROUP_AGG,
    7425             :                                             grouped_rel->relids);
    7426        3094 :     partially_grouped_rel->consider_parallel =
    7427        3094 :         grouped_rel->consider_parallel;
    7428        3094 :     partially_grouped_rel->reloptkind = grouped_rel->reloptkind;
    7429        3094 :     partially_grouped_rel->serverid = grouped_rel->serverid;
    7430        3094 :     partially_grouped_rel->userid = grouped_rel->userid;
    7431        3094 :     partially_grouped_rel->useridiscurrent = grouped_rel->useridiscurrent;
    7432        3094 :     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        3094 :     partially_grouped_rel->reltarget =
    7441        3094 :         make_partial_grouping_target(root, grouped_rel->reltarget,
    7442             :                                      extra->havingQual);
    7443             : 
    7444        3094 :     if (!extra->partial_costs_set)
    7445             :     {
    7446             :         /*
    7447             :          * Collect statistics about aggregates for estimating costs of
    7448             :          * performing aggregation in parallel.
    7449             :          */
    7450        9312 :         MemSet(agg_partial_costs, 0, sizeof(AggClauseCosts));
    7451        9312 :         MemSet(agg_final_costs, 0, sizeof(AggClauseCosts));
    7452        1552 :         if (parse->hasAggs)
    7453             :         {
    7454             :             /* partial phase */
    7455        1418 :             get_agg_clause_costs(root, AGGSPLIT_INITIAL_SERIAL,
    7456             :                                  agg_partial_costs);
    7457             : 
    7458             :             /* final phase */
    7459        1418 :             get_agg_clause_costs(root, AGGSPLIT_FINAL_DESERIAL,
    7460             :                                  agg_final_costs);
    7461             :         }
    7462             : 
    7463        1552 :         extra->partial_costs_set = true;
    7464             :     }
    7465             : 
    7466             :     /* Estimate number of partial groups. */
    7467        3094 :     if (cheapest_total_path != NULL)
    7468             :         dNumPartialGroups =
    7469         858 :             get_number_of_groups(root,
    7470             :                                  cheapest_total_path->rows,
    7471             :                                  gd,
    7472             :                                  extra->targetList);
    7473        3094 :     if (cheapest_partial_path != NULL)
    7474             :         dNumPartialPartialGroups =
    7475        2512 :             get_number_of_groups(root,
    7476             :                                  cheapest_partial_path->rows,
    7477             :                                  gd,
    7478             :                                  extra->targetList);
    7479             : 
    7480        3094 :     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        1716 :         foreach(lc, input_rel->pathlist)
    7490             :         {
    7491             :             ListCell   *lc2;
    7492         858 :             Path       *path = (Path *) lfirst(lc);
    7493         858 :             Path       *path_save = path;
    7494         858 :             List       *pathkey_orderings = NIL;
    7495             : 
    7496             :             /* generate alternative group orderings that might be useful */
    7497         858 :             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        1716 :             foreach(lc2, pathkey_orderings)
    7503             :             {
    7504         858 :                 GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
    7505             : 
    7506             :                 /* restore the path (we replace it in the loop) */
    7507         858 :                 path = path_save;
    7508             : 
    7509         858 :                 path = make_ordered_path(root,
    7510             :                                          partially_grouped_rel,
    7511             :                                          path,
    7512             :                                          cheapest_total_path,
    7513             :                                          info->pathkeys,
    7514             :                                          -1.0);
    7515             : 
    7516         858 :                 if (path == NULL)
    7517           0 :                     continue;
    7518             : 
    7519         858 :                 if (parse->hasAggs)
    7520         786 :                     add_path(partially_grouped_rel, (Path *)
    7521         786 :                              create_agg_path(root,
    7522             :                                              partially_grouped_rel,
    7523             :                                              path,
    7524         786 :                                              partially_grouped_rel->reltarget,
    7525         786 :                                              parse->groupClause ? AGG_SORTED : AGG_PLAIN,
    7526             :                                              AGGSPLIT_INITIAL_SERIAL,
    7527             :                                              info->clauses,
    7528             :                                              NIL,
    7529             :                                              agg_partial_costs,
    7530             :                                              dNumPartialGroups));
    7531             :                 else
    7532          72 :                     add_path(partially_grouped_rel, (Path *)
    7533          72 :                              create_group_path(root,
    7534             :                                                partially_grouped_rel,
    7535             :                                                path,
    7536             :                                                info->clauses,
    7537             :                                                NIL,
    7538             :                                                dNumPartialGroups));
    7539             :             }
    7540             :         }
    7541             :     }
    7542             : 
    7543        3094 :     if (can_sort && cheapest_partial_path != NULL)
    7544             :     {
    7545             :         /* Similar to above logic, but for partial paths. */
    7546        5360 :         foreach(lc, input_rel->partial_pathlist)
    7547             :         {
    7548             :             ListCell   *lc2;
    7549        2848 :             Path       *path = (Path *) lfirst(lc);
    7550        2848 :             Path       *path_save = path;
    7551        2848 :             List       *pathkey_orderings = NIL;
    7552             : 
    7553             :             /* generate alternative group orderings that might be useful */
    7554        2848 :             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        5696 :             foreach(lc2, pathkey_orderings)
    7560             :             {
    7561        2848 :                 GroupByOrdering *info = (GroupByOrdering *) lfirst(lc2);
    7562             : 
    7563             : 
    7564             :                 /* restore the path (we replace it in the loop) */
    7565        2848 :                 path = path_save;
    7566             : 
    7567        2848 :                 path = make_ordered_path(root,
    7568             :                                          partially_grouped_rel,
    7569             :                                          path,
    7570             :                                          cheapest_partial_path,
    7571             :                                          info->pathkeys,
    7572             :                                          -1.0);
    7573             : 
    7574        2848 :                 if (path == NULL)
    7575           6 :                     continue;
    7576             : 
    7577        2842 :                 if (parse->hasAggs)
    7578        2720 :                     add_partial_path(partially_grouped_rel, (Path *)
    7579        2720 :                                      create_agg_path(root,
    7580             :                                                      partially_grouped_rel,
    7581             :                                                      path,
    7582        2720 :                                                      partially_grouped_rel->reltarget,
    7583        2720 :                                                      parse->groupClause ? AGG_SORTED : AGG_PLAIN,
    7584             :                                                      AGGSPLIT_INITIAL_SERIAL,
    7585             :                                                      info->clauses,
    7586             :                                                      NIL,
    7587             :                                                      agg_partial_costs,
    7588             :                                                      dNumPartialPartialGroups));
    7589             :                 else
    7590         122 :                     add_partial_path(partially_grouped_rel, (Path *)
    7591         122 :                                      create_group_path(root,
    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        3094 :     if (can_hash && cheapest_total_path != NULL)
    7605             :     {
    7606             :         /* Checked above */
    7607             :         Assert(parse->hasAggs || parse->groupClause);
    7608             : 
    7609         858 :         add_path(partially_grouped_rel, (Path *)
    7610         858 :                  create_agg_path(root,
    7611             :                                  partially_grouped_rel,
    7612             :                                  cheapest_total_path,
    7613         858 :                                  partially_grouped_rel->reltarget,
    7614             :                                  AGG_HASHED,
    7615             :                                  AGGSPLIT_INITIAL_SERIAL,
    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        3094 :     if (can_hash && cheapest_partial_path != NULL)
    7626             :     {
    7627        1718 :         add_partial_path(partially_grouped_rel, (Path *)
    7628        1718 :                          create_agg_path(root,
    7629             :                                          partially_grouped_rel,
    7630             :                                          cheapest_partial_path,
    7631        1718 :                                          partially_grouped_rel->reltarget,
    7632             :                                          AGG_HASHED,
    7633             :                                          AGGSPLIT_INITIAL_SERIAL,
    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        3094 :     if (eager_agg_rel)
    7645             :     {
    7646             :         /* Add the paths */
    7647        2348 :         foreach(lc, eager_agg_rel->pathlist)
    7648             :         {
    7649        1450 :             Path       *path = (Path *) lfirst(lc);
    7650             : 
    7651             :             /* Shouldn't have any parameterized paths anymore */
    7652             :             Assert(path->param_info == NULL);
    7653             : 
    7654        1450 :             path = (Path *) create_projection_path(root,
    7655             :                                                    partially_grouped_rel,
    7656             :                                                    path,
    7657        1450 :                                                    partially_grouped_rel->reltarget);
    7658             : 
    7659        1450 :             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         898 :         if (partially_grouped_rel->consider_parallel)
    7667             :         {
    7668        2028 :             foreach(lc, eager_agg_rel->partial_pathlist)
    7669             :             {
    7670        1212 :                 Path       *path = (Path *) lfirst(lc);
    7671             : 
    7672             :                 /* Shouldn't have any parameterized paths anymore */
    7673             :                 Assert(path->param_info == NULL);
    7674             : 
    7675        1212 :                 path = (Path *) create_projection_path(root,
    7676             :                                                        partially_grouped_rel,
    7677             :                                                        path,
    7678        1212 :                                                        partially_grouped_rel->reltarget);
    7679             : 
    7680        1212 :                 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        3094 :     if (partially_grouped_rel->fdwroutine &&
    7690           6 :         partially_grouped_rel->fdwroutine->GetForeignUpperPaths)
    7691             :     {
    7692           6 :         FdwRoutine *fdwroutine = partially_grouped_rel->fdwroutine;
    7693             : 
    7694           6 :         fdwroutine->GetForeignUpperPaths(root,
    7695             :                                          UPPERREL_PARTIAL_GROUP_AGG,
    7696             :                                          input_rel, partially_grouped_rel,
    7697             :                                          extra);
    7698             :     }
    7699             : 
    7700        3094 :     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             :  */
    7709             : static Path *
    7710       57688 : make_ordered_path(PlannerInfo *root, RelOptInfo *rel, Path *path,
    7711             :                   Path *cheapest_path, List *pathkeys, double limit_tuples)
    7712             : {
    7713             :     bool        is_sorted;
    7714             :     int         presorted_keys;
    7715             : 
    7716       57688 :     is_sorted = pathkeys_count_contained_in(pathkeys,
    7717             :                                             path->pathkeys,
    7718             :                                             &presorted_keys);
    7719             : 
    7720       57688 :     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       16598 :         if (path != cheapest_path &&
    7729        3132 :             (presorted_keys == 0 || !enable_incremental_sort))
    7730        1488 :             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       15110 :         if (presorted_keys == 0 || !enable_incremental_sort)
    7738       13292 :             path = (Path *) create_sort_path(root,
    7739             :                                              rel,
    7740             :                                              path,
    7741             :                                              pathkeys,
    7742             :                                              limit_tuples);
    7743             :         else
    7744        1818 :             path = (Path *) create_incremental_sort_path(root,
    7745             :                                                          rel,
    7746             :                                                          path,
    7747             :                                                          pathkeys,
    7748             :                                                          presorted_keys,
    7749             :                                                          limit_tuples);
    7750             :     }
    7751             : 
    7752       56200 :     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             :  */
    7768             : static void
    7769        2326 : gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
    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        2326 :     if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys)
    7780          18 :         groupby_pathkeys = list_copy_head(root->group_pathkeys,
    7781             :                                           root->num_groupby_pathkeys);
    7782             :     else
    7783        2308 :         groupby_pathkeys = root->group_pathkeys;
    7784             : 
    7785             :     /* Try Gather for unordered paths and Gather Merge for ordered ones. */
    7786        2326 :     generate_useful_gather_paths(root, rel, true);
    7787             : 
    7788        2326 :     cheapest_partial_path = linitial(rel->partial_pathlist);
    7789             : 
    7790             :     /* XXX Shouldn't this also consider the group-key-reordering? */
    7791        5786 :     foreach(lc, rel->partial_pathlist)
    7792             :     {
    7793        3460 :         Path       *path = (Path *) lfirst(lc);
    7794             :         bool        is_sorted;
    7795             :         int         presorted_keys;
    7796             :         double      total_groups;
    7797             : 
    7798        3460 :         is_sorted = pathkeys_count_contained_in(groupby_pathkeys,
    7799             :                                                 path->pathkeys,
    7800             :                                                 &presorted_keys);
    7801             : 
    7802        3460 :         if (is_sorted)
    7803        1996 :             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        1464 :         if (path != cheapest_partial_path &&
    7812           0 :             (presorted_keys == 0 || !enable_incremental_sort))
    7813           0 :             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        1464 :         if (presorted_keys == 0 || !enable_incremental_sort)
    7821        1464 :             path = (Path *) create_sort_path(root, rel, path,
    7822             :                                              groupby_pathkeys,
    7823             :                                              -1.0);
    7824             :         else
    7825           0 :             path = (Path *) create_incremental_sort_path(root,
    7826             :                                                          rel,
    7827             :                                                          path,
    7828             :                                                          groupby_pathkeys,
    7829             :                                                          presorted_keys,
    7830             :                                                          -1.0);
    7831        1464 :         total_groups = compute_gather_rows(path);
    7832             :         path = (Path *)
    7833        1464 :             create_gather_merge_path(root,
    7834             :                                      rel,
    7835             :                                      path,
    7836        1464 :                                      rel->reltarget,
    7837             :                                      groupby_pathkeys,
    7838             :                                      NULL,
    7839             :                                      &total_groups);
    7840             : 
    7841        1464 :         add_path(rel, path);
    7842             :     }
    7843        2326 : }
    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             :  */
    7851             : static bool
    7852       39254 : can_partial_agg(PlannerInfo *root)
    7853             : {
    7854       39254 :     Query      *parse = root->parse;
    7855             : 
    7856       39254 :     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           0 :         return false;
    7863             :     }
    7864       39254 :     else if (parse->groupingSets)
    7865             :     {
    7866             :         /* We don't know how to do grouping sets in parallel. */
    7867         926 :         return false;
    7868             :     }
    7869       38328 :     else if (root->hasNonPartialAggs || root->hasNonSerialAggs)
    7870             :     {
    7871             :         /* Insufficient support for partial mode. */
    7872        3872 :         return false;
    7873             :     }
    7874             : 
    7875             :     /* Everything looks good. */
    7876       34456 :     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             :  */
    7893             : static void
    7894      539874 : apply_scanjoin_target_to_paths(PlannerInfo *root,
    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      539874 :     bool        rel_is_partitioned = IS_PARTITIONED_REL(rel);
    7902             :     PathTarget *scanjoin_target;
    7903             :     ListCell   *lc;
    7904             : 
    7905             :     /* This recurses, so be paranoid. */
    7906      539874 :     check_stack_depth();
    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      539874 :     if (rel_is_partitioned)
    7928       12906 :         rel->pathlist = NIL;
    7929             : 
    7930             :     /*
    7931             :      * If the scan/join target is not parallel-safe, partial paths cannot
    7932             :      * generate it.
    7933             :      */
    7934      539874 :     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             :          */
    7945       79324 :         generate_useful_gather_paths(root, rel, false);
    7946             : 
    7947             :         /* Can't use parallel query above this level. */
    7948       79324 :         rel->partial_pathlist = NIL;
    7949       79324 :         rel->consider_parallel = false;
    7950             :     }
    7951             : 
    7952             :     /* Finish dropping old paths for a partitioned rel, per comment above */
    7953      539874 :     if (rel_is_partitioned)
    7954       12906 :         rel->partial_pathlist = NIL;
    7955             : 
    7956             :     /* Extract SRF-free scan/join target. */
    7957      539874 :     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     1120098 :     foreach(lc, rel->pathlist)
    7969             :     {
    7970      580224 :         Path       *subpath = (Path *) lfirst(lc);
    7971             : 
    7972             :         /* Shouldn't have any parameterized paths anymore */
    7973             :         Assert(subpath->param_info == NULL);
    7974             : 
    7975      580224 :         if (tlist_same_exprs)
    7976      204128 :             subpath->pathtarget->sortgrouprefs =
    7977      204128 :                 scanjoin_target->sortgrouprefs;
    7978             :         else
    7979             :         {
    7980             :             Path       *newpath;
    7981             : 
    7982      376096 :             newpath = (Path *) create_projection_path(root, rel, subpath,
    7983             :                                                       scanjoin_target);
    7984      376096 :             lfirst(lc) = newpath;
    7985             :         }
    7986             :     }
    7987             : 
    7988             :     /* Likewise adjust the targets for any partial paths. */
    7989      561066 :     foreach(lc, rel->partial_pathlist)
    7990             :     {
    7991       21192 :         Path       *subpath = (Path *) lfirst(lc);
    7992             : 
    7993             :         /* Shouldn't have any parameterized paths anymore */
    7994             :         Assert(subpath->param_info == NULL);
    7995             : 
    7996       21192 :         if (tlist_same_exprs)
    7997       17264 :             subpath->pathtarget->sortgrouprefs =
    7998       17264 :                 scanjoin_target->sortgrouprefs;
    7999             :         else
    8000             :         {
    8001             :             Path       *newpath;
    8002             : 
    8003        3928 :             newpath = (Path *) create_projection_path(root, rel, subpath,
    8004             :                                                       scanjoin_target);
    8005        3928 :             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      539874 :     if (root->parse->hasTargetSRFs)
    8016       11704 :         adjust_paths_for_srfs(root, rel,
    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      539874 :     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      539874 :     if (rel_is_partitioned)
    8041             :     {
    8042       12906 :         List       *live_children = NIL;
    8043             :         int         i;
    8044             : 
    8045             :         /* Adjust each partition. */
    8046       12906 :         i = -1;
    8047       36588 :         while ((i = bms_next_member(rel->live_parts, i)) >= 0)
    8048             :         {
    8049       23682 :             RelOptInfo *child_rel = rel->part_rels[i];
    8050             :             AppendRelInfo **appinfos;
    8051             :             int         nappinfos;
    8052       23682 :             List       *child_scanjoin_targets = NIL;
    8053             : 
    8054             :             Assert(child_rel != NULL);
    8055             : 
    8056             :             /* Dummy children can be ignored. */
    8057       23682 :             if (IS_DUMMY_REL(child_rel))
    8058          42 :                 continue;
    8059             : 
    8060             :             /* Translate scan/join targets for this child. */
    8061       23640 :             appinfos = find_appinfos_by_relids(root, child_rel->relids,
    8062             :                                                &nappinfos);
    8063       47280 :             foreach(lc, scanjoin_targets)
    8064             :             {
    8065       23640 :                 PathTarget *target = lfirst_node(PathTarget, lc);
    8066             : 
    8067       23640 :                 target = copy_pathtarget(target);
    8068       23640 :                 target->exprs = (List *)
    8069       23640 :                     adjust_appendrel_attrs(root,
    8070       23640 :                                            (Node *) target->exprs,
    8071             :                                            nappinfos, appinfos);
    8072       23640 :                 child_scanjoin_targets = lappend(child_scanjoin_targets,
    8073             :                                                  target);
    8074             :             }
    8075       23640 :             pfree(appinfos);
    8076             : 
    8077             :             /* Recursion does the real work. */
    8078       23640 :             apply_scanjoin_target_to_paths(root, child_rel,
    8079             :                                            child_scanjoin_targets,
    8080             :                                            scanjoin_targets_contain_srfs,
    8081             :                                            scanjoin_target_parallel_safe,
    8082             :                                            tlist_same_exprs);
    8083             : 
    8084             :             /* Save non-dummy children for Append paths. */
    8085       23640 :             if (!IS_DUMMY_REL(child_rel))
    8086       23640 :                 live_children = lappend(live_children, child_rel);
    8087             :         }
    8088             : 
    8089             :         /* Build new paths for this relation by appending child paths. */
    8090       12906 :         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      539874 :     if (rel->consider_parallel && !IS_OTHER_REL(rel))
    8101      176164 :         generate_useful_gather_paths(root, rel, false);
    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      539874 :     set_cheapest(rel);
    8109      539874 : }
    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             :  */
    8128             : static void
    8129         814 : create_partitionwise_grouping_paths(PlannerInfo *root,
    8130             :                                     RelOptInfo *input_rel,
    8131             :                                     RelOptInfo *grouped_rel,
    8132             :                                     RelOptInfo *partially_grouped_rel,
    8133             :                                     const AggClauseCosts *agg_costs,
    8134             :                                     grouping_sets_data *gd,
    8135             :                                     PartitionwiseAggregateType patype,
    8136             :                                     GroupPathExtraData *extra)
    8137             : {
    8138         814 :     List       *grouped_live_children = NIL;
    8139         814 :     List       *partially_grouped_live_children = NIL;
    8140         814 :     PathTarget *target = grouped_rel->reltarget;
    8141         814 :     bool        partial_grouping_valid = true;
    8142             :     int         i;
    8143             : 
    8144             :     Assert(patype != PARTITIONWISE_AGGREGATE_NONE);
    8145             :     Assert(patype != PARTITIONWISE_AGGREGATE_PARTIAL ||
    8146             :            partially_grouped_rel != NULL);
    8147             : 
    8148             :     /* Add paths for partitionwise aggregation/grouping. */
    8149         814 :     i = -1;
    8150        2944 :     while ((i = bms_next_member(input_rel->live_parts, i)) >= 0)
    8151             :     {
    8152        2130 :         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        2130 :         if (IS_DUMMY_REL(child_input_rel))
    8164           0 :             continue;
    8165             : 
    8166        2130 :         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        2130 :         memcpy(&child_extra, extra, sizeof(child_extra));
    8173             : 
    8174        2130 :         appinfos = find_appinfos_by_relids(root, child_input_rel->relids,
    8175             :                                            &nappinfos);
    8176             : 
    8177        2130 :         child_target->exprs = (List *)
    8178        2130 :             adjust_appendrel_attrs(root,
    8179        2130 :                                    (Node *) target->exprs,
    8180             :                                    nappinfos, appinfos);
    8181             : 
    8182             :         /* Translate havingQual and targetList. */
    8183        2130 :         child_extra.havingQual = (Node *)
    8184             :             adjust_appendrel_attrs(root,
    8185             :                                    extra->havingQual,
    8186             :                                    nappinfos, appinfos);
    8187        2130 :         child_extra.targetList = (List *)
    8188        2130 :             adjust_appendrel_attrs(root,
    8189        2130 :                                    (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        2130 :         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        2130 :         child_grouped_rel = make_grouping_rel(root, child_input_rel,
    8204             :                                               child_target,
    8205        2130 :                                               extra->target_parallel_safe,
    8206             :                                               child_extra.havingQual);
    8207             : 
    8208             :         /* Create grouping paths for this child relation. */
    8209        2130 :         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        2130 :         if (child_partially_grouped_rel)
    8215             :         {
    8216             :             partially_grouped_live_children =
    8217        1542 :                 lappend(partially_grouped_live_children,
    8218             :                         child_partially_grouped_rel);
    8219             :         }
    8220             :         else
    8221         588 :             partial_grouping_valid = false;
    8222             : 
    8223        2130 :         if (patype == PARTITIONWISE_AGGREGATE_FULL)
    8224             :         {
    8225        1272 :             set_cheapest(child_grouped_rel);
    8226        1272 :             grouped_live_children = lappend(grouped_live_children,
    8227             :                                             child_grouped_rel);
    8228             :         }
    8229             : 
    8230        2130 :         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         814 :     if (partially_grouped_rel && partial_grouping_valid)
    8243             :     {
    8244             :         Assert(partially_grouped_live_children != NIL);
    8245             : 
    8246         602 :         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         814 :     if (patype == PARTITIONWISE_AGGREGATE_FULL)
    8252             :     {
    8253             :         Assert(grouped_live_children != NIL);
    8254             : 
    8255         476 :         add_paths_to_append_rel(root, grouped_rel, grouped_live_children);
    8256             :     }
    8257         814 : }
    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             :  */
    8265             : static bool
    8266         760 : group_by_has_partkey(RelOptInfo *input_rel,
    8267             :                      List *targetList,
    8268             :                      List *groupClause)
    8269             : {
    8270         760 :     List       *groupexprs = get_sortgrouplist_exprs(groupClause, targetList);
    8271         760 :     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         760 :     if (!input_rel->partexprs)
    8279           0 :         return false;
    8280             : 
    8281         760 :     partnatts = input_rel->part_scheme->partnatts;
    8282             : 
    8283        1272 :     for (cnt = 0; cnt < partnatts; cnt++)
    8284             :     {
    8285         796 :         List       *partexprs = input_rel->partexprs[cnt];
    8286             :         ListCell   *lc;
    8287         796 :         bool        found = false;
    8288             : 
    8289        1194 :         foreach(lc, partexprs)
    8290             :         {
    8291             :             ListCell   *lg;
    8292         922 :             Expr       *partexpr = lfirst(lc);
    8293         922 :             Oid         partcoll = input_rel->part_scheme->partcollation[cnt];
    8294             : 
    8295        1440 :             foreach(lg, groupexprs)
    8296             :             {
    8297        1042 :                 Expr       *groupexpr = lfirst(lg);
    8298        1042 :                 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        1042 :                 if (IsA(groupexpr, RelabelType))
    8306          24 :                     groupexpr = ((RelabelType *) groupexpr)->arg;
    8307             : 
    8308        1042 :                 if (equal(groupexpr, partexpr))
    8309             :                 {
    8310             :                     /*
    8311             :                      * Reject a match if the grouping collation does not match
    8312             :                      * the partitioning collation.
    8313             :                      */
    8314         524 :                     if (OidIsValid(partcoll) && OidIsValid(groupcoll) &&
    8315             :                         partcoll != groupcoll)
    8316          12 :                         return false;
    8317             : 
    8318         512 :                     found = true;
    8319         512 :                     break;
    8320             :                 }
    8321             :             }
    8322             : 
    8323         910 :             if (found)
    8324         512 :                 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         784 :         if (!found)
    8332         272 :             return false;
    8333             :     }
    8334             : 
    8335         476 :     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             :  */
    8352             : static List *
    8353       12488 : generate_setop_child_grouplist(SetOperationStmt *op, List *targetlist)
    8354             : {
    8355       12488 :     List       *grouplist = copyObject(op->groupClauses);
    8356             :     ListCell   *lg;
    8357             :     ListCell   *lt;
    8358             :     ListCell   *ct;
    8359             : 
    8360       12488 :     lg = list_head(grouplist);
    8361       12488 :     ct = list_head(op->colTypes);
    8362       48090 :     foreach(lt, targetlist)
    8363             :     {
    8364       36016 :         TargetEntry *tle = (TargetEntry *) lfirst(lt);
    8365             :         SortGroupClause *sgc;
    8366             :         Oid         coltype;
    8367             : 
    8368             :         /* resjunk columns could have sortgrouprefs.  Leave these alone */
    8369       36016 :         if (tle->resjunk)
    8370           0 :             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       36016 :         sgc = (SortGroupClause *) lfirst(lg);
    8379       36016 :         coltype = lfirst_oid(ct);
    8380             : 
    8381             :         /* reject if target type isn't the same as the setop target type */
    8382       36016 :         if (coltype != exprType((Node *) tle->expr))
    8383         414 :             return NIL;
    8384             : 
    8385       35602 :         lg = lnext(grouplist, lg);
    8386       35602 :         ct = lnext(op->colTypes, ct);
    8387             : 
    8388             :         /* assign a tleSortGroupRef, or reuse the existing one */
    8389       35602 :         sgc->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
    8390             :     }
    8391             : 
    8392             :     Assert(lg == NULL);
    8393             :     Assert(ct == NULL);
    8394             : 
    8395       12074 :     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             :  */
    8408             : RelOptInfo *
    8409        8776 : create_unique_paths(PlannerInfo *root, RelOptInfo *rel, SpecialJoinInfo *sjinfo)
    8410             : {
    8411             :     RelOptInfo *unique_rel;
    8412        8776 :     List       *sortPathkeys = NIL;
    8413        8776 :     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        8776 :     if (rel->unique_rel)
    8422        1820 :         return rel->unique_rel;
    8423             : 
    8424             :     /* If it's not possible to unique-ify, return NULL */
    8425        6956 :     if (!(sjinfo->semi_can_btree || sjinfo->semi_can_hash))
    8426         132 :         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        6824 :     if (IS_OTHER_REL(rel) && rel->top_parent->unique_rel == NULL)
    8435          12 :         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             :      */
    8447        6812 :     oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
    8448             : 
    8449        6812 :     unique_rel = makeNode(RelOptInfo);
    8450        6812 :     memcpy(unique_rel, rel, sizeof(RelOptInfo));
    8451             : 
    8452             :     /*
    8453             :      * clear path info
    8454             :      */
    8455        6812 :     unique_rel->pathlist = NIL;
    8456        6812 :     unique_rel->ppilist = NIL;
    8457        6812 :     unique_rel->partial_pathlist = NIL;
    8458        6812 :     unique_rel->cheapest_startup_path = NULL;
    8459        6812 :     unique_rel->cheapest_total_path = NULL;
    8460        6812 :     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        6812 :     if (IS_OTHER_REL(rel))
    8472         432 :     {
    8473             :         PathTarget *child_unique_target;
    8474             :         PathTarget *parent_unique_target;
    8475             : 
    8476         432 :         parent_unique_target = rel->top_parent->unique_rel->reltarget;
    8477             : 
    8478         432 :         child_unique_target = copy_pathtarget(parent_unique_target);
    8479             : 
    8480             :         /* Translate the target expressions */
    8481         432 :         child_unique_target->exprs = (List *)
    8482         432 :             adjust_appendrel_attrs_multilevel(root,
    8483         432 :                                               (Node *) parent_unique_target->exprs,
    8484             :                                               rel,
    8485         432 :                                               rel->top_parent);
    8486             : 
    8487         432 :         unique_rel->reltarget = child_unique_target;
    8488             : 
    8489         432 :         sortPathkeys = rel->top_parent->unique_pathkeys;
    8490         432 :         groupClause = rel->top_parent->unique_groupclause;
    8491             :     }
    8492             :     else
    8493             :     {
    8494             :         List       *newtlist;
    8495             :         int         nextresno;
    8496        6380 :         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        6380 :         newtlist = make_tlist_from_pathtarget(rel->reltarget);
    8515        6380 :         nextresno = list_length(newtlist) + 1;
    8516             : 
    8517       12994 :         forboth(lc1, sjinfo->semi_rhs_exprs, lc2, sjinfo->semi_operators)
    8518             :         {
    8519        6614 :             Expr       *uniqexpr = lfirst(lc1);
    8520        6614 :             Oid         in_oper = lfirst_oid(lc2);
    8521             :             Oid         sortop;
    8522             :             TargetEntry *tle;
    8523        6614 :             bool        made_tle = false;
    8524             : 
    8525        6614 :             tle = tlist_member(uniqexpr, newtlist);
    8526        6614 :             if (!tle)
    8527             :             {
    8528        3184 :                 tle = makeTargetEntry((Expr *) uniqexpr,
    8529             :                                       nextresno,
    8530             :                                       NULL,
    8531             :                                       false);
    8532        3184 :                 newtlist = lappend(newtlist, tle);
    8533        3184 :                 nextresno++;
    8534        3184 :                 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        6614 :             sortop = get_ordering_op_for_equality_op(in_oper, false);
    8544        6614 :             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        6614 :                 eqop = get_equality_op_for_ordering_op(sortop, NULL);
    8556        6614 :                 if (!OidIsValid(eqop))  /* shouldn't happen */
    8557           0 :                     elog(ERROR, "could not find equality operator for ordering operator %u",
    8558             :                          sortop);
    8559             : 
    8560        6614 :                 sortcl = makeNode(SortGroupClause);
    8561        6614 :                 sortcl->tleSortGroupRef = assignSortGroupRef(tle, newtlist);
    8562        6614 :                 sortcl->eqop = eqop;
    8563        6614 :                 sortcl->sortop = sortop;
    8564        6614 :                 sortcl->reverse_sort = false;
    8565        6614 :                 sortcl->nulls_first = false;
    8566        6614 :                 sortcl->hashable = false;    /* no need to make this accurate */
    8567        6614 :                 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        6614 :                 sortPathkeys = make_pathkeys_for_sortclauses(root, sortList,
    8575             :                                                              newtlist);
    8576        6614 :                 if (list_length(sortPathkeys) != list_length(sortList))
    8577             :                 {
    8578             :                     /* Drop the redundant SortGroupClause */
    8579        2052 :                     sortList = list_delete_last(sortList);
    8580             :                     Assert(list_length(sortPathkeys) == list_length(sortList));
    8581             :                     /* Undo tlist addition, if we made one */
    8582        2052 :                     if (made_tle)
    8583             :                     {
    8584          12 :                         newtlist = list_delete_last(newtlist);
    8585          12 :                         nextresno--;
    8586             :                     }
    8587             :                     /* We need not consider this clause for hashing, either */
    8588        2052 :                     continue;
    8589             :                 }
    8590             :             }
    8591           0 :             else if (sjinfo->semi_can_btree) /* shouldn't happen */
    8592           0 :                 elog(ERROR, "could not find ordering operator for equality operator %u",
    8593             :                      in_oper);
    8594             : 
    8595        4562 :             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        4562 :                 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
    8609           0 :                     elog(ERROR, "could not find compatible hash operator for operator %u",
    8610             :                          in_oper);
    8611             : 
    8612        4562 :                 groupcl = makeNode(SortGroupClause);
    8613        4562 :                 groupcl->tleSortGroupRef = assignSortGroupRef(tle, newtlist);
    8614        4562 :                 groupcl->eqop = eq_oper;
    8615        4562 :                 groupcl->sortop = sortop;
    8616        4562 :                 groupcl->reverse_sort = false;
    8617        4562 :                 groupcl->nulls_first = false;
    8618        4562 :                 groupcl->hashable = true;
    8619        4562 :                 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        6380 :         if (!sjinfo->semi_can_btree)
    8628           0 :             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        6380 :         if (sortPathkeys == NIL && groupClause == NIL)
    8637             :         {
    8638        1950 :             MemoryContextSwitchTo(oldcontext);
    8639        1950 :             return NULL;
    8640             :         }
    8641             : 
    8642             :         /* Convert the required targetlist back to PathTarget form */
    8643        4430 :         unique_rel->reltarget = create_pathtarget(root, newtlist);
    8644             :     }
    8645             : 
    8646             :     /* build unique paths based on input rel's pathlist */
    8647        4862 :     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        4862 :     create_partial_unique_paths(root, rel, sortPathkeys, groupClause,
    8652             :                                 sjinfo, unique_rel);
    8653             : 
    8654             :     /* Now choose the best path(s) */
    8655        4862 :     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        4862 :     rel->unique_rel = unique_rel;
    8665        4862 :     rel->unique_pathkeys = sortPathkeys;
    8666        4862 :     rel->unique_groupclause = groupClause;
    8667             : 
    8668        4862 :     MemoryContextSwitchTo(oldcontext);
    8669             : 
    8670        4862 :     return unique_rel;
    8671             : }
    8672             : 
    8673             : /*
    8674             :  * create_final_unique_paths
    8675             :  *    Create unique paths in 'unique_rel' based on 'input_rel' pathlist
    8676             :  */
    8677             : static void
    8678        8496 : create_final_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
    8679             :                           List *sortPathkeys, List *groupClause,
    8680             :                           SpecialJoinInfo *sjinfo, RelOptInfo *unique_rel)
    8681             : {
    8682        8496 :     Path       *cheapest_input_path = input_rel->cheapest_total_path;
    8683             : 
    8684             :     /* Estimate number of output rows */
    8685        8496 :     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        8496 :     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       18548 :         foreach(lc, input_rel->pathlist)
    8705             :         {
    8706       10052 :             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       10052 :             if (input_path->param_info &&
    8716             :                 input_path != cheapest_input_path)
    8717         916 :                 continue;
    8718             : 
    8719        9180 :             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        9180 :             if (!is_sorted && input_path != cheapest_input_path &&
    8729          92 :                 (presorted_keys == 0 || !enable_incremental_sort))
    8730          44 :                 continue;
    8731             : 
    8732             :             /*
    8733             :              * Make a separate ProjectionPath in case we need a Result node.
    8734             :              */
    8735        9136 :             path = (Path *) create_projection_path(root,
    8736             :                                                    unique_rel,
    8737             :                                                    input_path,
    8738        9136 :                                                    unique_rel->reltarget);
    8739             : 
    8740        9136 :             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        4842 :                 if (presorted_keys == 0 || !enable_incremental_sort)
    8748        4794 :                     path = (Path *) create_sort_path(root,
    8749             :                                                      unique_rel,
    8750             :                                                      path,
    8751             :                                                      sortPathkeys,
    8752             :                                                      -1.0);
    8753             :                 else
    8754          48 :                     path = (Path *) create_incremental_sort_path(root,
    8755             :                                                                  unique_rel,
    8756             :                                                                  path,
    8757             :                                                                  sortPathkeys,
    8758             :                                                                  presorted_keys,
    8759             :                                                                  -1.0);
    8760             :             }
    8761             : 
    8762        9136 :             path = (Path *) create_unique_path(root, unique_rel, path,
    8763             :                                                list_length(sortPathkeys),
    8764             :                                                unique_rel->rows);
    8765             : 
    8766        9136 :             add_path(unique_rel, path);
    8767             :         }
    8768             :     }
    8769             : 
    8770             :     /* Consider hash-based implementation, if possible. */
    8771        8496 :     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        8496 :         path = (Path *) create_projection_path(root,
    8779             :                                                unique_rel,
    8780             :                                                cheapest_input_path,
    8781        8496 :                                                unique_rel->reltarget);
    8782             : 
    8783        8496 :         path = (Path *) create_agg_path(root,
    8784             :                                         unique_rel,
    8785             :                                         path,
    8786             :                                         cheapest_input_path->pathtarget,
    8787             :                                         AGG_HASHED,
    8788             :                                         AGGSPLIT_SIMPLE,
    8789             :                                         groupClause,
    8790             :                                         NIL,
    8791             :                                         NULL,
    8792             :                                         unique_rel->rows);
    8793             : 
    8794        8496 :         add_path(unique_rel, path);
    8795             :     }
    8796        8496 : }
    8797             : 
    8798             : /*
    8799             :  * create_partial_unique_paths
    8800             :  *    Create unique paths in 'unique_rel' based on 'input_rel' partial_pathlist
    8801             :  */
    8802             : static void
    8803        4862 : create_partial_unique_paths(PlannerInfo *root, RelOptInfo *input_rel,
    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        4862 :     if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
    8812        1228 :         return;
    8813             : 
    8814             :     /*
    8815             :      * nothing to do if there's anything in the targetlist that's
    8816             :      * parallel-restricted.
    8817             :      */
    8818        3634 :     if (!is_parallel_safe(root, (Node *) unique_rel->reltarget->exprs))
    8819           0 :         return;
    8820             : 
    8821        3634 :     cheapest_partial_path = linitial(input_rel->partial_pathlist);
    8822             : 
    8823        3634 :     partial_unique_rel = makeNode(RelOptInfo);
    8824        3634 :     memcpy(partial_unique_rel, input_rel, sizeof(RelOptInfo));
    8825             : 
    8826             :     /*
    8827             :      * clear path info
    8828             :      */
    8829        3634 :     partial_unique_rel->pathlist = NIL;
    8830        3634 :     partial_unique_rel->ppilist = NIL;
    8831        3634 :     partial_unique_rel->partial_pathlist = NIL;
    8832        3634 :     partial_unique_rel->cheapest_startup_path = NULL;
    8833        3634 :     partial_unique_rel->cheapest_total_path = NULL;
    8834        3634 :     partial_unique_rel->cheapest_parameterized_paths = NIL;
    8835             : 
    8836             :     /* Estimate number of output rows */
    8837        3634 :     partial_unique_rel->rows = estimate_num_groups(root,
    8838             :                                                    sjinfo->semi_rhs_exprs,
    8839             :                                                    cheapest_partial_path->rows,
    8840             :                                                    NULL,
    8841             :                                                    NULL);
    8842        3634 :     partial_unique_rel->reltarget = unique_rel->reltarget;
    8843             : 
    8844             :     /* Consider sort-based implementations, if possible. */
    8845        3634 :     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        7580 :         foreach(lc, input_rel->partial_pathlist)
    8855             :         {
    8856        3946 :             Path       *input_path = (Path *) lfirst(lc);
    8857             :             Path       *path;
    8858             :             bool        is_sorted;
    8859             :             int         presorted_keys;
    8860             : 
    8861        3946 :             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        3946 :             if (!is_sorted && input_path != cheapest_partial_path &&
    8871           0 :                 (presorted_keys == 0 || !enable_incremental_sort))
    8872           0 :                 continue;
    8873             : 
    8874             :             /*
    8875             :              * Make a separate ProjectionPath in case we need a Result node.
    8876             :              */
    8877        3946 :             path = (Path *) create_projection_path(root,
    8878             :                                                    partial_unique_rel,
    8879             :                                                    input_path,
    8880        3946 :                                                    partial_unique_rel->reltarget);
    8881             : 
    8882        3946 :             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        3586 :                 if (presorted_keys == 0 || !enable_incremental_sort)
    8890        3586 :                     path = (Path *) create_sort_path(root,
    8891             :                                                      partial_unique_rel,
    8892             :                                                      path,
    8893             :                                                      sortPathkeys,
    8894             :                                                      -1.0);
    8895             :                 else
    8896           0 :                     path = (Path *) create_incremental_sort_path(root,
    8897             :                                                                  partial_unique_rel,
    8898             :                                                                  path,
    8899             :                                                                  sortPathkeys,
    8900             :                                                                  presorted_keys,
    8901             :                                                                  -1.0);
    8902             :             }
    8903             : 
    8904        3946 :             path = (Path *) create_unique_path(root, partial_unique_rel, path,
    8905             :                                                list_length(sortPathkeys),
    8906             :                                                partial_unique_rel->rows);
    8907             : 
    8908        3946 :             add_partial_path(partial_unique_rel, path);
    8909             :         }
    8910             :     }
    8911             : 
    8912             :     /* Consider hash-based implementation, if possible. */
    8913        3634 :     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        3634 :         path = (Path *) create_projection_path(root,
    8921             :                                                partial_unique_rel,
    8922             :                                                cheapest_partial_path,
    8923        3634 :                                                partial_unique_rel->reltarget);
    8924             : 
    8925        3634 :         path = (Path *) create_agg_path(root,
    8926             :                                         partial_unique_rel,
    8927             :                                         path,
    8928             :                                         cheapest_partial_path->pathtarget,
    8929             :                                         AGG_HASHED,
    8930             :                                         AGGSPLIT_SIMPLE,
    8931             :                                         groupClause,
    8932             :                                         NIL,
    8933             :                                         NULL,
    8934             :                                         partial_unique_rel->rows);
    8935             : 
    8936        3634 :         add_partial_path(partial_unique_rel, path);
    8937             :     }
    8938             : 
    8939        3634 :     if (partial_unique_rel->partial_pathlist != NIL)
    8940             :     {
    8941        3634 :         generate_useful_gather_paths(root, partial_unique_rel, true);
    8942        3634 :         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        3634 :         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             :  */
    8960             : char *
    8961       77594 : choose_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       77594 :     if (!always_number)
    8971             :     {
    8972       19842 :         bool        found = false;
    8973             : 
    8974       49532 :         foreach_ptr(char, subplan_name, glob->subplanNames)
    8975             :         {
    8976       15468 :             if (strcmp(subplan_name, name) == 0)
    8977             :             {
    8978        5620 :                 found = true;
    8979        5620 :                 break;
    8980             :             }
    8981             :         }
    8982             : 
    8983       19842 :         if (!found)
    8984             :         {
    8985             :             /* pstrdup here is just to avoid cast-away-const */
    8986       14222 :             char       *chosen_name = pstrdup(name);
    8987             : 
    8988       14222 :             glob->subplanNames = lappend(glob->subplanNames, chosen_name);
    8989       14222 :             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       63372 :     for (n = 1; true; ++n)
    8999       54626 :     {
    9000      117998 :         char       *proposed_name = psprintf("%s_%u", name, n);
    9001      117998 :         bool        found = false;
    9002             : 
    9003      450326 :         foreach_ptr(char, subplan_name, glob->subplanNames)
    9004             :         {
    9005      268956 :             if (strcmp(subplan_name, proposed_name) == 0)
    9006             :             {
    9007       54626 :                 found = true;
    9008       54626 :                 break;
    9009             :             }
    9010             :         }
    9011             : 
    9012      117998 :         if (!found)
    9013             :         {
    9014       63372 :             glob->subplanNames = lappend(glob->subplanNames, proposed_name);
    9015       63372 :             return proposed_name;
    9016             :         }
    9017             : 
    9018       54626 :         pfree(proposed_name);
    9019             :     }
    9020             : }

Generated by: LCOV version 1.16