PostgreSQL Source Code  git master
cost.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * cost.h
4  * prototypes for costsize.c and clausesel.c.
5  *
6  *
7  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/optimizer/cost.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef COST_H
15 #define COST_H
16 
17 #include "nodes/pathnodes.h"
18 #include "nodes/plannodes.h"
19 
20 
21 /* defaults for costsize.c's Cost parameters */
22 /* NB: cost-estimation code should use the variables, not these constants! */
23 /* If you change these, update backend/utils/misc/postgresql.conf.sample */
24 #define DEFAULT_SEQ_PAGE_COST 1.0
25 #define DEFAULT_RANDOM_PAGE_COST 4.0
26 #define DEFAULT_CPU_TUPLE_COST 0.01
27 #define DEFAULT_CPU_INDEX_TUPLE_COST 0.005
28 #define DEFAULT_CPU_OPERATOR_COST 0.0025
29 #define DEFAULT_PARALLEL_TUPLE_COST 0.1
30 #define DEFAULT_PARALLEL_SETUP_COST 1000.0
31 
32 /* defaults for non-Cost parameters */
33 #define DEFAULT_RECURSIVE_WORKTABLE_FACTOR 10.0
34 #define DEFAULT_EFFECTIVE_CACHE_SIZE 524288 /* measured in pages */
35 
36 typedef enum
37 {
38  CONSTRAINT_EXCLUSION_OFF, /* do not use c_e */
39  CONSTRAINT_EXCLUSION_ON, /* apply c_e to all rels */
40  CONSTRAINT_EXCLUSION_PARTITION /* apply c_e to otherrels only */
42 
43 
44 /*
45  * prototypes for costsize.c
46  * routines to compute costs and sizes
47  */
48 
49 /* parameter variables and flags (see also optimizer.h) */
52 extern PGDLLIMPORT bool enable_seqscan;
53 extern PGDLLIMPORT bool enable_indexscan;
55 extern PGDLLIMPORT bool enable_bitmapscan;
56 extern PGDLLIMPORT bool enable_tidscan;
57 extern PGDLLIMPORT bool enable_sort;
59 extern PGDLLIMPORT bool enable_hashagg;
60 extern PGDLLIMPORT bool enable_nestloop;
61 extern PGDLLIMPORT bool enable_material;
62 extern PGDLLIMPORT bool enable_memoize;
63 extern PGDLLIMPORT bool enable_mergejoin;
64 extern PGDLLIMPORT bool enable_hashjoin;
74 
75 extern double index_pages_fetched(double tuples_fetched, BlockNumber pages,
76  double index_pages, PlannerInfo *root);
77 extern void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
78  ParamPathInfo *param_info);
79 extern void cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
80  ParamPathInfo *param_info);
81 extern void cost_index(IndexPath *path, PlannerInfo *root,
82  double loop_count, bool partial_path);
83 extern void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel,
84  ParamPathInfo *param_info,
85  Path *bitmapqual, double loop_count);
86 extern void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root);
87 extern void cost_bitmap_or_node(BitmapOrPath *path, PlannerInfo *root);
88 extern void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec);
89 extern void cost_tidscan(Path *path, PlannerInfo *root,
90  RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info);
91 extern void cost_tidrangescan(Path *path, PlannerInfo *root,
92  RelOptInfo *baserel, List *tidrangequals,
93  ParamPathInfo *param_info);
94 extern void cost_subqueryscan(SubqueryScanPath *path, PlannerInfo *root,
95  RelOptInfo *baserel, ParamPathInfo *param_info,
96  bool trivial_pathtarget);
97 extern void cost_functionscan(Path *path, PlannerInfo *root,
98  RelOptInfo *baserel, ParamPathInfo *param_info);
99 extern void cost_valuesscan(Path *path, PlannerInfo *root,
100  RelOptInfo *baserel, ParamPathInfo *param_info);
101 extern void cost_tablefuncscan(Path *path, PlannerInfo *root,
102  RelOptInfo *baserel, ParamPathInfo *param_info);
103 extern void cost_ctescan(Path *path, PlannerInfo *root,
104  RelOptInfo *baserel, ParamPathInfo *param_info);
105 extern void cost_namedtuplestorescan(Path *path, PlannerInfo *root,
106  RelOptInfo *baserel, ParamPathInfo *param_info);
107 extern void cost_resultscan(Path *path, PlannerInfo *root,
108  RelOptInfo *baserel, ParamPathInfo *param_info);
109 extern void cost_recursive_union(Path *runion, Path *nrterm, Path *rterm);
110 extern void cost_sort(Path *path, PlannerInfo *root,
111  List *pathkeys, Cost input_cost, double tuples, int width,
112  Cost comparison_cost, int sort_mem,
113  double limit_tuples);
114 extern void cost_incremental_sort(Path *path,
115  PlannerInfo *root, List *pathkeys, int presorted_keys,
116  Cost input_startup_cost, Cost input_total_cost,
117  double input_tuples, int width, Cost comparison_cost, int sort_mem,
118  double limit_tuples);
119 extern void cost_append(AppendPath *apath);
120 extern void cost_merge_append(Path *path, PlannerInfo *root,
121  List *pathkeys, int n_streams,
122  Cost input_startup_cost, Cost input_total_cost,
123  double tuples);
124 extern void cost_material(Path *path,
125  Cost input_startup_cost, Cost input_total_cost,
126  double tuples, int width);
127 extern void cost_agg(Path *path, PlannerInfo *root,
128  AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
129  int numGroupCols, double numGroups,
130  List *quals,
131  Cost input_startup_cost, Cost input_total_cost,
132  double input_tuples, double input_width);
133 extern void cost_windowagg(Path *path, PlannerInfo *root,
134  List *windowFuncs, int numPartCols, int numOrderCols,
135  Cost input_startup_cost, Cost input_total_cost,
136  double input_tuples);
137 extern void cost_group(Path *path, PlannerInfo *root,
138  int numGroupCols, double numGroups,
139  List *quals,
140  Cost input_startup_cost, Cost input_total_cost,
141  double input_tuples);
142 extern void initial_cost_nestloop(PlannerInfo *root,
143  JoinCostWorkspace *workspace,
144  JoinType jointype,
145  Path *outer_path, Path *inner_path,
146  JoinPathExtraData *extra);
147 extern void final_cost_nestloop(PlannerInfo *root, NestPath *path,
148  JoinCostWorkspace *workspace,
149  JoinPathExtraData *extra);
150 extern void initial_cost_mergejoin(PlannerInfo *root,
151  JoinCostWorkspace *workspace,
152  JoinType jointype,
153  List *mergeclauses,
154  Path *outer_path, Path *inner_path,
155  List *outersortkeys, List *innersortkeys,
156  JoinPathExtraData *extra);
157 extern void final_cost_mergejoin(PlannerInfo *root, MergePath *path,
158  JoinCostWorkspace *workspace,
159  JoinPathExtraData *extra);
160 extern void initial_cost_hashjoin(PlannerInfo *root,
161  JoinCostWorkspace *workspace,
162  JoinType jointype,
163  List *hashclauses,
164  Path *outer_path, Path *inner_path,
165  JoinPathExtraData *extra,
166  bool parallel_hash);
167 extern void final_cost_hashjoin(PlannerInfo *root, HashPath *path,
168  JoinCostWorkspace *workspace,
169  JoinPathExtraData *extra);
170 extern void cost_gather(GatherPath *path, PlannerInfo *root,
171  RelOptInfo *rel, ParamPathInfo *param_info, double *rows);
172 extern void cost_gather_merge(GatherMergePath *path, PlannerInfo *root,
173  RelOptInfo *rel, ParamPathInfo *param_info,
174  Cost input_startup_cost, Cost input_total_cost,
175  double *rows);
176 extern void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan);
177 extern void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root);
178 extern void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root);
180  RelOptInfo *joinrel,
181  RelOptInfo *outerrel,
182  RelOptInfo *innerrel,
183  JoinType jointype,
184  SpecialJoinInfo *sjinfo,
185  List *restrictlist,
186  SemiAntiJoinFactors *semifactors);
187 extern void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel);
188 extern double get_parameterized_baserel_size(PlannerInfo *root,
189  RelOptInfo *rel,
190  List *param_clauses);
191 extern double get_parameterized_joinrel_size(PlannerInfo *root,
192  RelOptInfo *rel,
193  Path *outer_path,
194  Path *inner_path,
195  SpecialJoinInfo *sjinfo,
196  List *restrict_clauses);
197 extern void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
198  RelOptInfo *outer_rel,
199  RelOptInfo *inner_rel,
200  SpecialJoinInfo *sjinfo,
201  List *restrictlist);
202 extern void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel);
203 extern void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel);
204 extern void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel);
205 extern void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel,
206  double cte_rows);
207 extern void set_tablefunc_size_estimates(PlannerInfo *root, RelOptInfo *rel);
209 extern void set_result_size_estimates(PlannerInfo *root, RelOptInfo *rel);
210 extern void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel);
212 extern double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel,
213  Path *bitmapqual, int loop_count, Cost *cost, double *tuple);
214 
215 #endif /* COST_H */
uint32 BlockNumber
Definition: block.h:31
#define PGDLLIMPORT
Definition: c.h:1303
void set_namedtuplestore_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5750
PGDLLIMPORT bool enable_tidscan
Definition: costsize.c:139
PGDLLIMPORT bool enable_partitionwise_join
Definition: costsize.c:149
void final_cost_hashjoin(PlannerInfo *root, HashPath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
Definition: costsize.c:3908
double index_pages_fetched(double tuples_fetched, BlockNumber pages, double index_pages, PlannerInfo *root)
Definition: costsize.c:869
PGDLLIMPORT int max_parallel_workers_per_gather
Definition: costsize.c:133
PGDLLIMPORT bool enable_parallel_hash
Definition: costsize.c:152
void cost_bitmap_tree_node(Path *path, Cost *cost, Selectivity *selec)
Definition: costsize.c:1085
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:5017
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:5098
void final_cost_mergejoin(PlannerInfo *root, MergePath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
Definition: costsize.c:3472
void initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *mergeclauses, Path *outer_path, Path *inner_path, List *outersortkeys, List *innersortkeys, JoinPathExtraData *extra)
Definition: costsize.c:3242
void compute_semi_anti_join_factors(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:4729
PGDLLIMPORT bool enable_async_append
Definition: costsize.c:155
void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:4987
PGDLLIMPORT bool enable_nestloop
Definition: costsize.c:143
void cost_functionscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1502
PGDLLIMPORT bool enable_incremental_sort
Definition: costsize.c:141
void cost_material(Path *path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition: costsize.c:2424
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:984
void cost_tidrangescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, List *tidrangequals, ParamPathInfo *param_info)
Definition: costsize.c:1328
PathTarget * set_pathtarget_cost_width(PlannerInfo *root, PathTarget *target)
Definition: costsize.c:6005
void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5620
void cost_merge_append(Path *path, PlannerInfo *root, List *pathkeys, int n_streams, Cost input_startup_cost, Cost input_total_cost, double tuples)
Definition: costsize.c:2375
PGDLLIMPORT bool enable_material
Definition: costsize.c:144
PGDLLIMPORT bool enable_presorted_aggregate
Definition: costsize.c:154
void final_cost_nestloop(PlannerInfo *root, NestPath *path, JoinCostWorkspace *workspace, JoinPathExtraData *extra)
Definition: costsize.c:3036
void cost_recursive_union(Path *runion, Path *nrterm, Path *rterm)
Definition: costsize.c:1784
double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel, Path *bitmapqual, int loop_count, Cost *cost, double *tuple)
Definition: costsize.c:6138
void cost_incremental_sort(Path *path, PlannerInfo *root, List *pathkeys, int presorted_keys, Cost input_startup_cost, Cost input_total_cost, double input_tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:1957
void cost_tablefuncscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1563
void initial_cost_hashjoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *hashclauses, Path *outer_path, Path *inner_path, JoinPathExtraData *extra, bool parallel_hash)
Definition: costsize.c:3800
PGDLLIMPORT bool enable_seqscan
Definition: costsize.c:135
void cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:332
void cost_gather(GatherPath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, double *rows)
Definition: costsize.c:407
ConstraintExclusionType
Definition: cost.h:37
@ CONSTRAINT_EXCLUSION_OFF
Definition: cost.h:38
@ CONSTRAINT_EXCLUSION_PARTITION
Definition: cost.h:40
@ CONSTRAINT_EXCLUSION_ON
Definition: cost.h:39
PGDLLIMPORT bool enable_mergejoin
Definition: costsize.c:146
void set_cte_size_estimates(PlannerInfo *root, RelOptInfo *rel, double cte_rows)
Definition: costsize.c:5712
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5066
void cost_agg(Path *path, PlannerInfo *root, AggStrategy aggstrategy, const AggClauseCosts *aggcosts, int numGroupCols, double numGroups, List *quals, Cost input_startup_cost, Cost input_total_cost, double input_tuples, double input_width)
Definition: costsize.c:2619
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:4393
void cost_namedtuplestorescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1710
void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:255
void cost_valuesscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1619
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:4367
void initial_cost_nestloop(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
Definition: costsize.c:2961
PGDLLIMPORT bool enable_gathermerge
Definition: costsize.c:148
void set_result_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5783
PGDLLIMPORT bool enable_sort
Definition: costsize.c:140
void cost_resultscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1747
PGDLLIMPORT bool enable_indexscan
Definition: costsize.c:136
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:1128
void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5540
PGDLLIMPORT Cost disable_cost
Definition: costsize.c:131
void set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5812
void cost_gather_merge(GatherMergePath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, Cost input_startup_cost, Cost input_total_cost, double *rows)
Definition: costsize.c:445
PGDLLIMPORT bool enable_partitionwise_aggregate
Definition: costsize.c:150
void cost_tidscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info)
Definition: costsize.c:1220
PGDLLIMPORT int constraint_exclusion
Definition: plancat.c:58
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:4162
PGDLLIMPORT bool enable_bitmapscan
Definition: costsize.c:138
PGDLLIMPORT bool enable_indexonlyscan
Definition: costsize.c:137
void cost_subqueryscan(SubqueryScanPath *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, bool trivial_pathtarget)
Definition: costsize.c:1422
void cost_append(AppendPath *apath)
Definition: costsize.c:2202
void cost_ctescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1669
PGDLLIMPORT bool enable_hashagg
Definition: costsize.c:142
void cost_bitmap_or_node(BitmapOrPath *path, PlannerInfo *root)
Definition: costsize.c:1172
void set_tablefunc_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5658
void cost_index(IndexPath *path, PlannerInfo *root, double loop_count, bool partial_path)
Definition: costsize.c:520
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:2095
void cost_windowagg(Path *path, PlannerInfo *root, List *windowFuncs, int numPartCols, int numOrderCols, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:2817
PGDLLIMPORT bool enable_memoize
Definition: costsize.c:145
void cost_group(Path *path, PlannerInfo *root, int numGroupCols, double numGroups, List *quals, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:2891
void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel)
Definition: costsize.c:5680
PGDLLIMPORT bool enable_partition_pruning
Definition: costsize.c:153
PGDLLIMPORT bool enable_parallel_append
Definition: costsize.c:151
PGDLLIMPORT bool enable_hashjoin
Definition: costsize.c:147
double Cost
Definition: nodes.h:251
AggStrategy
Definition: nodes.h:350
double Selectivity
Definition: nodes.h:250
JoinType
Definition: nodes.h:288
Definition: pg_list.h:54
Definition: nodes.h:118