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-2018, 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 "fmgr.h"
19 #include "access/htup.h"
20 #include "nodes/relation.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 number of distinct values in a table */
46 #define DEFAULT_NUM_DISTINCT 200
47 
48 /* default selectivity estimate for boolean and null test nodes */
49 #define DEFAULT_UNK_SEL 0.005
50 #define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
51 
52 
53 /*
54  * Clamp a computed probability estimate (which may suffer from roundoff or
55  * estimation errors) to valid range. Argument must be a float variable.
56  */
57 #define CLAMP_PROBABILITY(p) \
58  do { \
59  if (p < 0.0) \
60  p = 0.0; \
61  else if (p > 1.0) \
62  p = 1.0; \
63  } while (0)
64 
65 
66 /* Return data from examine_variable and friends */
67 typedef struct VariableStatData
68 {
69  Node *var; /* the Var or expression tree */
70  RelOptInfo *rel; /* Relation, or NULL if not identifiable */
71  HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
72  /* NB: if statsTuple!=NULL, it must be freed when caller is done */
73  void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
74  Oid vartype; /* exposed type of expression */
75  Oid atttype; /* actual type (after stripping relabel) */
76  int32 atttypmod; /* actual typmod (after stripping relabel) */
77  bool isunique; /* matches unique index or DISTINCT clause */
78  bool acl_ok; /* result of ACL check on table or column */
80 
81 #define ReleaseVariableStats(vardata) \
82  do { \
83  if (HeapTupleIsValid((vardata).statsTuple)) \
84  (vardata).freefunc((vardata).statsTuple); \
85  } while(0)
86 
87 
88 typedef enum
89 {
95 } Pattern_Type;
96 
97 typedef enum
98 {
101 
102 /*
103  * deconstruct_indexquals is a simple function to examine the indexquals
104  * attached to a proposed IndexPath. It returns a list of IndexQualInfo
105  * structs, one per qual expression.
106  */
107 typedef struct
108 {
109  RestrictInfo *rinfo; /* the indexqual itself */
110  int indexcol; /* zero-based index column number */
111  bool varonleft; /* true if index column is on left of qual */
112  Oid clause_op; /* qual's operator OID, if relevant */
113  Node *other_operand; /* non-index operand of qual's operator */
114 } IndexQualInfo;
115 
116 /*
117  * genericcostestimate is a general-purpose estimator that can be used for
118  * most index types. In some cases we use genericcostestimate as the base
119  * code and then incorporate additional index-type-specific knowledge in
120  * the type-specific calling function. To avoid code duplication, we make
121  * genericcostestimate return a number of intermediate values as well as
122  * its preliminary estimates of the output cost values. The GenericCosts
123  * struct includes all these values.
124  *
125  * Callers should initialize all fields of GenericCosts to zero. In addition,
126  * they can set numIndexTuples to some positive value if they have a better
127  * than default way of estimating the number of leaf index tuples visited.
128  */
129 typedef struct
130 {
131  /* These are the values the cost estimator must return to the planner */
132  Cost indexStartupCost; /* index-related startup cost */
133  Cost indexTotalCost; /* total index-related scan cost */
134  Selectivity indexSelectivity; /* selectivity of index */
135  double indexCorrelation; /* order correlation of index */
136 
137  /* Intermediate values we obtain along the way */
138  double numIndexPages; /* number of leaf pages visited */
139  double numIndexTuples; /* number of leaf tuples visited */
140  double spc_random_page_cost; /* relevant random_page_cost value */
141  double num_sa_scans; /* # indexscans from ScalarArrayOps */
142 } GenericCosts;
143 
144 /* Hooks for plugins to get control when we ask for stats */
146  RangeTblEntry *rte,
148  VariableStatData *vardata);
151  Oid indexOid,
152  AttrNumber indexattnum,
153  VariableStatData *vardata);
155 
156 /* Functions in selfuncs.c */
157 
158 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
159  VariableStatData *vardata);
160 extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
161 extern bool get_restriction_variable(PlannerInfo *root, List *args,
162  int varRelid,
163  VariableStatData *vardata, Node **other,
164  bool *varonleft);
165 extern void get_join_variables(PlannerInfo *root, List *args,
166  SpecialJoinInfo *sjinfo,
167  VariableStatData *vardata1,
168  VariableStatData *vardata2,
169  bool *join_is_reversed);
170 extern double get_variable_numdistinct(VariableStatData *vardata,
171  bool *isdefault);
172 extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
173  Datum constval, bool varonleft,
174  double *sumcommonp);
175 extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
176  Datum constval, bool varonleft,
177  int min_hist_size, int n_skip,
178  int *hist_size);
179 
181  Pattern_Type ptype,
182  Oid collation,
183  Const **prefix,
184  Selectivity *rest_selec);
185 extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc,
186  Oid collation);
187 
188 extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
189 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
190  Node *arg, int varRelid,
191  JoinType jointype, SpecialJoinInfo *sjinfo);
192 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
193  Node *arg, int varRelid,
194  JoinType jointype, SpecialJoinInfo *sjinfo);
196  ScalarArrayOpExpr *clause,
197  bool is_join_clause,
198  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
199 extern int estimate_array_length(Node *arrayexpr);
201  RowCompareExpr *clause,
202  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
203 
204 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
205  Oid opfamily, int strategy, bool nulls_first,
206  Selectivity *leftstart, Selectivity *leftend,
207  Selectivity *rightstart, Selectivity *rightend);
208 
209 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
210  double input_rows, List **pgset);
211 
212 extern void estimate_hash_bucket_stats(PlannerInfo *root,
213  Node *hashkey, double nbuckets,
214  Selectivity *mcv_freq,
215  Selectivity *bucketsize_frac);
216 
217 extern List *deconstruct_indexquals(IndexPath *path);
218 extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
219  double loop_count,
220  List *qinfos,
221  GenericCosts *costs);
222 
223 /* Functions in array_selfuncs.c */
224 
226  Node *leftop, Node *rightop,
227  Oid elemtype, bool isEquality, bool useOr,
228  int varRelid);
229 
230 #endif /* SELFUNCS_H */
Selectivity indexSelectivity
Definition: selfuncs.h:134
Definition: fmgr.h:56
HeapTuple statsTuple
Definition: selfuncs.h:71
Selectivity rowcomparesel(PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:2217
PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
Definition: selfuncs.c:155
BoolTestType
Definition: primnodes.h:1203
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3419
RelOptInfo * rel
Definition: selfuncs.h:70
Pattern_Type
Definition: selfuncs.h:88
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:5154
Definition: nodes.h:517
void(* freefunc)(HeapTuple tuple)
Definition: selfuncs.h:73
Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1860
double Selectivity
Definition: nodes.h:647
NullTestType
Definition: primnodes.h:1180
unsigned int Oid
Definition: postgres_ext.h:31
List * deconstruct_indexquals(IndexPath *path)
Definition: selfuncs.c:6552
int32 atttypmod
Definition: selfuncs.h:76
RestrictInfo * rinfo
Definition: selfuncs.h:109
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4776
#define PGDLLIMPORT
Definition: c.h:1105
char bool
Definition: c.h:275
signed int int32
Definition: c.h:313
JoinType
Definition: nodes.h:681
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition: selfuncs.c:1554
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4654
bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
Definition: selfuncs.h:150
double num_sa_scans
Definition: selfuncs.h:141
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1751
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:711
bool(* get_relation_stats_hook_type)(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)
Definition: selfuncs.h:145
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:2179
Cost indexTotalCost
Definition: selfuncs.h:133
Const * make_greater_string(const Const *str_const, FmgrInfo *ltproc, Oid collation)
Definition: selfuncs.c:6326
double indexCorrelation
Definition: selfuncs.h:135
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:639
uintptr_t Datum
Definition: postgres.h:365
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:5183
bool varonleft
Definition: selfuncs.h:111
double spc_random_page_cost
Definition: selfuncs.h:140
Pattern_Prefix_Status pattern_fixed_prefix(Const *patt, Pattern_Type ptype, Oid collation, Const **prefix, Selectivity *rest_selec)
Definition: selfuncs.c:5946
struct VariableStatData VariableStatData
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:156
int16 attnum
Definition: pg_attribute.h:79
double numIndexTuples
Definition: selfuncs.h:139
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1593
void estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
Definition: selfuncs.c:3782
Cost indexStartupCost
Definition: selfuncs.h:132
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, List *qinfos, GenericCosts *costs)
Definition: selfuncs.c:6701
Node * other_operand
Definition: selfuncs.h:113
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:2995
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
double Cost
Definition: nodes.h:648
Pattern_Prefix_Status
Definition: selfuncs.h:97
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition: selfuncs.c:4714
double numIndexPages
Definition: selfuncs.h:138