PostgreSQL Source Code git master
statistics.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * statistics.h
4 * Extended statistics and selectivity estimation functions.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/include/statistics/statistics.h
10 *
11 *-------------------------------------------------------------------------
12 */
13#ifndef STATISTICS_H
14#define STATISTICS_H
15
16#include "commands/vacuum.h"
17#include "nodes/pathnodes.h"
18
19#define STATS_MAX_DIMENSIONS 8 /* max number of attributes */
20
21/* Multivariate distinct coefficients */
22#define STATS_NDISTINCT_MAGIC 0xA352BFA4 /* struct identifier */
23#define STATS_NDISTINCT_TYPE_BASIC 1 /* struct version */
24
25/* MVNDistinctItem represents a single combination of columns */
26typedef struct MVNDistinctItem
27{
28 double ndistinct; /* ndistinct value for this combination */
29 int nattributes; /* number of attributes */
30 AttrNumber *attributes; /* attribute numbers */
32
33/* A MVNDistinct object, comprising all possible combinations of columns */
34typedef struct MVNDistinct
35{
36 uint32 magic; /* magic constant marker */
37 uint32 type; /* type of ndistinct (BASIC) */
38 uint32 nitems; /* number of items in the statistic */
41
42/* Multivariate functional dependencies */
43#define STATS_DEPS_MAGIC 0xB4549A2C /* marks serialized bytea */
44#define STATS_DEPS_TYPE_BASIC 1 /* basic dependencies type */
45
46/*
47 * Functional dependencies, tracking column-level relationships (values
48 * in one column determine values in another one).
49 */
50typedef struct MVDependency
51{
52 double degree; /* degree of validity (0-1) */
53 AttrNumber nattributes; /* number of attributes */
56
57typedef struct MVDependencies
58{
59 uint32 magic; /* magic constant marker */
60 uint32 type; /* type of MV Dependencies (BASIC) */
61 uint32 ndeps; /* number of dependencies */
64
65/* used to flag stats serialized to bytea */
66#define STATS_MCV_MAGIC 0xE1A651C2 /* marks serialized bytea */
67#define STATS_MCV_TYPE_BASIC 1 /* basic MCV list type */
68
69/* max items in MCV list */
70#define STATS_MCVLIST_MAX_ITEMS MAX_STATISTICS_TARGET
71
72/*
73 * Multivariate MCV (most-common value) lists
74 *
75 * A straightforward extension of MCV items - i.e. a list (array) of
76 * combinations of attribute values, together with a frequency and null flags.
77 */
78typedef struct MCVItem
79{
80 double frequency; /* frequency of this combination */
81 double base_frequency; /* frequency if independent */
82 bool *isnull; /* NULL flags */
83 Datum *values; /* item values */
85
86/* multivariate MCV list - essentially an array of MCV items */
87typedef struct MCVList
88{
89 uint32 magic; /* magic constant marker */
90 uint32 type; /* type of MCV list (BASIC) */
91 uint32 nitems; /* number of MCV items in the array */
92 AttrNumber ndimensions; /* number of dimensions */
93 Oid types[STATS_MAX_DIMENSIONS]; /* OIDs of data types */
94 MCVItem items[FLEXIBLE_ARRAY_MEMBER]; /* array of MCV items */
96
97extern MVNDistinct *statext_ndistinct_load(Oid mvoid, bool inh);
98extern MVDependencies *statext_dependencies_load(Oid mvoid, bool inh);
99extern MCVList *statext_mcv_load(Oid mvoid, bool inh);
100
101extern void BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
102 int numrows, HeapTuple *rows,
103 int natts, VacAttrStats **vacattrstats);
104extern int ComputeExtStatisticsRows(Relation onerel,
105 int natts, VacAttrStats **vacattrstats);
106extern bool statext_is_kind_built(HeapTuple htup, char type);
108 List *clauses,
109 int varRelid,
110 JoinType jointype,
111 SpecialJoinInfo *sjinfo,
112 RelOptInfo *rel,
113 Bitmapset **estimatedclauses);
115 List *clauses,
116 int varRelid,
117 JoinType jointype,
118 SpecialJoinInfo *sjinfo,
119 RelOptInfo *rel,
120 Bitmapset **estimatedclauses,
121 bool is_or);
122extern bool has_stats_of_kind(List *stats, char requiredkind);
123extern StatisticExtInfo *choose_best_statistics(List *stats, char requiredkind,
124 bool inh,
125 Bitmapset **clause_attnums,
126 List **clause_exprs,
127 int nclauses);
128extern HeapTuple statext_expressions_load(Oid stxoid, bool inh, int idx);
129
130#endif /* STATISTICS_H */
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
int16 AttrNumber
Definition: attnum.h:21
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:420
uint32_t uint32
Definition: c.h:488
double Selectivity
Definition: nodes.h:250
JoinType
Definition: nodes.h:288
uintptr_t Datum
Definition: postgres.h:69
unsigned int Oid
Definition: postgres_ext.h:32
tree ctl root
Definition: radixtree.h:1857
bool has_stats_of_kind(List *stats, char requiredkind)
struct MVNDistinct MVNDistinct
MVDependencies * statext_dependencies_load(Oid mvoid, bool inh)
Definition: dependencies.c:619
MCVList * statext_mcv_load(Oid mvoid, bool inh)
Definition: mcv.c:558
StatisticExtInfo * choose_best_statistics(List *stats, char requiredkind, bool inh, Bitmapset **clause_attnums, List **clause_exprs, int nclauses)
int ComputeExtStatisticsRows(Relation onerel, int natts, VacAttrStats **vacattrstats)
struct MVNDistinctItem MVNDistinctItem
struct MVDependencies MVDependencies
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
MVNDistinct * statext_ndistinct_load(Oid mvoid, bool inh)
Definition: mvdistinct.c:148
bool statext_is_kind_built(HeapTuple htup, char type)
struct MCVItem MCVItem
struct MVDependency MVDependency
void BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
Selectivity statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses, bool is_or)
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
HeapTuple statext_expressions_load(Oid stxoid, bool inh, int idx)
struct MCVList MCVList
Definition: pg_list.h:54
bool * isnull
Definition: statistics.h:82
double frequency
Definition: statistics.h:80
double base_frequency
Definition: statistics.h:81
Datum * values
Definition: statistics.h:83
uint32 type
Definition: statistics.h:90
uint32 magic
Definition: statistics.h:89
uint32 nitems
Definition: statistics.h:91
MCVItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:94
AttrNumber ndimensions
Definition: statistics.h:92
Oid types[STATS_MAX_DIMENSIONS]
Definition: statistics.h:93
uint32 ndeps
Definition: statistics.h:61
uint32 magic
Definition: statistics.h:59
MVDependency * deps[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:62
AttrNumber nattributes
Definition: statistics.h:53
double degree
Definition: statistics.h:52
AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:54
double ndistinct
Definition: statistics.h:28
AttrNumber * attributes
Definition: statistics.h:30
uint32 nitems
Definition: statistics.h:38
uint32 type
Definition: statistics.h:37
uint32 magic
Definition: statistics.h:36
MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER]
Definition: statistics.h:39
const char * type