pg_statistic.h 12 KB

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