PostgreSQL Source Code git master
Loading...
Searching...
No Matches
selfuncs.h
Go to the documentation of this file.
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-2026, 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 "access/htup.h"
19#include "fmgr.h"
20#include "nodes/pathnodes.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 multirange inequalities "A > b AND A < c" */
43#define DEFAULT_MULTIRANGE_INEQ_SEL 0.005
44
45/* default selectivity estimate for pattern-match operators such as LIKE */
46#define DEFAULT_MATCH_SEL 0.005
47
48/* default selectivity estimate for other matching operators */
49#define DEFAULT_MATCHING_SEL 0.010
50
51/* default number of distinct values in a table */
52#define DEFAULT_NUM_DISTINCT 200
53
54/* default selectivity estimate for boolean and null test nodes */
55#define DEFAULT_UNK_SEL 0.005
56#define DEFAULT_NOT_UNK_SEL (1.0 - DEFAULT_UNK_SEL)
57
58
59/*
60 * Clamp a computed probability estimate (which may suffer from roundoff or
61 * estimation errors) to valid range. Argument must be a float variable.
62 */
63#define CLAMP_PROBABILITY(p) \
64 do { \
65 if (p < 0.0) \
66 p = 0.0; \
67 else if (p > 1.0) \
68 p = 1.0; \
69 } while (0)
70
71/*
72 * A set of flags which some selectivity estimation functions can pass back to
73 * callers to provide further details about some assumptions which were made
74 * during the estimation.
75 */
76#define SELFLAG_USED_DEFAULT (1 << 0) /* Estimation fell back on one
77 * of the DEFAULTs as defined
78 * above. */
79
80typedef struct EstimationInfo
81{
82 uint32 flags; /* Flags, as defined above to mark special
83 * properties of the estimation. */
86/* Return data from examine_variable and friends */
87typedef struct VariableStatData
89 Node *var; /* the Var or expression tree */
90 RelOptInfo *rel; /* Relation, or NULL if not identifiable */
91 HeapTuple statsTuple; /* pg_statistic tuple, or NULL if none */
92 /* NB: if statsTuple!=NULL, it must be freed when caller is done */
93 void (*freefunc) (HeapTuple tuple); /* how to free statsTuple */
94 Oid vartype; /* exposed type of expression */
95 Oid atttype; /* actual type (after stripping relabel) */
96 int32 atttypmod; /* actual typmod (after stripping relabel) */
97 bool isunique; /* matches unique index, DISTINCT or GROUP-BY
98 * clause */
99 bool acl_ok; /* true if user has SELECT privilege on all
100 * rows from the table or column */
102
103#define ReleaseVariableStats(vardata) \
104 do { \
105 if (HeapTupleIsValid((vardata).statsTuple)) \
106 (vardata).freefunc((vardata).statsTuple); \
107 } while(0)
108
109
110/*
111 * genericcostestimate is a general-purpose estimator that can be used for
112 * most index types. In some cases we use genericcostestimate as the base
113 * code and then incorporate additional index-type-specific knowledge in
114 * the type-specific calling function. To avoid code duplication, we make
115 * genericcostestimate return a number of intermediate values as well as
116 * its preliminary estimates of the output cost values. The GenericCosts
117 * struct includes all these values.
118 *
119 * Callers should initialize all fields of GenericCosts to zero. In addition,
120 * they can set numIndexTuples to some positive value if they have a better
121 * than default way of estimating the number of leaf index tuples visited.
122 * Similarly, they can set num_sa_scans to some value >= 1 for an index AM
123 * that doesn't necessarily perform exactly one primitive index scan per
124 * distinct combination of ScalarArrayOp array elements.
125 * Similarly, they can set numNonLeafPages to some value >= 1 if they know
126 * how many index pages are not leaf pages. (It's always good to count
127 * totally non-data-bearing pages such as metapages here, since accounting
128 * for the metapage can move cost estimates for a small index significantly.
129 * But upper pages in large indexes may be few enough relative to leaf pages
130 * that it's not worth trying to count them.)
131 */
132typedef struct
134 /* These are the values the cost estimator must return to the planner */
135 Cost indexStartupCost; /* index-related startup cost */
136 Cost indexTotalCost; /* total index-related scan cost */
137 Selectivity indexSelectivity; /* selectivity of index */
138 double indexCorrelation; /* order correlation of index */
140 /* Intermediate values we obtain along the way */
141 double numIndexPages; /* number of leaf pages visited */
142 double numIndexTuples; /* number of leaf tuples visited */
143 double spc_random_page_cost; /* relevant random_page_cost value */
144 double num_sa_scans; /* # indexscans from ScalarArrayOpExprs */
145 BlockNumber numNonLeafPages; /* # of index pages that are not leaves */
148/* Hooks for plugins to get control when we ask for stats */
155 Oid indexOid,
159
160/* Functions in selfuncs.c */
161
162extern void examine_variable(PlannerInfo *root, Node *node, int varRelid,
164extern bool all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos);
167 int varRelid,
169 bool *varonleft);
170extern void get_join_variables(PlannerInfo *root, List *args,
171 SpecialJoinInfo *sjinfo,
174 bool *join_is_reversed);
176 bool *isdefault);
178 FmgrInfo *opproc, Oid collation,
179 Datum constval, bool varonleft,
180 double *sumcommonp);
182 FmgrInfo *opproc, Oid collation,
183 Datum constval, bool varonleft,
184 int min_hist_size, int n_skip,
185 int *hist_size);
187 Oid oproid, Oid collation,
188 List *args, int varRelid,
189 double default_selectivity);
193 bool isgt, bool iseq,
194 Oid collation,
195 Datum constval, Oid consttype);
197 Oid oproid, Oid collation,
198 Datum constval, bool constisnull,
199 bool varonleft, bool negate);
201 Oid oproid, Oid collation,
202 Node *other,
203 bool varonleft, bool negate);
204
205extern Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid);
207 Node *arg, int varRelid,
208 JoinType jointype, SpecialJoinInfo *sjinfo);
210 Node *arg, int varRelid,
211 JoinType jointype, SpecialJoinInfo *sjinfo);
213 ScalarArrayOpExpr *clause,
214 bool is_join_clause,
215 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
216extern double estimate_array_length(PlannerInfo *root, Node *arrayexpr);
218 RowCompareExpr *clause,
219 int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo);
220
221extern void mergejoinscansel(PlannerInfo *root, Node *clause,
222 Oid opfamily, CompareType cmptype, bool nulls_first,
225
227 double input_rows, List **pgset,
229
231 RelOptInfo *inner,
232 List *hashclauses,
235 Node *hashkey, double nbuckets,
238extern double estimate_hashagg_tablesize(PlannerInfo *root, Path *path,
240 double dNumGroups);
241
242extern List *get_quals_from_indexclauses(List *indexclauses);
244 List *indexquals);
248 double loop_count,
249 GenericCosts *costs);
250
251/* Functions in array_selfuncs.c */
252
255 Oid elemtype, bool isEquality, bool useOr,
256 int varRelid);
257
258#endif /* SELFUNCS_H */
int16 AttrNumber
Definition attnum.h:21
uint32 BlockNumber
Definition block.h:31
#define PGDLLIMPORT
Definition c.h:1423
int32_t int32
Definition c.h:614
uint32_t uint32
Definition c.h:618
unsigned int Index
Definition c.h:700
CompareType
Definition cmptype.h:32
Datum arg
Definition elog.c:1322
double Cost
Definition nodes.h:261
double Selectivity
Definition nodes.h:260
JoinType
Definition nodes.h:298
int16 attnum
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
BoolTestType
Definition primnodes.h:2002
NullTestType
Definition primnodes.h:1978
tree ctl root
Definition radixtree.h:1857
Selectivity scalararraysel_containment(PlannerInfo *root, Node *leftop, Node *rightop, Oid elemtype, bool isEquality, bool useOr, int varRelid)
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition selfuncs.c:5500
void mergejoinscansel(PlannerInfo *root, Node *clause, Oid opfamily, CompareType cmptype, bool nulls_first, Selectivity *leftstart, Selectivity *leftend, Selectivity *rightstart, Selectivity *rightend)
Definition selfuncs.c:3302
bool all_rows_selectable(PlannerInfo *root, Index varno, Bitmapset *varattnos)
Definition selfuncs.c:6301
List * get_quals_from_indexclauses(List *indexclauses)
Definition selfuncs.c:7308
double var_eq_const(VariableStatData *vardata, Oid oproid, Oid collation, Datum constval, bool constisnull, bool varonleft, bool negate)
Definition selfuncs.c:368
List * add_predicate_to_index_quals(IndexOptInfo *index, List *indexQuals)
Definition selfuncs.c:7622
double generic_restriction_selectivity(PlannerInfo *root, Oid oproid, Oid collation, List *args, int varRelid, double default_selectivity)
Definition selfuncs.c:987
Selectivity booltestsel(PlannerInfo *root, BoolTestType booltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition selfuncs.c:1624
double estimate_array_length(PlannerInfo *root, Node *arrayexpr)
Definition selfuncs.c:2240
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, double *sumcommonp)
Definition selfuncs.c:805
Selectivity nulltestsel(PlannerInfo *root, NullTestType nulltesttype, Node *arg, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition selfuncs.c:1782
PGDLLIMPORT get_relation_stats_hook_type get_relation_stats_hook
Definition selfuncs.c:183
void examine_variable(PlannerInfo *root, Node *node, int varRelid, VariableStatData *vardata)
Definition selfuncs.c:5629
bool(* get_relation_stats_hook_type)(PlannerInfo *root, RangeTblEntry *rte, AttrNumber attnum, VariableStatData *vardata)
Definition selfuncs.h:147
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset, EstimationInfo *estinfo)
Definition selfuncs.c:3788
double ineq_histogram_selectivity(PlannerInfo *root, VariableStatData *vardata, Oid opoid, FmgrInfo *opproc, bool isgt, bool iseq, Oid collation, Datum constval, Oid consttype)
Definition selfuncs.c:1114
void genericcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, GenericCosts *costs)
Definition selfuncs.c:7397
List * estimate_multivariate_bucketsize(PlannerInfo *root, RelOptInfo *inner, List *hashclauses, Selectivity *innerbucketsize)
Definition selfuncs.c:4140
bool(* get_index_stats_hook_type)(PlannerInfo *root, Oid indexOid, AttrNumber indexattnum, VariableStatData *vardata)
Definition selfuncs.h:152
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Oid collation, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition selfuncs.c:896
Selectivity boolvarsel(PlannerInfo *root, Node *arg, int varRelid)
Definition selfuncs.c:1585
Selectivity scalararraysel(PlannerInfo *root, ScalarArrayOpExpr *clause, bool is_join_clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition selfuncs.c:1900
double var_eq_non_const(VariableStatData *vardata, Oid oproid, Oid collation, Node *other, bool varonleft, bool negate)
Definition selfuncs.c:539
double get_variable_numdistinct(VariableStatData *vardata, bool *isdefault)
Definition selfuncs.c:6599
PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook
Definition selfuncs.c:184
bool statistic_proc_security_check(VariableStatData *vardata, Oid func_oid)
Definition selfuncs.c:6570
double estimate_hashagg_tablesize(PlannerInfo *root, Path *path, const AggClauseCosts *agg_costs, double dNumGroups)
Definition selfuncs.c:4514
void estimate_hash_bucket_stats(PlannerInfo *root, Node *hashkey, double nbuckets, Selectivity *mcv_freq, Selectivity *bucketsize_frac)
Definition selfuncs.c:4408
Cost index_other_operands_eval_cost(PlannerInfo *root, List *indexquals)
Definition selfuncs.c:7338
Selectivity rowcomparesel(PlannerInfo *root, RowCompareExpr *clause, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo)
Definition selfuncs.c:2306
void get_join_variables(PlannerInfo *root, List *args, SpecialJoinInfo *sjinfo, VariableStatData *vardata1, VariableStatData *vardata2, bool *join_is_reversed)
Definition selfuncs.c:5560
uint32 flags
Definition selfuncs.h:80
Definition pg_list.h:54
Definition nodes.h:135
HeapTuple statsTuple
Definition selfuncs.h:89
RelOptInfo * rel
Definition selfuncs.h:88
void(* freefunc)(HeapTuple tuple)
Definition selfuncs.h:91
Definition type.h:96