1
/*-------------------------------------------------------------------------
4
* Selectivity functions and index cost estimation functions for
5
* standard operators and index access methods.
8
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
9
* Portions Copyright (c) 1994, Regents of the University of California
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; /* true if matched to a unique index */
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;
101
/* Hooks for plugins to get control when we ask for stats */
102
typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
105
VariableStatData *vardata);
106
extern PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook;
107
typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
109
AttrNumber indexattnum,
110
VariableStatData *vardata);
111
extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
113
extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
114
VariableStatData *vardata);
115
extern bool get_restriction_variable(PlannerInfo *root, List *args,
117
VariableStatData *vardata, Node **other,
119
extern void get_join_variables(PlannerInfo *root, List *args,
120
SpecialJoinInfo *sjinfo,
121
VariableStatData *vardata1,
122
VariableStatData *vardata2,
123
bool *join_is_reversed);
124
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
extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc);
139
extern Datum eqsel(PG_FUNCTION_ARGS);
140
extern Datum neqsel(PG_FUNCTION_ARGS);
141
extern Datum scalarltsel(PG_FUNCTION_ARGS);
142
extern Datum scalargtsel(PG_FUNCTION_ARGS);
143
extern Datum regexeqsel(PG_FUNCTION_ARGS);
144
extern Datum icregexeqsel(PG_FUNCTION_ARGS);
145
extern Datum likesel(PG_FUNCTION_ARGS);
146
extern Datum iclikesel(PG_FUNCTION_ARGS);
147
extern Datum regexnesel(PG_FUNCTION_ARGS);
148
extern Datum icregexnesel(PG_FUNCTION_ARGS);
149
extern Datum nlikesel(PG_FUNCTION_ARGS);
150
extern Datum icnlikesel(PG_FUNCTION_ARGS);
152
extern Datum eqjoinsel(PG_FUNCTION_ARGS);
153
extern Datum neqjoinsel(PG_FUNCTION_ARGS);
154
extern Datum scalarltjoinsel(PG_FUNCTION_ARGS);
155
extern Datum scalargtjoinsel(PG_FUNCTION_ARGS);
156
extern Datum regexeqjoinsel(PG_FUNCTION_ARGS);
157
extern Datum icregexeqjoinsel(PG_FUNCTION_ARGS);
158
extern Datum likejoinsel(PG_FUNCTION_ARGS);
159
extern Datum iclikejoinsel(PG_FUNCTION_ARGS);
160
extern Datum regexnejoinsel(PG_FUNCTION_ARGS);
161
extern Datum icregexnejoinsel(PG_FUNCTION_ARGS);
162
extern Datum nlikejoinsel(PG_FUNCTION_ARGS);
163
extern Datum icnlikejoinsel(PG_FUNCTION_ARGS);
165
extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
166
Node *arg, int varRelid,
167
JoinType jointype, SpecialJoinInfo *sjinfo);
168
extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
169
Node *arg, int varRelid,
170
JoinType jointype, SpecialJoinInfo *sjinfo);
171
extern Selectivity scalararraysel(PlannerInfo *root,
172
ScalarArrayOpExpr *clause,
174
int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
175
extern int estimate_array_length(Node *arrayexpr);
176
extern Selectivity rowcomparesel(PlannerInfo *root,
177
RowCompareExpr *clause,
178
int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
180
extern void mergejoinscansel(PlannerInfo *root, Node *clause,
181
Oid opfamily, int strategy, bool nulls_first,
182
Selectivity *leftstart, Selectivity *leftend,
183
Selectivity *rightstart, Selectivity *rightend);
185
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
188
extern Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey,
191
extern Datum btcostestimate(PG_FUNCTION_ARGS);
192
extern Datum hashcostestimate(PG_FUNCTION_ARGS);
193
extern Datum gistcostestimate(PG_FUNCTION_ARGS);
194
extern Datum gincostestimate(PG_FUNCTION_ARGS);
196
#endif /* SELFUNCS_H */