LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/plan - planner.c (source / functions) Coverage Total Hit UNC UBC GBC GIC GNC CBC ECB DCB
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 96.7 % 2565 2481 11 73 217 2264 22
Current Date: 2025-10-26 12:31:50 -0700 Functions: 100.0 % 63 63 14 49 1
Baseline: lcov-20251027-010456-baseline Branches: 83.5 % 2310 1928 50 332 1 2 140 1785 1
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 10 10 10
(7,30] days: 96.7 % 61 59 2 59
(30,360] days: 96.6 % 296 286 9 1 158 128
(360..) days: 96.7 % 2198 2126 72 2126
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 6 6 3 3
(360..) days: 100.0 % 55 55 9 46
Branch coverage date bins:
(1,7] days: 100.0 % 8 8 8
(7,30] days: 86.7 % 60 52 8 52
(30,360] days: 79.6 % 270 215 42 13 88 127
(360..) days: 83.8 % 1973 1653 319 1 2 1650 1

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

Generated by: LCOV version 2.4-beta