Header And Logo

PostgreSQL
| The world's most advanced open source database.

selfuncs.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * selfuncs.h
00004  *    Selectivity functions and index cost estimation functions for
00005  *    standard operators and index access methods.
00006  *
00007  *
00008  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00009  * Portions Copyright (c) 1994, Regents of the University of California
00010  *
00011  * src/include/utils/selfuncs.h
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 #ifndef SELFUNCS_H
00016 #define SELFUNCS_H
00017 
00018 #include "fmgr.h"
00019 #include "access/htup.h"
00020 #include "nodes/relation.h"
00021 
00022 
00023 /*
00024  * Note: the default selectivity estimates are not chosen entirely at random.
00025  * We want them to be small enough to ensure that indexscans will be used if
00026  * available, for typical table densities of ~100 tuples/page.  Thus, for
00027  * example, 0.01 is not quite small enough, since that makes it appear that
00028  * nearly all pages will be hit anyway.  Also, since we sometimes estimate
00029  * eqsel as 1/num_distinct, we probably want DEFAULT_NUM_DISTINCT to equal
00030  * 1/DEFAULT_EQ_SEL.
00031  */
00032 
00033 /* default selectivity estimate for equalities such as "A = b" */
00034 #define DEFAULT_EQ_SEL  0.005
00035 
00036 /* default selectivity estimate for inequalities such as "A < b" */
00037 #define DEFAULT_INEQ_SEL  0.3333333333333333
00038 
00039 /* default selectivity estimate for range inequalities "A > b AND A < c" */
00040 #define DEFAULT_RANGE_INEQ_SEL  0.005
00041 
00042 /* default selectivity estimate for pattern-match operators such as LIKE */
00043 #define DEFAULT_MATCH_SEL   0.005
00044 
00045 /* default number of distinct values in a table */
00046 #define DEFAULT_NUM_DISTINCT  200
00047 
00048 /* default selectivity estimate for boolean and null test nodes */
00049 #define DEFAULT_UNK_SEL         0.005
00050 #define DEFAULT_NOT_UNK_SEL     (1.0 - DEFAULT_UNK_SEL)
00051 
00052 
00053 /*
00054  * Clamp a computed probability estimate (which may suffer from roundoff or
00055  * estimation errors) to valid range.  Argument must be a float variable.
00056  */
00057 #define CLAMP_PROBABILITY(p) \
00058     do { \
00059         if (p < 0.0) \
00060             p = 0.0; \
00061         else if (p > 1.0) \
00062             p = 1.0; \
00063     } while (0)
00064 
00065 
00066 /* Return data from examine_variable and friends */
00067 typedef struct VariableStatData
00068 {
00069     Node       *var;            /* the Var or expression tree */
00070     RelOptInfo *rel;            /* Relation, or NULL if not identifiable */
00071     HeapTuple   statsTuple;     /* pg_statistic tuple, or NULL if none */
00072     /* NB: if statsTuple!=NULL, it must be freed when caller is done */
00073     void        (*freefunc) (HeapTuple tuple);  /* how to free statsTuple */
00074     Oid         vartype;        /* exposed type of expression */
00075     Oid         atttype;        /* type to pass to get_attstatsslot */
00076     int32       atttypmod;      /* typmod to pass to get_attstatsslot */
00077     bool        isunique;       /* matches unique index or DISTINCT clause */
00078 } VariableStatData;
00079 
00080 #define ReleaseVariableStats(vardata)  \
00081     do { \
00082         if (HeapTupleIsValid((vardata).statsTuple)) \
00083             (* (vardata).freefunc) ((vardata).statsTuple); \
00084     } while(0)
00085 
00086 
00087 typedef enum
00088 {
00089     Pattern_Type_Like, Pattern_Type_Like_IC,
00090     Pattern_Type_Regex, Pattern_Type_Regex_IC
00091 } Pattern_Type;
00092 
00093 typedef enum
00094 {
00095     Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact
00096 } Pattern_Prefix_Status;
00097 
00098 /* Hooks for plugins to get control when we ask for stats */
00099 typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
00100                                                           RangeTblEntry *rte,
00101                                                           AttrNumber attnum,
00102                                                   VariableStatData *vardata);
00103 extern PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook;
00104 typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
00105                                                        Oid indexOid,
00106                                                        AttrNumber indexattnum,
00107                                                   VariableStatData *vardata);
00108 extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
00109 
00110 /* Functions in selfuncs.c */
00111 
00112 extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
00113                  VariableStatData *vardata);
00114 extern bool get_restriction_variable(PlannerInfo *root, List *args,
00115                          int varRelid,
00116                          VariableStatData *vardata, Node **other,
00117                          bool *varonleft);
00118 extern void get_join_variables(PlannerInfo *root, List *args,
00119                    SpecialJoinInfo *sjinfo,
00120                    VariableStatData *vardata1,
00121                    VariableStatData *vardata2,
00122                    bool *join_is_reversed);
00123 extern double get_variable_numdistinct(VariableStatData *vardata,
00124                          bool *isdefault);
00125 extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
00126                 Datum constval, bool varonleft,
00127                 double *sumcommonp);
00128 extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
00129                       Datum constval, bool varonleft,
00130                       int min_hist_size, int n_skip,
00131                       int *hist_size);
00132 
00133 extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
00134                      Pattern_Type ptype,
00135                      Oid collation,
00136                      Const **prefix,
00137                      Selectivity *rest_selec);
00138 extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc,
00139                     Oid collation);
00140 
00141 extern Datum eqsel(PG_FUNCTION_ARGS);
00142 extern Datum neqsel(PG_FUNCTION_ARGS);
00143 extern Datum scalarltsel(PG_FUNCTION_ARGS);
00144 extern Datum scalargtsel(PG_FUNCTION_ARGS);
00145 extern Datum regexeqsel(PG_FUNCTION_ARGS);
00146 extern Datum icregexeqsel(PG_FUNCTION_ARGS);
00147 extern Datum likesel(PG_FUNCTION_ARGS);
00148 extern Datum iclikesel(PG_FUNCTION_ARGS);
00149 extern Datum regexnesel(PG_FUNCTION_ARGS);
00150 extern Datum icregexnesel(PG_FUNCTION_ARGS);
00151 extern Datum nlikesel(PG_FUNCTION_ARGS);
00152 extern Datum icnlikesel(PG_FUNCTION_ARGS);
00153 
00154 extern Datum eqjoinsel(PG_FUNCTION_ARGS);
00155 extern Datum neqjoinsel(PG_FUNCTION_ARGS);
00156 extern Datum scalarltjoinsel(PG_FUNCTION_ARGS);
00157 extern Datum scalargtjoinsel(PG_FUNCTION_ARGS);
00158 extern Datum regexeqjoinsel(PG_FUNCTION_ARGS);
00159 extern Datum icregexeqjoinsel(PG_FUNCTION_ARGS);
00160 extern Datum likejoinsel(PG_FUNCTION_ARGS);
00161 extern Datum iclikejoinsel(PG_FUNCTION_ARGS);
00162 extern Datum regexnejoinsel(PG_FUNCTION_ARGS);
00163 extern Datum icregexnejoinsel(PG_FUNCTION_ARGS);
00164 extern Datum nlikejoinsel(PG_FUNCTION_ARGS);
00165 extern Datum icnlikejoinsel(PG_FUNCTION_ARGS);
00166 
00167 extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
00168             Node *arg, int varRelid,
00169             JoinType jointype, SpecialJoinInfo *sjinfo);
00170 extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
00171             Node *arg, int varRelid,
00172             JoinType jointype, SpecialJoinInfo *sjinfo);
00173 extern Selectivity scalararraysel(PlannerInfo *root,
00174                ScalarArrayOpExpr *clause,
00175                bool is_join_clause,
00176                int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
00177 extern int  estimate_array_length(Node *arrayexpr);
00178 extern Selectivity rowcomparesel(PlannerInfo *root,
00179               RowCompareExpr *clause,
00180               int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
00181 
00182 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
00183                  Oid opfamily, int strategy, bool nulls_first,
00184                  Selectivity *leftstart, Selectivity *leftend,
00185                  Selectivity *rightstart, Selectivity *rightend);
00186 
00187 extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
00188                     double input_rows);
00189 
00190 extern Selectivity estimate_hash_bucketsize(PlannerInfo *root, Node *hashkey,
00191                          double nbuckets);
00192 
00193 extern Datum btcostestimate(PG_FUNCTION_ARGS);
00194 extern Datum hashcostestimate(PG_FUNCTION_ARGS);
00195 extern Datum gistcostestimate(PG_FUNCTION_ARGS);
00196 extern Datum spgcostestimate(PG_FUNCTION_ARGS);
00197 extern Datum gincostestimate(PG_FUNCTION_ARGS);
00198 
00199 /* Functions in array_selfuncs.c */
00200 
00201 extern Selectivity scalararraysel_containment(PlannerInfo *root,
00202                            Node *leftop, Node *rightop,
00203                            Oid elemtype, bool isEquality, bool useOr,
00204                            int varRelid);
00205 extern Datum arraycontsel(PG_FUNCTION_ARGS);
00206 extern Datum arraycontjoinsel(PG_FUNCTION_ARGS);
00207 
00208 #endif   /* SELFUNCS_H */