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-2019, 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 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 /*
89  * genericcostestimate is a general-purpose estimator that can be used for
90  * most index types. In some cases we use genericcostestimate as the base
91  * code and then incorporate additional index-type-specific knowledge in
92  * the type-specific calling function. To avoid code duplication, we make
93  * genericcostestimate return a number of intermediate values as well as
94  * its preliminary estimates of the output cost values. The GenericCosts
95  * struct includes all these values.
96  *
97  * Callers should initialize all fields of GenericCosts to zero. In addition,
98  * they can set numIndexTuples to some positive value if they have a better
99  * than default way of estimating the number of leaf index tuples visited.
100  */
101 typedef struct
102 {
103  /* These are the values the cost estimator must return to the planner */
104  Cost indexStartupCost; /* index-related startup cost */
105  Cost indexTotalCost; /* total index-related scan cost */
106  Selectivity indexSelectivity; /* selectivity of index */
107  double indexCorrelation; /* order correlation of index */
108 
109  /* Intermediate values we obtain along the way */
110  double numIndexPages; /* number of leaf pages visited */
111  double numIndexTuples; /* number of leaf tuples visited */
112  double spc_random_page_cost; /* relevant random_page_cost value */
113  double num_sa_scans; /* # indexscans from ScalarArrayOpExprs */
114 } GenericCosts;
115 
116 /* Hooks for plugins to get control when we ask for stats */
118  RangeTblEntry *rte,
120  VariableStatData *vardata);
123  Oid indexOid,
124  AttrNumber indexattnum,
125  VariableStatData *vardata);
127 
128 /* Functions in selfuncs.c */
129 
130 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
131  VariableStatData *vardata);
132 extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
133 extern bool get_restriction_variable(PlannerInfo *root, List *args,
134  int varRelid,
135  VariableStatData *vardata, Node **other,
136  bool *varonleft);
137 extern void get_join_variables(PlannerInfo *root, List *args,
138  SpecialJoinInfo *sjinfo,
139  VariableStatData *vardata1,
140  VariableStatData *vardata2,
141  bool *join_is_reversed);
142 extern double get_variable_numdistinct(VariableStatData *vardata,
143  bool *isdefault);
144 extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
145  Datum constval, bool varonleft,
146  double *sumcommonp);
147 extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
148  Datum constval, bool varonleft,
149  int min_hist_size, int n_skip,
150  int *hist_size);
151 extern double ineq_histogram_selectivity(PlannerInfo *root,
152  VariableStatData *vardata,
153  FmgrInfo *opproc, bool isgt, bool iseq,
154  Datum constval, Oid consttype);
155 extern double var_eq_const(VariableStatData *vardata, Oid oproid,
156  Datum constval, bool constisnull,
157  bool varonleft, bool negate);
158 extern double var_eq_non_const(VariableStatData *vardata, Oid oproid,
159  Node *other,
160  bool varonleft, bool negate);
161 
162 extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
163 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
164  Node *arg, int varRelid,
165  JoinType jointype, SpecialJoinInfo *sjinfo);
166 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
167  Node *arg, int varRelid,
168  JoinType jointype, SpecialJoinInfo *sjinfo);
170  ScalarArrayOpExpr *clause,
171  bool is_join_clause,
172  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
173 extern int estimate_array_length(Node *arrayexpr);
175  RowCompareExpr *clause,
176  int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
177 
178 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
179  Oid opfamily, int strategy, bool nulls_first,
180  Selectivity *leftstart, Selectivity *leftend,
181  Selectivity *rightstart, Selectivity *rightend);
182 
183 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
184  double input_rows, List **pgset);
185 
186 extern void estimate_hash_bucket_stats(PlannerInfo *root,
187  Node *hashkey, double nbuckets,
188  Selectivity *mcv_freq,
189  Selectivity *bucketsize_frac);
190 extern double estimate_hashagg_tablesize(Path *path,
191  const AggClauseCosts *agg_costs,
192  double dNumGroups);
193 
194 extern List *get_quals_from_indexclauses(List *indexclauses);
196  List *indexquals);
198  List *indexQuals);
199 extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
200  double loop_count,
201  GenericCosts *costs);
202 
203 /* Functions in array_selfuncs.c */
204 
206  Node *leftop, Node *rightop,
207  Oid elemtype, bool isEquality, bool useOr,
208  int varRelid);
209 
210 #endif /* SELFUNCS_H */
Selectivity indexSelectivity
Definition: selfuncs.h:106
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:1928
PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
Definition: selfuncs.c:146
double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, FmgrInfo *opproc, bool isgt, bool iseq, Datum constval, Oid consttype)
Definition: selfuncs.c:846
BoolTestType
Definition: primnodes.h:1220
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3043
RelOptInfo * rel
Definition: selfuncs.h:70
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition: selfuncs.c:4821
Definition: nodes.h:525
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:1571
double Selectivity
Definition: nodes.h:658
NullTestType
Definition: primnodes.h:1197
unsigned int Oid
Definition: postgres_ext.h:31
int32 atttypmod
Definition: selfuncs.h:76
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition: selfuncs.c:4417
#define PGDLLIMPORT
Definition: c.h:1266
signed int int32
Definition: c.h:347
JoinType
Definition: nodes.h:692
Definition: type.h:89
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition: selfuncs.c:1267
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:5449
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4295
bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
Definition: selfuncs.h:122
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition: selfuncs.c:5751
double num_sa_scans
Definition: selfuncs.h:113
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1453
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:778
bool(* get_relation_stats_hook_type)(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)
Definition: selfuncs.h:117
int estimate_array_length(Node *arrayexpr)
Definition: selfuncs.c:1890
double estimate_hashagg_tablesize(Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition: selfuncs.c:3525
Cost indexTotalCost
Definition: selfuncs.h:105
double indexCorrelation
Definition: selfuncs.h:107
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:706
uintptr_t Datum
Definition: postgres.h:367
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition: selfuncs.c:4850
double var_eq_non_const(VariableStatData *vardata, Oid oproid, Node *other, bool varonleft, bool negate)
Definition: selfuncs.c:442
double spc_random_page_cost
Definition: selfuncs.h:112
struct VariableStatData VariableStatData
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
Definition: selfuncs.c:147
int16 attnum
Definition: pg_attribute.h:79
double numIndexTuples
Definition: selfuncs.h:111
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition: selfuncs.c:1295
void estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
Definition: selfuncs.c:3406
Cost indexStartupCost
Definition: selfuncs.h:104
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, GenericCosts *costs)
Definition: selfuncs.c:5533
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:2624
Cost index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
Definition: selfuncs.c:5479
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
double Cost
Definition: nodes.h:659
double var_eq_const(VariableStatData *vardata, Oid oproid, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition: selfuncs.c:288
unsigned char bool
Definition: c.h:309
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition: selfuncs.c:4355
double numIndexPages
Definition: selfuncs.h:110