PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vacuum.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/tableam.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_inherits.h"
#include "commands/cluster.h"
#include "commands/defrem.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/interrupt.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/guc_hooks.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for vacuum.c:

Go to the source code of this file.

Functions

static Listexpand_vacuum_rel (VacuumRelation *vrel, MemoryContext vac_context, int options)
 
static Listget_all_vacuum_rels (MemoryContext vac_context, int options)
 
static void vac_truncate_clog (TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
 
static bool vacuum_rel (Oid relid, RangeVar *relation, VacuumParams *params, BufferAccessStrategy bstrategy)
 
static double compute_parallel_delay (void)
 
static VacOptValue get_vacoptval_from_boolean (DefElem *def)
 
static bool vac_tid_reaped (ItemPointer itemptr, void *state)
 
bool check_vacuum_buffer_usage_limit (int *newval, void **extra, GucSource source)
 
void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
 
bool vacuum_is_permitted_for_relation (Oid relid, Form_pg_class reltuple, bits32 options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
 
bool vacuum_get_cutoffs (Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
 
bool vacuum_xid_failsafe_check (const struct VacuumCutoffs *cutoffs)
 
double vac_estimate_reltuples (Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
 
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)
 
void vac_update_datfrozenxid (void)
 
void vac_open_indexes (Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
 
void vac_close_indexes (int nindexes, Relation *Irel, LOCKMODE lockmode)
 
void vacuum_delay_point (void)
 
IndexBulkDeleteResultvac_bulkdel_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
 
IndexBulkDeleteResultvac_cleanup_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
 

Variables

int vacuum_freeze_min_age
 
int vacuum_freeze_table_age
 
int vacuum_multixact_freeze_min_age
 
int vacuum_multixact_freeze_table_age
 
int vacuum_failsafe_age
 
int vacuum_multixact_failsafe_age
 
double vacuum_cost_delay = 0
 
int vacuum_cost_limit = 200
 
bool VacuumFailsafeActive = false
 
pg_atomic_uint32VacuumSharedCostBalance = NULL
 
pg_atomic_uint32VacuumActiveNWorkers = NULL
 
int VacuumCostBalanceLocal = 0
 

Function Documentation

◆ check_vacuum_buffer_usage_limit()

bool check_vacuum_buffer_usage_limit ( int *  newval,
void **  extra,
GucSource  source 
)

Definition at line 124 of file vacuum.c.

126 {
127  /* Value upper and lower hard limits are inclusive */
128  if (*newval == 0 || (*newval >= MIN_BAS_VAC_RING_SIZE_KB &&
130  return true;
131 
132  /* Value does not fall within any allowable range */
133  GUC_check_errdetail("\"%s\" must be 0 or between %d kB and %d kB.",
134  "vacuum_buffer_usage_limit",
136 
137  return false;
138 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:476
#define MIN_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:277
#define MAX_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:278

References GUC_check_errdetail, MAX_BAS_VAC_RING_SIZE_KB, MIN_BAS_VAC_RING_SIZE_KB, and newval.

◆ compute_parallel_delay()

static double compute_parallel_delay ( void  )
static

Definition at line 2461 of file vacuum.c.

2462 {
2463  double msec = 0;
2464  uint32 shared_balance;
2465  int nworkers;
2466 
2467  /* Parallel vacuum must be active */
2469 
2471 
2472  /* At least count itself */
2473  Assert(nworkers >= 1);
2474 
2475  /* Update the shared cost balance value atomically */
2477 
2478  /* Compute the total local balance for the current worker */
2480 
2481  if ((shared_balance >= vacuum_cost_limit) &&
2482  (VacuumCostBalanceLocal > 0.5 * ((double) vacuum_cost_limit / nworkers)))
2483  {
2484  /* Compute sleep time based on the local cost balance */
2488  }
2489 
2490  /*
2491  * Reset the local balance as we accumulated it into the shared value.
2492  */
2493  VacuumCostBalance = 0;
2494 
2495  return msec;
2496 }
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:439
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:424
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
unsigned int uint32
Definition: c.h:492
#define Assert(condition)
Definition: c.h:837
int VacuumCostBalance
Definition: globals.c:156
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:102
double vacuum_cost_delay
Definition: vacuum.c:79
int VacuumCostBalanceLocal
Definition: vacuum.c:103
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:101
int vacuum_cost_limit
Definition: vacuum.c:80

References Assert, pg_atomic_add_fetch_u32(), pg_atomic_read_u32(), pg_atomic_sub_fetch_u32(), vacuum_cost_delay, vacuum_cost_limit, VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, and VacuumSharedCostBalance.

Referenced by vacuum_delay_point().

◆ ExecVacuum()

void ExecVacuum ( ParseState pstate,
VacuumStmt vacstmt,
bool  isTopLevel 
)

Definition at line 147 of file vacuum.c.

148 {
149  VacuumParams params;
150  BufferAccessStrategy bstrategy = NULL;
151  bool verbose = false;
152  bool skip_locked = false;
153  bool analyze = false;
154  bool freeze = false;
155  bool full = false;
156  bool disable_page_skipping = false;
157  bool process_main = true;
158  bool process_toast = true;
159  int ring_size;
160  bool skip_database_stats = false;
161  bool only_database_stats = false;
162  MemoryContext vac_context;
163  ListCell *lc;
164 
165  /* index_cleanup and truncate values unspecified for now */
168 
169  /* By default parallel vacuum is enabled */
170  params.nworkers = 0;
171 
172  /* Will be set later if we recurse to a TOAST table. */
173  params.toast_parent = InvalidOid;
174 
175  /*
176  * Set this to an invalid value so it is clear whether or not a
177  * BUFFER_USAGE_LIMIT was specified when making the access strategy.
178  */
179  ring_size = -1;
180 
181  /* Parse options list */
182  foreach(lc, vacstmt->options)
183  {
184  DefElem *opt = (DefElem *) lfirst(lc);
185 
186  /* Parse common options for VACUUM and ANALYZE */
187  if (strcmp(opt->defname, "verbose") == 0)
188  verbose = defGetBoolean(opt);
189  else if (strcmp(opt->defname, "skip_locked") == 0)
190  skip_locked = defGetBoolean(opt);
191  else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
192  {
193  const char *hintmsg;
194  int result;
195  char *vac_buffer_size;
196 
197  vac_buffer_size = defGetString(opt);
198 
199  /*
200  * Check that the specified value is valid and the size falls
201  * within the hard upper and lower limits if it is not 0.
202  */
203  if (!parse_int(vac_buffer_size, &result, GUC_UNIT_KB, &hintmsg) ||
204  (result != 0 &&
205  (result < MIN_BAS_VAC_RING_SIZE_KB || result > MAX_BAS_VAC_RING_SIZE_KB)))
206  {
207  ereport(ERROR,
208  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
209  errmsg("BUFFER_USAGE_LIMIT option must be 0 or between %d kB and %d kB",
211  hintmsg ? errhint("%s", _(hintmsg)) : 0));
212  }
213 
214  ring_size = result;
215  }
216  else if (!vacstmt->is_vacuumcmd)
217  ereport(ERROR,
218  (errcode(ERRCODE_SYNTAX_ERROR),
219  errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
220  parser_errposition(pstate, opt->location)));
221 
222  /* Parse options available on VACUUM */
223  else if (strcmp(opt->defname, "analyze") == 0)
224  analyze = defGetBoolean(opt);
225  else if (strcmp(opt->defname, "freeze") == 0)
226  freeze = defGetBoolean(opt);
227  else if (strcmp(opt->defname, "full") == 0)
228  full = defGetBoolean(opt);
229  else if (strcmp(opt->defname, "disable_page_skipping") == 0)
230  disable_page_skipping = defGetBoolean(opt);
231  else if (strcmp(opt->defname, "index_cleanup") == 0)
232  {
233  /* Interpret no string as the default, which is 'auto' */
234  if (!opt->arg)
236  else
237  {
238  char *sval = defGetString(opt);
239 
240  /* Try matching on 'auto' string, or fall back on boolean */
241  if (pg_strcasecmp(sval, "auto") == 0)
243  else
245  }
246  }
247  else if (strcmp(opt->defname, "process_main") == 0)
248  process_main = defGetBoolean(opt);
249  else if (strcmp(opt->defname, "process_toast") == 0)
250  process_toast = defGetBoolean(opt);
251  else if (strcmp(opt->defname, "truncate") == 0)
252  params.truncate = get_vacoptval_from_boolean(opt);
253  else if (strcmp(opt->defname, "parallel") == 0)
254  {
255  if (opt->arg == NULL)
256  {
257  ereport(ERROR,
258  (errcode(ERRCODE_SYNTAX_ERROR),
259  errmsg("parallel option requires a value between 0 and %d",
261  parser_errposition(pstate, opt->location)));
262  }
263  else
264  {
265  int nworkers;
266 
267  nworkers = defGetInt32(opt);
268  if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
269  ereport(ERROR,
270  (errcode(ERRCODE_SYNTAX_ERROR),
271  errmsg("parallel workers for vacuum must be between 0 and %d",
273  parser_errposition(pstate, opt->location)));
274 
275  /*
276  * Disable parallel vacuum, if user has specified parallel
277  * degree as zero.
278  */
279  if (nworkers == 0)
280  params.nworkers = -1;
281  else
282  params.nworkers = nworkers;
283  }
284  }
285  else if (strcmp(opt->defname, "skip_database_stats") == 0)
286  skip_database_stats = defGetBoolean(opt);
287  else if (strcmp(opt->defname, "only_database_stats") == 0)
288  only_database_stats = defGetBoolean(opt);
289  else
290  ereport(ERROR,
291  (errcode(ERRCODE_SYNTAX_ERROR),
292  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
293  parser_errposition(pstate, opt->location)));
294  }
295 
296  /* Set vacuum options */
297  params.options =
298  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
299  (verbose ? VACOPT_VERBOSE : 0) |
300  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
301  (analyze ? VACOPT_ANALYZE : 0) |
302  (freeze ? VACOPT_FREEZE : 0) |
303  (full ? VACOPT_FULL : 0) |
304  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
305  (process_main ? VACOPT_PROCESS_MAIN : 0) |
306  (process_toast ? VACOPT_PROCESS_TOAST : 0) |
307  (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
308  (only_database_stats ? VACOPT_ONLY_DATABASE_STATS : 0);
309 
310  /* sanity checks on options */
312  Assert((params.options & VACOPT_VACUUM) ||
313  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
314 
315  if ((params.options & VACOPT_FULL) && params.nworkers > 0)
316  ereport(ERROR,
317  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
318  errmsg("VACUUM FULL cannot be performed in parallel")));
319 
320  /*
321  * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
322  * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
323  * we'll permit that.
324  */
325  if (ring_size != -1 && (params.options & VACOPT_FULL) &&
326  !(params.options & VACOPT_ANALYZE))
327  ereport(ERROR,
328  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
329  errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
330 
331  /*
332  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
333  */
334  if (!(params.options & VACOPT_ANALYZE))
335  {
336  foreach(lc, vacstmt->rels)
337  {
339 
340  if (vrel->va_cols != NIL)
341  ereport(ERROR,
342  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
343  errmsg("ANALYZE option must be specified when a column list is provided")));
344  }
345  }
346 
347 
348  /*
349  * Sanity check DISABLE_PAGE_SKIPPING option.
350  */
351  if ((params.options & VACOPT_FULL) != 0 &&
352  (params.options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
353  ereport(ERROR,
354  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
355  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
356 
357  /* sanity check for PROCESS_TOAST */
358  if ((params.options & VACOPT_FULL) != 0 &&
359  (params.options & VACOPT_PROCESS_TOAST) == 0)
360  ereport(ERROR,
361  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
362  errmsg("PROCESS_TOAST required with VACUUM FULL")));
363 
364  /* sanity check for ONLY_DATABASE_STATS */
365  if (params.options & VACOPT_ONLY_DATABASE_STATS)
366  {
367  Assert(params.options & VACOPT_VACUUM);
368  if (vacstmt->rels != NIL)
369  ereport(ERROR,
370  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
371  errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
372  /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
373  if (params.options & ~(VACOPT_VACUUM |
378  ereport(ERROR,
379  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
380  errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
381  }
382 
383  /*
384  * All freeze ages are zero if the FREEZE option is given; otherwise pass
385  * them as -1 which means to use the default values.
386  */
387  if (params.options & VACOPT_FREEZE)
388  {
389  params.freeze_min_age = 0;
390  params.freeze_table_age = 0;
391  params.multixact_freeze_min_age = 0;
392  params.multixact_freeze_table_age = 0;
393  }
394  else
395  {
396  params.freeze_min_age = -1;
397  params.freeze_table_age = -1;
398  params.multixact_freeze_min_age = -1;
399  params.multixact_freeze_table_age = -1;
400  }
401 
402  /* user-invoked vacuum is never "for wraparound" */
403  params.is_wraparound = false;
404 
405  /* user-invoked vacuum uses VACOPT_VERBOSE instead of log_min_duration */
406  params.log_min_duration = -1;
407 
408  /*
409  * Create special memory context for cross-transaction storage.
410  *
411  * Since it is a child of PortalContext, it will go away eventually even
412  * if we suffer an error; there's no need for special abort cleanup logic.
413  */
414  vac_context = AllocSetContextCreate(PortalContext,
415  "Vacuum",
417 
418  /*
419  * Make a buffer strategy object in the cross-transaction memory context.
420  * We needn't bother making this for VACUUM (FULL) or VACUUM
421  * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
422  * ANALYZE) is possible, so we'd better ensure that we make a strategy
423  * when we see ANALYZE.
424  */
425  if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
426  VACOPT_FULL)) == 0 ||
427  (params.options & VACOPT_ANALYZE) != 0)
428  {
429 
430  MemoryContext old_context = MemoryContextSwitchTo(vac_context);
431 
432  Assert(ring_size >= -1);
433 
434  /*
435  * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
436  * command, it overrides the value of VacuumBufferUsageLimit. Either
437  * value may be 0, in which case GetAccessStrategyWithSize() will
438  * return NULL, effectively allowing full use of shared buffers.
439  */
440  if (ring_size == -1)
441  ring_size = VacuumBufferUsageLimit;
442 
443  bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, ring_size);
444 
445  MemoryContextSwitchTo(old_context);
446  }
447 
448  /* Now go through the common routine */
449  vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
450 
451  /* Finally, clean up the vacuum memory context */
452  MemoryContextDelete(vac_context);
453 }
#define MAX_PARALLEL_WORKER_LIMIT
@ BAS_VACUUM
Definition: bufmgr.h:39
int32 defGetInt32(DefElem *def)
Definition: define.c:162
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define _(x)
Definition: elog.c:90
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584
int VacuumBufferUsageLimit
Definition: globals.c:148
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:2871
#define GUC_UNIT_KB
Definition: guc.h:228
int verbose
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define InvalidOid
Definition: postgres_ext.h:36
MemoryContextSwitchTo(old_ctx)
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:3051
char * defname
Definition: parsenodes.h:817
ParseLoc location
Definition: parsenodes.h:821
Node * arg
Definition: parsenodes.h:818
int nworkers
Definition: vacuum.h:239
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
Oid toast_parent
Definition: vacuum.h:232
VacOptValue index_cleanup
Definition: vacuum.h:230
List * options
Definition: parsenodes.h:3868
bool is_vacuumcmd
Definition: parsenodes.h:3870
List * rels
Definition: parsenodes.h:3869
static VacOptValue get_vacoptval_from_boolean(DefElem *def)
Definition: vacuum.c:2505
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition: vacuum.c:478
#define VACOPT_FREEZE
Definition: vacuum.h:183
#define VACOPT_SKIP_LOCKED
Definition: vacuum.h:185
#define VACOPT_VACUUM
Definition: vacuum.h:180
#define VACOPT_VERBOSE
Definition: vacuum.h:182
#define VACOPT_FULL
Definition: vacuum.h:184
#define VACOPT_SKIP_DATABASE_STATS
Definition: vacuum.h:189
@ VACOPTVALUE_AUTO
Definition: vacuum.h:203
@ VACOPTVALUE_UNSPECIFIED
Definition: vacuum.h:202
#define VACOPT_PROCESS_TOAST
Definition: vacuum.h:187
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition: vacuum.h:188
#define VACOPT_ONLY_DATABASE_STATS
Definition: vacuum.h:190
#define VACOPT_PROCESS_MAIN
Definition: vacuum.h:186
#define VACOPT_ANALYZE
Definition: vacuum.h:181

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze(), DefElem::arg, Assert, BAS_VACUUM, defGetBoolean(), defGetInt32(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, get_vacoptval_from_boolean(), GetAccessStrategyWithSize(), GUC_UNIT_KB, VacuumParams::index_cleanup, InvalidOid, VacuumStmt::is_vacuumcmd, VacuumParams::is_wraparound, lfirst, lfirst_node, DefElem::location, VacuumParams::log_min_duration, MAX_BAS_VAC_RING_SIZE_KB, MAX_PARALLEL_WORKER_LIMIT, MemoryContextDelete(), MemoryContextSwitchTo(), MIN_BAS_VAC_RING_SIZE_KB, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, VacuumParams::nworkers, VacuumParams::options, VacuumStmt::options, parse_int(), parser_errposition(), pg_strcasecmp(), PortalContext, VacuumStmt::rels, VacuumParams::toast_parent, VacuumParams::truncate, VacuumRelation::va_cols, VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FREEZE, VACOPT_FULL, VACOPT_ONLY_DATABASE_STATS, VACOPT_PROCESS_MAIN, VACOPT_PROCESS_TOAST, VACOPT_SKIP_DATABASE_STATS, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, VACOPT_VERBOSE, VACOPTVALUE_AUTO, VACOPTVALUE_UNSPECIFIED, vacuum(), VacuumBufferUsageLimit, and verbose.

Referenced by standard_ProcessUtility().

◆ expand_vacuum_rel()

static List * expand_vacuum_rel ( VacuumRelation vrel,
MemoryContext  vac_context,
int  options 
)
static

Definition at line 867 of file vacuum.c.

869 {
870  List *vacrels = NIL;
871  MemoryContext oldcontext;
872 
873  /* If caller supplied OID, there's nothing we need do here. */
874  if (OidIsValid(vrel->oid))
875  {
876  oldcontext = MemoryContextSwitchTo(vac_context);
877  vacrels = lappend(vacrels, vrel);
878  MemoryContextSwitchTo(oldcontext);
879  }
880  else
881  {
882  /*
883  * Process a specific relation, and possibly partitions or child
884  * tables thereof.
885  */
886  Oid relid;
887  HeapTuple tuple;
888  Form_pg_class classForm;
889  bool include_children;
890  bool is_partitioned_table;
891  int rvr_opts;
892 
893  /*
894  * Since autovacuum workers supply OIDs when calling vacuum(), no
895  * autovacuum worker should reach this code.
896  */
898 
899  /*
900  * We transiently take AccessShareLock to protect the syscache lookup
901  * below, as well as find_all_inheritors's expectation that the caller
902  * holds some lock on the starting relation.
903  */
904  rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
905  relid = RangeVarGetRelidExtended(vrel->relation,
907  rvr_opts,
908  NULL, NULL);
909 
910  /*
911  * If the lock is unavailable, emit the same log statement that
912  * vacuum_rel() and analyze_rel() would.
913  */
914  if (!OidIsValid(relid))
915  {
916  if (options & VACOPT_VACUUM)
918  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
919  errmsg("skipping vacuum of \"%s\" --- lock not available",
920  vrel->relation->relname)));
921  else
923  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
924  errmsg("skipping analyze of \"%s\" --- lock not available",
925  vrel->relation->relname)));
926  return vacrels;
927  }
928 
929  /*
930  * To check whether the relation is a partitioned table and its
931  * ownership, fetch its syscache entry.
932  */
933  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
934  if (!HeapTupleIsValid(tuple))
935  elog(ERROR, "cache lookup failed for relation %u", relid);
936  classForm = (Form_pg_class) GETSTRUCT(tuple);
937 
938  /*
939  * Make a returnable VacuumRelation for this rel if the user has the
940  * required privileges.
941  */
942  if (vacuum_is_permitted_for_relation(relid, classForm, options))
943  {
944  oldcontext = MemoryContextSwitchTo(vac_context);
945  vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
946  relid,
947  vrel->va_cols));
948  MemoryContextSwitchTo(oldcontext);
949  }
950 
951  /*
952  * Vacuuming a partitioned table with ONLY will not do anything since
953  * the partitioned table itself is empty. Issue a warning if the user
954  * requests this.
955  */
956  include_children = vrel->relation->inh;
957  is_partitioned_table = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
958  if ((options & VACOPT_VACUUM) && is_partitioned_table && !include_children)
960  (errmsg("VACUUM ONLY of partitioned table \"%s\" has no effect",
961  vrel->relation->relname)));
962 
963  ReleaseSysCache(tuple);
964 
965  /*
966  * Unless the user has specified ONLY, make relation list entries for
967  * its partitions or inheritance child tables. Note that the list
968  * returned by find_all_inheritors() includes the passed-in OID, so we
969  * have to skip that. There's no point in taking locks on the
970  * individual partitions or child tables yet, and doing so would just
971  * add unnecessary deadlock risk. For this last reason, we do not yet
972  * check the ownership of the partitions/tables, which get added to
973  * the list to process. Ownership will be checked later on anyway.
974  */
975  if (include_children)
976  {
977  List *part_oids = find_all_inheritors(relid, NoLock, NULL);
978  ListCell *part_lc;
979 
980  foreach(part_lc, part_oids)
981  {
982  Oid part_oid = lfirst_oid(part_lc);
983 
984  if (part_oid == relid)
985  continue; /* ignore original table */
986 
987  /*
988  * We omit a RangeVar since it wouldn't be appropriate to
989  * complain about failure to open one of these relations
990  * later.
991  */
992  oldcontext = MemoryContextSwitchTo(vac_context);
993  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
994  part_oid,
995  vrel->va_cols));
996  MemoryContextSwitchTo(oldcontext);
997  }
998  }
999 
1000  /*
1001  * Release lock again. This means that by the time we actually try to
1002  * process the table, it might be gone or renamed. In the former case
1003  * we'll silently ignore it; in the latter case we'll process it
1004  * anyway, but we must beware that the RangeVar doesn't necessarily
1005  * identify it anymore. This isn't ideal, perhaps, but there's little
1006  * practical alternative, since we're typically going to commit this
1007  * transaction and begin a new one between now and then. Moreover,
1008  * holding locks on multiple relations would create significant risk
1009  * of deadlock.
1010  */
1012  }
1013 
1014  return vacrels;
1015 }
#define OidIsValid(objectId)
Definition: c.h:754
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:225
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
List * lappend(List *list, void *datum)
Definition: list.c:339
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:226
#define NoLock
Definition: lockdefs.h:34
#define AccessShareLock
Definition: lockdefs.h:36
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:857
#define AmAutoVacuumWorkerProcess()
Definition: miscadmin.h:373
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:441
@ RVR_SKIP_LOCKED
Definition: namespace.h:74
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255
#define lfirst_oid(lc)
Definition: pg_list.h:174
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:54
char * relname
Definition: primnodes.h:82
bool inh
Definition: primnodes.h:85
RangeVar * relation
Definition: parsenodes.h:3883
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
bool vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple, bits32 options)
Definition: vacuum.c:703

References AccessShareLock, AmAutoVacuumWorkerProcess, Assert, elog, ereport, errcode(), errmsg(), ERROR, find_all_inheritors(), GETSTRUCT, HeapTupleIsValid, RangeVar::inh, lappend(), lfirst_oid, makeVacuumRelation(), MemoryContextSwitchTo(), NIL, NoLock, ObjectIdGetDatum(), VacuumRelation::oid, OidIsValid, RangeVarGetRelidExtended(), VacuumRelation::relation, ReleaseSysCache(), RangeVar::relname, RVR_SKIP_LOCKED, SearchSysCache1(), UnlockRelationOid(), VacuumRelation::va_cols, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, vacuum_is_permitted_for_relation(), and WARNING.

Referenced by vacuum().

◆ get_all_vacuum_rels()

static List * get_all_vacuum_rels ( MemoryContext  vac_context,
int  options 
)
static

Definition at line 1022 of file vacuum.c.

1023 {
1024  List *vacrels = NIL;
1025  Relation pgclass;
1026  TableScanDesc scan;
1027  HeapTuple tuple;
1028 
1029  pgclass = table_open(RelationRelationId, AccessShareLock);
1030 
1031  scan = table_beginscan_catalog(pgclass, 0, NULL);
1032 
1033  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1034  {
1035  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
1036  MemoryContext oldcontext;
1037  Oid relid = classForm->oid;
1038 
1039  /*
1040  * We include partitioned tables here; depending on which operation is
1041  * to be performed, caller will decide whether to process or ignore
1042  * them.
1043  */
1044  if (classForm->relkind != RELKIND_RELATION &&
1045  classForm->relkind != RELKIND_MATVIEW &&
1046  classForm->relkind != RELKIND_PARTITIONED_TABLE)
1047  continue;
1048 
1049  /* check permissions of relation */
1050  if (!vacuum_is_permitted_for_relation(relid, classForm, options))
1051  continue;
1052 
1053  /*
1054  * Build VacuumRelation(s) specifying the table OIDs to be processed.
1055  * We omit a RangeVar since it wouldn't be appropriate to complain
1056  * about failure to open one of these relations later.
1057  */
1058  oldcontext = MemoryContextSwitchTo(vac_context);
1059  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
1060  relid,
1061  NIL));
1062  MemoryContextSwitchTo(oldcontext);
1063  }
1064 
1065  table_endscan(scan);
1066  table_close(pgclass, AccessShareLock);
1067 
1068  return vacrels;
1069 }
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1243
@ ForwardScanDirection
Definition: sdir.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1028

References AccessShareLock, ForwardScanDirection, GETSTRUCT, heap_getnext(), lappend(), makeVacuumRelation(), MemoryContextSwitchTo(), NIL, table_beginscan_catalog(), table_close(), table_endscan(), table_open(), and vacuum_is_permitted_for_relation().

Referenced by vacuum().

◆ get_vacoptval_from_boolean()

static VacOptValue get_vacoptval_from_boolean ( DefElem def)
static

Definition at line 2505 of file vacuum.c.

2506 {
2508 }
@ VACOPTVALUE_ENABLED
Definition: vacuum.h:205
@ VACOPTVALUE_DISABLED
Definition: vacuum.h:204

References defGetBoolean(), VACOPTVALUE_DISABLED, and VACOPTVALUE_ENABLED.

Referenced by ExecVacuum().

◆ vac_bulkdel_one_index()

IndexBulkDeleteResult* vac_bulkdel_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat,
TidStore dead_items,
VacDeadItemsInfo dead_items_info 
)

Definition at line 2516 of file vacuum.c.

2518 {
2519  /* Do bulk deletion */
2520  istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2521  dead_items);
2522 
2523  ereport(ivinfo->message_level,
2524  (errmsg("scanned index \"%s\" to remove %lld row versions",
2525  RelationGetRelationName(ivinfo->index),
2526  (long long) dead_items_info->num_items)));
2527 
2528  return istat;
2529 }
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition: indexam.c:750
#define RelationGetRelationName(relation)
Definition: rel.h:539
Relation index
Definition: genam.h:46
int message_level
Definition: genam.h:51
int64 num_items
Definition: vacuum.h:288
static bool vac_tid_reaped(ItemPointer itemptr, void *state)
Definition: vacuum.c:2563

References ereport, errmsg(), IndexVacuumInfo::index, index_bulk_delete(), IndexVacuumInfo::message_level, VacDeadItemsInfo::num_items, RelationGetRelationName, and vac_tid_reaped().

Referenced by lazy_vacuum_one_index(), and parallel_vacuum_process_one_index().

◆ vac_cleanup_one_index()

IndexBulkDeleteResult* vac_cleanup_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat 
)

Definition at line 2537 of file vacuum.c.

2538 {
2539  istat = index_vacuum_cleanup(ivinfo, istat);
2540 
2541  if (istat)
2542  ereport(ivinfo->message_level,
2543  (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2544  RelationGetRelationName(ivinfo->index),
2545  istat->num_index_tuples,
2546  istat->num_pages),
2547  errdetail("%.0f index row versions were removed.\n"
2548  "%u index pages were newly deleted.\n"
2549  "%u index pages are currently deleted, of which %u are currently reusable.",
2550  istat->tuples_removed,
2551  istat->pages_newly_deleted,
2552  istat->pages_deleted, istat->pages_free)));
2553 
2554  return istat;
2555 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *istat)
Definition: indexam.c:771
BlockNumber pages_deleted
Definition: genam.h:82
BlockNumber pages_newly_deleted
Definition: genam.h:81
BlockNumber pages_free
Definition: genam.h:83
BlockNumber num_pages
Definition: genam.h:77
double tuples_removed
Definition: genam.h:80
double num_index_tuples
Definition: genam.h:79

References ereport, errdetail(), errmsg(), IndexVacuumInfo::index, index_vacuum_cleanup(), IndexVacuumInfo::message_level, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, IndexBulkDeleteResult::pages_deleted, IndexBulkDeleteResult::pages_free, IndexBulkDeleteResult::pages_newly_deleted, RelationGetRelationName, and IndexBulkDeleteResult::tuples_removed.

Referenced by lazy_cleanup_one_index(), and parallel_vacuum_process_one_index().

◆ vac_close_indexes()

void vac_close_indexes ( int  nindexes,
Relation Irel,
LOCKMODE  lockmode 
)

Definition at line 2341 of file vacuum.c.

2342 {
2343  if (Irel == NULL)
2344  return;
2345 
2346  while (nindexes--)
2347  {
2348  Relation ind = Irel[nindexes];
2349 
2350  index_close(ind, lockmode);
2351  }
2352  pfree(Irel);
2353 }
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
void pfree(void *pointer)
Definition: mcxt.c:1521

References index_close(), and pfree().

Referenced by do_analyze_rel(), heap_vacuum_rel(), and parallel_vacuum_main().

◆ vac_estimate_reltuples()

double vac_estimate_reltuples ( Relation  relation,
BlockNumber  total_pages,
BlockNumber  scanned_pages,
double  scanned_tuples 
)

Definition at line 1314 of file vacuum.c.

1318 {
1319  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1320  double old_rel_tuples = relation->rd_rel->reltuples;
1321  double old_density;
1322  double unscanned_pages;
1323  double total_tuples;
1324 
1325  /* If we did scan the whole table, just use the count as-is */
1326  if (scanned_pages >= total_pages)
1327  return scanned_tuples;
1328 
1329  /*
1330  * When successive VACUUM commands scan the same few pages again and
1331  * again, without anything from the table really changing, there is a risk
1332  * that our beliefs about tuple density will gradually become distorted.
1333  * This might be caused by vacuumlazy.c implementation details, such as
1334  * its tendency to always scan the last heap page. Handle that here.
1335  *
1336  * If the relation is _exactly_ the same size according to the existing
1337  * pg_class entry, and only a few of its pages (less than 2%) were
1338  * scanned, keep the existing value of reltuples. Also keep the existing
1339  * value when only a subset of rel's pages <= a single page were scanned.
1340  *
1341  * (Note: we might be returning -1 here.)
1342  */
1343  if (old_rel_pages == total_pages &&
1344  scanned_pages < (double) total_pages * 0.02)
1345  return old_rel_tuples;
1346  if (scanned_pages <= 1)
1347  return old_rel_tuples;
1348 
1349  /*
1350  * If old density is unknown, we can't do much except scale up
1351  * scanned_tuples to match total_pages.
1352  */
1353  if (old_rel_tuples < 0 || old_rel_pages == 0)
1354  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1355 
1356  /*
1357  * Okay, we've covered the corner cases. The normal calculation is to
1358  * convert the old measurement to a density (tuples per page), then
1359  * estimate the number of tuples in the unscanned pages using that figure,
1360  * and finally add on the number of tuples in the scanned pages.
1361  */
1362  old_density = old_rel_tuples / old_rel_pages;
1363  unscanned_pages = (double) total_pages - (double) scanned_pages;
1364  total_tuples = old_density * unscanned_pages + scanned_tuples;
1365  return floor(total_tuples + 0.5);
1366 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:111

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

◆ vac_open_indexes()

void vac_open_indexes ( Relation  relation,
LOCKMODE  lockmode,
int *  nindexes,
Relation **  Irel 
)

Definition at line 2298 of file vacuum.c.

2300 {
2301  List *indexoidlist;
2302  ListCell *indexoidscan;
2303  int i;
2304 
2305  Assert(lockmode != NoLock);
2306 
2307  indexoidlist = RelationGetIndexList(relation);
2308 
2309  /* allocate enough memory for all indexes */
2310  i = list_length(indexoidlist);
2311 
2312  if (i > 0)
2313  *Irel = (Relation *) palloc(i * sizeof(Relation));
2314  else
2315  *Irel = NULL;
2316 
2317  /* collect just the ready indexes */
2318  i = 0;
2319  foreach(indexoidscan, indexoidlist)
2320  {
2321  Oid indexoid = lfirst_oid(indexoidscan);
2322  Relation indrel;
2323 
2324  indrel = index_open(indexoid, lockmode);
2325  if (indrel->rd_index->indisready)
2326  (*Irel)[i++] = indrel;
2327  else
2328  index_close(indrel, lockmode);
2329  }
2330 
2331  *nindexes = i;
2332 
2333  list_free(indexoidlist);
2334 }
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
int i
Definition: isn.c:72
void list_free(List *list)
Definition: list.c:1546
void * palloc(Size size)
Definition: mcxt.c:1317
static int list_length(const List *l)
Definition: pg_list.h:152
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4766
Form_pg_index rd_index
Definition: rel.h:192

References Assert, i, index_close(), index_open(), lfirst_oid, list_free(), list_length(), NoLock, palloc(), RelationData::rd_index, and RelationGetIndexList().

Referenced by do_analyze_rel(), heap_vacuum_rel(), and parallel_vacuum_main().

◆ vac_tid_reaped()

static bool vac_tid_reaped ( ItemPointer  itemptr,
void *  state 
)
static

Definition at line 2563 of file vacuum.c.

2564 {
2565  TidStore *dead_items = (TidStore *) state;
2566 
2567  return TidStoreIsMember(dead_items, itemptr);
2568 }
Definition: regguts.h:323
bool TidStoreIsMember(TidStore *ts, ItemPointer tid)
Definition: tidstore.c:429

References TidStoreIsMember().

Referenced by vac_bulkdel_one_index().

◆ vac_truncate_clog()

static void vac_truncate_clog ( TransactionId  frozenXID,
MultiXactId  minMulti,
TransactionId  lastSaneFrozenXid,
MultiXactId  lastSaneMinMulti 
)
static

Definition at line 1805 of file vacuum.c.

1809 {
1811  Relation relation;
1812  TableScanDesc scan;
1813  HeapTuple tuple;
1814  Oid oldestxid_datoid;
1815  Oid minmulti_datoid;
1816  bool bogus = false;
1817  bool frozenAlreadyWrapped = false;
1818 
1819  /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
1820  LWLockAcquire(WrapLimitsVacuumLock, LW_EXCLUSIVE);
1821 
1822  /* init oldest datoids to sync with my frozenXID/minMulti values */
1823  oldestxid_datoid = MyDatabaseId;
1824  minmulti_datoid = MyDatabaseId;
1825 
1826  /*
1827  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1828  *
1829  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1830  * the values could change while we look at them. Fetch each one just
1831  * once to ensure sane behavior of the comparison logic. (Here, as in
1832  * many other places, we assume that fetching or updating an XID in shared
1833  * storage is atomic.)
1834  *
1835  * Note: we need not worry about a race condition with new entries being
1836  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1837  * existing DB's datfrozenxid, and that source DB cannot be ours because
1838  * of the interlock against copying a DB containing an active backend.
1839  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1840  * concurrently modify the datfrozenxid's of different databases, the
1841  * worst possible outcome is that pg_xact is not truncated as aggressively
1842  * as it could be.
1843  */
1844  relation = table_open(DatabaseRelationId, AccessShareLock);
1845 
1846  scan = table_beginscan_catalog(relation, 0, NULL);
1847 
1848  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1849  {
1850  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1851  TransactionId datfrozenxid = dbform->datfrozenxid;
1852  TransactionId datminmxid = dbform->datminmxid;
1853 
1856 
1857  /*
1858  * If database is in the process of getting dropped, or has been
1859  * interrupted while doing so, no connections to it are possible
1860  * anymore. Therefore we don't need to take it into account here.
1861  * Which is good, because it can't be processed by autovacuum either.
1862  */
1864  {
1865  elog(DEBUG2,
1866  "skipping invalid database \"%s\" while computing relfrozenxid",
1867  NameStr(dbform->datname));
1868  continue;
1869  }
1870 
1871  /*
1872  * If things are working properly, no database should have a
1873  * datfrozenxid or datminmxid that is "in the future". However, such
1874  * cases have been known to arise due to bugs in pg_upgrade. If we
1875  * see any entries that are "in the future", chicken out and don't do
1876  * anything. This ensures we won't truncate clog before those
1877  * databases have been scanned and cleaned up. (We will issue the
1878  * "already wrapped" warning if appropriate, though.)
1879  */
1880  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1881  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1882  bogus = true;
1883 
1884  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1885  frozenAlreadyWrapped = true;
1886  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1887  {
1888  frozenXID = datfrozenxid;
1889  oldestxid_datoid = dbform->oid;
1890  }
1891 
1892  if (MultiXactIdPrecedes(datminmxid, minMulti))
1893  {
1894  minMulti = datminmxid;
1895  minmulti_datoid = dbform->oid;
1896  }
1897  }
1898 
1899  table_endscan(scan);
1900 
1901  table_close(relation, AccessShareLock);
1902 
1903  /*
1904  * Do not truncate CLOG if we seem to have suffered wraparound already;
1905  * the computed minimum XID might be bogus. This case should now be
1906  * impossible due to the defenses in GetNewTransactionId, but we keep the
1907  * test anyway.
1908  */
1909  if (frozenAlreadyWrapped)
1910  {
1911  ereport(WARNING,
1912  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1913  errdetail("You might have already suffered transaction-wraparound data loss.")));
1914  LWLockRelease(WrapLimitsVacuumLock);
1915  return;
1916  }
1917 
1918  /* chicken out if data is bogus in any other way */
1919  if (bogus)
1920  {
1921  LWLockRelease(WrapLimitsVacuumLock);
1922  return;
1923  }
1924 
1925  /*
1926  * Advance the oldest value for commit timestamps before truncating, so
1927  * that if a user requests a timestamp for a transaction we're truncating
1928  * away right after this point, they get NULL instead of an ugly "file not
1929  * found" error from slru.c. This doesn't matter for xact/multixact
1930  * because they are not subject to arbitrary lookups from users.
1931  */
1932  AdvanceOldestCommitTsXid(frozenXID);
1933 
1934  /*
1935  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1936  */
1937  TruncateCLOG(frozenXID, oldestxid_datoid);
1938  TruncateCommitTs(frozenXID);
1939  TruncateMultiXact(minMulti, minmulti_datoid);
1940 
1941  /*
1942  * Update the wrap limit for GetNewTransactionId and creation of new
1943  * MultiXactIds. Note: these functions will also signal the postmaster
1944  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1945  * signaling twice?
1946  */
1947  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1948  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1949 
1950  LWLockRelease(WrapLimitsVacuumLock);
1951 }
#define NameStr(name)
Definition: c.h:725
uint32 TransactionId
Definition: c.h:631
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition: clog.c:1000
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:936
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:883
bool database_is_invalid_form(Form_pg_database datform)
Definition: dbcommands.c:3211
#define DEBUG2
Definition: elog.h:29
Oid MyDatabaseId
Definition: globals.c:93
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3317
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2362
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:3102
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
TransactionId datfrozenxid
Definition: pg_database.h:62
TransactionId datminmxid
Definition: pg_database.h:65
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
FormData_pg_database
Definition: pg_database.h:89
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:315
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:372

References AccessShareLock, AdvanceOldestCommitTsXid(), Assert, database_is_invalid_form(), datfrozenxid, datminmxid, DEBUG2, elog, ereport, errdetail(), errmsg(), FormData_pg_database, ForwardScanDirection, GETSTRUCT, heap_getnext(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, NameStr, ReadNextTransactionId(), SetMultiXactIdLimit(), SetTransactionIdLimit(), table_beginscan_catalog(), table_close(), table_endscan(), table_open(), TransactionIdIsNormal, TransactionIdPrecedes(), TruncateCLOG(), TruncateCommitTs(), TruncateMultiXact(), and WARNING.

Referenced by vac_update_datfrozenxid().

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1586 of file vacuum.c.

1587 {
1588  HeapTuple tuple;
1589  Form_pg_database dbform;
1590  Relation relation;
1591  SysScanDesc scan;
1592  HeapTuple classTup;
1593  TransactionId newFrozenXid;
1594  MultiXactId newMinMulti;
1595  TransactionId lastSaneFrozenXid;
1596  MultiXactId lastSaneMinMulti;
1597  bool bogus = false;
1598  bool dirty = false;
1599  ScanKeyData key[1];
1600  void *inplace_state;
1601 
1602  /*
1603  * Restrict this task to one backend per database. This avoids race
1604  * conditions that would move datfrozenxid or datminmxid backward. It
1605  * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1606  * datfrozenxid passed to an earlier vac_truncate_clog() call.
1607  */
1609 
1610  /*
1611  * Initialize the "min" calculation with
1612  * GetOldestNonRemovableTransactionId(), which is a reasonable
1613  * approximation to the minimum relfrozenxid for not-yet-committed
1614  * pg_class entries for new tables; see AddNewRelationTuple(). So we
1615  * cannot produce a wrong minimum by starting with this.
1616  */
1617  newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
1618 
1619  /*
1620  * Similarly, initialize the MultiXact "min" with the value that would be
1621  * used on pg_class for new tables. See AddNewRelationTuple().
1622  */
1623  newMinMulti = GetOldestMultiXactId();
1624 
1625  /*
1626  * Identify the latest relfrozenxid and relminmxid values that we could
1627  * validly see during the scan. These are conservative values, but it's
1628  * not really worth trying to be more exact.
1629  */
1630  lastSaneFrozenXid = ReadNextTransactionId();
1631  lastSaneMinMulti = ReadNextMultiXactId();
1632 
1633  /*
1634  * We must seqscan pg_class to find the minimum Xid, because there is no
1635  * index that can help us here.
1636  *
1637  * See vac_truncate_clog() for the race condition to prevent.
1638  */
1639  relation = table_open(RelationRelationId, AccessShareLock);
1640 
1641  scan = systable_beginscan(relation, InvalidOid, false,
1642  NULL, 0, NULL);
1643 
1644  while ((classTup = systable_getnext(scan)) != NULL)
1645  {
1646  volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1647  TransactionId relfrozenxid = classForm->relfrozenxid;
1648  TransactionId relminmxid = classForm->relminmxid;
1649 
1650  /*
1651  * Only consider relations able to hold unfrozen XIDs (anything else
1652  * should have InvalidTransactionId in relfrozenxid anyway).
1653  */
1654  if (classForm->relkind != RELKIND_RELATION &&
1655  classForm->relkind != RELKIND_MATVIEW &&
1656  classForm->relkind != RELKIND_TOASTVALUE)
1657  {
1658  Assert(!TransactionIdIsValid(relfrozenxid));
1659  Assert(!MultiXactIdIsValid(relminmxid));
1660  continue;
1661  }
1662 
1663  /*
1664  * Some table AMs might not need per-relation xid / multixid horizons.
1665  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1666  * to not be set (i.e. set to their respective Invalid*Id)
1667  * independently. Thus validate and compute horizon for each only if
1668  * set.
1669  *
1670  * If things are working properly, no relation should have a
1671  * relfrozenxid or relminmxid that is "in the future". However, such
1672  * cases have been known to arise due to bugs in pg_upgrade. If we
1673  * see any entries that are "in the future", chicken out and don't do
1674  * anything. This ensures we won't truncate clog & multixact SLRUs
1675  * before those relations have been scanned and cleaned up.
1676  */
1677 
1678  if (TransactionIdIsValid(relfrozenxid))
1679  {
1680  Assert(TransactionIdIsNormal(relfrozenxid));
1681 
1682  /* check for values in the future */
1683  if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1684  {
1685  bogus = true;
1686  break;
1687  }
1688 
1689  /* determine new horizon */
1690  if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1691  newFrozenXid = relfrozenxid;
1692  }
1693 
1694  if (MultiXactIdIsValid(relminmxid))
1695  {
1696  /* check for values in the future */
1697  if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1698  {
1699  bogus = true;
1700  break;
1701  }
1702 
1703  /* determine new horizon */
1704  if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1705  newMinMulti = relminmxid;
1706  }
1707  }
1708 
1709  /* we're done with pg_class */
1710  systable_endscan(scan);
1711  table_close(relation, AccessShareLock);
1712 
1713  /* chicken out if bogus data found */
1714  if (bogus)
1715  return;
1716 
1717  Assert(TransactionIdIsNormal(newFrozenXid));
1718  Assert(MultiXactIdIsValid(newMinMulti));
1719 
1720  /* Now fetch the pg_database tuple we need to update. */
1721  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1722 
1723  /*
1724  * Fetch a copy of the tuple to scribble on. We could check the syscache
1725  * tuple first. If that concluded !dirty, we'd avoid waiting on
1726  * concurrent heap_update() and would avoid exclusive-locking the buffer.
1727  * For now, don't optimize that.
1728  */
1729  ScanKeyInit(&key[0],
1730  Anum_pg_database_oid,
1731  BTEqualStrategyNumber, F_OIDEQ,
1733 
1734  systable_inplace_update_begin(relation, DatabaseOidIndexId, true,
1735  NULL, 1, key, &tuple, &inplace_state);
1736 
1737  if (!HeapTupleIsValid(tuple))
1738  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1739 
1740  dbform = (Form_pg_database) GETSTRUCT(tuple);
1741 
1742  /*
1743  * As in vac_update_relstats(), we ordinarily don't want to let
1744  * datfrozenxid go backward; but if it's "in the future" then it must be
1745  * corrupt and it seems best to overwrite it.
1746  */
1747  if (dbform->datfrozenxid != newFrozenXid &&
1748  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1749  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1750  {
1751  dbform->datfrozenxid = newFrozenXid;
1752  dirty = true;
1753  }
1754  else
1755  newFrozenXid = dbform->datfrozenxid;
1756 
1757  /* Ditto for datminmxid */
1758  if (dbform->datminmxid != newMinMulti &&
1759  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1760  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1761  {
1762  dbform->datminmxid = newMinMulti;
1763  dirty = true;
1764  }
1765  else
1766  newMinMulti = dbform->datminmxid;
1767 
1768  if (dirty)
1769  systable_inplace_update_finish(inplace_state, tuple);
1770  else
1771  systable_inplace_update_cancel(inplace_state);
1772 
1773  heap_freetuple(tuple);
1774  table_close(relation, RowExclusiveLock);
1775 
1776  /*
1777  * If we were able to advance datfrozenxid or datminmxid, see if we can
1778  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1779  * XID-wrap-limit info is stale, since this action will update that too.
1780  */
1781  if (dirty || ForceTransactionIdLimitUpdate())
1782  vac_truncate_clog(newFrozenXid, newMinMulti,
1783  lastSaneFrozenXid, lastSaneMinMulti);
1784 }
TransactionId MultiXactId
Definition: c.h:641
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:604
void systable_inplace_update_cancel(void *state)
Definition: genam.c:901
void systable_inplace_update_begin(Relation relation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, const ScanKeyData *key, HeapTuple *oldtupcopy, void **state)
Definition: genam.c:806
void systable_inplace_update_finish(void *state, HeapTuple tuple)
Definition: genam.c:882
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:511
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:387
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition: lmgr.c:486
#define ExclusiveLock
Definition: lockdefs.h:42
#define RowExclusiveLock
Definition: lockdefs.h:38
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2660
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:771
FormData_pg_class
Definition: pg_class.h:142
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:2005
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1805
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:517

References AccessShareLock, Assert, BTEqualStrategyNumber, elog, ERROR, ExclusiveLock, ForceTransactionIdLimitUpdate(), FormData_pg_class, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), GETSTRUCT, heap_freetuple(), HeapTupleIsValid, InvalidOid, sort-test::key, LockDatabaseFrozenIds(), MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ObjectIdGetDatum(), ReadNextMultiXactId(), ReadNextTransactionId(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), table_close(), table_open(), TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), and vac_truncate_clog().

Referenced by do_autovacuum(), and vacuum().

◆ vac_update_relstats()

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 at line 1410 of file vacuum.c.

1417 {
1418  Oid relid = RelationGetRelid(relation);
1419  Relation rd;
1420  ScanKeyData key[1];
1421  HeapTuple ctup;
1422  void *inplace_state;
1423  Form_pg_class pgcform;
1424  bool dirty,
1425  futurexid,
1426  futuremxid;
1427  TransactionId oldfrozenxid;
1428  MultiXactId oldminmulti;
1429 
1430  rd = table_open(RelationRelationId, RowExclusiveLock);
1431 
1432  /* Fetch a copy of the tuple to scribble on */
1433  ScanKeyInit(&key[0],
1434  Anum_pg_class_oid,
1435  BTEqualStrategyNumber, F_OIDEQ,
1436  ObjectIdGetDatum(relid));
1437  systable_inplace_update_begin(rd, ClassOidIndexId, true,
1438  NULL, 1, key, &ctup, &inplace_state);
1439  if (!HeapTupleIsValid(ctup))
1440  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1441  relid);
1442  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1443 
1444  /* Apply statistical updates, if any, to copied tuple */
1445 
1446  dirty = false;
1447  if (pgcform->relpages != (int32) num_pages)
1448  {
1449  pgcform->relpages = (int32) num_pages;
1450  dirty = true;
1451  }
1452  if (pgcform->reltuples != (float4) num_tuples)
1453  {
1454  pgcform->reltuples = (float4) num_tuples;
1455  dirty = true;
1456  }
1457  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1458  {
1459  pgcform->relallvisible = (int32) num_all_visible_pages;
1460  dirty = true;
1461  }
1462 
1463  /* Apply DDL updates, but not inside an outer transaction (see above) */
1464 
1465  if (!in_outer_xact)
1466  {
1467  /*
1468  * If we didn't find any indexes, reset relhasindex.
1469  */
1470  if (pgcform->relhasindex && !hasindex)
1471  {
1472  pgcform->relhasindex = false;
1473  dirty = true;
1474  }
1475 
1476  /* We also clear relhasrules and relhastriggers if needed */
1477  if (pgcform->relhasrules && relation->rd_rules == NULL)
1478  {
1479  pgcform->relhasrules = false;
1480  dirty = true;
1481  }
1482  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1483  {
1484  pgcform->relhastriggers = false;
1485  dirty = true;
1486  }
1487  }
1488 
1489  /*
1490  * Update relfrozenxid, unless caller passed InvalidTransactionId
1491  * indicating it has no new data.
1492  *
1493  * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1494  * stored relfrozenxid is "in the future" then it seems best to assume
1495  * it's corrupt, and overwrite with the oldest remaining XID in the table.
1496  * This should match vac_update_datfrozenxid() concerning what we consider
1497  * to be "in the future".
1498  */
1499  oldfrozenxid = pgcform->relfrozenxid;
1500  futurexid = false;
1501  if (frozenxid_updated)
1502  *frozenxid_updated = false;
1503  if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1504  {
1505  bool update = false;
1506 
1507  if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1508  update = true;
1509  else if (TransactionIdPrecedes(ReadNextTransactionId(), oldfrozenxid))
1510  futurexid = update = true;
1511 
1512  if (update)
1513  {
1514  pgcform->relfrozenxid = frozenxid;
1515  dirty = true;
1516  if (frozenxid_updated)
1517  *frozenxid_updated = true;
1518  }
1519  }
1520 
1521  /* Similarly for relminmxid */
1522  oldminmulti = pgcform->relminmxid;
1523  futuremxid = false;
1524  if (minmulti_updated)
1525  *minmulti_updated = false;
1526  if (MultiXactIdIsValid(minmulti) && oldminmulti != minmulti)
1527  {
1528  bool update = false;
1529 
1530  if (MultiXactIdPrecedes(oldminmulti, minmulti))
1531  update = true;
1532  else if (MultiXactIdPrecedes(ReadNextMultiXactId(), oldminmulti))
1533  futuremxid = update = true;
1534 
1535  if (update)
1536  {
1537  pgcform->relminmxid = minmulti;
1538  dirty = true;
1539  if (minmulti_updated)
1540  *minmulti_updated = true;
1541  }
1542  }
1543 
1544  /* If anything changed, write out the tuple. */
1545  if (dirty)
1546  systable_inplace_update_finish(inplace_state, ctup);
1547  else
1548  systable_inplace_update_cancel(inplace_state);
1549 
1551 
1552  if (futurexid)
1553  ereport(WARNING,
1555  errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1556  oldfrozenxid, frozenxid,
1557  RelationGetRelationName(relation))));
1558  if (futuremxid)
1559  ereport(WARNING,
1561  errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1562  oldminmulti, minmulti,
1563  RelationGetRelationName(relation))));
1564 }
signed int int32
Definition: c.h:482
float float4
Definition: c.h:608
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
#define RelationGetRelid(relation)
Definition: rel.h:505
TriggerDesc * trigdesc
Definition: rel.h:117
RuleLock * rd_rules
Definition: rel.h:115

References BTEqualStrategyNumber, elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, GETSTRUCT, HeapTupleIsValid, sort-test::key, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum(), RelationData::rd_rules, ReadNextMultiXactId(), ReadNextTransactionId(), RelationGetRelationName, RelationGetRelid, RowExclusiveLock, ScanKeyInit(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), table_close(), table_open(), TransactionIdIsNormal, TransactionIdPrecedes(), RelationData::trigdesc, and WARNING.

Referenced by do_analyze_rel(), heap_vacuum_rel(), and update_relstats_all_indexes().

◆ vacuum()

void vacuum ( List relations,
VacuumParams params,
BufferAccessStrategy  bstrategy,
MemoryContext  vac_context,
bool  isTopLevel 
)

Definition at line 478 of file vacuum.c.

480 {
481  static bool in_vacuum = false;
482 
483  const char *stmttype;
484  volatile bool in_outer_xact,
485  use_own_xacts;
486 
487  Assert(params != NULL);
488 
489  stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
490 
491  /*
492  * We cannot run VACUUM inside a user transaction block; if we were inside
493  * a transaction, then our commit- and start-transaction-command calls
494  * would not have the intended effect! There are numerous other subtle
495  * dependencies on this, too.
496  *
497  * ANALYZE (without VACUUM) can run either way.
498  */
499  if (params->options & VACOPT_VACUUM)
500  {
501  PreventInTransactionBlock(isTopLevel, stmttype);
502  in_outer_xact = false;
503  }
504  else
505  in_outer_xact = IsInTransactionBlock(isTopLevel);
506 
507  /*
508  * Check for and disallow recursive calls. This could happen when VACUUM
509  * FULL or ANALYZE calls a hostile index expression that itself calls
510  * ANALYZE.
511  */
512  if (in_vacuum)
513  ereport(ERROR,
514  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
515  errmsg("%s cannot be executed from VACUUM or ANALYZE",
516  stmttype)));
517 
518  /*
519  * Build list of relation(s) to process, putting any new data in
520  * vac_context for safekeeping.
521  */
522  if (params->options & VACOPT_ONLY_DATABASE_STATS)
523  {
524  /* We don't process any tables in this case */
525  Assert(relations == NIL);
526  }
527  else if (relations != NIL)
528  {
529  List *newrels = NIL;
530  ListCell *lc;
531 
532  foreach(lc, relations)
533  {
535  List *sublist;
536  MemoryContext old_context;
537 
538  sublist = expand_vacuum_rel(vrel, vac_context, params->options);
539  old_context = MemoryContextSwitchTo(vac_context);
540  newrels = list_concat(newrels, sublist);
541  MemoryContextSwitchTo(old_context);
542  }
543  relations = newrels;
544  }
545  else
546  relations = get_all_vacuum_rels(vac_context, params->options);
547 
548  /*
549  * Decide whether we need to start/commit our own transactions.
550  *
551  * For VACUUM (with or without ANALYZE): always do so, so that we can
552  * release locks as soon as possible. (We could possibly use the outer
553  * transaction for a one-table VACUUM, but handling TOAST tables would be
554  * problematic.)
555  *
556  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
557  * start/commit our own transactions. Also, there's no need to do so if
558  * only processing one relation. For multiple relations when not within a
559  * transaction block, and also in an autovacuum worker, use own
560  * transactions so we can release locks sooner.
561  */
562  if (params->options & VACOPT_VACUUM)
563  use_own_xacts = true;
564  else
565  {
566  Assert(params->options & VACOPT_ANALYZE);
568  use_own_xacts = true;
569  else if (in_outer_xact)
570  use_own_xacts = false;
571  else if (list_length(relations) > 1)
572  use_own_xacts = true;
573  else
574  use_own_xacts = false;
575  }
576 
577  /*
578  * vacuum_rel expects to be entered with no transaction active; it will
579  * start and commit its own transaction. But we are called by an SQL
580  * command, and so we are executing inside a transaction already. We
581  * commit the transaction started in PostgresMain() here, and start
582  * another one before exiting to match the commit waiting for us back in
583  * PostgresMain().
584  */
585  if (use_own_xacts)
586  {
587  Assert(!in_outer_xact);
588 
589  /* ActiveSnapshot is not set by autovacuum */
590  if (ActiveSnapshotSet())
592 
593  /* matches the StartTransaction in PostgresMain() */
595  }
596 
597  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
598  PG_TRY();
599  {
600  ListCell *cur;
601 
602  in_vacuum = true;
603  VacuumFailsafeActive = false;
605  VacuumCostBalance = 0;
608  VacuumActiveNWorkers = NULL;
609 
610  /*
611  * Loop to process each selected relation.
612  */
613  foreach(cur, relations)
614  {
616 
617  if (params->options & VACOPT_VACUUM)
618  {
619  if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy))
620  continue;
621  }
622 
623  if (params->options & VACOPT_ANALYZE)
624  {
625  /*
626  * If using separate xacts, start one for analyze. Otherwise,
627  * we can use the outer transaction.
628  */
629  if (use_own_xacts)
630  {
632  /* functions in indexes may want a snapshot set */
634  }
635 
636  analyze_rel(vrel->oid, vrel->relation, params,
637  vrel->va_cols, in_outer_xact, bstrategy);
638 
639  if (use_own_xacts)
640  {
643  }
644  else
645  {
646  /*
647  * If we're not using separate xacts, better separate the
648  * ANALYZE actions with CCIs. This avoids trouble if user
649  * says "ANALYZE t, t".
650  */
652  }
653  }
654 
655  /*
656  * Ensure VacuumFailsafeActive has been reset before vacuuming the
657  * next relation.
658  */
659  VacuumFailsafeActive = false;
660  }
661  }
662  PG_FINALLY();
663  {
664  in_vacuum = false;
665  VacuumCostActive = false;
666  VacuumFailsafeActive = false;
667  VacuumCostBalance = 0;
668  }
669  PG_END_TRY();
670 
671  /*
672  * Finish up processing.
673  */
674  if (use_own_xacts)
675  {
676  /* here, we are not in a transaction */
677 
678  /*
679  * This matches the CommitTransaction waiting for us in
680  * PostgresMain().
681  */
683  }
684 
685  if ((params->options & VACOPT_VACUUM) &&
686  !(params->options & VACOPT_SKIP_DATABASE_STATS))
687  {
688  /*
689  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
690  */
692  }
693 
694 }
void VacuumUpdateCosts(void)
Definition: autovacuum.c:1635
void analyze_rel(Oid relid, RangeVar *relation, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:109
struct cursor * cur
Definition: ecpg.c:29
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_FINALLY(...)
Definition: elog.h:388
bool VacuumCostActive
Definition: globals.c:157
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:216
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:648
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:782
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
static List * expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context, int options)
Definition: vacuum.c:867
static List * get_all_vacuum_rels(MemoryContext vac_context, int options)
Definition: vacuum.c:1022
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuum.c:1974
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1586
bool VacuumFailsafeActive
Definition: vacuum.c:95
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3761
void CommandCounterIncrement(void)
Definition: xact.c:1099
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3640
void StartTransactionCommand(void)
Definition: xact.c:3051
void CommitTransactionCommand(void)
Definition: xact.c:3149

References ActiveSnapshotSet(), AmAutoVacuumWorkerProcess, analyze_rel(), Assert, CommandCounterIncrement(), CommitTransactionCommand(), cur, ereport, errcode(), errmsg(), ERROR, expand_vacuum_rel(), get_all_vacuum_rels(), GetTransactionSnapshot(), IsInTransactionBlock(), lfirst_node, list_concat(), list_length(), MemoryContextSwitchTo(), NIL, VacuumRelation::oid, VacuumParams::options, PG_END_TRY, PG_FINALLY, PG_TRY, PopActiveSnapshot(), PreventInTransactionBlock(), PushActiveSnapshot(), VacuumRelation::relation, StartTransactionCommand(), VacuumRelation::va_cols, vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_ONLY_DATABASE_STATS, VACOPT_SKIP_DATABASE_STATS, VACOPT_VACUUM, vacuum_rel(), VacuumActiveNWorkers, VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumFailsafeActive, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by autovacuum_do_vac_analyze(), ExecVacuum(), parallel_vacuum_index_is_parallel_safe(), and parallel_vacuum_process_all_indexes().

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 2362 of file vacuum.c.

2363 {
2364  double msec = 0;
2365 
2366  /* Always check for interrupts */
2368 
2369  if (InterruptPending ||
2371  return;
2372 
2373  /*
2374  * Autovacuum workers should reload the configuration file if requested.
2375  * This allows changes to [autovacuum_]vacuum_cost_limit and
2376  * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2377  * vacuumed or analyzed.
2378  */
2380  {
2381  ConfigReloadPending = false;
2384  }
2385 
2386  /*
2387  * If we disabled cost-based delays after reloading the config file,
2388  * return.
2389  */
2390  if (!VacuumCostActive)
2391  return;
2392 
2393  /*
2394  * For parallel vacuum, the delay is computed based on the shared cost
2395  * balance. See compute_parallel_delay.
2396  */
2397  if (VacuumSharedCostBalance != NULL)
2398  msec = compute_parallel_delay();
2401 
2402  /* Nap if appropriate */
2403  if (msec > 0)
2404  {
2405  if (msec > vacuum_cost_delay * 4)
2406  msec = vacuum_cost_delay * 4;
2407 
2408  pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);
2409  pg_usleep(msec * 1000);
2411 
2412  /*
2413  * We don't want to ignore postmaster death during very long vacuums
2414  * with vacuum_cost_delay configured. We can't use the usual
2415  * WaitLatch() approach here because we want microsecond-based sleep
2416  * durations above.
2417  */
2419  exit(1);
2420 
2421  VacuumCostBalance = 0;
2422 
2423  /*
2424  * Balance and update limit values for autovacuum workers. We must do
2425  * this periodically, as the number of workers across which we are
2426  * balancing the limit may have changed.
2427  *
2428  * TODO: There may be better criteria for determining when to do this
2429  * besides "check after napping".
2430  */
2432 
2433  /* Might have gotten an interrupt while sleeping */
2435  }
2436 }
void AutoVacuumUpdateCostLimit(void)
Definition: autovacuum.c:1704
volatile sig_atomic_t InterruptPending
Definition: globals.c:31
bool IsUnderPostmaster
Definition: globals.c:119
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
exit(1)
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define PostmasterIsAlive()
Definition: pmsignal.h:105
void pg_usleep(long microsec)
Definition: signal.c:53
static double compute_parallel_delay(void)
Definition: vacuum.c:2461
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101

References AmAutoVacuumWorkerProcess, AutoVacuumUpdateCostLimit(), CHECK_FOR_INTERRUPTS, compute_parallel_delay(), ConfigReloadPending, exit(), InterruptPending, IsUnderPostmaster, pg_usleep(), PGC_SIGHUP, pgstat_report_wait_end(), pgstat_report_wait_start(), PostmasterIsAlive, ProcessConfigFile(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by acquire_sample_rows(), blbulkdelete(), blvacuumcleanup(), btvacuumpage(), compute_array_stats(), compute_distinct_stats(), compute_index_stats(), compute_range_stats(), compute_scalar_stats(), compute_trivial_stats(), compute_tsvector_stats(), file_acquire_sample_rows(), ginbulkdelete(), ginInsertCleanup(), ginvacuumcleanup(), gistvacuumpage(), hashbucketcleanup(), lazy_scan_heap(), lazy_vacuum_heap_rel(), spgprocesspending(), and spgvacuumpage().

◆ vacuum_get_cutoffs()

bool vacuum_get_cutoffs ( Relation  rel,
const VacuumParams params,
struct VacuumCutoffs cutoffs 
)

Definition at line 1084 of file vacuum.c.

1086 {
1087  int freeze_min_age,
1088  multixact_freeze_min_age,
1089  freeze_table_age,
1090  multixact_freeze_table_age,
1091  effective_multixact_freeze_max_age;
1092  TransactionId nextXID,
1093  safeOldestXmin,
1094  aggressiveXIDCutoff;
1095  MultiXactId nextMXID,
1096  safeOldestMxact,
1097  aggressiveMXIDCutoff;
1098 
1099  /* Use mutable copies of freeze age parameters */
1100  freeze_min_age = params->freeze_min_age;
1101  multixact_freeze_min_age = params->multixact_freeze_min_age;
1102  freeze_table_age = params->freeze_table_age;
1103  multixact_freeze_table_age = params->multixact_freeze_table_age;
1104 
1105  /* Set pg_class fields in cutoffs */
1106  cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1107  cutoffs->relminmxid = rel->rd_rel->relminmxid;
1108 
1109  /*
1110  * Acquire OldestXmin.
1111  *
1112  * We can always ignore processes running lazy vacuum. This is because we
1113  * use these values only for deciding which tuples we must keep in the
1114  * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1115  * XID assigned), it's safe to ignore it. In theory it could be
1116  * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1117  * that only one vacuum process can be working on a particular table at
1118  * any time, and that each vacuum is always an independent transaction.
1119  */
1121 
1123 
1124  /* Acquire OldestMxact */
1125  cutoffs->OldestMxact = GetOldestMultiXactId();
1127 
1128  /* Acquire next XID/next MXID values used to apply age-based settings */
1129  nextXID = ReadNextTransactionId();
1130  nextMXID = ReadNextMultiXactId();
1131 
1132  /*
1133  * Also compute the multixact age for which freezing is urgent. This is
1134  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1135  * short of multixact member space.
1136  */
1137  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1138 
1139  /*
1140  * Almost ready to set freeze output parameters; check if OldestXmin or
1141  * OldestMxact are held back to an unsafe degree before we start on that
1142  */
1143  safeOldestXmin = nextXID - autovacuum_freeze_max_age;
1144  if (!TransactionIdIsNormal(safeOldestXmin))
1145  safeOldestXmin = FirstNormalTransactionId;
1146  safeOldestMxact = nextMXID - effective_multixact_freeze_max_age;
1147  if (safeOldestMxact < FirstMultiXactId)
1148  safeOldestMxact = FirstMultiXactId;
1149  if (TransactionIdPrecedes(cutoffs->OldestXmin, safeOldestXmin))
1150  ereport(WARNING,
1151  (errmsg("cutoff for removing and freezing tuples is far in the past"),
1152  errhint("Close open transactions soon to avoid wraparound problems.\n"
1153  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1154  if (MultiXactIdPrecedes(cutoffs->OldestMxact, safeOldestMxact))
1155  ereport(WARNING,
1156  (errmsg("cutoff for freezing multixacts is far in the past"),
1157  errhint("Close open transactions soon to avoid wraparound problems.\n"
1158  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1159 
1160  /*
1161  * Determine the minimum freeze age to use: as specified by the caller, or
1162  * vacuum_freeze_min_age, but in any case not more than half
1163  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1164  * wraparound won't occur too frequently.
1165  */
1166  if (freeze_min_age < 0)
1167  freeze_min_age = vacuum_freeze_min_age;
1168  freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1169  Assert(freeze_min_age >= 0);
1170 
1171  /* Compute FreezeLimit, being careful to generate a normal XID */
1172  cutoffs->FreezeLimit = nextXID - freeze_min_age;
1173  if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1175  /* FreezeLimit must always be <= OldestXmin */
1176  if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1177  cutoffs->FreezeLimit = cutoffs->OldestXmin;
1178 
1179  /*
1180  * Determine the minimum multixact freeze age to use: as specified by
1181  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1182  * than half effective_multixact_freeze_max_age, so that autovacuums to
1183  * prevent MultiXact wraparound won't occur too frequently.
1184  */
1185  if (multixact_freeze_min_age < 0)
1186  multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1187  multixact_freeze_min_age = Min(multixact_freeze_min_age,
1188  effective_multixact_freeze_max_age / 2);
1189  Assert(multixact_freeze_min_age >= 0);
1190 
1191  /* Compute MultiXactCutoff, being careful to generate a valid value */
1192  cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1193  if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1194  cutoffs->MultiXactCutoff = FirstMultiXactId;
1195  /* MultiXactCutoff must always be <= OldestMxact */
1196  if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1197  cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1198 
1199  /*
1200  * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1201  *
1202  * Determine the table freeze age to use: as specified by the caller, or
1203  * the value of the vacuum_freeze_table_age GUC, but in any case not more
1204  * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1205  * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1206  * anti-wraparound autovacuum is launched.
1207  */
1208  if (freeze_table_age < 0)
1209  freeze_table_age = vacuum_freeze_table_age;
1210  freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1211  Assert(freeze_table_age >= 0);
1212  aggressiveXIDCutoff = nextXID - freeze_table_age;
1213  if (!TransactionIdIsNormal(aggressiveXIDCutoff))
1214  aggressiveXIDCutoff = FirstNormalTransactionId;
1216  aggressiveXIDCutoff))
1217  return true;
1218 
1219  /*
1220  * Similar to the above, determine the table freeze age to use for
1221  * multixacts: as specified by the caller, or the value of the
1222  * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1223  * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1224  * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1225  * multixacts before anti-wraparound autovacuum is launched.
1226  */
1227  if (multixact_freeze_table_age < 0)
1228  multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1229  multixact_freeze_table_age =
1230  Min(multixact_freeze_table_age,
1231  effective_multixact_freeze_max_age * 0.95);
1232  Assert(multixact_freeze_table_age >= 0);
1233  aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1234  if (aggressiveMXIDCutoff < FirstMultiXactId)
1235  aggressiveMXIDCutoff = FirstMultiXactId;
1237  aggressiveMXIDCutoff))
1238  return true;
1239 
1240  /* Non-aggressive VACUUM */
1241  return false;
1242 }
int autovacuum_freeze_max_age
Definition: autovacuum.c:127
#define Min(x, y)
Definition: c.h:983
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3331
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2978
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId FreezeLimit
Definition: vacuum.h:277
TransactionId OldestXmin
Definition: vacuum.h:267
TransactionId relfrozenxid
Definition: vacuum.h:251
MultiXactId relminmxid
Definition: vacuum.h:252
MultiXactId MultiXactCutoff
Definition: vacuum.h:278
MultiXactId OldestMxact
Definition: vacuum.h:268
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define FirstNormalTransactionId
Definition: transam.h:34
int vacuum_freeze_min_age
Definition: vacuum.c:66
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:69
int vacuum_freeze_table_age
Definition: vacuum.c:67
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:68

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg(), FirstMultiXactId, FirstNormalTransactionId, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, VacuumCutoffs::FreezeLimit, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), Min, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), MultiXactMemberFreezeThreshold(), VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, RelationData::rd_rel, ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), vacuum_freeze_min_age, vacuum_freeze_table_age, vacuum_multixact_freeze_min_age, vacuum_multixact_freeze_table_age, and WARNING.

Referenced by copy_table_data(), and heap_vacuum_rel().

◆ vacuum_is_permitted_for_relation()

bool vacuum_is_permitted_for_relation ( Oid  relid,
Form_pg_class  reltuple,
bits32  options 
)

Definition at line 703 of file vacuum.c.

705 {
706  char *relname;
707 
709 
710  /*----------
711  * A role has privileges to vacuum or analyze the relation if any of the
712  * following are true:
713  * - the role owns the current database and the relation is not shared
714  * - the role has the MAINTAIN privilege on the relation
715  *----------
716  */
717  if ((object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()) &&
718  !reltuple->relisshared) ||
720  return true;
721 
722  relname = NameStr(reltuple->relname);
723 
724  if ((options & VACOPT_VACUUM) != 0)
725  {
727  (errmsg("permission denied to vacuum \"%s\", skipping it",
728  relname)));
729 
730  /*
731  * For VACUUM ANALYZE, both logs could show up, but just generate
732  * information for VACUUM as that would be the first one to be
733  * processed.
734  */
735  return false;
736  }
737 
738  if ((options & VACOPT_ANALYZE) != 0)
740  (errmsg("permission denied to analyze \"%s\", skipping it",
741  relname)));
742 
743  return false;
744 }
@ ACLCHECK_OK
Definition: acl.h:183
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4064
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4013
Oid GetUserId(void)
Definition: miscinit.c:524
#define ACL_MAINTAIN
Definition: parsenodes.h:90
NameData relname
Definition: pg_class.h:38

References ACL_MAINTAIN, ACLCHECK_OK, Assert, ereport, errmsg(), GetUserId(), MyDatabaseId, NameStr, object_ownercheck(), pg_class_aclcheck(), relname, VACOPT_ANALYZE, VACOPT_VACUUM, and WARNING.

Referenced by analyze_rel(), expand_vacuum_rel(), get_all_vacuum_rels(), and vacuum_rel().

◆ vacuum_open_relation()

Relation vacuum_open_relation ( Oid  relid,
RangeVar relation,
bits32  options,
bool  verbose,
LOCKMODE  lmode 
)

Definition at line 755 of file vacuum.c.

757 {
758  Relation rel;
759  bool rel_lock = true;
760  int elevel;
761 
763 
764  /*
765  * Open the relation and get the appropriate lock on it.
766  *
767  * There's a race condition here: the relation may have gone away since
768  * the last time we saw it. If so, we don't need to vacuum or analyze it.
769  *
770  * If we've been asked not to wait for the relation lock, acquire it first
771  * in non-blocking mode, before calling try_relation_open().
772  */
773  if (!(options & VACOPT_SKIP_LOCKED))
774  rel = try_relation_open(relid, lmode);
775  else if (ConditionalLockRelationOid(relid, lmode))
776  rel = try_relation_open(relid, NoLock);
777  else
778  {
779  rel = NULL;
780  rel_lock = false;
781  }
782 
783  /* if relation is opened, leave */
784  if (rel)
785  return rel;
786 
787  /*
788  * Relation could not be opened, hence generate if possible a log
789  * informing on the situation.
790  *
791  * If the RangeVar is not defined, we do not have enough information to
792  * provide a meaningful log statement. Chances are that the caller has
793  * intentionally not provided this information so that this logging is
794  * skipped, anyway.
795  */
796  if (relation == NULL)
797  return NULL;
798 
799  /*
800  * Determine the log level.
801  *
802  * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
803  * statements in the permission checks; otherwise, only log if the caller
804  * so requested.
805  */
807  elevel = WARNING;
808  else if (verbose)
809  elevel = LOG;
810  else
811  return NULL;
812 
813  if ((options & VACOPT_VACUUM) != 0)
814  {
815  if (!rel_lock)
816  ereport(elevel,
817  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
818  errmsg("skipping vacuum of \"%s\" --- lock not available",
819  relation->relname)));
820  else
821  ereport(elevel,
823  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
824  relation->relname)));
825 
826  /*
827  * For VACUUM ANALYZE, both logs could show up, but just generate
828  * information for VACUUM as that would be the first one to be
829  * processed.
830  */
831  return NULL;
832  }
833 
834  if ((options & VACOPT_ANALYZE) != 0)
835  {
836  if (!rel_lock)
837  ereport(elevel,
838  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
839  errmsg("skipping analyze of \"%s\" --- lock not available",
840  relation->relname)));
841  else
842  ereport(elevel,
844  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
845  relation->relname)));
846  }
847 
848  return NULL;
849 }
#define LOG
Definition: elog.h:31
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:150
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88

References AmAutoVacuumWorkerProcess, Assert, ConditionalLockRelationOid(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), LOG, NoLock, RangeVar::relname, try_relation_open(), VACOPT_ANALYZE, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, verbose, and WARNING.

Referenced by analyze_rel(), and vacuum_rel().

◆ vacuum_rel()

static bool vacuum_rel ( Oid  relid,
RangeVar relation,
VacuumParams params,
BufferAccessStrategy  bstrategy 
)
static

Definition at line 1974 of file vacuum.c.

1976 {
1977  LOCKMODE lmode;
1978  Relation rel;
1979  LockRelId lockrelid;
1980  Oid priv_relid;
1981  Oid toast_relid;
1982  Oid save_userid;
1983  int save_sec_context;
1984  int save_nestlevel;
1985 
1986  Assert(params != NULL);
1987 
1988  /* Begin a transaction for vacuuming this relation */
1990 
1991  if (!(params->options & VACOPT_FULL))
1992  {
1993  /*
1994  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1995  * other concurrent VACUUMs know that they can ignore this one while
1996  * determining their OldestXmin. (The reason we don't set it during a
1997  * full VACUUM is exactly that we may have to run user-defined
1998  * functions for functional indexes, and we want to make sure that if
1999  * they use the snapshot set above, any tuples it requires can't get
2000  * removed from other tables. An index function that depends on the
2001  * contents of other tables is arguably broken, but we won't break it
2002  * here by violating transaction semantics.)
2003  *
2004  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
2005  * autovacuum; it's used to avoid canceling a vacuum that was invoked
2006  * in an emergency.
2007  *
2008  * Note: these flags remain set until CommitTransaction or
2009  * AbortTransaction. We don't want to clear them until we reset
2010  * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
2011  * might appear to go backwards, which is probably Not Good. (We also
2012  * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
2013  * xmin doesn't become visible ahead of setting the flag.)
2014  */
2015  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2017  if (params->is_wraparound)
2020  LWLockRelease(ProcArrayLock);
2021  }
2022 
2023  /*
2024  * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
2025  * cutoff xids in local memory wrapping around, and to have updated xmin
2026  * horizons.
2027  */
2029 
2030  /*
2031  * Check for user-requested abort. Note we want this to be inside a
2032  * transaction, so xact.c doesn't issue useless WARNING.
2033  */
2035 
2036  /*
2037  * Determine the type of lock we want --- hard exclusive lock for a FULL
2038  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
2039  * way, we can be sure that no other backend is vacuuming the same table.
2040  */
2041  lmode = (params->options & VACOPT_FULL) ?
2043 
2044  /* open the relation and get the appropriate lock on it */
2045  rel = vacuum_open_relation(relid, relation, params->options,
2046  params->log_min_duration >= 0, lmode);
2047 
2048  /* leave if relation could not be opened or locked */
2049  if (!rel)
2050  {
2053  return false;
2054  }
2055 
2056  /*
2057  * When recursing to a TOAST table, check privileges on the parent. NB:
2058  * This is only safe to do because we hold a session lock on the main
2059  * relation that prevents concurrent deletion.
2060  */
2061  if (OidIsValid(params->toast_parent))
2062  priv_relid = params->toast_parent;
2063  else
2064  priv_relid = RelationGetRelid(rel);
2065 
2066  /*
2067  * Check if relation needs to be skipped based on privileges. This check
2068  * happens also when building the relation list to vacuum for a manual
2069  * operation, and needs to be done additionally here as VACUUM could
2070  * happen across multiple transactions where privileges could have changed
2071  * in-between. Make sure to only generate logs for VACUUM in this case.
2072  */
2073  if (!vacuum_is_permitted_for_relation(priv_relid,
2074  rel->rd_rel,
2075  params->options & ~VACOPT_ANALYZE))
2076  {
2077  relation_close(rel, lmode);
2080  return false;
2081  }
2082 
2083  /*
2084  * Check that it's of a vacuumable relkind.
2085  */
2086  if (rel->rd_rel->relkind != RELKIND_RELATION &&
2087  rel->rd_rel->relkind != RELKIND_MATVIEW &&
2088  rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
2089  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2090  {
2091  ereport(WARNING,
2092  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
2093  RelationGetRelationName(rel))));
2094  relation_close(rel, lmode);
2097  return false;
2098  }
2099 
2100  /*
2101  * Silently ignore tables that are temp tables of other backends ---
2102  * trying to vacuum these will lead to great unhappiness, since their
2103  * contents are probably not up-to-date on disk. (We don't throw a
2104  * warning here; it would just lead to chatter during a database-wide
2105  * VACUUM.)
2106  */
2107  if (RELATION_IS_OTHER_TEMP(rel))
2108  {
2109  relation_close(rel, lmode);
2112  return false;
2113  }
2114 
2115  /*
2116  * Silently ignore partitioned tables as there is no work to be done. The
2117  * useful work is on their child partitions, which have been queued up for
2118  * us separately.
2119  */
2120  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2121  {
2122  relation_close(rel, lmode);
2125  /* It's OK to proceed with ANALYZE on this table */
2126  return true;
2127  }
2128 
2129  /*
2130  * Get a session-level lock too. This will protect our access to the
2131  * relation across multiple transactions, so that we can vacuum the
2132  * relation's TOAST table (if any) secure in the knowledge that no one is
2133  * deleting the parent relation.
2134  *
2135  * NOTE: this cannot block, even if someone else is waiting for access,
2136  * because the lock manager knows that both lock requests are from the
2137  * same process.
2138  */
2139  lockrelid = rel->rd_lockInfo.lockRelId;
2140  LockRelationIdForSession(&lockrelid, lmode);
2141 
2142  /*
2143  * Set index_cleanup option based on index_cleanup reloption if it wasn't
2144  * specified in VACUUM command, or when running in an autovacuum worker
2145  */
2146  if (params->index_cleanup == VACOPTVALUE_UNSPECIFIED)
2147  {
2148  StdRdOptIndexCleanup vacuum_index_cleanup;
2149 
2150  if (rel->rd_options == NULL)
2151  vacuum_index_cleanup = STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO;
2152  else
2153  vacuum_index_cleanup =
2154  ((StdRdOptions *) rel->rd_options)->vacuum_index_cleanup;
2155 
2156  if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO)
2157  params->index_cleanup = VACOPTVALUE_AUTO;
2158  else if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON)
2160  else
2161  {
2162  Assert(vacuum_index_cleanup ==
2165  }
2166  }
2167 
2168  /*
2169  * Set truncate option based on truncate reloption if it wasn't specified
2170  * in VACUUM command, or when running in an autovacuum worker
2171  */
2172  if (params->truncate == VACOPTVALUE_UNSPECIFIED)
2173  {
2174  if (rel->rd_options == NULL ||
2175  ((StdRdOptions *) rel->rd_options)->vacuum_truncate)
2176  params->truncate = VACOPTVALUE_ENABLED;
2177  else
2178  params->truncate = VACOPTVALUE_DISABLED;
2179  }
2180 
2181  /*
2182  * Remember the relation's TOAST relation for later, if the caller asked
2183  * us to process it. In VACUUM FULL, though, the toast table is
2184  * automatically rebuilt by cluster_rel so we shouldn't recurse to it,
2185  * unless PROCESS_MAIN is disabled.
2186  */
2187  if ((params->options & VACOPT_PROCESS_TOAST) != 0 &&
2188  ((params->options & VACOPT_FULL) == 0 ||
2189  (params->options & VACOPT_PROCESS_MAIN) == 0))
2190  toast_relid = rel->rd_rel->reltoastrelid;
2191  else
2192  toast_relid = InvalidOid;
2193 
2194  /*
2195  * Switch to the table owner's userid, so that any index functions are run
2196  * as that user. Also lock down security-restricted operations and
2197  * arrange to make GUC variable changes local to this command. (This is
2198  * unnecessary, but harmless, for lazy VACUUM.)
2199  */
2200  GetUserIdAndSecContext(&save_userid, &save_sec_context);
2201  SetUserIdAndSecContext(rel->rd_rel->relowner,
2202  save_sec_context | SECURITY_RESTRICTED_OPERATION);
2203  save_nestlevel = NewGUCNestLevel();
2205 
2206  /*
2207  * If PROCESS_MAIN is set (the default), it's time to vacuum the main
2208  * relation. Otherwise, we can skip this part. If processing the TOAST
2209  * table is required (e.g., PROCESS_TOAST is set), we force PROCESS_MAIN
2210  * to be set when we recurse to the TOAST table.
2211  */
2212  if (params->options & VACOPT_PROCESS_MAIN)
2213  {
2214  /*
2215  * Do the actual work --- either FULL or "lazy" vacuum
2216  */
2217  if (params->options & VACOPT_FULL)
2218  {
2219  ClusterParams cluster_params = {0};
2220 
2221  /* close relation before vacuuming, but hold lock until commit */
2222  relation_close(rel, NoLock);
2223  rel = NULL;
2224 
2225  if ((params->options & VACOPT_VERBOSE) != 0)
2226  cluster_params.options |= CLUOPT_VERBOSE;
2227 
2228  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
2229  cluster_rel(relid, InvalidOid, &cluster_params);
2230  }
2231  else
2232  table_relation_vacuum(rel, params, bstrategy);
2233  }
2234 
2235  /* Roll back any GUC changes executed by index functions */
2236  AtEOXact_GUC(false, save_nestlevel);
2237 
2238  /* Restore userid and security context */
2239  SetUserIdAndSecContext(save_userid, save_sec_context);
2240 
2241  /* all done with this class, but hold lock until commit */
2242  if (rel)
2243  relation_close(rel, NoLock);
2244 
2245  /*
2246  * Complete the transaction and free all temporary memory used.
2247  */
2250 
2251  /*
2252  * If the relation has a secondary toast rel, vacuum that too while we
2253  * still hold the session lock on the main table. Note however that
2254  * "analyze" will not get done on the toast table. This is good, because
2255  * the toaster always uses hardcoded index access and statistics are
2256  * totally unimportant for toast relations.
2257  */
2258  if (toast_relid != InvalidOid)
2259  {
2260  VacuumParams toast_vacuum_params;
2261 
2262  /*
2263  * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise,
2264  * set toast_parent so that the privilege checks are done on the main
2265  * relation. NB: This is only safe to do because we hold a session
2266  * lock on the main relation that prevents concurrent deletion.
2267  */
2268  memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
2269  toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
2270  toast_vacuum_params.toast_parent = relid;
2271 
2272  vacuum_rel(toast_relid, NULL, &toast_vacuum_params, bstrategy);
2273  }
2274 
2275  /*
2276  * Now release the session-level lock on the main table.
2277  */
2278  UnlockRelationIdForSession(&lockrelid, lmode);
2279 
2280  /* Report that we really did it. */
2281  return true;
2282 }
void cluster_rel(Oid tableOid, Oid indexOid, ClusterParams *params)
Definition: cluster.c:310
#define CLUOPT_VERBOSE
Definition: cluster.h:23
int NewGUCNestLevel(void)
Definition: guc.c:2235
void RestrictSearchPath(void)
Definition: guc.c:2246
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:386
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:399
int LOCKMODE
Definition: lockdefs.h:26
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:312
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:667
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:674
#define PROC_IN_VACUUM
Definition: proc.h:58
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:60
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:658
StdRdOptIndexCleanup
Definition: rel.h:330
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO
Definition: rel.h:331
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF
Definition: rel.h:332
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON
Definition: rel.h:333
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
bits32 options
Definition: cluster.h:30
LockRelId lockRelId
Definition: rel.h:46
Definition: rel.h:39
uint8 statusFlags
Definition: proc.h:242
int pgxactoff
Definition: proc.h:184
uint8 * statusFlags
Definition: proc.h:399
LockInfoData rd_lockInfo
Definition: rel.h:114
bytea * rd_options
Definition: rel.h:175
static void table_relation_vacuum(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: tableam.h:1716
Relation vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:755

References AccessExclusiveLock, Assert, AtEOXact_GUC(), CHECK_FOR_INTERRUPTS, CLUOPT_VERBOSE, cluster_rel(), CommitTransactionCommand(), ereport, errmsg(), GetTransactionSnapshot(), GetUserIdAndSecContext(), VacuumParams::index_cleanup, InvalidOid, VacuumParams::is_wraparound, LockRelationIdForSession(), LockInfoData::lockRelId, VacuumParams::log_min_duration, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, NewGUCNestLevel(), NoLock, OidIsValid, ClusterParams::options, VacuumParams::options, PGPROC::pgxactoff, PopActiveSnapshot(), PROC_IN_VACUUM, PROC_VACUUM_FOR_WRAPAROUND, ProcGlobal, PushActiveSnapshot(), RelationData::rd_lockInfo, RelationData::rd_options, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetRelationName, RelationGetRelid, RestrictSearchPath(), SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, StartTransactionCommand(), PGPROC::statusFlags, PROC_HDR::statusFlags, STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO, STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF, STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON, table_relation_vacuum(), VacuumParams::toast_parent, VacuumParams::truncate, UnlockRelationIdForSession(), VACOPT_ANALYZE, VACOPT_FULL, VACOPT_PROCESS_MAIN, VACOPT_PROCESS_TOAST, VACOPT_VERBOSE, VACOPTVALUE_AUTO, VACOPTVALUE_DISABLED, VACOPTVALUE_ENABLED, VACOPTVALUE_UNSPECIFIED, vacuum_is_permitted_for_relation(), vacuum_open_relation(), and WARNING.

Referenced by vacuum().

◆ vacuum_xid_failsafe_check()

bool vacuum_xid_failsafe_check ( const struct VacuumCutoffs cutoffs)

Definition at line 1252 of file vacuum.c.

1253 {
1254  TransactionId relfrozenxid = cutoffs->relfrozenxid;
1255  MultiXactId relminmxid = cutoffs->relminmxid;
1256  TransactionId xid_skip_limit;
1257  MultiXactId multi_skip_limit;
1258  int skip_index_vacuum;
1259 
1260  Assert(TransactionIdIsNormal(relfrozenxid));
1261  Assert(MultiXactIdIsValid(relminmxid));
1262 
1263  /*
1264  * Determine the index skipping age to use. In any case no less than
1265  * autovacuum_freeze_max_age * 1.05.
1266  */
1267  skip_index_vacuum = Max(vacuum_failsafe_age, autovacuum_freeze_max_age * 1.05);
1268 
1269  xid_skip_limit = ReadNextTransactionId() - skip_index_vacuum;
1270  if (!TransactionIdIsNormal(xid_skip_limit))
1271  xid_skip_limit = FirstNormalTransactionId;
1272 
1273  if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1274  {
1275  /* The table's relfrozenxid is too old */
1276  return true;
1277  }
1278 
1279  /*
1280  * Similar to above, determine the index skipping age to use for
1281  * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1282  * 1.05.
1283  */
1284  skip_index_vacuum = Max(vacuum_multixact_failsafe_age,
1286 
1287  multi_skip_limit = ReadNextMultiXactId() - skip_index_vacuum;
1288  if (multi_skip_limit < FirstMultiXactId)
1289  multi_skip_limit = FirstMultiXactId;
1290 
1291  if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1292  {
1293  /* The table's relminmxid is too old */
1294  return true;
1295  }
1296 
1297  return false;
1298 }
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:128
#define Max(x, y)
Definition: c.h:977
int vacuum_multixact_failsafe_age
Definition: vacuum.c:71
int vacuum_failsafe_age
Definition: vacuum.c:70

References Assert, autovacuum_freeze_max_age, autovacuum_multixact_freeze_max_age, FirstMultiXactId, FirstNormalTransactionId, Max, MultiXactIdIsValid, MultiXactIdPrecedes(), ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), vacuum_failsafe_age, and vacuum_multixact_failsafe_age.

Referenced by lazy_check_wraparound_failsafe().

Variable Documentation

◆ vacuum_cost_delay

double vacuum_cost_delay = 0

◆ vacuum_cost_limit

int vacuum_cost_limit = 200

◆ vacuum_failsafe_age

int vacuum_failsafe_age

Definition at line 70 of file vacuum.c.

Referenced by vacuum_xid_failsafe_check().

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 66 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_freeze_table_age

int vacuum_freeze_table_age

Definition at line 67 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_failsafe_age

int vacuum_multixact_failsafe_age

Definition at line 71 of file vacuum.c.

Referenced by vacuum_xid_failsafe_check().

◆ vacuum_multixact_freeze_min_age

int vacuum_multixact_freeze_min_age

Definition at line 68 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_freeze_table_age

int vacuum_multixact_freeze_table_age

Definition at line 69 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ VacuumActiveNWorkers

◆ VacuumCostBalanceLocal

int VacuumCostBalanceLocal = 0

◆ VacuumFailsafeActive

◆ VacuumSharedCostBalance

pg_atomic_uint32* VacuumSharedCostBalance = NULL