PostgreSQL Source Code git master
pg_statistic.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_statistic.h
4 * definition of the "statistics" system catalog (pg_statistic)
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * src/include/catalog/pg_statistic.h
11 *
12 * NOTES
13 * The Catalog.pm module reads this file and derives schema
14 * information.
15 *
16 *-------------------------------------------------------------------------
17 */
18#ifndef PG_STATISTIC_H
19#define PG_STATISTIC_H
20
21#include "catalog/genbki.h"
22#include "catalog/pg_statistic_d.h" /* IWYU pragma: export */
23
24/* ----------------
25 * pg_statistic definition. cpp turns this into
26 * typedef struct FormData_pg_statistic
27 * ----------------
28 */
29CATALOG(pg_statistic,2619,StatisticRelationId)
30{
31 /* These fields form the unique key for the entry: */
32 Oid starelid BKI_LOOKUP(pg_class); /* relation containing
33 * attribute */
34 int16 staattnum; /* attribute (column) stats are for */
35 bool stainherit; /* true if inheritance children are included */
36
37 /* the fraction of the column's entries that are NULL: */
38 float4 stanullfrac;
39
40 /*
41 * stawidth is the average width in bytes of non-null entries. For
42 * fixed-width datatypes this is of course the same as the typlen, but for
43 * var-width types it is more useful. Note that this is the average width
44 * of the data as actually stored, post-TOASTing (eg, for a
45 * moved-out-of-line value, only the size of the pointer object is
46 * counted). This is the appropriate definition for the primary use of
47 * the statistic, which is to estimate sizes of in-memory hash tables of
48 * tuples.
49 */
50 int32 stawidth;
51
52 /* ----------------
53 * stadistinct indicates the (approximate) number of distinct non-null
54 * data values in the column. The interpretation is:
55 * 0 unknown or not computed
56 * > 0 actual number of distinct values
57 * < 0 negative of multiplier for number of rows
58 * The special negative case allows us to cope with columns that are
59 * unique (stadistinct = -1) or nearly so (for example, a column in which
60 * non-null values appear about twice on the average could be represented
61 * by stadistinct = -0.5 if there are no nulls, or -0.4 if 20% of the
62 * column is nulls). Because the number-of-rows statistic in pg_class may
63 * be updated more frequently than pg_statistic is, it's important to be
64 * able to describe such situations as a multiple of the number of rows,
65 * rather than a fixed number of distinct values. But in other cases a
66 * fixed number is correct (eg, a boolean column).
67 * ----------------
68 */
69 float4 stadistinct;
70
71 /* ----------------
72 * To allow keeping statistics on different kinds of datatypes,
73 * we do not hard-wire any particular meaning for the remaining
74 * statistical fields. Instead, we provide several "slots" in which
75 * statistical data can be placed. Each slot includes:
76 * kind integer code identifying kind of data (see below)
77 * op OID of associated operator, if needed
78 * coll OID of relevant collation, or 0 if none
79 * numbers float4 array (for statistical values)
80 * values anyarray (for representations of data values)
81 * The ID, operator, and collation fields are never NULL; they are zeroes
82 * in an unused slot. The numbers and values fields are NULL in an
83 * unused slot, and might also be NULL in a used slot if the slot kind
84 * has no need for one or the other.
85 * ----------------
86 */
87
88 int16 stakind1;
89 int16 stakind2;
90 int16 stakind3;
91 int16 stakind4;
92 int16 stakind5;
93
94 Oid staop1 BKI_LOOKUP_OPT(pg_operator);
95 Oid staop2 BKI_LOOKUP_OPT(pg_operator);
96 Oid staop3 BKI_LOOKUP_OPT(pg_operator);
97 Oid staop4 BKI_LOOKUP_OPT(pg_operator);
98 Oid staop5 BKI_LOOKUP_OPT(pg_operator);
99
100 Oid stacoll1 BKI_LOOKUP_OPT(pg_collation);
101 Oid stacoll2 BKI_LOOKUP_OPT(pg_collation);
102 Oid stacoll3 BKI_LOOKUP_OPT(pg_collation);
103 Oid stacoll4 BKI_LOOKUP_OPT(pg_collation);
104 Oid stacoll5 BKI_LOOKUP_OPT(pg_collation);
105
106#ifdef CATALOG_VARLEN /* variable-length fields start here */
107 float4 stanumbers1[1];
108 float4 stanumbers2[1];
109 float4 stanumbers3[1];
110 float4 stanumbers4[1];
111 float4 stanumbers5[1];
112
113 /*
114 * Values in these arrays are values of the column's data type, or of some
115 * related type such as an array element type. We presently have to cheat
116 * quite a bit to allow polymorphic arrays of this kind, but perhaps
117 * someday it'll be a less bogus facility.
118 */
119 anyarray stavalues1;
120 anyarray stavalues2;
121 anyarray stavalues3;
122 anyarray stavalues4;
123 anyarray stavalues5;
124#endif
126
127#define STATISTIC_NUM_SLOTS 5
128
129
130/* ----------------
131 * Form_pg_statistic corresponds to a pointer to a tuple with
132 * the format of pg_statistic relation.
133 * ----------------
134 */
136
137DECLARE_TOAST(pg_statistic, 2840, 2841);
138
139DECLARE_UNIQUE_INDEX_PKEY(pg_statistic_relid_att_inh_index, 2696, StatisticRelidAttnumInhIndexId, pg_statistic, btree(starelid oid_ops, staattnum int2_ops, stainherit bool_ops));
140
141MAKE_SYSCACHE(STATRELATTINH, pg_statistic_relid_att_inh_index, 128);
142
143DECLARE_FOREIGN_KEY((starelid, staattnum), pg_attribute, (attrelid, attnum));
144
145#ifdef EXPOSE_TO_CLIENT_CODE
146
147/*
148 * Several statistical slot "kinds" are defined by core PostgreSQL, as
149 * documented below. Also, custom data types can define their own "kind"
150 * codes by mutual agreement between a custom typanalyze routine and the
151 * selectivity estimation functions of the type's operators.
152 *
153 * Code reading the pg_statistic relation should not assume that a particular
154 * data "kind" will appear in any particular slot. Instead, search the
155 * stakind fields to see if the desired data is available. (The standard
156 * function get_attstatsslot() may be used for this.)
157 *
158 * Note: The pg_stats view needs to be modified whenever a new slot kind is
159 * added to core.
160 */
161
162/*
163 * The present allocation of "kind" codes is:
164 *
165 * 1-99: reserved for assignment by the core PostgreSQL project
166 * (values in this range will be documented in this file)
167 * 100-199: reserved for assignment by the PostGIS project
168 * (values to be documented in PostGIS documentation)
169 * 200-299: reserved for assignment by the ESRI ST_Geometry project
170 * (values to be documented in ESRI ST_Geometry documentation)
171 * 300-9999: reserved for future public assignments
172 *
173 * For private use you may choose a "kind" code at random in the range
174 * 10000-30000. However, for code that is to be widely disseminated it is
175 * better to obtain a publicly defined "kind" code by request from the
176 * PostgreSQL Global Development Group.
177 */
178
179/*
180 * In a "most common values" slot, staop is the OID of the "=" operator
181 * used to decide whether values are the same or not, and stacoll is the
182 * collation used (same as column's collation). stavalues contains
183 * the K most common non-null values appearing in the column, and stanumbers
184 * contains their frequencies (fractions of total row count). The values
185 * shall be ordered in decreasing frequency. Note that since the arrays are
186 * variable-size, K may be chosen at ANALYZE time. Values should not appear
187 * in MCV unless they have been observed to occur more than once; a unique
188 * column will have no MCV slot.
189 */
190#define STATISTIC_KIND_MCV 1
191
192/*
193 * A "histogram" slot describes the distribution of scalar data. staop is
194 * the OID of the "<" operator that describes the sort ordering, and stacoll
195 * is the relevant collation. (In theory more than one histogram could appear,
196 * if a datatype has more than one useful sort operator or we care about more
197 * than one collation. Currently the collation will always be that of the
198 * underlying column.) stavalues contains M (>=2) non-null values that
199 * divide the non-null column data values into M-1 bins of approximately equal
200 * population. The first stavalues item is the MIN and the last is the MAX.
201 * stanumbers is not used and should be NULL. IMPORTANT POINT: if an MCV
202 * slot is also provided, then the histogram describes the data distribution
203 * *after removing the values listed in MCV* (thus, it's a "compressed
204 * histogram" in the technical parlance). This allows a more accurate
205 * representation of the distribution of a column with some very-common
206 * values. In a column with only a few distinct values, it's possible that
207 * the MCV list describes the entire data population; in this case the
208 * histogram reduces to empty and should be omitted.
209 */
210#define STATISTIC_KIND_HISTOGRAM 2
211
212/*
213 * A "correlation" slot describes the correlation between the physical order
214 * of table tuples and the ordering of data values of this column, as seen
215 * by the "<" operator identified by staop with the collation identified by
216 * stacoll. (As with the histogram, more than one entry could theoretically
217 * appear.) stavalues is not used and should be NULL. stanumbers contains
218 * a single entry, the correlation coefficient between the sequence of data
219 * values and the sequence of their actual tuple positions. The coefficient
220 * ranges from +1 to -1.
221 */
222#define STATISTIC_KIND_CORRELATION 3
223
224/*
225 * A "most common elements" slot is similar to a "most common values" slot,
226 * except that it stores the most common non-null *elements* of the column
227 * values. This is useful when the column datatype is an array or some other
228 * type with identifiable elements (for instance, tsvector). staop contains
229 * the equality operator appropriate to the element type, and stacoll
230 * contains the collation to use with it. stavalues contains
231 * the most common element values, and stanumbers their frequencies. Unlike
232 * MCV slots, frequencies are measured as the fraction of non-null rows the
233 * element value appears in, not the frequency of all rows. Also unlike
234 * MCV slots, the values are sorted into the element type's default order
235 * (to support binary search for a particular value). Since this puts the
236 * minimum and maximum frequencies at unpredictable spots in stanumbers,
237 * there are two extra members of stanumbers, holding copies of the minimum
238 * and maximum frequencies. Optionally, there can be a third extra member,
239 * which holds the frequency of null elements (expressed in the same terms:
240 * the fraction of non-null rows that contain at least one null element). If
241 * this member is omitted, the column is presumed to contain no null elements.
242 *
243 * Note: in current usage for tsvector columns, the stavalues elements are of
244 * type text, even though their representation within tsvector is not
245 * exactly text.
246 */
247#define STATISTIC_KIND_MCELEM 4
248
249/*
250 * A "distinct elements count histogram" slot describes the distribution of
251 * the number of distinct element values present in each row of an array-type
252 * column. Only non-null rows are considered, and only non-null elements.
253 * staop contains the equality operator appropriate to the element type,
254 * and stacoll contains the collation to use with it.
255 * stavalues is not used and should be NULL. The last member of stanumbers is
256 * the average count of distinct element values over all non-null rows. The
257 * preceding M (>=2) members form a histogram that divides the population of
258 * distinct-elements counts into M-1 bins of approximately equal population.
259 * The first of these is the minimum observed count, and the last the maximum.
260 */
261#define STATISTIC_KIND_DECHIST 5
262
263/*
264 * A "length histogram" slot describes the distribution of range lengths in
265 * rows of a range-type column. stanumbers contains a single entry, the
266 * fraction of empty ranges. stavalues is a histogram of non-empty lengths, in
267 * a format similar to STATISTIC_KIND_HISTOGRAM: it contains M (>=2) range
268 * values that divide the column data values into M-1 bins of approximately
269 * equal population. The lengths are stored as float8s, as measured by the
270 * range type's subdiff function. Only non-null, non-empty rows are
271 * considered.
272 */
273#define STATISTIC_KIND_RANGE_LENGTH_HISTOGRAM 6
274
275/*
276 * A "bounds histogram" slot is similar to STATISTIC_KIND_HISTOGRAM, but for
277 * a range-type column. stavalues contains M (>=2) range values that divide
278 * the column data values into M-1 bins of approximately equal population.
279 * Unlike a regular scalar histogram, this is actually two histograms combined
280 * into a single array, with the lower bounds of each value forming a
281 * histogram of lower bounds, and the upper bounds a histogram of upper
282 * bounds. Only non-NULL, non-empty ranges are included.
283 */
284#define STATISTIC_KIND_BOUNDS_HISTOGRAM 7
285
286#endif /* EXPOSE_TO_CLIENT_CODE */
287
288#endif /* PG_STATISTIC_H */
int16_t int16
Definition: c.h:497
int32_t int32
Definition: c.h:498
float float4
Definition: c.h:600
#define BKI_LOOKUP(catalog)
Definition: genbki.h:46
#define BKI_LOOKUP_OPT(catalog)
Definition: genbki.h:47
int16 attnum
Definition: pg_attribute.h:74
DECLARE_FOREIGN_KEY((starelid, staattnum), pg_attribute,(attrelid, attnum))
DECLARE_UNIQUE_INDEX_PKEY(pg_statistic_relid_att_inh_index, 2696, StatisticRelidAttnumInhIndexId, pg_statistic, btree(starelid oid_ops, staattnum int2_ops, stainherit bool_ops))
CATALOG(pg_statistic, 2619, StatisticRelationId)
Definition: pg_statistic.h:29
FormData_pg_statistic
Definition: pg_statistic.h:125
MAKE_SYSCACHE(STATRELATTINH, pg_statistic_relid_att_inh_index, 128)
DECLARE_TOAST(pg_statistic, 2840, 2841)
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:135
unsigned int Oid
Definition: postgres_ext.h:32