1 /*-------------------------------------------------------------------------
4 * Selectivity functions and index cost estimation functions for
5 * standard operators and index access methods.
8 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/include/utils/selfuncs.h
13 *-------------------------------------------------------------------------
19 #include "access/htup.h"
20 #include "nodes/relation.h"
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
33 /* default selectivity estimate for equalities such as "A = b" */
34 #define DEFAULT_EQ_SEL 0.005
36 /* default selectivity estimate for inequalities such as "A < b" */
37 #define DEFAULT_INEQ_SEL 0.3333333333333333
39 /* default selectivity estimate for range inequalities "A > b AND A < c" */
40 #define DEFAULT_RANGE_INEQ_SEL 0.005
42 /* default selectivity estimate for pattern-match operators such as LIKE */
43 #define DEFAULT_MATCH_SEL 0.005
45 /* default number of distinct values in a table */
46 #define DEFAULT_NUM_DISTINCT 200
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)
54 * Clamp a computed probability estimate (which may suffer from roundoff or
55 * estimation errors) to valid range. Argument must be a float variable.
57 #define CLAMP_PROBABILITY(p) \
66 /* Return data from examine_variable and friends */
67 typedef struct VariableStatData
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; /* type to pass to get_attstatsslot */
76 int32 atttypmod; /* typmod to pass to get_attstatsslot */
77 bool isunique; /* matches unique index or DISTINCT clause */
80 #define ReleaseVariableStats(vardata) \
82 if (HeapTupleIsValid((vardata).statsTuple)) \
83 (* (vardata).freefunc) ((vardata).statsTuple); \
89 Pattern_Type_Like, Pattern_Type_Like_IC,
90 Pattern_Type_Regex, Pattern_Type_Regex_IC
95 Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact
96 } Pattern_Prefix_Status;
98 /* Hooks for plugins to get control when we ask for stats */
99 typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
102 VariableStatData *vardata);
103 extern PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook;
104 typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
106 AttrNumber indexattnum,
107 VariableStatData *vardata);
108 extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
110 /* Functions in selfuncs.c */
112 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
113 VariableStatData *vardata);
114 extern bool get_restriction_variable(PlannerInfo *root, List *args,
116 VariableStatData *vardata, Node **other,
118 extern void get_join_variables(PlannerInfo *root, List *args,
119 SpecialJoinInfo *sjinfo,
120 VariableStatData *vardata1,
121 VariableStatData *vardata2,
122 bool *join_is_reversed);
123 extern double get_variable_numdistinct(VariableStatData *vardata,
125 extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
126 Datum constval, bool varonleft,
128 extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
129 Datum constval, bool varonleft,
130 int min_hist_size, int n_skip,
133 extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
137 Selectivity *rest_selec);
138 extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc,
141 extern Datum eqsel(PG_FUNCTION_ARGS);
142 extern Datum neqsel(PG_FUNCTION_ARGS);
143 extern Datum scalarltsel(PG_FUNCTION_ARGS);
144 extern Datum scalargtsel(PG_FUNCTION_ARGS);
145 extern Datum regexeqsel(PG_FUNCTION_ARGS);
146 extern Datum icregexeqsel(PG_FUNCTION_ARGS);
147 extern Datum likesel(PG_FUNCTION_ARGS);
148 extern Datum iclikesel(PG_FUNCTION_ARGS);
149 extern Datum regexnesel(PG_FUNCTION_ARGS);
150 extern Datum icregexnesel(PG_FUNCTION_ARGS);
151 extern Datum nlikesel(PG_FUNCTION_ARGS);
152 extern Datum icnlikesel(PG_FUNCTION_ARGS);
154 extern Datum eqjoinsel(PG_FUNCTION_ARGS);
155 extern Datum neqjoinsel(PG_FUNCTION_ARGS);
156 extern Datum scalarltjoinsel(PG_FUNCTION_ARGS);
157 extern Datum scalargtjoinsel(PG_FUNCTION_ARGS);
158 extern Datum regexeqjoinsel(PG_FUNCTION_ARGS);
159 extern Datum icregexeqjoinsel(PG_FUNCTION_ARGS);
160 extern Datum likejoinsel(PG_FUNCTION_ARGS);
161 extern Datum iclikejoinsel(PG_FUNCTION_ARGS);
162 extern Datum regexnejoinsel(PG_FUNCTION_ARGS);
163 extern Datum icregexnejoinsel(PG_FUNCTION_ARGS);
164 extern Datum nlikejoinsel(PG_FUNCTION_ARGS);
165 extern Datum icnlikejoinsel(PG_FUNCTION_ARGS);
167 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
168 Node *arg, int varRelid,
169 JoinType jointype, SpecialJoinInfo *sjinfo);
170 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
171 Node *arg, int varRelid,
172 JoinType jointype, SpecialJoinInfo *sjinfo);
173 extern Selectivity scalararraysel(PlannerInfo *root,
174 ScalarArrayOpExpr *clause,
176 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
177 extern int estimate_array_length(Node *arrayexpr);
178 extern Selectivity rowcomparesel(PlannerInfo *root,
179 RowCompareExpr *clause,
180 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
182 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
183 Oid opfamily, int strategy, bool nulls_first,
184 Selectivity *leftstart, Selectivity *leftend,
185 Selectivity *rightstart, Selectivity *rightend);
187 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
190 extern Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey,
193 extern Datum btcostestimate(PG_FUNCTION_ARGS);
194 extern Datum hashcostestimate(PG_FUNCTION_ARGS);
195 extern Datum gistcostestimate(PG_FUNCTION_ARGS);
196 extern Datum spgcostestimate(PG_FUNCTION_ARGS);
197 extern Datum gincostestimate(PG_FUNCTION_ARGS);
199 /* Functions in array_selfuncs.c */
201 extern Selectivity scalararraysel_containment(PlannerInfo *root,
202 Node *leftop, Node *rightop,
203 Oid elemtype, bool isEquality, bool useOr,
205 extern Datum arraycontsel(PG_FUNCTION_ARGS);
206 extern Datum arraycontjoinsel(PG_FUNCTION_ARGS);
208 #endif /* SELFUNCS_H */