00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #define DEFAULT_EQ_SEL 0.005
00035
00036
00037 #define DEFAULT_INEQ_SEL 0.3333333333333333
00038
00039
00040 #define DEFAULT_RANGE_INEQ_SEL 0.005
00041
00042
00043 #define DEFAULT_MATCH_SEL 0.005
00044
00045
00046 #define DEFAULT_NUM_DISTINCT 200
00047
00048
00049 #define DEFAULT_UNK_SEL 0.005
00050 #define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
00051
00052
00053
00054
00055
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
00067 typedef struct VariableStatData
00068 {
00069 Node *var;
00070 RelOptInfo *rel;
00071 HeapTuple statsTuple;
00072
00073 void (*freefunc) (HeapTuple tuple);
00074 Oid vartype;
00075 Oid atttype;
00076 int32 atttypmod;
00077 bool isunique;
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
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
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
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