1 /*-------------------------------------------------------------------------
4 * Extended statistics and selectivity estimation functions.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/include/statistics/statistics.h
11 *-------------------------------------------------------------------------
16 #include "commands/vacuum.h"
17 #include "nodes/pathnodes.h"
19 #define STATS_MAX_DIMENSIONS 8 /* max number of attributes */
21 /* Multivariate distinct coefficients */
22 #define STATS_NDISTINCT_MAGIC 0xA352BFA4 /* struct identifier */
23 #define STATS_NDISTINCT_TYPE_BASIC 1 /* struct version */
25 /* MVNDistinctItem represents a single combination of columns */
26 typedef struct MVNDistinctItem
28 double ndistinct
; /* ndistinct value for this combination */
29 int nattributes
; /* number of attributes */
30 AttrNumber
*attributes
; /* attribute numbers */
33 /* A MVNDistinct object, comprising all possible combinations of columns */
34 typedef struct MVNDistinct
36 uint32 magic
; /* magic constant marker */
37 uint32 type
; /* type of ndistinct (BASIC) */
38 uint32 nitems
; /* number of items in the statistic */
39 MVNDistinctItem items
[FLEXIBLE_ARRAY_MEMBER
];
42 /* Multivariate functional dependencies */
43 #define STATS_DEPS_MAGIC 0xB4549A2C /* marks serialized bytea */
44 #define STATS_DEPS_TYPE_BASIC 1 /* basic dependencies type */
47 * Functional dependencies, tracking column-level relationships (values
48 * in one column determine values in another one).
50 typedef struct MVDependency
52 double degree
; /* degree of validity (0-1) */
53 AttrNumber nattributes
; /* number of attributes */
54 AttrNumber attributes
[FLEXIBLE_ARRAY_MEMBER
]; /* attribute numbers */
57 typedef struct MVDependencies
59 uint32 magic
; /* magic constant marker */
60 uint32 type
; /* type of MV Dependencies (BASIC) */
61 uint32 ndeps
; /* number of dependencies */
62 MVDependency
*deps
[FLEXIBLE_ARRAY_MEMBER
]; /* dependencies */
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 */
69 /* max items in MCV list */
70 #define STATS_MCVLIST_MAX_ITEMS MAX_STATISTICS_TARGET
73 * Multivariate MCV (most-common value) lists
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.
78 typedef struct MCVItem
80 double frequency
; /* frequency of this combination */
81 double base_frequency
; /* frequency if independent */
82 bool *isnull
; /* NULL flags */
83 Datum
*values
; /* item values */
86 /* multivariate MCV list - essentially an array of MCV items */
87 typedef struct MCVList
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 */
97 extern MVNDistinct
*statext_ndistinct_load(Oid mvoid
, bool inh
);
98 extern MVDependencies
*statext_dependencies_load(Oid mvoid
, bool inh
);
99 extern MCVList
*statext_mcv_load(Oid mvoid
, bool inh
);
101 extern void BuildRelationExtStatistics(Relation onerel
, bool inh
, double totalrows
,
102 int numrows
, HeapTuple
*rows
,
103 int natts
, VacAttrStats
**vacattrstats
);
104 extern int ComputeExtStatisticsRows(Relation onerel
,
105 int natts
, VacAttrStats
**vacattrstats
);
106 extern bool statext_is_kind_built(HeapTuple htup
, char type
);
107 extern Selectivity
dependencies_clauselist_selectivity(PlannerInfo
*root
,
111 SpecialJoinInfo
*sjinfo
,
113 Bitmapset
**estimatedclauses
);
114 extern Selectivity
statext_clauselist_selectivity(PlannerInfo
*root
,
118 SpecialJoinInfo
*sjinfo
,
120 Bitmapset
**estimatedclauses
,
122 extern bool has_stats_of_kind(List
*stats
, char requiredkind
);
123 extern StatisticExtInfo
*choose_best_statistics(List
*stats
, char requiredkind
,
125 Bitmapset
**clause_attnums
,
128 extern HeapTuple
statext_expressions_load(Oid stxoid
, bool inh
, int idx
);
130 #endif /* STATISTICS_H */