]> granicus.if.org Git - postgresql/blob - src/include/utils/selfuncs.h
Add new to_reg* functions for error-free OID lookups.
[postgresql] / src / include / utils / selfuncs.h
1 /*-------------------------------------------------------------------------
2  *
3  * selfuncs.h
4  *        Selectivity functions and index cost estimation functions for
5  *        standard operators and index access methods.
6  *
7  *
8  * Portions Copyright (c) 1996-2014, 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;                /* type to pass to get_attstatsslot */
76         int32           atttypmod;              /* typmod to pass to get_attstatsslot */
77         bool            isunique;               /* matches unique index or DISTINCT clause */
78 } VariableStatData;
79
80 #define ReleaseVariableStats(vardata)  \
81         do { \
82                 if (HeapTupleIsValid((vardata).statsTuple)) \
83                         (* (vardata).freefunc) ((vardata).statsTuple); \
84         } while(0)
85
86
87 typedef enum
88 {
89         Pattern_Type_Like, Pattern_Type_Like_IC,
90         Pattern_Type_Regex, Pattern_Type_Regex_IC
91 } Pattern_Type;
92
93 typedef enum
94 {
95         Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact
96 } Pattern_Prefix_Status;
97
98 /* Hooks for plugins to get control when we ask for stats */
99 typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
100                                                                                                                   RangeTblEntry *rte,
101                                                                                                                   AttrNumber attnum,
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,
105                                                                                                            Oid indexOid,
106                                                                                                            AttrNumber indexattnum,
107                                                                                                   VariableStatData *vardata);
108 extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
109
110 /* Functions in selfuncs.c */
111
112 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
113                                  VariableStatData *vardata);
114 extern bool get_restriction_variable(PlannerInfo *root, List *args,
115                                                  int varRelid,
116                                                  VariableStatData *vardata, Node **other,
117                                                  bool *varonleft);
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,
124                                                  bool *isdefault);
125 extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
126                                 Datum constval, bool varonleft,
127                                 double *sumcommonp);
128 extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
129                                           Datum constval, bool varonleft,
130                                           int min_hist_size, int n_skip,
131                                           int *hist_size);
132
133 extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
134                                          Pattern_Type ptype,
135                                          Oid collation,
136                                          Const **prefix,
137                                          Selectivity *rest_selec);
138 extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc,
139                                         Oid collation);
140
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);
153
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);
166
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,
175                            bool is_join_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);
181
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);
186
187 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
188                                         double input_rows);
189
190 extern Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey,
191                                                  double nbuckets);
192
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);
198
199 /* Functions in array_selfuncs.c */
200
201 extern Selectivity scalararraysel_containment(PlannerInfo *root,
202                                                    Node *leftop, Node *rightop,
203                                                    Oid elemtype, bool isEquality, bool useOr,
204                                                    int varRelid);
205 extern Datum arraycontsel(PG_FUNCTION_ARGS);
206 extern Datum arraycontjoinsel(PG_FUNCTION_ARGS);
207
208 #endif   /* SELFUNCS_H */