vacuum.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*-------------------------------------------------------------------------
  2. *
  3. * vacuum.h
  4. * header file for postgres vacuum cleaner and statistics analyzer
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/commands/vacuum.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef VACUUM_H
  15. #define VACUUM_H
  16. #include "access/htup.h"
  17. #include "catalog/pg_statistic.h"
  18. #include "catalog/pg_type.h"
  19. #include "nodes/parsenodes.h"
  20. #include "storage/buf.h"
  21. #include "storage/lock.h"
  22. #include "utils/relcache.h"
  23. /*----------
  24. * ANALYZE builds one of these structs for each attribute (column) that is
  25. * to be analyzed. The struct and subsidiary data are in anl_context,
  26. * so they live until the end of the ANALYZE operation.
  27. *
  28. * The type-specific typanalyze function is passed a pointer to this struct
  29. * and must return TRUE to continue analysis, FALSE to skip analysis of this
  30. * column. In the TRUE case it must set the compute_stats and minrows fields,
  31. * and can optionally set extra_data to pass additional info to compute_stats.
  32. * minrows is its request for the minimum number of sample rows to be gathered
  33. * (but note this request might not be honored, eg if there are fewer rows
  34. * than that in the table).
  35. *
  36. * The compute_stats routine will be called after sample rows have been
  37. * gathered. Aside from this struct, it is passed:
  38. * fetchfunc: a function for accessing the column values from the
  39. * sample rows
  40. * samplerows: the number of sample tuples
  41. * totalrows: estimated total number of rows in relation
  42. * The fetchfunc may be called with rownum running from 0 to samplerows-1.
  43. * It returns a Datum and an isNull flag.
  44. *
  45. * compute_stats should set stats_valid TRUE if it is able to compute
  46. * any useful statistics. If it does, the remainder of the struct holds
  47. * the information to be stored in a pg_statistic row for the column. Be
  48. * careful to allocate any pointed-to data in anl_context, which will NOT
  49. * be CurrentMemoryContext when compute_stats is called.
  50. *
  51. * Note: for the moment, all comparisons done for statistical purposes
  52. * should use the database's default collation (DEFAULT_COLLATION_OID).
  53. * This might change in some future release.
  54. *----------
  55. */
  56. typedef struct VacAttrStats *VacAttrStatsP;
  57. typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
  58. bool *isNull);
  59. typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
  60. AnalyzeAttrFetchFunc fetchfunc,
  61. int samplerows,
  62. double totalrows);
  63. typedef struct VacAttrStats
  64. {
  65. /*
  66. * These fields are set up by the main ANALYZE code before invoking the
  67. * type-specific typanalyze function.
  68. *
  69. * Note: do not assume that the data being analyzed has the same datatype
  70. * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
  71. * because some index opclasses store a different type than the underlying
  72. * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
  73. * information about the datatype being fed to the typanalyze function.
  74. */
  75. Form_pg_attribute attr; /* copy of pg_attribute row for column */
  76. Oid attrtypid; /* type of data being analyzed */
  77. int32 attrtypmod; /* typmod of data being analyzed */
  78. Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
  79. MemoryContext anl_context; /* where to save long-lived data */
  80. /*
  81. * These fields must be filled in by the typanalyze routine, unless it
  82. * returns FALSE.
  83. */
  84. AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */
  85. int minrows; /* Minimum # of rows wanted for stats */
  86. void *extra_data; /* for extra type-specific data */
  87. /*
  88. * These fields are to be filled in by the compute_stats routine. (They
  89. * are initialized to zero when the struct is created.)
  90. */
  91. bool stats_valid;
  92. float4 stanullfrac; /* fraction of entries that are NULL */
  93. int32 stawidth; /* average width of column values */
  94. float4 stadistinct; /* # distinct values */
  95. int16 stakind[STATISTIC_NUM_SLOTS];
  96. Oid staop[STATISTIC_NUM_SLOTS];
  97. int numnumbers[STATISTIC_NUM_SLOTS];
  98. float4 *stanumbers[STATISTIC_NUM_SLOTS];
  99. int numvalues[STATISTIC_NUM_SLOTS];
  100. Datum *stavalues[STATISTIC_NUM_SLOTS];
  101. /*
  102. * These fields describe the stavalues[n] element types. They will be
  103. * initialized to match attrtypid, but a custom typanalyze function might
  104. * want to store an array of something other than the analyzed column's
  105. * elements. It should then overwrite these fields.
  106. */
  107. Oid statypid[STATISTIC_NUM_SLOTS];
  108. int16 statyplen[STATISTIC_NUM_SLOTS];
  109. bool statypbyval[STATISTIC_NUM_SLOTS];
  110. char statypalign[STATISTIC_NUM_SLOTS];
  111. /*
  112. * These fields are private to the main ANALYZE code and should not be
  113. * looked at by type-specific functions.
  114. */
  115. int tupattnum; /* attribute number within tuples */
  116. HeapTuple *rows; /* access info for std fetch function */
  117. TupleDesc tupDesc;
  118. Datum *exprvals; /* access info for index fetch function */
  119. bool *exprnulls;
  120. int rowstride;
  121. } VacAttrStats;
  122. /*
  123. * Parameters customizing behavior of VACUUM and ANALYZE.
  124. */
  125. typedef struct VacuumParams
  126. {
  127. int freeze_min_age; /* min freeze age, -1 to use default */
  128. int freeze_table_age; /* age at which to scan whole table */
  129. int multixact_freeze_min_age; /* min multixact freeze age,
  130. * -1 to use default */
  131. int multixact_freeze_table_age; /* multixact age at which to
  132. * scan whole table */
  133. bool is_wraparound; /* force a for-wraparound vacuum */
  134. int log_min_duration; /* minimum execution threshold in ms
  135. * at which verbose logs are
  136. * activated, -1 to use default */
  137. } VacuumParams;
  138. /* GUC parameters */
  139. extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for
  140. * PostGIS */
  141. extern int vacuum_freeze_min_age;
  142. extern int vacuum_freeze_table_age;
  143. extern int vacuum_multixact_freeze_min_age;
  144. extern int vacuum_multixact_freeze_table_age;
  145. /* in commands/vacuum.c */
  146. extern void ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel);
  147. extern void vacuum(int options, RangeVar *relation, Oid relid,
  148. VacuumParams *params, List *va_cols,
  149. BufferAccessStrategy bstrategy, bool isTopLevel);
  150. extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
  151. int *nindexes, Relation **Irel);
  152. extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
  153. extern double vac_estimate_reltuples(Relation relation, bool is_analyze,
  154. BlockNumber total_pages,
  155. BlockNumber scanned_pages,
  156. double scanned_tuples);
  157. extern void vac_update_relstats(Relation relation,
  158. BlockNumber num_pages,
  159. double num_tuples,
  160. BlockNumber num_all_visible_pages,
  161. bool hasindex,
  162. TransactionId frozenxid,
  163. MultiXactId minmulti,
  164. bool in_outer_xact);
  165. extern void vacuum_set_xid_limits(Relation rel,
  166. int freeze_min_age, int freeze_table_age,
  167. int multixact_freeze_min_age,
  168. int multixact_freeze_table_age,
  169. TransactionId *oldestXmin,
  170. TransactionId *freezeLimit,
  171. TransactionId *xidFullScanLimit,
  172. MultiXactId *multiXactCutoff,
  173. MultiXactId *mxactFullScanLimit);
  174. extern void vac_update_datfrozenxid(void);
  175. extern void vacuum_delay_point(void);
  176. /* in commands/vacuumlazy.c */
  177. extern void lazy_vacuum_rel(Relation onerel, int options,
  178. VacuumParams *params, BufferAccessStrategy bstrategy);
  179. /* in commands/analyze.c */
  180. extern void analyze_rel(Oid relid, RangeVar *relation, int options,
  181. VacuumParams *params, List *va_cols, bool in_outer_xact,
  182. BufferAccessStrategy bstrategy);
  183. extern bool std_typanalyze(VacAttrStats *stats);
  184. /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
  185. extern double anl_random_fract(void);
  186. extern double anl_init_selection_state(int n);
  187. extern double anl_get_next_S(double t, int n, double *stateptr);
  188. #endif /* VACUUM_H */