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-2020, 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 */
26 typedef struct MVNDistinctItem
27 {
28  double ndistinct; /* ndistinct value for this combination */
29  Bitmapset *attrs; /* attr numbers of items */
31 
32 /* A MVNDistinct object, comprising all possible combinations of columns */
33 typedef struct MVNDistinct
34 {
35  uint32 magic; /* magic constant marker */
36  uint32 type; /* type of ndistinct (BASIC) */
37  uint32 nitems; /* number of items in the statistic */
39 } MVNDistinct;
40 
41 /* Multivariate functional dependencies */
42 #define STATS_DEPS_MAGIC 0xB4549A2C /* marks serialized bytea */
43 #define STATS_DEPS_TYPE_BASIC 1 /* basic dependencies type */
44 
45 /*
46  * Functional dependencies, tracking column-level relationships (values
47  * in one column determine values in another one).
48  */
49 typedef struct MVDependency
50 {
51  double degree; /* degree of validity (0-1) */
52  AttrNumber nattributes; /* number of attributes */
53  AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]; /* attribute numbers */
54 } MVDependency;
55 
56 typedef struct MVDependencies
57 {
58  uint32 magic; /* magic constant marker */
59  uint32 type; /* type of MV Dependencies (BASIC) */
60  uint32 ndeps; /* number of dependencies */
61  MVDependency *deps[FLEXIBLE_ARRAY_MEMBER]; /* dependencies */
63 
64 /* used to flag stats serialized to bytea */
65 #define STATS_MCV_MAGIC 0xE1A651C2 /* marks serialized bytea */
66 #define STATS_MCV_TYPE_BASIC 1 /* basic MCV list type */
67 
68 /* max items in MCV list (should be equal to max default_statistics_target) */
69 #define STATS_MCVLIST_MAX_ITEMS 10000
70 
71 /*
72  * Multivariate MCV (most-common value) lists
73  *
74  * A straightforward extension of MCV items - i.e. a list (array) of
75  * combinations of attribute values, together with a frequency and null flags.
76  */
77 typedef struct MCVItem
78 {
79  double frequency; /* frequency of this combination */
80  double base_frequency; /* frequency if independent */
81  bool *isnull; /* NULL flags */
82  Datum *values; /* item values */
83 } MCVItem;
84 
85 /* multivariate MCV list - essentially an array of MCV items */
86 typedef struct MCVList
87 {
88  uint32 magic; /* magic constant marker */
89  uint32 type; /* type of MCV list (BASIC) */
90  uint32 nitems; /* number of MCV items in the array */
91  AttrNumber ndimensions; /* number of dimensions */
92  Oid types[STATS_MAX_DIMENSIONS]; /* OIDs of data types */
93  MCVItem items[FLEXIBLE_ARRAY_MEMBER]; /* array of MCV items */
94 } MCVList;
95 
98 extern MCVList *statext_mcv_load(Oid mvoid);
99 
100 extern void BuildRelationExtStatistics(Relation onerel, double totalrows,
101  int numrows, HeapTuple *rows,
102  int natts, VacAttrStats **vacattrstats);
103 extern int ComputeExtStatisticsRows(Relation onerel,
104  int natts, VacAttrStats **stats);
105 extern bool statext_is_kind_built(HeapTuple htup, char kind);
107  List *clauses,
108  int varRelid,
109  JoinType jointype,
110  SpecialJoinInfo *sjinfo,
111  RelOptInfo *rel,
112  Bitmapset **estimatedclauses);
114  List *clauses,
115  int varRelid,
116  JoinType jointype,
117  SpecialJoinInfo *sjinfo,
118  RelOptInfo *rel,
119  Bitmapset **estimatedclauses);
120 extern bool has_stats_of_kind(List *stats, char requiredkind);
121 extern StatisticExtInfo *choose_best_statistics(List *stats, char requiredkind,
122  Bitmapset **clause_attnums,
123  int nclauses);
124 
125 #endif /* STATISTICS_H */
uint32 nitems
Definition: statistics.h:90
bool statext_is_kind_built(HeapTuple htup, char kind)
struct typedefs * types
Definition: ecpg.c:29
Selectivity dependencies_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
double ndistinct
Definition: statistics.h:28
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:276
bool has_stats_of_kind(List *stats, char requiredkind)
uint32 magic
Definition: statistics.h:88
AttrNumber ndimensions
Definition: statistics.h:91
struct MCVItem MCVItem
Datum * values
Definition: statistics.h:82
double Selectivity
Definition: nodes.h:662
unsigned int Oid
Definition: postgres_ext.h:31
AttrNumber nattributes
Definition: statistics.h:52
struct MVNDistinct MVNDistinct
Selectivity statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, JoinType jointype, SpecialJoinInfo *sjinfo, RelOptInfo *rel, Bitmapset **estimatedclauses)
JoinType
Definition: nodes.h:696
MVNDistinct * statext_ndistinct_load(Oid mvoid)
Definition: mvdistinct.c:141
struct MVDependency MVDependency
void BuildRelationExtStatistics(Relation onerel, double totalrows, int numrows, HeapTuple *rows, int natts, VacAttrStats **vacattrstats)
StatisticExtInfo * choose_best_statistics(List *stats, char requiredkind, Bitmapset **clause_attnums, int nclauses)
unsigned int uint32
Definition: c.h:367
MVDependencies * statext_dependencies_load(Oid mvoid)
Definition: dependencies.c:627
uint32 nitems
Definition: statistics.h:37
uint32 type
Definition: statistics.h:89
struct MVDependencies MVDependencies
uint32 magic
Definition: statistics.h:35
uintptr_t Datum
Definition: postgres.h:367
int ComputeExtStatisticsRows(Relation onerel, int natts, VacAttrStats **stats)
uint32 magic
Definition: statistics.h:58
uint32 type
Definition: statistics.h:36
uint32 ndeps
Definition: statistics.h:60
double base_frequency
Definition: statistics.h:80
double degree
Definition: statistics.h:51
struct MVNDistinctItem MVNDistinctItem
Bitmapset * attrs
Definition: statistics.h:29
bool * isnull
Definition: statistics.h:81
#define STATS_MAX_DIMENSIONS
Definition: statistics.h:19
struct MCVList MCVList
MCVList * statext_mcv_load(Oid mvoid)
Definition: mcv.c:557
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
double frequency
Definition: statistics.h:79