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