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

Generated by: LCOV version 1.13