PostgreSQL Source Code  git master
planner.c File Reference
#include "postgres.h"
#include <limits.h>
#include <math.h>
#include "access/genam.h"
#include "access/parallel.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "foreign/fdwapi.h"
#include "jit/jit.h"
#include "lib/bipartite_match.h"
#include "lib/knapsack.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/supportnodes.h"
#include "optimizer/appendinfo.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/paramassign.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/prep.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "partitioning/partdesc.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/selfuncs.h"
Include dependency graph for planner.c:

Go to the source code of this file.

Data Structures

struct  grouping_sets_data
 
struct  WindowClauseSortData
 
struct  standard_qp_extra
 

Macros

#define EXPRKIND_QUAL   0
 
#define EXPRKIND_TARGET   1
 
#define EXPRKIND_RTFUNC   2
 
#define EXPRKIND_RTFUNC_LATERAL   3
 
#define EXPRKIND_VALUES   4
 
#define EXPRKIND_VALUES_LATERAL   5
 
#define EXPRKIND_LIMIT   6
 
#define EXPRKIND_APPINFO   7
 
#define EXPRKIND_PHV   8
 
#define EXPRKIND_TABLESAMPLE   9
 
#define EXPRKIND_ARBITER_ELEM   10
 
#define EXPRKIND_TABLEFUNC   11
 
#define EXPRKIND_TABLEFUNC_LATERAL   12
 

Functions

static Nodepreprocess_expression (PlannerInfo *root, Node *expr, int kind)
 
static void preprocess_qual_conditions (PlannerInfo *root, Node *jtnode)
 
static void grouping_planner (PlannerInfo *root, double tuple_fraction)
 
static grouping_sets_datapreprocess_grouping_sets (PlannerInfo *root)
 
static Listremap_to_groupclause_idx (List *groupClause, List *gsets, int *tleref_to_colnum_map)
 
static void preprocess_rowmarks (PlannerInfo *root)
 
static double preprocess_limit (PlannerInfo *root, double tuple_fraction, int64 *offset_est, int64 *count_est)
 
static void remove_useless_groupby_columns (PlannerInfo *root)
 
static Listgroupclause_apply_groupingset (PlannerInfo *root, List *force)
 
static Listextract_rollup_sets (List *groupingSets)
 
static Listreorder_grouping_sets (List *groupingSets, List *sortclause)
 
static void standard_qp_callback (PlannerInfo *root, void *extra)
 
static double get_number_of_groups (PlannerInfo *root, double path_rows, grouping_sets_data *gd, List *target_list)
 
static RelOptInfocreate_grouping_paths (PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, grouping_sets_data *gd)
 
static bool is_degenerate_grouping (PlannerInfo *root)
 
static void create_degenerate_grouping_paths (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel)
 
static RelOptInfomake_grouping_rel (PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, Node *havingQual)
 
static void create_ordinary_grouping_paths (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, GroupPathExtraData *extra, RelOptInfo **partially_grouped_rel_p)
 
static void consider_groupingsets_paths (PlannerInfo *root, RelOptInfo *grouped_rel, Path *path, bool is_sorted, bool can_hash, grouping_sets_data *gd, const AggClauseCosts *agg_costs, double dNumGroups)
 
static RelOptInfocreate_window_paths (PlannerInfo *root, RelOptInfo *input_rel, PathTarget *input_target, PathTarget *output_target, bool output_target_parallel_safe, WindowFuncLists *wflists, List *activeWindows)
 
static void create_one_window_path (PlannerInfo *root, RelOptInfo *window_rel, Path *path, PathTarget *input_target, PathTarget *output_target, WindowFuncLists *wflists, List *activeWindows)
 
static RelOptInfocreate_distinct_paths (PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target)
 
static void create_partial_distinct_paths (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *final_distinct_rel, PathTarget *target)
 
static RelOptInfocreate_final_distinct_paths (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *distinct_rel)
 
static RelOptInfocreate_ordered_paths (PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, double limit_tuples)
 
static PathTargetmake_group_input_target (PlannerInfo *root, PathTarget *final_target)
 
static PathTargetmake_partial_grouping_target (PlannerInfo *root, PathTarget *grouping_target, Node *havingQual)
 
static Listpostprocess_setop_tlist (List *new_tlist, List *orig_tlist)
 
static void optimize_window_clauses (PlannerInfo *root, WindowFuncLists *wflists)
 
static Listselect_active_windows (PlannerInfo *root, WindowFuncLists *wflists)
 
static PathTargetmake_window_input_target (PlannerInfo *root, PathTarget *final_target, List *activeWindows)
 
static Listmake_pathkeys_for_window (PlannerInfo *root, WindowClause *wc, List *tlist)
 
static PathTargetmake_sort_input_target (PlannerInfo *root, PathTarget *final_target, bool *have_postponed_srfs)
 
static void adjust_paths_for_srfs (PlannerInfo *root, RelOptInfo *rel, List *targets, List *targets_contain_srfs)
 
static void add_paths_to_grouping_rel (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, double dNumGroups, GroupPathExtraData *extra)
 
static RelOptInfocreate_partial_grouping_paths (PlannerInfo *root, RelOptInfo *grouped_rel, RelOptInfo *input_rel, grouping_sets_data *gd, GroupPathExtraData *extra, bool force_rel_creation)
 
static void gather_grouping_paths (PlannerInfo *root, RelOptInfo *rel)
 
static bool can_partial_agg (PlannerInfo *root)
 
static void apply_scanjoin_target_to_paths (PlannerInfo *root, RelOptInfo *rel, List *scanjoin_targets, List *scanjoin_targets_contain_srfs, bool scanjoin_target_parallel_safe, bool tlist_same_exprs)
 
static void create_partitionwise_grouping_paths (PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, PartitionwiseAggregateType patype, GroupPathExtraData *extra)
 
static bool group_by_has_partkey (RelOptInfo *input_rel, List *targetList, List *groupClause)
 
static int common_prefix_cmp (const void *a, const void *b)
 
PlannedStmtplanner (Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
 
PlannedStmtstandard_planner (Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
 
PlannerInfosubquery_planner (PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
 
Exprpreprocess_phv_expression (PlannerInfo *root, Expr *expr)
 
RowMarkType select_rowmark_type (RangeTblEntry *rte, LockClauseStrength strength)
 
bool limit_needed (Query *parse)
 
static bool has_volatile_pathkey (List *keys)
 
static void adjust_group_pathkeys_for_groupagg (PlannerInfo *root)
 
void mark_partial_aggref (Aggref *agg, AggSplit aggsplit)
 
Pathget_cheapest_fractional_path (RelOptInfo *rel, double tuple_fraction)
 
Exprexpression_planner (Expr *expr)
 
Exprexpression_planner_with_deps (Expr *expr, List **relationOids, List **invalItems)
 
bool plan_cluster_use_sort (Oid tableOid, Oid indexOid)
 
int plan_create_index_workers (Oid tableOid, Oid indexOid)
 
static Pathmake_ordered_path (PlannerInfo *root, RelOptInfo *rel, Path *path, Path *cheapest_path, List *pathkeys)
 

Variables

double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION
 
int debug_parallel_query = DEBUG_PARALLEL_OFF
 
bool parallel_leader_participation = true
 
planner_hook_type planner_hook = NULL
 
create_upper_paths_hook_type create_upper_paths_hook = NULL
 

Macro Definition Documentation

◆ EXPRKIND_APPINFO

#define EXPRKIND_APPINFO   7

Definition at line 84 of file planner.c.

◆ EXPRKIND_ARBITER_ELEM

#define EXPRKIND_ARBITER_ELEM   10

Definition at line 87 of file planner.c.

◆ EXPRKIND_LIMIT

#define EXPRKIND_LIMIT   6

Definition at line 83 of file planner.c.

◆ EXPRKIND_PHV

#define EXPRKIND_PHV   8

Definition at line 85 of file planner.c.

◆ EXPRKIND_QUAL

#define EXPRKIND_QUAL   0

Definition at line 77 of file planner.c.

◆ EXPRKIND_RTFUNC

#define EXPRKIND_RTFUNC   2

Definition at line 79 of file planner.c.

◆ EXPRKIND_RTFUNC_LATERAL

#define EXPRKIND_RTFUNC_LATERAL   3

Definition at line 80 of file planner.c.

◆ EXPRKIND_TABLEFUNC

#define EXPRKIND_TABLEFUNC   11

Definition at line 88 of file planner.c.

◆ EXPRKIND_TABLEFUNC_LATERAL

#define EXPRKIND_TABLEFUNC_LATERAL   12

Definition at line 89 of file planner.c.

◆ EXPRKIND_TABLESAMPLE

#define EXPRKIND_TABLESAMPLE   9

Definition at line 86 of file planner.c.

◆ EXPRKIND_TARGET

#define EXPRKIND_TARGET   1

Definition at line 78 of file planner.c.

◆ EXPRKIND_VALUES

#define EXPRKIND_VALUES   4

Definition at line 81 of file planner.c.

◆ EXPRKIND_VALUES_LATERAL

#define EXPRKIND_VALUES_LATERAL   5

Definition at line 82 of file planner.c.

Function Documentation

◆ add_paths_to_grouping_rel()

static void add_paths_to_grouping_rel ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo grouped_rel,
RelOptInfo partially_grouped_rel,
const AggClauseCosts agg_costs,
grouping_sets_data gd,
double  dNumGroups,
GroupPathExtraData extra 
)
static

Definition at line 6785 of file planner.c.

6791 {
6792  Query *parse = root->parse;
6793  Path *cheapest_path = input_rel->cheapest_total_path;
6794  ListCell *lc;
6795  bool can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
6796  bool can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
6797  List *havingQual = (List *) extra->havingQual;
6798  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
6799 
6800  if (can_sort)
6801  {
6802  /*
6803  * Use any available suitably-sorted path as input, and also consider
6804  * sorting the cheapest-total path and incremental sort on any paths
6805  * with presorted keys.
6806  */
6807  foreach(lc, input_rel->pathlist)
6808  {
6809  ListCell *lc2;
6810  Path *path = (Path *) lfirst(lc);
6811  Path *path_save = path;
6812  List *pathkey_orderings = NIL;
6813 
6814  /* generate alternative group orderings that might be useful */
6815  pathkey_orderings = get_useful_group_keys_orderings(root, path);
6816 
6817  Assert(list_length(pathkey_orderings) > 0);
6818 
6819  foreach(lc2, pathkey_orderings)
6820  {
6821  PathKeyInfo *info = (PathKeyInfo *) lfirst(lc2);
6822 
6823  /* restore the path (we replace it in the loop) */
6824  path = path_save;
6825 
6826  path = make_ordered_path(root,
6827  grouped_rel,
6828  path,
6829  cheapest_path,
6830  info->pathkeys);
6831  if (path == NULL)
6832  continue;
6833 
6834  /* Now decide what to stick atop it */
6835  if (parse->groupingSets)
6836  {
6837  consider_groupingsets_paths(root, grouped_rel,
6838  path, true, can_hash,
6839  gd, agg_costs, dNumGroups);
6840  }
6841  else if (parse->hasAggs)
6842  {
6843  /*
6844  * We have aggregation, possibly with plain GROUP BY. Make
6845  * an AggPath.
6846  */
6847  add_path(grouped_rel, (Path *)
6848  create_agg_path(root,
6849  grouped_rel,
6850  path,
6851  grouped_rel->reltarget,
6852  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
6854  info->clauses,
6855  havingQual,
6856  agg_costs,
6857  dNumGroups));
6858  }
6859  else if (parse->groupClause)
6860  {
6861  /*
6862  * We have GROUP BY without aggregation or grouping sets.
6863  * Make a GroupPath.
6864  */
6865  add_path(grouped_rel, (Path *)
6866  create_group_path(root,
6867  grouped_rel,
6868  path,
6869  info->clauses,
6870  havingQual,
6871  dNumGroups));
6872  }
6873  else
6874  {
6875  /* Other cases should have been handled above */
6876  Assert(false);
6877  }
6878  }
6879  }
6880 
6881  /*
6882  * Instead of operating directly on the input relation, we can
6883  * consider finalizing a partially aggregated path.
6884  */
6885  if (partially_grouped_rel != NULL)
6886  {
6887  foreach(lc, partially_grouped_rel->pathlist)
6888  {
6889  ListCell *lc2;
6890  Path *path = (Path *) lfirst(lc);
6891  Path *path_save = path;
6892  List *pathkey_orderings = NIL;
6893 
6894  /* generate alternative group orderings that might be useful */
6895  pathkey_orderings = get_useful_group_keys_orderings(root, path);
6896 
6897  Assert(list_length(pathkey_orderings) > 0);
6898 
6899  /* process all potentially interesting grouping reorderings */
6900  foreach(lc2, pathkey_orderings)
6901  {
6902  PathKeyInfo *info = (PathKeyInfo *) lfirst(lc2);
6903 
6904  /* restore the path (we replace it in the loop) */
6905  path = path_save;
6906 
6907  path = make_ordered_path(root,
6908  grouped_rel,
6909  path,
6910  partially_grouped_rel->cheapest_total_path,
6911  info->pathkeys);
6912 
6913  if (path == NULL)
6914  continue;
6915 
6916  if (parse->hasAggs)
6917  add_path(grouped_rel, (Path *)
6918  create_agg_path(root,
6919  grouped_rel,
6920  path,
6921  grouped_rel->reltarget,
6922  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
6924  info->clauses,
6925  havingQual,
6926  agg_final_costs,
6927  dNumGroups));
6928  else
6929  add_path(grouped_rel, (Path *)
6930  create_group_path(root,
6931  grouped_rel,
6932  path,
6933  info->clauses,
6934  havingQual,
6935  dNumGroups));
6936 
6937  }
6938  }
6939  }
6940  }
6941 
6942  if (can_hash)
6943  {
6944  if (parse->groupingSets)
6945  {
6946  /*
6947  * Try for a hash-only groupingsets path over unsorted input.
6948  */
6949  consider_groupingsets_paths(root, grouped_rel,
6950  cheapest_path, false, true,
6951  gd, agg_costs, dNumGroups);
6952  }
6953  else
6954  {
6955  /*
6956  * Generate a HashAgg Path. We just need an Agg over the
6957  * cheapest-total input path, since input order won't matter.
6958  */
6959  add_path(grouped_rel, (Path *)
6960  create_agg_path(root, grouped_rel,
6961  cheapest_path,
6962  grouped_rel->reltarget,
6963  AGG_HASHED,
6965  root->processed_groupClause,
6966  havingQual,
6967  agg_costs,
6968  dNumGroups));
6969  }
6970 
6971  /*
6972  * Generate a Finalize HashAgg Path atop of the cheapest partially
6973  * grouped path, assuming there is one
6974  */
6975  if (partially_grouped_rel && partially_grouped_rel->pathlist)
6976  {
6977  Path *path = partially_grouped_rel->cheapest_total_path;
6978 
6979  add_path(grouped_rel, (Path *)
6980  create_agg_path(root,
6981  grouped_rel,
6982  path,
6983  grouped_rel->reltarget,
6984  AGG_HASHED,
6986  root->processed_groupClause,
6987  havingQual,
6988  agg_final_costs,
6989  dNumGroups));
6990  }
6991  }
6992 
6993  /*
6994  * When partitionwise aggregate is used, we might have fully aggregated
6995  * paths in the partial pathlist, because add_paths_to_append_rel() will
6996  * consider a path for grouped_rel consisting of a Parallel Append of
6997  * non-partial paths from each child.
6998  */
6999  if (grouped_rel->partial_pathlist != NIL)
7000  gather_grouping_paths(root, grouped_rel);
7001 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
@ AGG_SORTED
Definition: nodes.h:344
@ AGG_HASHED
Definition: nodes.h:345
@ AGG_PLAIN
Definition: nodes.h:343
@ AGGSPLIT_FINAL_DESERIAL
Definition: nodes.h:370
@ AGGSPLIT_SIMPLE
Definition: nodes.h:366
List * get_useful_group_keys_orderings(PlannerInfo *root, Path *path)
Definition: pathkeys.c:485
GroupPath * create_group_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *groupClause, List *qual, double numGroups)
Definition: pathnode.c:3030
AggPath * create_agg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, AggStrategy aggstrategy, AggSplit aggsplit, List *groupClause, List *qual, const AggClauseCosts *aggcosts, double numGroups)
Definition: pathnode.c:3141
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:418
#define GROUPING_CAN_USE_HASH
Definition: pathnodes.h:3228
#define GROUPING_CAN_USE_SORT
Definition: pathnodes.h:3227
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static void gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel)
Definition: planner.c:7319
static void consider_groupingsets_paths(PlannerInfo *root, RelOptInfo *grouped_rel, Path *path, bool is_sorted, bool can_hash, grouping_sets_data *gd, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: planner.c:3991
static Path * make_ordered_path(PlannerInfo *root, RelOptInfo *rel, Path *path, Path *cheapest_path, List *pathkeys)
Definition: planner.c:6734
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715
AggClauseCosts agg_final_costs
Definition: pathnodes.h:3268
Definition: pg_list.h:54
List * pathkeys
Definition: pathnodes.h:1467
List * clauses
Definition: pathnodes.h:1468
List * processed_groupClause
Definition: pathnodes.h:430
Query * parse
Definition: pathnodes.h:199
struct PathTarget * reltarget
Definition: pathnodes.h:878
List * pathlist
Definition: pathnodes.h:883
struct Path * cheapest_total_path
Definition: pathnodes.h:887
List * partial_pathlist
Definition: pathnodes.h:885

References add_path(), GroupPathExtraData::agg_final_costs, AGG_HASHED, AGG_PLAIN, AGG_SORTED, AGGSPLIT_FINAL_DESERIAL, AGGSPLIT_SIMPLE, Assert(), RelOptInfo::cheapest_total_path, PathKeyInfo::clauses, consider_groupingsets_paths(), create_agg_path(), create_group_path(), GroupPathExtraData::flags, gather_grouping_paths(), get_useful_group_keys_orderings(), GROUPING_CAN_USE_HASH, GROUPING_CAN_USE_SORT, GroupPathExtraData::havingQual, if(), lfirst, list_length(), make_ordered_path(), NIL, parse(), PlannerInfo::parse, RelOptInfo::partial_pathlist, PathKeyInfo::pathkeys, RelOptInfo::pathlist, PlannerInfo::processed_groupClause, and RelOptInfo::reltarget.

Referenced by create_ordinary_grouping_paths().

◆ adjust_group_pathkeys_for_groupagg()

static void adjust_group_pathkeys_for_groupagg ( PlannerInfo root)
static

Definition at line 3141 of file planner.c.

3142 {
3143  List *grouppathkeys = root->group_pathkeys;
3144  List *bestpathkeys;
3145  Bitmapset *bestaggs;
3146  Bitmapset *unprocessed_aggs;
3147  ListCell *lc;
3148  int i;
3149 
3150  /* Shouldn't be here if there are grouping sets */
3151  Assert(root->parse->groupingSets == NIL);
3152  /* Shouldn't be here unless there are some ordered aggregates */
3153  Assert(root->numOrderedAggs > 0);
3154 
3155  /* Do nothing if disabled */
3157  return;
3158 
3159  /*
3160  * Make a first pass over all AggInfos to collect a Bitmapset containing
3161  * the indexes of all AggInfos to be processed below.
3162  */
3163  unprocessed_aggs = NULL;
3164  foreach(lc, root->agginfos)
3165  {
3166  AggInfo *agginfo = lfirst_node(AggInfo, lc);
3167  Aggref *aggref = linitial_node(Aggref, agginfo->aggrefs);
3168 
3169  if (AGGKIND_IS_ORDERED_SET(aggref->aggkind))
3170  continue;
3171 
3172  /* only add aggregates with a DISTINCT or ORDER BY */
3173  if (aggref->aggdistinct != NIL || aggref->aggorder != NIL)
3174  unprocessed_aggs = bms_add_member(unprocessed_aggs,
3175  foreach_current_index(lc));
3176  }
3177 
3178  /*
3179  * Now process all the unprocessed_aggs to find the best set of pathkeys
3180  * for the given set of aggregates.
3181  *
3182  * On the first outer loop here 'bestaggs' will be empty. We'll populate
3183  * this during the first loop using the pathkeys for the very first
3184  * AggInfo then taking any stronger pathkeys from any other AggInfos with
3185  * a more strict set of compatible pathkeys. Once the outer loop is
3186  * complete, we mark off all the aggregates with compatible pathkeys then
3187  * remove those from the unprocessed_aggs and repeat the process to try to
3188  * find another set of pathkeys that are suitable for a larger number of
3189  * aggregates. The outer loop will stop when there are not enough
3190  * unprocessed aggregates for it to be possible to find a set of pathkeys
3191  * to suit a larger number of aggregates.
3192  */
3193  bestpathkeys = NIL;
3194  bestaggs = NULL;
3195  while (bms_num_members(unprocessed_aggs) > bms_num_members(bestaggs))
3196  {
3197  Bitmapset *aggindexes = NULL;
3198  List *currpathkeys = NIL;
3199 
3200  i = -1;
3201  while ((i = bms_next_member(unprocessed_aggs, i)) >= 0)
3202  {
3203  AggInfo *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3204  Aggref *aggref = linitial_node(Aggref, agginfo->aggrefs);
3205  List *sortlist;
3206  List *pathkeys;
3207 
3208  if (aggref->aggdistinct != NIL)
3209  sortlist = aggref->aggdistinct;
3210  else
3211  sortlist = aggref->aggorder;
3212 
3213  pathkeys = make_pathkeys_for_sortclauses(root, sortlist,
3214  aggref->args);
3215 
3216  /*
3217  * Ignore Aggrefs which have volatile functions in their ORDER BY
3218  * or DISTINCT clause.
3219  */
3220  if (has_volatile_pathkey(pathkeys))
3221  {
3222  unprocessed_aggs = bms_del_member(unprocessed_aggs, i);
3223  continue;
3224  }
3225 
3226  /*
3227  * When not set yet, take the pathkeys from the first unprocessed
3228  * aggregate.
3229  */
3230  if (currpathkeys == NIL)
3231  {
3232  currpathkeys = pathkeys;
3233 
3234  /* include the GROUP BY pathkeys, if they exist */
3235  if (grouppathkeys != NIL)
3236  currpathkeys = append_pathkeys(list_copy(grouppathkeys),
3237  currpathkeys);
3238 
3239  /* record that we found pathkeys for this aggregate */
3240  aggindexes = bms_add_member(aggindexes, i);
3241  }
3242  else
3243  {
3244  /* now look for a stronger set of matching pathkeys */
3245 
3246  /* include the GROUP BY pathkeys, if they exist */
3247  if (grouppathkeys != NIL)
3248  pathkeys = append_pathkeys(list_copy(grouppathkeys),
3249  pathkeys);
3250 
3251  /* are 'pathkeys' compatible or better than 'currpathkeys'? */
3252  switch (compare_pathkeys(currpathkeys, pathkeys))
3253  {
3254  case PATHKEYS_BETTER2:
3255  /* 'pathkeys' are stronger, use these ones instead */
3256  currpathkeys = pathkeys;
3257  /* FALLTHROUGH */
3258 
3259  case PATHKEYS_BETTER1:
3260  /* 'pathkeys' are less strict */
3261  /* FALLTHROUGH */
3262 
3263  case PATHKEYS_EQUAL:
3264  /* mark this aggregate as covered by 'currpathkeys' */
3265  aggindexes = bms_add_member(aggindexes, i);
3266  break;
3267 
3268  case PATHKEYS_DIFFERENT:
3269  break;
3270  }
3271  }
3272  }
3273 
3274  /* remove the aggregates that we've just processed */
3275  unprocessed_aggs = bms_del_members(unprocessed_aggs, aggindexes);
3276 
3277  /*
3278  * If this pass included more aggregates than the previous best then
3279  * use these ones as the best set.
3280  */
3281  if (bms_num_members(aggindexes) > bms_num_members(bestaggs))
3282  {
3283  bestaggs = aggindexes;
3284  bestpathkeys = currpathkeys;
3285  }
3286  }
3287 
3288  /*
3289  * If we found any ordered aggregates, update root->group_pathkeys to add
3290  * the best set of aggregate pathkeys. Note that bestpathkeys includes
3291  * the original GROUP BY pathkeys already.
3292  */
3293  if (bestpathkeys != NIL)
3294  root->group_pathkeys = bestpathkeys;
3295 
3296  /*
3297  * Now that we've found the best set of aggregates we can set the
3298  * presorted flag to indicate to the executor that it needn't bother
3299  * performing a sort for these Aggrefs. We're able to do this now as
3300  * there's no chance of a Hash Aggregate plan as create_grouping_paths
3301  * will not mark the GROUP BY as GROUPING_CAN_USE_HASH due to the presence
3302  * of ordered aggregates.
3303  */
3304  i = -1;
3305  while ((i = bms_next_member(bestaggs, i)) >= 0)
3306  {
3307  AggInfo *agginfo = list_nth_node(AggInfo, root->agginfos, i);
3308 
3309  foreach(lc, agginfo->aggrefs)
3310  {
3311  Aggref *aggref = lfirst_node(Aggref, lc);
3312 
3313  aggref->aggpresorted = true;
3314  }
3315  }
3316 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
bool enable_presorted_aggregate
Definition: costsize.c:153
int i
Definition: isn.c:73
List * list_copy(const List *oldlist)
Definition: list.c:1573
List * append_pathkeys(List *target, List *source)
Definition: pathkeys.c:106
List * make_pathkeys_for_sortclauses(PlannerInfo *root, List *sortclauses, List *tlist)
Definition: pathkeys.c:1347
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:302
@ PATHKEYS_BETTER2
Definition: paths.h:201
@ PATHKEYS_BETTER1
Definition: paths.h:200
@ PATHKEYS_DIFFERENT
Definition: paths.h:202
@ PATHKEYS_EQUAL
Definition: paths.h:199
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define linitial_node(type, l)
Definition: pg_list.h:181
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define list_nth_node(type, list, n)
Definition: pg_list.h:327
static bool has_volatile_pathkey(List *keys)
Definition: planner.c:3096
List * aggrefs
Definition: pathnodes.h:3349
List * aggdistinct
Definition: primnodes.h:460
List * args
Definition: primnodes.h:454
List * aggorder
Definition: primnodes.h:457
int numOrderedAggs
Definition: pathnodes.h:511
List * group_pathkeys
Definition: pathnodes.h:385
List * agginfos
Definition: pathnodes.h:507
List * groupingSets
Definition: parsenodes.h:202

References Aggref::aggdistinct, PlannerInfo::agginfos, Aggref::aggorder, AggInfo::aggrefs, append_pathkeys(), Aggref::args, Assert(), bms_add_member(), bms_del_member(), bms_del_members(), bms_next_member(), bms_num_members(), compare_pathkeys(), enable_presorted_aggregate, foreach_current_index, PlannerInfo::group_pathkeys, Query::groupingSets, has_volatile_pathkey(), i, lfirst_node, linitial_node, list_copy(), list_nth_node, make_pathkeys_for_sortclauses(), NIL, PlannerInfo::numOrderedAggs, PlannerInfo::parse, PATHKEYS_BETTER1, PATHKEYS_BETTER2, PATHKEYS_DIFFERENT, and PATHKEYS_EQUAL.

Referenced by standard_qp_callback().

◆ adjust_paths_for_srfs()

static void adjust_paths_for_srfs ( PlannerInfo root,
RelOptInfo rel,
List targets,
List targets_contain_srfs 
)
static

Definition at line 6284 of file planner.c.

6286 {
6287  ListCell *lc;
6288 
6289  Assert(list_length(targets) == list_length(targets_contain_srfs));
6290  Assert(!linitial_int(targets_contain_srfs));
6291 
6292  /* If no SRFs appear at this plan level, nothing to do */
6293  if (list_length(targets) == 1)
6294  return;
6295 
6296  /*
6297  * Stack SRF-evaluation nodes atop each path for the rel.
6298  *
6299  * In principle we should re-run set_cheapest() here to identify the
6300  * cheapest path, but it seems unlikely that adding the same tlist eval
6301  * costs to all the paths would change that, so we don't bother. Instead,
6302  * just assume that the cheapest-startup and cheapest-total paths remain
6303  * so. (There should be no parameterized paths anymore, so we needn't
6304  * worry about updating cheapest_parameterized_paths.)
6305  */
6306  foreach(lc, rel->pathlist)
6307  {
6308  Path *subpath = (Path *) lfirst(lc);
6309  Path *newpath = subpath;
6310  ListCell *lc1,
6311  *lc2;
6312 
6313  Assert(subpath->param_info == NULL);
6314  forboth(lc1, targets, lc2, targets_contain_srfs)
6315  {
6316  PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6317  bool contains_srfs = (bool) lfirst_int(lc2);
6318 
6319  /* If this level doesn't contain SRFs, do regular projection */
6320  if (contains_srfs)
6321  newpath = (Path *) create_set_projection_path(root,
6322  rel,
6323  newpath,
6324  thistarget);
6325  else
6326  newpath = (Path *) apply_projection_to_path(root,
6327  rel,
6328  newpath,
6329  thistarget);
6330  }
6331  lfirst(lc) = newpath;
6332  if (subpath == rel->cheapest_startup_path)
6333  rel->cheapest_startup_path = newpath;
6334  if (subpath == rel->cheapest_total_path)
6335  rel->cheapest_total_path = newpath;
6336  }
6337 
6338  /* Likewise for partial paths, if any */
6339  foreach(lc, rel->partial_pathlist)
6340  {
6341  Path *subpath = (Path *) lfirst(lc);
6342  Path *newpath = subpath;
6343  ListCell *lc1,
6344  *lc2;
6345 
6346  Assert(subpath->param_info == NULL);
6347  forboth(lc1, targets, lc2, targets_contain_srfs)
6348  {
6349  PathTarget *thistarget = lfirst_node(PathTarget, lc1);
6350  bool contains_srfs = (bool) lfirst_int(lc2);
6351 
6352  /* If this level doesn't contain SRFs, do regular projection */
6353  if (contains_srfs)
6354  newpath = (Path *) create_set_projection_path(root,
6355  rel,
6356  newpath,
6357  thistarget);
6358  else
6359  {
6360  /* avoid apply_projection_to_path, in case of multiple refs */
6361  newpath = (Path *) create_projection_path(root,
6362  rel,
6363  newpath,
6364  thistarget);
6365  }
6366  }
6367  lfirst(lc) = newpath;
6368  }
6369 }
unsigned char bool
Definition: c.h:443
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
ProjectionPath * create_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2671
ProjectSetPath * create_set_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2868
Path * apply_projection_to_path(PlannerInfo *root, RelOptInfo *rel, Path *path, PathTarget *target)
Definition: pathnode.c:2779
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lfirst_int(lc)
Definition: pg_list.h:173
#define linitial_int(l)
Definition: pg_list.h:179
struct Path * cheapest_startup_path
Definition: pathnodes.h:886

References apply_projection_to_path(), Assert(), RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, create_projection_path(), create_set_projection_path(), forboth, lfirst, lfirst_int, lfirst_node, linitial_int, list_length(), RelOptInfo::partial_pathlist, RelOptInfo::pathlist, and subpath().

Referenced by apply_scanjoin_target_to_paths(), and grouping_planner().

◆ apply_scanjoin_target_to_paths()

static void apply_scanjoin_target_to_paths ( PlannerInfo root,
RelOptInfo rel,
List scanjoin_targets,
List scanjoin_targets_contain_srfs,
bool  scanjoin_target_parallel_safe,
bool  tlist_same_exprs 
)
static

Definition at line 7446 of file planner.c.

7452 {
7453  bool rel_is_partitioned = IS_PARTITIONED_REL(rel);
7454  PathTarget *scanjoin_target;
7455  ListCell *lc;
7456 
7457  /* This recurses, so be paranoid. */
7459 
7460  /*
7461  * If the rel is partitioned, we want to drop its existing paths and
7462  * generate new ones. This function would still be correct if we kept the
7463  * existing paths: we'd modify them to generate the correct target above
7464  * the partitioning Append, and then they'd compete on cost with paths
7465  * generating the target below the Append. However, in our current cost
7466  * model the latter way is always the same or cheaper cost, so modifying
7467  * the existing paths would just be useless work. Moreover, when the cost
7468  * is the same, varying roundoff errors might sometimes allow an existing
7469  * path to be picked, resulting in undesirable cross-platform plan
7470  * variations. So we drop old paths and thereby force the work to be done
7471  * below the Append, except in the case of a non-parallel-safe target.
7472  *
7473  * Some care is needed, because we have to allow
7474  * generate_useful_gather_paths to see the old partial paths in the next
7475  * stanza. Hence, zap the main pathlist here, then allow
7476  * generate_useful_gather_paths to add path(s) to the main list, and
7477  * finally zap the partial pathlist.
7478  */
7479  if (rel_is_partitioned)
7480  rel->pathlist = NIL;
7481 
7482  /*
7483  * If the scan/join target is not parallel-safe, partial paths cannot
7484  * generate it.
7485  */
7486  if (!scanjoin_target_parallel_safe)
7487  {
7488  /*
7489  * Since we can't generate the final scan/join target in parallel
7490  * workers, this is our last opportunity to use any partial paths that
7491  * exist; so build Gather path(s) that use them and emit whatever the
7492  * current reltarget is. We don't do this in the case where the
7493  * target is parallel-safe, since we will be able to generate superior
7494  * paths by doing it after the final scan/join target has been
7495  * applied.
7496  */
7497  generate_useful_gather_paths(root, rel, false);
7498 
7499  /* Can't use parallel query above this level. */
7500  rel->partial_pathlist = NIL;
7501  rel->consider_parallel = false;
7502  }
7503 
7504  /* Finish dropping old paths for a partitioned rel, per comment above */
7505  if (rel_is_partitioned)
7506  rel->partial_pathlist = NIL;
7507 
7508  /* Extract SRF-free scan/join target. */
7509  scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
7510 
7511  /*
7512  * Apply the SRF-free scan/join target to each existing path.
7513  *
7514  * If the tlist exprs are the same, we can just inject the sortgroupref
7515  * information into the existing pathtargets. Otherwise, replace each
7516  * path with a projection path that generates the SRF-free scan/join
7517  * target. This can't change the ordering of paths within rel->pathlist,
7518  * so we just modify the list in place.
7519  */
7520  foreach(lc, rel->pathlist)
7521  {
7522  Path *subpath = (Path *) lfirst(lc);
7523 
7524  /* Shouldn't have any parameterized paths anymore */
7525  Assert(subpath->param_info == NULL);
7526 
7527  if (tlist_same_exprs)
7528  subpath->pathtarget->sortgrouprefs =
7529  scanjoin_target->sortgrouprefs;
7530  else
7531  {
7532  Path *newpath;
7533 
7534  newpath = (Path *) create_projection_path(root, rel, subpath,
7535  scanjoin_target);
7536  lfirst(lc) = newpath;
7537  }
7538  }
7539 
7540  /* Likewise adjust the targets for any partial paths. */
7541  foreach(lc, rel->partial_pathlist)
7542  {
7543  Path *subpath = (Path *) lfirst(lc);
7544 
7545  /* Shouldn't have any parameterized paths anymore */
7546  Assert(subpath->param_info == NULL);
7547 
7548  if (tlist_same_exprs)
7549  subpath->pathtarget->sortgrouprefs =
7550  scanjoin_target->sortgrouprefs;
7551  else
7552  {
7553  Path *newpath;
7554 
7555  newpath = (Path *) create_projection_path(root, rel, subpath,
7556  scanjoin_target);
7557  lfirst(lc) = newpath;
7558  }
7559  }
7560 
7561  /*
7562  * Now, if final scan/join target contains SRFs, insert ProjectSetPath(s)
7563  * atop each existing path. (Note that this function doesn't look at the
7564  * cheapest-path fields, which is a good thing because they're bogus right
7565  * now.)
7566  */
7567  if (root->parse->hasTargetSRFs)
7568  adjust_paths_for_srfs(root, rel,
7569  scanjoin_targets,
7570  scanjoin_targets_contain_srfs);
7571 
7572  /*
7573  * Update the rel's target to be the final (with SRFs) scan/join target.
7574  * This now matches the actual output of all the paths, and we might get
7575  * confused in createplan.c if they don't agree. We must do this now so
7576  * that any append paths made in the next part will use the correct
7577  * pathtarget (cf. create_append_path).
7578  *
7579  * Note that this is also necessary if GetForeignUpperPaths() gets called
7580  * on the final scan/join relation or on any of its children, since the
7581  * FDW might look at the rel's target to create ForeignPaths.
7582  */
7583  rel->reltarget = llast_node(PathTarget, scanjoin_targets);
7584 
7585  /*
7586  * If the relation is partitioned, recursively apply the scan/join target
7587  * to all partitions, and generate brand-new Append paths in which the
7588  * scan/join target is computed below the Append rather than above it.
7589  * Since Append is not projection-capable, that might save a separate
7590  * Result node, and it also is important for partitionwise aggregate.
7591  */
7592  if (rel_is_partitioned)
7593  {
7594  List *live_children = NIL;
7595  int i;
7596 
7597  /* Adjust each partition. */
7598  i = -1;
7599  while ((i = bms_next_member(rel->live_parts, i)) >= 0)
7600  {
7601  RelOptInfo *child_rel = rel->part_rels[i];
7602  AppendRelInfo **appinfos;
7603  int nappinfos;
7604  List *child_scanjoin_targets = NIL;
7605 
7606  Assert(child_rel != NULL);
7607 
7608  /* Dummy children can be ignored. */
7609  if (IS_DUMMY_REL(child_rel))
7610  continue;
7611 
7612  /* Translate scan/join targets for this child. */
7613  appinfos = find_appinfos_by_relids(root, child_rel->relids,
7614  &nappinfos);
7615  foreach(lc, scanjoin_targets)
7616  {
7617  PathTarget *target = lfirst_node(PathTarget, lc);
7618 
7619  target = copy_pathtarget(target);
7620  target->exprs = (List *)
7622  (Node *) target->exprs,
7623  nappinfos, appinfos);
7624  child_scanjoin_targets = lappend(child_scanjoin_targets,
7625  target);
7626  }
7627  pfree(appinfos);
7628 
7629  /* Recursion does the real work. */
7630  apply_scanjoin_target_to_paths(root, child_rel,
7631  child_scanjoin_targets,
7632  scanjoin_targets_contain_srfs,
7633  scanjoin_target_parallel_safe,
7635 
7636  /* Save non-dummy children for Append paths. */
7637  if (!IS_DUMMY_REL(child_rel))
7638  live_children = lappend(live_children, child_rel);
7639  }
7640 
7641  /* Build new paths for this relation by appending child paths. */
7642  add_paths_to_append_rel(root, rel, live_children);
7643  }
7644 
7645  /*
7646  * Consider generating Gather or Gather Merge paths. We must only do this
7647  * if the relation is parallel safe, and we don't do it for child rels to
7648  * avoid creating multiple Gather nodes within the same plan. We must do
7649  * this after all paths have been generated and before set_cheapest, since
7650  * one of the generated paths may turn out to be the cheapest one.
7651  */
7652  if (rel->consider_parallel && !IS_OTHER_REL(rel))
7653  generate_useful_gather_paths(root, rel, false);
7654 
7655  /*
7656  * Reassess which paths are the cheapest, now that we've potentially added
7657  * new Gather (or Gather Merge) and/or Append (or MergeAppend) paths to
7658  * this relation.
7659  */
7660  set_cheapest(rel);
7661 }
void generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
Definition: allpaths.c:3197
void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel, List *live_childrels)
Definition: allpaths.c:1302
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:733
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:196
List * lappend(List *list, void *datum)
Definition: list.c:339
void pfree(void *pointer)
Definition: mcxt.c:1508
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:240
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1926
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1043
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:839
#define llast_node(type, l)
Definition: pg_list.h:202
static void apply_scanjoin_target_to_paths(PlannerInfo *root, RelOptInfo *rel, List *scanjoin_targets, List *scanjoin_targets_contain_srfs, bool scanjoin_target_parallel_safe, bool tlist_same_exprs)
Definition: planner.c:7446
static void adjust_paths_for_srfs(PlannerInfo *root, RelOptInfo *rel, List *targets, List *targets_contain_srfs)
Definition: planner.c:6284
void check_stack_depth(void)
Definition: postgres.c:3531
Definition: nodes.h:129
List * exprs
Definition: pathnodes.h:1513
Relids relids
Definition: pathnodes.h:856
bool consider_parallel
Definition: pathnodes.h:872
Bitmapset * live_parts
Definition: pathnodes.h:1020
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:218
PathTarget * copy_pathtarget(PathTarget *src)
Definition: tlist.c:657

References add_paths_to_append_rel(), adjust_appendrel_attrs(), adjust_paths_for_srfs(), Assert(), bms_next_member(), check_stack_depth(), RelOptInfo::consider_parallel, copy_pathtarget(), create_projection_path(), PathTarget::exprs, find_appinfos_by_relids(), generate_useful_gather_paths(), i, IS_DUMMY_REL, IS_OTHER_REL, IS_PARTITIONED_REL, lappend(), lfirst, lfirst_node, linitial_node, RelOptInfo::live_parts, llast_node, NIL, PlannerInfo::parse, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, pfree(), RelOptInfo::relids, RelOptInfo::reltarget, set_cheapest(), subpath(), and tlist_same_exprs().

Referenced by grouping_planner().

◆ can_partial_agg()

static bool can_partial_agg ( PlannerInfo root)
static

Definition at line 7404 of file planner.c.

7405 {
7406  Query *parse = root->parse;
7407 
7408  if (!parse->hasAggs && parse->groupClause == NIL)
7409  {
7410  /*
7411  * We don't know how to do parallel aggregation unless we have either
7412  * some aggregates or a grouping clause.
7413  */
7414  return false;
7415  }
7416  else if (parse->groupingSets)
7417  {
7418  /* We don't know how to do grouping sets in parallel. */
7419  return false;
7420  }
7421  else if (root->hasNonPartialAggs || root->hasNonSerialAggs)
7422  {
7423  /* Insufficient support for partial mode. */
7424  return false;
7425  }
7426 
7427  /* Everything looks good. */
7428  return true;
7429 }
bool hasNonPartialAggs
Definition: pathnodes.h:513
bool hasNonSerialAggs
Definition: pathnodes.h:515

References PlannerInfo::hasNonPartialAggs, PlannerInfo::hasNonSerialAggs, NIL, parse(), and PlannerInfo::parse.

Referenced by create_grouping_paths().

◆ common_prefix_cmp()

static int common_prefix_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 5755 of file planner.c.

5756 {
5757  const WindowClauseSortData *wcsa = a;
5758  const WindowClauseSortData *wcsb = b;
5759  ListCell *item_a;
5760  ListCell *item_b;
5761 
5762  forboth(item_a, wcsa->uniqueOrder, item_b, wcsb->uniqueOrder)
5763  {
5766 
5767  if (sca->tleSortGroupRef > scb->tleSortGroupRef)
5768  return -1;
5769  else if (sca->tleSortGroupRef < scb->tleSortGroupRef)
5770  return 1;
5771  else if (sca->sortop > scb->sortop)
5772  return -1;
5773  else if (sca->sortop < scb->sortop)
5774  return 1;
5775  else if (sca->nulls_first && !scb->nulls_first)
5776  return -1;
5777  else if (!sca->nulls_first && scb->nulls_first)
5778  return 1;
5779  /* no need to compare eqop, since it is fully determined by sortop */
5780  }
5781 
5782  if (list_length(wcsa->uniqueOrder) > list_length(wcsb->uniqueOrder))
5783  return -1;
5784  else if (list_length(wcsa->uniqueOrder) < list_length(wcsb->uniqueOrder))
5785  return 1;
5786 
5787  return 0;
5788 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
Index tleSortGroupRef
Definition: parsenodes.h:1397

References a, b, forboth, lfirst_node, list_length(), SortGroupClause::nulls_first, SortGroupClause::sortop, SortGroupClause::tleSortGroupRef, and WindowClauseSortData::uniqueOrder.

Referenced by select_active_windows().

◆ consider_groupingsets_paths()

static void consider_groupingsets_paths ( PlannerInfo root,
RelOptInfo grouped_rel,
Path path,
bool  is_sorted,
bool  can_hash,
grouping_sets_data gd,
const AggClauseCosts agg_costs,
double  dNumGroups 
)
static

Definition at line 3991 of file planner.c.

3999 {
4000  Query *parse = root->parse;
4001  Size hash_mem_limit = get_hash_memory_limit();
4002 
4003  /*
4004  * If we're not being offered sorted input, then only consider plans that
4005  * can be done entirely by hashing.
4006  *
4007  * We can hash everything if it looks like it'll fit in hash_mem. But if
4008  * the input is actually sorted despite not being advertised as such, we
4009  * prefer to make use of that in order to use less memory.
4010  *
4011  * If none of the grouping sets are sortable, then ignore the hash_mem
4012  * limit and generate a path anyway, since otherwise we'll just fail.
4013  */
4014  if (!is_sorted)
4015  {
4016  List *new_rollups = NIL;
4017  RollupData *unhashed_rollup = NULL;
4018  List *sets_data;
4019  List *empty_sets_data = NIL;
4020  List *empty_sets = NIL;
4021  ListCell *lc;
4022  ListCell *l_start = list_head(gd->rollups);
4023  AggStrategy strat = AGG_HASHED;
4024  double hashsize;
4025  double exclude_groups = 0.0;
4026 
4027  Assert(can_hash);
4028 
4029  /*
4030  * If the input is coincidentally sorted usefully (which can happen
4031  * even if is_sorted is false, since that only means that our caller
4032  * has set up the sorting for us), then save some hashtable space by
4033  * making use of that. But we need to watch out for degenerate cases:
4034  *
4035  * 1) If there are any empty grouping sets, then group_pathkeys might
4036  * be NIL if all non-empty grouping sets are unsortable. In this case,
4037  * there will be a rollup containing only empty groups, and the
4038  * pathkeys_contained_in test is vacuously true; this is ok.
4039  *
4040  * XXX: the above relies on the fact that group_pathkeys is generated
4041  * from the first rollup. If we add the ability to consider multiple
4042  * sort orders for grouping input, this assumption might fail.
4043  *
4044  * 2) If there are no empty sets and only unsortable sets, then the
4045  * rollups list will be empty (and thus l_start == NULL), and
4046  * group_pathkeys will be NIL; we must ensure that the vacuously-true
4047  * pathkeys_contained_in test doesn't cause us to crash.
4048  */
4049  if (l_start != NULL &&
4051  {
4052  unhashed_rollup = lfirst_node(RollupData, l_start);
4053  exclude_groups = unhashed_rollup->numGroups;
4054  l_start = lnext(gd->rollups, l_start);
4055  }
4056 
4057  hashsize = estimate_hashagg_tablesize(root,
4058  path,
4059  agg_costs,
4060  dNumGroups - exclude_groups);
4061 
4062  /*
4063  * gd->rollups is empty if we have only unsortable columns to work
4064  * with. Override hash_mem in that case; otherwise, we'll rely on the
4065  * sorted-input case to generate usable mixed paths.
4066  */
4067  if (hashsize > hash_mem_limit && gd->rollups)
4068  return; /* nope, won't fit */
4069 
4070  /*
4071  * We need to burst the existing rollups list into individual grouping
4072  * sets and recompute a groupClause for each set.
4073  */
4074  sets_data = list_copy(gd->unsortable_sets);
4075 
4076  for_each_cell(lc, gd->rollups, l_start)
4077  {
4078  RollupData *rollup = lfirst_node(RollupData, lc);
4079 
4080  /*
4081  * If we find an unhashable rollup that's not been skipped by the
4082  * "actually sorted" check above, we can't cope; we'd need sorted
4083  * input (with a different sort order) but we can't get that here.
4084  * So bail out; we'll get a valid path from the is_sorted case
4085  * instead.
4086  *
4087  * The mere presence of empty grouping sets doesn't make a rollup
4088  * unhashable (see preprocess_grouping_sets), we handle those
4089  * specially below.
4090  */
4091  if (!rollup->hashable)
4092  return;
4093 
4094  sets_data = list_concat(sets_data, rollup->gsets_data);
4095  }
4096  foreach(lc, sets_data)
4097  {
4099  List *gset = gs->set;
4100  RollupData *rollup;
4101 
4102  if (gset == NIL)
4103  {
4104  /* Empty grouping sets can't be hashed. */
4105  empty_sets_data = lappend(empty_sets_data, gs);
4106  empty_sets = lappend(empty_sets, NIL);
4107  }
4108  else
4109  {
4110  rollup = makeNode(RollupData);
4111 
4112  rollup->groupClause = groupclause_apply_groupingset(root, gset);
4113  rollup->gsets_data = list_make1(gs);
4114  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4115  rollup->gsets_data,
4116  gd->tleref_to_colnum_map);
4117  rollup->numGroups = gs->numGroups;
4118  rollup->hashable = true;
4119  rollup->is_hashed = true;
4120  new_rollups = lappend(new_rollups, rollup);
4121  }
4122  }
4123 
4124  /*
4125  * If we didn't find anything nonempty to hash, then bail. We'll
4126  * generate a path from the is_sorted case.
4127  */
4128  if (new_rollups == NIL)
4129  return;
4130 
4131  /*
4132  * If there were empty grouping sets they should have been in the
4133  * first rollup.
4134  */
4135  Assert(!unhashed_rollup || !empty_sets);
4136 
4137  if (unhashed_rollup)
4138  {
4139  new_rollups = lappend(new_rollups, unhashed_rollup);
4140  strat = AGG_MIXED;
4141  }
4142  else if (empty_sets)
4143  {
4144  RollupData *rollup = makeNode(RollupData);
4145 
4146  rollup->groupClause = NIL;
4147  rollup->gsets_data = empty_sets_data;
4148  rollup->gsets = empty_sets;
4149  rollup->numGroups = list_length(empty_sets);
4150  rollup->hashable = false;
4151  rollup->is_hashed = false;
4152  new_rollups = lappend(new_rollups, rollup);
4153  strat = AGG_MIXED;
4154  }
4155 
4156  add_path(grouped_rel, (Path *)
4158  grouped_rel,
4159  path,
4160  (List *) parse->havingQual,
4161  strat,
4162  new_rollups,
4163  agg_costs));
4164  return;
4165  }
4166 
4167  /*
4168  * If we have sorted input but nothing we can do with it, bail.
4169  */
4170  if (gd->rollups == NIL)
4171  return;
4172 
4173  /*
4174  * Given sorted input, we try and make two paths: one sorted and one mixed
4175  * sort/hash. (We need to try both because hashagg might be disabled, or
4176  * some columns might not be sortable.)
4177  *
4178  * can_hash is passed in as false if some obstacle elsewhere (such as
4179  * ordered aggs) means that we shouldn't consider hashing at all.
4180  */
4181  if (can_hash && gd->any_hashable)
4182  {
4183  List *rollups = NIL;
4184  List *hash_sets = list_copy(gd->unsortable_sets);
4185  double availspace = hash_mem_limit;
4186  ListCell *lc;
4187 
4188  /*
4189  * Account first for space needed for groups we can't sort at all.
4190  */
4191  availspace -= estimate_hashagg_tablesize(root,
4192  path,
4193  agg_costs,
4194  gd->dNumHashGroups);
4195 
4196  if (availspace > 0 && list_length(gd->rollups) > 1)
4197  {
4198  double scale;
4199  int num_rollups = list_length(gd->rollups);
4200  int k_capacity;
4201  int *k_weights = palloc(num_rollups * sizeof(int));
4202  Bitmapset *hash_items = NULL;
4203  int i;
4204 
4205  /*
4206  * We treat this as a knapsack problem: the knapsack capacity
4207  * represents hash_mem, the item weights are the estimated memory
4208  * usage of the hashtables needed to implement a single rollup,
4209  * and we really ought to use the cost saving as the item value;
4210  * however, currently the costs assigned to sort nodes don't
4211  * reflect the comparison costs well, and so we treat all items as
4212  * of equal value (each rollup we hash instead saves us one sort).
4213  *
4214  * To use the discrete knapsack, we need to scale the values to a
4215  * reasonably small bounded range. We choose to allow a 5% error
4216  * margin; we have no more than 4096 rollups in the worst possible
4217  * case, which with a 5% error margin will require a bit over 42MB
4218  * of workspace. (Anyone wanting to plan queries that complex had
4219  * better have the memory for it. In more reasonable cases, with
4220  * no more than a couple of dozen rollups, the memory usage will
4221  * be negligible.)
4222  *
4223  * k_capacity is naturally bounded, but we clamp the values for
4224  * scale and weight (below) to avoid overflows or underflows (or
4225  * uselessly trying to use a scale factor less than 1 byte).
4226  */
4227  scale = Max(availspace / (20.0 * num_rollups), 1.0);
4228  k_capacity = (int) floor(availspace / scale);
4229 
4230  /*
4231  * We leave the first rollup out of consideration since it's the
4232  * one that matches the input sort order. We assign indexes "i"
4233  * to only those entries considered for hashing; the second loop,
4234  * below, must use the same condition.
4235  */
4236  i = 0;
4237  for_each_from(lc, gd->rollups, 1)
4238  {
4239  RollupData *rollup = lfirst_node(RollupData, lc);
4240 
4241  if (rollup->hashable)
4242  {
4243  double sz = estimate_hashagg_tablesize(root,
4244  path,
4245  agg_costs,
4246  rollup->numGroups);
4247 
4248  /*
4249  * If sz is enormous, but hash_mem (and hence scale) is
4250  * small, avoid integer overflow here.
4251  */
4252  k_weights[i] = (int) Min(floor(sz / scale),
4253  k_capacity + 1.0);
4254  ++i;
4255  }
4256  }
4257 
4258  /*
4259  * Apply knapsack algorithm; compute the set of items which
4260  * maximizes the value stored (in this case the number of sorts
4261  * saved) while keeping the total size (approximately) within
4262  * capacity.
4263  */
4264  if (i > 0)
4265  hash_items = DiscreteKnapsack(k_capacity, i, k_weights, NULL);
4266 
4267  if (!bms_is_empty(hash_items))
4268  {
4269  rollups = list_make1(linitial(gd->rollups));
4270 
4271  i = 0;
4272  for_each_from(lc, gd->rollups, 1)
4273  {
4274  RollupData *rollup = lfirst_node(RollupData, lc);
4275 
4276  if (rollup->hashable)
4277  {
4278  if (bms_is_member(i, hash_items))
4279  hash_sets = list_concat(hash_sets,
4280  rollup->gsets_data);
4281  else
4282  rollups = lappend(rollups, rollup);
4283  ++i;
4284  }
4285  else
4286  rollups = lappend(rollups, rollup);
4287  }
4288  }
4289  }
4290 
4291  if (!rollups && hash_sets)
4292  rollups = list_copy(gd->rollups);
4293 
4294  foreach(lc, hash_sets)
4295  {
4297  RollupData *rollup = makeNode(RollupData);
4298 
4299  Assert(gs->set != NIL);
4300 
4301  rollup->groupClause = groupclause_apply_groupingset(root, gs->set);
4302  rollup->gsets_data = list_make1(gs);
4303  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
4304  rollup->gsets_data,
4305  gd->tleref_to_colnum_map);
4306  rollup->numGroups = gs->numGroups;
4307  rollup->hashable = true;
4308  rollup->is_hashed = true;
4309  rollups = lcons(rollup, rollups);
4310  }
4311 
4312  if (rollups)
4313  {
4314  add_path(grouped_rel, (Path *)
4316  grouped_rel,
4317  path,
4318  (List *) parse->havingQual,
4319  AGG_MIXED,
4320  rollups,
4321  agg_costs));
4322  }
4323  }
4324 
4325  /*
4326  * Now try the simple sorted case.
4327  */
4328  if (!gd->unsortable_sets)
4329  add_path(grouped_rel, (Path *)
4331  grouped_rel,
4332  path,
4333  (List *) parse->havingQual,
4334  AGG_SORTED,
4335  gd->rollups,
4336  agg_costs));
4337 }
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
#define bms_is_empty(a)
Definition: bitmapset.h:118
#define Min(x, y)
Definition: c.h:991
#define Max(x, y)
Definition: c.h:985
size_t Size
Definition: c.h:592
Bitmapset * DiscreteKnapsack(int max_weight, int num_items, int *item_weights, double *item_values)
Definition: knapsack.c:52
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * lcons(void *datum, List *list)
Definition: list.c:495
void * palloc(Size size)
Definition: mcxt.c:1304
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3595
AggStrategy
Definition: nodes.h:342
@ AGG_MIXED
Definition: nodes.h:346
#define makeNode(_type_)
Definition: nodes.h:155
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:341
GroupingSetsPath * create_groupingsets_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *having_qual, AggStrategy aggstrategy, List *rollups, const AggClauseCosts *agg_costs)
Definition: pathnode.c:3223
#define list_make1(x1)
Definition: pg_list.h:212
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:438
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
#define linitial(l)
Definition: pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
int scale
Definition: pgbench.c:181
static List * groupclause_apply_groupingset(PlannerInfo *root, List *force)
Definition: planner.c:2800
static List * remap_to_groupclause_idx(List *groupClause, List *gsets, int *tleref_to_colnum_map)
Definition: planner.c:2196
double estimate_hashagg_tablesize(PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: selfuncs.c:3917
Cardinality numGroups
Definition: pathnodes.h:2253
List * pathkeys
Definition: pathnodes.h:1645
Cardinality numGroups
Definition: pathnodes.h:2264
List * groupClause
Definition: pathnodes.h:2261
List * gsets_data
Definition: pathnodes.h:2263
bool hashable
Definition: pathnodes.h:2265
List * gsets
Definition: pathnodes.h:2262
bool is_hashed
Definition: pathnodes.h:2266
int * tleref_to_colnum_map
Definition: planner.c:103
List * rollups
Definition: planner.c:96
List * unsortable_sets
Definition: planner.c:102
double dNumHashGroups
Definition: planner.c:98

References add_path(), AGG_HASHED, AGG_MIXED, AGG_SORTED, grouping_sets_data::any_hashable, Assert(), bms_is_empty, bms_is_member(), create_groupingsets_path(), DiscreteKnapsack(), grouping_sets_data::dNumHashGroups, estimate_hashagg_tablesize(), for_each_cell, for_each_from, get_hash_memory_limit(), PlannerInfo::group_pathkeys, RollupData::groupClause, groupclause_apply_groupingset(), RollupData::gsets, RollupData::gsets_data, RollupData::hashable, i, RollupData::is_hashed, lappend(), lcons(), lfirst_node, linitial, list_concat(), list_copy(), list_head(), list_length(), list_make1, lnext(), makeNode, Max, Min, NIL, GroupingSetData::numGroups, RollupData::numGroups, palloc(), parse(), PlannerInfo::parse, Path::pathkeys, pathkeys_contained_in(), remap_to_groupclause_idx(), grouping_sets_data::rollups, scale, GroupingSetData::set, grouping_sets_data::tleref_to_colnum_map, and grouping_sets_data::unsortable_sets.

Referenced by add_paths_to_grouping_rel().

◆ create_degenerate_grouping_paths()

static void create_degenerate_grouping_paths ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo grouped_rel 
)
static

Definition at line 3788 of file planner.c.

3790 {
3791  Query *parse = root->parse;
3792  int nrows;
3793  Path *path;
3794 
3795  nrows = list_length(parse->groupingSets);
3796  if (nrows > 1)
3797  {
3798  /*
3799  * Doesn't seem worthwhile writing code to cons up a generate_series
3800  * or a values scan to emit multiple rows. Instead just make N clones
3801  * and append them. (With a volatile HAVING clause, this means you
3802  * might get between 0 and N output rows. Offhand I think that's
3803  * desired.)
3804  */
3805  List *paths = NIL;
3806 
3807  while (--nrows >= 0)
3808  {
3809  path = (Path *)
3810  create_group_result_path(root, grouped_rel,
3811  grouped_rel->reltarget,
3812  (List *) parse->havingQual);
3813  paths = lappend(paths, path);
3814  }
3815  path = (Path *)
3816  create_append_path(root,
3817  grouped_rel,
3818  paths,
3819  NIL,
3820  NIL,
3821  NULL,
3822  0,
3823  false,
3824  -1);
3825  }
3826  else
3827  {
3828  /* No grouping sets, or just one, so one output row */
3829  path = (Path *)
3830  create_group_result_path(root, grouped_rel,
3831  grouped_rel->reltarget,
3832  (List *) parse->havingQual);
3833  }
3834 
3835  add_path(grouped_rel, path);
3836 }
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, double rows)
Definition: pathnode.c:1242
GroupResultPath * create_group_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *havingqual)
Definition: pathnode.c:1516

References add_path(), create_append_path(), create_group_result_path(), lappend(), list_length(), NIL, parse(), PlannerInfo::parse, and RelOptInfo::reltarget.

Referenced by create_grouping_paths().

◆ create_distinct_paths()

static RelOptInfo * create_distinct_paths ( PlannerInfo root,
RelOptInfo input_rel,
PathTarget target 
)
static

Definition at line 4573 of file planner.c.

4575 {
4576  RelOptInfo *distinct_rel;
4577 
4578  /* For now, do all work in the (DISTINCT, NULL) upperrel */
4579  distinct_rel = fetch_upper_rel(root, UPPERREL_DISTINCT, NULL);
4580 
4581  /*
4582  * We don't compute anything at this level, so distinct_rel will be
4583  * parallel-safe if the input rel is parallel-safe. In particular, if
4584  * there is a DISTINCT ON (...) clause, any path for the input_rel will
4585  * output those expressions, and will not be parallel-safe unless those
4586  * expressions are parallel-safe.
4587  */
4588  distinct_rel->consider_parallel = input_rel->consider_parallel;
4589 
4590  /*
4591  * If the input rel belongs to a single FDW, so does the distinct_rel.
4592  */
4593  distinct_rel->serverid = input_rel->serverid;
4594  distinct_rel->userid = input_rel->userid;
4595  distinct_rel->useridiscurrent = input_rel->useridiscurrent;
4596  distinct_rel->fdwroutine = input_rel->fdwroutine;
4597 
4598  /* build distinct paths based on input_rel's pathlist */
4599  create_final_distinct_paths(root, input_rel, distinct_rel);
4600 
4601  /* now build distinct paths based on input_rel's partial_pathlist */
4602  create_partial_distinct_paths(root, input_rel, distinct_rel, target);
4603 
4604  /* Give a helpful error if we failed to create any paths */
4605  if (distinct_rel->pathlist == NIL)
4606  ereport(ERROR,
4607  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4608  errmsg("could not implement DISTINCT"),
4609  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
4610 
4611  /*
4612  * If there is an FDW that's responsible for all baserels of the query,
4613  * let it consider adding ForeignPaths.
4614  */
4615  if (distinct_rel->fdwroutine &&
4616  distinct_rel->fdwroutine->GetForeignUpperPaths)
4617  distinct_rel->fdwroutine->GetForeignUpperPaths(root,
4619  input_rel,
4620  distinct_rel,
4621  NULL);
4622 
4623  /* Let extensions possibly add some more paths */
4625  (*create_upper_paths_hook) (root, UPPERREL_DISTINCT, input_rel,
4626  distinct_rel, NULL);
4627 
4628  /* Now choose the best path(s) */
4629  set_cheapest(distinct_rel);
4630 
4631  return distinct_rel;
4632 }
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
@ UPPERREL_DISTINCT
Definition: pathnodes.h:77
static RelOptInfo * create_final_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *distinct_rel)
Definition: planner.c:4842
static void create_partial_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *final_distinct_rel, PathTarget *target)
Definition: planner.c:4643
create_upper_paths_hook_type create_upper_paths_hook
Definition: planner.c:73
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1463
bool useridiscurrent
Definition: pathnodes.h:949
Oid userid
Definition: pathnodes.h:947
Oid serverid
Definition: pathnodes.h:945

References RelOptInfo::consider_parallel, create_final_distinct_paths(), create_partial_distinct_paths(), create_upper_paths_hook, ereport, errcode(), errdetail(), errmsg(), ERROR, fetch_upper_rel(), NIL, RelOptInfo::pathlist, RelOptInfo::serverid, set_cheapest(), UPPERREL_DISTINCT, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by grouping_planner().

◆ create_final_distinct_paths()

static RelOptInfo * create_final_distinct_paths ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo distinct_rel 
)
static

Definition at line 4842 of file planner.c.

4844 {
4845  Query *parse = root->parse;
4846  Path *cheapest_input_path = input_rel->cheapest_total_path;
4847  double numDistinctRows;
4848  bool allow_hash;
4849 
4850  /* Estimate number of distinct rows there will be */
4851  if (parse->groupClause || parse->groupingSets || parse->hasAggs ||
4852  root->hasHavingQual)
4853  {
4854  /*
4855  * If there was grouping or aggregation, use the number of input rows
4856  * as the estimated number of DISTINCT rows (ie, assume the input is
4857  * already mostly unique).
4858  */
4859  numDistinctRows = cheapest_input_path->rows;
4860  }
4861  else
4862  {
4863  /*
4864  * Otherwise, the UNIQUE filter has effects comparable to GROUP BY.
4865  */
4866  List *distinctExprs;
4867 
4868  distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
4869  parse->targetList);
4870  numDistinctRows = estimate_num_groups(root, distinctExprs,
4871  cheapest_input_path->rows,
4872  NULL, NULL);
4873  }
4874 
4875  /*
4876  * Consider sort-based implementations of DISTINCT, if possible.
4877  */
4879  {
4880  /*
4881  * Firstly, if we have any adequately-presorted paths, just stick a
4882  * Unique node on those. We also, consider doing an explicit sort of
4883  * the cheapest input path and Unique'ing that. If any paths have
4884  * presorted keys then we'll create an incremental sort atop of those
4885  * before adding a unique node on the top.
4886  *
4887  * When we have DISTINCT ON, we must sort by the more rigorous of
4888  * DISTINCT and ORDER BY, else it won't have the desired behavior.
4889  * Also, if we do have to do an explicit sort, we might as well use
4890  * the more rigorous ordering to avoid a second sort later. (Note
4891  * that the parser will have ensured that one clause is a prefix of
4892  * the other.)
4893  */
4894  List *needed_pathkeys;
4895  ListCell *lc;
4896  double limittuples = root->distinct_pathkeys == NIL ? 1.0 : -1.0;
4897 
4898  if (parse->hasDistinctOn &&
4900  list_length(root->sort_pathkeys))
4901  needed_pathkeys = root->sort_pathkeys;
4902  else
4903  needed_pathkeys = root->distinct_pathkeys;
4904 
4905  foreach(lc, input_rel->pathlist)
4906  {
4907  Path *input_path = (Path *) lfirst(lc);
4908  Path *sorted_path;
4909  bool is_sorted;
4910  int presorted_keys;
4911 
4912  is_sorted = pathkeys_count_contained_in(needed_pathkeys,
4913  input_path->pathkeys,
4914  &presorted_keys);
4915 
4916  if (is_sorted)
4917  sorted_path = input_path;
4918  else
4919  {
4920  /*
4921  * Try at least sorting the cheapest path and also try
4922  * incrementally sorting any path which is partially sorted
4923  * already (no need to deal with paths which have presorted
4924  * keys when incremental sort is disabled unless it's the
4925  * cheapest input path).
4926  */
4927  if (input_path != cheapest_input_path &&
4928  (presorted_keys == 0 || !enable_incremental_sort))
4929  continue;
4930 
4931  /*
4932  * We've no need to consider both a sort and incremental sort.
4933  * We'll just do a sort if there are no presorted keys and an
4934  * incremental sort when there are presorted keys.
4935  */
4936  if (presorted_keys == 0 || !enable_incremental_sort)
4937  sorted_path = (Path *) create_sort_path(root,
4938  distinct_rel,
4939  input_path,
4940  needed_pathkeys,
4941  limittuples);
4942  else
4943  sorted_path = (Path *) create_incremental_sort_path(root,
4944  distinct_rel,
4945  input_path,
4946  needed_pathkeys,
4947  presorted_keys,
4948  limittuples);
4949  }
4950 
4951  /*
4952  * distinct_pathkeys may have become empty if all of the pathkeys
4953  * were determined to be redundant. If all of the pathkeys are
4954  * redundant then each DISTINCT target must only allow a single
4955  * value, therefore all resulting tuples must be identical (or at
4956  * least indistinguishable by an equality check). We can uniquify
4957  * these tuples simply by just taking the first tuple. All we do
4958  * here is add a path to do "LIMIT 1" atop of 'sorted_path'. When
4959  * doing a DISTINCT ON we may still have a non-NIL sort_pathkeys
4960  * list, so we must still only do this with paths which are
4961  * correctly sorted by sort_pathkeys.
4962  */
4963  if (root->distinct_pathkeys == NIL)
4964  {
4965  Node *limitCount;
4966 
4967  limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
4968  sizeof(int64),
4969  Int64GetDatum(1), false,
4970  FLOAT8PASSBYVAL);
4971 
4972  /*
4973  * If the query already has a LIMIT clause, then we could end
4974  * up with a duplicate LimitPath in the final plan. That does
4975  * not seem worth troubling over too much.
4976  */
4977  add_path(distinct_rel, (Path *)
4978  create_limit_path(root, distinct_rel, sorted_path,
4979  NULL, limitCount,
4980  LIMIT_OPTION_COUNT, 0, 1));
4981  }
4982  else
4983  {
4984  add_path(distinct_rel, (Path *)
4985  create_upper_unique_path(root, distinct_rel,
4986  sorted_path,
4988  numDistinctRows));
4989  }
4990  }
4991  }
4992 
4993  /*
4994  * Consider hash-based implementations of DISTINCT, if possible.
4995  *
4996  * If we were not able to make any other types of path, we *must* hash or
4997  * die trying. If we do have other choices, there are two things that
4998  * should prevent selection of hashing: if the query uses DISTINCT ON
4999  * (because it won't really have the expected behavior if we hash), or if
5000  * enable_hashagg is off.
5001  *
5002  * Note: grouping_is_hashable() is much more expensive to check than the
5003  * other gating conditions, so we want to do it last.
5004  */
5005  if (distinct_rel->pathlist == NIL)
5006  allow_hash = true; /* we have no alternatives */
5007  else if (parse->hasDistinctOn || !enable_hashagg)
5008  allow_hash = false; /* policy-based decision not to hash */
5009  else
5010  allow_hash = true; /* default */
5011 
5012  if (allow_hash && grouping_is_hashable(root->processed_distinctClause))
5013  {
5014  /* Generate hashed aggregate path --- no sort needed */
5015  add_path(distinct_rel, (Path *)
5016  create_agg_path(root,
5017  distinct_rel,
5018  cheapest_input_path,
5019  cheapest_input_path->pathtarget,
5020  AGG_HASHED,
5023  NIL,
5024  NULL,
5025  numDistinctRows));
5026  }
5027 
5028  return distinct_rel;
5029 }
#define FLOAT8PASSBYVAL
Definition: c.h:622
bool enable_hashagg
Definition: costsize.c:141
bool enable_incremental_sort
Definition: costsize.c:140
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
@ LIMIT_OPTION_COUNT
Definition: nodes.h:420
bool pathkeys_count_contained_in(List *keys1, List *keys2, int *n_common)
Definition: pathkeys.c:573
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
Definition: pathnode.c:2986
LimitPath * create_limit_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, Node *limitOffset, Node *limitCount, LimitOption limitOption, int64 offset_est, int64 count_est)
Definition: pathnode.c:3806
UpperUniquePath * create_upper_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, int numCols, double numGroups)
Definition: pathnode.c:3089
IncrementalSortPath * create_incremental_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, int presorted_keys, double limit_tuples)
Definition: pathnode.c:2937
#define InvalidOid
Definition: postgres_ext.h:36
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition: selfuncs.c:3416
Cardinality rows
Definition: pathnodes.h:1640
List * distinct_pathkeys
Definition: pathnodes.h:397
bool hasHavingQual
Definition: pathnodes.h:493
List * sort_pathkeys
Definition: pathnodes.h:399
List * processed_distinctClause
Definition: pathnodes.h:442
List * get_sortgrouplist_exprs(List *sgClauses, List *targetList)
Definition: tlist.c:392
bool grouping_is_sortable(List *groupClause)
Definition: tlist.c:540
bool grouping_is_hashable(List *groupClause)
Definition: tlist.c:560

References add_path(), AGG_HASHED, AGGSPLIT_SIMPLE, RelOptInfo::cheapest_total_path, create_agg_path(), create_incremental_sort_path(), create_limit_path(), create_sort_path(), create_upper_unique_path(), PlannerInfo::distinct_pathkeys, enable_hashagg, enable_incremental_sort, estimate_num_groups(), FLOAT8PASSBYVAL, get_sortgrouplist_exprs(), grouping_is_hashable(), grouping_is_sortable(), PlannerInfo::hasHavingQual, Int64GetDatum(), InvalidOid, lfirst, LIMIT_OPTION_COUNT, list_length(), makeConst(), NIL, parse(), PlannerInfo::parse, Path::pathkeys, pathkeys_count_contained_in(), RelOptInfo::pathlist, PlannerInfo::processed_distinctClause, Path::rows, and PlannerInfo::sort_pathkeys.

Referenced by create_distinct_paths(), and create_partial_distinct_paths().

◆ create_grouping_paths()

static RelOptInfo * create_grouping_paths ( PlannerInfo root,
RelOptInfo input_rel,
PathTarget target,
bool  target_parallel_safe,
grouping_sets_data gd 
)
static

Definition at line 3601 of file planner.c.

3606 {
3607  Query *parse = root->parse;
3608  RelOptInfo *grouped_rel;
3609  RelOptInfo *partially_grouped_rel;
3610  AggClauseCosts agg_costs;
3611 
3612  MemSet(&agg_costs, 0, sizeof(AggClauseCosts));
3613  get_agg_clause_costs(root, AGGSPLIT_SIMPLE, &agg_costs);
3614 
3615  /*
3616  * Create grouping relation to hold fully aggregated grouping and/or
3617  * aggregation paths.
3618  */
3619  grouped_rel = make_grouping_rel(root, input_rel, target,
3620  target_parallel_safe, parse->havingQual);
3621 
3622  /*
3623  * Create either paths for a degenerate grouping or paths for ordinary
3624  * grouping, as appropriate.
3625  */
3626  if (is_degenerate_grouping(root))
3627  create_degenerate_grouping_paths(root, input_rel, grouped_rel);
3628  else
3629  {
3630  int flags = 0;
3631  GroupPathExtraData extra;
3632 
3633  /*
3634  * Determine whether it's possible to perform sort-based
3635  * implementations of grouping. (Note that if processed_groupClause
3636  * is empty, grouping_is_sortable() is trivially true, and all the
3637  * pathkeys_contained_in() tests will succeed too, so that we'll
3638  * consider every surviving input path.)
3639  *
3640  * If we have grouping sets, we might be able to sort some but not all
3641  * of them; in this case, we need can_sort to be true as long as we
3642  * must consider any sorted-input plan.
3643  */
3644  if ((gd && gd->rollups != NIL)
3646  flags |= GROUPING_CAN_USE_SORT;
3647 
3648  /*
3649  * Determine whether we should consider hash-based implementations of
3650  * grouping.
3651  *
3652  * Hashed aggregation only applies if we're grouping. If we have
3653  * grouping sets, some groups might be hashable but others not; in
3654  * this case we set can_hash true as long as there is nothing globally
3655  * preventing us from hashing (and we should therefore consider plans
3656  * with hashes).
3657  *
3658  * Executor doesn't support hashed aggregation with DISTINCT or ORDER
3659  * BY aggregates. (Doing so would imply storing *all* the input
3660  * values in the hash table, and/or running many sorts in parallel,
3661  * either of which seems like a certain loser.) We similarly don't
3662  * support ordered-set aggregates in hashed aggregation, but that case
3663  * is also included in the numOrderedAggs count.
3664  *
3665  * Note: grouping_is_hashable() is much more expensive to check than
3666  * the other gating conditions, so we want to do it last.
3667  */
3668  if ((parse->groupClause != NIL &&
3669  root->numOrderedAggs == 0 &&
3671  flags |= GROUPING_CAN_USE_HASH;
3672 
3673  /*
3674  * Determine whether partial aggregation is possible.
3675  */
3676  if (can_partial_agg(root))
3677  flags |= GROUPING_CAN_PARTIAL_AGG;
3678 
3679  extra.flags = flags;
3680  extra.target_parallel_safe = target_parallel_safe;
3681  extra.havingQual = parse->havingQual;
3682  extra.targetList = parse->targetList;
3683  extra.partial_costs_set = false;
3684 
3685  /*
3686  * Determine whether partitionwise aggregation is in theory possible.
3687  * It can be disabled by the user, and for now, we don't try to
3688  * support grouping sets. create_ordinary_grouping_paths() will check
3689  * additional conditions, such as whether input_rel is partitioned.
3690  */
3691  if (enable_partitionwise_aggregate && !parse->groupingSets)
3693  else
3695 
3696  create_ordinary_grouping_paths(root, input_rel, grouped_rel,
3697  &agg_costs, gd, &extra,
3698  &partially_grouped_rel);
3699  }
3700 
3701  set_cheapest(grouped_rel);
3702  return grouped_rel;
3703 }
#define MemSet(start, val, len)
Definition: c.h:1007
bool enable_partitionwise_aggregate
Definition: costsize.c:149
@ PARTITIONWISE_AGGREGATE_FULL
Definition: pathnodes.h:3245
@ PARTITIONWISE_AGGREGATE_NONE
Definition: pathnodes.h:3244
#define GROUPING_CAN_PARTIAL_AGG
Definition: pathnodes.h:3229
static void create_degenerate_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel)
Definition: planner.c:3788
static bool is_degenerate_grouping(PlannerInfo *root)
Definition: planner.c:3767
static void create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, GroupPathExtraData *extra, RelOptInfo **partially_grouped_rel_p)
Definition: planner.c:3852
static bool can_partial_agg(PlannerInfo *root)
Definition: planner.c:7404
static RelOptInfo * make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, Node *havingQual)
Definition: planner.c:3714
void get_agg_clause_costs(PlannerInfo *root, AggSplit aggsplit, AggClauseCosts *costs)
Definition: prepagg.c:560
PartitionwiseAggregateType patype
Definition: pathnodes.h:3274

References AGGSPLIT_SIMPLE, grouping_sets_data::any_hashable, can_partial_agg(), create_degenerate_grouping_paths(), create_ordinary_grouping_paths(), enable_partitionwise_aggregate, GroupPathExtraData::flags, get_agg_clause_costs(), GROUPING_CAN_PARTIAL_AGG, GROUPING_CAN_USE_HASH, GROUPING_CAN_USE_SORT, grouping_is_hashable(), grouping_is_sortable(), GroupPathExtraData::havingQual, is_degenerate_grouping(), make_grouping_rel(), MemSet, NIL, PlannerInfo::numOrderedAggs, parse(), PlannerInfo::parse, GroupPathExtraData::partial_costs_set, PARTITIONWISE_AGGREGATE_FULL, PARTITIONWISE_AGGREGATE_NONE, GroupPathExtraData::patype, PlannerInfo::processed_groupClause, grouping_sets_data::rollups, set_cheapest(), GroupPathExtraData::target_parallel_safe, and GroupPathExtraData::targetList.

Referenced by grouping_planner().

◆ create_one_window_path()

static void create_one_window_path ( PlannerInfo root,
RelOptInfo window_rel,
Path path,
PathTarget input_target,
PathTarget output_target,
WindowFuncLists wflists,
List activeWindows 
)
static

Definition at line 4440 of file planner.c.

4447 {
4448  PathTarget *window_target;
4449  ListCell *l;
4450  List *topqual = NIL;
4451 
4452  /*
4453  * Since each window clause could require a different sort order, we stack
4454  * up a WindowAgg node for each clause, with sort steps between them as
4455  * needed. (We assume that select_active_windows chose a good order for
4456  * executing the clauses in.)
4457  *
4458  * input_target should contain all Vars and Aggs needed for the result.
4459  * (In some cases we wouldn't need to propagate all of these all the way
4460  * to the top, since they might only be needed as inputs to WindowFuncs.
4461  * It's probably not worth trying to optimize that though.) It must also
4462  * contain all window partitioning and sorting expressions, to ensure
4463  * they're computed only once at the bottom of the stack (that's critical
4464  * for volatile functions). As we climb up the stack, we'll add outputs
4465  * for the WindowFuncs computed at each level.
4466  */
4467  window_target = input_target;
4468 
4469  foreach(l, activeWindows)
4470  {
4472  List *window_pathkeys;
4473  int presorted_keys;
4474  bool is_sorted;
4475  bool topwindow;
4476 
4477  window_pathkeys = make_pathkeys_for_window(root,
4478  wc,
4479  root->processed_tlist);
4480 
4481  is_sorted = pathkeys_count_contained_in(window_pathkeys,
4482  path->pathkeys,
4483  &presorted_keys);
4484 
4485  /* Sort if necessary */
4486  if (!is_sorted)
4487  {
4488  /*
4489  * No presorted keys or incremental sort disabled, just perform a
4490  * complete sort.
4491  */
4492  if (presorted_keys == 0 || !enable_incremental_sort)
4493  path = (Path *) create_sort_path(root, window_rel,
4494  path,
4495  window_pathkeys,
4496  -1.0);
4497  else
4498  {
4499  /*
4500  * Since we have presorted keys and incremental sort is
4501  * enabled, just use incremental sort.
4502  */
4503  path = (Path *) create_incremental_sort_path(root,
4504  window_rel,
4505  path,
4506  window_pathkeys,
4507  presorted_keys,
4508  -1.0);
4509  }
4510  }
4511 
4512  if (lnext(activeWindows, l))
4513  {
4514  /*
4515  * Add the current WindowFuncs to the output target for this
4516  * intermediate WindowAggPath. We must copy window_target to
4517  * avoid changing the previous path's target.
4518  *
4519  * Note: a WindowFunc adds nothing to the target's eval costs; but
4520  * we do need to account for the increase in tlist width.
4521  */
4522  int64 tuple_width = window_target->width;
4523  ListCell *lc2;
4524 
4525  window_target = copy_pathtarget(window_target);
4526  foreach(lc2, wflists->windowFuncs[wc->winref])
4527  {
4528  WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
4529 
4530  add_column_to_pathtarget(window_target, (Expr *) wfunc, 0);
4531  tuple_width += get_typavgwidth(wfunc->wintype, -1);
4532  }
4533  window_target->width = clamp_width_est(tuple_width);
4534  }
4535  else
4536  {
4537  /* Install the goal target in the topmost WindowAgg */
4538  window_target = output_target;
4539  }
4540 
4541  /* mark the final item in the list as the top-level window */
4542  topwindow = foreach_current_index(l) == list_length(activeWindows) - 1;
4543 
4544  /*
4545  * Accumulate all of the runConditions from each intermediate
4546  * WindowClause. The top-level WindowAgg must pass these as a qual so
4547  * that it filters out unwanted tuples correctly.
4548  */
4549  if (!topwindow)
4550  topqual = list_concat(topqual, wc->runCondition);
4551 
4552  path = (Path *)
4553  create_windowagg_path(root, window_rel, path, window_target,
4554  wflists->windowFuncs[wc->winref],
4555  wc, topwindow ? topqual : NIL, topwindow);
4556  }
4557 
4558  add_path(window_rel, path);
4559 }
int32 clamp_width_est(int64 tuple_width)
Definition: costsize.c:231
int32 get_typavgwidth(Oid typid, int32 typmod)
Definition: lsyscache.c:2534
WindowAggPath * create_windowagg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *windowFuncs, WindowClause *winclause, List *qual, bool topwindow)
Definition: pathnode.c:3470
static List * make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc, List *tlist)
Definition: planner.c:5944
List * processed_tlist
Definition: pathnodes.h:453
List ** windowFuncs
Definition: clauses.h:23
void add_column_to_pathtarget(PathTarget *target, Expr *expr, Index sortgroupref)
Definition: tlist.c:695

References add_column_to_pathtarget(), add_path(), clamp_width_est(), copy_pathtarget(), create_incremental_sort_path(), create_sort_path(), create_windowagg_path(), enable_incremental_sort, foreach_current_index, get_typavgwidth(), lfirst_node, list_concat(), list_length(), lnext(), make_pathkeys_for_window(), NIL, Path::pathkeys, pathkeys_count_contained_in(), PlannerInfo::processed_tlist, PathTarget::width, WindowFuncLists::windowFuncs, and WindowClause::winref.

Referenced by create_window_paths().

◆ create_ordered_paths()

static RelOptInfo * create_ordered_paths ( PlannerInfo root,
RelOptInfo input_rel,
PathTarget target,
bool  target_parallel_safe,
double  limit_tuples 
)
static

Definition at line 5049 of file planner.c.

5054 {
5055  Path *cheapest_input_path = input_rel->cheapest_total_path;
5056  RelOptInfo *ordered_rel;
5057  ListCell *lc;
5058 
5059  /* For now, do all work in the (ORDERED, NULL) upperrel */
5060  ordered_rel = fetch_upper_rel(root, UPPERREL_ORDERED, NULL);
5061 
5062  /*
5063  * If the input relation is not parallel-safe, then the ordered relation
5064  * can't be parallel-safe, either. Otherwise, it's parallel-safe if the
5065  * target list is parallel-safe.
5066  */
5067  if (input_rel->consider_parallel && target_parallel_safe)
5068  ordered_rel->consider_parallel = true;
5069 
5070  /*
5071  * If the input rel belongs to a single FDW, so does the ordered_rel.
5072  */
5073  ordered_rel->serverid = input_rel->serverid;
5074  ordered_rel->userid = input_rel->userid;
5075  ordered_rel->useridiscurrent = input_rel->useridiscurrent;
5076  ordered_rel->fdwroutine = input_rel->fdwroutine;
5077 
5078  foreach(lc, input_rel->pathlist)
5079  {
5080  Path *input_path = (Path *) lfirst(lc);
5081  Path *sorted_path;
5082  bool is_sorted;
5083  int presorted_keys;
5084 
5085  is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5086  input_path->pathkeys, &presorted_keys);
5087 
5088  if (is_sorted)
5089  sorted_path = input_path;
5090  else
5091  {
5092  /*
5093  * Try at least sorting the cheapest path and also try
5094  * incrementally sorting any path which is partially sorted
5095  * already (no need to deal with paths which have presorted keys
5096  * when incremental sort is disabled unless it's the cheapest
5097  * input path).
5098  */
5099  if (input_path != cheapest_input_path &&
5100  (presorted_keys == 0 || !enable_incremental_sort))
5101  continue;
5102 
5103  /*
5104  * We've no need to consider both a sort and incremental sort.
5105  * We'll just do a sort if there are no presorted keys and an
5106  * incremental sort when there are presorted keys.
5107  */
5108  if (presorted_keys == 0 || !enable_incremental_sort)
5109  sorted_path = (Path *) create_sort_path(root,
5110  ordered_rel,
5111  input_path,
5112  root->sort_pathkeys,
5113  limit_tuples);
5114  else
5115  sorted_path = (Path *) create_incremental_sort_path(root,
5116  ordered_rel,
5117  input_path,
5118  root->sort_pathkeys,
5119  presorted_keys,
5120  limit_tuples);
5121  }
5122 
5123  /* Add projection step if needed */
5124  if (sorted_path->pathtarget != target)
5125  sorted_path = apply_projection_to_path(root, ordered_rel,
5126  sorted_path, target);
5127 
5128  add_path(ordered_rel, sorted_path);
5129  }
5130 
5131  /*
5132  * generate_gather_paths() will have already generated a simple Gather
5133  * path for the best parallel path, if any, and the loop above will have
5134  * considered sorting it. Similarly, generate_gather_paths() will also
5135  * have generated order-preserving Gather Merge plans which can be used
5136  * without sorting if they happen to match the sort_pathkeys, and the loop
5137  * above will have handled those as well. However, there's one more
5138  * possibility: it may make sense to sort the cheapest partial path or
5139  * incrementally sort any partial path that is partially sorted according
5140  * to the required output order and then use Gather Merge.
5141  */
5142  if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL &&
5143  input_rel->partial_pathlist != NIL)
5144  {
5145  Path *cheapest_partial_path;
5146 
5147  cheapest_partial_path = linitial(input_rel->partial_pathlist);
5148 
5149  foreach(lc, input_rel->partial_pathlist)
5150  {
5151  Path *input_path = (Path *) lfirst(lc);
5152  Path *sorted_path;
5153  bool is_sorted;
5154  int presorted_keys;
5155  double total_groups;
5156 
5157  is_sorted = pathkeys_count_contained_in(root->sort_pathkeys,
5158  input_path->pathkeys,
5159  &presorted_keys);
5160 
5161  if (is_sorted)
5162  continue;
5163 
5164  /*
5165  * Try at least sorting the cheapest path and also try
5166  * incrementally sorting any path which is partially sorted
5167  * already (no need to deal with paths which have presorted keys
5168  * when incremental sort is disabled unless it's the cheapest
5169  * partial path).
5170  */
5171  if (input_path != cheapest_partial_path &&
5172  (presorted_keys == 0 || !enable_incremental_sort))
5173  continue;
5174 
5175  /*
5176  * We've no need to consider both a sort and incremental sort.
5177  * We'll just do a sort if there are no presorted keys and an
5178  * incremental sort when there are presorted keys.
5179  */
5180  if (presorted_keys == 0 || !enable_incremental_sort)
5181  sorted_path = (Path *) create_sort_path(root,
5182  ordered_rel,
5183  input_path,
5184  root->sort_pathkeys,
5185  limit_tuples);
5186  else
5187  sorted_path = (Path *) create_incremental_sort_path(root,
5188  ordered_rel,
5189  input_path,
5190  root->sort_pathkeys,
5191  presorted_keys,
5192  limit_tuples);
5193  total_groups = input_path->rows *
5194  input_path->parallel_workers;
5195  sorted_path = (Path *)
5196  create_gather_merge_path(root, ordered_rel,
5197  sorted_path,
5198  sorted_path->pathtarget,
5199  root->sort_pathkeys, NULL,
5200  &total_groups);
5201 
5202  /* Add projection step if needed */
5203  if (sorted_path->pathtarget != target)
5204  sorted_path = apply_projection_to_path(root, ordered_rel,
5205  sorted_path, target);
5206 
5207  add_path(ordered_rel, sorted_path);
5208  }
5209  }
5210 
5211  /*
5212  * If there is an FDW that's responsible for all baserels of the query,
5213  * let it consider adding ForeignPaths.
5214  */
5215  if (ordered_rel->fdwroutine &&
5216  ordered_rel->fdwroutine->GetForeignUpperPaths)
5217  ordered_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_ORDERED,
5218  input_rel, ordered_rel,
5219  NULL);
5220 
5221  /* Let extensions possibly add some more paths */
5223  (*create_upper_paths_hook) (root, UPPERREL_ORDERED,
5224  input_rel, ordered_rel, NULL);
5225 
5226  /*
5227  * No need to bother with set_cheapest here; grouping_planner does not
5228  * need us to do it.
5229  */
5230  Assert(ordered_rel->pathlist != NIL);
5231 
5232  return ordered_rel;
5233 }
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1878
@ UPPERREL_ORDERED
Definition: pathnodes.h:78
int parallel_workers
Definition: pathnodes.h:1637

References add_path(), apply_projection_to_path(), Assert(), RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_gather_merge_path(), create_incremental_sort_path(), create_sort_path(), create_upper_paths_hook, enable_incremental_sort, fetch_upper_rel(), lfirst, linitial, NIL, Path::parallel_workers, RelOptInfo::partial_pathlist, Path::pathkeys, pathkeys_count_contained_in(), RelOptInfo::pathlist, Path::rows, RelOptInfo::serverid, PlannerInfo::sort_pathkeys, UPPERREL_ORDERED, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by grouping_planner().

◆ create_ordinary_grouping_paths()

static void create_ordinary_grouping_paths ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo grouped_rel,
const AggClauseCosts agg_costs,
grouping_sets_data gd,
GroupPathExtraData extra,
RelOptInfo **  partially_grouped_rel_p 
)
static

Definition at line 3852 of file planner.c.

3858 {
3859  Path *cheapest_path = input_rel->cheapest_total_path;
3860  RelOptInfo *partially_grouped_rel = NULL;
3861  double dNumGroups;
3863 
3864  /*
3865  * If this is the topmost grouping relation or if the parent relation is
3866  * doing some form of partitionwise aggregation, then we may be able to do
3867  * it at this level also. However, if the input relation is not
3868  * partitioned, partitionwise aggregate is impossible.
3869  */
3870  if (extra->patype != PARTITIONWISE_AGGREGATE_NONE &&
3871  IS_PARTITIONED_REL(input_rel))
3872  {
3873  /*
3874  * If this is the topmost relation or if the parent relation is doing
3875  * full partitionwise aggregation, then we can do full partitionwise
3876  * aggregation provided that the GROUP BY clause contains all of the
3877  * partitioning columns at this level. Otherwise, we can do at most
3878  * partial partitionwise aggregation. But if partial aggregation is
3879  * not supported in general then we can't use it for partitionwise
3880  * aggregation either.
3881  *
3882  * Check parse->groupClause not processed_groupClause, because it's
3883  * okay if some of the partitioning columns were proved redundant.
3884  */
3885  if (extra->patype == PARTITIONWISE_AGGREGATE_FULL &&
3886  group_by_has_partkey(input_rel, extra->targetList,
3887  root->parse->groupClause))
3889  else if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
3891  else
3893  }
3894 
3895  /*
3896  * Before generating paths for grouped_rel, we first generate any possible
3897  * partially grouped paths; that way, later code can easily consider both
3898  * parallel and non-parallel approaches to grouping.
3899  */
3900  if ((extra->flags & GROUPING_CAN_PARTIAL_AGG) != 0)
3901  {
3902  bool force_rel_creation;
3903 
3904  /*
3905  * If we're doing partitionwise aggregation at this level, force
3906  * creation of a partially_grouped_rel so we can add partitionwise
3907  * paths to it.
3908  */
3909  force_rel_creation = (patype == PARTITIONWISE_AGGREGATE_PARTIAL);
3910 
3911  partially_grouped_rel =
3913  grouped_rel,
3914  input_rel,
3915  gd,
3916  extra,
3917  force_rel_creation);
3918  }
3919 
3920  /* Set out parameter. */
3921  *partially_grouped_rel_p = partially_grouped_rel;
3922 
3923  /* Apply partitionwise aggregation technique, if possible. */
3924  if (patype != PARTITIONWISE_AGGREGATE_NONE)
3925  create_partitionwise_grouping_paths(root, input_rel, grouped_rel,
3926  partially_grouped_rel, agg_costs,
3927  gd, patype, extra);
3928 
3929  /* If we are doing partial aggregation only, return. */
3931  {
3932  Assert(partially_grouped_rel);
3933 
3934  if (partially_grouped_rel->pathlist)
3935  set_cheapest(partially_grouped_rel);
3936 
3937  return;
3938  }
3939 
3940  /* Gather any partially grouped partial paths. */
3941  if (partially_grouped_rel && partially_grouped_rel->partial_pathlist)
3942  {
3943  gather_grouping_paths(root, partially_grouped_rel);
3944  set_cheapest(partially_grouped_rel);
3945  }
3946 
3947  /*
3948  * Estimate number of groups.
3949  */
3950  dNumGroups = get_number_of_groups(root,
3951  cheapest_path->rows,
3952  gd,
3953  extra->targetList);
3954 
3955  /* Build final grouping paths */
3956  add_paths_to_grouping_rel(root, input_rel, grouped_rel,
3957  partially_grouped_rel, agg_costs, gd,
3958  dNumGroups, extra);
3959 
3960  /* Give a helpful error if we failed to find any implementation */
3961  if (grouped_rel->pathlist == NIL)
3962  ereport(ERROR,
3963  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3964  errmsg("could not implement GROUP BY"),
3965  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
3966 
3967  /*
3968  * If there is an FDW that's responsible for all baserels of the query,
3969  * let it consider adding ForeignPaths.
3970  */
3971  if (grouped_rel->fdwroutine &&
3972  grouped_rel->fdwroutine->GetForeignUpperPaths)
3973  grouped_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_GROUP_AGG,
3974  input_rel, grouped_rel,
3975  extra);
3976 
3977  /* Let extensions possibly add some more paths */
3979  (*create_upper_paths_hook) (root, UPPERREL_GROUP_AGG,
3980  input_rel, grouped_rel,
3981  extra);
3982 }
PartitionwiseAggregateType
Definition: pathnodes.h:3243
@ PARTITIONWISE_AGGREGATE_PARTIAL
Definition: pathnodes.h:3246
@ UPPERREL_GROUP_AGG
Definition: pathnodes.h:74
static RelOptInfo * create_partial_grouping_paths(PlannerInfo *root, RelOptInfo *grouped_rel, RelOptInfo *input_rel, grouping_sets_data *gd, GroupPathExtraData *extra, bool force_rel_creation)
Definition: planner.c:7020
static void add_paths_to_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, double dNumGroups, GroupPathExtraData *extra)
Definition: planner.c:6785
static double get_number_of_groups(PlannerInfo *root, double path_rows, grouping_sets_data *gd, List *target_list)
Definition: planner.c:3479
static void create_partitionwise_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, RelOptInfo *grouped_rel, RelOptInfo *partially_grouped_rel, const AggClauseCosts *agg_costs, grouping_sets_data *gd, PartitionwiseAggregateType patype, GroupPathExtraData *extra)
Definition: planner.c:7681
static bool group_by_has_partkey(RelOptInfo *input_rel, List *targetList, List *groupClause)
Definition: planner.c:7825
List * groupClause
Definition: parsenodes.h:199

References add_paths_to_grouping_rel(), Assert(), RelOptInfo::cheapest_total_path, create_partial_grouping_paths(), create_partitionwise_grouping_paths(), create_upper_paths_hook, ereport, errcode(), errdetail(), errmsg(), ERROR, GroupPathExtraData::flags, gather_grouping_paths(), get_number_of_groups(), group_by_has_partkey(), Query::groupClause, GROUPING_CAN_PARTIAL_AGG, IS_PARTITIONED_REL, NIL, PlannerInfo::parse, RelOptInfo::partial_pathlist, PARTITIONWISE_AGGREGATE_FULL, PARTITIONWISE_AGGREGATE_NONE, PARTITIONWISE_AGGREGATE_PARTIAL, RelOptInfo::pathlist, GroupPathExtraData::patype, Path::rows, set_cheapest(), GroupPathExtraData::targetList, and UPPERREL_GROUP_AGG.

Referenced by create_grouping_paths(), and create_partitionwise_grouping_paths().

◆ create_partial_distinct_paths()

static void create_partial_distinct_paths ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo final_distinct_rel,
PathTarget target 
)
static

Definition at line 4643 of file planner.c.

4646 {
4647  RelOptInfo *partial_distinct_rel;
4648  Query *parse;
4649  List *distinctExprs;
4650  double numDistinctRows;
4651  Path *cheapest_partial_path;
4652  ListCell *lc;
4653 
4654  /* nothing to do when there are no partial paths in the input rel */
4655  if (!input_rel->consider_parallel || input_rel->partial_pathlist == NIL)
4656  return;
4657 
4658  parse = root->parse;
4659 
4660  /* can't do parallel DISTINCT ON */
4661  if (parse->hasDistinctOn)
4662  return;
4663 
4664  partial_distinct_rel = fetch_upper_rel(root, UPPERREL_PARTIAL_DISTINCT,
4665  NULL);
4666  partial_distinct_rel->reltarget = target;
4667  partial_distinct_rel->consider_parallel = input_rel->consider_parallel;
4668 
4669  /*
4670  * If input_rel belongs to a single FDW, so does the partial_distinct_rel.
4671  */
4672  partial_distinct_rel->serverid = input_rel->serverid;
4673  partial_distinct_rel->userid = input_rel->userid;
4674  partial_distinct_rel->useridiscurrent = input_rel->useridiscurrent;
4675  partial_distinct_rel->fdwroutine = input_rel->fdwroutine;
4676 
4677  cheapest_partial_path = linitial(input_rel->partial_pathlist);
4678 
4679  distinctExprs = get_sortgrouplist_exprs(root->processed_distinctClause,
4680  parse->targetList);
4681 
4682  /* estimate how many distinct rows we'll get from each worker */
4683  numDistinctRows = estimate_num_groups(root, distinctExprs,
4684  cheapest_partial_path->rows,
4685  NULL, NULL);
4686 
4687  /*
4688  * Try sorting the cheapest path and incrementally sorting any paths with
4689  * presorted keys and put a unique paths atop of those.
4690  */
4692  {
4693  foreach(lc, input_rel->partial_pathlist)
4694  {
4695  Path *input_path = (Path *) lfirst(lc);
4696  Path *sorted_path;
4697  bool is_sorted;
4698  int presorted_keys;
4699 
4701  input_path->pathkeys,
4702  &presorted_keys);
4703 
4704  if (is_sorted)
4705  sorted_path = input_path;
4706  else
4707  {
4708  /*
4709  * Try at least sorting the cheapest path and also try
4710  * incrementally sorting any path which is partially sorted
4711  * already (no need to deal with paths which have presorted
4712  * keys when incremental sort is disabled unless it's the
4713  * cheapest partial path).
4714  */
4715  if (input_path != cheapest_partial_path &&
4716  (presorted_keys == 0 || !enable_incremental_sort))
4717  continue;
4718 
4719  /*
4720  * We've no need to consider both a sort and incremental sort.
4721  * We'll just do a sort if there are no presorted keys and an
4722  * incremental sort when there are presorted keys.
4723  */
4724  if (presorted_keys == 0 || !enable_incremental_sort)
4725  sorted_path = (Path *) create_sort_path(root,
4726  partial_distinct_rel,
4727  input_path,
4728  root->distinct_pathkeys,
4729  -1.0);
4730  else
4731  sorted_path = (Path *) create_incremental_sort_path(root,
4732  partial_distinct_rel,
4733  input_path,
4734  root->distinct_pathkeys,
4735  presorted_keys,
4736  -1.0);
4737  }
4738 
4739  /*
4740  * An empty distinct_pathkeys means all tuples have the same value
4741  * for the DISTINCT clause. See create_final_distinct_paths()
4742  */
4743  if (root->distinct_pathkeys == NIL)
4744  {
4745  Node *limitCount;
4746 
4747  limitCount = (Node *) makeConst(INT8OID, -1, InvalidOid,
4748  sizeof(int64),
4749  Int64GetDatum(1), false,
4750  FLOAT8PASSBYVAL);
4751 
4752  /*
4753  * Apply a LimitPath onto the partial path to restrict the
4754  * tuples from each worker to 1. create_final_distinct_paths
4755  * will need to apply an additional LimitPath to restrict this
4756  * to a single row after the Gather node. If the query
4757  * already has a LIMIT clause, then we could end up with three
4758  * Limit nodes in the final plan. Consolidating the top two
4759  * of these could be done, but does not seem worth troubling
4760  * over.
4761  */
4762  add_partial_path(partial_distinct_rel, (Path *)
4763  create_limit_path(root, partial_distinct_rel,
4764  sorted_path,
4765  NULL,
4766  limitCount,
4768  0, 1));
4769  }
4770  else
4771  {
4772  add_partial_path(partial_distinct_rel, (Path *)
4773  create_upper_unique_path(root, partial_distinct_rel,
4774  sorted_path,
4776  numDistinctRows));
4777  }
4778  }
4779  }
4780 
4781  /*
4782  * Now try hash aggregate paths, if enabled and hashing is possible. Since
4783  * we're not on the hook to ensure we do our best to create at least one
4784  * path here, we treat enable_hashagg as a hard off-switch rather than the
4785  * slightly softer variant in create_final_distinct_paths.
4786  */
4788  {
4789  add_partial_path(partial_distinct_rel, (Path *)
4790  create_agg_path(root,
4791  partial_distinct_rel,
4792  cheapest_partial_path,
4793  cheapest_partial_path->pathtarget,
4794  AGG_HASHED,
4797  NIL,
4798  NULL,
4799  numDistinctRows));
4800  }
4801 
4802  /*
4803  * If there is an FDW that's responsible for all baserels of the query,
4804  * let it consider adding ForeignPaths.
4805  */
4806  if (partial_distinct_rel->fdwroutine &&
4807  partial_distinct_rel->fdwroutine->GetForeignUpperPaths)
4808  partial_distinct_rel->fdwroutine->GetForeignUpperPaths(root,
4810  input_rel,
4811  partial_distinct_rel,
4812  NULL);
4813 
4814  /* Let extensions possibly add some more partial paths */
4816  (*create_upper_paths_hook) (root, UPPERREL_PARTIAL_DISTINCT,
4817  input_rel, partial_distinct_rel, NULL);
4818 
4819  if (partial_distinct_rel->partial_pathlist != NIL)
4820  {
4821  generate_useful_gather_paths(root, partial_distinct_rel, true);
4822  set_cheapest(partial_distinct_rel);
4823 
4824  /*
4825  * Finally, create paths to distinctify the final result. This step
4826  * is needed to remove any duplicates due to combining rows from
4827  * parallel workers.
4828  */
4829  create_final_distinct_paths(root, partial_distinct_rel,
4830  final_distinct_rel);
4831  }
4832 }
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:745
@ UPPERREL_PARTIAL_DISTINCT
Definition: pathnodes.h:76

References add_partial_path(), AGG_HASHED, AGGSPLIT_SIMPLE, RelOptInfo::consider_parallel, create_agg_path(), create_final_distinct_paths(), create_incremental_sort_path(), create_limit_path(), create_sort_path(), create_upper_paths_hook, create_upper_unique_path(), PlannerInfo::distinct_pathkeys, enable_hashagg, enable_incremental_sort, estimate_num_groups(), fetch_upper_rel(), FLOAT8PASSBYVAL, generate_useful_gather_paths(), get_sortgrouplist_exprs(), grouping_is_hashable(), grouping_is_sortable(), Int64GetDatum(), InvalidOid, lfirst, LIMIT_OPTION_COUNT, linitial, list_length(), makeConst(), NIL, parse(), PlannerInfo::parse, RelOptInfo::partial_pathlist, Path::pathkeys, pathkeys_count_contained_in(), PlannerInfo::processed_distinctClause, RelOptInfo::reltarget, Path::rows, RelOptInfo::serverid, set_cheapest(), UPPERREL_PARTIAL_DISTINCT, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by create_distinct_paths().

◆ create_partial_grouping_paths()

static RelOptInfo * create_partial_grouping_paths ( PlannerInfo root,
RelOptInfo grouped_rel,
RelOptInfo input_rel,
grouping_sets_data gd,
GroupPathExtraData extra,
bool  force_rel_creation 
)
static

Definition at line 7020 of file planner.c.

7026 {
7027  Query *parse = root->parse;
7028  RelOptInfo *partially_grouped_rel;
7029  AggClauseCosts *agg_partial_costs = &extra->agg_partial_costs;
7030  AggClauseCosts *agg_final_costs = &extra->agg_final_costs;
7031  Path *cheapest_partial_path = NULL;
7032  Path *cheapest_total_path = NULL;
7033  double dNumPartialGroups = 0;
7034  double dNumPartialPartialGroups = 0;
7035  ListCell *lc;
7036  bool can_hash = (extra->flags & GROUPING_CAN_USE_HASH) != 0;
7037  bool can_sort = (extra->flags & GROUPING_CAN_USE_SORT) != 0;
7038 
7039  /*
7040  * Consider whether we should generate partially aggregated non-partial
7041  * paths. We can only do this if we have a non-partial path, and only if
7042  * the parent of the input rel is performing partial partitionwise
7043  * aggregation. (Note that extra->patype is the type of partitionwise
7044  * aggregation being used at the parent level, not this level.)
7045  */
7046  if (input_rel->pathlist != NIL &&
7048  cheapest_total_path = input_rel->cheapest_total_path;
7049 
7050  /*
7051  * If parallelism is possible for grouped_rel, then we should consider
7052  * generating partially-grouped partial paths. However, if the input rel
7053  * has no partial paths, then we can't.
7054  */
7055  if (grouped_rel->consider_parallel && input_rel->partial_pathlist != NIL)
7056  cheapest_partial_path = linitial(input_rel->partial_pathlist);
7057 
7058  /*
7059  * If we can't partially aggregate partial paths, and we can't partially
7060  * aggregate non-partial paths, then don't bother creating the new
7061  * RelOptInfo at all, unless the caller specified force_rel_creation.
7062  */
7063  if (cheapest_total_path == NULL &&
7064  cheapest_partial_path == NULL &&
7065  !force_rel_creation)
7066  return NULL;
7067 
7068  /*
7069  * Build a new upper relation to represent the result of partially
7070  * aggregating the rows from the input relation.
7071  */
7072  partially_grouped_rel = fetch_upper_rel(root,
7074  grouped_rel->relids);
7075  partially_grouped_rel->consider_parallel =
7076  grouped_rel->consider_parallel;
7077  partially_grouped_rel->reloptkind = grouped_rel->reloptkind;
7078  partially_grouped_rel->serverid = grouped_rel->serverid;
7079  partially_grouped_rel->userid = grouped_rel->userid;
7080  partially_grouped_rel->useridiscurrent = grouped_rel->useridiscurrent;
7081  partially_grouped_rel->fdwroutine = grouped_rel->fdwroutine;
7082 
7083  /*
7084  * Build target list for partial aggregate paths. These paths cannot just
7085  * emit the same tlist as regular aggregate paths, because (1) we must
7086  * include Vars and Aggrefs needed in HAVING, which might not appear in
7087  * the result tlist, and (2) the Aggrefs must be set in partial mode.
7088  */
7089  partially_grouped_rel->reltarget =
7090  make_partial_grouping_target(root, grouped_rel->reltarget,
7091  extra->havingQual);
7092 
7093  if (!extra->partial_costs_set)
7094  {
7095  /*
7096  * Collect statistics about aggregates for estimating costs of
7097  * performing aggregation in parallel.
7098  */
7099  MemSet(agg_partial_costs, 0, sizeof(AggClauseCosts));
7100  MemSet(agg_final_costs, 0, sizeof(AggClauseCosts));
7101  if (parse->hasAggs)
7102  {
7103  /* partial phase */
7105  agg_partial_costs);
7106 
7107  /* final phase */
7109  agg_final_costs);
7110  }
7111 
7112  extra->partial_costs_set = true;
7113  }
7114 
7115  /* Estimate number of partial groups. */
7116  if (cheapest_total_path != NULL)
7117  dNumPartialGroups =
7118  get_number_of_groups(root,
7119  cheapest_total_path->rows,
7120  gd,
7121  extra->targetList);
7122  if (cheapest_partial_path != NULL)
7123  dNumPartialPartialGroups =
7124  get_number_of_groups(root,
7125  cheapest_partial_path->rows,
7126  gd,
7127  extra->targetList);
7128 
7129  if (can_sort && cheapest_total_path != NULL)
7130  {
7131  /* This should have been checked previously */
7132  Assert(parse->hasAggs || parse->groupClause);
7133 
7134  /*
7135  * Use any available suitably-sorted path as input, and also consider
7136  * sorting the cheapest partial path.
7137  */
7138  foreach(lc, input_rel->pathlist)
7139  {
7140  ListCell *lc2;
7141  Path *path = (Path *) lfirst(lc);
7142  Path *path_save = path;
7143  List *pathkey_orderings = NIL;
7144 
7145  /* generate alternative group orderings that might be useful */
7146  pathkey_orderings = get_useful_group_keys_orderings(root, path);
7147 
7148  Assert(list_length(pathkey_orderings) > 0);
7149 
7150  /* process all potentially interesting grouping reorderings */
7151  foreach(lc2, pathkey_orderings)
7152  {
7153  PathKeyInfo *info = (PathKeyInfo *) lfirst(lc2);
7154 
7155  /* restore the path (we replace it in the loop) */
7156  path = path_save;
7157 
7158  path = make_ordered_path(root,
7159  partially_grouped_rel,
7160  path,
7161  cheapest_total_path,
7162  info->pathkeys);
7163 
7164  if (path == NULL)
7165  continue;
7166 
7167  if (parse->hasAggs)
7168  add_path(partially_grouped_rel, (Path *)
7169  create_agg_path(root,
7170  partially_grouped_rel,
7171  path,
7172  partially_grouped_rel->reltarget,
7173  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7175  info->clauses,
7176  NIL,
7177  agg_partial_costs,
7178  dNumPartialGroups));
7179  else
7180  add_path(partially_grouped_rel, (Path *)
7181  create_group_path(root,
7182  partially_grouped_rel,
7183  path,
7184  info->clauses,
7185  NIL,
7186  dNumPartialGroups));
7187  }
7188  }
7189  }
7190 
7191  if (can_sort && cheapest_partial_path != NULL)
7192  {
7193  /* Similar to above logic, but for partial paths. */
7194  foreach(lc, input_rel->partial_pathlist)
7195  {
7196  ListCell *lc2;
7197  Path *path = (Path *) lfirst(lc);
7198  Path *path_save = path;
7199  List *pathkey_orderings = NIL;
7200 
7201  /* generate alternative group orderings that might be useful */
7202  pathkey_orderings = get_useful_group_keys_orderings(root, path);
7203 
7204  Assert(list_length(pathkey_orderings) > 0);
7205 
7206  /* process all potentially interesting grouping reorderings */
7207  foreach(lc2, pathkey_orderings)
7208  {
7209  PathKeyInfo *info = (PathKeyInfo *) lfirst(lc2);
7210 
7211 
7212  /* restore the path (we replace it in the loop) */
7213  path = path_save;
7214 
7215  path = make_ordered_path(root,
7216  partially_grouped_rel,
7217  path,
7218  cheapest_partial_path,
7219  info->pathkeys);
7220 
7221  if (path == NULL)
7222  continue;
7223 
7224  if (parse->hasAggs)
7225  add_partial_path(partially_grouped_rel, (Path *)
7226  create_agg_path(root,
7227  partially_grouped_rel,
7228  path,
7229  partially_grouped_rel->reltarget,
7230  parse->groupClause ? AGG_SORTED : AGG_PLAIN,
7232  info->clauses,
7233  NIL,
7234  agg_partial_costs,
7235  dNumPartialPartialGroups));
7236  else
7237  add_partial_path(partially_grouped_rel, (Path *)
7238  create_group_path(root,
7239  partially_grouped_rel,
7240  path,
7241  info->clauses,
7242  NIL,
7243  dNumPartialPartialGroups));
7244  }
7245  }
7246  }
7247 
7248  /*
7249  * Add a partially-grouped HashAgg Path where possible
7250  */
7251  if (can_hash && cheapest_total_path != NULL)
7252  {
7253  /* Checked above */
7254  Assert(parse->hasAggs || parse->groupClause);
7255 
7256  add_path(partially_grouped_rel, (Path *)
7257  create_agg_path(root,
7258  partially_grouped_rel,
7259  cheapest_total_path,
7260  partially_grouped_rel->reltarget,
7261  AGG_HASHED,
7263  root->processed_groupClause,
7264  NIL,
7265  agg_partial_costs,
7266  dNumPartialGroups));
7267  }
7268 
7269  /*
7270  * Now add a partially-grouped HashAgg partial Path where possible
7271  */
7272  if (can_hash && cheapest_partial_path != NULL)
7273  {
7274  add_partial_path(partially_grouped_rel, (Path *)
7275  create_agg_path(root,
7276  partially_grouped_rel,
7277  cheapest_partial_path,
7278  partially_grouped_rel->reltarget,
7279  AGG_HASHED,
7281  root->processed_groupClause,
7282  NIL,
7283  agg_partial_costs,
7284  dNumPartialPartialGroups));
7285  }
7286 
7287  /*
7288  * If there is an FDW that's responsible for all baserels of the query,
7289  * let it consider adding partially grouped ForeignPaths.
7290  */
7291  if (partially_grouped_rel->fdwroutine &&
7292  partially_grouped_rel->fdwroutine->GetForeignUpperPaths)
7293  {
7294  FdwRoutine *fdwroutine = partially_grouped_rel->fdwroutine;
7295 
7296  fdwroutine->GetForeignUpperPaths(root,
7298  input_rel, partially_grouped_rel,
7299  extra);
7300  }
7301 
7302  return partially_grouped_rel;
7303 }
@ AGGSPLIT_INITIAL_SERIAL
Definition: nodes.h:368
@ UPPERREL_PARTIAL_GROUP_AGG
Definition: pathnodes.h:72
static PathTarget * make_partial_grouping_target(PlannerInfo *root, PathTarget *grouping_target, Node *havingQual)
Definition: planner.c:5352
GetForeignUpperPaths_function GetForeignUpperPaths
Definition: fdwapi.h:226
AggClauseCosts agg_partial_costs
Definition: pathnodes.h:3267
RelOptKind reloptkind
Definition: pathnodes.h:850

References add_partial_path(), add_path(), GroupPathExtraData::agg_final_costs, AGG_HASHED, GroupPathExtraData::agg_partial_costs, AGG_PLAIN, AGG_SORTED, AGGSPLIT_FINAL_DESERIAL, AGGSPLIT_INITIAL_SERIAL, Assert(), RelOptInfo::cheapest_total_path, PathKeyInfo::clauses, RelOptInfo::consider_parallel, create_agg_path(), create_group_path(), fetch_upper_rel(), GroupPathExtraData::flags, get_agg_clause_costs(), get_number_of_groups(), get_useful_group_keys_orderings(), FdwRoutine::GetForeignUpperPaths, GROUPING_CAN_USE_HASH, GROUPING_CAN_USE_SORT, GroupPathExtraData::havingQual, lfirst, linitial, list_length(), make_ordered_path(), make_partial_grouping_target(), MemSet, NIL, parse(), PlannerInfo::parse, GroupPathExtraData::partial_costs_set, RelOptInfo::partial_pathlist, PARTITIONWISE_AGGREGATE_PARTIAL, PathKeyInfo::pathkeys, RelOptInfo::pathlist, GroupPathExtraData::patype, PlannerInfo::processed_groupClause, RelOptInfo::relids, RelOptInfo::reloptkind, RelOptInfo::reltarget, Path::rows, RelOptInfo::serverid, GroupPathExtraData::targetList, UPPERREL_PARTIAL_GROUP_AGG, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by create_ordinary_grouping_paths().

◆ create_partitionwise_grouping_paths()

static void create_partitionwise_grouping_paths ( PlannerInfo root,
RelOptInfo input_rel,
RelOptInfo grouped_rel,
RelOptInfo partially_grouped_rel,
const AggClauseCosts agg_costs,
grouping_sets_data gd,
PartitionwiseAggregateType  patype,
GroupPathExtraData extra 
)
static

Definition at line 7681 of file planner.c.

7689 {
7690  List *grouped_live_children = NIL;
7691  List *partially_grouped_live_children = NIL;
7692  PathTarget *target = grouped_rel->reltarget;
7693  bool partial_grouping_valid = true;
7694  int i;
7695 
7698  partially_grouped_rel != NULL);
7699 
7700  /* Add paths for partitionwise aggregation/grouping. */
7701  i = -1;
7702  while ((i = bms_next_member(input_rel->live_parts, i)) >= 0)
7703  {
7704  RelOptInfo *child_input_rel = input_rel->part_rels[i];
7705  PathTarget *child_target;
7706  AppendRelInfo **appinfos;
7707  int nappinfos;
7708  GroupPathExtraData child_extra;
7709  RelOptInfo *child_grouped_rel;
7710  RelOptInfo *child_partially_grouped_rel;
7711 
7712  Assert(child_input_rel != NULL);
7713 
7714  /* Dummy children can be ignored. */
7715  if (IS_DUMMY_REL(child_input_rel))
7716  continue;
7717 
7718  child_target = copy_pathtarget(target);
7719 
7720  /*
7721  * Copy the given "extra" structure as is and then override the
7722  * members specific to this child.
7723  */
7724  memcpy(&child_extra, extra, sizeof(child_extra));
7725 
7726  appinfos = find_appinfos_by_relids(root, child_input_rel->relids,
7727  &nappinfos);
7728 
7729  child_target->exprs = (List *)
7731  (Node *) target->exprs,
7732  nappinfos, appinfos);
7733 
7734  /* Translate havingQual and targetList. */
7735  child_extra.havingQual = (Node *)
7737  extra->havingQual,
7738  nappinfos, appinfos);
7739  child_extra.targetList = (List *)
7741  (Node *) extra->targetList,
7742  nappinfos, appinfos);
7743 
7744  /*
7745  * extra->patype was the value computed for our parent rel; patype is
7746  * the value for this relation. For the child, our value is its
7747  * parent rel's value.
7748  */
7749  child_extra.patype = patype;
7750 
7751  /*
7752  * Create grouping relation to hold fully aggregated grouping and/or
7753  * aggregation paths for the child.
7754  */
7755  child_grouped_rel = make_grouping_rel(root, child_input_rel,
7756  child_target,
7757  extra->target_parallel_safe,
7758  child_extra.havingQual);
7759 
7760  /* Create grouping paths for this child relation. */
7761  create_ordinary_grouping_paths(root, child_input_rel,
7762  child_grouped_rel,
7763  agg_costs, gd, &child_extra,
7764  &child_partially_grouped_rel);
7765 
7766  if (child_partially_grouped_rel)
7767  {
7768  partially_grouped_live_children =
7769  lappend(partially_grouped_live_children,
7770  child_partially_grouped_rel);
7771  }
7772  else
7773  partial_grouping_valid = false;
7774 
7775  if (patype == PARTITIONWISE_AGGREGATE_FULL)
7776  {
7777  set_cheapest(child_grouped_rel);
7778  grouped_live_children = lappend(grouped_live_children,
7779  child_grouped_rel);
7780  }
7781 
7782  pfree(appinfos);
7783  }
7784 
7785  /*
7786  * Try to create append paths for partially grouped children. For full
7787  * partitionwise aggregation, we might have paths in the partial_pathlist
7788  * if parallel aggregation is possible. For partial partitionwise
7789  * aggregation, we may have paths in both pathlist and partial_pathlist.
7790  *
7791  * NB: We must have a partially grouped path for every child in order to
7792  * generate a partially grouped path for this relation.
7793  */
7794  if (partially_grouped_rel && partial_grouping_valid)
7795  {
7796  Assert(partially_grouped_live_children != NIL);
7797 
7798  add_paths_to_append_rel(root, partially_grouped_rel,
7799  partially_grouped_live_children);
7800 
7801  /*
7802  * We need call set_cheapest, since the finalization step will use the
7803  * cheapest path from the rel.
7804  */
7805  if (partially_grouped_rel->pathlist)
7806  set_cheapest(partially_grouped_rel);
7807  }
7808 
7809  /* If possible, create append paths for fully grouped children. */
7810  if (patype == PARTITIONWISE_AGGREGATE_FULL)
7811  {
7812  Assert(grouped_live_children != NIL);
7813 
7814  add_paths_to_append_rel(root, grouped_rel, grouped_live_children);
7815  }
7816 }

References add_paths_to_append_rel(), adjust_appendrel_attrs(), Assert(), bms_next_member(), copy_pathtarget(), create_ordinary_grouping_paths(), PathTarget::exprs, find_appinfos_by_relids(), GroupPathExtraData::havingQual, i, IS_DUMMY_REL, lappend(), RelOptInfo::live_parts, make_grouping_rel(), NIL, PARTITIONWISE_AGGREGATE_FULL, PARTITIONWISE_AGGREGATE_NONE, PARTITIONWISE_AGGREGATE_PARTIAL, RelOptInfo::pathlist, GroupPathExtraData::patype, pfree(), RelOptInfo::relids, RelOptInfo::reltarget, set_cheapest(), GroupPathExtraData::target_parallel_safe, and GroupPathExtraData::targetList.

Referenced by create_ordinary_grouping_paths().

◆ create_window_paths()

static RelOptInfo * create_window_paths ( PlannerInfo root,
RelOptInfo input_rel,
PathTarget input_target,
PathTarget output_target,
bool  output_target_parallel_safe,
WindowFuncLists wflists,
List activeWindows 
)
static

Definition at line 4353 of file planner.c.

4360 {
4361  RelOptInfo *window_rel;
4362  ListCell *lc;
4363 
4364  /* For now, do all work in the (WINDOW, NULL) upperrel */
4365  window_rel = fetch_upper_rel(root, UPPERREL_WINDOW, NULL);
4366 
4367  /*
4368  * If the input relation is not parallel-safe, then the window relation
4369  * can't be parallel-safe, either. Otherwise, we need to examine the
4370  * target list and active windows for non-parallel-safe constructs.
4371  */
4372  if (input_rel->consider_parallel && output_target_parallel_safe &&
4373  is_parallel_safe(root, (Node *) activeWindows))
4374  window_rel->consider_parallel = true;
4375 
4376  /*
4377  * If the input rel belongs to a single FDW, so does the window rel.
4378  */
4379  window_rel->serverid = input_rel->serverid;
4380  window_rel->userid = input_rel->userid;
4381  window_rel->useridiscurrent = input_rel->useridiscurrent;
4382  window_rel->fdwroutine = input_rel->fdwroutine;
4383 
4384  /*
4385  * Consider computing window functions starting from the existing
4386  * cheapest-total path (which will likely require a sort) as well as any
4387  * existing paths that satisfy or partially satisfy root->window_pathkeys.
4388  */
4389  foreach(lc, input_rel->pathlist)
4390  {
4391  Path *path = (Path *) lfirst(lc);
4392  int presorted_keys;
4393 
4394  if (path == input_rel->cheapest_total_path ||
4396  &presorted_keys) ||
4397  presorted_keys > 0)
4399  window_rel,
4400  path,
4401  input_target,
4402  output_target,
4403  wflists,
4404  activeWindows);
4405  }
4406 
4407  /*
4408  * If there is an FDW that's responsible for all baserels of the query,
4409  * let it consider adding ForeignPaths.
4410  */
4411  if (window_rel->fdwroutine &&
4412  window_rel->fdwroutine->GetForeignUpperPaths)
4413  window_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_WINDOW,
4414  input_rel, window_rel,
4415  NULL);
4416 
4417  /* Let extensions possibly add some more paths */
4419  (*create_upper_paths_hook) (root, UPPERREL_WINDOW,
4420  input_rel, window_rel, NULL);
4421 
4422  /* Now choose the best path(s) */
4423  set_cheapest(window_rel);
4424 
4425  return window_rel;
4426 }
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:733
@ UPPERREL_WINDOW
Definition: pathnodes.h:75
static void create_one_window_path(PlannerInfo *root, RelOptInfo *window_rel, Path *path, PathTarget *input_target, PathTarget *output_target, WindowFuncLists *wflists, List *activeWindows)
Definition: planner.c:4440
List * window_pathkeys
Definition: pathnodes.h:395

References RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_one_window_path(), create_upper_paths_hook, fetch_upper_rel(), is_parallel_safe(), lfirst, Path::pathkeys, pathkeys_count_contained_in(), RelOptInfo::pathlist, RelOptInfo::serverid, set_cheapest(), UPPERREL_WINDOW, RelOptInfo::userid, RelOptInfo::useridiscurrent, and PlannerInfo::window_pathkeys.

Referenced by grouping_planner().

◆ expression_planner()

Expr* expression_planner ( Expr expr)

Definition at line 6400 of file planner.c.

6401 {
6402  Node *result;
6403 
6404  /*
6405  * Convert named-argument function calls, insert default arguments and
6406  * simplify constant subexprs
6407  */
6408  result = eval_const_expressions(NULL, (Node *) expr);
6409 
6410  /* Fill in opfuncid values if missing */
6411  fix_opfuncids(result);
6412 
6413  return (Expr *) result;
6414 }
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2234
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1759

References eval_const_expressions(), and fix_opfuncids().

Referenced by ATExecAddColumn(), ATExecSetExpression(), ATPrepAlterColumnType(), BeginCopyFrom(), ComputePartitionAttrs(), contain_mutable_functions_after_planning(), contain_volatile_functions_after_planning(), ExecPrepareCheck(), ExecPrepareExpr(), ExecPrepareQual(), load_domaintype_info(), set_baserel_partition_constraint(), slot_fill_defaults(), StoreAttrDefault(), and transformPartitionBoundValue().

◆ expression_planner_with_deps()

Expr* expression_planner_with_deps ( Expr expr,
List **  relationOids,
List **  invalItems 
)

Definition at line 6427 of file planner.c.

6430 {
6431  Node *result;
6432  PlannerGlobal glob;
6433  PlannerInfo root;
6434 
6435  /* Make up dummy planner state so we can use setrefs machinery */
6436  MemSet(&glob, 0, sizeof(glob));
6437  glob.type = T_PlannerGlobal;
6438  glob.relationOids = NIL;
6439  glob.invalItems = NIL;
6440 
6441  MemSet(&root, 0, sizeof(root));
6442  root.type = T_PlannerInfo;
6443  root.glob = &glob;
6444 
6445  /*
6446  * Convert named-argument function calls, insert default arguments and
6447  * simplify constant subexprs. Collect identities of inlined functions
6448  * and elided domains, too.
6449  */
6450  result = eval_const_expressions(&root, (Node *) expr);
6451 
6452  /* Fill in opfuncid values if missing */
6453  fix_opfuncids(result);
6454 
6455  /*
6456  * Now walk the finished expression to find anything else we ought to
6457  * record as an expression dependency.
6458  */
6459  (void) extract_query_dependencies_walker(result, &root);
6460 
6461  *relationOids = glob.relationOids;
6462  *invalItems = glob.invalItems;
6463 
6464  return (Expr *) result;
6465 }
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3561
List * invalItems
Definition: pathnodes.h:132
List * relationOids
Definition: pathnodes.h:129
PlannerGlobal * glob
Definition: pathnodes.h:202

References eval_const_expressions(), extract_query_dependencies_walker(), fix_opfuncids(), PlannerInfo::glob, PlannerGlobal::invalItems, MemSet, NIL, and PlannerGlobal::relationOids.

Referenced by GetCachedExpression().

◆ extract_rollup_sets()

static List * extract_rollup_sets ( List groupingSets)
static

Definition at line 2836 of file planner.c.

2837 {
2838  int num_sets_raw = list_length(groupingSets);
2839  int num_empty = 0;
2840  int num_sets = 0; /* distinct sets */
2841  int num_chains = 0;
2842  List *result = NIL;
2843  List **results;
2844  List **orig_sets;
2845  Bitmapset **set_masks;
2846  int *chains;
2847  short **adjacency;
2848  short *adjacency_buf;
2850  int i;
2851  int j;
2852  int j_size;
2853  ListCell *lc1 = list_head(groupingSets);
2854  ListCell *lc;
2855 
2856  /*
2857  * Start by stripping out empty sets. The algorithm doesn't require this,
2858  * but the planner currently needs all empty sets to be returned in the
2859  * first list, so we strip them here and add them back after.
2860  */
2861  while (lc1 && lfirst(lc1) == NIL)
2862  {
2863  ++num_empty;
2864  lc1 = lnext(groupingSets, lc1);
2865  }
2866 
2867  /* bail out now if it turns out that all we had were empty sets. */
2868  if (!lc1)
2869  return list_make1(groupingSets);
2870 
2871  /*----------
2872  * We don't strictly need to remove duplicate sets here, but if we don't,
2873  * they tend to become scattered through the result, which is a bit
2874  * confusing (and irritating if we ever decide to optimize them out).
2875  * So we remove them here and add them back after.
2876  *
2877  * For each non-duplicate set, we fill in the following:
2878  *
2879  * orig_sets[i] = list of the original set lists
2880  * set_masks[i] = bitmapset for testing inclusion
2881  * adjacency[i] = array [n, v1, v2, ... vn] of adjacency indices
2882  *
2883  * chains[i] will be the result group this set is assigned to.
2884  *
2885  * We index all of these from 1 rather than 0 because it is convenient
2886  * to leave 0 free for the NIL node in the graph algorithm.
2887  *----------
2888  */
2889  orig_sets = palloc0((num_sets_raw + 1) * sizeof(List *));
2890  set_masks = palloc0((num_sets_raw + 1) * sizeof(Bitmapset *));
2891  adjacency = palloc0((num_sets_raw + 1) * sizeof(short *));
2892  adjacency_buf = palloc((num_sets_raw + 1) * sizeof(short));
2893 
2894  j_size = 0;
2895  j = 0;
2896  i = 1;
2897 
2898  for_each_cell(lc, groupingSets, lc1)
2899  {
2900  List *candidate = (List *) lfirst(lc);
2901  Bitmapset *candidate_set = NULL;
2902  ListCell *lc2;
2903  int dup_of = 0;
2904 
2905  foreach(lc2, candidate)
2906  {
2907  candidate_set = bms_add_member(candidate_set, lfirst_int(lc2));
2908  }
2909 
2910  /* we can only be a dup if we're the same length as a previous set */
2911  if (j_size == list_length(candidate))
2912  {
2913  int k;
2914 
2915  for (k = j; k < i; ++k)
2916  {
2917  if (bms_equal(set_masks[k], candidate_set))
2918  {
2919  dup_of = k;
2920  break;
2921  }
2922  }
2923  }
2924  else if (j_size < list_length(candidate))
2925  {
2926  j_size = list_length(candidate);
2927  j = i;
2928  }
2929 
2930  if (dup_of > 0)
2931  {
2932  orig_sets[dup_of] = lappend(orig_sets[dup_of], candidate);
2933  bms_free(candidate_set);
2934  }
2935  else
2936  {
2937  int k;
2938  int n_adj = 0;
2939 
2940  orig_sets[i] = list_make1(candidate);
2941  set_masks[i] = candidate_set;
2942 
2943  /* fill in adjacency list; no need to compare equal-size sets */
2944 
2945  for (k = j - 1; k > 0; --k)
2946  {
2947  if (bms_is_subset(set_masks[k], candidate_set))
2948  adjacency_buf[++n_adj] = k;
2949  }
2950 
2951  if (n_adj > 0)
2952  {
2953  adjacency_buf[0] = n_adj;
2954  adjacency[i] = palloc((n_adj + 1) * sizeof(short));
2955  memcpy(adjacency[i], adjacency_buf, (n_adj + 1) * sizeof(short));
2956  }
2957  else
2958  adjacency[i] = NULL;
2959 
2960  ++i;
2961  }
2962  }
2963 
2964  num_sets = i - 1;
2965 
2966  /*
2967  * Apply the graph matching algorithm to do the work.
2968  */
2969  state = BipartiteMatch(num_sets, num_sets, adjacency);
2970 
2971  /*
2972  * Now, the state->pair* fields have the info we need to assign sets to
2973  * chains. Two sets (u,v) belong to the same chain if pair_uv[u] = v or
2974  * pair_vu[v] = u (both will be true, but we check both so that we can do
2975  * it in one pass)
2976  */
2977  chains = palloc0((num_sets + 1) * sizeof(int));
2978 
2979  for (i = 1; i <= num_sets; ++i)
2980  {
2981  int u = state->pair_vu[i];
2982  int v = state->pair_uv[i];
2983 
2984  if (u > 0 && u < i)
2985  chains[i] = chains[u];
2986  else if (v > 0 && v < i)
2987  chains[i] = chains[v];
2988  else
2989  chains[i] = ++num_chains;
2990  }
2991 
2992  /* build result lists. */
2993  results = palloc0((num_chains + 1) * sizeof(List *));
2994 
2995  for (i = 1; i <= num_sets; ++i)
2996  {
2997  int c = chains[i];
2998 
2999  Assert(c > 0);
3000 
3001  results[c] = list_concat(results[c], orig_sets[i]);
3002  }
3003 
3004  /* push any empty sets back on the first list. */
3005  while (num_empty-- > 0)
3006  results[1] = lcons(NIL, results[1]);
3007 
3008  /* make result list */
3009  for (i = 1; i <= num_chains; ++i)
3010  result = lappend(result, results[i]);
3011 
3012  /*
3013  * Free all the things.
3014  *
3015  * (This is over-fussy for small sets but for large sets we could have
3016  * tied up a nontrivial amount of memory.)
3017  */
3019  pfree(results);
3020  pfree(chains);
3021  for (i = 1; i <= num_sets; ++i)
3022  if (adjacency[i])
3023  pfree(adjacency[i]);
3024  pfree(adjacency);
3025  pfree(adjacency_buf);
3026  pfree(orig_sets);
3027  for (i = 1; i <= num_sets; ++i)
3028  bms_free(set_masks[i]);
3029  pfree(set_masks);
3030 
3031  return result;
3032 }
void BipartiteMatchFree(BipartiteMatchState *state)
BipartiteMatchState * BipartiteMatch(int u_size, int v_size, short **adjacency)
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int j
Definition: isn.c:74
void * palloc0(Size size)
Definition: mcxt.c:1334
char * c
Definition: regguts.h:323

References Assert(), BipartiteMatch(), BipartiteMatchFree(), bms_add_member(), bms_equal(), bms_free(), bms_is_subset(), for_each_cell, i, j, lappend(), lcons(), lfirst, lfirst_int, list_concat(), list_head(), list_length(), list_make1, lnext(), NIL, palloc(), palloc0(), and pfree().

Referenced by preprocess_grouping_sets().

◆ gather_grouping_paths()

static void gather_grouping_paths ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 7319 of file planner.c.

7320 {
7321  ListCell *lc;
7322  Path *cheapest_partial_path;
7323  List *groupby_pathkeys;
7324 
7325  /*
7326  * This occurs after any partial aggregation has taken place, so trim off
7327  * any pathkeys added for ORDER BY / DISTINCT aggregates.
7328  */
7329  if (list_length(root->group_pathkeys) > root->num_groupby_pathkeys)
7330  groupby_pathkeys = list_copy_head(root->group_pathkeys,
7331  root->num_groupby_pathkeys);
7332  else
7333  groupby_pathkeys = root->group_pathkeys;
7334 
7335  /* Try Gather for unordered paths and Gather Merge for ordered ones. */
7336  generate_useful_gather_paths(root, rel, true);
7337 
7338  cheapest_partial_path = linitial(rel->partial_pathlist);
7339 
7340  /* XXX Shouldn't this also consider the group-key-reordering? */
7341  foreach(lc, rel->partial_pathlist)
7342  {
7343  Path *path = (Path *) lfirst(lc);
7344  bool is_sorted;
7345  int presorted_keys;
7346  double total_groups;
7347 
7348  is_sorted = pathkeys_count_contained_in(groupby_pathkeys,
7349  path->pathkeys,
7350  &presorted_keys);
7351 
7352  if (is_sorted)
7353  continue;
7354 
7355  /*
7356  * Try at least sorting the cheapest path and also try incrementally
7357  * sorting any path which is partially sorted already (no need to deal
7358  * with paths which have presorted keys when incremental sort is
7359  * disabled unless it's the cheapest input path).
7360  */
7361  if (path != cheapest_partial_path &&
7362  (presorted_keys == 0 || !enable_incremental_sort))
7363  continue;
7364 
7365  total_groups = path->rows * path->parallel_workers;
7366 
7367  /*
7368  * We've no need to consider both a sort and incremental sort. We'll
7369  * just do a sort if there are no presorted keys and an incremental
7370  * sort when there are presorted keys.
7371  */
7372  if (presorted_keys == 0 || !enable_incremental_sort)
7373  path = (Path *) create_sort_path(root, rel, path,
7374  groupby_pathkeys,
7375  -1.0);
7376  else
7377  path = (Path *) create_incremental_sort_path(root,
7378  rel,
7379  path,
7380  groupby_pathkeys,
7381  presorted_keys,
7382  -1.0);
7383 
7384  path = (Path *)
7386  rel,
7387  path,
7388  rel->reltarget,
7389  groupby_pathkeys,
7390  NULL,
7391  &total_groups);
7392 
7393  add_path(rel, path);
7394  }
7395 }
List * list_copy_head(const List *oldlist, int len)
Definition: list.c:1593
int num_groupby_pathkeys
Definition: pathnodes.h:392

References add_path(), create_gather_merge_path(), create_incremental_sort_path(), create_sort_path(), enable_incremental_sort, generate_useful_gather_paths(), PlannerInfo::group_pathkeys, lfirst, linitial, list_copy_head(), list_length(), PlannerInfo::num_groupby_pathkeys, Path::parallel_workers, RelOptInfo::partial_pathlist, Path::pathkeys, pathkeys_count_contained_in(), RelOptInfo::reltarget, and Path::rows.

Referenced by add_paths_to_grouping_rel(), and create_ordinary_grouping_paths().

◆ get_cheapest_fractional_path()

Path* get_cheapest_fractional_path ( RelOptInfo rel,
double  tuple_fraction 
)

Definition at line 6241 of file planner.c.

6242 {
6243  Path *best_path = rel->cheapest_total_path;
6244  ListCell *l;
6245 
6246  /* If all tuples will be retrieved, just return the cheapest-total path */
6247  if (tuple_fraction <= 0.0)
6248  return best_path;
6249 
6250  /* Convert absolute # of tuples to a fraction; no need to clamp to 0..1 */
6251  if (tuple_fraction >= 1.0 && best_path->rows > 0)
6252  tuple_fraction /= best_path->rows;
6253 
6254  foreach(l, rel->pathlist)
6255  {
6256  Path *path = (Path *) lfirst(l);
6257 
6258  if (path == rel->cheapest_total_path ||
6259  compare_fractional_path_costs(best_path, path, tuple_fraction) <= 0)
6260  continue;
6261 
6262  best_path = path;
6263  }
6264 
6265  return best_path;
6266 }
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:113

References RelOptInfo::cheapest_total_path, compare_fractional_path_costs(), lfirst, RelOptInfo::pathlist, and Path::rows.

Referenced by make_subplan(), recurse_set_operations(), and standard_planner().

◆ get_number_of_groups()

static double get_number_of_groups ( PlannerInfo root,
double  path_rows,
grouping_sets_data gd,
List target_list 
)
static

Definition at line 3479 of file planner.c.

3483 {
3484  Query *parse = root->parse;
3485  double dNumGroups;
3486 
3487  if (parse->groupClause)
3488  {
3489  List *groupExprs;
3490 
3491  if (parse->groupingSets)
3492  {
3493  /* Add up the estimates for each grouping set */
3494  ListCell *lc;
3495 
3496  Assert(gd); /* keep Coverity happy */
3497 
3498  dNumGroups = 0;
3499 
3500  foreach(lc, gd->rollups)
3501  {
3502  RollupData *rollup = lfirst_node(RollupData, lc);
3503  ListCell *lc2;
3504  ListCell *lc3;
3505 
3506  groupExprs = get_sortgrouplist_exprs(rollup->groupClause,
3507  target_list);
3508 
3509  rollup->numGroups = 0.0;
3510 
3511  forboth(lc2, rollup->gsets, lc3, rollup->gsets_data)
3512  {
3513  List *gset = (List *) lfirst(lc2);
3515  double numGroups = estimate_num_groups(root,
3516  groupExprs,
3517  path_rows,
3518  &gset,
3519  NULL);
3520 
3521  gs->numGroups = numGroups;
3522  rollup->numGroups += numGroups;
3523  }
3524 
3525  dNumGroups += rollup->numGroups;
3526  }
3527 
3528  if (gd->hash_sets_idx)
3529  {
3530  ListCell *lc2;
3531 
3532  gd->dNumHashGroups = 0;
3533 
3534  groupExprs = get_sortgrouplist_exprs(parse->groupClause,
3535  target_list);
3536 
3537  forboth(lc, gd->hash_sets_idx, lc2, gd->unsortable_sets)
3538  {
3539  List *gset = (List *) lfirst(lc);
3541  double numGroups = estimate_num_groups(root,
3542  groupExprs,
3543  path_rows,
3544  &gset,
3545  NULL);
3546 
3547  gs->numGroups = numGroups;
3548  gd->dNumHashGroups += numGroups;
3549  }
3550 
3551  dNumGroups += gd->dNumHashGroups;
3552  }
3553  }
3554  else
3555  {
3556  /* Plain GROUP BY -- estimate based on optimized groupClause */
3558  target_list);
3559 
3560  dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
3561  NULL, NULL);
3562  }
3563  }
3564  else if (parse->groupingSets)
3565  {
3566  /* Empty grouping sets ... one result row for each one */
3567  dNumGroups = list_length(parse->groupingSets);
3568  }
3569  else if (parse->hasAggs || root->hasHavingQual)
3570  {
3571  /* Plain aggregation, one result row */
3572  dNumGroups = 1;
3573  }
3574  else
3575  {
3576  /* Not grouping */
3577  dNumGroups = 1;
3578  }
3579 
3580  return dNumGroups;
3581 }
List * hash_sets_idx
Definition: planner.c:97

References Assert(), grouping_sets_data::dNumHashGroups, estimate_num_groups(), forboth, get_sortgrouplist_exprs(), RollupData::groupClause, RollupData::gsets, RollupData::gsets_data, grouping_sets_data::hash_sets_idx, PlannerInfo::hasHavingQual, lfirst, lfirst_node, list_length(), GroupingSetData::numGroups, RollupData::numGroups, parse(), PlannerInfo::parse, PlannerInfo::processed_groupClause, grouping_sets_data::rollups, and grouping_sets_data::unsortable_sets.

Referenced by create_ordinary_grouping_paths(), and create_partial_grouping_paths().

◆ group_by_has_partkey()

static bool group_by_has_partkey ( RelOptInfo input_rel,
List targetList,
List groupClause 
)
static

Definition at line 7825 of file planner.c.

7828 {
7829  List *groupexprs = get_sortgrouplist_exprs(groupClause, targetList);
7830  int cnt = 0;
7831  int partnatts;
7832 
7833  /* Input relation should be partitioned. */
7834  Assert(input_rel->part_scheme);
7835 
7836  /* Rule out early, if there are no partition keys present. */
7837  if (!input_rel->partexprs)
7838  return false;
7839 
7840  partnatts = input_rel->part_scheme->partnatts;
7841 
7842  for (cnt = 0; cnt < partnatts; cnt++)
7843  {
7844  List *partexprs = input_rel->partexprs[cnt];
7845  ListCell *lc;
7846  bool found = false;
7847 
7848  foreach(lc, partexprs)
7849  {
7850  Expr *partexpr = lfirst(lc);
7851 
7852  if (list_member(groupexprs, partexpr))
7853  {
7854  found = true;
7855  break;
7856  }
7857  }
7858 
7859  /*
7860  * If none of the partition key expressions match with any of the
7861  * GROUP BY expression, return false.
7862  */
7863  if (!found)
7864  return false;
7865  }
7866 
7867  return true;
7868 }
bool list_member(const List *list, const void *datum)
Definition: list.c:661

References Assert(), get_sortgrouplist_exprs(), lfirst, and list_member().

Referenced by create_ordinary_grouping_paths().

◆ groupclause_apply_groupingset()

static List * groupclause_apply_groupingset ( PlannerInfo root,
List force 
)
static

Definition at line 2800 of file planner.c.

2801 {
2802  Query *parse = root->parse;
2803  List *new_groupclause = NIL;
2804  ListCell *sl;
2805 
2806  foreach(sl, gset)
2807  {
2808  Index ref = lfirst_int(sl);
2809  SortGroupClause *cl = get_sortgroupref_clause(ref, parse->groupClause);
2810 
2811  new_groupclause = lappend(new_groupclause, cl);
2812  }
2813  return new_groupclause;
2814 }
unsigned int Index
Definition: c.h:601
SortGroupClause * get_sortgroupref_clause(Index sortref, List *clauses)
Definition: tlist.c:422

References get_sortgroupref_clause(), lappend(), lfirst_int, NIL, parse(), and PlannerInfo::parse.

Referenced by consider_groupingsets_paths(), and preprocess_grouping_sets().

◆ grouping_planner()

static void grouping_planner ( PlannerInfo root,
double  tuple_fraction 
)
static

Definition at line 1288 of file planner.c.

1289 {
1290  Query *parse = root->parse;
1291  int64 offset_est = 0;
1292  int64 count_est = 0;
1293  double limit_tuples = -1.0;
1294  bool have_postponed_srfs = false;
1295  PathTarget *final_target;
1296  List *final_targets;
1297  List *final_targets_contain_srfs;
1298  bool final_target_parallel_safe;
1299  RelOptInfo *current_rel;
1300  RelOptInfo *final_rel;
1301  FinalPathExtraData extra;
1302  ListCell *lc;
1303 
1304  /* Tweak caller-supplied tuple_fraction if have LIMIT/OFFSET */
1305  if (parse->limitCount || parse->limitOffset)
1306  {
1307  tuple_fraction = preprocess_limit(root, tuple_fraction,
1308  &offset_est, &count_est);
1309 
1310  /*
1311  * If we have a known LIMIT, and don't have an unknown OFFSET, we can
1312  * estimate the effects of using a bounded sort.
1313  */
1314  if (count_est > 0 && offset_est >= 0)
1315  limit_tuples = (double) count_est + (double) offset_est;
1316  }
1317 
1318  /* Make tuple_fraction accessible to lower-level routines */
1319  root->tuple_fraction = tuple_fraction;
1320 
1321  if (parse->setOperations)
1322  {
1323  /*
1324  * If there's a top-level ORDER BY, assume we have to fetch all the
1325  * tuples. This might be too simplistic given all the hackery below
1326  * to possibly avoid the sort; but the odds of accurate estimates here
1327  * are pretty low anyway. XXX try to get rid of this in favor of
1328  * letting plan_set_operations generate both fast-start and
1329  * cheapest-total paths.
1330  */
1331  if (parse->sortClause)
1332  root->tuple_fraction = 0.0;
1333 
1334  /*
1335  * Construct Paths for set operations. The results will not need any
1336  * work except perhaps a top-level sort and/or LIMIT. Note that any
1337  * special work for recursive unions is the responsibility of
1338  * plan_set_operations.
1339  */
1340  current_rel = plan_set_operations(root);
1341 
1342  /*
1343  * We should not need to call preprocess_targetlist, since we must be
1344  * in a SELECT query node. Instead, use the processed_tlist returned
1345  * by plan_set_operations (since this tells whether it returned any
1346  * resjunk columns!), and transfer any sort key information from the
1347  * original tlist.
1348  */
1349  Assert(parse->commandType == CMD_SELECT);
1350 
1351  /* for safety, copy processed_tlist instead of modifying in-place */
1352  root->processed_tlist =
1354  parse->targetList);
1355 
1356  /* Also extract the PathTarget form of the setop result tlist */
1357  final_target = current_rel->cheapest_total_path->pathtarget;
1358 
1359  /* And check whether it's parallel safe */
1360  final_target_parallel_safe =
1361  is_parallel_safe(root, (Node *) final_target->exprs);
1362 
1363  /* The setop result tlist couldn't contain any SRFs */
1364  Assert(!parse->hasTargetSRFs);
1365  final_targets = final_targets_contain_srfs = NIL;
1366 
1367  /*
1368  * Can't handle FOR [KEY] UPDATE/SHARE here (parser should have
1369  * checked already, but let's make sure).
1370  */
1371  if (parse->rowMarks)
1372  ereport(ERROR,
1373  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1374  /*------
1375  translator: %s is a SQL row locking clause such as FOR UPDATE */
1376  errmsg("%s is not allowed with UNION/INTERSECT/EXCEPT",
1378  parse->rowMarks)->strength))));
1379 
1380  /*
1381  * Calculate pathkeys that represent result ordering requirements
1382  */
1383  Assert(parse->distinctClause == NIL);
1385  parse->sortClause,
1386  root->processed_tlist);
1387  }
1388  else
1389  {
1390  /* No set operations, do regular planning */
1391  PathTarget *sort_input_target;
1392  List *sort_input_targets;
1393  List *sort_input_targets_contain_srfs;
1394  bool sort_input_target_parallel_safe;
1395  PathTarget *grouping_target;
1396  List *grouping_targets;
1397  List *grouping_targets_contain_srfs;
1398  bool grouping_target_parallel_safe;
1399  PathTarget *scanjoin_target;
1400  List *scanjoin_targets;
1401  List *scanjoin_targets_contain_srfs;
1402  bool scanjoin_target_parallel_safe;
1403  bool scanjoin_target_same_exprs;
1404  bool have_grouping;
1405  WindowFuncLists *wflists = NULL;
1406  List *activeWindows = NIL;
1407  grouping_sets_data *gset_data = NULL;
1408  standard_qp_extra qp_extra;
1409 
1410  /* A recursive query should always have setOperations */
1411  Assert(!root->hasRecursion);
1412 
1413  /* Preprocess grouping sets and GROUP BY clause, if any */
1414  if (parse->groupingSets)
1415  {
1416  gset_data = preprocess_grouping_sets(root);
1417  }
1418  else if (parse->groupClause)
1419  {
1420  /* Preprocess regular GROUP BY clause, if any */
1421  root->processed_groupClause = list_copy(parse->groupClause);
1422  /* Remove any redundant GROUP BY columns */
1424  }
1425 
1426  /*
1427  * Preprocess targetlist. Note that much of the remaining planning
1428  * work will be done with the PathTarget representation of tlists, but
1429  * we must also maintain the full representation of the final tlist so
1430  * that we can transfer its decoration (resnames etc) to the topmost
1431  * tlist of the finished Plan. This is kept in processed_tlist.
1432  */
1433  preprocess_targetlist(root);
1434 
1435  /*
1436  * Mark all the aggregates with resolved aggtranstypes, and detect
1437  * aggregates that are duplicates or can share transition state. We
1438  * must do this before slicing and dicing the tlist into various
1439  * pathtargets, else some copies of the Aggref nodes might escape
1440  * being marked.
1441  */
1442  if (parse->hasAggs)
1443  {
1444  preprocess_aggrefs(root, (Node *) root->processed_tlist);
1445  preprocess_aggrefs(root, (Node *) parse->havingQual);
1446  }
1447 
1448  /*
1449  * Locate any window functions in the tlist. (We don't need to look
1450  * anywhere else, since expressions used in ORDER BY will be in there
1451  * too.) Note that they could all have been eliminated by constant
1452  * folding, in which case we don't need to do any more work.
1453  */
1454  if (parse->hasWindowFuncs)
1455  {
1456  wflists = find_window_functions((Node *) root->processed_tlist,
1457  list_length(parse->windowClause));
1458  if (wflists->numWindowFuncs > 0)
1459  {
1460  /*
1461  * See if any modifications can be made to each WindowClause
1462  * to allow the executor to execute the WindowFuncs more
1463  * quickly.
1464  */
1465  optimize_window_clauses(root, wflists);
1466 
1467  activeWindows = select_active_windows(root, wflists);
1468  }
1469  else
1470  parse->hasWindowFuncs = false;
1471  }
1472 
1473  /*
1474  * Preprocess MIN/MAX aggregates, if any. Note: be careful about
1475  * adding logic between here and the query_planner() call. Anything
1476  * that is needed in MIN/MAX-optimizable cases will have to be
1477  * duplicated in planagg.c.
1478  */
1479  if (parse->hasAggs)
1481 
1482  /*
1483  * Figure out whether there's a hard limit on the number of rows that
1484  * query_planner's result subplan needs to return. Even if we know a
1485  * hard limit overall, it doesn't apply if the query has any
1486  * grouping/aggregation operations, or SRFs in the tlist.
1487  */
1488  if (parse->groupClause ||
1489  parse->groupingSets ||
1490  parse->distinctClause ||
1491  parse->hasAggs ||
1492  parse->hasWindowFuncs ||
1493  parse->hasTargetSRFs ||
1494  root->hasHavingQual)
1495  root->limit_tuples = -1.0;
1496  else
1497  root->limit_tuples = limit_tuples;
1498 
1499  /* Set up data needed by standard_qp_callback */
1500  qp_extra.activeWindows = activeWindows;
1501  qp_extra.gset_data = gset_data;
1502 
1503  /*
1504  * Generate the best unsorted and presorted paths for the scan/join
1505  * portion of this Query, ie the processing represented by the
1506  * FROM/WHERE clauses. (Note there may not be any presorted paths.)
1507  * We also generate (in standard_qp_callback) pathkey representations
1508  * of the query's sort clause, distinct clause, etc.
1509  */
1510  current_rel = query_planner(root, standard_qp_callback, &qp_extra);
1511 
1512  /*
1513  * Convert the query's result tlist into PathTarget format.
1514  *
1515  * Note: this cannot be done before query_planner() has performed
1516  * appendrel expansion, because that might add resjunk entries to
1517  * root->processed_tlist. Waiting till afterwards is also helpful
1518  * because the target width estimates can use per-Var width numbers
1519  * that were obtained within query_planner().
1520  */
1521  final_target = create_pathtarget(root, root->processed_tlist);
1522  final_target_parallel_safe =
1523  is_parallel_safe(root, (Node *) final_target->exprs);
1524 
1525  /*
1526  * If ORDER BY was given, consider whether we should use a post-sort
1527  * projection, and compute the adjusted target for preceding steps if
1528  * so.
1529  */
1530  if (parse->sortClause)
1531  {
1532  sort_input_target = make_sort_input_target(root,
1533  final_target,
1534  &have_postponed_srfs);
1535  sort_input_target_parallel_safe =
1536  is_parallel_safe(root, (Node *) sort_input_target->exprs);
1537  }
1538  else
1539  {
1540  sort_input_target = final_target;
1541  sort_input_target_parallel_safe = final_target_parallel_safe;
1542  }
1543 
1544  /*
1545  * If we have window functions to deal with, the output from any
1546  * grouping step needs to be what the window functions want;
1547  * otherwise, it should be sort_input_target.
1548  */
1549  if (activeWindows)
1550  {
1551  grouping_target = make_window_input_target(root,
1552  final_target,
1553  activeWindows);
1554  grouping_target_parallel_safe =
1555  is_parallel_safe(root, (Node *) grouping_target->exprs);
1556  }
1557  else
1558  {
1559  grouping_target = sort_input_target;
1560  grouping_target_parallel_safe = sort_input_target_parallel_safe;
1561  }
1562 
1563  /*
1564  * If we have grouping or aggregation to do, the topmost scan/join
1565  * plan node must emit what the grouping step wants; otherwise, it
1566  * should emit grouping_target.
1567  */
1568  have_grouping = (parse->groupClause || parse->groupingSets ||
1569  parse->hasAggs || root->hasHavingQual);
1570  if (have_grouping)
1571  {
1572  scanjoin_target = make_group_input_target(root, final_target);
1573  scanjoin_target_parallel_safe =
1574  is_parallel_safe(root, (Node *) scanjoin_target->exprs);
1575  }
1576  else
1577  {
1578  scanjoin_target = grouping_target;
1579  scanjoin_target_parallel_safe = grouping_target_parallel_safe;
1580  }
1581 
1582  /*
1583  * If there are any SRFs in the targetlist, we must separate each of
1584  * these PathTargets into SRF-computing and SRF-free targets. Replace
1585  * each of the named targets with a SRF-free version, and remember the
1586  * list of additional projection steps we need to add afterwards.
1587  */
1588  if (parse->hasTargetSRFs)
1589  {
1590  /* final_target doesn't recompute any SRFs in sort_input_target */
1591  split_pathtarget_at_srfs(root, final_target, sort_input_target,
1592  &final_targets,
1593  &final_targets_contain_srfs);
1594  final_target = linitial_node(PathTarget, final_targets);
1595  Assert(!linitial_int(final_targets_contain_srfs));
1596  /* likewise for sort_input_target vs. grouping_target */
1597  split_pathtarget_at_srfs(root, sort_input_target, grouping_target,
1598  &sort_input_targets,
1599  &sort_input_targets_contain_srfs);
1600  sort_input_target = linitial_node(PathTarget, sort_input_targets);
1601  Assert(!linitial_int(sort_input_targets_contain_srfs));
1602  /* likewise for grouping_target vs. scanjoin_target */
1603  split_pathtarget_at_srfs(root, grouping_target, scanjoin_target,
1604  &grouping_targets,
1605  &grouping_targets_contain_srfs);
1606  grouping_target = linitial_node(PathTarget, grouping_targets);
1607  Assert(!linitial_int(grouping_targets_contain_srfs));
1608  /* scanjoin_target will not have any SRFs precomputed for it */
1609  split_pathtarget_at_srfs(root, scanjoin_target, NULL,
1610  &scanjoin_targets,
1611  &scanjoin_targets_contain_srfs);
1612  scanjoin_target = linitial_node(PathTarget, scanjoin_targets);
1613  Assert(!linitial_int(scanjoin_targets_contain_srfs));
1614  }
1615  else
1616  {
1617  /* initialize lists; for most of these, dummy values are OK */
1618  final_targets = final_targets_contain_srfs = NIL;
1619  sort_input_targets = sort_input_targets_contain_srfs = NIL;
1620  grouping_targets = grouping_targets_contain_srfs = NIL;
1621  scanjoin_targets = list_make1(scanjoin_target);
1622  scanjoin_targets_contain_srfs = NIL;
1623  }
1624 
1625  /* Apply scan/join target. */
1626  scanjoin_target_same_exprs = list_length(scanjoin_targets) == 1
1627  && equal(scanjoin_target->exprs, current_rel->reltarget->exprs);
1628  apply_scanjoin_target_to_paths(root, current_rel, scanjoin_targets,
1629  scanjoin_targets_contain_srfs,
1630  scanjoin_target_parallel_safe,
1631  scanjoin_target_same_exprs);
1632 
1633  /*
1634  * Save the various upper-rel PathTargets we just computed into
1635  * root->upper_targets[]. The core code doesn't use this, but it
1636  * provides a convenient place for extensions to get at the info. For
1637  * consistency, we save all the intermediate targets, even though some
1638  * of the corresponding upperrels might not be needed for this query.
1639  */
1640  root->upper_targets[UPPERREL_FINAL] = final_target;
1641  root->upper_targets[UPPERREL_ORDERED] = final_target;
1642  root->upper_targets[UPPERREL_DISTINCT] = sort_input_target;
1643  root->upper_targets[UPPERREL_PARTIAL_DISTINCT] = sort_input_target;
1644  root->upper_targets[UPPERREL_WINDOW] = sort_input_target;
1645  root->upper_targets[UPPERREL_GROUP_AGG] = grouping_target;
1646 
1647  /*
1648  * If we have grouping and/or aggregation, consider ways to implement
1649  * that. We build a new upperrel representing the output of this
1650  * phase.
1651  */
1652  if (have_grouping)
1653  {
1654  current_rel = create_grouping_paths(root,
1655  current_rel,
1656  grouping_target,
1657  grouping_target_parallel_safe,
1658  gset_data);
1659  /* Fix things up if grouping_target contains SRFs */
1660  if (parse->hasTargetSRFs)
1661  adjust_paths_for_srfs(root, current_rel,
1662  grouping_targets,
1663  grouping_targets_contain_srfs);
1664  }
1665 
1666  /*
1667  * If we have window functions, consider ways to implement those. We
1668  * build a new upperrel representing the output of this phase.
1669  */
1670  if (activeWindows)
1671  {
1672  current_rel = create_window_paths(root,
1673  current_rel,
1674  grouping_target,
1675  sort_input_target,
1676  sort_input_target_parallel_safe,
1677  wflists,
1678  activeWindows);
1679  /* Fix things up if sort_input_target contains SRFs */
1680  if (parse->hasTargetSRFs)
1681  adjust_paths_for_srfs(root, current_rel,
1682  sort_input_targets,
1683  sort_input_targets_contain_srfs);
1684  }
1685 
1686  /*
1687  * If there is a DISTINCT clause, consider ways to implement that. We
1688  * build a new upperrel representing the output of this phase.
1689  */
1690  if (parse->distinctClause)
1691  {
1692  current_rel = create_distinct_paths(root,
1693  current_rel,
1694  sort_input_target);
1695  }
1696  } /* end of if (setOperations) */
1697 
1698  /*
1699  * If ORDER BY was given, consider ways to implement that, and generate a
1700  * new upperrel containing only paths that emit the correct ordering and
1701  * project the correct final_target. We can apply the original
1702  * limit_tuples limit in sort costing here, but only if there are no
1703  * postponed SRFs.
1704  */
1705  if (parse->sortClause)
1706  {
1707  current_rel = create_ordered_paths(root,
1708  current_rel,
1709  final_target,
1710  final_target_parallel_safe,
1711  have_postponed_srfs ? -1.0 :
1712  limit_tuples);
1713  /* Fix things up if final_target contains SRFs */
1714  if (parse->hasTargetSRFs)
1715  adjust_paths_for_srfs(root, current_rel,
1716  final_targets,
1717  final_targets_contain_srfs);
1718  }
1719 
1720  /*
1721  * Now we are prepared to build the final-output upperrel.
1722  */
1723  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
1724 
1725  /*
1726  * If the input rel is marked consider_parallel and there's nothing that's
1727  * not parallel-safe in the LIMIT clause, then the final_rel can be marked
1728  * consider_parallel as well. Note that if the query has rowMarks or is
1729  * not a SELECT, consider_parallel will be false for every relation in the
1730  * query.
1731  */
1732  if (current_rel->consider_parallel &&
1733  is_parallel_safe(root, parse->limitOffset) &&
1734  is_parallel_safe(root, parse->limitCount))
1735  final_rel->consider_parallel = true;
1736 
1737  /*
1738  * If the current_rel belongs to a single FDW, so does the final_rel.
1739  */
1740  final_rel->serverid = current_rel->serverid;
1741  final_rel->userid = current_rel->userid;
1742  final_rel->useridiscurrent = current_rel->useridiscurrent;
1743  final_rel->fdwroutine = current_rel->fdwroutine;
1744 
1745  /*
1746  * Generate paths for the final_rel. Insert all surviving paths, with
1747  * LockRows, Limit, and/or ModifyTable steps added if needed.
1748  */
1749  foreach(lc, current_rel->pathlist)
1750  {
1751  Path *path = (Path *) lfirst(lc);
1752 
1753  /*
1754  * If there is a FOR [KEY] UPDATE/SHARE clause, add the LockRows node.
1755  * (Note: we intentionally test parse->rowMarks not root->rowMarks
1756  * here. If there are only non-locking rowmarks, they should be
1757  * handled by the ModifyTable node instead. However, root->rowMarks
1758  * is what goes into the LockRows node.)
1759  */
1760  if (parse->rowMarks)
1761  {
1762  path = (Path *) create_lockrows_path(root, final_rel, path,
1763  root->rowMarks,
1765  }
1766 
1767  /*
1768  * If there is a LIMIT/OFFSET clause, add the LIMIT node.
1769  */
1770  if (limit_needed(parse))
1771  {
1772  path = (Path *) create_limit_path(root, final_rel, path,
1773  parse->limitOffset,
1774  parse->limitCount,
1775  parse->limitOption,
1776  offset_est, count_est);
1777  }
1778 
1779  /*
1780  * If this is an INSERT/UPDATE/DELETE/MERGE, add the ModifyTable node.
1781  */
1782  if (parse->commandType != CMD_SELECT)
1783  {
1784  Index rootRelation;
1785  List *resultRelations = NIL;
1786  List *updateColnosLists = NIL;
1787  List *withCheckOptionLists = NIL;
1788  List *returningLists = NIL;
1789  List *mergeActionLists = NIL;
1790  List *rowMarks;
1791 
1793  {
1794  /* Inherited UPDATE/DELETE/MERGE */
1795  RelOptInfo *top_result_rel = find_base_rel(root,
1796  parse->resultRelation);
1797  int resultRelation = -1;
1798 
1799  /* Pass the root result rel forward to the executor. */
1800  rootRelation = parse->resultRelation;
1801 
1802  /* Add only leaf children to ModifyTable. */
1803  while ((resultRelation = bms_next_member(root->leaf_result_relids,
1804  resultRelation)) >= 0)
1805  {
1806  RelOptInfo *this_result_rel = find_base_rel(root,
1807  resultRelation);
1808 
1809  /*
1810  * Also exclude any leaf rels that have turned dummy since
1811  * being added to the list, for example, by being excluded
1812  * by constraint exclusion.
1813  */
1814  if (IS_DUMMY_REL(this_result_rel))
1815  continue;
1816 
1817  /* Build per-target-rel lists needed by ModifyTable */
1818  resultRelations = lappend_int(resultRelations,
1819  resultRelation);
1820  if (parse->commandType == CMD_UPDATE)
1821  {
1822  List *update_colnos = root->update_colnos;
1823 
1824  if (this_result_rel != top_result_rel)
1825  update_colnos =
1827  update_colnos,
1828  this_result_rel->relid,
1829  top_result_rel->relid);
1830  updateColnosLists = lappend(updateColnosLists,
1831  update_colnos);
1832  }
1833  if (parse->withCheckOptions)
1834  {
1835  List *withCheckOptions = parse->withCheckOptions;
1836 
1837  if (this_result_rel != top_result_rel)
1838  withCheckOptions = (List *)
1840  (Node *) withCheckOptions,
1841  this_result_rel,
1842  top_result_rel);
1843  withCheckOptionLists = lappend(withCheckOptionLists,
1844  withCheckOptions);
1845  }
1846  if (parse->returningList)
1847  {
1848  List *returningList = parse->returningList;
1849 
1850  if (this_result_rel != top_result_rel)
1851  returningList = (List *)
1853  (Node *) returningList,
1854  this_result_rel,
1855  top_result_rel);
1856  returningLists = lappend(returningLists,
1857  returningList);
1858  }
1859  if (parse->mergeActionList)
1860  {
1861  ListCell *l;
1862  List *mergeActionList = NIL;
1863 
1864  /*
1865  * Copy MergeActions and translate stuff that
1866  * references attribute numbers.
1867  */
1868  foreach(l, parse->mergeActionList)
1869  {
1870  MergeAction *action = lfirst(l),
1871  *leaf_action = copyObject(action);
1872 
1873  leaf_action->qual =
1875  (Node *) action->qual,
1876  this_result_rel,
1877  top_result_rel);
1878  leaf_action->targetList = (List *)
1880  (Node *) action->targetList,
1881  this_result_rel,
1882  top_result_rel);
1883  if (leaf_action->commandType == CMD_UPDATE)
1884  leaf_action->updateColnos =
1886  action->updateColnos,
1887  this_result_rel->relid,
1888  top_result_rel->relid);
1889  mergeActionList = lappend(mergeActionList,
1890  leaf_action);
1891  }
1892 
1893  mergeActionLists = lappend(mergeActionLists,
1894  mergeActionList);
1895  }
1896  }
1897 
1898  if (resultRelations == NIL)
1899  {
1900  /*
1901  * We managed to exclude every child rel, so generate a
1902  * dummy one-relation plan using info for the top target
1903  * rel (even though that may not be a leaf target).
1904  * Although it's clear that no data will be updated or
1905  * deleted, we still need to have a ModifyTable node so
1906  * that any statement triggers will be executed. (This
1907  * could be cleaner if we fixed nodeModifyTable.c to allow
1908  * zero target relations, but that probably wouldn't be a
1909  * net win.)
1910  */
1911  resultRelations = list_make1_int(parse->resultRelation);
1912  if (parse->commandType == CMD_UPDATE)
1913  updateColnosLists = list_make1(root->update_colnos);
1914  if (parse->withCheckOptions)
1915  withCheckOptionLists = list_make1(parse->withCheckOptions);
1916  if (parse->returningList)
1917  returningLists = list_make1(parse->returningList);
1918  if (parse->mergeActionList)
1919  mergeActionLists = list_make1(parse->mergeActionList);
1920  }
1921  }
1922  else
1923  {
1924  /* Single-relation INSERT/UPDATE/DELETE/MERGE. */
1925  rootRelation = 0; /* there's no separate root rel */
1926  resultRelations = list_make1_int(parse->resultRelation);
1927  if (parse->commandType == CMD_UPDATE)
1928  updateColnosLists = list_make1(root->update_colnos);
1929  if (parse->withCheckOptions)
1930  withCheckOptionLists = list_make1(parse->withCheckOptions);
1931  if (parse->returningList)
1932  returningLists = list_make1(parse->returningList);
1933  if (parse->mergeActionList)
1934  mergeActionLists = list_make1(parse->mergeActionList);
1935  }
1936 
1937  /*
1938  * If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node
1939  * will have dealt with fetching non-locked marked rows, else we
1940  * need to have ModifyTable do that.
1941  */
1942  if (parse->rowMarks)
1943  rowMarks = NIL;
1944  else
1945  rowMarks = root->rowMarks;
1946 
1947  path = (Path *)
1948  create_modifytable_path(root, final_rel,
1949  path,
1950  parse->commandType,
1951  parse->canSetTag,
1952  parse->resultRelation,
1953  rootRelation,
1954  root->partColsUpdated,
1955  resultRelations,
1956  updateColnosLists,
1957  withCheckOptionLists,
1958  returningLists,
1959  rowMarks,
1960  parse->onConflict,
1961  mergeActionLists,
1963  }
1964 
1965  /* And shove it into final_rel */
1966  add_path(final_rel, path);
1967  }
1968 
1969  /*
1970  * Generate partial paths for final_rel, too, if outer query levels might
1971  * be able to make use of them.
1972  */
1973  if (final_rel->consider_parallel && root->query_level > 1 &&
1974  !limit_needed(parse))
1975  {
1976  Assert(!parse->rowMarks && parse->commandType == CMD_SELECT);
1977  foreach(lc, current_rel->partial_pathlist)
1978  {
1979  Path *partial_path = (Path *) lfirst(lc);
1980 
1981  add_partial_path(final_rel, partial_path);
1982  }
1983  }
1984 
1985  extra.limit_needed = limit_needed(parse);
1986  extra.limit_tuples = limit_tuples;
1987  extra.count_est = count_est;
1988  extra.offset_est = offset_est;
1989 
1990  /*
1991  * If there is an FDW that's responsible for all baserels of the query,
1992  * let it consider adding ForeignPaths.
1993  */
1994  if (final_rel->fdwroutine &&
1995  final_rel->fdwroutine->GetForeignUpperPaths)
1996  final_rel->fdwroutine->GetForeignUpperPaths(root, UPPERREL_FINAL,
1997  current_rel, final_rel,
1998  &extra);
1999 
2000  /* Let extensions possibly add some more paths */
2002  (*create_upper_paths_hook) (root, UPPERREL_FINAL,
2003  current_rel, final_rel, &extra);
2004 
2005  /* Note: currently, we leave it to callers to do set_cheapest() */
2006 }
Node * adjust_appendrel_attrs_multilevel(PlannerInfo *root, Node *node, RelOptInfo *childrel, RelOptInfo *parentrel)
Definition: appendinfo.c:521
List * adjust_inherited_attnums_multilevel(PlannerInfo *root, List *attnums, Index child_relid, Index top_parent_relid)
Definition: appendinfo.c:662
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:781
@ BMS_MULTIPLE
Definition: bitmapset.h:73
WindowFuncLists * find_window_functions(Node *clause, Index maxWinRef)
Definition: clauses.c:226
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:223
List * lappend_int(List *list, int datum)
Definition: list.c:357
#define copyObject(obj)
Definition: nodes.h:223
@ CMD_UPDATE
Definition: nodes.h:256
@ CMD_SELECT
Definition: nodes.h:255
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:664
const char * LCS_asString(LockClauseStrength strength)
Definition: analyze.c:3212
LockRowsPath * create_lockrows_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *rowMarks, int epqParam)
Definition: pathnode.c:3645
ModifyTablePath * create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, CmdType operation, bool canSetTag, Index nominalRelation, Index rootRelation, bool partColsUpdated, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, List *mergeActionLists, int epqParam)
Definition: pathnode.c:3707
@ UPPERREL_FINAL
Definition: pathnodes.h:79
#define list_make1_int(x1)
Definition: pg_list.h:227
void preprocess_minmax_aggregates(PlannerInfo *root)
Definition: planagg.c:72
RelOptInfo * query_planner(PlannerInfo *root, query_pathkeys_callback qp_callback, void *qp_extra)
Definition: planmain.c:54
static List * postprocess_setop_tlist(List *new_tlist, List *orig_tlist)
Definition: planner.c:5490
static void remove_useless_groupby_columns(PlannerInfo *root)
Definition: planner.c:2655
static double preprocess_limit(PlannerInfo *root, double tuple_fraction, int64 *offset_est, int64 *count_est)
Definition: planner.c:2411
static PathTarget * make_window_input_target(PlannerInfo *root, PathTarget *final_target, List *activeWindows)
Definition: planner.c:5824
static RelOptInfo * create_distinct_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target)
Definition: planner.c:4573
static void optimize_window_clauses(PlannerInfo *root, WindowFuncLists *wflists)
Definition: planner.c:5527
static PathTarget * make_sort_input_target(PlannerInfo *root, PathTarget *final_target, bool *have_postponed_srfs)
Definition: planner.c:6070
static grouping_sets_data * preprocess_grouping_sets(PlannerInfo *root)
Definition: planner.c:2015
static PathTarget * make_group_input_target(PlannerInfo *root, PathTarget *final_target)
Definition: planner.c:5264
static List * select_active_windows(PlannerInfo *root, WindowFuncLists *wflists)
Definition: planner.c:5667
bool limit_needed(Query *parse)
Definition: planner.c:2596
static RelOptInfo * create_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, double limit_tuples)
Definition: planner.c:5049
static RelOptInfo * create_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *target, bool target_parallel_safe, grouping_sets_data *gd)
Definition: planner.c:3601
static void standard_qp_callback(PlannerInfo *root, void *extra)
Definition: planner.c:3322
static RelOptInfo * create_window_paths(PlannerInfo *root, RelOptInfo *input_rel, PathTarget *input_target, PathTarget *output_target, bool output_target_parallel_safe, WindowFuncLists *wflists, List *activeWindows)
Definition: planner.c:4353
void preprocess_aggrefs(PlannerInfo *root, Node *clause)
Definition: prepagg.c:110
void preprocess_targetlist(PlannerInfo *root)
Definition: preptlist.c:62
RelOptInfo * plan_set_operations(PlannerInfo *root)
Definition: prepunion.c:96
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:407
Cardinality limit_tuples
Definition: pathnodes.h:3290
bool partColsUpdated
Definition: pathnodes.h:546
bool hasRecursion
Definition: pathnodes.h:501
Index query_level
Definition: pathnodes.h:205
List * rowMarks
Definition: pathnodes.h:368
Cardinality limit_tuples
Definition: pathnodes.h:480
Selectivity tuple_fraction
Definition: pathnodes.h:478
List * update_colnos
Definition: pathnodes.h:461
Relids all_result_relids
Definition: pathnodes.h:351
Relids leaf_result_relids
Definition: pathnodes.h:353
Index relid
Definition: pathnodes.h:903
int numWindowFuncs
Definition: clauses.h:21
List * activeWindows
Definition: planner.c:120
grouping_sets_data * gset_data
Definition: planner.c:121
void split_pathtarget_at_srfs(PlannerInfo *root, PathTarget *target, PathTarget *input_target, List **targets, List **targets_contain_srfs)
Definition: tlist.c:881
#define create_pathtarget(root, tlist)
Definition: tlist.h:53

References generate_unaccent_rules::action, standard_qp_extra::activeWindows, add_partial_path(), add_path(), adjust_appendrel_attrs_multilevel(), adjust_inherited_attnums_multilevel(), adjust_paths_for_srfs(), PlannerInfo::all_result_relids, apply_scanjoin_target_to_paths(), Assert(), assign_special_exec_param(), bms_membership(), BMS_MULTIPLE, bms_next_member(), RelOptInfo::cheapest_total_path, CMD_SELECT, CMD_UPDATE, RelOptInfo::consider_parallel, copyObject, FinalPathExtraData::count_est, create_distinct_paths(), create_grouping_paths(), create_limit_path(), create_lockrows_path(), create_modifytable_path(), create_ordered_paths(), create_pathtarget, create_upper_paths_hook, create_window_paths(), equal(), ereport, errcode(), errmsg(), ERROR, PathTarget::exprs, fetch_upper_rel(), find_base_rel(), find_window_functions(), standard_qp_extra::gset_data, PlannerInfo::hasHavingQual, PlannerInfo::hasRecursion, IS_DUMMY_REL, is_parallel_safe(), lappend(), lappend_int(), LCS_asString(), PlannerInfo::leaf_result_relids, lfirst, limit_needed(), FinalPathExtraData::limit_needed, PlannerInfo::limit_tuples, FinalPathExtraData::limit_tuples, linitial_int, linitial_node, list_copy(), list_length(), list_make1, list_make1_int, make_group_input_target(), make_pathkeys_for_sortclauses(), make_sort_input_target(), make_window_input_target(), NIL, WindowFuncLists::numWindowFuncs, FinalPathExtraData::offset_est, optimize_window_clauses(), parse(), PlannerInfo::parse, PlannerInfo::partColsUpdated, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, plan_set_operations(), postprocess_setop_tlist(), preprocess_aggrefs(), preprocess_grouping_sets(), preprocess_limit(), preprocess_minmax_aggregates(), preprocess_targetlist(), PlannerInfo::processed_groupClause, PlannerInfo::processed_tlist, PlannerInfo::query_level, query_planner(), RelOptInfo::relid, RelOptInfo::reltarget, remove_useless_groupby_columns(), PlannerInfo::rowMarks, select_active_windows(), RelOptInfo::serverid, PlannerInfo::sort_pathkeys, split_pathtarget_at_srfs(), standard_qp_callback(), PlannerInfo::tuple_fraction, PlannerInfo::update_colnos, UPPERREL_DISTINCT, UPPERREL_FINAL, UPPERREL_GROUP_AGG, UPPERREL_ORDERED, UPPERREL_PARTIAL_DISTINCT, UPPERREL_WINDOW, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by subquery_planner().

◆ has_volatile_pathkey()

static bool has_volatile_pathkey ( List keys)
static

Definition at line 3096 of file planner.c.

3097 {
3098  ListCell *lc;
3099 
3100  foreach(lc, keys)
3101  {
3102  PathKey *pathkey = lfirst_node(PathKey, lc);
3103 
3104  if (pathkey->pk_eclass->ec_has_volatile)
3105  return true;
3106  }
3107 
3108  return false;
3109 }

References lfirst_node.

Referenced by adjust_group_pathkeys_for_groupagg().

◆ is_degenerate_grouping()

static bool is_degenerate_grouping ( PlannerInfo root)
static

Definition at line 3767 of file planner.c.

3768 {
3769  Query *parse = root->parse;
3770 
3771  return (root->hasHavingQual || parse->groupingSets) &&
3772  !parse->hasAggs && parse->groupClause == NIL;
3773 }

References PlannerInfo::hasHavingQual, NIL, parse(), and PlannerInfo::parse.

Referenced by create_grouping_paths().

◆ limit_needed()

bool limit_needed ( Query parse)

Definition at line 2596 of file planner.c.

2597 {
2598  Node *node;
2599 
2600  node = parse->limitCount;
2601  if (node)
2602  {
2603  if (IsA(node, Const))
2604  {
2605  /* NULL indicates LIMIT ALL, ie, no limit */
2606  if (!((Const *) node)->constisnull)
2607  return true; /* LIMIT with a constant value */
2608  }
2609  else
2610  return true; /* non-constant LIMIT */
2611  }
2612 
2613  node = parse->limitOffset;
2614  if (node)
2615  {
2616  if (IsA(node, Const))
2617  {
2618  /* Treat NULL as no offset; the executor would too */
2619  if (!((Const *) node)->constisnull)
2620  {
2621  int64 offset = DatumGetInt64(((Const *) node)->constvalue);
2622 
2623  if (offset != 0)
2624  return true; /* OFFSET with a nonzero value */
2625  }
2626  }
2627  else
2628  return true; /* non-constant OFFSET */
2629  }
2630 
2631  return false; /* don't need a Limit plan node */
2632 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385

References DatumGetInt64(), IsA, and parse().

Referenced by grouping_planner(), and set_rel_consider_parallel().

◆ make_group_input_target()

static PathTarget * make_group_input_target ( PlannerInfo root,
PathTarget final_target 
)
static

Definition at line 5264 of file planner.c.

5265 {
5266  Query *parse = root->parse;
5267  PathTarget *input_target;
5268  List *non_group_cols;
5269  List *non_group_vars;
5270  int i;
5271  ListCell *lc;
5272 
5273  /*
5274  * We must build a target containing all grouping columns, plus any other
5275  * Vars mentioned in the query's targetlist and HAVING qual.
5276  */
5277  input_target = create_empty_pathtarget();
5278  non_group_cols = NIL;
5279 
5280  i = 0;
5281  foreach(lc, final_target->exprs)
5282  {
5283  Expr *expr = (Expr *) lfirst(lc);
5284  Index sgref = get_pathtarget_sortgroupref(final_target, i);
5285 
5286  if (sgref && root->processed_groupClause &&
5288  root->processed_groupClause) != NULL)
5289  {
5290  /*
5291  * It's a grouping column, so add it to the input target as-is.
5292  */
5293  add_column_to_pathtarget(input_target, expr, sgref);
5294  }
5295  else
5296  {
5297  /*
5298  * Non-grouping column, so just remember the expression for later
5299  * call to pull_var_clause.
5300  */
5301  non_group_cols = lappend(non_group_cols, expr);
5302  }
5303 
5304  i++;
5305  }
5306 
5307  /*
5308  * If there's a HAVING clause, we'll need the Vars it uses, too.
5309  */
5310  if (parse->havingQual)
5311  non_group_cols = lappend(non_group_cols, parse->havingQual);
5312 
5313  /*
5314  * Pull out all the Vars mentioned in non-group cols (plus HAVING), and
5315  * add them to the input target if not already present. (A Var used
5316  * directly as a GROUP BY item will be present already.) Note this
5317  * includes Vars used in resjunk items, so we are covering the needs of
5318  * ORDER BY and window specifications. Vars used within Aggrefs and
5319  * WindowFuncs will be pulled out here, too.
5320  */
5321  non_group_vars = pull_var_clause((Node *) non_group_cols,
5325  add_new_columns_to_pathtarget(input_target, non_group_vars);
5326 
5327  /* clean up cruft */
5328  list_free(non_group_vars);
5329  list_free(non_group_cols);
5330 
5331  /* XXX this causes some redundant cost calculation ... */
5332  return set_pathtarget_cost_width(root, input_target);
5333 }
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition: costsize.c:6286
void list_free(List *list)
Definition: list.c:1546
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:187
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:189
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:190
#define get_pathtarget_sortgroupref(target, colno)
Definition: pathnodes.h:1529
SortGroupClause * get_sortgroupref_clause_noerr(Index sortref, List *clauses)
Definition: tlist.c:443
void add_new_columns_to_pathtarget(PathTarget *target, List *exprs)
Definition: tlist.c:752
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:681
List * pull_var_clause(Node *node, int flags)
Definition: var.c:607

References add_column_to_pathtarget(), add_new_columns_to_pathtarget(), create_empty_pathtarget(), PathTarget::exprs, get_pathtarget_sortgroupref, get_sortgroupref_clause_noerr(), i, lappend(), lfirst, list_free(), NIL, parse(), PlannerInfo::parse, PlannerInfo::processed_groupClause, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, and set_pathtarget_cost_width().

Referenced by grouping_planner().

◆ make_grouping_rel()

static RelOptInfo * make_grouping_rel ( PlannerInfo root,
RelOptInfo input_rel,
PathTarget target,
bool  target_parallel_safe,
Node havingQual 
)
static

Definition at line 3714 of file planner.c.

3717 {
3718  RelOptInfo *grouped_rel;
3719 
3720  if (IS_OTHER_REL(input_rel))
3721  {
3722  grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG,
3723  input_rel->relids);
3724  grouped_rel->reloptkind = RELOPT_OTHER_UPPER_REL;
3725  }
3726  else
3727  {
3728  /*
3729  * By tradition, the relids set for the main grouping relation is
3730  * NULL. (This could be changed, but might require adjustments
3731  * elsewhere.)
3732  */
3733  grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
3734  }
3735 
3736  /* Set target. */
3737  grouped_rel->reltarget = target;
3738 
3739  /*
3740  * If the input relation is not parallel-safe, then the grouped relation
3741  * can't be parallel-safe, either. Otherwise, it's parallel-safe if the
3742  * target list and HAVING quals are parallel-safe.
3743  */
3744  if (input_rel->consider_parallel && target_parallel_safe &&
3745  is_parallel_safe(root, (Node *) havingQual))
3746  grouped_rel->consider_parallel = true;
3747 
3748  /*
3749  * If the input rel belongs to a single FDW, so does the grouped rel.
3750  */
3751  grouped_rel->serverid = input_rel->serverid;
3752  grouped_rel->userid = input_rel->userid;
3753  grouped_rel->useridiscurrent = input_rel->useridiscurrent;
3754  grouped_rel->fdwroutine = input_rel->fdwroutine;
3755 
3756  return grouped_rel;
3757 }
@ RELOPT_OTHER_UPPER_REL
Definition: pathnodes.h:817

References RelOptInfo::consider_parallel, fetch_upper_rel(), IS_OTHER_REL, is_parallel_safe(), RelOptInfo::relids, RELOPT_OTHER_UPPER_REL, RelOptInfo::reloptkind, RelOptInfo::reltarget, RelOptInfo::serverid, UPPERREL_GROUP_AGG, RelOptInfo::userid, and RelOptInfo::useridiscurrent.

Referenced by create_grouping_paths(), and create_partitionwise_grouping_paths().

◆ make_ordered_path()

static Path* make_ordered_path ( PlannerInfo root,
RelOptInfo rel,
Path path,
Path cheapest_path,
List pathkeys 
)
static

Definition at line 6734 of file planner.c.

6736 {
6737  bool is_sorted;
6738  int presorted_keys;
6739 
6740  is_sorted = pathkeys_count_contained_in(pathkeys,
6741  path->pathkeys,
6742  &presorted_keys);
6743 
6744  if (!is_sorted)
6745  {
6746  /*
6747  * Try at least sorting the cheapest path and also try incrementally
6748  * sorting any path which is partially sorted already (no need to deal
6749  * with paths which have presorted keys when incremental sort is
6750  * disabled unless it's the cheapest input path).
6751  */
6752  if (path != cheapest_path &&
6753  (presorted_keys == 0 || !enable_incremental_sort))
6754  return NULL;
6755 
6756  /*
6757  * We've no need to consider both a sort and incremental sort. We'll
6758  * just do a sort if there are no presorted keys and an incremental
6759  * sort when there are presorted keys.
6760  */
6761  if (presorted_keys == 0 || !enable_incremental_sort)
6762  path = (Path *) create_sort_path(root,
6763  rel,
6764  path,
6765  pathkeys,
6766  -1.0);
6767  else
6768  path = (Path *) create_incremental_sort_path(root,
6769  rel,
6770  path,
6771  pathkeys,
6772  presorted_keys,
6773  -1.0);
6774  }
6775 
6776  return path;
6777 }

References create_incremental_sort_path(), create_sort_path(), enable_incremental_sort, Path::pathkeys, and pathkeys_count_contained_in().

Referenced by add_paths_to_grouping_rel(), and create_partial_grouping_paths().

◆ make_partial_grouping_target()

static PathTarget * make_partial_grouping_target ( PlannerInfo root,
PathTarget grouping_target,
Node havingQual 
)
static

Definition at line 5352 of file planner.c.

5355 {
5356  PathTarget *partial_target;
5357  List *non_group_cols;
5358  List *non_group_exprs;
5359  int i;
5360  ListCell *lc;
5361 
5362  partial_target = create_empty_pathtarget();
5363  non_group_cols = NIL;
5364 
5365  i = 0;
5366  foreach(lc, grouping_target->exprs)
5367  {
5368  Expr *expr = (Expr *) lfirst(lc);
5369  Index sgref = get_pathtarget_sortgroupref(grouping_target, i);
5370 
5371  if (sgref && root->processed_groupClause &&
5373  root->processed_groupClause) != NULL)
5374  {
5375  /*
5376  * It's a grouping column, so add it to the partial_target as-is.
5377  * (This allows the upper agg step to repeat the grouping calcs.)
5378  */
5379  add_column_to_pathtarget(partial_target, expr, sgref);
5380  }
5381  else
5382  {
5383  /*
5384  * Non-grouping column, so just remember the expression for later
5385  * call to pull_var_clause.
5386  */
5387  non_group_cols = lappend(non_group_cols, expr);
5388  }
5389 
5390  i++;
5391  }
5392 
5393  /*
5394  * If there's a HAVING clause, we'll need the Vars/Aggrefs it uses, too.
5395  */
5396  if (havingQual)
5397  non_group_cols = lappend(non_group_cols, havingQual);
5398 
5399  /*
5400  * Pull out all the Vars, PlaceHolderVars, and Aggrefs mentioned in
5401  * non-group cols (plus HAVING), and add them to the partial_target if not
5402  * already present. (An expression used directly as a GROUP BY item will
5403  * be present already.) Note this includes Vars used in resjunk items, so
5404  * we are covering the needs of ORDER BY and window specifications.
5405  */
5406  non_group_exprs = pull_var_clause((Node *) non_group_cols,
5410 
5411  add_new_columns_to_pathtarget(partial_target, non_group_exprs);
5412 
5413  /*
5414  * Adjust Aggrefs to put them in partial mode. At this point all Aggrefs
5415  * are at the top level of the target list, so we can just scan the list
5416  * rather than recursing through the expression trees.
5417  */
5418  foreach(lc, partial_target->exprs)
5419  {
5420  Aggref *aggref = (Aggref *) lfirst(lc);
5421 
5422  if (IsA(aggref, Aggref))
5423  {
5424  Aggref *newaggref;
5425 
5426  /*
5427  * We shouldn't need to copy the substructure of the Aggref node,
5428  * but flat-copy the node itself to avoid damaging other trees.
5429  */
5430  newaggref = makeNode(Aggref);
5431  memcpy(newaggref, aggref, sizeof(Aggref));
5432 
5433  /* For now, assume serialization is required */
5435 
5436  lfirst(lc) = newaggref;
5437  }
5438  }
5439 
5440  /* clean up cruft */
5441  list_free(non_group_exprs);
5442  list_free(non_group_cols);
5443 
5444  /* XXX this causes some redundant cost calculation ... */
5445  return set_pathtarget_cost_width(root, partial_target);
5446 }
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:186
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:5455

References add_column_to_pathtarget(), add_new_columns_to_pathtarget(), AGGSPLIT_INITIAL_SERIAL, create_empty_pathtarget(), PathTarget::exprs, get_pathtarget_sortgroupref, get_sortgroupref_clause_noerr(), i, IsA, lappend(), lfirst, list_free(), makeNode, mark_partial_aggref(), NIL, PlannerInfo::processed_groupClause, pull_var_clause(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and set_pathtarget_cost_width().

Referenced by create_partial_grouping_paths().

◆ make_pathkeys_for_window()

static List * make_pathkeys_for_window ( PlannerInfo root,
WindowClause wc,
List tlist 
)
static

Definition at line 5944 of file planner.c.

5946 {
5947  List *window_pathkeys = NIL;
5948 
5949  /* Throw error if can't sort */
5951  ereport(ERROR,
5952  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5953  errmsg("could not implement window PARTITION BY"),
5954  errdetail("Window partitioning columns must be of sortable datatypes.")));
5956  ereport(ERROR,
5957  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5958  errmsg("could not implement window ORDER BY"),
5959  errdetail("Window ordering columns must be of sortable datatypes.")));
5960 
5961  /*
5962  * First fetch the pathkeys for the PARTITION BY clause. We can safely
5963  * remove any clauses from the wc->partitionClause for redundant pathkeys.
5964  */
5965  if (wc->partitionClause != NIL)
5966  {
5967  bool sortable;
5968 
5969  window_pathkeys = make_pathkeys_for_sortclauses_extended(root,
5970  &wc->partitionClause,
5971  tlist,
5972  true,
5973  &sortable);
5974 
5975  Assert(sortable);
5976  }
5977 
5978  /*
5979  * In principle, we could also consider removing redundant ORDER BY items
5980  * too as doing so does not alter the result of peer row checks done by
5981  * the executor. However, we must *not* remove the ordering column for
5982  * RANGE OFFSET cases, as the executor needs that for in_range tests even
5983  * if it's known to be equal to some partitioning column.
5984  */
5985  if (wc->orderClause != NIL)
5986  {
5987  List *orderby_pathkeys;
5988 
5989  orderby_pathkeys = make_pathkeys_for_sortclauses(root,
5990  wc->orderClause,
5991  tlist);
5992 
5993  /* Okay, make the combined pathkeys */
5994  if (window_pathkeys != NIL)
5995  window_pathkeys = append_pathkeys(window_pathkeys, orderby_pathkeys);
5996  else
5997  window_pathkeys = orderby_pathkeys;
5998  }
5999 
6000  return window_pathkeys;
6001 }
List * make_pathkeys_for_sortclauses_extended(PlannerInfo *root, List **sortclauses, List *tlist, bool remove_redundant, bool *sortable)
Definition: pathkeys.c:1384
List * partitionClause
Definition: parsenodes.h:1501
List * orderClause
Definition: parsenodes.h:1503

References append_pathkeys(), Assert(), ereport, errcode(), errdetail(), errmsg(), ERROR, grouping_is_sortable(), make_pathkeys_for_sortclauses(), make_pathkeys_for_sortclauses_extended(), NIL, WindowClause::orderClause, and WindowClause::partitionClause.

Referenced by create_one_window_path(), and standard_qp_callback().

◆ make_sort_input_target()

static PathTarget * make_sort_input_target ( PlannerInfo root,
PathTarget final_target,
bool have_postponed_srfs 
)
static

Definition at line 6070 of file planner.c.

6073 {
6074  Query *parse = root->parse;
6075  PathTarget *input_target;
6076  int ncols;
6077  bool *col_is_srf;
6078  bool *postpone_col;
6079  bool have_srf;
6080  bool have_volatile;
6081  bool have_expensive;
6082  bool have_srf_sortcols;
6083  bool postpone_srfs;
6084  List *postponable_cols;
6085  List *postponable_vars;
6086  int i;
6087  ListCell *lc;
6088 
6089  /* Shouldn't get here unless query has ORDER BY */
6090  Assert(parse->sortClause);
6091 
6092  *have_postponed_srfs = false; /* default result */
6093 
6094  /* Inspect tlist and collect per-column information */
6095  ncols = list_length(final_target->exprs);
6096  col_is_srf = (bool *) palloc0(ncols * sizeof(bool));
6097  postpone_col = (bool *) palloc0(ncols * sizeof(bool));
6098  have_srf = have_volatile = have_expensive = have_srf_sortcols = false;
6099 
6100  i = 0;
6101  foreach(lc, final_target->exprs)
6102  {
6103  Expr *expr = (Expr *) lfirst(lc);
6104 
6105  /*
6106  * If the column has a sortgroupref, assume it has to be evaluated
6107  * before sorting. Generally such columns would be ORDER BY, GROUP
6108  * BY, etc targets. One exception is columns that were removed from
6109  * GROUP BY by remove_useless_groupby_columns() ... but those would
6110  * only be Vars anyway. There don't seem to be any cases where it
6111  * would be worth the trouble to double-check.
6112  */
6113  if (get_pathtarget_sortgroupref(final_target, i) == 0)
6114  {
6115  /*
6116  * Check for SRF or volatile functions. Check the SRF case first
6117  * because we must know whether we have any postponed SRFs.
6118  */
6119  if (parse->hasTargetSRFs &&
6120  expression_returns_set((Node *) expr))
6121  {
6122  /* We'll decide below whether these are postponable */
6123  col_is_srf[i] = true;
6124  have_srf = true;
6125  }
6126  else if (contain_volatile_functions((Node *) expr))
6127  {
6128  /* Unconditionally postpone */
6129  postpone_col[i] = true;
6130  have_volatile = true;
6131  }
6132  else
6133  {
6134  /*
6135  * Else check the cost. XXX it's annoying to have to do this
6136  * when set_pathtarget_cost_width() just did it. Refactor to
6137  * allow sharing the work?
6138  */
6139  QualCost cost;
6140 
6141  cost_qual_eval_node(&cost, (Node *) expr, root);
6142 
6143  /*
6144  * We arbitrarily define "expensive" as "more than 10X
6145  * cpu_operator_cost". Note this will take in any PL function
6146  * with default cost.
6147  */
6148  if (cost.per_tuple > 10 * cpu_operator_cost)
6149  {
6150  postpone_col[i] = true;
6151  have_expensive = true;
6152  }
6153  }
6154  }
6155  else
6156  {
6157  /* For sortgroupref cols, just check if any contain SRFs */
6158  if (!have_srf_sortcols &&
6159  parse->hasTargetSRFs &&
6160  expression_returns_set((Node *) expr))
6161  have_srf_sortcols = true;
6162  }
6163 
6164  i++;
6165  }
6166 
6167  /*
6168  * We can postpone SRFs if we have some but none are in sortgroupref cols.
6169  */
6170  postpone_srfs = (have_srf && !have_srf_sortcols);
6171 
6172  /*
6173  * If we don't need a post-sort projection, just return final_target.
6174  */
6175  if (!(postpone_srfs || have_volatile ||
6176  (have_expensive &&
6177  (parse->limitCount || root->tuple_fraction > 0))))
6178  return final_target;
6179 
6180  /*
6181  * Report whether the post-sort projection will contain set-returning
6182  * functions. This is important because it affects whether the Sort can
6183  * rely on the query's LIMIT (if any) to bound the number of rows it needs
6184  * to return.
6185  */
6186  *have_postponed_srfs = postpone_srfs;
6187 
6188  /*
6189  * Construct the sort-input target, taking all non-postponable columns and
6190  * then adding Vars, PlaceHolderVars, Aggrefs, and WindowFuncs found in
6191  * the postponable ones.
6192  */
6193  input_target = create_empty_pathtarget();
6194  postponable_cols = NIL;
6195 
6196  i = 0;
6197  foreach(lc, final_target->exprs)
6198  {
6199  Expr *expr = (Expr *) lfirst(lc);
6200 
6201  if (postpone_col[i] || (postpone_srfs && col_is_srf[i]))
6202  postponable_cols = lappend(postponable_cols, expr);
6203  else
6204  add_column_to_pathtarget(input_target, expr,
6205  get_pathtarget_sortgroupref(final_target, i));
6206 
6207  i++;
6208  }
6209 
6210  /*
6211  * Pull out all the Vars, Aggrefs, and WindowFuncs mentioned in
6212  * postponable columns, and add them to the sort-input target if not
6213  * already present. (Some might be there already.) We mustn't
6214  * deconstruct Aggrefs or WindowFuncs here, since the projection node
6215  * would be unable to recompute them.
6216  */
6217  postponable_vars = pull_var_clause((Node *) postponable_cols,
6221  add_new_columns_to_pathtarget(input_target, postponable_vars);
6222 
6223  /* clean up cruft */
6224  list_free(postponable_vars);
6225  list_free(postponable_cols);
6226 
6227  /* XXX this represents even more redundant cost calculation ... */
6228  return set_pathtarget_cost_width(root, input_target);
6229 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:518
double cpu_operator_cost
Definition: costsize.c:123
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4666
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:730
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:188
Cost per_tuple
Definition: pathnodes.h:48

References add_column_to_pathtarget(), add_new_columns_to_pathtarget(), Assert(), contain_volatile_functions(), cost_qual_eval_node(), cpu_operator_cost, create_empty_pathtarget(), expression_returns_set(), PathTarget::exprs, get_pathtarget_sortgroupref, i, lappend(), lfirst, list_free(), list_length(), NIL, palloc0(), parse(), PlannerInfo::parse, QualCost::per_tuple, pull_var_clause(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, set_pathtarget_cost_width(), and PlannerInfo::tuple_fraction.

Referenced by grouping_planner().

◆ make_window_input_target()

static PathTarget * make_window_input_target ( PlannerInfo root,
PathTarget final_target,
List activeWindows 
)
static

Definition at line 5824 of file planner.c.

5827 {
5828  PathTarget *input_target;
5829  Bitmapset *sgrefs;
5830  List *flattenable_cols;
5831  List *flattenable_vars;
5832  int i;
5833  ListCell *lc;
5834 
5835  Assert(root->parse->hasWindowFuncs);
5836 
5837  /*
5838  * Collect the sortgroupref numbers of window PARTITION/ORDER BY clauses
5839  * into a bitmapset for convenient reference below.
5840  */
5841  sgrefs = NULL;
5842  foreach(lc, activeWindows)
5843  {
5845  ListCell *lc2;
5846 
5847  foreach(lc2, wc->partitionClause)
5848  {
5850 
5851  sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
5852  }
5853  foreach(lc2, wc->orderClause)
5854  {
5856 
5857  sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
5858  }
5859  }
5860 
5861  /* Add in sortgroupref numbers of GROUP BY clauses, too */
5862  foreach(lc, root->processed_groupClause)
5863  {
5865 
5866  sgrefs = bms_add_member(sgrefs, grpcl->tleSortGroupRef);
5867  }
5868 
5869  /*
5870  * Construct a target containing all the non-flattenable targetlist items,
5871  * and save aside the others for a moment.
5872  */
5873  input_target = create_empty_pathtarget();
5874  flattenable_cols = NIL;
5875 
5876  i = 0;
5877  foreach(lc, final_target->exprs)
5878  {
5879  Expr *expr = (Expr *) lfirst(lc);
5880  Index sgref = get_pathtarget_sortgroupref(final_target, i);
5881 
5882  /*
5883  * Don't want to deconstruct window clauses or GROUP BY items. (Note
5884  * that such items can't contain window functions, so it's okay to
5885  * compute them below the WindowAgg nodes.)
5886  */
5887  if (sgref != 0 && bms_is_member(sgref, sgrefs))
5888  {
5889  /*
5890  * Don't want to deconstruct this value, so add it to the input
5891  * target as-is.
5892  */
5893  add_column_to_pathtarget(input_target, expr, sgref);
5894  }
5895  else
5896  {
5897  /*
5898  * Column is to be flattened, so just remember the expression for
5899  * later call to pull_var_clause.
5900  */
5901  flattenable_cols = lappend(flattenable_cols, expr);
5902  }
5903 
5904  i++;
5905  }
5906 
5907  /*
5908  * Pull out all the Vars and Aggrefs mentioned in flattenable columns, and
5909  * add them to the input target if not already present. (Some might be
5910  * there already because they're used directly as window/group clauses.)
5911  *
5912  * Note: it's essential to use PVC_INCLUDE_AGGREGATES here, so that any
5913  * Aggrefs are placed in the Agg node's tlist and not left to be computed
5914  * at higher levels. On the other hand, we should recurse into
5915  * WindowFuncs to make sure their input expressions are available.
5916  */
5917  flattenable_vars = pull_var_clause((Node *) flattenable_cols,
5921  add_new_columns_to_pathtarget(input_target, flattenable_vars);
5922 
5923  /* clean up cruft */
5924  list_free(flattenable_vars);
5925  list_free(flattenable_cols);
5926 
5927  /* XXX this causes some redundant cost calculation ... */
5928  return set_pathtarget_cost_width(root, input_target);
5929 }

References add_column_to_pathtarget(), add_new_columns_to_pathtarget(), Assert(), bms_add_member(), bms_is_member(), create_empty_pathtarget(), PathTarget::exprs, get_pathtarget_sortgroupref, i, lappend(), lfirst, lfirst_node, list_free(), NIL, WindowClause::orderClause, PlannerInfo::parse, WindowClause::partitionClause, PlannerInfo::processed_groupClause, pull_var_clause(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, set_pathtarget_cost_width(), and SortGroupClause::tleSortGroupRef.

Referenced by grouping_planner().

◆ mark_partial_aggref()

void mark_partial_aggref ( Aggref agg,
AggSplit  aggsplit 
)

Definition at line 5455 of file planner.c.

5456 {
5457  /* aggtranstype should be computed by this point */
5458  Assert(OidIsValid(agg->aggtranstype));
5459  /* ... but aggsplit should still be as the parser left it */
5460  Assert(agg->aggsplit == AGGSPLIT_SIMPLE);
5461 
5462  /* Mark the Aggref with the intended partial-aggregation mode */
5463  agg->aggsplit = aggsplit;
5464 
5465  /*
5466  * Adjust result type if needed. Normally, a partial aggregate returns
5467  * the aggregate's transition type; but if that's INTERNAL and we're
5468  * serializing, it returns BYTEA instead.
5469  */
5470  if (DO_AGGSPLIT_SKIPFINAL(aggsplit))
5471  {
5472  if (agg->aggtranstype == INTERNALOID && DO_AGGSPLIT_SERIALIZE(aggsplit))
5473  agg->aggtype = BYTEAOID;
5474  else
5475  agg->aggtype = agg->aggtranstype;
5476  }
5477 }
#define OidIsValid(objectId)
Definition: c.h:762
#define DO_AGGSPLIT_SKIPFINAL(as)
Definition: nodes.h:375
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:376

References AGGSPLIT_SIMPLE, Assert(), DO_AGGSPLIT_SERIALIZE, DO_AGGSPLIT_SKIPFINAL, and OidIsValid.

Referenced by convert_combining_aggrefs(), and make_partial_grouping_target().

◆ optimize_window_clauses()

static void optimize_window_clauses ( PlannerInfo root,
WindowFuncLists wflists 
)
static

Definition at line 5527 of file planner.c.

5528 {
5529  List *windowClause = root->parse->windowClause;
5530  ListCell *lc;
5531 
5532  foreach(lc, windowClause)
5533  {
5535  ListCell *lc2;
5536  int optimizedFrameOptions = 0;
5537 
5538  Assert(wc->winref <= wflists->maxWinRef);
5539 
5540  /* skip any WindowClauses that have no WindowFuncs */
5541  if (wflists->windowFuncs[wc->winref] == NIL)
5542  continue;
5543 
5544  foreach(lc2, wflists->windowFuncs[wc->winref])
5545  {
5548  WindowFunc *wfunc = lfirst_node(WindowFunc, lc2);
5549  Oid prosupport;
5550 
5551  prosupport = get_func_support(wfunc->winfnoid);
5552 
5553  /* Check if there's a support function for 'wfunc' */
5554  if (!OidIsValid(prosupport))
5555  break; /* can't optimize this WindowClause */
5556 
5557  req.type = T_SupportRequestOptimizeWindowClause;
5558  req.window_clause = wc;
5559  req.window_func = wfunc;
5560  req.frameOptions = wc->frameOptions;
5561 
5562  /* call the support function */
5564  DatumGetPointer(OidFunctionCall1(prosupport,
5565  PointerGetDatum(&req)));
5566 
5567  /*
5568  * Skip to next WindowClause if the support function does not
5569  * support this request type.
5570  */
5571  if (res == NULL)
5572  break;
5573 
5574  /*
5575  * Save these frameOptions for the first WindowFunc for this
5576  * WindowClause.
5577  */
5578  if (foreach_current_index(lc2) == 0)
5579  optimizedFrameOptions = res->frameOptions;
5580 
5581  /*
5582  * On subsequent WindowFuncs, if the frameOptions are not the same
5583  * then we're unable to optimize the frameOptions for this
5584  * WindowClause.
5585  */
5586  else if (optimizedFrameOptions != res->frameOptions)
5587  break; /* skip to the next WindowClause, if any */
5588  }
5589 
5590  /* adjust the frameOptions if all WindowFunc's agree that it's ok */
5591  if (lc2 == NULL && wc->frameOptions != optimizedFrameOptions)
5592  {
5593  ListCell *lc3;
5594 
5595  /* apply the new frame options */
5596  wc->frameOptions = optimizedFrameOptions;
5597 
5598  /*
5599  * We now check to see if changing the frameOptions has caused
5600  * this WindowClause to be a duplicate of some other WindowClause.
5601  * This can only happen if we have multiple WindowClauses, so
5602  * don't bother if there's only 1.
5603  */
5604  if (list_length(windowClause) == 1)
5605  continue;
5606 
5607  /*
5608  * Do the duplicate check and reuse the existing WindowClause if
5609  * we find a duplicate.
5610  */
5611  foreach(lc3, windowClause)
5612  {
5613  WindowClause *existing_wc = lfirst_node(WindowClause, lc3);
5614 
5615  /* skip over the WindowClause we're currently editing */
5616  if (existing_wc == wc)
5617  continue;
5618 
5619  /*
5620  * Perform the same duplicate check that is done in
5621  * transformWindowFuncCall.
5622  */
5623  if (equal(wc->partitionClause, existing_wc->partitionClause) &&
5624  equal(wc->orderClause, existing_wc->orderClause) &&
5625  wc->frameOptions == existing_wc->frameOptions &&
5626  equal(wc->startOffset, existing_wc->startOffset) &&
5627  equal(wc->endOffset, existing_wc->endOffset))
5628  {
5629  ListCell *lc4;
5630 
5631  /*
5632  * Now move each WindowFunc in 'wc' into 'existing_wc'.
5633  * This required adjusting each WindowFunc's winref and
5634  * moving the WindowFuncs in 'wc' to the list of
5635  * WindowFuncs in 'existing_wc'.
5636  */
5637  foreach(lc4, wflists->windowFuncs[wc->winref])
5638  {
5639  WindowFunc *wfunc = lfirst_node(WindowFunc, lc4);
5640 
5641  wfunc->winref = existing_wc->winref;
5642  }
5643 
5644  /* move list items */
5645  wflists->windowFuncs[existing_wc->winref] = list_concat(wflists->windowFuncs[existing_wc->winref],
5646  wflists->windowFuncs[wc->winref]);
5647  wflists->windowFuncs[wc->winref] = NIL;
5648 
5649  /*
5650  * transformWindowFuncCall() should have made sure there
5651  * are no other duplicates, so we needn't bother looking
5652  * any further.
5653  */
5654  break;
5655  }
5656  }
5657  }
5658  }
5659 }
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:680
RegProcedure get_func_support(Oid funcid)
Definition: lsyscache.c:1836
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
List * windowClause
Definition: parsenodes.h:206
struct WindowClause * window_clause
Definition: supportnodes.h:339
Node * startOffset
Definition: parsenodes.h:1505
Node * endOffset
Definition: parsenodes.h:1506
Index maxWinRef
Definition: clauses.h:22
Index winref
Definition: primnodes.h:565
Oid winfnoid
Definition: primnodes.h:553

References Assert(), DatumGetPointer(), WindowClause::endOffset, equal(), foreach_current_index, WindowClause::frameOptions, SupportRequestOptimizeWindowClause::frameOptions, get_func_support(), if(), lfirst_node, list_concat(), list_length(), WindowFuncLists::maxWinRef, NIL, OidFunctionCall1, OidIsValid, WindowClause::orderClause, PlannerInfo::parse, WindowClause::partitionClause, PointerGetDatum(), res, WindowClause::startOffset, SupportRequestOptimizeWindowClause::type, SupportRequestOptimizeWindowClause::window_clause, SupportRequestOptimizeWindowClause::window_func, Query::windowClause, WindowFuncLists::windowFuncs, WindowFunc::winfnoid, WindowClause::winref, and WindowFunc::winref.

Referenced by grouping_planner().

◆ plan_cluster_use_sort()

bool plan_cluster_use_sort ( Oid  tableOid,
Oid  indexOid 
)

Definition at line 6480 of file planner.c.

6481 {
6482  PlannerInfo *root;
6483  Query *query;
6484  PlannerGlobal *glob;
6485  RangeTblEntry *rte;
6486  RelOptInfo *rel;
6487  IndexOptInfo *indexInfo;
6488  QualCost indexExprCost;
6489  Cost comparisonCost;
6490  Path *seqScanPath;
6491  Path seqScanAndSortPath;
6492  IndexPath *indexScanPath;
6493  ListCell *lc;
6494 
6495  /* We can short-circuit the cost comparison if indexscans are disabled */
6496  if (!enable_indexscan)
6497  return true; /* use sort */
6498 
6499  /* Set up mostly-dummy planner state */
6500  query = makeNode(Query);
6501  query->commandType = CMD_SELECT;
6502 
6503  glob = makeNode(PlannerGlobal);
6504 
6505  root = makeNode(PlannerInfo);
6506  root->parse = query;
6507  root->glob = glob;
6508  root->query_level = 1;
6509  root->planner_cxt = CurrentMemoryContext;
6510  root->wt_param_id = -1;
6512 
6513  /* Build a minimal RTE for the rel */
6514  rte = makeNode(RangeTblEntry);
6515  rte->rtekind = RTE_RELATION;
6516  rte->relid = tableOid;
6517  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6519  rte->lateral = false;
6520  rte->inh = false;
6521  rte->inFromCl = true;
6522  query->rtable = list_make1(rte);
6523  addRTEPermissionInfo(&query->rteperminfos, rte);
6524 
6525  /* Set up RTE/RelOptInfo arrays */
6527 
6528  /* Build RelOptInfo */
6529  rel = build_simple_rel(root, 1, NULL);
6530 
6531  /* Locate IndexOptInfo for the target index */
6532  indexInfo = NULL;
6533  foreach(lc, rel->indexlist)
6534  {
6535  indexInfo = lfirst_node(IndexOptInfo, lc);
6536  if (indexInfo->indexoid == indexOid)
6537  break;
6538  }
6539 
6540  /*
6541  * It's possible that get_relation_info did not generate an IndexOptInfo
6542  * for the desired index; this could happen if it's not yet reached its
6543  * indcheckxmin usability horizon, or if it's a system index and we're
6544  * ignoring system indexes. In such cases we should tell CLUSTER to not
6545  * trust the index contents but use seqscan-and-sort.
6546  */
6547  if (lc == NULL) /* not in the list? */
6548  return true; /* use sort */
6549 
6550  /*
6551  * Rather than doing all the pushups that would be needed to use
6552  * set_baserel_size_estimates, just do a quick hack for rows and width.
6553  */
6554  rel->rows = rel->tuples;
6555  rel->reltarget->width = get_relation_data_width(tableOid, NULL);
6556 
6557  root->total_table_pages = rel->pages;
6558 
6559  /*
6560  * Determine eval cost of the index expressions, if any. We need to
6561  * charge twice that amount for each tuple comparison that happens during
6562  * the sort, since tuplesort.c will have to re-evaluate the index
6563  * expressions each time. (XXX that's pretty inefficient...)
6564  */
6565  cost_qual_eval(&indexExprCost, indexInfo->indexprs, root);
6566  comparisonCost = 2.0 * (indexExprCost.startup + indexExprCost.per_tuple);
6567 
6568  /* Estimate the cost of seq scan + sort */
6569  seqScanPath = create_seqscan_path(root, rel, NULL, 0);
6570  cost_sort(&seqScanAndSortPath, root, NIL,
6571  seqScanPath->total_cost, rel->tuples, rel->reltarget->width,
6572  comparisonCost, maintenance_work_mem, -1.0);
6573 
6574  /* Estimate the cost of index scan */
6575  indexScanPath = create_index_path(root, indexInfo,
6576  NIL, NIL, NIL, NIL,
6577  ForwardScanDirection, false,
6578  NULL, 1.0, false);
6579 
6580  return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
6581 }
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:4640
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:2124
bool enable_indexscan
Definition: costsize.c:135
int maintenance_work_mem
Definition: globals.c:130
#define AccessShareLock
Definition: lockdefs.h:36
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
double Cost
Definition: nodes.h:241
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
@ RTE_RELATION
Definition: parsenodes.h:1011
IndexPath * create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, List *indexorderbys, List *indexorderbycols, List *pathkeys, ScanDirection indexscandir, bool indexonly, Relids required_outer, double loop_count, bool partial_path)
Definition: pathnode.c:991
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:925
int32 get_relation_data_width(Oid relid, int32 *attr_widths)
Definition: plancat.c:1197
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:94
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:192
@ ForwardScanDirection
Definition: sdir.h:28
Path path
Definition: pathnodes.h:1689
Cost total_cost
Definition: pathnodes.h:1642
List * join_domains
Definition: pathnodes.h:308
int wt_param_id
Definition: pathnodes.h:521
Cardinality total_table_pages
Definition: pathnodes.h:475
Cost startup
Definition: pathnodes.h:47
List * rtable
Definition: parsenodes.h:168
CmdType commandType
Definition: parsenodes.h:121
RTEKind rtekind
Definition: parsenodes.h:1030
Cardinality tuples
Definition: pathnodes.h:930
BlockNumber pages
Definition: pathnodes.h:929
List * indexlist
Definition: pathnodes.h:925
Cardinality rows
Definition: pathnodes.h:862

References AccessShareLock, addRTEPermissionInfo(), build_simple_rel(), CMD_SELECT, Query::commandType, cost_qual_eval(), cost_sort(), create_index_path(), create_seqscan_path(), CurrentMemoryContext, enable_indexscan, ForwardScanDirection, get_relation_data_width(), PlannerInfo::glob, RelOptInfo::indexlist, IndexOptInfo::indexoid, RangeTblEntry::inFromCl, RangeTblEntry::inh, PlannerInfo::join_domains, RangeTblEntry::lateral, lfirst_node, list_make1, maintenance_work_mem, makeNode, NIL, RelOptInfo::pages, PlannerInfo::parse, IndexPath::path, QualCost::per_tuple, PlannerInfo::query_level, RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, RelOptInfo::reltarget, RelOptInfo::rows, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, setup_simple_rel_arrays(), QualCost::startup, Path::total_cost, PlannerInfo::total_table_pages, RelOptInfo::tuples, PathTarget::width, and PlannerInfo::wt_param_id.

Referenced by copy_table_data().

◆ plan_create_index_workers()

int plan_create_index_workers ( Oid  tableOid,
Oid  indexOid 
)

Definition at line 6600 of file planner.c.

6601 {
6602  PlannerInfo *root;
6603  Query *query;
6604  PlannerGlobal *glob;
6605  RangeTblEntry *rte;
6606  Relation heap;
6607  Relation index;
6608  RelOptInfo *rel;
6609  int parallel_workers;
6610  BlockNumber heap_blocks;
6611  double reltuples;
6612  double allvisfrac;
6613 
6614  /*
6615  * We don't allow performing parallel operation in standalone backend or
6616  * when parallelism is disabled.
6617  */
6619  return 0;
6620 
6621  /* Set up largely-dummy planner state */
6622  query = makeNode(Query);
6623  query->commandType = CMD_SELECT;
6624 
6625  glob = makeNode(PlannerGlobal);
6626 
6627  root = makeNode(PlannerInfo);
6628  root->parse = query;
6629  root->glob = glob;
6630  root->query_level = 1;
6631  root->planner_cxt = CurrentMemoryContext;
6632  root->wt_param_id = -1;
6634 
6635  /*
6636  * Build a minimal RTE.
6637  *
6638  * Mark the RTE with inh = true. This is a kludge to prevent
6639  * get_relation_info() from fetching index info, which is necessary
6640  * because it does not expect that any IndexOptInfo is currently
6641  * undergoing REINDEX.
6642  */
6643  rte = makeNode(RangeTblEntry);
6644  rte->rtekind = RTE_RELATION;
6645  rte->relid = tableOid;
6646  rte->relkind = RELKIND_RELATION; /* Don't be too picky. */
6648  rte->lateral = false;
6649  rte->inh = true;
6650  rte->inFromCl = true;
6651  query->rtable = list_make1(rte);
6652  addRTEPermissionInfo(&query->rteperminfos, rte);
6653 
6654  /* Set up RTE/RelOptInfo arrays */
6656 
6657  /* Build RelOptInfo */
6658  rel = build_simple_rel(root, 1, NULL);
6659 
6660  /* Rels are assumed already locked by the caller */
6661  heap = table_open(tableOid, NoLock);
6662  index = index_open(indexOid, NoLock);
6663 
6664  /*
6665  * Determine if it's safe to proceed.
6666  *
6667  * Currently, parallel workers can't access the leader's temporary tables.
6668  * Furthermore, any index predicate or index expressions must be parallel
6669  * safe.
6670  */
6671  if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
6674  {
6675  parallel_workers = 0;
6676  goto done;
6677  }
6678 
6679  /*
6680  * If parallel_workers storage parameter is set for the table, accept that
6681  * as the number of parallel worker processes to launch (though still cap
6682  * at max_parallel_maintenance_workers). Note that we deliberately do not
6683  * consider any other factor when parallel_workers is set. (e.g., memory
6684  * use by workers.)
6685  */
6686  if (rel->rel_parallel_workers != -1)
6687  {
6688  parallel_workers = Min(rel->rel_parallel_workers,
6690  goto done;
6691  }
6692 
6693  /*
6694  * Estimate heap relation size ourselves, since rel->pages cannot be
6695  * trusted (heap RTE was marked as inheritance parent)
6696  */
6697  estimate_rel_size(heap, NULL, &heap_blocks, &reltuples, &allvisfrac);
6698 
6699  /*
6700  * Determine number of workers to scan the heap relation using generic
6701  * model
6702  */
6703  parallel_workers = compute_parallel_worker(rel, heap_blocks, -1,
6705 
6706  /*
6707  * Cap workers based on available maintenance_work_mem as needed.
6708  *
6709  * Note that each tuplesort participant receives an even share of the
6710  * total maintenance_work_mem budget. Aim to leave participants
6711  * (including the leader as a participant) with no less than 32MB of
6712  * memory. This leaves cases where maintenance_work_mem is set to 64MB
6713  * immediately past the threshold of being capable of launching a single
6714  * parallel worker to sort.
6715  */
6716  while (parallel_workers > 0 &&
6717  maintenance_work_mem / (parallel_workers + 1) < 32768L)
6718  parallel_workers--;
6719 
6720 done:
6722  table_close(heap, NoLock);
6723 
6724  return parallel_workers;
6725 }
int compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, int max_workers)
Definition: allpaths.c:4210
uint32 BlockNumber
Definition: block.h:31
int max_parallel_maintenance_workers
Definition: globals.c:131
bool IsUnderPostmaster
Definition: globals.c:117
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
#define NoLock
Definition: lockdefs.h:34
void estimate_rel_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
Definition: plancat.c:1030
List * RelationGetIndexPredicate(Relation relation)
Definition: relcache.c:5127
List * RelationGetIndexExpressions(Relation relation)
Definition: relcache.c:5014
int rel_parallel_workers
Definition: pathnodes.h:937
Form_pg_class rd_rel
Definition: rel.h:111
Definition: type.h:95
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessShareLock, addRTEPermissionInfo(), build_simple_rel(), CMD_SELECT, Query::commandType, compute_parallel_worker(), CurrentMemoryContext, estimate_rel_size(), PlannerInfo::glob, index_close(), index_open(), RangeTblEntry::inFromCl, RangeTblEntry::inh, is_parallel_safe(), IsUnderPostmaster, PlannerInfo::join_domains, RangeTblEntry::lateral, list_make1, maintenance_work_mem, makeNode, max_parallel_maintenance_workers, Min, NoLock, PlannerInfo::parse, PlannerInfo::query_level, RelationData::rd_rel, RelOptInfo::rel_parallel_workers, RelationGetIndexExpressions(), RelationGetIndexPredicate(), RangeTblEntry::relid, RangeTblEntry::relkind, RangeTblEntry::rellockmode, Query::rtable, RTE_RELATION, RangeTblEntry::rtekind, setup_simple_rel_arrays(), table_close(), table_open(), and PlannerInfo::wt_param_id.

Referenced by index_build().

◆ planner()

PlannedStmt* planner ( Query parse,
const char *  query_string,
int  cursorOptions,
ParamListInfo  boundParams 
)

Definition at line 268 of file planner.c.

270 {
271  PlannedStmt *result;
272 
273  if (planner_hook)
274  result = (*planner_hook) (parse, query_string, cursorOptions, boundParams);
275  else
276  result = standard_planner(parse, query_string, cursorOptions, boundParams);
277  return result;
278 }
planner_hook_type planner_hook
Definition: planner.c:70
PlannedStmt * standard_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: planner.c:281

References parse(), planner_hook, and standard_planner().

Referenced by pg_plan_query().

◆ postprocess_setop_tlist()

static List * postprocess_setop_tlist ( List new_tlist,
List orig_tlist 
)
static

Definition at line 5490 of file planner.c.

5491 {
5492  ListCell *l;
5493  ListCell *orig_tlist_item = list_head(orig_tlist);
5494 
5495  foreach(l, new_tlist)
5496  {
5497  TargetEntry *new_tle = lfirst_node(TargetEntry, l);
5498  TargetEntry *orig_tle;
5499 
5500  /* ignore resjunk columns in setop result */
5501  if (new_tle->resjunk)
5502  continue;
5503 
5504  Assert(orig_tlist_item != NULL);
5505  orig_tle = lfirst_node(TargetEntry, orig_tlist_item);
5506  orig_tlist_item = lnext(orig_tlist, orig_tlist_item);
5507  if (orig_tle->resjunk) /* should not happen */
5508  elog(ERROR, "resjunk output columns are not implemented");
5509  Assert(new_tle->resno == orig_tle->resno);
5510  new_tle->ressortgroupref = orig_tle->ressortgroupref;
5511  }
5512  if (orig_tlist_item != NULL)
5513  elog(ERROR, "resjunk output columns are not implemented");
5514  return new_tlist;
5515 }
#define elog(elevel,...)
Definition: elog.h:224
AttrNumber resno
Definition: primnodes.h:1945
Index ressortgroupref
Definition: primnodes.h:1949

References Assert(), elog, ERROR, lfirst_node, list_head(), lnext(), TargetEntry::resno, and TargetEntry::ressortgroupref.

Referenced by grouping_planner().

◆ preprocess_expression()

static Node * preprocess_expression ( PlannerInfo root,
Node expr,
int  kind 
)
static

Definition at line 1113 of file planner.c.

1114 {
1115  /*
1116  * Fall out quickly if expression is empty. This occurs often enough to
1117  * be worth checking. Note that null->null is the correct conversion for
1118  * implicit-AND result format, too.
1119  */
1120  if (expr == NULL)
1121  return NULL;
1122 
1123  /*
1124  * If the query has any join RTEs, replace join alias variables with
1125  * base-relation variables. We must do this first, since any expressions
1126  * we may extract from the joinaliasvars lists have not been preprocessed.
1127  * For example, if we did this after sublink processing, sublinks expanded
1128  * out from join aliases would not get processed. But we can skip this in
1129  * non-lateral RTE functions, VALUES lists, and TABLESAMPLE clauses, since
1130  * they can't contain any Vars of the current query level.
1131  */
1132  if (root->hasJoinRTEs &&
1133  !(kind == EXPRKIND_RTFUNC ||
1134  kind == EXPRKIND_VALUES ||
1135  kind == EXPRKIND_TABLESAMPLE ||
1136  kind == EXPRKIND_TABLEFUNC))
1137  expr = flatten_join_alias_vars(root, root->parse, expr);
1138 
1139  /*
1140  * Simplify constant expressions. For function RTEs, this was already
1141  * done by preprocess_function_rtes. (But note we must do it again for
1142  * EXPRKIND_RTFUNC_LATERAL, because those might by now contain
1143  * un-simplified subexpressions inserted by flattening of subqueries or
1144  * join alias variables.)
1145  *
1146  * Note: an essential effect of this is to convert named-argument function
1147  * calls to positional notation and insert the current actual values of
1148  * any default arguments for functions. To ensure that happens, we *must*
1149  * process all expressions here. Previous PG versions sometimes skipped
1150  * const-simplification if it didn't seem worth the trouble, but we can't
1151  * do that anymore.
1152  *
1153  * Note: this also flattens nested AND and OR expressions into N-argument
1154  * form. All processing of a qual expression after this point must be
1155  * careful to maintain AND/OR flatness --- that is, do not generate a tree
1156  * with AND directly under AND, nor OR directly under OR.
1157  */
1158  if (kind != EXPRKIND_RTFUNC)
1159  expr = eval_const_expressions(root, expr);
1160 
1161  /*
1162  * If it's a qual or havingQual, canonicalize it.
1163  */
1164  if (kind == EXPRKIND_QUAL)
1165  {
1166  expr = (Node *) canonicalize_qual((Expr *) expr, false);
1167 
1168 #ifdef OPTIMIZER_DEBUG
1169  printf("After canonicalize_qual()\n");
1170  pprint(expr);
1171 #endif
1172  }
1173 
1174  /*
1175  * Check for ANY ScalarArrayOpExpr with Const arrays and set the
1176  * hashfuncid of any that might execute more quickly by using hash lookups
1177  * instead of a linear search.
1178  */
1179  if (kind == EXPRKIND_QUAL || kind == EXPRKIND_TARGET)
1180  {
1182  }
1183 
1184  /* Expand SubLinks to SubPlans */
1185  if (root->parse->hasSubLinks)
1186  expr = SS_process_sublinks(root, expr, (kind == EXPRKIND_QUAL));
1187 
1188  /*
1189  * XXX do not insert anything here unless you have grokked the comments in
1190  * SS_replace_correlation_vars ...
1191  */
1192 
1193  /* Replace uplevel vars with Param nodes (this IS possible in VALUES) */
1194  if (root->query_level > 1)
1195  expr = SS_replace_correlation_vars(root, expr);
1196 
1197  /*
1198  * If it's a qual or havingQual, convert it to implicit-AND format. (We
1199  * don't want to do this before eval_const_expressions, since the latter
1200  * would be unable to simplify a top-level AND correctly. Also,
1201  * SS_process_sublinks expects explicit-AND format.)
1202  */
1203  if (kind == EXPRKIND_QUAL)
1204  expr = (Node *) make_ands_implicit((Expr *) expr);
1205 
1206  return expr;
1207 }
void pprint(const void *obj)
Definition: print.c:54
void convert_saop_to_hashed_saop(Node *node)
Definition: clauses.c:2267
List * make_ands_implicit(Expr *clause)
Definition: makefuncs.c:721
#define EXPRKIND_TARGET
Definition: planner.c:78
#define EXPRKIND_TABLESAMPLE
Definition: planner.c:86
#define EXPRKIND_VALUES
Definition: planner.c:81
#define EXPRKIND_QUAL
Definition: planner.c:77
#define EXPRKIND_TABLEFUNC
Definition: planner.c:88
#define EXPRKIND_RTFUNC
Definition: planner.c:79
#define printf(...)
Definition: port.h:244
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition: prepqual.c:293
bool hasJoinRTEs
Definition: pathnodes.h:489
Node * SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
Definition: subselect.c:1881
Node * SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
Definition: subselect.c:1932
Node * flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
Definition: var.c:744

References canonicalize_qual(), convert_saop_to_hashed_saop(), eval_const_expressions(), EXPRKIND_QUAL, EXPRKIND_RTFUNC, EXPRKIND_TABLEFUNC, EXPRKIND_TABLESAMPLE, EXPRKIND_TARGET, EXPRKIND_VALUES, flatten_join_alias_vars(), PlannerInfo::hasJoinRTEs, make_ands_implicit(), PlannerInfo::parse, pprint(), printf, PlannerInfo::query_level, SS_process_sublinks(), and SS_replace_correlation_vars().

Referenced by preprocess_phv_expression(), preprocess_qual_conditions(), and subquery_planner().

◆ preprocess_grouping_sets()

static grouping_sets_data * preprocess_grouping_sets ( PlannerInfo root)
static

Definition at line 2015 of file planner.c.

2016 {
2017  Query *parse = root->parse;
2018  List *sets;
2019  int maxref = 0;
2020  ListCell *lc_set;
2022 
2023  parse->groupingSets = expand_grouping_sets(parse->groupingSets, parse->groupDistinct, -1);
2024 
2025  gd->any_hashable = false;
2026  gd->unhashable_refs = NULL;
2027  gd->unsortable_refs = NULL;
2028  gd->unsortable_sets = NIL;
2029 
2030  /*
2031  * We don't currently make any attempt to optimize the groupClause when
2032  * there are grouping sets, so just duplicate it in processed_groupClause.
2033  */
2034  root->processed_groupClause = parse->groupClause;
2035 
2036  if (parse->groupClause)
2037  {
2038  ListCell *lc;
2039 
2040  foreach(lc, parse->groupClause)
2041  {
2043  Index ref = gc->tleSortGroupRef;
2044 
2045  if (ref > maxref)
2046  maxref = ref;
2047 
2048  if (!gc->hashable)
2050 
2051  if (!OidIsValid(gc->sortop))
2053  }
2054  }
2055 
2056  /* Allocate workspace array for remapping */
2057  gd->tleref_to_colnum_map = (int *) palloc((maxref + 1) * sizeof(int));
2058 
2059  /*
2060  * If we have any unsortable sets, we must extract them before trying to
2061  * prepare rollups. Unsortable sets don't go through
2062  * reorder_grouping_sets, so we must apply the GroupingSetData annotation
2063  * here.
2064  */
2065  if (!bms_is_empty(gd->unsortable_refs))
2066  {
2067  List *sortable_sets = NIL;
2068  ListCell *lc;
2069 
2070  foreach(lc, parse->groupingSets)
2071  {
2072  List *gset = (List *) lfirst(lc);
2073 
2074  if (bms_overlap_list(gd->unsortable_refs, gset))
2075  {
2077 
2078  gs->set = gset;
2079  gd->unsortable_sets = lappend(gd->unsortable_sets, gs);
2080 
2081  /*
2082  * We must enforce here that an unsortable set is hashable;
2083  * later code assumes this. Parse analysis only checks that
2084  * every individual column is either hashable or sortable.
2085  *
2086  * Note that passing this test doesn't guarantee we can
2087  * generate a plan; there might be other showstoppers.
2088  */
2089  if (bms_overlap_list(gd->unhashable_refs, gset))
2090  ereport(ERROR,
2091  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2092  errmsg("could not implement GROUP BY"),
2093  errdetail("Some of the datatypes only support hashing, while others only support sorting.")));
2094  }
2095  else
2096  sortable_sets = lappend(sortable_sets, gset);
2097  }
2098 
2099  if (sortable_sets)
2100  sets = extract_rollup_sets(sortable_sets);
2101  else
2102  sets = NIL;
2103  }
2104  else
2105  sets = extract_rollup_sets(parse->groupingSets);
2106 
2107  foreach(lc_set, sets)
2108  {
2109  List *current_sets = (List *) lfirst(lc_set);
2110  RollupData *rollup = makeNode(RollupData);
2111  GroupingSetData *gs;
2112 
2113  /*
2114  * Reorder the current list of grouping sets into correct prefix
2115  * order. If only one aggregation pass is needed, try to make the
2116  * list match the ORDER BY clause; if more than one pass is needed, we
2117  * don't bother with that.
2118  *
2119  * Note that this reorders the sets from smallest-member-first to
2120  * largest-member-first, and applies the GroupingSetData annotations,
2121  * though the data will be filled in later.
2122  */
2123  current_sets = reorder_grouping_sets(current_sets,
2124  (list_length(sets) == 1
2125  ? parse->sortClause
2126  : NIL));
2127 
2128  /*
2129  * Get the initial (and therefore largest) grouping set.
2130  */
2131  gs = linitial_node(GroupingSetData, current_sets);
2132 
2133  /*
2134  * Order the groupClause appropriately. If the first grouping set is
2135  * empty, then the groupClause must also be empty; otherwise we have
2136  * to force the groupClause to match that grouping set's order.
2137  *
2138  * (The first grouping set can be empty even though parse->groupClause
2139  * is not empty only if all non-empty grouping sets are unsortable.
2140  * The groupClauses for hashed grouping sets are built later on.)
2141  */
2142  if (gs->set)
2143  rollup->groupClause = groupclause_apply_groupingset(root, gs->set);
2144  else
2145  rollup->groupClause = NIL;
2146 
2147  /*
2148  * Is it hashable? We pretend empty sets are hashable even though we
2149  * actually force them not to be hashed later. But don't bother if
2150  * there's nothing but empty sets (since in that case we can't hash
2151  * anything).
2152  */
2153  if (gs->set &&
2155  {
2156  rollup->hashable = true;
2157  gd->any_hashable = true;
2158  }
2159 
2160  /*
2161  * Now that we've pinned down an order for the groupClause for this
2162  * list of grouping sets, we need to remap the entries in the grouping
2163  * sets from sortgrouprefs to plain indices (0-based) into the
2164  * groupClause for this collection of grouping sets. We keep the
2165  * original form for later use, though.
2166  */
2167  rollup->gsets = remap_to_groupclause_idx(rollup->groupClause,
2168  current_sets,
2169  gd->tleref_to_colnum_map);
2170  rollup->gsets_data = current_sets;
2171 
2172  gd->rollups = lappend(gd->rollups, rollup);
2173  }
2174 
2175  if (gd->unsortable_sets)
2176  {
2177  /*
2178  * We have not yet pinned down a groupclause for this, but we will
2179  * need index-based lists for estimation purposes. Construct
2180  * hash_sets_idx based on the entire original groupclause for now.
2181  */
2182  gd->hash_sets_idx = remap_to_groupclause_idx(parse->groupClause,
2183  gd->unsortable_sets,
2184  gd->tleref_to_colnum_map);
2185  gd->any_hashable = true;
2186  }
2187 
2188  return gd;
2189 }
bool bms_overlap_list(const Bitmapset *a, const List *b)
Definition: bitmapset.c:608
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Definition: parse_agg.c:1805
static List * reorder_grouping_sets(List *groupingSets, List *sortclause)
Definition: planner.c:3048
static List * extract_rollup_sets(List *groupingSets)
Definition: planner.c:2836
Bitmapset * unhashable_refs
Definition: planner.c:101
Bitmapset * unsortable_refs
Definition: planner.c:100

References grouping_sets_data::any_hashable, bms_add_member(), bms_is_empty, bms_overlap_list(), ereport, errcode(), errdetail(), errmsg(), ERROR, expand_grouping_sets(), extract_rollup_sets(), RollupData::groupClause, groupclause_apply_groupingset(), RollupData::gsets, RollupData::gsets_data, grouping_sets_data::hash_sets_idx, RollupData::hashable, lappend(), lfirst, lfirst_node, linitial_node, list_length(), makeNode, NIL, OidIsValid, palloc(), palloc0(), parse(), PlannerInfo::parse, PlannerInfo::processed_groupClause, remap_to_groupclause_idx(), reorder_grouping_sets(), grouping_sets_data::rollups, GroupingSetData::set, SortGroupClause::sortop, grouping_sets_data::tleref_to_colnum_map, SortGroupClause::tleSortGroupRef, grouping_sets_data::unhashable_refs, grouping_sets_data::unsortable_refs, and grouping_sets_data::unsortable_sets.

Referenced by grouping_planner().

◆ preprocess_limit()

static double preprocess_limit ( PlannerInfo root,
double  tuple_fraction,
int64 *  offset_est,
int64 *  count_est 
)
static

Definition at line 2411 of file planner.c.

2413 {
2414  Query *parse = root->parse;
2415  Node *est;
2416  double limit_fraction;
2417 
2418  /* Should not be called unless LIMIT or OFFSET */
2419  Assert(parse->limitCount || parse->limitOffset);
2420 
2421  /*
2422  * Try to obtain the clause values. We use estimate_expression_value
2423  * primarily because it can sometimes do something useful with Params.
2424  */
2425  if (parse->limitCount)
2426  {
2427  est = estimate_expression_value(root, parse->limitCount);
2428  if (est && IsA(est, Const))
2429  {
2430  if (((Const *) est)->constisnull)
2431  {
2432  /* NULL indicates LIMIT ALL, ie, no limit */
2433  *count_est = 0; /* treat as not present */
2434  }
2435  else
2436  {
2437  *count_est = DatumGetInt64(((Const *) est)->constvalue);
2438  if (*count_est <= 0)
2439  *count_est = 1; /* force to at least 1 */
2440  }
2441  }
2442  else
2443  *count_est = -1; /* can't estimate */
2444  }
2445  else
2446  *count_est = 0; /* not present */
2447 
2448  if (parse->limitOffset)
2449  {
2450  est = estimate_expression_value(root, parse->limitOffset);
2451  if (est && IsA(est, Const))
2452  {
2453  if (((Const *) est)->constisnull)
2454  {
2455  /* Treat NULL as no offset; the executor will too */
2456  *offset_est = 0; /* treat as not present */
2457  }
2458  else
2459  {
2460  *offset_est = DatumGetInt64(((Const *) est)->constvalue);
2461  if (*offset_est < 0)
2462  *offset_est = 0; /* treat as not present */
2463  }
2464  }
2465  else
2466  *offset_est = -1; /* can't estimate */
2467  }
2468  else
2469  *offset_est = 0; /* not present */
2470 
2471  if (*count_est != 0)
2472  {
2473  /*
2474  * A LIMIT clause limits the absolute number of tuples returned.
2475  * However, if it's not a constant LIMIT then we have to guess; for
2476  * lack of a better idea, assume 10% of the plan's result is wanted.
2477  */
2478  if (*count_est < 0 || *offset_est < 0)
2479  {
2480  /* LIMIT or OFFSET is an expression ... punt ... */
2481  limit_fraction = 0.10;
2482  }
2483  else
2484  {
2485  /* LIMIT (plus OFFSET, if any) is max number of tuples needed */
2486  limit_fraction = (double) *count_est + (double) *offset_est;
2487  }
2488 
2489  /*
2490  * If we have absolute limits from both caller and LIMIT, use the
2491  * smaller value; likewise if they are both fractional. If one is
2492  * fractional and the other absolute, we can't easily determine which
2493  * is smaller, but we use the heuristic that the absolute will usually
2494  * be smaller.
2495  */
2496  if (tuple_fraction >= 1.0)
2497  {
2498  if (limit_fraction >= 1.0)
2499  {
2500  /* both absolute */
2501  tuple_fraction = Min(tuple_fraction, limit_fraction);
2502  }
2503  else
2504  {
2505  /* caller absolute, limit fractional; use caller's value */
2506  }
2507  }
2508  else if (tuple_fraction > 0.0)
2509  {
2510  if (limit_fraction >= 1.0)
2511  {
2512  /* caller fractional, limit absolute; use limit */
2513  tuple_fraction = limit_fraction;
2514  }
2515  else
2516  {
2517  /* both fractional */
2518  tuple_fraction = Min(tuple_fraction, limit_fraction);
2519  }
2520  }
2521  else
2522  {
2523  /* no info from caller, just use limit */
2524  tuple_fraction = limit_fraction;
2525  }
2526  }
2527  else if (*offset_est != 0 && tuple_fraction > 0.0)
2528  {
2529  /*
2530  * We have an OFFSET but no LIMIT. This acts entirely differently
2531  * from the LIMIT case: here, we need to increase rather than decrease
2532  * the caller's tuple_fraction, because the OFFSET acts to cause more
2533  * tuples to be fetched instead of fewer. This only matters if we got
2534  * a tuple_fraction > 0, however.
2535  *
2536  * As above, use 10% if OFFSET is present but unestimatable.
2537  */
2538  if (*offset_est < 0)
2539  limit_fraction = 0.10;
2540  else
2541  limit_fraction = (double) *offset_est;
2542 
2543  /*
2544  * If we have absolute counts from both caller and OFFSET, add them
2545  * together; likewise if they are both fractional. If one is
2546  * fractional and the other absolute, we want to take the larger, and
2547  * we heuristically assume that's the fractional one.
2548  */
2549  if (tuple_fraction >= 1.0)
2550  {
2551  if (limit_fraction >= 1.0)
2552  {
2553  /* both absolute, so add them together */
2554  tuple_fraction += limit_fraction;
2555  }
2556  else
2557  {
2558  /* caller absolute, limit fractional; use limit */
2559  tuple_fraction = limit_fraction;
2560  }
2561  }
2562  else
2563  {
2564  if (limit_fraction >= 1.0)
2565  {
2566  /* caller fractional, limit absolute; use caller's value */
2567  }
2568  else
2569  {
2570  /* both fractional, so add them together */
2571  tuple_fraction += limit_fraction;
2572  if (tuple_fraction >= 1.0)
2573  tuple_fraction = 0.0; /* assume fetch all */
2574  }
2575  }
2576  }
2577 
2578  return tuple_fraction;
2579 }
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2375

References Assert(), DatumGetInt64(), estimate_expression_value(), IsA, Min, parse(), and PlannerInfo::parse.

Referenced by grouping_planner().

◆ preprocess_phv_expression()

Expr* preprocess_phv_expression ( PlannerInfo root,
Expr expr 
)

Definition at line 1259 of file planner.c.

1260 {
1261  return (Expr *) preprocess_expression(root, (Node *) expr, EXPRKIND_PHV);
1262 }
#define EXPRKIND_PHV
Definition: planner.c:85
static Node * preprocess_expression(PlannerInfo *root, Node *expr, int kind)
Definition: planner.c:1113

References EXPRKIND_PHV, and preprocess_expression().

Referenced by extract_lateral_references().

◆ preprocess_qual_conditions()

static void preprocess_qual_conditions ( PlannerInfo root,
Node jtnode 
)
static

Definition at line 1215 of file planner.c.

1216 {
1217  if (jtnode == NULL)
1218  return;
1219  if (IsA(jtnode, RangeTblRef))
1220  {
1221  /* nothing to do here */
1222  }
1223  else if (IsA(jtnode, FromExpr))
1224  {
1225  FromExpr *f = (FromExpr *) jtnode;
1226  ListCell *l;
1227 
1228  foreach(l, f->fromlist)
1230 
1232  }
1233  else if (IsA(jtnode, JoinExpr))
1234  {
1235  JoinExpr *j = (JoinExpr *) jtnode;
1236 
1237  preprocess_qual_conditions(root, j->larg);
1238  preprocess_qual_conditions(root, j->rarg);
1239 
1240  j->quals = preprocess_expression(root, j->quals, EXPRKIND_QUAL);
1241  }
1242  else
1243  elog(ERROR, "unrecognized node type: %d",
1244  (int) nodeTag(jtnode));
1245 }
#define nodeTag(nodeptr)
Definition: nodes.h:133
static void preprocess_qual_conditions(PlannerInfo *root, Node *jtnode)
Definition: planner.c:1215
Node * quals
Definition: primnodes.h:2062
List * fromlist
Definition: primnodes.h:2061

References elog, ERROR, EXPRKIND_QUAL, FromExpr::fromlist, IsA, j, lfirst, nodeTag, preprocess_expression(), and FromExpr::quals.

Referenced by subquery_planner().

◆ preprocess_rowmarks()

static void preprocess_rowmarks ( PlannerInfo root)
static

Definition at line 2233 of file planner.c.

2234 {
2235  Query *parse = root->parse;
2236  Bitmapset *rels;
2237  List *prowmarks;
2238  ListCell *l;
2239  int i;
2240 
2241  if (parse->rowMarks)
2242  {
2243  /*
2244  * We've got trouble if FOR [KEY] UPDATE/SHARE appears inside
2245  * grouping, since grouping renders a reference to individual tuple
2246  * CTIDs invalid. This is also checked at parse time, but that's
2247  * insufficient because of rule substitution, query pullup, etc.
2248  */
2250  parse->rowMarks)->strength);
2251  }
2252  else
2253  {
2254  /*
2255  * We only need rowmarks for UPDATE, DELETE, MERGE, or FOR [KEY]
2256  * UPDATE/SHARE.
2257  */
2258  if (parse->commandType != CMD_UPDATE &&
2259  parse->commandType != CMD_DELETE &&
2260  parse->commandType != CMD_MERGE)
2261  return;
2262  }
2263 
2264  /*
2265  * We need to have rowmarks for all base relations except the target. We
2266  * make a bitmapset of all base rels and then remove the items we don't
2267  * need or have FOR [KEY] UPDATE/SHARE marks for.
2268  */
2269  rels = get_relids_in_jointree((Node *) parse->jointree, false, false);
2270  if (parse->resultRelation)
2271  rels = bms_del_member(rels, parse->resultRelation);
2272 
2273  /*
2274  * Convert RowMarkClauses to PlanRowMark representation.
2275  */
2276  prowmarks = NIL;
2277  foreach(l, parse->rowMarks)
2278  {
2280  RangeTblEntry *rte = rt_fetch(rc->rti, parse->rtable);
2281  PlanRowMark *newrc;
2282 
2283  /*
2284  * Currently, it is syntactically impossible to have FOR UPDATE et al
2285  * applied to an update/delete target rel. If that ever becomes
2286  * possible, we should drop the target from the PlanRowMark list.
2287  */
2288  Assert(rc->rti != parse->resultRelation);
2289 
2290  /*
2291  * Ignore RowMarkClauses for subqueries; they aren't real tables and
2292  * can't support true locking. Subqueries that got flattened into the
2293  * main query should be ignored completely. Any that didn't will get
2294  * ROW_MARK_COPY items in the next loop.
2295  */
2296  if (rte->rtekind != RTE_RELATION)
2297  continue;
2298 
2299  rels = bms_del_member(rels, rc->rti);
2300 
2301  newrc = makeNode(PlanRowMark);
2302  newrc->rti = newrc->prti = rc->rti;
2303  newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2304  newrc->markType = select_rowmark_type(rte, rc->strength);
2305  newrc->allMarkTypes = (1 << newrc->markType);
2306  newrc->strength = rc->strength;
2307  newrc->waitPolicy = rc->waitPolicy;
2308  newrc->isParent = false;
2309 
2310  prowmarks = lappend(prowmarks, newrc);
2311  }
2312 
2313  /*
2314  * Now, add rowmarks for any non-target, non-locked base relations.
2315  */
2316  i = 0;
2317  foreach(l, parse->rtable)
2318  {
2320  PlanRowMark *newrc;
2321 
2322  i++;
2323  if (!bms_is_member(i, rels))
2324  continue;
2325 
2326  newrc = makeNode(PlanRowMark);
2327  newrc->rti = newrc->prti = i;
2328  newrc->rowmarkId = ++(root->glob->lastRowMarkId);
2329  newrc->markType = select_rowmark_type(rte, LCS_NONE);
2330  newrc->allMarkTypes = (1 << newrc->markType);
2331  newrc->strength = LCS_NONE;
2332  newrc->waitPolicy = LockWaitBlock; /* doesn't matter */
2333  newrc->isParent = false;
2334 
2335  prowmarks = lappend(prowmarks, newrc);
2336  }
2337 
2338  root->rowMarks = prowmarks;
2339 }
@ LockWaitBlock
Definition: lockoptions.h:39
@ LCS_NONE
Definition: lockoptions.h:23
@ CMD_MERGE
Definition: nodes.h:259
@ CMD_DELETE
Definition: nodes.h:258
void CheckSelectLocking(Query *qry, LockClauseStrength strength)
Definition: analyze.c:3237
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
RowMarkType select_rowmark_type(RangeTblEntry *rte, LockClauseStrength strength)
Definition: planner.c:2345
Relids get_relids_in_jointree(Node *jtnode, bool include_outer_joins, bool include_inner_joins)
LockClauseStrength strength
Definition: plannodes.h:1385
Index prti
Definition: plannodes.h:1381
RowMarkType markType
Definition: plannodes.h:1383
LockWaitPolicy waitPolicy
Definition: plannodes.h:1386
bool isParent
Definition: plannodes.h:1387
Index rowmarkId
Definition: plannodes.h:1382
int allMarkTypes
Definition: plannodes.h:1384
Index lastRowMarkId
Definition: pathnodes.h:141
LockClauseStrength strength
Definition: parsenodes.h:1540
LockWaitPolicy waitPolicy
Definition: parsenodes.h:1541

References PlanRowMark::allMarkTypes, Assert(), bms_del_member(), bms_is_member(), CheckSelectLocking(), CMD_DELETE, CMD_MERGE, CMD_UPDATE, get_relids_in_jointree(), PlannerInfo::glob, i, PlanRowMark::isParent, lappend(), PlannerGlobal::lastRowMarkId, LCS_NONE, lfirst_node, linitial_node, LockWaitBlock, makeNode, PlanRowMark::markType, NIL, parse(), PlannerInfo::parse, PlanRowMark::prti, PlanRowMark::rowmarkId, PlannerInfo::rowMarks, rt_fetch, RTE_RELATION, RangeTblEntry::rtekind, RowMarkClause::rti, PlanRowMark::rti, select_rowmark_type(), RowMarkClause::strength, PlanRowMark::strength, RowMarkClause::waitPolicy, and PlanRowMark::waitPolicy.

Referenced by subquery_planner().

◆ remap_to_groupclause_idx()

static List * remap_to_groupclause_idx ( List groupClause,
List gsets,
int *  tleref_to_colnum_map 
)
static

Definition at line 2196 of file planner.c.

2199 {
2200  int ref = 0;
2201  List *result = NIL;
2202  ListCell *lc;
2203 
2204  foreach(lc, groupClause)
2205  {
2207 
2208  tleref_to_colnum_map[gc->tleSortGroupRef] = ref++;
2209  }
2210 
2211  foreach(lc, gsets)
2212  {
2213  List *set = NIL;
2214  ListCell *lc2;
2216 
2217  foreach(lc2, gs->set)
2218  {
2219  set = lappend_int(set, tleref_to_colnum_map[lfirst_int(lc2)]);
2220  }
2221 
2222  result = lappend(result, set);
2223  }
2224 
2225  return result;
2226 }

References lappend(), lappend_int(), lfirst_int, lfirst_node, NIL, GroupingSetData::set, and SortGroupClause::tleSortGroupRef.

Referenced by consider_groupingsets_paths(), and preprocess_grouping_sets().

◆ remove_useless_groupby_columns()

static void remove_useless_groupby_columns ( PlannerInfo root)
static

Definition at line 2655 of file planner.c.

2656 {
2657  Query *parse = root->parse;
2658  Bitmapset **groupbyattnos;
2659  Bitmapset **surplusvars;
2660  ListCell *lc;
2661  int relid;
2662 
2663  /* No chance to do anything if there are less than two GROUP BY items */
2664  if (list_length(root->processed_groupClause) < 2)
2665  return;
2666 
2667  /* Don't fiddle with the GROUP BY clause if the query has grouping sets */
2668  if (parse->groupingSets)
2669  return;
2670 
2671  /*
2672  * Scan the GROUP BY clause to find GROUP BY items that are simple Vars.
2673  * Fill groupbyattnos[k] with a bitmapset of the column attnos of RTE k
2674  * that are GROUP BY items.
2675  */
2676  groupbyattnos = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
2677  (list_length(parse->rtable) + 1));
2678  foreach(lc, root->processed_groupClause)
2679  {
2681  TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
2682  Var *var = (Var *) tle->expr;
2683 
2684  /*
2685  * Ignore non-Vars and Vars from other query levels.
2686  *
2687  * XXX in principle, stable expressions containing Vars could also be
2688  * removed, if all the Vars are functionally dependent on other GROUP
2689  * BY items. But it's not clear that such cases occur often enough to
2690  * be worth troubling over.
2691  */
2692  if (!IsA(var, Var) ||
2693  var->varlevelsup > 0)
2694  continue;
2695 
2696  /* OK, remember we have this Var */
2697  relid = var->varno;
2698  Assert(relid <= list_length(parse->rtable));
2699  groupbyattnos[relid] = bms_add_member(groupbyattnos[relid],
2701  }
2702 
2703  /*
2704  * Consider each relation and see if it is possible to remove some of its
2705  * Vars from GROUP BY. For simplicity and speed, we do the actual removal
2706  * in a separate pass. Here, we just fill surplusvars[k] with a bitmapset
2707  * of the column attnos of RTE k that are removable GROUP BY items.
2708  */
2709  surplusvars = NULL; /* don't allocate array unless required */
2710  relid = 0;
2711  foreach(lc, parse->rtable)
2712  {
2714  Bitmapset *relattnos;
2715  Bitmapset *pkattnos;
2716  Oid constraintOid;
2717 
2718  relid++;
2719 
2720  /* Only plain relations could have primary-key constraints */
2721  if (rte->rtekind != RTE_RELATION)
2722  continue;
2723 
2724  /*
2725  * We must skip inheritance parent tables as some of the child rels
2726  * may cause duplicate rows. This cannot happen with partitioned
2727  * tables, however.
2728  */
2729  if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
2730  continue;
2731 
2732  /* Nothing to do unless this rel has multiple Vars in GROUP BY */
2733  relattnos = groupbyattnos[relid];
2734  if (bms_membership(relattnos) != BMS_MULTIPLE)
2735  continue;
2736 
2737  /*
2738  * Can't remove any columns for this rel if there is no suitable
2739  * (i.e., nondeferrable) primary key constraint.
2740  */
2741  pkattnos = get_primary_key_attnos(rte->relid, false, &constraintOid);
2742  if (pkattnos == NULL)
2743  continue;
2744 
2745  /*
2746  * If the primary key is a proper subset of relattnos then we have
2747  * some items in the GROUP BY that can be removed.
2748  */
2749  if (bms_subset_compare(pkattnos, relattnos) == BMS_SUBSET1)
2750  {
2751  /*
2752  * To easily remember whether we've found anything to do, we don't
2753  * allocate the surplusvars[] array until we find something.
2754  */
2755  if (surplusvars == NULL)
2756  surplusvars = (Bitmapset **) palloc0(sizeof(Bitmapset *) *
2757  (list_length(parse->rtable) + 1));
2758 
2759  /* Remember the attnos of the removable columns */
2760  surplusvars[relid] = bms_difference(relattnos, pkattnos);
2761  }
2762  }
2763 
2764  /*
2765  * If we found any surplus Vars, build a new GROUP BY clause without them.
2766  * (Note: this may leave some TLEs with unreferenced ressortgroupref
2767  * markings, but that's harmless.)
2768  */
2769  if (surplusvars != NULL)
2770  {
2771  List *new_groupby = NIL;
2772 
2773  foreach(lc, root->processed_groupClause)
2774  {
2776  TargetEntry *tle = get_sortgroupclause_tle(sgc, parse->targetList);
2777  Var *var = (Var *) tle->expr;
2778 
2779  /*
2780  * New list must include non-Vars, outer Vars, and anything not
2781  * marked as surplus.
2782  */
2783  if (!IsA(var, Var) ||
2784  var->varlevelsup > 0 ||
2786  surplusvars[var->varno]))
2787  new_groupby = lappend(new_groupby, sgc);
2788  }
2789 
2790  root->processed_groupClause = new_groupby;
2791  }
2792 }
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:445
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:346
@ BMS_SUBSET1
Definition: bitmapset.h:63
Bitmapset * get_primary_key_attnos(Oid relid, bool deferrableOk, Oid *constraintOid)
Expr * expr
Definition: primnodes.h:1943
Definition: primnodes.h:234
AttrNumber varattno
Definition: primnodes.h:246
int varno
Definition: primnodes.h:241
Index varlevelsup
Definition: primnodes.h:266
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:367

References Assert(), bms_add_member(), bms_difference(), bms_is_member(), bms_membership(), BMS_MULTIPLE, BMS_SUBSET1, bms_subset_compare(), TargetEntry::expr, FirstLowInvalidHeapAttributeNumber, get_primary_key_attnos(), get_sortgroupclause_tle(), if(), RangeTblEntry::inh, IsA, lappend(), lfirst_node, list_length(), NIL, palloc0(), parse(), PlannerInfo::parse, PlannerInfo::processed_groupClause, RangeTblEntry::relid, RangeTblEntry::relkind, RTE_RELATION, RangeTblEntry::rtekind, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by grouping_planner().

◆ reorder_grouping_sets()

static List * reorder_grouping_sets ( List groupingSets,
List sortclause 
)
static

Definition at line 3048 of file planner.c.

3049 {
3050  ListCell *lc;
3051  List *previous = NIL;
3052  List *result = NIL;
3053 
3054  foreach(lc, groupingSets)
3055  {
3056  List *candidate = (List *) lfirst(lc);
3057  List *new_elems = list_difference_int(candidate, previous);
3059 
3060  while (list_length(sortclause) > list_length(previous) &&
3061  new_elems != NIL)
3062  {
3063  SortGroupClause *sc = list_nth(sortclause, list_length(previous));
3064  int ref = sc->tleSortGroupRef;
3065 
3066  if (list_member_int(new_elems, ref))
3067  {
3068  previous = lappend_int(previous, ref);
3069  new_elems = list_delete_int(new_elems, ref);
3070  }
3071  else
3072  {
3073  /* diverged from the sortclause; give up on it */
3074  sortclause = NIL;
3075  break;
3076  }
3077  }
3078 
3079  previous = list_concat(previous, new_elems);
3080 
3081  gs->set = list_copy(previous);
3082  result = lcons(gs, result);
3083  }
3084 
3085  list_free(previous);
3086 
3087  return result;
3088 }
List * list_delete_int(List *list, int datum)
Definition: list.c:891
List * list_difference_int(const List *list1, const List *list2)
Definition: list.c:1288
bool list_member_int(const List *list, int datum)
Definition: list.c:702
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299

References lappend_int(), lcons(), lfirst, list_concat(), list_copy(), list_delete_int(), list_difference_int(), list_free(), list_length(), list_member_int(), list_nth(), makeNode, NIL, GroupingSetData::set, and SortGroupClause::tleSortGroupRef.

Referenced by preprocess_grouping_sets().

◆ select_active_windows()

static List * select_active_windows ( PlannerInfo root,
WindowFuncLists wflists 
)
static

Definition at line 5667 of file planner.c.

5668 {
5669  List *windowClause = root->parse->windowClause;
5670  List *result = NIL;
5671  ListCell *lc;
5672  int nActive = 0;
5674  * list_length(windowClause));
5675 
5676  /* First, construct an array of the active windows */
5677  foreach(lc, windowClause)
5678  {
5680 
5681  /* It's only active if wflists shows some related WindowFuncs */
5682  Assert(wc->winref <= wflists->maxWinRef);
5683  if (wflists->windowFuncs[wc->winref] == NIL)
5684  continue;
5685 
5686  actives[nActive].wc = wc; /* original clause */
5687 
5688  /*
5689  * For sorting, we want the list of partition keys followed by the
5690  * list of sort keys. But pathkeys construction will remove duplicates
5691  * between the two, so we can as well (even though we can't detect all
5692  * of the duplicates, since some may come from ECs - that might mean
5693  * we miss optimization chances here). We must, however, ensure that
5694  * the order of entries is preserved with respect to the ones we do
5695  * keep.
5696  *
5697  * partitionClause and orderClause had their own duplicates removed in
5698  * parse analysis, so we're only concerned here with removing
5699  * orderClause entries that also appear in partitionClause.
5700  */
5701  actives[nActive].uniqueOrder =
5703  wc->orderClause);
5704  nActive++;
5705  }
5706 
5707  /*
5708  * Sort active windows by their partitioning/ordering clauses, ignoring
5709  * any framing clauses, so that the windows that need the same sorting are
5710  * adjacent in the list. When we come to generate paths, this will avoid
5711  * inserting additional Sort nodes.
5712  *
5713  * This is how we implement a specific requirement from the SQL standard,
5714  * which says that when two or more windows are order-equivalent (i.e.
5715  * have matching partition and order clauses, even if their names or
5716  * framing clauses differ), then all peer rows must be presented in the
5717  * same order in all of them. If we allowed multiple sort nodes for such
5718  * cases, we'd risk having the peer rows end up in different orders in
5719  * equivalent windows due to sort instability. (See General Rule 4 of
5720  * <window clause> in SQL2008 - SQL2016.)
5721  *
5722  * Additionally, if the entire list of clauses of one window is a prefix
5723  * of another, put first the window with stronger sorting requirements.
5724  * This way we will first sort for stronger window, and won't have to sort
5725  * again for the weaker one.
5726  */
5727  qsort(actives, nActive, sizeof(WindowClauseSortData), common_prefix_cmp);
5728 
5729  /* build ordered list of the original WindowClause nodes */
5730  for (int i = 0; i < nActive; i++)
5731  result = lappend(result, actives[i].wc);
5732 
5733  pfree(actives);
5734 
5735  return result;
5736 }
List * list_concat_unique(List *list1, const List *list2)
Definition: list.c:1405
static int common_prefix_cmp(const void *a, const void *b)
Definition: planner.c:5755
#define qsort(a, b, c, d)
Definition: port.h:449
WindowClause * wc
Definition: planner.c:112

References Assert(), common_prefix_cmp(), i, lappend(), lfirst_node, list_concat_unique(), list_copy(), list_length(), WindowFuncLists::maxWinRef, NIL, WindowClause::orderClause, palloc(), PlannerInfo::parse, WindowClause::partitionClause, pfree(), qsort, WindowClauseSortData::uniqueOrder, WindowClauseSortData::wc, Query::windowClause, WindowFuncLists::windowFuncs, and WindowClause::winref.

Referenced by grouping_planner().

◆ select_rowmark_type()

RowMarkType select_rowmark_type ( RangeTblEntry rte,
LockClauseStrength  strength 
)

Definition at line 2345 of file planner.c.

2346 {
2347  if (rte->rtekind != RTE_RELATION)
2348  {
2349  /* If it's not a table at all, use ROW_MARK_COPY */
2350  return ROW_MARK_COPY;
2351  }
2352  else if (rte->relkind == RELKIND_FOREIGN_TABLE)
2353  {
2354  /* Let the FDW select the rowmark type, if it wants to */
2355  FdwRoutine *fdwroutine = GetFdwRoutineByRelId(rte->relid);
2356 
2357  if (fdwroutine->GetForeignRowMarkType != NULL)
2358  return fdwroutine->GetForeignRowMarkType(rte, strength);
2359  /* Otherwise, use ROW_MARK_COPY by default */
2360  return ROW_MARK_COPY;
2361  }
2362  else
2363  {
2364  /* Regular table, apply the appropriate lock type */
2365  switch (strength)
2366  {
2367  case LCS_NONE:
2368 
2369  /*
2370  * We don't need a tuple lock, only the ability to re-fetch
2371  * the row.
2372  */
2373  return ROW_MARK_REFERENCE;
2374  break;
2375  case LCS_FORKEYSHARE:
2376  return ROW_MARK_KEYSHARE;
2377  break;
2378  case LCS_FORSHARE:
2379  return ROW_MARK_SHARE;
2380  break;
2381  case LCS_FORNOKEYUPDATE:
2382  return ROW_MARK_NOKEYEXCLUSIVE;
2383  break;
2384  case LCS_FORUPDATE:
2385  return ROW_MARK_EXCLUSIVE;
2386  break;
2387  }
2388  elog(ERROR, "unrecognized LockClauseStrength %d", (int) strength);
2389  return ROW_MARK_EXCLUSIVE; /* keep compiler quiet */
2390  }
2391 }
FdwRoutine * GetFdwRoutineByRelId(Oid relid)
Definition: foreign.c:409
@ LCS_FORUPDATE
Definition: lockoptions.h:27
@ LCS_FORSHARE
Definition: lockoptions.h:25
@ LCS_FORKEYSHARE
Definition: lockoptions.h:24
@ LCS_FORNOKEYUPDATE
Definition: lockoptions.h:26
@ ROW_MARK_COPY
Definition: plannodes.h:1332
@ ROW_MARK_REFERENCE
Definition: plannodes.h:1331
@ ROW_MARK_SHARE
Definition: plannodes.h:1329
@ ROW_MARK_EXCLUSIVE
Definition: plannodes.h:1327
@ ROW_MARK_NOKEYEXCLUSIVE
Definition: plannodes.h:1328
@ ROW_MARK_KEYSHARE
Definition: plannodes.h:1330
GetForeignRowMarkType_function GetForeignRowMarkType
Definition: fdwapi.h:247

References elog, ERROR, GetFdwRoutineByRelId(), FdwRoutine::GetForeignRowMarkType, LCS_FORKEYSHARE, LCS_FORNOKEYUPDATE, LCS_FORSHARE, LCS_FORUPDATE, LCS_NONE, RangeTblEntry::relid, RangeTblEntry::relkind, ROW_MARK_COPY, ROW_MARK_EXCLUSIVE, ROW_MARK_KEYSHARE, ROW_MARK_NOKEYEXCLUSIVE, ROW_MARK_REFERENCE, ROW_MARK_SHARE, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by expand_single_inheritance_child(), and preprocess_rowmarks().

◆ standard_planner()

PlannedStmt* standard_planner ( Query parse,
const char *  query_string,
int  cursorOptions,
ParamListInfo  boundParams 
)

Definition at line 281 of file planner.c.

283 {
284  PlannedStmt *result;
285  PlannerGlobal *glob;
286  double tuple_fraction;
287  PlannerInfo *root;
288  RelOptInfo *final_rel;
289  Path *best_path;
290  Plan *top_plan;
291  ListCell *lp,
292  *lr;
293 
294  /*
295  * Set up global state for this planner invocation. This data is needed
296  * across all levels of sub-Query that might exist in the given command,
297  * so we keep it in a separate struct that's linked to by each per-Query
298  * PlannerInfo.
299  */
300  glob = makeNode(PlannerGlobal);
301 
302  glob->boundParams = boundParams;
303  glob->subplans = NIL;
304  glob->subroots = NIL;
305  glob->rewindPlanIDs = NULL;
306  glob->finalrtable = NIL;
307  glob->finalrteperminfos = NIL;
308  glob->finalrowmarks = NIL;
309  glob->resultRelations = NIL;
310  glob->appendRelations = NIL;
311  glob->relationOids = NIL;
312  glob->invalItems = NIL;
313  glob->paramExecTypes = NIL;
314  glob->lastPHId = 0;
315  glob->lastRowMarkId = 0;
316  glob->lastPlanNodeId = 0;
317  glob->transientPlan = false;
318  glob->dependsOnRole = false;
319 
320  /*
321  * Assess whether it's feasible to use parallel mode for this query. We
322  * can't do this in a standalone backend, or if the command will try to
323  * modify any data, or if this is a cursor operation, or if GUCs are set
324  * to values that don't permit parallelism, or if parallel-unsafe
325  * functions are present in the query tree.
326  *
327  * (Note that we do allow CREATE TABLE AS, SELECT INTO, and CREATE
328  * MATERIALIZED VIEW to use parallel plans, but this is safe only because
329  * the command is writing into a completely new table which workers won't
330  * be able to see. If the workers could see the table, the fact that
331  * group locking would cause them to ignore the leader's heavyweight GIN
332  * page locks would make this unsafe. We'll have to fix that somehow if
333  * we want to allow parallel inserts in general; updates and deletes have
334  * additional problems especially around combo CIDs.)
335  *
336  * For now, we don't try to use parallel mode if we're running inside a
337  * parallel worker. We might eventually be able to relax this
338  * restriction, but for now it seems best not to have parallel workers
339  * trying to create their own parallel workers.
340  */
341  if ((cursorOptions & CURSOR_OPT_PARALLEL_OK) != 0 &&
343  parse->commandType == CMD_SELECT &&
344  !parse->hasModifyingCTE &&
346  !IsParallelWorker())
347  {
348  /* all the cheap tests pass, so scan the query tree */
350  glob->parallelModeOK = (glob->maxParallelHazard != PROPARALLEL_UNSAFE);
351  }
352  else
353  {
354  /* skip the query tree scan, just assume it's unsafe */
355  glob->maxParallelHazard = PROPARALLEL_UNSAFE;
356  glob->parallelModeOK = false;
357  }
358 
359  /*
360  * glob->parallelModeNeeded is normally set to false here and changed to
361  * true during plan creation if a Gather or Gather Merge plan is actually
362  * created (cf. create_gather_plan, create_gather_merge_plan).
363  *
364  * However, if debug_parallel_query = on or debug_parallel_query =
365  * regress, then we impose parallel mode whenever it's safe to do so, even
366  * if the final plan doesn't use parallelism. It's not safe to do so if
367  * the query contains anything parallel-unsafe; parallelModeOK will be
368  * false in that case. Note that parallelModeOK can't change after this
369  * point. Otherwise, everything in the query is either parallel-safe or
370  * parallel-restricted, and in either case it should be OK to impose
371  * parallel-mode restrictions. If that ends up breaking something, then
372  * either some function the user included in the query is incorrectly
373  * labeled as parallel-safe or parallel-restricted when in reality it's
374  * parallel-unsafe, or else the query planner itself has a bug.
375  */
376  glob->parallelModeNeeded = glob->parallelModeOK &&
378 
379  /* Determine what fraction of the plan is likely to be scanned */
380  if (cursorOptions & CURSOR_OPT_FAST_PLAN)
381  {
382  /*
383  * We have no real idea how many tuples the user will ultimately FETCH
384  * from a cursor, but it is often the case that he doesn't want 'em
385  * all, or would prefer a fast-start plan anyway so that he can
386  * process some of the tuples sooner. Use a GUC parameter to decide
387  * what fraction to optimize for.
388  */
389  tuple_fraction = cursor_tuple_fraction;
390 
391  /*
392  * We document cursor_tuple_fraction as simply being a fraction, which
393  * means the edge cases 0 and 1 have to be treated specially here. We
394  * convert 1 to 0 ("all the tuples") and 0 to a very small fraction.
395  */
396  if (tuple_fraction >= 1.0)
397  tuple_fraction = 0.0;
398  else if (tuple_fraction <= 0.0)
399  tuple_fraction = 1e-10;
400  }
401  else
402  {
403  /* Default assumption is we need all the tuples */
404  tuple_fraction = 0.0;
405  }
406 
407  /* primary planning entry point (may recurse for subqueries) */
408  root = subquery_planner(glob, parse, NULL,
409  false, tuple_fraction);
410 
411  /* Select best Path and turn it into a Plan */
412  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
413  best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
414 
415  top_plan = create_plan(root, best_path);
416 
417  /*
418  * If creating a plan for a scrollable cursor, make sure it can run
419  * backwards on demand. Add a Material node at the top at need.
420  */
421  if (cursorOptions & CURSOR_OPT_SCROLL)
422  {
423  if (!ExecSupportsBackwardScan(top_plan))
424  top_plan = materialize_finished_plan(top_plan);
425  }
426 
427  /*
428  * Optionally add a Gather node for testing purposes, provided this is
429  * actually a safe thing to do.
430  *
431  * We can add Gather even when top_plan has parallel-safe initPlans, but
432  * then we have to move the initPlans to the Gather node because of
433  * SS_finalize_plan's limitations. That would cause cosmetic breakage of
434  * regression tests when debug_parallel_query = regress, because initPlans
435  * that would normally appear on the top_plan move to the Gather, causing
436  * them to disappear from EXPLAIN output. That doesn't seem worth kluging
437  * EXPLAIN to hide, so skip it when debug_parallel_query = regress.
438  */
440  top_plan->parallel_safe &&
441  (top_plan->initPlan == NIL ||
443  {
444  Gather *gather = makeNode(Gather);
445  Cost initplan_cost;
446  bool unsafe_initplans;
447 
448  gather->plan.targetlist = top_plan->targetlist;
449  gather->plan.qual = NIL;
450  gather->plan.lefttree = top_plan;
451  gather->plan.righttree = NULL;
452  gather->num_workers = 1;
453  gather->single_copy = true;
455 
456  /* Transfer any initPlans to the new top node */
457  gather->plan.initPlan = top_plan->initPlan;
458  top_plan->initPlan = NIL;
459 
460  /*
461  * Since this Gather has no parallel-aware descendants to signal to,
462  * we don't need a rescan Param.
463  */
464  gather->rescan_param = -1;
465 
466  /*
467  * Ideally we'd use cost_gather here, but setting up dummy path data
468  * to satisfy it doesn't seem much cleaner than knowing what it does.
469  */
470  gather->plan.startup_cost = top_plan->startup_cost +
472  gather->plan.total_cost = top_plan->total_cost +
474  gather->plan.plan_rows = top_plan->plan_rows;
475  gather->plan.plan_width = top_plan->plan_width;
476  gather->plan.parallel_aware = false;
477  gather->plan.parallel_safe = false;
478 
479  /*
480  * Delete the initplans' cost from top_plan. We needn't add it to the
481  * Gather node, since the above coding already included it there.
482  */
484  &initplan_cost, &unsafe_initplans);
485  top_plan->startup_cost -= initplan_cost;
486  top_plan->total_cost -= initplan_cost;
487 
488  /* use parallel mode for parallel plans. */
489  root->glob->parallelModeNeeded = true;
490 
491  top_plan = &gather->plan;
492  }
493 
494  /*
495  * If any Params were generated, run through the plan tree and compute
496  * each plan node's extParam/allParam sets. Ideally we'd merge this into
497  * set_plan_references' tree traversal, but for now it has to be separate
498  * because we need to visit subplans before not after main plan.
499  */
500  if (glob->paramExecTypes != NIL)
501  {
502  Assert(list_length(glob->subplans) == list_length(glob->subroots));
503  forboth(lp, glob->subplans, lr, glob->subroots)
504  {
505  Plan *subplan = (Plan *) lfirst(lp);
506  PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
507 
508  SS_finalize_plan(subroot, subplan);
509  }
510  SS_finalize_plan(root, top_plan);
511  }
512 
513  /* final cleanup of the plan */
514  Assert(glob->finalrtable == NIL);
515  Assert(glob->finalrteperminfos == NIL);
516  Assert(glob->finalrowmarks == NIL);
517  Assert(glob->resultRelations == NIL);
518  Assert(glob->appendRelations == NIL);
519  top_plan = set_plan_references(root, top_plan);
520  /* ... and the subplans (both regular subplans and initplans) */
521  Assert(list_length(glob->subplans) == list_length(glob->subroots));
522  forboth(lp, glob->subplans, lr, glob->subroots)
523  {
524  Plan *subplan = (Plan *) lfirst(lp);
525  PlannerInfo *subroot = lfirst_node(PlannerInfo, lr);
526 
527  lfirst(lp) = set_plan_references(subroot, subplan);
528  }
529 
530  /* build the PlannedStmt result */
531  result = makeNode(PlannedStmt);
532 
533  result->commandType = parse->commandType;
534  result->queryId = parse->queryId;
535  result->hasReturning = (parse->returningList != NIL);
536  result->hasModifyingCTE = parse->hasModifyingCTE;
537  result->canSetTag = parse->canSetTag;
538  result->transientPlan = glob->transientPlan;
539  result->dependsOnRole = glob->dependsOnRole;
540  result->parallelModeNeeded = glob->parallelModeNeeded;
541  result->planTree = top_plan;
542  result->rtable = glob->finalrtable;
543  result->permInfos = glob->finalrteperminfos;
544  result->resultRelations = glob->resultRelations;
545  result->appendRelations = glob->appendRelations;
546  result->subplans = glob->subplans;
547  result->rewindPlanIDs = glob->rewindPlanIDs;
548  result->rowMarks = glob->finalrowmarks;
549  result->relationOids = glob->relationOids;
550  result->invalItems = glob->invalItems;
551  result->paramExecTypes = glob->paramExecTypes;
552  /* utilityStmt should be null, but we might as well copy it */
553  result->utilityStmt = parse->utilityStmt;
554  result->stmt_location = parse->stmt_location;
555  result->stmt_len = parse->stmt_len;
556 
557  result->jitFlags = PGJIT_NONE;
558  if (jit_enabled && jit_above_cost >= 0 &&
559  top_plan->total_cost > jit_above_cost)
560  {
561  result->jitFlags |= PGJIT_PERFORM;
562 
563  /*
564  * Decide how much effort should be put into generating better code.
565  */
566  if (jit_optimize_above_cost >= 0 &&
568  result->jitFlags |= PGJIT_OPT3;
569  if (jit_inline_above_cost >= 0 &&
570  top_plan->total_cost > jit_inline_above_cost)
571  result->jitFlags |= PGJIT_INLINE;
572 
573  /*
574  * Decide which operations should be JITed.
575  */
576  if (jit_expressions)
577  result->jitFlags |= PGJIT_EXPR;
579  result->jitFlags |= PGJIT_DEFORM;
580  }
581 
582  if (glob->partition_directory != NULL)
583  DestroyPartitionDirectory(glob->partition_directory);
584 
585  return result;
586 }
char max_parallel_hazard(Query *parse)
Definition: clauses.c:714
int max_parallel_workers_per_gather
Definition: costsize.c:132
double parallel_setup_cost
Definition: costsize.c:125
double parallel_tuple_cost
Definition: costsize.c:124
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:335
Plan * materialize_finished_plan(Plan *subplan)
Definition: createplan.c:6508
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:510
#define IsParallelWorker()
Definition: parallel.h:60
double jit_optimize_above_cost
Definition: jit.c:41
bool jit_enabled
Definition: jit.c:32
bool jit_expressions
Definition: jit.c:36
bool jit_tuple_deforming
Definition: jit.c:38
double jit_above_cost
Definition: jit.c:39
double jit_inline_above_cost
Definition: jit.c:40
#define PGJIT_OPT3
Definition: jit.h:21
#define PGJIT_NONE
Definition: jit.h:19
#define PGJIT_EXPR
Definition: jit.h:23
#define PGJIT_DEFORM
Definition: jit.h:24
#define PGJIT_INLINE
Definition: jit.h:22
#define PGJIT_PERFORM
Definition: jit.h:20
@ DEBUG_PARALLEL_REGRESS
Definition: optimizer.h:108
@ DEBUG_PARALLEL_OFF
Definition: optimizer.h:106
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:3135
#define CURSOR_OPT_FAST_PLAN
Definition: parsenodes.h:3141
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3144
void DestroyPartitionDirectory(PartitionDirectory pdir)
Definition: partdesc.c:442
double cursor_tuple_fraction
Definition: planner.c:65
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:618
int debug_parallel_query
Definition: planner.c:66
Path * get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
Definition: planner.c:6241
e
Definition: preproc-init.c:82
Plan * set_plan_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:287
int num_workers
Definition: plannodes.h:1141
bool invisible
Definition: plannodes.h:1144
bool single_copy
Definition: plannodes.h:1143
Plan plan
Definition: plannodes.h:1140
int rescan_param
Definition: plannodes.h:1142
struct Plan * lefttree
Definition: plannodes.h:154
Cost total_cost
Definition: plannodes.h:129
struct Plan * righttree
Definition: plannodes.h:155
bool parallel_aware
Definition: plannodes.h:140
Cost startup_cost
Definition: plannodes.h:128
List * qual
Definition: plannodes.h:153
int plan_width
Definition: plannodes.h:135
bool parallel_safe
Definition: plannodes.h:141
Cardinality plan_rows
Definition: plannodes.h:134
List * targetlist
Definition: plannodes.h:152
List * initPlan
Definition: plannodes.h:156
struct Plan * planTree
Definition: plannodes.h:70
bool hasModifyingCTE
Definition: plannodes.h:58
List * appendRelations
Definition: plannodes.h:80
List * permInfos
Definition: plannodes.h:74
bool canSetTag
Definition: plannodes.h:60
List * rowMarks
Definition: plannodes.h:87
int stmt_location
Definition: plannodes.h:98
int stmt_len
Definition: plannodes.h:99
int jitFlags
Definition: plannodes.h:68
Bitmapset * rewindPlanIDs
Definition: plannodes.h:85
bool hasReturning
Definition: plannodes.h:56
List * invalItems
Definition: plannodes.h:91
bool transientPlan
Definition: plannodes.h:62
List * resultRelations
Definition: plannodes.h:78
List * subplans
Definition: plannodes.h:82
List * relationOids
Definition: plannodes.h:89
bool dependsOnRole
Definition: plannodes.h:64
CmdType commandType
Definition: plannodes.h:52
Node * utilityStmt
Definition: plannodes.h:95
List * rtable
Definition: plannodes.h:72
List * paramExecTypes
Definition: plannodes.h:93
bool parallelModeNeeded
Definition: plannodes.h:66
uint64 queryId
Definition: plannodes.h:54
int lastPlanNodeId
Definition: pathnodes.h:144
char maxParallelHazard
Definition: pathnodes.h:159
List * subplans
Definition: pathnodes.h:105
bool dependsOnRole
Definition: pathnodes.h:150
List * appendRelations
Definition: pathnodes.h:126
List * finalrowmarks
Definition: pathnodes.h:120
List * paramExecTypes
Definition: pathnodes.h:135
bool parallelModeOK
Definition: pathnodes.h:153
bool transientPlan
Definition: pathnodes.h:147
Bitmapset * rewindPlanIDs
Definition: pathnodes.h:111
List * finalrteperminfos
Definition: pathnodes.h:117
Index lastPHId
Definition: pathnodes.h:138
List * resultRelations
Definition: pathnodes.h:123
List * finalrtable
Definition: pathnodes.h:114
bool parallelModeNeeded
Definition: pathnodes.h:156
void SS_finalize_plan(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2267
void SS_compute_initplan_cost(List *init_plans, Cost *initplan_cost_p, bool *unsafe_initplans_p)
Definition: subselect.c:2211

References PlannerGlobal::appendRelations, PlannedStmt::appendRelations, Assert(), PlannedStmt::canSetTag, CMD_SELECT, PlannedStmt::commandType, create_plan(), CURSOR_OPT_FAST_PLAN, CURSOR_OPT_PARALLEL_OK, CURSOR_OPT_SCROLL, cursor_tuple_fraction, DEBUG_PARALLEL_OFF, debug_parallel_query, DEBUG_PARALLEL_REGRESS, PlannerGlobal::dependsOnRole, PlannedStmt::dependsOnRole, DestroyPartitionDirectory(), ExecSupportsBackwardScan(), fetch_upper_rel(), PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, PlannerGlobal::finalrteperminfos, forboth, get_cheapest_fractional_path(), PlannerInfo::glob, PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, Plan::initPlan, PlannerGlobal::invalItems, PlannedStmt::invalItems, Gather::invisible, IsParallelWorker, IsUnderPostmaster, jit_above_cost, jit_enabled, jit_expressions, jit_inline_above_cost, jit_optimize_above_cost, jit_tuple_deforming, PlannedStmt::jitFlags, PlannerGlobal::lastPHId, PlannerGlobal::lastPlanNodeId, PlannerGlobal::lastRowMarkId, Plan::lefttree, lfirst, lfirst_node, list_length(), makeNode, materialize_finished_plan(), max_parallel_hazard(), max_parallel_workers_per_gather, PlannerGlobal::maxParallelHazard, NIL, Gather::num_workers, Plan::parallel_aware, Plan::parallel_safe, parallel_setup_cost, parallel_tuple_cost, PlannerGlobal::parallelModeNeeded, PlannedStmt::parallelModeNeeded, PlannerGlobal::parallelModeOK, PlannerGlobal::paramExecTypes, PlannedStmt::paramExecTypes, parse(), PlannedStmt::permInfos, PGJIT_DEFORM, PGJIT_EXPR, PGJIT_INLINE, PGJIT_NONE, PGJIT_OPT3, PGJIT_PERFORM, Gather::plan, Plan::plan_rows, Plan::plan_width, PlannedStmt::planTree, Plan::qual, PlannedStmt::queryId, PlannerGlobal::relationOids, PlannedStmt::relationOids, Gather::rescan_param, PlannerGlobal::resultRelations, PlannedStmt::resultRelations, PlannerGlobal::rewindPlanIDs, PlannedStmt::rewindPlanIDs, Plan::righttree, PlannedStmt::rowMarks, PlannedStmt::rtable, set_plan_references(), Gather::single_copy, SS_compute_initplan_cost(), SS_finalize_plan(), Plan::startup_cost, PlannedStmt::stmt_len, PlannedStmt::stmt_location, PlannerGlobal::subplans, PlannedStmt::subplans, subquery_planner(), Plan::targetlist, Plan::total_cost, PlannerGlobal::transientPlan, PlannedStmt::transientPlan, UPPERREL_FINAL, and PlannedStmt::utilityStmt.

Referenced by delay_execution_planner(), pgss_planner(), and planner().

◆ standard_qp_callback()

static void standard_qp_callback ( PlannerInfo root,
void *  extra 
)
static

Definition at line 3322 of file planner.c.

3323 {
3324  Query *parse = root->parse;
3325  standard_qp_extra *qp_extra = (standard_qp_extra *) extra;
3326  List *tlist = root->processed_tlist;
3327  List *activeWindows = qp_extra->activeWindows;
3328 
3329  /*
3330  * Calculate pathkeys that represent grouping/ordering and/or ordered
3331  * aggregate requirements.
3332  */
3333  if (qp_extra->gset_data)
3334  {
3335  /*
3336  * With grouping sets, just use the first RollupData's groupClause. We
3337  * don't make any effort to optimize grouping clauses when there are
3338  * grouping sets, nor can we combine aggregate ordering keys with
3339  * grouping.
3340  */
3341  List *rollups = qp_extra->gset_data->rollups;
3342  List *groupClause = (rollups ? linitial_node(RollupData, rollups)->groupClause : NIL);
3343 
3344  if (grouping_is_sortable(groupClause))
3345  {
3347  groupClause,
3348  tlist);
3350  }
3351  else
3352  {
3353  root->group_pathkeys = NIL;
3354  root->num_groupby_pathkeys = 0;
3355  }
3356  }
3357  else if (parse->groupClause || root->numOrderedAggs > 0)
3358  {
3359  /*
3360  * With a plain GROUP BY list, we can remove any grouping items that
3361  * are proven redundant by EquivalenceClass processing. For example,
3362  * we can remove y given "WHERE x = y GROUP BY x, y". These aren't
3363  * especially common cases, but they're nearly free to detect. Note
3364  * that we remove redundant items from processed_groupClause but not
3365  * the original parse->groupClause.
3366  */
3367  bool sortable;
3368 
3369  root->group_pathkeys =
3371  &root->processed_groupClause,
3372  tlist,
3373  true,
3374  &sortable);
3375  if (!sortable)
3376  {
3377  /* Can't sort; no point in considering aggregate ordering either */
3378  root->group_pathkeys = NIL;
3379  root->num_groupby_pathkeys = 0;
3380  }
3381  else
3382  {
3384  /* If we have ordered aggs, consider adding onto group_pathkeys */
3385  if (root->numOrderedAggs > 0)
3387  }
3388  }
3389  else
3390  {
3391  root->group_pathkeys = NIL;
3392  root->num_groupby_pathkeys = 0;
3393  }
3394 
3395  /* We consider only the first (bottom) window in pathkeys logic */
3396  if (activeWindows != NIL)
3397  {
3398  WindowClause *wc = linitial_node(WindowClause, activeWindows);
3399 
3401  wc,
3402  tlist);
3403  }
3404  else
3405  root->window_pathkeys = NIL;
3406 
3407  /*
3408  * As with GROUP BY, we can discard any DISTINCT items that are proven
3409  * redundant by EquivalenceClass processing. The non-redundant list is
3410  * kept in root->processed_distinctClause, leaving the original
3411  * parse->distinctClause alone.
3412  */
3413  if (parse->distinctClause)
3414  {
3415  bool sortable;
3416 
3417  /* Make a copy since pathkey processing can modify the list */
3418  root->processed_distinctClause = list_copy(parse->distinctClause);
3419  root->distinct_pathkeys =
3421  &root->processed_distinctClause,
3422  tlist,
3423  true,
3424  &sortable);
3425  if (!sortable)
3426  root->distinct_pathkeys = NIL;
3427  }
3428  else
3429  root->distinct_pathkeys = NIL;
3430 
3431  root->sort_pathkeys =
3433  parse->sortClause,
3434  tlist);
3435 
3436  /*
3437  * Figure out whether we want a sorted result from query_planner.
3438  *
3439  * If we have a sortable GROUP BY clause, then we want a result sorted
3440  * properly for grouping. Otherwise, if we have window functions to
3441  * evaluate, we try to sort for the first window. Otherwise, if there's a
3442  * sortable DISTINCT clause that's more rigorous than the ORDER BY clause,
3443  * we try to produce output that's sufficiently well sorted for the
3444  * DISTINCT. Otherwise, if there is an ORDER BY clause, we want to sort
3445  * by the ORDER BY clause.
3446  *
3447  * Note: if we have both ORDER BY and GROUP BY, and ORDER BY is a superset
3448  * of GROUP BY, it would be tempting to request sort by ORDER BY --- but
3449  * that might just leave us failing to exploit an available sort order at
3450  * all. Needs more thought. The choice for DISTINCT versus ORDER BY is
3451  * much easier, since we know that the parser ensured that one is a
3452  * superset of the other.
3453  */
3454  if (root->group_pathkeys)
3455  root->query_pathkeys = root->group_pathkeys;
3456  else if (root->window_pathkeys)
3457  root->query_pathkeys = root->window_pathkeys;
3458  else if (list_length(root->distinct_pathkeys) >
3459  list_length(root->sort_pathkeys))
3460  root->query_pathkeys = root->distinct_pathkeys;
3461  else if (root->sort_pathkeys)
3462  root->query_pathkeys = root->sort_pathkeys;
3463  else
3464  root->query_pathkeys = NIL;
3465 }
static void adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
Definition: planner.c:3141
List * query_pathkeys
Definition: pathnodes.h:382

References standard_qp_extra::activeWindows, adjust_group_pathkeys_for_groupagg(), PlannerInfo::distinct_pathkeys, PlannerInfo::group_pathkeys, grouping_is_sortable(), standard_qp_extra::gset_data, linitial_node, list_copy(), list_length(), make_pathkeys_for_sortclauses(), make_pathkeys_for_sortclauses_extended(), make_pathkeys_for_window(), NIL, PlannerInfo::num_groupby_pathkeys, PlannerInfo::numOrderedAggs, parse(), PlannerInfo::parse, PlannerInfo::processed_distinctClause, PlannerInfo::processed_groupClause, PlannerInfo::processed_tlist, PlannerInfo::query_pathkeys, grouping_sets_data::rollups, PlannerInfo::sort_pathkeys, and PlannerInfo::window_pathkeys.

Referenced by grouping_planner().

◆ subquery_planner()

PlannerInfo* subquery_planner ( PlannerGlobal glob,
Query parse,
PlannerInfo parent_root,
bool  hasRecursion,
double  tuple_fraction 
)

Definition at line 618 of file planner.c.

621 {
622  PlannerInfo *root;
623  List *newWithCheckOptions;
624  List *newHaving;
625  bool hasOuterJoins;
626  bool hasResultRTEs;
627  RelOptInfo *final_rel;
628  ListCell *l;
629 
630  /* Create a PlannerInfo data structure for this subquery */
631  root = makeNode(PlannerInfo);
632  root->parse = parse;
633  root->glob = glob;
634  root->query_level = parent_root ? parent_root->query_level + 1 : 1;
635  root->parent_root = parent_root;
636  root->plan_params = NIL;
637  root->outer_params = NULL;
638  root->planner_cxt = CurrentMemoryContext;
639  root->init_plans = NIL;
640  root->cte_plan_ids = NIL;
641  root->multiexpr_params = NIL;
642  root->join_domains = NIL;
643  root->eq_classes = NIL;
644  root->ec_merging_done = false;
645  root->last_rinfo_serial = 0;
646  root->all_result_relids =
647  parse->resultRelation ? bms_make_singleton(parse->resultRelation) : NULL;
648  root->leaf_result_relids = NULL; /* we'll find out leaf-ness later */
649  root->append_rel_list = NIL;
650  root->row_identity_vars = NIL;
651  root->rowMarks = NIL;
652  memset(root->upper_rels, 0, sizeof(root->upper_rels));
653  memset(root->upper_targets, 0, sizeof(root->upper_targets));
654  root->processed_groupClause = NIL;
656  root->processed_tlist = NIL;
657  root->update_colnos = NIL;
658  root->grouping_map = NULL;
659  root->minmax_aggs = NIL;
660  root->qual_security_level = 0;
661  root->hasPseudoConstantQuals = false;
662  root->hasAlternativeSubPlans = false;
663  root->placeholdersFrozen = false;
664  root->hasRecursion = hasRecursion;
665  if (hasRecursion)
667  else
668  root->wt_param_id = -1;
669  root->non_recursive_path = NULL;
670  root->partColsUpdated = false;
671 
672  /*
673  * Create the top-level join domain. This won't have valid contents until
674  * deconstruct_jointree fills it in, but the node needs to exist before
675  * that so we can build EquivalenceClasses referencing it.
676  */
678 
679  /*
680  * If there is a WITH list, process each WITH query and either convert it
681  * to RTE_SUBQUERY RTE(s) or build an initplan SubPlan structure for it.
682  */
683  if (parse->cteList)
684  SS_process_ctes(root);
685 
686  /*
687  * If it's a MERGE command, transform the joinlist as appropriate.
688  */
690 
691  /*
692  * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
693  * that we don't need so many special cases to deal with that situation.
694  */
696 
697  /*
698  * Look for ANY and EXISTS SubLinks in WHERE and JOIN/ON clauses, and try
699  * to transform them into joins. Note that this step does not descend
700  * into subqueries; if we pull up any subqueries below, their SubLinks are
701  * processed just before pulling them up.
702  */
703  if (parse->hasSubLinks)
704  pull_up_sublinks(root);
705 
706  /*
707  * Scan the rangetable for function RTEs, do const-simplification on them,
708  * and then inline them if possible (producing subqueries that might get
709  * pulled up next). Recursion issues here are handled in the same way as
710  * for SubLinks.
711  */
713 
714  /*
715  * Check to see if any subqueries in the jointree can be merged into this
716  * query.
717  */
718  pull_up_subqueries(root);
719 
720  /*
721  * If this is a simple UNION ALL query, flatten it into an appendrel. We
722  * do this now because it requires applying pull_up_subqueries to the leaf
723  * queries of the UNION ALL, which weren't touched above because they
724  * weren't referenced by the jointree (they will be after we do this).
725  */
726  if (parse->setOperations)
728 
729  /*
730  * Survey the rangetable to see what kinds of entries are present. We can
731  * skip some later processing if relevant SQL features are not used; for
732  * example if there are no JOIN RTEs we can avoid the expense of doing
733  * flatten_join_alias_vars(). This must be done after we have finished
734  * adding rangetable entries, of course. (Note: actually, processing of
735  * inherited or partitioned rels can cause RTEs for their child tables to
736  * get added later; but those must all be RTE_RELATION entries, so they
737  * don't invalidate the conclusions drawn here.)
738  */
739  root->hasJoinRTEs = false;
740  root->hasLateralRTEs = false;
741  hasOuterJoins = false;
742  hasResultRTEs = false;
743  foreach(l, parse->rtable)
744  {
746 
747  switch (rte->rtekind)
748  {
749  case RTE_RELATION:
750  if (rte->inh)
751  {
752  /*
753  * Check to see if the relation actually has any children;
754  * if not, clear the inh flag so we can treat it as a
755  * plain base relation.
756  *
757  * Note: this could give a false-positive result, if the
758  * rel once had children but no longer does. We used to
759  * be able to clear rte->inh later on when we discovered
760  * that, but no more; we have to handle such cases as
761  * full-fledged inheritance.
762  */
763  rte->inh = has_subclass(rte->relid);
764  }
765  break;
766  case RTE_JOIN:
767  root->hasJoinRTEs = true;
768  if (IS_OUTER_JOIN(rte->jointype))
769  hasOuterJoins = true;
770  break;
771  case RTE_RESULT:
772  hasResultRTEs = true;
773  break;
774  default:
775  /* No work here for other RTE types */
776  break;
777  }
778 
779  if (rte->lateral)
780  root->hasLateralRTEs = true;
781 
782  /*
783  * We can also determine the maximum security level required for any
784  * securityQuals now. Addition of inheritance-child RTEs won't affect
785  * this, because child tables don't have their own securityQuals; see
786  * expand_single_inheritance_child().
787  */
788  if (rte->securityQuals)
790  list_length(rte->securityQuals));
791  }
792 
793  /*
794  * If we have now verified that the query target relation is
795  * non-inheriting, mark it as a leaf target.
796  */
797  if (parse->resultRelation)
798  {
799  RangeTblEntry *rte = rt_fetch(parse->resultRelation, parse->rtable);
800 
801  if (!rte->inh)
802  root->leaf_result_relids =
803  bms_make_singleton(parse->resultRelation);
804  }
805 
806  /*
807  * Preprocess RowMark information. We need to do this after subquery
808  * pullup, so that all base relations are present.
809  */
810  preprocess_rowmarks(root);
811 
812  /*
813  * Set hasHavingQual to remember if HAVING clause is present. Needed
814  * because preprocess_expression will reduce a constant-true condition to
815  * an empty qual list ... but "HAVING TRUE" is not a semantic no-op.
816  */
817  root->hasHavingQual = (parse->havingQual != NULL);
818 
819  /*
820  * Do expression preprocessing on targetlist and quals, as well as other
821  * random expressions in the querytree. Note that we do not need to
822  * handle sort/group expressions explicitly, because they are actually
823  * part of the targetlist.
824  */
825  parse->targetList = (List *)
826  preprocess_expression(root, (Node *) parse->targetList,
828 
829  /* Constant-folding might have removed all set-returning functions */
830  if (parse->hasTargetSRFs)
831  parse->hasTargetSRFs = expression_returns_set((Node *) parse->targetList);
832 
833  newWithCheckOptions = NIL;
834  foreach(l, parse->withCheckOptions)
835  {
837 
838  wco->qual = preprocess_expression(root, wco->qual,
839  EXPRKIND_QUAL);
840  if (wco->qual != NULL)
841  newWithCheckOptions = lappend(newWithCheckOptions, wco);
842  }
843  parse->withCheckOptions = newWithCheckOptions;
844 
845  parse->returningList = (List *)
846  preprocess_expression(root, (Node *) parse->returningList,
848 
849  preprocess_qual_conditions(root, (Node *) parse->jointree);
850 
851  parse->havingQual = preprocess_expression(root, parse->havingQual,
852  EXPRKIND_QUAL);
853 
854  foreach(l, parse->windowClause)
855  {
857 
858  /* partitionClause/orderClause are sort/group expressions */
861  wc->endOffset = preprocess_expression(root, wc->endOffset,
863  wc->runCondition = (List *) preprocess_expression(root,
864  (Node *) wc->runCondition,
866  }
867 
868  parse->limitOffset = preprocess_expression(root, parse->limitOffset,
870  parse->limitCount = preprocess_expression(root, parse->limitCount,
872 
873  if (parse->onConflict)
874  {
875  parse->onConflict->arbiterElems = (List *)
877  (Node *) parse->onConflict->arbiterElems,
879  parse->onConflict->arbiterWhere =
881  parse->onConflict->arbiterWhere,
882  EXPRKIND_QUAL);
883  parse->onConflict->onConflictSet = (List *)
885  (Node *) parse->onConflict->onConflictSet,
887  parse->onConflict->onConflictWhere =
889  parse->onConflict->onConflictWhere,
890  EXPRKIND_QUAL);
891  /* exclRelTlist contains only Vars, so no preprocessing needed */
892  }
893 
894  foreach(l, parse->mergeActionList)
895  {
897 
898  action->targetList = (List *)
900  (Node *) action->targetList,
902  action->qual =
904  (Node *) action->qual,
905  EXPRKIND_QUAL);
906  }
907 
908  root->append_rel_list = (List *)
911 
912  /* Also need to preprocess expressions within RTEs */
913  foreach(l, parse->rtable)
914  {
916  int kind;
917  ListCell *lcsq;
918 
919  if (rte->rtekind == RTE_RELATION)
920  {
921  if (rte->tablesample)
922  rte->tablesample = (TableSampleClause *)
924  (Node *) rte->tablesample,
926  }
927  else if (rte->rtekind == RTE_SUBQUERY)
928  {
929  /*
930  * We don't want to do all preprocessing yet on the subquery's
931  * expressions, since that will happen when we plan it. But if it
932  * contains any join aliases of our level, those have to get
933  * expanded now, because planning of the subquery won't do it.
934  * That's only possible if the subquery is LATERAL.
935  */
936  if (rte->lateral && root->hasJoinRTEs)
937  rte->subquery = (Query *)
938  flatten_join_alias_vars(root, root->parse,
939  (Node *) rte->subquery);
940  }
941  else if (rte->rtekind == RTE_FUNCTION)
942  {
943  /* Preprocess the function expression(s) fully */
945  rte->functions = (List *)
946  preprocess_expression(root, (Node *) rte->functions, kind);
947  }
948  else if (rte->rtekind == RTE_TABLEFUNC)
949  {
950  /* Preprocess the function expression(s) fully */
952  rte->tablefunc = (TableFunc *)
953  preprocess_expression(root, (Node *) rte->tablefunc, kind);
954  }
955  else if (rte->rtekind == RTE_VALUES)
956  {
957  /* Preprocess the values lists fully */
959  rte->values_lists = (List *)
960  preprocess_expression(root, (Node *) rte->values_lists, kind);
961  }
962 
963  /*
964  * Process each element of the securityQuals list as if it were a
965  * separate qual expression (as indeed it is). We need to do it this
966  * way to get proper canonicalization of AND/OR structure. Note that
967  * this converts each element into an implicit-AND sublist.
968  */
969  foreach(lcsq, rte->securityQuals)
970  {
971  lfirst(lcsq) = preprocess_expression(root,
972  (Node *) lfirst(lcsq),
973  EXPRKIND_QUAL);
974  }
975  }
976 
977  /*
978  * Now that we are done preprocessing expressions, and in particular done
979  * flattening join alias variables, get rid of the joinaliasvars lists.
980  * They no longer match what expressions in the rest of the tree look
981  * like, because we have not preprocessed expressions in those lists (and
982  * do not want to; for example, expanding a SubLink there would result in
983  * a useless unreferenced subplan). Leaving them in place simply creates
984  * a hazard for later scans of the tree. We could try to prevent that by
985  * using QTW_IGNORE_JOINALIASES in every tree scan done after this point,
986  * but that doesn't sound very reliable.
987  */
988  if (root->hasJoinRTEs)
989  {
990  foreach(l, parse->rtable)
991  {
993 
994  rte->joinaliasvars = NIL;
995  }
996  }
997 
998  /*
999  * In some cases we may want to transfer a HAVING clause into WHERE. We
1000  * cannot do so if the HAVING clause contains aggregates (obviously) or
1001  * volatile functions (since a HAVING clause is supposed to be executed
1002  * only once per group). We also can't do this if there are any nonempty
1003  * grouping sets; moving such a clause into WHERE would potentially change
1004  * the results, if any referenced column isn't present in all the grouping
1005  * sets. (If there are only empty grouping sets, then the HAVING clause
1006  * must be degenerate as discussed below.)
1007  *
1008  * Also, it may be that the clause is so expensive to execute that we're
1009  * better off doing it only once per group, despite the loss of
1010  * selectivity. This is hard to estimate short of doing the entire
1011  * planning process twice, so we use a heuristic: clauses containing
1012  * subplans are left in HAVING. Otherwise, we move or copy the HAVING
1013  * clause into WHERE, in hopes of eliminating tuples before aggregation
1014  * instead of after.
1015  *
1016  * If the query has explicit grouping then we can simply move such a
1017  * clause into WHERE; any group that fails the clause will not be in the
1018  * output because none of its tuples will reach the grouping or
1019  * aggregation stage. Otherwise we must have a degenerate (variable-free)
1020  * HAVING clause, which we put in WHERE so that query_planner() can use it
1021  * in a gating Result node, but also keep in HAVING to ensure that we
1022  * don't emit a bogus aggregated row. (This could be done better, but it
1023  * seems not worth optimizing.)
1024  *
1025  * Note that both havingQual and parse->jointree->quals are in
1026  * implicitly-ANDed-list form at this point, even though they are declared
1027  * as Node *.
1028  */
1029  newHaving = NIL;
1030  foreach(l, (List *) parse->havingQual)
1031  {
1032  Node *havingclause = (Node *) lfirst(l);
1033 
1034  if ((parse->groupClause && parse->groupingSets) ||
1035  contain_agg_clause(havingclause) ||
1036  contain_volatile_functions(havingclause) ||
1037  contain_subplans(havingclause))
1038  {
1039  /* keep it in HAVING */
1040  newHaving = lappend(newHaving, havingclause);
1041  }
1042  else if (parse->groupClause && !parse->groupingSets)
1043  {
1044  /* move it to WHERE */
1045  parse->jointree->quals = (Node *)
1046  lappend((List *) parse->jointree->quals, havingclause);
1047  }
1048  else
1049  {
1050  /* put a copy in WHERE, keep it in HAVING */
1051  parse->jointree->quals = (Node *)
1052  lappend((List *) parse->jointree->quals,
1053  copyObject(havingclause));
1054  newHaving = lappend(newHaving, havingclause);
1055  }
1056  }
1057  parse->havingQual = (Node *) newHaving;
1058 
1059  /*
1060  * If we have any outer joins, try to reduce them to plain inner joins.
1061  * This step is most easily done after we've done expression
1062  * preprocessing.
1063  */
1064  if (hasOuterJoins)
1065  reduce_outer_joins(root);
1066 
1067  /*
1068  * If we have any RTE_RESULT relations, see if they can be deleted from
1069  * the jointree. We also rely on this processing to flatten single-child
1070  * FromExprs underneath outer joins. This step is most effectively done
1071  * after we've done expression preprocessing and outer join reduction.
1072  */
1073  if (hasResultRTEs || hasOuterJoins)
1075 
1076  /*
1077  * Do the main planning.
1078  */
1079  grouping_planner(root, tuple_fraction);
1080 
1081  /*
1082  * Capture the set of outer-level param IDs we have access to, for use in
1083  * extParam/allParam calculations later.
1084  */
1086 
1087  /*
1088  * If any initPlans were created in this query level, adjust the surviving
1089  * Paths' costs and parallel-safety flags to account for them. The
1090  * initPlans won't actually get attached to the plan tree till
1091  * create_plan() runs, but we must include their effects now.
1092  */
1093  final_rel = fetch_upper_rel(root, UPPERREL_FINAL, NULL);
1094  SS_charge_for_initplans(root, final_rel);
1095 
1096  /*
1097  * Make sure we've identified the cheapest Path for the final rel. (By
1098  * doing this here not in grouping_planner, we include initPlan costs in
1099  * the decision, though it's unlikely that will change anything.)
1100  */
1101  set_cheapest(final_rel);
1102 
1103  return root;
1104 }
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
bool contain_agg_clause(Node *clause)
Definition: clauses.c:176
bool contain_subplans(Node *clause)
Definition: clauses.c:329
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:327
@ RTE_JOIN
Definition: parsenodes.h:1013
@ RTE_VALUES
Definition: parsenodes.h:1016
@ RTE_SUBQUERY
Definition: parsenodes.h:1012
@ RTE_RESULT
Definition: parsenodes.h:1019
@ RTE_FUNCTION
Definition: parsenodes.h:1014
@ RTE_TABLEFUNC
Definition: parsenodes.h:1015
bool has_subclass(Oid relationId)
Definition: pg_inherits.c:355
#define EXPRKIND_TABLEFUNC_LATERAL
Definition: planner.c:89
#define EXPRKIND_APPINFO
Definition: planner.c:84
static void preprocess_rowmarks(PlannerInfo *root)
Definition: planner.c:2233
#define EXPRKIND_RTFUNC_LATERAL
Definition: planner.c:80
#define EXPRKIND_VALUES_LATERAL
Definition: planner.c:82
#define EXPRKIND_LIMIT
Definition: planner.c:83
static void grouping_planner(PlannerInfo *root, double tuple_fraction)
Definition: planner.c:1288
#define EXPRKIND_ARBITER_ELEM
Definition: planner.c:87
void preprocess_function_rtes(PlannerInfo *root)
Definition: prepjointree.c:745
void flatten_simple_union_all(PlannerInfo *root)
void transform_MERGE_to_join(Query *parse)
Definition: prepjointree.c:152
void remove_useless_result_rtes(PlannerInfo *root)
void pull_up_sublinks(PlannerInfo *root)
Definition: prepjointree.c:311
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:253
void pull_up_subqueries(PlannerInfo *root)
Definition: prepjointree.c:786
void reduce_outer_joins(PlannerInfo *root)
List * minmax_aggs
Definition: pathnodes.h:469
List * cte_plan_ids
Definition: pathnodes.h:302
int last_rinfo_serial
Definition: pathnodes.h:340
bool hasLateralRTEs
Definition: pathnodes.h:491
Index qual_security_level
Definition: pathnodes.h:486
List * init_plans
Definition: pathnodes.h:296
List * multiexpr_params
Definition: pathnodes.h:305
List * row_identity_vars
Definition: pathnodes.h:365
bool ec_merging_done
Definition: pathnodes.h:314
Bitmapset * outer_params
Definition: pathnodes.h:218
List * append_rel_list
Definition: pathnodes.h:362
struct Path * non_recursive_path
Definition: pathnodes.h:523
List * eq_classes
Definition: pathnodes.h:311
List * plan_params
Definition: pathnodes.h:217
bool hasAlternativeSubPlans
Definition: pathnodes.h:497
bool placeholdersFrozen
Definition: pathnodes.h:499
bool hasPseudoConstantQuals
Definition: pathnodes.h:495
TableFunc * tablefunc
Definition: parsenodes.h:1159
struct TableSampleClause * tablesample
Definition: parsenodes.h:1081
List * securityQuals
Definition: parsenodes.h:1208
Query * subquery
Definition: parsenodes.h:1086
List * values_lists
Definition: parsenodes.h:1164
List * joinaliasvars
Definition: parsenodes.h:1134
JoinType jointype
Definition: parsenodes.h:1132
List * functions
Definition: parsenodes.h:1153
void SS_process_ctes(PlannerInfo *root)
Definition: subselect.c:893
void SS_identify_outer_params(PlannerInfo *root)
Definition: subselect.c:2085
void SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
Definition: subselect.c:2147

References generate_unaccent_rules::action, PlannerInfo::all_result_relids, PlannerInfo::append_rel_list, assign_special_exec_param(), bms_make_singleton(), contain_agg_clause(), contain_subplans(), contain_volatile_functions(), copyObject, PlannerInfo::cte_plan_ids, CurrentMemoryContext, PlannerInfo::ec_merging_done, WindowClause::endOffset, PlannerInfo::eq_classes, expression_returns_set(), EXPRKIND_APPINFO, EXPRKIND_ARBITER_ELEM, EXPRKIND_LIMIT, EXPRKIND_QUAL, EXPRKIND_RTFUNC, EXPRKIND_RTFUNC_LATERAL, EXPRKIND_TABLEFUNC, EXPRKIND_TABLEFUNC_LATERAL, EXPRKIND_TABLESAMPLE, EXPRKIND_TARGET, EXPRKIND_VALUES, EXPRKIND_VALUES_LATERAL, fetch_upper_rel(), flatten_join_alias_vars(), flatten_simple_union_all(), RangeTblEntry::functions, PlannerInfo::glob, grouping_planner(), has_subclass(), PlannerInfo::hasAlternativeSubPlans, PlannerInfo::hasHavingQual, PlannerInfo::hasJoinRTEs, PlannerInfo::hasLateralRTEs, PlannerInfo::hasPseudoConstantQuals, PlannerInfo::hasRecursion, RangeTblEntry::inh, PlannerInfo::init_plans, IS_OUTER_JOIN, PlannerInfo::join_domains, RangeTblEntry::joinaliasvars, RangeTblEntry::jointype, lappend(), PlannerInfo::last_rinfo_serial, RangeTblEntry::lateral, PlannerInfo::leaf_result_relids, lfirst, lfirst_node, list_length(), list_make1, makeNode, Max, PlannerInfo::minmax_aggs, PlannerInfo::multiexpr_params, NIL, PlannerInfo::non_recursive_path, PlannerInfo::outer_params, parse(), PlannerInfo::parse, PlannerInfo::partColsUpdated, PlannerInfo::placeholdersFrozen, PlannerInfo::plan_params, preprocess_expression(), preprocess_function_rtes(), preprocess_qual_conditions(), preprocess_rowmarks(), PlannerInfo::processed_distinctClause, PlannerInfo::processed_groupClause, PlannerInfo::processed_tlist, pull_up_sublinks(), pull_up_subqueries(), WithCheckOption::qual, PlannerInfo::qual_security_level, PlannerInfo::query_level, reduce_outer_joins(), RangeTblEntry::relid, remove_useless_result_rtes(), replace_empty_jointree(), PlannerInfo::row_identity_vars, PlannerInfo::rowMarks, rt_fetch, RTE_FUNCTION, RTE_JOIN, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::securityQuals, set_cheapest(), SS_charge_for_initplans(), SS_identify_outer_params(), SS_process_ctes(), WindowClause::startOffset, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, transform_MERGE_to_join(), PlannerInfo::update_colnos, UPPERREL_FINAL, RangeTblEntry::values_lists, and PlannerInfo::wt_param_id.

Referenced by make_subplan(), recurse_set_operations(), set_subquery_pathlist(), SS_process_ctes(), and standard_planner().

Variable Documentation

◆ create_upper_paths_hook

◆ cursor_tuple_fraction

double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION

Definition at line 65 of file planner.c.

Referenced by standard_planner().

◆ debug_parallel_query

int debug_parallel_query = DEBUG_PARALLEL_OFF

Definition at line 66 of file planner.c.

Referenced by HandleParallelMessage(), query_planner(), and standard_planner().

◆ parallel_leader_participation

bool parallel_leader_participation = true

Definition at line 67 of file planner.c.

Referenced by ExecGather(), ExecGatherMerge(), ExecInitGather(), and get_parallel_divisor().

◆ planner_hook

planner_hook_type planner_hook = NULL

Definition at line 70 of file planner.c.

Referenced by _PG_init(), and planner().