PostgreSQL Source Code  git master
selfuncs.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * selfuncs.h
4  * Selectivity functions for standard operators, and assorted
5  * infrastructure for selectivity and cost estimation.
6  *
7  *
8  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * src/include/utils/selfuncs.h
12  *
13  *-------------------------------------------------------------------------
14  */
15 #ifndef SELFUNCS_H
16 #define SELFUNCS_H
17 
18 #include "access/htup.h"
19 #include "fmgr.h"
20 #include "nodes/pathnodes.h"
21 
22 
23 /*
24  * Note: the default selectivity estimates are not chosen entirely at random.
25  * We want them to be small enough to ensure that indexscans will be used if
26  * available, for typical table densities of ~100 tuples/page. Thus, for
27  * example, 0.01 is not quite small enough, since that makes it appear that
28  * nearly all pages will be hit anyway. Also, since we sometimes estimate
29  * eqsel as 1/num_distinct, we probably want DEFAULT_NUM_DISTINCT to equal
30  * 1/DEFAULT_EQ_SEL.
31  */
32 
33 /* default selectivity estimate for equalities such as "A = b" */
34 #define DEFAULT_EQ_SEL 0.005
35 
36 /* default selectivity estimate for inequalities such as "A < b" */
37 #define DEFAULT_INEQ_SEL 0.3333333333333333
38 
39 /* default selectivity estimate for range inequalities "A > b AND A < c" */
40 #define DEFAULT_RANGE_INEQ_SEL 0.005
41 
42 /* default selectivity estimate for multirange inequalities "A > b AND A < c" */
43 #define DEFAULT_MULTIRANGE_INEQ_SEL 0.005
44 
45 /* default selectivity estimate for pattern-match operators such as LIKE */
46 #define DEFAULT_MATCH_SEL 0.005
47 
48 /* default selectivity estimate for other matching operators */
49 #define DEFAULT_MATCHING_SEL 0.010
50 
51 /* default number of distinct values in a table */
52 #define DEFAULT_NUM_DISTINCT 200
53 
54 /* default selectivity estimate for boolean and null test nodes */
55 #define DEFAULT_UNK_SEL 0.005
56 #define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
57 
58 
59 /*
60  * Clamp a computed probability estimate (which may suffer from roundoff or
61  * estimation errors) to valid range. Argument must be a float variable.
62  */
63 #define CLAMP_PROBABILITY(p) \
64  do { \
65  if (p < 0.0) \
66  p = 0.0; \
67  else if (p > 1.0) \
68  p = 1.0; \
69  } while (0)
70 
71 
72 /* Return data from examine_variable and friends */
73 typedef struct VariableStatData
74 {
75  Node *var; /* the Var or expression tree */
76  RelOptInfo *rel; /* Relation, or NULL if not identifiable */
77  HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
78  /* NB: if statsTuple!=NULL, it must be freed when caller is done */
79  void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
80  Oid vartype; /* exposed type of expression */
81  Oid atttype; /* actual type (after stripping relabel) */
82  int32 atttypmod; /* actual typmod (after stripping relabel) */
83  bool isunique; /* matches unique index or DISTINCT clause */
84  bool acl_ok; /* result of ACL check on table or column */
86 
87 #define ReleaseVariableStats(vardata) \
88  do { \
89  if (HeapTupleIsValid((vardata).statsTuple)) \
90  (vardata).freefunc((vardata).statsTuple); \
91  } while(0)
92 
93 
94 /*
95  * genericcostestimate is a general-purpose estimator that can be used for
96  * most index types. In some cases we use genericcostestimate as the base
97  * code and then incorporate additional index-type-specific knowledge in
98  * the type-specific calling function. To avoid code duplication, we make
99  * genericcostestimate return a number of intermediate values as well as
100  * its preliminary estimates of the output cost values. The GenericCosts
101  * struct includes all these values.
102  *
103  * Callers should initialize all fields of GenericCosts to zero. In addition,
104  * they can set numIndexTuples to some positive value if they have a better
105  * than default way of estimating the number of leaf index tuples visited.
106  */
107 typedef struct
108 {
109  /* These are the values the cost estimator must return to the planner */
110  Cost indexStartupCost; /* index-related startup cost */
111  Cost indexTotalCost; /* total index-related scan cost */
112  Selectivity indexSelectivity; /* selectivity of index */
113  double indexCorrelation; /* order correlation of index */
114 
115  /* Intermediate values we obtain along the way */
116  double numIndexPages; /* number of leaf pages visited */
117  double numIndexTuples; /* number of leaf tuples visited */
118  double spc_random_page_cost; /* relevant random_page_cost value */
119  double num_sa_scans; /* # indexscans from ScalarArrayOpExprs */
120 } GenericCosts;
121 
122 /* Hooks for plugins to get control when we ask for stats */
124  RangeTblEntry *rte,
126  VariableStatData *vardata);
129  Oid indexOid,
130  AttrNumber indexattnum,
131  VariableStatData *vardata);
133 
134 /* Functions in selfuncs.c */
135 
136 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
137  VariableStatData *vardata);
138 extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
139 extern bool get_restriction_variable(PlannerInfo *root, List *args,
140  int varRelid,
141  VariableStatData *vardata, Node **other,
142  bool *varonleft);
143 extern void get_join_variables(PlannerInfo *root, List *args,
144  SpecialJoinInfo *sjinfo,
145  VariableStatData *vardata1,
146  VariableStatData *vardata2,
147  bool *join_is_reversed);
148 extern double get_variable_numdistinct(VariableStatData *vardata,
149  bool *isdefault);
150 extern double mcv_selectivity(VariableStatData *vardata,
151  FmgrInfo *opproc, Oid collation,
152  Datum constval, bool varonleft,
153  double *sumcommonp);
154 extern double histogram_selectivity(VariableStatData *vardata,
155  FmgrInfo *opproc, Oid collation,
156  Datum constval, bool varonleft,
157  int min_hist_size, int n_skip,
158  int *hist_size);
159 extern double generic_restriction_selectivity(PlannerInfo *root,
160  Oid oproid, Oid collation,
161  List *args, int varRelid,
162  double default_selectivity);
163 extern double ineq_histogram_selectivity(PlannerInfo *root,
164  VariableStatData *vardata,
165  Oid opoid, FmgrInfo *opproc,
166  bool isgt, bool iseq,
167  Oid collation,
168  Datum constval, Oid consttype);
169 extern double var_eq_const(VariableStatData *vardata,
170  Oid oproid, Oid collation,
171  Datum constval, bool constisnull,
172  bool varonleft, bool negate);
173 extern double var_eq_non_const(VariableStatData *vardata,
174  Oid oproid, Oid collation,
175  Node *other,
176  bool varonleft, bool negate);
177 
178 extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
179 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
180  Node *arg, int varRelid,
181  JoinType jointype, SpecialJoinInfo *sjinfo);
182 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
183  Node *arg, int varRelid,
184  JoinType jointype, SpecialJoinInfo *sjinfo);
186  ScalarArrayOpExpr *clause,
187  bool is_join_clause,
188  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
189 extern int estimate_array_length(Node *arrayexpr);
191  RowCompareExpr *clause,
192  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
193 
194 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
195  Oid opfamily, int strategy, bool nulls_first,
196  Selectivity *leftstart, Selectivity *leftend,
197  Selectivity *rightstart, Selectivity *rightend);
198 
199 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
200  double input_rows, List **pgset);
201 
202 extern void estimate_hash_bucket_stats(PlannerInfo *root,
203  Node *hashkey, double nbuckets,
204  Selectivity *mcv_freq,
205  Selectivity *bucketsize_frac);
206 extern double estimate_hashagg_tablesize(PlannerInfo *root, Path *path,
207  const AggClauseCosts *agg_costs,
208  double dNumGroups);
209 
210 extern List *get_quals_from_indexclauses(List *indexclauses);
212  List *indexquals);
214  List *indexQuals);
215 extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
216  double loop_count,
217  GenericCosts *costs);
218 
219 /* Functions in array_selfuncs.c */
220 
222  Node *leftop, Node *rightop,
223  Oid elemtype, bool isEquality, bool useOr,
224  int varRelid);
225 
226 #endif /* SELFUNCS_H */
Selectivity indexSelectivity
Definition: selfuncs.h:112
Definition: fmgr.h:56
HeapTuple statsTuple
Definition: selfuncs.h:77
Selectivity rowcomparesel(PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:2170
PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
Definition: selfuncs.c:144
double estimate_hashagg_tablesize(PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: selfuncs.c:3842
BoolTestType
Definition: primnodes.h:1258
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3360
RelOptInfo * rel
Definition: selfuncs.h:76
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5249
Definition: nodes.h:533
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:79
Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1813
double Selectivity
Definition: nodes.h:666
NullTestType
Definition: primnodes.h:1235
unsigned int Oid
Definition: postgres_ext.h:31
int32 atttypmod
Definition: selfuncs.h:82
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4729
#define PGDLLIMPORT
Definition: c.h:1317
signed int int32
Definition: c.h:429
double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq, Oid collation, Datum constval, Oid consttype)
Definition: selfuncs.c:1038
JoinType
Definition: nodes.h:700
Definition: type.h:89
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition: selfuncs.c:1509
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
List * get_quals_from_indexclauses(List *indexclauses)
Definition: selfuncs.c:5918
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4607
bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
Definition: selfuncs.h:128
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6220
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:729
double num_sa_scans
Definition: selfuncs.h:119
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1695
bool(* get_relation_stats_hook_type)(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)
Definition: selfuncs.h:123
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2132
Cost indexTotalCost
Definition: selfuncs.h:111
double var_eq_non_const(VariableStatData *vardata, Oid oproid, Oid collation, Node *other, bool varonleft, bool negate)
Definition: selfuncs.c:463
double generic_restriction_selectivity(PlannerInfo *root, Oid oproid, Oid collation, List *args, int varRelid, double default_selectivity)
Definition: selfuncs.c:911
double indexCorrelation
Definition: selfuncs.h:113
double var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition: selfuncs.c:292
uintptr_t Datum
Definition: postgres.h:367
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:5278
double spc_random_page_cost
Definition: selfuncs.h:118
struct VariableStatData VariableStatData
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:145
int16 attnum
Definition: pg_attribute.h:83
double numIndexTuples
Definition: selfuncs.h:117
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1537
void estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
Definition: selfuncs.c:3723
Cost indexStartupCost
Definition: selfuncs.h:110
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:820
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, GenericCosts *costs)
Definition: selfuncs.c:6002
void * arg
void mergejoinscansel(PlannerInfo *root, Node *clause, Oid opfamily, int strategy, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
Definition: selfuncs.c:2904
Cost index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
Definition: selfuncs.c:5948
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
double Cost
Definition: nodes.h:667
unsigned char bool
Definition: c.h:391
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition: selfuncs.c:4667
double numIndexPages
Definition: selfuncs.h:116