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-2020, 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 pattern-match operators such as LIKE */
43 #define DEFAULT_MATCH_SEL 0.005
44 
45 /* default selectivity estimate for other matching operators */
46 #define DEFAULT_MATCHING_SEL 0.010
47 
48 /* default number of distinct values in a table */
49 #define DEFAULT_NUM_DISTINCT 200
50 
51 /* default selectivity estimate for boolean and null test nodes */
52 #define DEFAULT_UNK_SEL 0.005
53 #define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
54 
55 
56 /*
57  * Clamp a computed probability estimate (which may suffer from roundoff or
58  * estimation errors) to valid range. Argument must be a float variable.
59  */
60 #define CLAMP_PROBABILITY(p) \
61  do { \
62  if (p < 0.0) \
63  p = 0.0; \
64  else if (p > 1.0) \
65  p = 1.0; \
66  } while (0)
67 
68 
69 /* Return data from examine_variable and friends */
70 typedef struct VariableStatData
71 {
72  Node *var; /* the Var or expression tree */
73  RelOptInfo *rel; /* Relation, or NULL if not identifiable */
74  HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
75  /* NB: if statsTuple!=NULL, it must be freed when caller is done */
76  void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
77  Oid vartype; /* exposed type of expression */
78  Oid atttype; /* actual type (after stripping relabel) */
79  int32 atttypmod; /* actual typmod (after stripping relabel) */
80  bool isunique; /* matches unique index or DISTINCT clause */
81  bool acl_ok; /* result of ACL check on table or column */
83 
84 #define ReleaseVariableStats(vardata) \
85  do { \
86  if (HeapTupleIsValid((vardata).statsTuple)) \
87  (vardata).freefunc((vardata).statsTuple); \
88  } while(0)
89 
90 
91 /*
92  * genericcostestimate is a general-purpose estimator that can be used for
93  * most index types. In some cases we use genericcostestimate as the base
94  * code and then incorporate additional index-type-specific knowledge in
95  * the type-specific calling function. To avoid code duplication, we make
96  * genericcostestimate return a number of intermediate values as well as
97  * its preliminary estimates of the output cost values. The GenericCosts
98  * struct includes all these values.
99  *
100  * Callers should initialize all fields of GenericCosts to zero. In addition,
101  * they can set numIndexTuples to some positive value if they have a better
102  * than default way of estimating the number of leaf index tuples visited.
103  */
104 typedef struct
105 {
106  /* These are the values the cost estimator must return to the planner */
107  Cost indexStartupCost; /* index-related startup cost */
108  Cost indexTotalCost; /* total index-related scan cost */
109  Selectivity indexSelectivity; /* selectivity of index */
110  double indexCorrelation; /* order correlation of index */
111 
112  /* Intermediate values we obtain along the way */
113  double numIndexPages; /* number of leaf pages visited */
114  double numIndexTuples; /* number of leaf tuples visited */
115  double spc_random_page_cost; /* relevant random_page_cost value */
116  double num_sa_scans; /* # indexscans from ScalarArrayOpExprs */
117 } GenericCosts;
118 
119 /* Hooks for plugins to get control when we ask for stats */
121  RangeTblEntry *rte,
123  VariableStatData *vardata);
126  Oid indexOid,
127  AttrNumber indexattnum,
128  VariableStatData *vardata);
130 
131 /* Functions in selfuncs.c */
132 
133 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
134  VariableStatData *vardata);
135 extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
136 extern bool get_restriction_variable(PlannerInfo *root, List *args,
137  int varRelid,
138  VariableStatData *vardata, Node **other,
139  bool *varonleft);
140 extern void get_join_variables(PlannerInfo *root, List *args,
141  SpecialJoinInfo *sjinfo,
142  VariableStatData *vardata1,
143  VariableStatData *vardata2,
144  bool *join_is_reversed);
145 extern double get_variable_numdistinct(VariableStatData *vardata,
146  bool *isdefault);
147 extern double mcv_selectivity(VariableStatData *vardata,
148  FmgrInfo *opproc, Oid collation,
149  Datum constval, bool varonleft,
150  double *sumcommonp);
151 extern double histogram_selectivity(VariableStatData *vardata,
152  FmgrInfo *opproc, Oid collation,
153  Datum constval, bool varonleft,
154  int min_hist_size, int n_skip,
155  int *hist_size);
156 extern double generic_restriction_selectivity(PlannerInfo *root,
157  Oid oproid, Oid collation,
158  List *args, int varRelid,
159  double default_selectivity);
160 extern double ineq_histogram_selectivity(PlannerInfo *root,
161  VariableStatData *vardata,
162  Oid opoid, FmgrInfo *opproc,
163  bool isgt, bool iseq,
164  Oid collation,
165  Datum constval, Oid consttype);
166 extern double var_eq_const(VariableStatData *vardata,
167  Oid oproid, Oid collation,
168  Datum constval, bool constisnull,
169  bool varonleft, bool negate);
170 extern double var_eq_non_const(VariableStatData *vardata,
171  Oid oproid, Oid collation,
172  Node *other,
173  bool varonleft, bool negate);
174 
175 extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
176 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
177  Node *arg, int varRelid,
178  JoinType jointype, SpecialJoinInfo *sjinfo);
179 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
180  Node *arg, int varRelid,
181  JoinType jointype, SpecialJoinInfo *sjinfo);
183  ScalarArrayOpExpr *clause,
184  bool is_join_clause,
185  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
186 extern int estimate_array_length(Node *arrayexpr);
188  RowCompareExpr *clause,
189  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
190 
191 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
192  Oid opfamily, int strategy, bool nulls_first,
193  Selectivity *leftstart, Selectivity *leftend,
194  Selectivity *rightstart, Selectivity *rightend);
195 
196 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
197  double input_rows, List **pgset);
198 
199 extern void estimate_hash_bucket_stats(PlannerInfo *root,
200  Node *hashkey, double nbuckets,
201  Selectivity *mcv_freq,
202  Selectivity *bucketsize_frac);
203 extern double estimate_hashagg_tablesize(Path *path,
204  const AggClauseCosts *agg_costs,
205  double dNumGroups);
206 
207 extern List *get_quals_from_indexclauses(List *indexclauses);
209  List *indexquals);
211  List *indexQuals);
212 extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
213  double loop_count,
214  GenericCosts *costs);
215 
216 /* Functions in array_selfuncs.c */
217 
219  Node *leftop, Node *rightop,
220  Oid elemtype, bool isEquality, bool useOr,
221  int varRelid);
222 
223 #endif /* SELFUNCS_H */
Selectivity indexSelectivity
Definition: selfuncs.h:109
Definition: fmgr.h:56
HeapTuple statsTuple
Definition: selfuncs.h:74
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
BoolTestType
Definition: primnodes.h:1237
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3360
RelOptInfo * rel
Definition: selfuncs.h:73
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5247
Definition: nodes.h:528
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:76
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:661
NullTestType
Definition: primnodes.h:1214
unsigned int Oid
Definition: postgres_ext.h:31
int32 atttypmod
Definition: selfuncs.h:79
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4727
#define PGDLLIMPORT
Definition: c.h:1259
signed int int32
Definition: c.h:363
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:695
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:5916
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4605
bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
Definition: selfuncs.h:125
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:6218
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:116
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:120
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2132
double estimate_hashagg_tablesize(Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: selfuncs.c:3842
Cost indexTotalCost
Definition: selfuncs.h:108
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:110
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:5276
double spc_random_page_cost
Definition: selfuncs.h:115
struct VariableStatData VariableStatData
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:145
int16 attnum
Definition: pg_attribute.h:79
double numIndexTuples
Definition: selfuncs.h:114
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:107
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:6000
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:5946
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
double Cost
Definition: nodes.h:662
unsigned char bool
Definition: c.h:325
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition: selfuncs.c:4665
double numIndexPages
Definition: selfuncs.h:113