1 /*-------------------------------------------------------------------------
2  *
3  * statistics.h
4  *	  Extended statistics and selectivity estimation functions.
5  *
6  * Portions Copyright (c) 1996-2018, 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/relation.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 /* MVDistinctItem 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 */
30 } MVNDistinctItem;
31 
32 /* size of the struct, excluding attribute list */
33 #define SizeOfMVNDistinctItem \
34 	(offsetof(MVNDistinctItem, ndistinct) + sizeof(double))
35 
36 /* A MVNDistinct object, comprising all possible combinations of columns */
37 typedef struct MVNDistinct
38 {
39 	uint32		magic;			/* magic constant marker */
40 	uint32		type;			/* type of ndistinct (BASIC) */
41 	uint32		nitems;			/* number of items in the statistic */
42 	MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER];
43 } MVNDistinct;
44 
45 /* size of the struct excluding the items array */
46 #define SizeOfMVNDistinct	(offsetof(MVNDistinct, nitems) + sizeof(uint32))
47 
48 
49 /* size of the struct excluding the items array */
50 #define SizeOfMVNDistinct	(offsetof(MVNDistinct, nitems) + sizeof(uint32))
51 
52 #define STATS_DEPS_MAGIC		0xB4549A2C	/* marks serialized bytea */
53 #define STATS_DEPS_TYPE_BASIC	1	/* basic dependencies type */
54 
55 /*
56  * Functional dependencies, tracking column-level relationships (values
57  * in one column determine values in another one).
58  */
59 typedef struct MVDependency
60 {
61 	double		degree;			/* degree of validity (0-1) */
62 	AttrNumber	nattributes;	/* number of attributes */
63 	AttrNumber	attributes[FLEXIBLE_ARRAY_MEMBER];	/* attribute numbers */
64 } MVDependency;
65 
66 /* size of the struct excluding the deps array */
67 #define SizeOfDependency \
68 	(offsetof(MVDependency, nattributes) + sizeof(AttrNumber))
69 
70 typedef struct MVDependencies
71 {
72 	uint32		magic;			/* magic constant marker */
73 	uint32		type;			/* type of MV Dependencies (BASIC) */
74 	uint32		ndeps;			/* number of dependencies */
75 	MVDependency *deps[FLEXIBLE_ARRAY_MEMBER];	/* dependencies */
76 } MVDependencies;
77 
78 /* size of the struct excluding the deps array */
79 #define SizeOfDependencies	(offsetof(MVDependencies, ndeps) + sizeof(uint32))
80 
81 extern MVNDistinct *statext_ndistinct_load(Oid mvoid);
82 extern MVDependencies *statext_dependencies_load(Oid mvoid);
83 
84 extern void BuildRelationExtStatistics(Relation onerel, double totalrows,
85 						   int numrows, HeapTuple *rows,
86 						   int natts, VacAttrStats **vacattrstats);
87 extern bool statext_is_kind_built(HeapTuple htup, char kind);
88 extern Selectivity dependencies_clauselist_selectivity(PlannerInfo *root,
89 									List *clauses,
90 									int varRelid,
91 									JoinType jointype,
92 									SpecialJoinInfo *sjinfo,
93 									RelOptInfo *rel,
94 									Bitmapset **estimatedclauses);
95 extern bool has_stats_of_kind(List *stats, char requiredkind);
96 extern StatisticExtInfo *choose_best_statistics(List *stats,
97 					   Bitmapset *attnums, char requiredkind);
98 
99 #endif							/* STATISTICS_H */
100