YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/postgres/src/include/utils/selfuncs.h
Line
Count
Source
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-2018, 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
119
#define DEFAULT_EQ_SEL  0.005
35
36
/* default selectivity estimate for inequalities such as "A < b" */
37
8.13k
#define DEFAULT_INEQ_SEL  0.3333333333333333
38
39
/* default selectivity estimate for range inequalities "A > b AND A < c" */
40
10
#define DEFAULT_RANGE_INEQ_SEL  0.005
41
42
/* default selectivity estimate for pattern-match operators such as LIKE */
43
186
#define DEFAULT_MATCH_SEL 0.005
44
45
/* default number of distinct values in a table */
46
40.9k
#define DEFAULT_NUM_DISTINCT  200
47
48
/* default selectivity estimate for boolean and null test nodes */
49
205
#define DEFAULT_UNK_SEL     0.005
50
90
#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
44.5k
  do { \
59
44.5k
    if (p < 0.0) \
60
44.5k
      
p = 0.0126
; \
61
44.5k
    else 
if (44.3k
p > 1.044.3k
) \
62
44.3k
      
p = 1.0164
; \
63
44.5k
  } 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;    /* actual type (after stripping relabel) */
76
  int32   atttypmod;    /* actual typmod (after stripping relabel) */
77
  bool    isunique;   /* matches unique index or DISTINCT clause */
78
  bool    acl_ok;     /* result of ACL check on table or column */
79
} VariableStatData;
80
81
#define ReleaseVariableStats(vardata)  \
82
83.5k
  do { \
83
83.5k
    if (HeapTupleIsValid((vardata).statsTuple)) \
84
83.5k
      
(vardata).freefunc((vardata).statsTuple)20.8k
; \
85
83.5k
  } while(0)
86
87
88
typedef enum
89
{
90
  Pattern_Type_Like,
91
  Pattern_Type_Like_IC,
92
  Pattern_Type_Regex,
93
  Pattern_Type_Regex_IC,
94
  Pattern_Type_Prefix
95
} Pattern_Type;
96
97
typedef enum
98
{
99
  Pattern_Prefix_None, Pattern_Prefix_Partial, Pattern_Prefix_Exact
100
} Pattern_Prefix_Status;
101
102
/*
103
 * deconstruct_indexquals is a simple function to examine the indexquals
104
 * attached to a proposed IndexPath.  It returns a list of IndexQualInfo
105
 * structs, one per qual expression.
106
 */
107
typedef struct
108
{
109
  RestrictInfo *rinfo;    /* the indexqual itself */
110
  int     indexcol;   /* zero-based index column number */
111
  bool    varonleft;    /* true if index column is on left of qual */
112
  bool    is_hashed;    /* true if the variable is hashed */
113
  Oid     clause_op;    /* qual's operator OID, if relevant */
114
  Node     *other_operand;  /* non-index operand of qual's operator */
115
} IndexQualInfo;
116
117
/*
118
 * genericcostestimate is a general-purpose estimator that can be used for
119
 * most index types.  In some cases we use genericcostestimate as the base
120
 * code and then incorporate additional index-type-specific knowledge in
121
 * the type-specific calling function.  To avoid code duplication, we make
122
 * genericcostestimate return a number of intermediate values as well as
123
 * its preliminary estimates of the output cost values.  The GenericCosts
124
 * struct includes all these values.
125
 *
126
 * Callers should initialize all fields of GenericCosts to zero.  In addition,
127
 * they can set numIndexTuples to some positive value if they have a better
128
 * than default way of estimating the number of leaf index tuples visited.
129
 */
130
typedef struct
131
{
132
  /* These are the values the cost estimator must return to the planner */
133
  Cost    indexStartupCost; /* index-related startup cost */
134
  Cost    indexTotalCost; /* total index-related scan cost */
135
  Selectivity indexSelectivity; /* selectivity of index */
136
  double    indexCorrelation; /* order correlation of index */
137
138
  /* Intermediate values we obtain along the way */
139
  double    numIndexPages;  /* number of leaf pages visited */
140
  double    numIndexTuples; /* number of leaf tuples visited */
141
  double    spc_random_page_cost; /* relevant random_page_cost value */
142
  double    num_sa_scans; /* # indexscans from ScalarArrayOps */
143
} GenericCosts;
144
145
/* Hooks for plugins to get control when we ask for stats */
146
typedef bool (*get_relation_stats_hook_type) (PlannerInfo *root,
147
                        RangeTblEntry *rte,
148
                        AttrNumber attnum,
149
                        VariableStatData *vardata);
150
extern PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook;
151
typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
152
                       Oid indexOid,
153
                       AttrNumber indexattnum,
154
                       VariableStatData *vardata);
155
extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
156
157
/* Functions in selfuncs.c */
158
159
extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
160
         VariableStatData *vardata);
161
extern bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid);
162
extern bool get_restriction_variable(PlannerInfo *root, List *args,
163
             int varRelid,
164
             VariableStatData *vardata, Node **other,
165
             bool *varonleft);
166
extern void get_join_variables(PlannerInfo *root, List *args,
167
           SpecialJoinInfo *sjinfo,
168
           VariableStatData *vardata1,
169
           VariableStatData *vardata2,
170
           bool *join_is_reversed);
171
extern double get_variable_numdistinct(VariableStatData *vardata,
172
             bool *isdefault);
173
extern double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
174
        Datum constval, bool varonleft,
175
        double *sumcommonp);
176
extern double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc,
177
            Datum constval, bool varonleft,
178
            int min_hist_size, int n_skip,
179
            int *hist_size);
180
181
extern Pattern_Prefix_Status pattern_fixed_prefix(Const *patt,
182
           Pattern_Type ptype,
183
           Oid collation,
184
           Const **prefix,
185
           Selectivity *rest_selec);
186
extern Const *make_greater_string(const Const *str_const, FmgrInfo *ltproc,
187
          Oid collation);
188
189
extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
190
extern Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype,
191
      Node *arg, int varRelid,
192
      JoinType jointype, SpecialJoinInfo *sjinfo);
193
extern Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype,
194
      Node *arg, int varRelid,
195
      JoinType jointype, SpecialJoinInfo *sjinfo);
196
extern Selectivity scalararraysel(PlannerInfo *root,
197
         ScalarArrayOpExpr *clause,
198
         bool is_join_clause,
199
         int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
200
extern int  estimate_array_length(Node *arrayexpr);
201
extern Selectivity rowcomparesel(PlannerInfo *root,
202
        RowCompareExpr *clause,
203
        int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
204
205
extern void mergejoinscansel(PlannerInfo *root, Node *clause,
206
         Oid opfamily, int strategy, bool nulls_first,
207
         Selectivity *leftstart, Selectivity *leftend,
208
         Selectivity *rightstart, Selectivity *rightend);
209
210
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
211
          double input_rows, List **pgset);
212
213
extern void estimate_hash_bucket_stats(PlannerInfo *root,
214
               Node *hashkey, double nbuckets,
215
               Selectivity *mcv_freq,
216
               Selectivity *bucketsize_frac);
217
218
extern List *deconstruct_indexquals(IndexPath *path);
219
extern void genericcostestimate(PlannerInfo *root, IndexPath *path,
220
          double loop_count,
221
          List *qinfos,
222
          GenericCosts *costs);
223
224
/* Functions in array_selfuncs.c */
225
226
extern Selectivity scalararraysel_containment(PlannerInfo *root,
227
               Node *leftop, Node *rightop,
228
               Oid elemtype, bool isEquality, bool useOr,
229
               int varRelid);
230
231
#endif              /* SELFUNCS_H */