vacuum.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*-------------------------------------------------------------------------
  2. *
  3. * vacuum.h
  4. * header file for postgres vacuum cleaner and statistics analyzer
  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/commands/vacuum.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef VACUUM_H
  15. #define VACUUM_H
  16. #include "access/htup.h"
  17. #include "access/genam.h"
  18. #include "access/parallel.h"
  19. #include "catalog/pg_class.h"
  20. #include "catalog/pg_statistic.h"
  21. #include "catalog/pg_type.h"
  22. #include "parser/parse_node.h"
  23. #include "storage/buf.h"
  24. #include "storage/lock.h"
  25. #include "utils/relcache.h"
  26. /*
  27. * Flags for amparallelvacuumoptions to control the participation of bulkdelete
  28. * and vacuumcleanup in parallel vacuum.
  29. */
  30. /*
  31. * Both bulkdelete and vacuumcleanup are disabled by default. This will be
  32. * used by IndexAM's that don't want to or cannot participate in parallel
  33. * vacuum. For example, if an index AM doesn't have a way to communicate the
  34. * index statistics allocated by the first ambulkdelete call to the subsequent
  35. * ones until amvacuumcleanup, the index AM cannot participate in parallel
  36. * vacuum.
  37. */
  38. #define VACUUM_OPTION_NO_PARALLEL 0
  39. /*
  40. * bulkdelete can be performed in parallel. This option can be used by
  41. * index AMs that need to scan indexes to delete tuples.
  42. */
  43. #define VACUUM_OPTION_PARALLEL_BULKDEL (1 << 0)
  44. /*
  45. * vacuumcleanup can be performed in parallel if bulkdelete is not performed
  46. * yet. This will be used by IndexAM's that can scan the index if the
  47. * bulkdelete is not performed.
  48. */
  49. #define VACUUM_OPTION_PARALLEL_COND_CLEANUP (1 << 1)
  50. /*
  51. * vacuumcleanup can be performed in parallel even if bulkdelete has already
  52. * processed the index. This will be used by IndexAM's that scan the index
  53. * during the cleanup phase of index irrespective of whether the index is
  54. * already scanned or not during bulkdelete phase.
  55. */
  56. #define VACUUM_OPTION_PARALLEL_CLEANUP (1 << 2)
  57. /* value for checking vacuum flags */
  58. #define VACUUM_OPTION_MAX_VALID_VALUE ((1 << 3) - 1)
  59. /* Abstract type for parallel vacuum state */
  60. typedef struct ParallelVacuumState ParallelVacuumState;
  61. /*----------
  62. * ANALYZE builds one of these structs for each attribute (column) that is
  63. * to be analyzed. The struct and subsidiary data are in anl_context,
  64. * so they live until the end of the ANALYZE operation.
  65. *
  66. * The type-specific typanalyze function is passed a pointer to this struct
  67. * and must return true to continue analysis, false to skip analysis of this
  68. * column. In the true case it must set the compute_stats and minrows fields,
  69. * and can optionally set extra_data to pass additional info to compute_stats.
  70. * minrows is its request for the minimum number of sample rows to be gathered
  71. * (but note this request might not be honored, eg if there are fewer rows
  72. * than that in the table).
  73. *
  74. * The compute_stats routine will be called after sample rows have been
  75. * gathered. Aside from this struct, it is passed:
  76. * fetchfunc: a function for accessing the column values from the
  77. * sample rows
  78. * samplerows: the number of sample tuples
  79. * totalrows: estimated total number of rows in relation
  80. * The fetchfunc may be called with rownum running from 0 to samplerows-1.
  81. * It returns a Datum and an isNull flag.
  82. *
  83. * compute_stats should set stats_valid true if it is able to compute
  84. * any useful statistics. If it does, the remainder of the struct holds
  85. * the information to be stored in a pg_statistic row for the column. Be
  86. * careful to allocate any pointed-to data in anl_context, which will NOT
  87. * be CurrentMemoryContext when compute_stats is called.
  88. *
  89. * Note: all comparisons done for statistical purposes should use the
  90. * underlying column's collation (attcollation), except in situations
  91. * where a noncollatable container type contains a collatable type;
  92. * in that case use the type's default collation. Be sure to record
  93. * the appropriate collation in stacoll.
  94. *----------
  95. */
  96. typedef struct VacAttrStats *VacAttrStatsP;
  97. typedef Datum (*AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum,
  98. bool *isNull);
  99. typedef void (*AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats,
  100. AnalyzeAttrFetchFunc fetchfunc,
  101. int samplerows,
  102. double totalrows);
  103. typedef struct VacAttrStats
  104. {
  105. /*
  106. * These fields are set up by the main ANALYZE code before invoking the
  107. * type-specific typanalyze function.
  108. *
  109. * Note: do not assume that the data being analyzed has the same datatype
  110. * shown in attr, ie do not trust attr->atttypid, attlen, etc. This is
  111. * because some index opclasses store a different type than the underlying
  112. * column/expression. Instead use attrtypid, attrtypmod, and attrtype for
  113. * information about the datatype being fed to the typanalyze function.
  114. * Likewise, use attrcollid not attr->attcollation.
  115. */
  116. Form_pg_attribute attr; /* copy of pg_attribute row for column */
  117. Oid attrtypid; /* type of data being analyzed */
  118. int32 attrtypmod; /* typmod of data being analyzed */
  119. Form_pg_type attrtype; /* copy of pg_type row for attrtypid */
  120. Oid attrcollid; /* collation of data being analyzed */
  121. MemoryContext anl_context; /* where to save long-lived data */
  122. /*
  123. * These fields must be filled in by the typanalyze routine, unless it
  124. * returns false.
  125. */
  126. AnalyzeAttrComputeStatsFunc compute_stats; /* function pointer */
  127. int minrows; /* Minimum # of rows wanted for stats */
  128. void *extra_data; /* for extra type-specific data */
  129. /*
  130. * These fields are to be filled in by the compute_stats routine. (They
  131. * are initialized to zero when the struct is created.)
  132. */
  133. bool stats_valid;
  134. float4 stanullfrac; /* fraction of entries that are NULL */
  135. int32 stawidth; /* average width of column values */
  136. float4 stadistinct; /* # distinct values */
  137. int16 stakind[STATISTIC_NUM_SLOTS];
  138. Oid staop[STATISTIC_NUM_SLOTS];
  139. Oid stacoll[STATISTIC_NUM_SLOTS];
  140. int numnumbers[STATISTIC_NUM_SLOTS];
  141. float4 *stanumbers[STATISTIC_NUM_SLOTS];
  142. int numvalues[STATISTIC_NUM_SLOTS];
  143. Datum *stavalues[STATISTIC_NUM_SLOTS];
  144. /*
  145. * These fields describe the stavalues[n] element types. They will be
  146. * initialized to match attrtypid, but a custom typanalyze function might
  147. * want to store an array of something other than the analyzed column's
  148. * elements. It should then overwrite these fields.
  149. */
  150. Oid statypid[STATISTIC_NUM_SLOTS];
  151. int16 statyplen[STATISTIC_NUM_SLOTS];
  152. bool statypbyval[STATISTIC_NUM_SLOTS];
  153. char statypalign[STATISTIC_NUM_SLOTS];
  154. /*
  155. * These fields are private to the main ANALYZE code and should not be
  156. * looked at by type-specific functions.
  157. */
  158. int tupattnum; /* attribute number within tuples */
  159. HeapTuple *rows; /* access info for std fetch function */
  160. TupleDesc tupDesc;
  161. Datum *exprvals; /* access info for index fetch function */
  162. bool *exprnulls;
  163. int rowstride;
  164. } VacAttrStats;
  165. /* flag bits for VacuumParams->options */
  166. #define VACOPT_VACUUM 0x01 /* do VACUUM */
  167. #define VACOPT_ANALYZE 0x02 /* do ANALYZE */
  168. #define VACOPT_VERBOSE 0x04 /* output INFO instrumentation messages */
  169. #define VACOPT_FREEZE 0x08 /* FREEZE option */
  170. #define VACOPT_FULL 0x10 /* FULL (non-concurrent) vacuum */
  171. #define VACOPT_SKIP_LOCKED 0x20 /* skip if cannot get lock */
  172. #define VACOPT_PROCESS_TOAST 0x40 /* process the TOAST table, if any */
  173. #define VACOPT_DISABLE_PAGE_SKIPPING 0x80 /* don't skip any pages */
  174. /*
  175. * Values used by index_cleanup and truncate params.
  176. *
  177. * VACOPTVALUE_UNSPECIFIED is used as an initial placeholder when VACUUM
  178. * command has no explicit value. When that happens the final usable value
  179. * comes from the corresponding reloption (though the reloption default is
  180. * usually used).
  181. */
  182. typedef enum VacOptValue
  183. {
  184. VACOPTVALUE_UNSPECIFIED = 0,
  185. VACOPTVALUE_AUTO,
  186. VACOPTVALUE_DISABLED,
  187. VACOPTVALUE_ENABLED,
  188. } VacOptValue;
  189. /*
  190. * Parameters customizing behavior of VACUUM and ANALYZE.
  191. *
  192. * Note that at least one of VACOPT_VACUUM and VACOPT_ANALYZE must be set
  193. * in options.
  194. */
  195. typedef struct VacuumParams
  196. {
  197. bits32 options; /* bitmask of VACOPT_* */
  198. int freeze_min_age; /* min freeze age, -1 to use default */
  199. int freeze_table_age; /* age at which to scan whole table */
  200. int multixact_freeze_min_age; /* min multixact freeze age, -1 to
  201. * use default */
  202. int multixact_freeze_table_age; /* multixact age at which to scan
  203. * whole table */
  204. bool is_wraparound; /* force a for-wraparound vacuum */
  205. int log_min_duration; /* minimum execution threshold in ms at
  206. * which autovacuum is logged, -1 to use
  207. * default */
  208. VacOptValue index_cleanup; /* Do index vacuum and cleanup */
  209. VacOptValue truncate; /* Truncate empty pages at the end */
  210. /*
  211. * The number of parallel vacuum workers. 0 by default which means choose
  212. * based on the number of indexes. -1 indicates parallel vacuum is
  213. * disabled.
  214. */
  215. int nworkers;
  216. } VacuumParams;
  217. /*
  218. * VacDeadItems stores TIDs whose index tuples are deleted by index vacuuming.
  219. */
  220. typedef struct VacDeadItems
  221. {
  222. int max_items; /* # slots allocated in array */
  223. int num_items; /* current # of entries */
  224. /* Sorted array of TIDs to delete from indexes */
  225. ItemPointerData items[FLEXIBLE_ARRAY_MEMBER];
  226. } VacDeadItems;
  227. #define MAXDEADITEMS(avail_mem) \
  228. (((avail_mem) - offsetof(VacDeadItems, items)) / sizeof(ItemPointerData))
  229. /* GUC parameters */
  230. extern PGDLLIMPORT int default_statistics_target; /* PGDLLIMPORT for PostGIS */
  231. extern PGDLLIMPORT int vacuum_freeze_min_age;
  232. extern PGDLLIMPORT int vacuum_freeze_table_age;
  233. extern PGDLLIMPORT int vacuum_multixact_freeze_min_age;
  234. extern PGDLLIMPORT int vacuum_multixact_freeze_table_age;
  235. extern PGDLLIMPORT int vacuum_failsafe_age;
  236. extern PGDLLIMPORT int vacuum_multixact_failsafe_age;
  237. /* Variables for cost-based parallel vacuum */
  238. extern PGDLLIMPORT pg_atomic_uint32 *VacuumSharedCostBalance;
  239. extern PGDLLIMPORT pg_atomic_uint32 *VacuumActiveNWorkers;
  240. extern PGDLLIMPORT int VacuumCostBalanceLocal;
  241. /* in commands/vacuum.c */
  242. extern void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel);
  243. extern void vacuum(List *relations, VacuumParams *params,
  244. BufferAccessStrategy bstrategy, bool isTopLevel);
  245. extern void vac_open_indexes(Relation relation, LOCKMODE lockmode,
  246. int *nindexes, Relation **Irel);
  247. extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode);
  248. extern double vac_estimate_reltuples(Relation relation,
  249. BlockNumber total_pages,
  250. BlockNumber scanned_pages,
  251. double scanned_tuples);
  252. extern void vac_update_relstats(Relation relation,
  253. BlockNumber num_pages,
  254. double num_tuples,
  255. BlockNumber num_all_visible_pages,
  256. bool hasindex,
  257. TransactionId frozenxid,
  258. MultiXactId minmulti,
  259. bool *frozenxid_updated,
  260. bool *minmulti_updated,
  261. bool in_outer_xact);
  262. extern bool vacuum_set_xid_limits(Relation rel,
  263. int freeze_min_age, int freeze_table_age,
  264. int multixact_freeze_min_age,
  265. int multixact_freeze_table_age,
  266. TransactionId *oldestXmin,
  267. MultiXactId *oldestMxact,
  268. TransactionId *freezeLimit,
  269. MultiXactId *multiXactCutoff);
  270. extern bool vacuum_xid_failsafe_check(TransactionId relfrozenxid,
  271. MultiXactId relminmxid);
  272. extern void vac_update_datfrozenxid(void);
  273. extern void vacuum_delay_point(void);
  274. extern bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple,
  275. bits32 options);
  276. extern Relation vacuum_open_relation(Oid relid, RangeVar *relation,
  277. bits32 options, bool verbose,
  278. LOCKMODE lmode);
  279. extern IndexBulkDeleteResult *vac_bulkdel_one_index(IndexVacuumInfo *ivinfo,
  280. IndexBulkDeleteResult *istat,
  281. VacDeadItems *dead_items);
  282. extern IndexBulkDeleteResult *vac_cleanup_one_index(IndexVacuumInfo *ivinfo,
  283. IndexBulkDeleteResult *istat);
  284. extern Size vac_max_items_to_alloc_size(int max_items);
  285. /* in commands/vacuumparallel.c */
  286. extern ParallelVacuumState *parallel_vacuum_init(Relation rel, Relation *indrels,
  287. int nindexes, int nrequested_workers,
  288. int max_items, int elevel,
  289. BufferAccessStrategy bstrategy);
  290. extern void parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats);
  291. extern VacDeadItems *parallel_vacuum_get_dead_items(ParallelVacuumState *pvs);
  292. extern void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs,
  293. long num_table_tuples,
  294. int num_index_scans);
  295. extern void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs,
  296. long num_table_tuples,
  297. int num_index_scans,
  298. bool estimated_count);
  299. extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc);
  300. /* in commands/analyze.c */
  301. extern void analyze_rel(Oid relid, RangeVar *relation,
  302. VacuumParams *params, List *va_cols, bool in_outer_xact,
  303. BufferAccessStrategy bstrategy);
  304. extern bool std_typanalyze(VacAttrStats *stats);
  305. /* in utils/misc/sampling.c --- duplicate of declarations in utils/sampling.h */
  306. extern double anl_random_fract(void);
  307. extern double anl_init_selection_state(int n);
  308. extern double anl_get_next_S(double t, int n, double *stateptr);
  309. #endif /* VACUUM_H */