gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
9.3KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * vacuum.h
  4. * header file for postgres vacuum cleaner and statistics analyzer
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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_class.h"
  18. #include "catalog/pg_statistic.h"
  19. #include "catalog/pg_type.h"
  20. #include "nodes/parsenodes.h"
  21. #include "storage/buf.h"
  22. #include "storage/lock.h"
  23. #include "utils/relcache.h"
  24. /*----------
  25. * ANALYZE builds one of these structs for each attribute (column) that is
  26. * to be analyzed. The struct and subsidiary data are in anl_context,
  27. * so they live until the end of the ANALYZE operation.
  28. *
  29. * The type-specific typanalyze function is passed a pointer to this struct
  30. * and must return true to continue analysis, false to skip analysis of this
  31. * column. In the true case it must set the compute_stats and minrows fields,
  32. * and can optionally set extra_data to pass additional info to compute_stats.
  33. * minrows is its request for the minimum number of sample rows to be gathered
  34. * (but note this request might not be honored, eg if there are fewer rows
  35. * than that in the table).
  36. *
  37. * The compute_stats routine will be called after sample rows have been
  38. * gathered. Aside from this struct, it is passed:
  39. * fetchfunc: a function for accessing the column values from the
  40. * sample rows
  41. * samplerows: the number of sample tuples
  42. * totalrows: estimated total number of rows in relation
  43. * The fetchfunc may be called with rownum running from 0 to samplerows-1.
  44. * It returns a Datum and an isNull flag.
  45. *
  46. * compute_stats should set stats_valid true if it is able to compute
  47. * any useful statistics. If it does, the remainder of the struct holds
  48. * the information to be stored in a pg_statistic row for the column. Be
  49. * careful to allocate any pointed-to data in anl_context, which will NOT
  50. * be CurrentMemoryContext when compute_stats is called.
  51. *
  52. * Note: all comparisons done for statistical purposes should use the
  53. * underlying column's collation (attcollation), except in situations
  54. * where a noncollatable container type contains a collatable type;
  55. * in that case use the type's default collation. Be sure to record
  56. * the appropriate collation in stacoll.
  57. *----------
  58. */
  59. typedef struct VacAttrStats *VacAttrStatsP;
  60. typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
  61. bool *isNull);
  62. typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
  63. AnalyzeAttrFetchFunc fetchfunc,
  64. int samplerows,
  65. double totalrows);
  66. typedef struct VacAttrStats
  67. {
  68. /*
  69. * These fields are set up by the main ANALYZE code before invoking the
  70. * type-specific typanalyze function.
  71. *
  72. * Note: do not assume that the data being analyzed has the same datatype
  73. * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
  74. * because some index opclasses store a different type than the underlying
  75. * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
  76. * information about the datatype being fed to the typanalyze function.
  77. * Likewise, use attrcollid not attr->attcollation.
  78. */
  79. Form_pg_attribute attr; /* copy of pg_attribute row for column */
  80. Oid attrtypid; /* type of data being analyzed */
  81. int32 attrtypmod; /* typmod of data being analyzed */
  82. Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
  83. Oid attrcollid; /* collation of data being analyzed */
  84. MemoryContext anl_context; /* where to save long-lived data */
  85. /*
  86. * These fields must be filled in by the typanalyze routine, unless it
  87. * returns false.
  88. */
  89. AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */
  90. int minrows; /* Minimum # of rows wanted for stats */
  91. void *extra_data; /* for extra type-specific data */
  92. /*
  93. * These fields are to be filled in by the compute_stats routine. (They
  94. * are initialized to zero when the struct is created.)
  95. */
  96. bool stats_valid;
  97. float4 stanullfrac; /* fraction of entries that are NULL */
  98. int32 stawidth; /* average width of column values */
  99. float4 stadistinct; /* # distinct values */
  100. int16 stakind[STATISTIC_NUM_SLOTS];
  101. Oid staop[STATISTIC_NUM_SLOTS];
  102. Oid stacoll[STATISTIC_NUM_SLOTS];
  103. int numnumbers[STATISTIC_NUM_SLOTS];
  104. float4 *stanumbers[STATISTIC_NUM_SLOTS];
  105. int numvalues[STATISTIC_NUM_SLOTS];
  106. Datum *stavalues[STATISTIC_NUM_SLOTS];
  107. /*
  108. * These fields describe the stavalues[n] element types. They will be
  109. * initialized to match attrtypid, but a custom typanalyze function might
  110. * want to store an array of something other than the analyzed column's
  111. * elements. It should then overwrite these fields.
  112. */
  113. Oid statypid[STATISTIC_NUM_SLOTS];
  114. int16 statyplen[STATISTIC_NUM_SLOTS];
  115. bool statypbyval[STATISTIC_NUM_SLOTS];
  116. char statypalign[STATISTIC_NUM_SLOTS];
  117. /*
  118. * These fields are private to the main ANALYZE code and should not be
  119. * looked at by type-specific functions.
  120. */
  121. int tupattnum; /* attribute number within tuples */
  122. HeapTuple *rows; /* access info for std fetch function */
  123. TupleDesc tupDesc;
  124. Datum *exprvals; /* access info for index fetch function */
  125. bool *exprnulls;
  126. int rowstride;
  127. } VacAttrStats;
  128. typedef enum VacuumOption
  129. {
  130. VACOPT_VACUUM = 1 << 0, /* do VACUUM */
  131. VACOPT_ANALYZE = 1 << 1, /* do ANALYZE */
  132. VACOPT_VERBOSE = 1 << 2, /* print progress info */
  133. VACOPT_FREEZE = 1 << 3, /* FREEZE option */
  134. VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */
  135. VACOPT_SKIP_LOCKED = 1 << 5, /* skip if cannot get lock */
  136. VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */
  137. VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */
  138. } VacuumOption;
  139. /*
  140. * A ternary value used by vacuum parameters.
  141. *
  142. * DEFAULT value is used to determine the value based on other
  143. * configurations, e.g. reloptions.
  144. */
  145. typedef enum VacOptTernaryValue
  146. {
  147. VACOPT_TERNARY_DEFAULT = 0,
  148. VACOPT_TERNARY_DISABLED,
  149. VACOPT_TERNARY_ENABLED,
  150. } VacOptTernaryValue;
  151. /*
  152. * Parameters customizing behavior of VACUUM and ANALYZE.
  153. *
  154. * Note that at least one of VACOPT_VACUUM and VACOPT_ANALYZE must be set
  155. * in options.
  156. */
  157. typedef struct VacuumParams
  158. {
  159. int options; /* bitmask of VacuumOption */
  160. int freeze_min_age; /* min freeze age, -1 to use default */
  161. int freeze_table_age; /* age at which to scan whole table */
  162. int multixact_freeze_min_age; /* min multixact freeze age, -1 to
  163. * use default */
  164. int multixact_freeze_table_age; /* multixact age at which to scan
  165. * whole table */
  166. bool is_wraparound; /* force a for-wraparound vacuum */
  167. int log_min_duration; /* minimum execution threshold in ms at
  168. * which verbose logs are activated, -1
  169. * to use default */
  170. VacOptTernaryValue index_cleanup; /* Do index vacuum and cleanup,
  171. * default value depends on reloptions */
  172. VacOptTernaryValue truncate; /* Truncate empty pages at the end,
  173. * default value depends on reloptions */
  174. } VacuumParams;
  175. /* GUC parameters */
  176. extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */
  177. extern int vacuum_freeze_min_age;
  178. extern int vacuum_freeze_table_age;
  179. extern int vacuum_multixact_freeze_min_age;
  180. extern int vacuum_multixact_freeze_table_age;
  181. /* in commands/vacuum.c */
  182. extern void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel);
  183. extern void vacuum(List *relations, VacuumParams *params,
  184. BufferAccessStrategy bstrategy, bool isTopLevel);
  185. extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
  186. int *nindexes, Relation **Irel);
  187. extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
  188. extern double vac_estimate_reltuples(Relation relation,
  189. BlockNumber total_pages,
  190. BlockNumber scanned_pages,
  191. double scanned_tuples);
  192. extern void vac_update_relstats(Relation relation,
  193. BlockNumber num_pages,
  194. double num_tuples,
  195. BlockNumber num_all_visible_pages,
  196. bool hasindex,
  197. TransactionId frozenxid,
  198. MultiXactId minmulti,
  199. bool in_outer_xact);
  200. extern void vacuum_set_xid_limits(Relation rel,
  201. int freeze_min_age, int freeze_table_age,
  202. int multixact_freeze_min_age,
  203. int multixact_freeze_table_age,
  204. TransactionId *oldestXmin,
  205. TransactionId *freezeLimit,
  206. TransactionId *xidFullScanLimit,
  207. MultiXactId *multiXactCutoff,
  208. MultiXactId *mxactFullScanLimit);
  209. extern void vac_update_datfrozenxid(void);
  210. extern void vacuum_delay_point(void);
  211. extern bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple,
  212. int options);
  213. extern Relation vacuum_open_relation(Oid relid, RangeVar *relation,
  214. int options, bool verbose, LOCKMODE lmode);
  215. /* in commands/analyze.c */
  216. extern void analyze_rel(Oid relid, RangeVar *relation,
  217. VacuumParams *params, List *va_cols, bool in_outer_xact,
  218. BufferAccessStrategy bstrategy);
  219. extern bool std_typanalyze(VacAttrStats *stats);
  220. /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
  221. extern double anl_random_fract(void);
  222. extern double anl_init_selection_state(int n);
  223. extern double anl_get_next_S(double t, int n, double *stateptr);
  224. #endif /* VACUUM_H */
上海开阖软件有限公司 沪ICP备12045867号-1