statistics.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*-------------------------------------------------------------------------
  2. *
  3. * statistics.h
  4. * Extended statistics and selectivity estimation functions.
  5. *
  6. * Portions Copyright (c) 1996-2022, 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. #include "commands/vacuum.h"
  16. #include "nodes/pathnodes.h"
  17. #define STATS_MAX_DIMENSIONS 8 /* max number of attributes */
  18. /* Multivariate distinct coefficients */
  19. #define STATS_NDISTINCT_MAGIC 0xA352BFA4 /* struct identifier */
  20. #define STATS_NDISTINCT_TYPE_BASIC 1 /* struct version */
  21. /* MVNDistinctItem represents a single combination of columns */
  22. typedef struct MVNDistinctItem
  23. {
  24. double ndistinct; /* ndistinct value for this combination */
  25. int nattributes; /* number of attributes */
  26. AttrNumber *attributes; /* attribute numbers */
  27. } MVNDistinctItem;
  28. /* A MVNDistinct object, comprising all possible combinations of columns */
  29. typedef struct MVNDistinct
  30. {
  31. uint32 magic; /* magic constant marker */
  32. uint32 type; /* type of ndistinct (BASIC) */
  33. uint32 nitems; /* number of items in the statistic */
  34. MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER];
  35. } MVNDistinct;
  36. /* Multivariate functional dependencies */
  37. #define STATS_DEPS_MAGIC 0xB4549A2C /* marks serialized bytea */
  38. #define STATS_DEPS_TYPE_BASIC 1 /* basic dependencies type */
  39. /*
  40. * Functional dependencies, tracking column-level relationships (values
  41. * in one column determine values in another one).
  42. */
  43. typedef struct MVDependency
  44. {
  45. double degree; /* degree of validity (0-1) */
  46. AttrNumber nattributes; /* number of attributes */
  47. AttrNumber attributes[FLEXIBLE_ARRAY_MEMBER]; /* attribute numbers */
  48. } MVDependency;
  49. typedef struct MVDependencies
  50. {
  51. uint32 magic; /* magic constant marker */
  52. uint32 type; /* type of MV Dependencies (BASIC) */
  53. uint32 ndeps; /* number of dependencies */
  54. MVDependency *deps[FLEXIBLE_ARRAY_MEMBER]; /* dependencies */
  55. } MVDependencies;
  56. /* used to flag stats serialized to bytea */
  57. #define STATS_MCV_MAGIC 0xE1A651C2 /* marks serialized bytea */
  58. #define STATS_MCV_TYPE_BASIC 1 /* basic MCV list type */
  59. /* max items in MCV list (should be equal to max default_statistics_target) */
  60. #define STATS_MCVLIST_MAX_ITEMS 10000
  61. /*
  62. * Multivariate MCV (most-common value) lists
  63. *
  64. * A straightforward extension of MCV items - i.e. a list (array) of
  65. * combinations of attribute values, together with a frequency and null flags.
  66. */
  67. typedef struct MCVItem
  68. {
  69. double frequency; /* frequency of this combination */
  70. double base_frequency; /* frequency if independent */
  71. bool *isnull; /* NULL flags */
  72. Datum *values; /* item values */
  73. } MCVItem;
  74. /* multivariate MCV list - essentially an array of MCV items */
  75. typedef struct MCVList
  76. {
  77. uint32 magic; /* magic constant marker */
  78. uint32 type; /* type of MCV list (BASIC) */
  79. uint32 nitems; /* number of MCV items in the array */
  80. AttrNumber ndimensions; /* number of dimensions */
  81. Oid types[STATS_MAX_DIMENSIONS]; /* OIDs of data types */
  82. MCVItem items[FLEXIBLE_ARRAY_MEMBER]; /* array of MCV items */
  83. } MCVList;
  84. extern MVNDistinct *statext_ndistinct_load(Oid mvoid, bool inh);
  85. extern MVDependencies *statext_dependencies_load(Oid mvoid, bool inh);
  86. extern MCVList *statext_mcv_load(Oid mvoid, bool inh);
  87. extern void BuildRelationExtStatistics(Relation onerel, bool inh, double totalrows,
  88. int numrows, HeapTuple *rows,
  89. int natts, VacAttrStats **vacattrstats);
  90. extern int ComputeExtStatisticsRows(Relation onerel,
  91. int natts, VacAttrStats **stats);
  92. extern bool statext_is_kind_built(HeapTuple htup, char kind);
  93. extern Selectivity dependencies_clauselist_selectivity(PlannerInfo *root,
  94. List *clauses,
  95. int varRelid,
  96. JoinType jointype,
  97. SpecialJoinInfo *sjinfo,
  98. RelOptInfo *rel,
  99. Bitmapset **estimatedclauses);
  100. extern Selectivity statext_clauselist_selectivity(PlannerInfo *root,
  101. List *clauses,
  102. int varRelid,
  103. JoinType jointype,
  104. SpecialJoinInfo *sjinfo,
  105. RelOptInfo *rel,
  106. Bitmapset **estimatedclauses,
  107. bool is_or);
  108. extern bool has_stats_of_kind(List *stats, char requiredkind);
  109. extern StatisticExtInfo *choose_best_statistics(List *stats, char requiredkind,
  110. bool inh,
  111. Bitmapset **clause_attnums,
  112. List **clause_exprs,
  113. int nclauses);
  114. extern HeapTuple statext_expressions_load(Oid stxoid, bool inh, int idx);
  115. #endif /* STATISTICS_H */