gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

229 rindas
9.0KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * selfuncs.h
  4. * Selectivity functions for standard operators, and assorted
  5. * infrastructure for selectivity and cost estimation.
  6. *
  7. *
  8. * Portions Copyright (c) 1996-2019, 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. #include "access/htup.h"
  18. #include "nodes/pathnodes.h"
  19. /*
  20. * Note: the default selectivity estimates are not chosen entirely at random.
  21. * We want them to be small enough to ensure that indexscans will be used if
  22. * available, for typical table densities of ~100 tuples/page. Thus, for
  23. * example, 0.01 is not quite small enough, since that makes it appear that
  24. * nearly all pages will be hit anyway. Also, since we sometimes estimate
  25. * eqsel as 1/num_distinct, we probably want DEFAULT_NUM_DISTINCT to equal
  26. * 1/DEFAULT_EQ_SEL.
  27. */
  28. /* default selectivity estimate for equalities such as "A = b" */
  29. #define DEFAULT_EQ_SEL 0.005
  30. /* default selectivity estimate for inequalities such as "A < b" */
  31. #define DEFAULT_INEQ_SEL 0.3333333333333333
  32. /* default selectivity estimate for range inequalities "A > b AND A < c" */
  33. #define DEFAULT_RANGE_INEQ_SEL 0.005
  34. /* default selectivity estimate for pattern-match operators such as LIKE */
  35. #define DEFAULT_MATCH_SEL 0.005
  36. /* default number of distinct values in a table */
  37. #define DEFAULT_NUM_DISTINCT 200
  38. /* default selectivity estimate for boolean and null test nodes */
  39. #define DEFAULT_UNK_SEL 0.005
  40. #define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
  41. /*
  42. * Clamp a computed probability estimate (which may suffer from roundoff or
  43. * estimation errors) to valid range. Argument must be a float variable.
  44. */
  45. #define CLAMP_PROBABILITY(p) \
  46. do { \
  47. if (p < 0.0) \
  48. p = 0.0; \
  49. else if (p > 1.0) \
  50. p = 1.0; \
  51. } while (0)
  52. /* Return data from examine_variable and friends */
  53. typedef struct VariableStatData
  54. {
  55. Node *var; /* the Var or expression tree */
  56. RelOptInfo *rel; /* Relation, or NULL if not identifiable */
  57. HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
  58. /* NB: if statsTuple!=NULL, it must be freed when caller is done */
  59. void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
  60. Oid vartype; /* exposed type of expression */
  61. Oid atttype; /* actual type (after stripping relabel) */
  62. int32 atttypmod; /* actual typmod (after stripping relabel) */
  63. bool isunique; /* matches unique index or DISTINCT clause */
  64. bool acl_ok; /* result of ACL check on table or column */
  65. } VariableStatData;
  66. #define ReleaseVariableStats(vardata) \
  67. do { \
  68. if (HeapTupleIsValid((vardata).statsTuple)) \
  69. (vardata).freefunc((vardata).statsTuple); \
  70. } while(0)
  71. /*
  72. * genericcostestimate is a general-purpose estimator that can be used for
  73. * most index types. In some cases we use genericcostestimate as the base
  74. * code and then incorporate additional index-type-specific knowledge in
  75. * the type-specific calling function. To avoid code duplication, we make
  76. * genericcostestimate return a number of intermediate values as well as
  77. * its preliminary estimates of the output cost values. The GenericCosts
  78. * struct includes all these values.
  79. *
  80. * Callers should initialize all fields of GenericCosts to zero. In addition,
  81. * they can set numIndexTuples to some positive value if they have a better
  82. * than default way of estimating the number of leaf index tuples visited.
  83. */
  84. typedef struct
  85. {
  86. /* These are the values the cost estimator must return to the planner */
  87. Cost indexStartupCost; /* index-related startup cost */
  88. Cost indexTotalCost; /* total index-related scan cost */
  89. Selectivity indexSelectivity; /* selectivity of index */
  90. double indexCorrelation; /* order correlation of index */
  91. /* Intermediate values we obtain along the way */
  92. double numIndexPages; /* number of leaf pages visited */
  93. double numIndexTuples; /* number of leaf tuples visited */
  94. double spc_random_page_cost; /* relevant random_page_cost value */
  95. double num_sa_scans; /* # indexscans from ScalarArrayOps */
  96. } GenericCosts;
  97. /* Hooks for plugins to get control when we ask for stats */
  98. typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
  99. RangeTblEntry *rte,
  100. AttrNumber attnum,
  101. VariableStatData *vardata);
  102. extern PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook;
  103. typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
  104. Oid indexOid,
  105. AttrNumber indexattnum,
  106. VariableStatData *vardata);
  107. extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
  108. /* Functions in selfuncs.c */
  109. extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
  110. VariableStatData *vardata);
  111. extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
  112. extern bool get_restriction_variable(PlannerInfo *root, List *args,
  113. int varRelid,
  114. VariableStatData *vardata, Node **other,
  115. bool *varonleft);
  116. extern void get_join_variables(PlannerInfo *root, List *args,
  117. SpecialJoinInfo *sjinfo,
  118. VariableStatData *vardata1,
  119. VariableStatData *vardata2,
  120. bool *join_is_reversed);
  121. extern double get_variable_numdistinct(VariableStatData *vardata,
  122. bool *isdefault);
  123. extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
  124. Datum constval, bool varonleft,
  125. double *sumcommonp);
  126. extern double mcv_selectivity_ext(VariableStatData *vardata,
  127. FmgrInfo *opproc, Oid collation,
  128. Datum constval, bool varonleft,
  129. double *sumcommonp);
  130. extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
  131. Datum constval, bool varonleft,
  132. int min_hist_size, int n_skip,
  133. int *hist_size);
  134. extern double histogram_selectivity_ext(VariableStatData *vardata,
  135. FmgrInfo *opproc, Oid collation,
  136. Datum constval, bool varonleft,
  137. int min_hist_size, int n_skip,
  138. int *hist_size);
  139. extern double ineq_histogram_selectivity(PlannerInfo *root,
  140. VariableStatData *vardata,
  141. FmgrInfo *opproc, bool isgt, bool iseq,
  142. Datum constval, Oid consttype);
  143. extern double ineq_histogram_selectivity_ext(PlannerInfo *root,
  144. VariableStatData *vardata,
  145. FmgrInfo *opproc,
  146. bool isgt, bool iseq,
  147. Oid collation,
  148. Datum constval, Oid consttype);
  149. extern double var_eq_const(VariableStatData *vardata, Oid oproid,
  150. Datum constval, bool constisnull,
  151. bool varonleft, bool negate);
  152. extern double var_eq_const_ext(VariableStatData *vardata,
  153. Oid oproid, Oid collation,
  154. Datum constval, bool constisnull,
  155. bool varonleft, bool negate);
  156. extern double var_eq_non_const(VariableStatData *vardata, Oid oproid,
  157. Node *other,
  158. bool varonleft, bool negate);
  159. extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
  160. extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
  161. Node *arg, int varRelid,
  162. JoinType jointype, SpecialJoinInfo *sjinfo);
  163. extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
  164. Node *arg, int varRelid,
  165. JoinType jointype, SpecialJoinInfo *sjinfo);
  166. extern Selectivity scalararraysel(PlannerInfo *root,
  167. ScalarArrayOpExpr *clause,
  168. bool is_join_clause,
  169. int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
  170. extern int estimate_array_length(Node *arrayexpr);
  171. extern Selectivity rowcomparesel(PlannerInfo *root,
  172. RowCompareExpr *clause,
  173. int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
  174. extern void mergejoinscansel(PlannerInfo *root, Node *clause,
  175. Oid opfamily, int strategy, bool nulls_first,
  176. Selectivity *leftstart, Selectivity *leftend,
  177. Selectivity *rightstart, Selectivity *rightend);
  178. extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
  179. double input_rows, List **pgset);
  180. extern void estimate_hash_bucket_stats(PlannerInfo *root,
  181. Node *hashkey, double nbuckets,
  182. Selectivity *mcv_freq,
  183. Selectivity *bucketsize_frac);
  184. extern double estimate_hashagg_tablesize(Path *path,
  185. const AggClauseCosts *agg_costs,
  186. double dNumGroups);
  187. extern List *get_quals_from_indexclauses(List *indexclauses);
  188. extern Cost index_other_operands_eval_cost(PlannerInfo *root,
  189. List *indexquals);
  190. extern List *add_predicate_to_index_quals(IndexOptInfo *index,
  191. List *indexQuals);
  192. extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
  193. double loop_count,
  194. GenericCosts *costs);
  195. /* Functions in array_selfuncs.c */
  196. extern Selectivity scalararraysel_containment(PlannerInfo *root,
  197. Node *leftop, Node *rightop,
  198. Oid elemtype, bool isEquality, bool useOr,
  199. int varRelid);
  200. #endif /* SELFUNCS_H */
上海开阖软件有限公司 沪ICP备12045867号-1