PostgreSQL Source Code  git master
vacuum.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * vacuum.c
4  * The postgres vacuum cleaner.
5  *
6  * This file includes (a) control and dispatch code for VACUUM and ANALYZE
7  * commands, (b) code to compute various vacuum thresholds, and (c) index
8  * vacuum code.
9  *
10  * VACUUM for heap AM is implemented in vacuumlazy.c, parallel vacuum in
11  * vacuumparallel.c, ANALYZE in analyze.c, and VACUUM FULL is a variant of
12  * CLUSTER, handled in cluster.c.
13  *
14  *
15  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
16  * Portions Copyright (c) 1994, Regents of the University of California
17  *
18  *
19  * IDENTIFICATION
20  * src/backend/commands/vacuum.c
21  *
22  *-------------------------------------------------------------------------
23  */
24 #include "postgres.h"
25 
26 #include <math.h>
27 
28 #include "access/clog.h"
29 #include "access/commit_ts.h"
30 #include "access/genam.h"
31 #include "access/heapam.h"
32 #include "access/htup_details.h"
33 #include "access/multixact.h"
34 #include "access/tableam.h"
35 #include "access/transam.h"
36 #include "access/xact.h"
37 #include "catalog/namespace.h"
38 #include "catalog/pg_database.h"
39 #include "catalog/pg_inherits.h"
40 #include "commands/cluster.h"
41 #include "commands/defrem.h"
42 #include "commands/vacuum.h"
43 #include "miscadmin.h"
44 #include "nodes/makefuncs.h"
45 #include "pgstat.h"
46 #include "postmaster/autovacuum.h"
48 #include "postmaster/interrupt.h"
49 #include "storage/bufmgr.h"
50 #include "storage/lmgr.h"
51 #include "storage/pmsignal.h"
52 #include "storage/proc.h"
53 #include "storage/procarray.h"
54 #include "utils/acl.h"
55 #include "utils/fmgroids.h"
56 #include "utils/guc.h"
57 #include "utils/guc_hooks.h"
58 #include "utils/memutils.h"
59 #include "utils/snapmgr.h"
60 #include "utils/syscache.h"
61 
62 
63 /*
64  * GUC parameters
65  */
72 
73 /*
74  * Variables for cost-based vacuum delay. The defaults differ between
75  * autovacuum and vacuum. They should be set with the appropriate GUC value in
76  * vacuum code. They are initialized here to the defaults for client backends
77  * executing VACUUM or ANALYZE.
78  */
79 double vacuum_cost_delay = 0;
81 
82 /*
83  * VacuumFailsafeActive is a defined as a global so that we can determine
84  * whether or not to re-enable cost-based vacuum delay when vacuuming a table.
85  * If failsafe mode has been engaged, we will not re-enable cost-based delay
86  * for the table until after vacuuming has completed, regardless of other
87  * settings.
88  *
89  * Only VACUUM code should inspect this variable and only table access methods
90  * should set it to true. In Table AM-agnostic VACUUM code, this variable is
91  * inspected to determine whether or not to allow cost-based delays. Table AMs
92  * are free to set it if they desire this behavior, but it is false by default
93  * and reset to false in between vacuuming each relation.
94  */
95 bool VacuumFailsafeActive = false;
96 
97 /*
98  * Variables for cost-based parallel vacuum. See comments atop
99  * compute_parallel_delay to understand how it works.
100  */
104 
105 /* non-export function prototypes */
107  MemoryContext vac_context, int options);
108 static List *get_all_vacuum_rels(MemoryContext vac_context, int options);
109 static void vac_truncate_clog(TransactionId frozenXID,
110  MultiXactId minMulti,
111  TransactionId lastSaneFrozenXid,
112  MultiXactId lastSaneMinMulti);
113 static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
114  BufferAccessStrategy bstrategy);
115 static double compute_parallel_delay(void);
117 static bool vac_tid_reaped(ItemPointer itemptr, void *state);
118 
119 /*
120  * GUC check function to ensure GUC value specified is within the allowable
121  * range.
122  */
123 bool
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("\"vacuum_buffer_usage_limit\" must be 0 or between %d kB and %d kB",
135 
136  return false;
137 }
138 
139 /*
140  * Primary entry point for manual VACUUM and ANALYZE commands
141  *
142  * This is mainly a preparation wrapper for the real operations that will
143  * happen in vacuum().
144  */
145 void
146 ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
147 {
148  VacuumParams params;
149  BufferAccessStrategy bstrategy = NULL;
150  bool verbose = false;
151  bool skip_locked = false;
152  bool analyze = false;
153  bool freeze = false;
154  bool full = false;
155  bool disable_page_skipping = false;
156  bool process_main = true;
157  bool process_toast = true;
158  int ring_size;
159  bool skip_database_stats = false;
160  bool only_database_stats = false;
161  MemoryContext vac_context;
162  ListCell *lc;
163 
164  /* index_cleanup and truncate values unspecified for now */
167 
168  /* By default parallel vacuum is enabled */
169  params.nworkers = 0;
170 
171  /* Will be set later if we recurse to a TOAST table. */
172  params.toast_parent = InvalidOid;
173 
174  /*
175  * Set this to an invalid value so it is clear whether or not a
176  * BUFFER_USAGE_LIMIT was specified when making the access strategy.
177  */
178  ring_size = -1;
179 
180  /* Parse options list */
181  foreach(lc, vacstmt->options)
182  {
183  DefElem *opt = (DefElem *) lfirst(lc);
184 
185  /* Parse common options for VACUUM and ANALYZE */
186  if (strcmp(opt->defname, "verbose") == 0)
187  verbose = defGetBoolean(opt);
188  else if (strcmp(opt->defname, "skip_locked") == 0)
189  skip_locked = defGetBoolean(opt);
190  else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
191  {
192  const char *hintmsg;
193  int result;
194  char *vac_buffer_size;
195 
196  vac_buffer_size = defGetString(opt);
197 
198  /*
199  * Check that the specified value is valid and the size falls
200  * within the hard upper and lower limits if it is not 0.
201  */
202  if (!parse_int(vac_buffer_size, &result, GUC_UNIT_KB, &hintmsg) ||
203  (result != 0 &&
204  (result < MIN_BAS_VAC_RING_SIZE_KB || result > MAX_BAS_VAC_RING_SIZE_KB)))
205  {
206  ereport(ERROR,
207  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
208  errmsg("BUFFER_USAGE_LIMIT option must be 0 or between %d kB and %d kB",
210  hintmsg ? errhint("%s", _(hintmsg)) : 0));
211  }
212 
213  ring_size = result;
214  }
215  else if (!vacstmt->is_vacuumcmd)
216  ereport(ERROR,
217  (errcode(ERRCODE_SYNTAX_ERROR),
218  errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
219  parser_errposition(pstate, opt->location)));
220 
221  /* Parse options available on VACUUM */
222  else if (strcmp(opt->defname, "analyze") == 0)
223  analyze = defGetBoolean(opt);
224  else if (strcmp(opt->defname, "freeze") == 0)
225  freeze = defGetBoolean(opt);
226  else if (strcmp(opt->defname, "full") == 0)
227  full = defGetBoolean(opt);
228  else if (strcmp(opt->defname, "disable_page_skipping") == 0)
229  disable_page_skipping = defGetBoolean(opt);
230  else if (strcmp(opt->defname, "index_cleanup") == 0)
231  {
232  /* Interpret no string as the default, which is 'auto' */
233  if (!opt->arg)
235  else
236  {
237  char *sval = defGetString(opt);
238 
239  /* Try matching on 'auto' string, or fall back on boolean */
240  if (pg_strcasecmp(sval, "auto") == 0)
242  else
244  }
245  }
246  else if (strcmp(opt->defname, "process_main") == 0)
247  process_main = defGetBoolean(opt);
248  else if (strcmp(opt->defname, "process_toast") == 0)
249  process_toast = defGetBoolean(opt);
250  else if (strcmp(opt->defname, "truncate") == 0)
251  params.truncate = get_vacoptval_from_boolean(opt);
252  else if (strcmp(opt->defname, "parallel") == 0)
253  {
254  if (opt->arg == NULL)
255  {
256  ereport(ERROR,
257  (errcode(ERRCODE_SYNTAX_ERROR),
258  errmsg("parallel option requires a value between 0 and %d",
260  parser_errposition(pstate, opt->location)));
261  }
262  else
263  {
264  int nworkers;
265 
266  nworkers = defGetInt32(opt);
267  if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
268  ereport(ERROR,
269  (errcode(ERRCODE_SYNTAX_ERROR),
270  errmsg("parallel workers for vacuum must be between 0 and %d",
272  parser_errposition(pstate, opt->location)));
273 
274  /*
275  * Disable parallel vacuum, if user has specified parallel
276  * degree as zero.
277  */
278  if (nworkers == 0)
279  params.nworkers = -1;
280  else
281  params.nworkers = nworkers;
282  }
283  }
284  else if (strcmp(opt->defname, "skip_database_stats") == 0)
285  skip_database_stats = defGetBoolean(opt);
286  else if (strcmp(opt->defname, "only_database_stats") == 0)
287  only_database_stats = defGetBoolean(opt);
288  else
289  ereport(ERROR,
290  (errcode(ERRCODE_SYNTAX_ERROR),
291  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
292  parser_errposition(pstate, opt->location)));
293  }
294 
295  /* Set vacuum options */
296  params.options =
297  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
298  (verbose ? VACOPT_VERBOSE : 0) |
299  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
300  (analyze ? VACOPT_ANALYZE : 0) |
301  (freeze ? VACOPT_FREEZE : 0) |
302  (full ? VACOPT_FULL : 0) |
303  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
304  (process_main ? VACOPT_PROCESS_MAIN : 0) |
305  (process_toast ? VACOPT_PROCESS_TOAST : 0) |
306  (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
307  (only_database_stats ? VACOPT_ONLY_DATABASE_STATS : 0);
308 
309  /* sanity checks on options */
311  Assert((params.options & VACOPT_VACUUM) ||
312  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
313 
314  if ((params.options & VACOPT_FULL) && params.nworkers > 0)
315  ereport(ERROR,
316  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
317  errmsg("VACUUM FULL cannot be performed in parallel")));
318 
319  /*
320  * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
321  * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
322  * we'll permit that.
323  */
324  if (ring_size != -1 && (params.options & VACOPT_FULL) &&
325  !(params.options & VACOPT_ANALYZE))
326  ereport(ERROR,
327  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
328  errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
329 
330  /*
331  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
332  */
333  if (!(params.options & VACOPT_ANALYZE))
334  {
335  foreach(lc, vacstmt->rels)
336  {
338 
339  if (vrel->va_cols != NIL)
340  ereport(ERROR,
341  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
342  errmsg("ANALYZE option must be specified when a column list is provided")));
343  }
344  }
345 
346 
347  /*
348  * Sanity check DISABLE_PAGE_SKIPPING option.
349  */
350  if ((params.options & VACOPT_FULL) != 0 &&
351  (params.options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
352  ereport(ERROR,
353  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
354  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
355 
356  /* sanity check for PROCESS_TOAST */
357  if ((params.options & VACOPT_FULL) != 0 &&
358  (params.options & VACOPT_PROCESS_TOAST) == 0)
359  ereport(ERROR,
360  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
361  errmsg("PROCESS_TOAST required with VACUUM FULL")));
362 
363  /* sanity check for ONLY_DATABASE_STATS */
364  if (params.options & VACOPT_ONLY_DATABASE_STATS)
365  {
366  Assert(params.options & VACOPT_VACUUM);
367  if (vacstmt->rels != NIL)
368  ereport(ERROR,
369  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
370  errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
371  /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
372  if (params.options & ~(VACOPT_VACUUM |
377  ereport(ERROR,
378  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
379  errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
380  }
381 
382  /*
383  * All freeze ages are zero if the FREEZE option is given; otherwise pass
384  * them as -1 which means to use the default values.
385  */
386  if (params.options & VACOPT_FREEZE)
387  {
388  params.freeze_min_age = 0;
389  params.freeze_table_age = 0;
390  params.multixact_freeze_min_age = 0;
391  params.multixact_freeze_table_age = 0;
392  }
393  else
394  {
395  params.freeze_min_age = -1;
396  params.freeze_table_age = -1;
397  params.multixact_freeze_min_age = -1;
398  params.multixact_freeze_table_age = -1;
399  }
400 
401  /* user-invoked vacuum is never "for wraparound" */
402  params.is_wraparound = false;
403 
404  /* user-invoked vacuum uses VACOPT_VERBOSE instead of log_min_duration */
405  params.log_min_duration = -1;
406 
407  /*
408  * Create special memory context for cross-transaction storage.
409  *
410  * Since it is a child of PortalContext, it will go away eventually even
411  * if we suffer an error; there's no need for special abort cleanup logic.
412  */
413  vac_context = AllocSetContextCreate(PortalContext,
414  "Vacuum",
416 
417  /*
418  * Make a buffer strategy object in the cross-transaction memory context.
419  * We needn't bother making this for VACUUM (FULL) or VACUUM
420  * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
421  * ANALYZE) is possible, so we'd better ensure that we make a strategy
422  * when we see ANALYZE.
423  */
424  if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
425  VACOPT_FULL)) == 0 ||
426  (params.options & VACOPT_ANALYZE) != 0)
427  {
428 
429  MemoryContext old_context = MemoryContextSwitchTo(vac_context);
430 
431  Assert(ring_size >= -1);
432 
433  /*
434  * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
435  * command, it overrides the value of VacuumBufferUsageLimit. Either
436  * value may be 0, in which case GetAccessStrategyWithSize() will
437  * return NULL, effectively allowing full use of shared buffers.
438  */
439  if (ring_size == -1)
440  ring_size = VacuumBufferUsageLimit;
441 
442  bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, ring_size);
443 
444  MemoryContextSwitchTo(old_context);
445  }
446 
447  /* Now go through the common routine */
448  vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
449 
450  /* Finally, clean up the vacuum memory context */
451  MemoryContextDelete(vac_context);
452 }
453 
454 /*
455  * Internal entry point for autovacuum and the VACUUM / ANALYZE commands.
456  *
457  * relations, if not NIL, is a list of VacuumRelation to process; otherwise,
458  * we process all relevant tables in the database. For each VacuumRelation,
459  * if a valid OID is supplied, the table with that OID is what to process;
460  * otherwise, the VacuumRelation's RangeVar indicates what to process.
461  *
462  * params contains a set of parameters that can be used to customize the
463  * behavior.
464  *
465  * bstrategy may be passed in as NULL when the caller does not want to
466  * restrict the number of shared_buffers that VACUUM / ANALYZE can use,
467  * otherwise, the caller must build a BufferAccessStrategy with the number of
468  * shared_buffers that VACUUM / ANALYZE should try to limit themselves to
469  * using.
470  *
471  * isTopLevel should be passed down from ProcessUtility.
472  *
473  * It is the caller's responsibility that all parameters are allocated in a
474  * memory context that will not disappear at transaction commit.
475  */
476 void
477 vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
478  MemoryContext vac_context, bool isTopLevel)
479 {
480  static bool in_vacuum = false;
481 
482  const char *stmttype;
483  volatile bool in_outer_xact,
484  use_own_xacts;
485 
486  Assert(params != NULL);
487 
488  stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
489 
490  /*
491  * We cannot run VACUUM inside a user transaction block; if we were inside
492  * a transaction, then our commit- and start-transaction-command calls
493  * would not have the intended effect! There are numerous other subtle
494  * dependencies on this, too.
495  *
496  * ANALYZE (without VACUUM) can run either way.
497  */
498  if (params->options & VACOPT_VACUUM)
499  {
500  PreventInTransactionBlock(isTopLevel, stmttype);
501  in_outer_xact = false;
502  }
503  else
504  in_outer_xact = IsInTransactionBlock(isTopLevel);
505 
506  /*
507  * Check for and disallow recursive calls. This could happen when VACUUM
508  * FULL or ANALYZE calls a hostile index expression that itself calls
509  * ANALYZE.
510  */
511  if (in_vacuum)
512  ereport(ERROR,
513  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
514  errmsg("%s cannot be executed from VACUUM or ANALYZE",
515  stmttype)));
516 
517  /*
518  * Build list of relation(s) to process, putting any new data in
519  * vac_context for safekeeping.
520  */
521  if (params->options & VACOPT_ONLY_DATABASE_STATS)
522  {
523  /* We don't process any tables in this case */
524  Assert(relations == NIL);
525  }
526  else if (relations != NIL)
527  {
528  List *newrels = NIL;
529  ListCell *lc;
530 
531  foreach(lc, relations)
532  {
534  List *sublist;
535  MemoryContext old_context;
536 
537  sublist = expand_vacuum_rel(vrel, vac_context, params->options);
538  old_context = MemoryContextSwitchTo(vac_context);
539  newrels = list_concat(newrels, sublist);
540  MemoryContextSwitchTo(old_context);
541  }
542  relations = newrels;
543  }
544  else
545  relations = get_all_vacuum_rels(vac_context, params->options);
546 
547  /*
548  * Decide whether we need to start/commit our own transactions.
549  *
550  * For VACUUM (with or without ANALYZE): always do so, so that we can
551  * release locks as soon as possible. (We could possibly use the outer
552  * transaction for a one-table VACUUM, but handling TOAST tables would be
553  * problematic.)
554  *
555  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
556  * start/commit our own transactions. Also, there's no need to do so if
557  * only processing one relation. For multiple relations when not within a
558  * transaction block, and also in an autovacuum worker, use own
559  * transactions so we can release locks sooner.
560  */
561  if (params->options & VACOPT_VACUUM)
562  use_own_xacts = true;
563  else
564  {
565  Assert(params->options & VACOPT_ANALYZE);
567  use_own_xacts = true;
568  else if (in_outer_xact)
569  use_own_xacts = false;
570  else if (list_length(relations) > 1)
571  use_own_xacts = true;
572  else
573  use_own_xacts = false;
574  }
575 
576  /*
577  * vacuum_rel expects to be entered with no transaction active; it will
578  * start and commit its own transaction. But we are called by an SQL
579  * command, and so we are executing inside a transaction already. We
580  * commit the transaction started in PostgresMain() here, and start
581  * another one before exiting to match the commit waiting for us back in
582  * PostgresMain().
583  */
584  if (use_own_xacts)
585  {
586  Assert(!in_outer_xact);
587 
588  /* ActiveSnapshot is not set by autovacuum */
589  if (ActiveSnapshotSet())
591 
592  /* matches the StartTransaction in PostgresMain() */
594  }
595 
596  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
597  PG_TRY();
598  {
599  ListCell *cur;
600 
601  in_vacuum = true;
602  VacuumFailsafeActive = false;
604  VacuumCostBalance = 0;
607  VacuumActiveNWorkers = NULL;
608 
609  /*
610  * Loop to process each selected relation.
611  */
612  foreach(cur, relations)
613  {
615 
616  if (params->options & VACOPT_VACUUM)
617  {
618  if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy))
619  continue;
620  }
621 
622  if (params->options & VACOPT_ANALYZE)
623  {
624  /*
625  * If using separate xacts, start one for analyze. Otherwise,
626  * we can use the outer transaction.
627  */
628  if (use_own_xacts)
629  {
631  /* functions in indexes may want a snapshot set */
633  }
634 
635  analyze_rel(vrel->oid, vrel->relation, params,
636  vrel->va_cols, in_outer_xact, bstrategy);
637 
638  if (use_own_xacts)
639  {
642  }
643  else
644  {
645  /*
646  * If we're not using separate xacts, better separate the
647  * ANALYZE actions with CCIs. This avoids trouble if user
648  * says "ANALYZE t, t".
649  */
651  }
652  }
653 
654  /*
655  * Ensure VacuumFailsafeActive has been reset before vacuuming the
656  * next relation.
657  */
658  VacuumFailsafeActive = false;
659  }
660  }
661  PG_FINALLY();
662  {
663  in_vacuum = false;
664  VacuumCostActive = false;
665  VacuumFailsafeActive = false;
666  VacuumCostBalance = 0;
667  }
668  PG_END_TRY();
669 
670  /*
671  * Finish up processing.
672  */
673  if (use_own_xacts)
674  {
675  /* here, we are not in a transaction */
676 
677  /*
678  * This matches the CommitTransaction waiting for us in
679  * PostgresMain().
680  */
682  }
683 
684  if ((params->options & VACOPT_VACUUM) &&
685  !(params->options & VACOPT_SKIP_DATABASE_STATS))
686  {
687  /*
688  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
689  */
691  }
692 
693 }
694 
695 /*
696  * Check if the current user has privileges to vacuum or analyze the relation.
697  * If not, issue a WARNING log message and return false to let the caller
698  * decide what to do with this relation. This routine is used to decide if a
699  * relation can be processed for VACUUM or ANALYZE.
700  */
701 bool
703  bits32 options)
704 {
705  char *relname;
706 
708 
709  /*----------
710  * A role has privileges to vacuum or analyze the relation if any of the
711  * following are true:
712  * - the role owns the current database and the relation is not shared
713  * - the role has the MAINTAIN privilege on the relation
714  *----------
715  */
716  if ((object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()) &&
717  !reltuple->relisshared) ||
719  return true;
720 
721  relname = NameStr(reltuple->relname);
722 
723  if ((options & VACOPT_VACUUM) != 0)
724  {
726  (errmsg("permission denied to vacuum \"%s\", skipping it",
727  relname)));
728 
729  /*
730  * For VACUUM ANALYZE, both logs could show up, but just generate
731  * information for VACUUM as that would be the first one to be
732  * processed.
733  */
734  return false;
735  }
736 
737  if ((options & VACOPT_ANALYZE) != 0)
739  (errmsg("permission denied to analyze \"%s\", skipping it",
740  relname)));
741 
742  return false;
743 }
744 
745 
746 /*
747  * vacuum_open_relation
748  *
749  * This routine is used for attempting to open and lock a relation which
750  * is going to be vacuumed or analyzed. If the relation cannot be opened
751  * or locked, a log is emitted if possible.
752  */
753 Relation
755  bool verbose, LOCKMODE lmode)
756 {
757  Relation rel;
758  bool rel_lock = true;
759  int elevel;
760 
762 
763  /*
764  * Open the relation and get the appropriate lock on it.
765  *
766  * There's a race condition here: the relation may have gone away since
767  * the last time we saw it. If so, we don't need to vacuum or analyze it.
768  *
769  * If we've been asked not to wait for the relation lock, acquire it first
770  * in non-blocking mode, before calling try_relation_open().
771  */
772  if (!(options & VACOPT_SKIP_LOCKED))
773  rel = try_relation_open(relid, lmode);
774  else if (ConditionalLockRelationOid(relid, lmode))
775  rel = try_relation_open(relid, NoLock);
776  else
777  {
778  rel = NULL;
779  rel_lock = false;
780  }
781 
782  /* if relation is opened, leave */
783  if (rel)
784  return rel;
785 
786  /*
787  * Relation could not be opened, hence generate if possible a log
788  * informing on the situation.
789  *
790  * If the RangeVar is not defined, we do not have enough information to
791  * provide a meaningful log statement. Chances are that the caller has
792  * intentionally not provided this information so that this logging is
793  * skipped, anyway.
794  */
795  if (relation == NULL)
796  return NULL;
797 
798  /*
799  * Determine the log level.
800  *
801  * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
802  * statements in the permission checks; otherwise, only log if the caller
803  * so requested.
804  */
806  elevel = WARNING;
807  else if (verbose)
808  elevel = LOG;
809  else
810  return NULL;
811 
812  if ((options & VACOPT_VACUUM) != 0)
813  {
814  if (!rel_lock)
815  ereport(elevel,
816  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
817  errmsg("skipping vacuum of \"%s\" --- lock not available",
818  relation->relname)));
819  else
820  ereport(elevel,
822  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
823  relation->relname)));
824 
825  /*
826  * For VACUUM ANALYZE, both logs could show up, but just generate
827  * information for VACUUM as that would be the first one to be
828  * processed.
829  */
830  return NULL;
831  }
832 
833  if ((options & VACOPT_ANALYZE) != 0)
834  {
835  if (!rel_lock)
836  ereport(elevel,
837  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
838  errmsg("skipping analyze of \"%s\" --- lock not available",
839  relation->relname)));
840  else
841  ereport(elevel,
843  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
844  relation->relname)));
845  }
846 
847  return NULL;
848 }
849 
850 
851 /*
852  * Given a VacuumRelation, fill in the table OID if it wasn't specified,
853  * and optionally add VacuumRelations for partitions or inheritance children.
854  *
855  * If a VacuumRelation does not have an OID supplied and is a partitioned
856  * table, an extra entry will be added to the output for each partition.
857  * Presently, only autovacuum supplies OIDs when calling vacuum(), and
858  * it does not want us to expand partitioned tables.
859  *
860  * We take care not to modify the input data structure, but instead build
861  * new VacuumRelation(s) to return. (But note that they will reference
862  * unmodified parts of the input, eg column lists.) New data structures
863  * are made in vac_context.
864  */
865 static List *
867  int options)
868 {
869  List *vacrels = NIL;
870  MemoryContext oldcontext;
871 
872  /* If caller supplied OID, there's nothing we need do here. */
873  if (OidIsValid(vrel->oid))
874  {
875  oldcontext = MemoryContextSwitchTo(vac_context);
876  vacrels = lappend(vacrels, vrel);
877  MemoryContextSwitchTo(oldcontext);
878  }
879  else
880  {
881  /*
882  * Process a specific relation, and possibly partitions or child
883  * tables thereof.
884  */
885  Oid relid;
886  HeapTuple tuple;
887  Form_pg_class classForm;
888  bool include_children;
889  bool is_partitioned_table;
890  int rvr_opts;
891 
892  /*
893  * Since autovacuum workers supply OIDs when calling vacuum(), no
894  * autovacuum worker should reach this code.
895  */
897 
898  /*
899  * We transiently take AccessShareLock to protect the syscache lookup
900  * below, as well as find_all_inheritors's expectation that the caller
901  * holds some lock on the starting relation.
902  */
903  rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
904  relid = RangeVarGetRelidExtended(vrel->relation,
906  rvr_opts,
907  NULL, NULL);
908 
909  /*
910  * If the lock is unavailable, emit the same log statement that
911  * vacuum_rel() and analyze_rel() would.
912  */
913  if (!OidIsValid(relid))
914  {
915  if (options & VACOPT_VACUUM)
917  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
918  errmsg("skipping vacuum of \"%s\" --- lock not available",
919  vrel->relation->relname)));
920  else
922  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
923  errmsg("skipping analyze of \"%s\" --- lock not available",
924  vrel->relation->relname)));
925  return vacrels;
926  }
927 
928  /*
929  * To check whether the relation is a partitioned table and its
930  * ownership, fetch its syscache entry.
931  */
932  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
933  if (!HeapTupleIsValid(tuple))
934  elog(ERROR, "cache lookup failed for relation %u", relid);
935  classForm = (Form_pg_class) GETSTRUCT(tuple);
936 
937  /*
938  * Make a returnable VacuumRelation for this rel if the user has the
939  * required privileges.
940  */
941  if (vacuum_is_permitted_for_relation(relid, classForm, options))
942  {
943  oldcontext = MemoryContextSwitchTo(vac_context);
944  vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
945  relid,
946  vrel->va_cols));
947  MemoryContextSwitchTo(oldcontext);
948  }
949 
950  /*
951  * Vacuuming a partitioned table with ONLY will not do anything since
952  * the partitioned table itself is empty. Issue a warning if the user
953  * requests this.
954  */
955  include_children = vrel->relation->inh;
956  is_partitioned_table = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
957  if ((options & VACOPT_VACUUM) && is_partitioned_table && !include_children)
959  (errmsg("VACUUM ONLY of partitioned table \"%s\" has no effect",
960  vrel->relation->relname)));
961 
962  ReleaseSysCache(tuple);
963 
964  /*
965  * Unless the user has specified ONLY, make relation list entries for
966  * its partitions or inheritance child tables. Note that the list
967  * returned by find_all_inheritors() includes the passed-in OID, so we
968  * have to skip that. There's no point in taking locks on the
969  * individual partitions or child tables yet, and doing so would just
970  * add unnecessary deadlock risk. For this last reason, we do not yet
971  * check the ownership of the partitions/tables, which get added to
972  * the list to process. Ownership will be checked later on anyway.
973  */
974  if (include_children)
975  {
976  List *part_oids = find_all_inheritors(relid, NoLock, NULL);
977  ListCell *part_lc;
978 
979  foreach(part_lc, part_oids)
980  {
981  Oid part_oid = lfirst_oid(part_lc);
982 
983  if (part_oid == relid)
984  continue; /* ignore original table */
985 
986  /*
987  * We omit a RangeVar since it wouldn't be appropriate to
988  * complain about failure to open one of these relations
989  * later.
990  */
991  oldcontext = MemoryContextSwitchTo(vac_context);
992  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
993  part_oid,
994  vrel->va_cols));
995  MemoryContextSwitchTo(oldcontext);
996  }
997  }
998 
999  /*
1000  * Release lock again. This means that by the time we actually try to
1001  * process the table, it might be gone or renamed. In the former case
1002  * we'll silently ignore it; in the latter case we'll process it
1003  * anyway, but we must beware that the RangeVar doesn't necessarily
1004  * identify it anymore. This isn't ideal, perhaps, but there's little
1005  * practical alternative, since we're typically going to commit this
1006  * transaction and begin a new one between now and then. Moreover,
1007  * holding locks on multiple relations would create significant risk
1008  * of deadlock.
1009  */
1011  }
1012 
1013  return vacrels;
1014 }
1015 
1016 /*
1017  * Construct a list of VacuumRelations for all vacuumable rels in
1018  * the current database. The list is built in vac_context.
1019  */
1020 static List *
1022 {
1023  List *vacrels = NIL;
1024  Relation pgclass;
1025  TableScanDesc scan;
1026  HeapTuple tuple;
1027 
1028  pgclass = table_open(RelationRelationId, AccessShareLock);
1029 
1030  scan = table_beginscan_catalog(pgclass, 0, NULL);
1031 
1032  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1033  {
1034  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
1035  MemoryContext oldcontext;
1036  Oid relid = classForm->oid;
1037 
1038  /*
1039  * We include partitioned tables here; depending on which operation is
1040  * to be performed, caller will decide whether to process or ignore
1041  * them.
1042  */
1043  if (classForm->relkind != RELKIND_RELATION &&
1044  classForm->relkind != RELKIND_MATVIEW &&
1045  classForm->relkind != RELKIND_PARTITIONED_TABLE)
1046  continue;
1047 
1048  /* check permissions of relation */
1049  if (!vacuum_is_permitted_for_relation(relid, classForm, options))
1050  continue;
1051 
1052  /*
1053  * Build VacuumRelation(s) specifying the table OIDs to be processed.
1054  * We omit a RangeVar since it wouldn't be appropriate to complain
1055  * about failure to open one of these relations later.
1056  */
1057  oldcontext = MemoryContextSwitchTo(vac_context);
1058  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
1059  relid,
1060  NIL));
1061  MemoryContextSwitchTo(oldcontext);
1062  }
1063 
1064  table_endscan(scan);
1065  table_close(pgclass, AccessShareLock);
1066 
1067  return vacrels;
1068 }
1069 
1070 /*
1071  * vacuum_get_cutoffs() -- compute OldestXmin and freeze cutoff points
1072  *
1073  * The target relation and VACUUM parameters are our inputs.
1074  *
1075  * Output parameters are the cutoffs that VACUUM caller should use.
1076  *
1077  * Return value indicates if vacuumlazy.c caller should make its VACUUM
1078  * operation aggressive. An aggressive VACUUM must advance relfrozenxid up to
1079  * FreezeLimit (at a minimum), and relminmxid up to MultiXactCutoff (at a
1080  * minimum).
1081  */
1082 bool
1084  struct VacuumCutoffs *cutoffs)
1085 {
1086  int freeze_min_age,
1087  multixact_freeze_min_age,
1088  freeze_table_age,
1089  multixact_freeze_table_age,
1090  effective_multixact_freeze_max_age;
1091  TransactionId nextXID,
1092  safeOldestXmin,
1093  aggressiveXIDCutoff;
1094  MultiXactId nextMXID,
1095  safeOldestMxact,
1096  aggressiveMXIDCutoff;
1097 
1098  /* Use mutable copies of freeze age parameters */
1099  freeze_min_age = params->freeze_min_age;
1100  multixact_freeze_min_age = params->multixact_freeze_min_age;
1101  freeze_table_age = params->freeze_table_age;
1102  multixact_freeze_table_age = params->multixact_freeze_table_age;
1103 
1104  /* Set pg_class fields in cutoffs */
1105  cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1106  cutoffs->relminmxid = rel->rd_rel->relminmxid;
1107 
1108  /*
1109  * Acquire OldestXmin.
1110  *
1111  * We can always ignore processes running lazy vacuum. This is because we
1112  * use these values only for deciding which tuples we must keep in the
1113  * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1114  * XID assigned), it's safe to ignore it. In theory it could be
1115  * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1116  * that only one vacuum process can be working on a particular table at
1117  * any time, and that each vacuum is always an independent transaction.
1118  */
1120 
1122 
1123  /* Acquire OldestMxact */
1124  cutoffs->OldestMxact = GetOldestMultiXactId();
1126 
1127  /* Acquire next XID/next MXID values used to apply age-based settings */
1128  nextXID = ReadNextTransactionId();
1129  nextMXID = ReadNextMultiXactId();
1130 
1131  /*
1132  * Also compute the multixact age for which freezing is urgent. This is
1133  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1134  * short of multixact member space.
1135  */
1136  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1137 
1138  /*
1139  * Almost ready to set freeze output parameters; check if OldestXmin or
1140  * OldestMxact are held back to an unsafe degree before we start on that
1141  */
1142  safeOldestXmin = nextXID - autovacuum_freeze_max_age;
1143  if (!TransactionIdIsNormal(safeOldestXmin))
1144  safeOldestXmin = FirstNormalTransactionId;
1145  safeOldestMxact = nextMXID - effective_multixact_freeze_max_age;
1146  if (safeOldestMxact < FirstMultiXactId)
1147  safeOldestMxact = FirstMultiXactId;
1148  if (TransactionIdPrecedes(cutoffs->OldestXmin, safeOldestXmin))
1149  ereport(WARNING,
1150  (errmsg("cutoff for removing and freezing tuples is far in the past"),
1151  errhint("Close open transactions soon to avoid wraparound problems.\n"
1152  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1153  if (MultiXactIdPrecedes(cutoffs->OldestMxact, safeOldestMxact))
1154  ereport(WARNING,
1155  (errmsg("cutoff for freezing multixacts is far in the past"),
1156  errhint("Close open transactions soon to avoid wraparound problems.\n"
1157  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1158 
1159  /*
1160  * Determine the minimum freeze age to use: as specified by the caller, or
1161  * vacuum_freeze_min_age, but in any case not more than half
1162  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1163  * wraparound won't occur too frequently.
1164  */
1165  if (freeze_min_age < 0)
1166  freeze_min_age = vacuum_freeze_min_age;
1167  freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1168  Assert(freeze_min_age >= 0);
1169 
1170  /* Compute FreezeLimit, being careful to generate a normal XID */
1171  cutoffs->FreezeLimit = nextXID - freeze_min_age;
1172  if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1174  /* FreezeLimit must always be <= OldestXmin */
1175  if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1176  cutoffs->FreezeLimit = cutoffs->OldestXmin;
1177 
1178  /*
1179  * Determine the minimum multixact freeze age to use: as specified by
1180  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1181  * than half effective_multixact_freeze_max_age, so that autovacuums to
1182  * prevent MultiXact wraparound won't occur too frequently.
1183  */
1184  if (multixact_freeze_min_age < 0)
1185  multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1186  multixact_freeze_min_age = Min(multixact_freeze_min_age,
1187  effective_multixact_freeze_max_age / 2);
1188  Assert(multixact_freeze_min_age >= 0);
1189 
1190  /* Compute MultiXactCutoff, being careful to generate a valid value */
1191  cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1192  if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1193  cutoffs->MultiXactCutoff = FirstMultiXactId;
1194  /* MultiXactCutoff must always be <= OldestMxact */
1195  if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1196  cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1197 
1198  /*
1199  * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1200  *
1201  * Determine the table freeze age to use: as specified by the caller, or
1202  * the value of the vacuum_freeze_table_age GUC, but in any case not more
1203  * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1204  * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1205  * anti-wraparound autovacuum is launched.
1206  */
1207  if (freeze_table_age < 0)
1208  freeze_table_age = vacuum_freeze_table_age;
1209  freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1210  Assert(freeze_table_age >= 0);
1211  aggressiveXIDCutoff = nextXID - freeze_table_age;
1212  if (!TransactionIdIsNormal(aggressiveXIDCutoff))
1213  aggressiveXIDCutoff = FirstNormalTransactionId;
1215  aggressiveXIDCutoff))
1216  return true;
1217 
1218  /*
1219  * Similar to the above, determine the table freeze age to use for
1220  * multixacts: as specified by the caller, or the value of the
1221  * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1222  * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1223  * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1224  * multixacts before anti-wraparound autovacuum is launched.
1225  */
1226  if (multixact_freeze_table_age < 0)
1227  multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1228  multixact_freeze_table_age =
1229  Min(multixact_freeze_table_age,
1230  effective_multixact_freeze_max_age * 0.95);
1231  Assert(multixact_freeze_table_age >= 0);
1232  aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1233  if (aggressiveMXIDCutoff < FirstMultiXactId)
1234  aggressiveMXIDCutoff = FirstMultiXactId;
1236  aggressiveMXIDCutoff))
1237  return true;
1238 
1239  /* Non-aggressive VACUUM */
1240  return false;
1241 }
1242 
1243 /*
1244  * vacuum_xid_failsafe_check() -- Used by VACUUM's wraparound failsafe
1245  * mechanism to determine if its table's relfrozenxid and relminmxid are now
1246  * dangerously far in the past.
1247  *
1248  * When we return true, VACUUM caller triggers the failsafe.
1249  */
1250 bool
1252 {
1253  TransactionId relfrozenxid = cutoffs->relfrozenxid;
1254  MultiXactId relminmxid = cutoffs->relminmxid;
1255  TransactionId xid_skip_limit;
1256  MultiXactId multi_skip_limit;
1257  int skip_index_vacuum;
1258 
1259  Assert(TransactionIdIsNormal(relfrozenxid));
1260  Assert(MultiXactIdIsValid(relminmxid));
1261 
1262  /*
1263  * Determine the index skipping age to use. In any case no less than
1264  * autovacuum_freeze_max_age * 1.05.
1265  */
1266  skip_index_vacuum = Max(vacuum_failsafe_age, autovacuum_freeze_max_age * 1.05);
1267 
1268  xid_skip_limit = ReadNextTransactionId() - skip_index_vacuum;
1269  if (!TransactionIdIsNormal(xid_skip_limit))
1270  xid_skip_limit = FirstNormalTransactionId;
1271 
1272  if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1273  {
1274  /* The table's relfrozenxid is too old */
1275  return true;
1276  }
1277 
1278  /*
1279  * Similar to above, determine the index skipping age to use for
1280  * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1281  * 1.05.
1282  */
1283  skip_index_vacuum = Max(vacuum_multixact_failsafe_age,
1285 
1286  multi_skip_limit = ReadNextMultiXactId() - skip_index_vacuum;
1287  if (multi_skip_limit < FirstMultiXactId)
1288  multi_skip_limit = FirstMultiXactId;
1289 
1290  if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1291  {
1292  /* The table's relminmxid is too old */
1293  return true;
1294  }
1295 
1296  return false;
1297 }
1298 
1299 /*
1300  * vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
1301  *
1302  * If we scanned the whole relation then we should just use the count of
1303  * live tuples seen; but if we did not, we should not blindly extrapolate
1304  * from that number, since VACUUM may have scanned a quite nonrandom
1305  * subset of the table. When we have only partial information, we take
1306  * the old value of pg_class.reltuples/pg_class.relpages as a measurement
1307  * of the tuple density in the unscanned pages.
1308  *
1309  * Note: scanned_tuples should count only *live* tuples, since
1310  * pg_class.reltuples is defined that way.
1311  */
1312 double
1314  BlockNumber total_pages,
1315  BlockNumber scanned_pages,
1316  double scanned_tuples)
1317 {
1318  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1319  double old_rel_tuples = relation->rd_rel->reltuples;
1320  double old_density;
1321  double unscanned_pages;
1322  double total_tuples;
1323 
1324  /* If we did scan the whole table, just use the count as-is */
1325  if (scanned_pages >= total_pages)
1326  return scanned_tuples;
1327 
1328  /*
1329  * When successive VACUUM commands scan the same few pages again and
1330  * again, without anything from the table really changing, there is a risk
1331  * that our beliefs about tuple density will gradually become distorted.
1332  * This might be caused by vacuumlazy.c implementation details, such as
1333  * its tendency to always scan the last heap page. Handle that here.
1334  *
1335  * If the relation is _exactly_ the same size according to the existing
1336  * pg_class entry, and only a few of its pages (less than 2%) were
1337  * scanned, keep the existing value of reltuples. Also keep the existing
1338  * value when only a subset of rel's pages <= a single page were scanned.
1339  *
1340  * (Note: we might be returning -1 here.)
1341  */
1342  if (old_rel_pages == total_pages &&
1343  scanned_pages < (double) total_pages * 0.02)
1344  return old_rel_tuples;
1345  if (scanned_pages <= 1)
1346  return old_rel_tuples;
1347 
1348  /*
1349  * If old density is unknown, we can't do much except scale up
1350  * scanned_tuples to match total_pages.
1351  */
1352  if (old_rel_tuples < 0 || old_rel_pages == 0)
1353  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1354 
1355  /*
1356  * Okay, we've covered the corner cases. The normal calculation is to
1357  * convert the old measurement to a density (tuples per page), then
1358  * estimate the number of tuples in the unscanned pages using that figure,
1359  * and finally add on the number of tuples in the scanned pages.
1360  */
1361  old_density = old_rel_tuples / old_rel_pages;
1362  unscanned_pages = (double) total_pages - (double) scanned_pages;
1363  total_tuples = old_density * unscanned_pages + scanned_tuples;
1364  return floor(total_tuples + 0.5);
1365 }
1366 
1367 
1368 /*
1369  * vac_update_relstats() -- update statistics for one relation
1370  *
1371  * Update the whole-relation statistics that are kept in its pg_class
1372  * row. There are additional stats that will be updated if we are
1373  * doing ANALYZE, but we always update these stats. This routine works
1374  * for both index and heap relation entries in pg_class.
1375  *
1376  * We violate transaction semantics here by overwriting the rel's
1377  * existing pg_class tuple with the new values. This is reasonably
1378  * safe as long as we're sure that the new values are correct whether or
1379  * not this transaction commits. The reason for doing this is that if
1380  * we updated these tuples in the usual way, vacuuming pg_class itself
1381  * wouldn't work very well --- by the time we got done with a vacuum
1382  * cycle, most of the tuples in pg_class would've been obsoleted. Of
1383  * course, this only works for fixed-size not-null columns, but these are.
1384  *
1385  * Another reason for doing it this way is that when we are in a lazy
1386  * VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
1387  * Somebody vacuuming pg_class might think they could delete a tuple
1388  * marked with xmin = our xid.
1389  *
1390  * In addition to fundamentally nontransactional statistics such as
1391  * relpages and relallvisible, we try to maintain certain lazily-updated
1392  * DDL flags such as relhasindex, by clearing them if no longer correct.
1393  * It's safe to do this in VACUUM, which can't run in parallel with
1394  * CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
1395  * However, it's *not* safe to do it in an ANALYZE that's within an
1396  * outer transaction, because for example the current transaction might
1397  * have dropped the last index; then we'd think relhasindex should be
1398  * cleared, but if the transaction later rolls back this would be wrong.
1399  * So we refrain from updating the DDL flags if we're inside an outer
1400  * transaction. This is OK since postponing the flag maintenance is
1401  * always allowable.
1402  *
1403  * Note: num_tuples should count only *live* tuples, since
1404  * pg_class.reltuples is defined that way.
1405  *
1406  * This routine is shared by VACUUM and ANALYZE.
1407  */
1408 void
1410  BlockNumber num_pages, double num_tuples,
1411  BlockNumber num_all_visible_pages,
1412  bool hasindex, TransactionId frozenxid,
1413  MultiXactId minmulti,
1414  bool *frozenxid_updated, bool *minmulti_updated,
1415  bool in_outer_xact)
1416 {
1417  Oid relid = RelationGetRelid(relation);
1418  Relation rd;
1419  ScanKeyData key[1];
1420  HeapTuple ctup;
1421  void *inplace_state;
1422  Form_pg_class pgcform;
1423  bool dirty,
1424  futurexid,
1425  futuremxid;
1426  TransactionId oldfrozenxid;
1427  MultiXactId oldminmulti;
1428 
1429  rd = table_open(RelationRelationId, RowExclusiveLock);
1430 
1431  /* Fetch a copy of the tuple to scribble on */
1432  ScanKeyInit(&key[0],
1433  Anum_pg_class_oid,
1434  BTEqualStrategyNumber, F_OIDEQ,
1435  ObjectIdGetDatum(relid));
1436  systable_inplace_update_begin(rd, ClassOidIndexId, true,
1437  NULL, 1, key, &ctup, &inplace_state);
1438  if (!HeapTupleIsValid(ctup))
1439  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1440  relid);
1441  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1442 
1443  /* Apply statistical updates, if any, to copied tuple */
1444 
1445  dirty = false;
1446  if (pgcform->relpages != (int32) num_pages)
1447  {
1448  pgcform->relpages = (int32) num_pages;
1449  dirty = true;
1450  }
1451  if (pgcform->reltuples != (float4) num_tuples)
1452  {
1453  pgcform->reltuples = (float4) num_tuples;
1454  dirty = true;
1455  }
1456  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1457  {
1458  pgcform->relallvisible = (int32) num_all_visible_pages;
1459  dirty = true;
1460  }
1461 
1462  /* Apply DDL updates, but not inside an outer transaction (see above) */
1463 
1464  if (!in_outer_xact)
1465  {
1466  /*
1467  * If we didn't find any indexes, reset relhasindex.
1468  */
1469  if (pgcform->relhasindex && !hasindex)
1470  {
1471  pgcform->relhasindex = false;
1472  dirty = true;
1473  }
1474 
1475  /* We also clear relhasrules and relhastriggers if needed */
1476  if (pgcform->relhasrules && relation->rd_rules == NULL)
1477  {
1478  pgcform->relhasrules = false;
1479  dirty = true;
1480  }
1481  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1482  {
1483  pgcform->relhastriggers = false;
1484  dirty = true;
1485  }
1486  }
1487 
1488  /*
1489  * Update relfrozenxid, unless caller passed InvalidTransactionId
1490  * indicating it has no new data.
1491  *
1492  * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1493  * stored relfrozenxid is "in the future" then it seems best to assume
1494  * it's corrupt, and overwrite with the oldest remaining XID in the table.
1495  * This should match vac_update_datfrozenxid() concerning what we consider
1496  * to be "in the future".
1497  */
1498  oldfrozenxid = pgcform->relfrozenxid;
1499  futurexid = false;
1500  if (frozenxid_updated)
1501  *frozenxid_updated = false;
1502  if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1503  {
1504  bool update = false;
1505 
1506  if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1507  update = true;
1508  else if (TransactionIdPrecedes(ReadNextTransactionId(), oldfrozenxid))
1509  futurexid = update = true;
1510 
1511  if (update)
1512  {
1513  pgcform->relfrozenxid = frozenxid;
1514  dirty = true;
1515  if (frozenxid_updated)
1516  *frozenxid_updated = true;
1517  }
1518  }
1519 
1520  /* Similarly for relminmxid */
1521  oldminmulti = pgcform->relminmxid;
1522  futuremxid = false;
1523  if (minmulti_updated)
1524  *minmulti_updated = false;
1525  if (MultiXactIdIsValid(minmulti) && oldminmulti != minmulti)
1526  {
1527  bool update = false;
1528 
1529  if (MultiXactIdPrecedes(oldminmulti, minmulti))
1530  update = true;
1531  else if (MultiXactIdPrecedes(ReadNextMultiXactId(), oldminmulti))
1532  futuremxid = update = true;
1533 
1534  if (update)
1535  {
1536  pgcform->relminmxid = minmulti;
1537  dirty = true;
1538  if (minmulti_updated)
1539  *minmulti_updated = true;
1540  }
1541  }
1542 
1543  /* If anything changed, write out the tuple. */
1544  if (dirty)
1545  systable_inplace_update_finish(inplace_state, ctup);
1546  else
1547  systable_inplace_update_cancel(inplace_state);
1548 
1550 
1551  if (futurexid)
1552  ereport(WARNING,
1554  errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1555  oldfrozenxid, frozenxid,
1556  RelationGetRelationName(relation))));
1557  if (futuremxid)
1558  ereport(WARNING,
1560  errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1561  oldminmulti, minmulti,
1562  RelationGetRelationName(relation))));
1563 }
1564 
1565 
1566 /*
1567  * vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
1568  *
1569  * Update pg_database's datfrozenxid entry for our database to be the
1570  * minimum of the pg_class.relfrozenxid values.
1571  *
1572  * Similarly, update our datminmxid to be the minimum of the
1573  * pg_class.relminmxid values.
1574  *
1575  * If we are able to advance either pg_database value, also try to
1576  * truncate pg_xact and pg_multixact.
1577  *
1578  * We violate transaction semantics here by overwriting the database's
1579  * existing pg_database tuple with the new values. This is reasonably
1580  * safe since the new values are correct whether or not this transaction
1581  * commits. As with vac_update_relstats, this avoids leaving dead tuples
1582  * behind after a VACUUM.
1583  */
1584 void
1586 {
1587  HeapTuple tuple;
1588  Form_pg_database dbform;
1589  Relation relation;
1590  SysScanDesc scan;
1591  HeapTuple classTup;
1592  TransactionId newFrozenXid;
1593  MultiXactId newMinMulti;
1594  TransactionId lastSaneFrozenXid;
1595  MultiXactId lastSaneMinMulti;
1596  bool bogus = false;
1597  bool dirty = false;
1598  ScanKeyData key[1];
1599  void *inplace_state;
1600 
1601  /*
1602  * Restrict this task to one backend per database. This avoids race
1603  * conditions that would move datfrozenxid or datminmxid backward. It
1604  * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1605  * datfrozenxid passed to an earlier vac_truncate_clog() call.
1606  */
1608 
1609  /*
1610  * Initialize the "min" calculation with
1611  * GetOldestNonRemovableTransactionId(), which is a reasonable
1612  * approximation to the minimum relfrozenxid for not-yet-committed
1613  * pg_class entries for new tables; see AddNewRelationTuple(). So we
1614  * cannot produce a wrong minimum by starting with this.
1615  */
1616  newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
1617 
1618  /*
1619  * Similarly, initialize the MultiXact "min" with the value that would be
1620  * used on pg_class for new tables. See AddNewRelationTuple().
1621  */
1622  newMinMulti = GetOldestMultiXactId();
1623 
1624  /*
1625  * Identify the latest relfrozenxid and relminmxid values that we could
1626  * validly see during the scan. These are conservative values, but it's
1627  * not really worth trying to be more exact.
1628  */
1629  lastSaneFrozenXid = ReadNextTransactionId();
1630  lastSaneMinMulti = ReadNextMultiXactId();
1631 
1632  /*
1633  * We must seqscan pg_class to find the minimum Xid, because there is no
1634  * index that can help us here.
1635  *
1636  * See vac_truncate_clog() for the race condition to prevent.
1637  */
1638  relation = table_open(RelationRelationId, AccessShareLock);
1639 
1640  scan = systable_beginscan(relation, InvalidOid, false,
1641  NULL, 0, NULL);
1642 
1643  while ((classTup = systable_getnext(scan)) != NULL)
1644  {
1645  volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1646  TransactionId relfrozenxid = classForm->relfrozenxid;
1647  TransactionId relminmxid = classForm->relminmxid;
1648 
1649  /*
1650  * Only consider relations able to hold unfrozen XIDs (anything else
1651  * should have InvalidTransactionId in relfrozenxid anyway).
1652  */
1653  if (classForm->relkind != RELKIND_RELATION &&
1654  classForm->relkind != RELKIND_MATVIEW &&
1655  classForm->relkind != RELKIND_TOASTVALUE)
1656  {
1657  Assert(!TransactionIdIsValid(relfrozenxid));
1658  Assert(!MultiXactIdIsValid(relminmxid));
1659  continue;
1660  }
1661 
1662  /*
1663  * Some table AMs might not need per-relation xid / multixid horizons.
1664  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1665  * to not be set (i.e. set to their respective Invalid*Id)
1666  * independently. Thus validate and compute horizon for each only if
1667  * set.
1668  *
1669  * If things are working properly, no relation should have a
1670  * relfrozenxid or relminmxid that is "in the future". However, such
1671  * cases have been known to arise due to bugs in pg_upgrade. If we
1672  * see any entries that are "in the future", chicken out and don't do
1673  * anything. This ensures we won't truncate clog & multixact SLRUs
1674  * before those relations have been scanned and cleaned up.
1675  */
1676 
1677  if (TransactionIdIsValid(relfrozenxid))
1678  {
1679  Assert(TransactionIdIsNormal(relfrozenxid));
1680 
1681  /* check for values in the future */
1682  if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1683  {
1684  bogus = true;
1685  break;
1686  }
1687 
1688  /* determine new horizon */
1689  if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1690  newFrozenXid = relfrozenxid;
1691  }
1692 
1693  if (MultiXactIdIsValid(relminmxid))
1694  {
1695  /* check for values in the future */
1696  if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1697  {
1698  bogus = true;
1699  break;
1700  }
1701 
1702  /* determine new horizon */
1703  if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1704  newMinMulti = relminmxid;
1705  }
1706  }
1707 
1708  /* we're done with pg_class */
1709  systable_endscan(scan);
1710  table_close(relation, AccessShareLock);
1711 
1712  /* chicken out if bogus data found */
1713  if (bogus)
1714  return;
1715 
1716  Assert(TransactionIdIsNormal(newFrozenXid));
1717  Assert(MultiXactIdIsValid(newMinMulti));
1718 
1719  /* Now fetch the pg_database tuple we need to update. */
1720  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1721 
1722  /*
1723  * Fetch a copy of the tuple to scribble on. We could check the syscache
1724  * tuple first. If that concluded !dirty, we'd avoid waiting on
1725  * concurrent heap_update() and would avoid exclusive-locking the buffer.
1726  * For now, don't optimize that.
1727  */
1728  ScanKeyInit(&key[0],
1729  Anum_pg_database_oid,
1730  BTEqualStrategyNumber, F_OIDEQ,
1732 
1733  systable_inplace_update_begin(relation, DatabaseOidIndexId, true,
1734  NULL, 1, key, &tuple, &inplace_state);
1735 
1736  if (!HeapTupleIsValid(tuple))
1737  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1738 
1739  dbform = (Form_pg_database) GETSTRUCT(tuple);
1740 
1741  /*
1742  * As in vac_update_relstats(), we ordinarily don't want to let
1743  * datfrozenxid go backward; but if it's "in the future" then it must be
1744  * corrupt and it seems best to overwrite it.
1745  */
1746  if (dbform->datfrozenxid != newFrozenXid &&
1747  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1748  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1749  {
1750  dbform->datfrozenxid = newFrozenXid;
1751  dirty = true;
1752  }
1753  else
1754  newFrozenXid = dbform->datfrozenxid;
1755 
1756  /* Ditto for datminmxid */
1757  if (dbform->datminmxid != newMinMulti &&
1758  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1759  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1760  {
1761  dbform->datminmxid = newMinMulti;
1762  dirty = true;
1763  }
1764  else
1765  newMinMulti = dbform->datminmxid;
1766 
1767  if (dirty)
1768  systable_inplace_update_finish(inplace_state, tuple);
1769  else
1770  systable_inplace_update_cancel(inplace_state);
1771 
1772  heap_freetuple(tuple);
1773  table_close(relation, RowExclusiveLock);
1774 
1775  /*
1776  * If we were able to advance datfrozenxid or datminmxid, see if we can
1777  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1778  * XID-wrap-limit info is stale, since this action will update that too.
1779  */
1780  if (dirty || ForceTransactionIdLimitUpdate())
1781  vac_truncate_clog(newFrozenXid, newMinMulti,
1782  lastSaneFrozenXid, lastSaneMinMulti);
1783 }
1784 
1785 
1786 /*
1787  * vac_truncate_clog() -- attempt to truncate the commit log
1788  *
1789  * Scan pg_database to determine the system-wide oldest datfrozenxid,
1790  * and use it to truncate the transaction commit log (pg_xact).
1791  * Also update the XID wrap limit info maintained by varsup.c.
1792  * Likewise for datminmxid.
1793  *
1794  * The passed frozenXID and minMulti are the updated values for my own
1795  * pg_database entry. They're used to initialize the "min" calculations.
1796  * The caller also passes the "last sane" XID and MXID, since it has
1797  * those at hand already.
1798  *
1799  * This routine is only invoked when we've managed to change our
1800  * DB's datfrozenxid/datminmxid values, or we found that the shared
1801  * XID-wrap-limit info is stale.
1802  */
1803 static void
1805  MultiXactId minMulti,
1806  TransactionId lastSaneFrozenXid,
1807  MultiXactId lastSaneMinMulti)
1808 {
1810  Relation relation;
1811  TableScanDesc scan;
1812  HeapTuple tuple;
1813  Oid oldestxid_datoid;
1814  Oid minmulti_datoid;
1815  bool bogus = false;
1816  bool frozenAlreadyWrapped = false;
1817 
1818  /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
1819  LWLockAcquire(WrapLimitsVacuumLock, LW_EXCLUSIVE);
1820 
1821  /* init oldest datoids to sync with my frozenXID/minMulti values */
1822  oldestxid_datoid = MyDatabaseId;
1823  minmulti_datoid = MyDatabaseId;
1824 
1825  /*
1826  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1827  *
1828  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1829  * the values could change while we look at them. Fetch each one just
1830  * once to ensure sane behavior of the comparison logic. (Here, as in
1831  * many other places, we assume that fetching or updating an XID in shared
1832  * storage is atomic.)
1833  *
1834  * Note: we need not worry about a race condition with new entries being
1835  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1836  * existing DB's datfrozenxid, and that source DB cannot be ours because
1837  * of the interlock against copying a DB containing an active backend.
1838  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1839  * concurrently modify the datfrozenxid's of different databases, the
1840  * worst possible outcome is that pg_xact is not truncated as aggressively
1841  * as it could be.
1842  */
1843  relation = table_open(DatabaseRelationId, AccessShareLock);
1844 
1845  scan = table_beginscan_catalog(relation, 0, NULL);
1846 
1847  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1848  {
1849  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1850  TransactionId datfrozenxid = dbform->datfrozenxid;
1851  TransactionId datminmxid = dbform->datminmxid;
1852 
1855 
1856  /*
1857  * If database is in the process of getting dropped, or has been
1858  * interrupted while doing so, no connections to it are possible
1859  * anymore. Therefore we don't need to take it into account here.
1860  * Which is good, because it can't be processed by autovacuum either.
1861  */
1863  {
1864  elog(DEBUG2,
1865  "skipping invalid database \"%s\" while computing relfrozenxid",
1866  NameStr(dbform->datname));
1867  continue;
1868  }
1869 
1870  /*
1871  * If things are working properly, no database should have a
1872  * datfrozenxid or datminmxid that is "in the future". However, such
1873  * cases have been known to arise due to bugs in pg_upgrade. If we
1874  * see any entries that are "in the future", chicken out and don't do
1875  * anything. This ensures we won't truncate clog before those
1876  * databases have been scanned and cleaned up. (We will issue the
1877  * "already wrapped" warning if appropriate, though.)
1878  */
1879  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1880  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1881  bogus = true;
1882 
1883  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1884  frozenAlreadyWrapped = true;
1885  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1886  {
1887  frozenXID = datfrozenxid;
1888  oldestxid_datoid = dbform->oid;
1889  }
1890 
1891  if (MultiXactIdPrecedes(datminmxid, minMulti))
1892  {
1893  minMulti = datminmxid;
1894  minmulti_datoid = dbform->oid;
1895  }
1896  }
1897 
1898  table_endscan(scan);
1899 
1900  table_close(relation, AccessShareLock);
1901 
1902  /*
1903  * Do not truncate CLOG if we seem to have suffered wraparound already;
1904  * the computed minimum XID might be bogus. This case should now be
1905  * impossible due to the defenses in GetNewTransactionId, but we keep the
1906  * test anyway.
1907  */
1908  if (frozenAlreadyWrapped)
1909  {
1910  ereport(WARNING,
1911  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1912  errdetail("You might have already suffered transaction-wraparound data loss.")));
1913  LWLockRelease(WrapLimitsVacuumLock);
1914  return;
1915  }
1916 
1917  /* chicken out if data is bogus in any other way */
1918  if (bogus)
1919  {
1920  LWLockRelease(WrapLimitsVacuumLock);
1921  return;
1922  }
1923 
1924  /*
1925  * Advance the oldest value for commit timestamps before truncating, so
1926  * that if a user requests a timestamp for a transaction we're truncating
1927  * away right after this point, they get NULL instead of an ugly "file not
1928  * found" error from slru.c. This doesn't matter for xact/multixact
1929  * because they are not subject to arbitrary lookups from users.
1930  */
1931  AdvanceOldestCommitTsXid(frozenXID);
1932 
1933  /*
1934  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1935  */
1936  TruncateCLOG(frozenXID, oldestxid_datoid);
1937  TruncateCommitTs(frozenXID);
1938  TruncateMultiXact(minMulti, minmulti_datoid);
1939 
1940  /*
1941  * Update the wrap limit for GetNewTransactionId and creation of new
1942  * MultiXactIds. Note: these functions will also signal the postmaster
1943  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1944  * signaling twice?
1945  */
1946  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1947  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1948 
1949  LWLockRelease(WrapLimitsVacuumLock);
1950 }
1951 
1952 
1953 /*
1954  * vacuum_rel() -- vacuum one heap relation
1955  *
1956  * relid identifies the relation to vacuum. If relation is supplied,
1957  * use the name therein for reporting any failure to open/lock the rel;
1958  * do not use it once we've successfully opened the rel, since it might
1959  * be stale.
1960  *
1961  * Returns true if it's okay to proceed with a requested ANALYZE
1962  * operation on this table.
1963  *
1964  * Doing one heap at a time incurs extra overhead, since we need to
1965  * check that the heap exists again just before we vacuum it. The
1966  * reason that we do this is so that vacuuming can be spread across
1967  * many small transactions. Otherwise, two-phase locking would require
1968  * us to lock the entire database during one pass of the vacuum cleaner.
1969  *
1970  * At entry and exit, we are not inside a transaction.
1971  */
1972 static bool
1973 vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
1974  BufferAccessStrategy bstrategy)
1975 {
1976  LOCKMODE lmode;
1977  Relation rel;
1978  LockRelId lockrelid;
1979  Oid priv_relid;
1980  Oid toast_relid;
1981  Oid save_userid;
1982  int save_sec_context;
1983  int save_nestlevel;
1984 
1985  Assert(params != NULL);
1986 
1987  /* Begin a transaction for vacuuming this relation */
1989 
1990  if (!(params->options & VACOPT_FULL))
1991  {
1992  /*
1993  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1994  * other concurrent VACUUMs know that they can ignore this one while
1995  * determining their OldestXmin. (The reason we don't set it during a
1996  * full VACUUM is exactly that we may have to run user-defined
1997  * functions for functional indexes, and we want to make sure that if
1998  * they use the snapshot set above, any tuples it requires can't get
1999  * removed from other tables. An index function that depends on the
2000  * contents of other tables is arguably broken, but we won't break it
2001  * here by violating transaction semantics.)
2002  *
2003  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
2004  * autovacuum; it's used to avoid canceling a vacuum that was invoked
2005  * in an emergency.
2006  *
2007  * Note: these flags remain set until CommitTransaction or
2008  * AbortTransaction. We don't want to clear them until we reset
2009  * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
2010  * might appear to go backwards, which is probably Not Good. (We also
2011  * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
2012  * xmin doesn't become visible ahead of setting the flag.)
2013  */
2014  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2016  if (params->is_wraparound)
2019  LWLockRelease(ProcArrayLock);
2020  }
2021 
2022  /*
2023  * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
2024  * cutoff xids in local memory wrapping around, and to have updated xmin
2025  * horizons.
2026  */
2028 
2029  /*
2030  * Check for user-requested abort. Note we want this to be inside a
2031  * transaction, so xact.c doesn't issue useless WARNING.
2032  */
2034 
2035  /*
2036  * Determine the type of lock we want --- hard exclusive lock for a FULL
2037  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
2038  * way, we can be sure that no other backend is vacuuming the same table.
2039  */
2040  lmode = (params->options & VACOPT_FULL) ?
2042 
2043  /* open the relation and get the appropriate lock on it */
2044  rel = vacuum_open_relation(relid, relation, params->options,
2045  params->log_min_duration >= 0, lmode);
2046 
2047  /* leave if relation could not be opened or locked */
2048  if (!rel)
2049  {
2052  return false;
2053  }
2054 
2055  /*
2056  * When recursing to a TOAST table, check privileges on the parent. NB:
2057  * This is only safe to do because we hold a session lock on the main
2058  * relation that prevents concurrent deletion.
2059  */
2060  if (OidIsValid(params->toast_parent))
2061  priv_relid = params->toast_parent;
2062  else
2063  priv_relid = RelationGetRelid(rel);
2064 
2065  /*
2066  * Check if relation needs to be skipped based on privileges. This check
2067  * happens also when building the relation list to vacuum for a manual
2068  * operation, and needs to be done additionally here as VACUUM could
2069  * happen across multiple transactions where privileges could have changed
2070  * in-between. Make sure to only generate logs for VACUUM in this case.
2071  */
2072  if (!vacuum_is_permitted_for_relation(priv_relid,
2073  rel->rd_rel,
2074  params->options & ~VACOPT_ANALYZE))
2075  {
2076  relation_close(rel, lmode);
2079  return false;
2080  }
2081 
2082  /*
2083  * Check that it's of a vacuumable relkind.
2084  */
2085  if (rel->rd_rel->relkind != RELKIND_RELATION &&
2086  rel->rd_rel->relkind != RELKIND_MATVIEW &&
2087  rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
2088  rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2089  {
2090  ereport(WARNING,
2091  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
2092  RelationGetRelationName(rel))));
2093  relation_close(rel, lmode);
2096  return false;
2097  }
2098 
2099  /*
2100  * Silently ignore tables that are temp tables of other backends ---
2101  * trying to vacuum these will lead to great unhappiness, since their
2102  * contents are probably not up-to-date on disk. (We don't throw a
2103  * warning here; it would just lead to chatter during a database-wide
2104  * VACUUM.)
2105  */
2106  if (RELATION_IS_OTHER_TEMP(rel))
2107  {
2108  relation_close(rel, lmode);
2111  return false;
2112  }
2113 
2114  /*
2115  * Silently ignore partitioned tables as there is no work to be done. The
2116  * useful work is on their child partitions, which have been queued up for
2117  * us separately.
2118  */
2119  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2120  {
2121  relation_close(rel, lmode);
2124  /* It's OK to proceed with ANALYZE on this table */
2125  return true;
2126  }
2127 
2128  /*
2129  * Get a session-level lock too. This will protect our access to the
2130  * relation across multiple transactions, so that we can vacuum the
2131  * relation's TOAST table (if any) secure in the knowledge that no one is
2132  * deleting the parent relation.
2133  *
2134  * NOTE: this cannot block, even if someone else is waiting for access,
2135  * because the lock manager knows that both lock requests are from the
2136  * same process.
2137  */
2138  lockrelid = rel->rd_lockInfo.lockRelId;
2139  LockRelationIdForSession(&lockrelid, lmode);
2140 
2141  /*
2142  * Set index_cleanup option based on index_cleanup reloption if it wasn't
2143  * specified in VACUUM command, or when running in an autovacuum worker
2144  */
2145  if (params->index_cleanup == VACOPTVALUE_UNSPECIFIED)
2146  {
2147  StdRdOptIndexCleanup vacuum_index_cleanup;
2148 
2149  if (rel->rd_options == NULL)
2150  vacuum_index_cleanup = STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO;
2151  else
2152  vacuum_index_cleanup =
2153  ((StdRdOptions *) rel->rd_options)->vacuum_index_cleanup;
2154 
2155  if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO)
2156  params->index_cleanup = VACOPTVALUE_AUTO;
2157  else if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON)
2159  else
2160  {
2161  Assert(vacuum_index_cleanup ==
2164  }
2165  }
2166 
2167  /*
2168  * Set truncate option based on truncate reloption if it wasn't specified
2169  * in VACUUM command, or when running in an autovacuum worker
2170  */
2171  if (params->truncate == VACOPTVALUE_UNSPECIFIED)
2172  {
2173  if (rel->rd_options == NULL ||
2174  ((StdRdOptions *) rel->rd_options)->vacuum_truncate)
2175  params->truncate = VACOPTVALUE_ENABLED;
2176  else
2177  params->truncate = VACOPTVALUE_DISABLED;
2178  }
2179 
2180  /*
2181  * Remember the relation's TOAST relation for later, if the caller asked
2182  * us to process it. In VACUUM FULL, though, the toast table is
2183  * automatically rebuilt by cluster_rel so we shouldn't recurse to it,
2184  * unless PROCESS_MAIN is disabled.
2185  */
2186  if ((params->options & VACOPT_PROCESS_TOAST) != 0 &&
2187  ((params->options & VACOPT_FULL) == 0 ||
2188  (params->options & VACOPT_PROCESS_MAIN) == 0))
2189  toast_relid = rel->rd_rel->reltoastrelid;
2190  else
2191  toast_relid = InvalidOid;
2192 
2193  /*
2194  * Switch to the table owner's userid, so that any index functions are run
2195  * as that user. Also lock down security-restricted operations and
2196  * arrange to make GUC variable changes local to this command. (This is
2197  * unnecessary, but harmless, for lazy VACUUM.)
2198  */
2199  GetUserIdAndSecContext(&save_userid, &save_sec_context);
2200  SetUserIdAndSecContext(rel->rd_rel->relowner,
2201  save_sec_context | SECURITY_RESTRICTED_OPERATION);
2202  save_nestlevel = NewGUCNestLevel();
2204 
2205  /*
2206  * If PROCESS_MAIN is set (the default), it's time to vacuum the main
2207  * relation. Otherwise, we can skip this part. If processing the TOAST
2208  * table is required (e.g., PROCESS_TOAST is set), we force PROCESS_MAIN
2209  * to be set when we recurse to the TOAST table.
2210  */
2211  if (params->options & VACOPT_PROCESS_MAIN)
2212  {
2213  /*
2214  * Do the actual work --- either FULL or "lazy" vacuum
2215  */
2216  if (params->options & VACOPT_FULL)
2217  {
2218  ClusterParams cluster_params = {0};
2219 
2220  /* close relation before vacuuming, but hold lock until commit */
2221  relation_close(rel, NoLock);
2222  rel = NULL;
2223 
2224  if ((params->options & VACOPT_VERBOSE) != 0)
2225  cluster_params.options |= CLUOPT_VERBOSE;
2226 
2227  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
2228  cluster_rel(relid, InvalidOid, &cluster_params);
2229  }
2230  else
2231  table_relation_vacuum(rel, params, bstrategy);
2232  }
2233 
2234  /* Roll back any GUC changes executed by index functions */
2235  AtEOXact_GUC(false, save_nestlevel);
2236 
2237  /* Restore userid and security context */
2238  SetUserIdAndSecContext(save_userid, save_sec_context);
2239 
2240  /* all done with this class, but hold lock until commit */
2241  if (rel)
2242  relation_close(rel, NoLock);
2243 
2244  /*
2245  * Complete the transaction and free all temporary memory used.
2246  */
2249 
2250  /*
2251  * If the relation has a secondary toast rel, vacuum that too while we
2252  * still hold the session lock on the main table. Note however that
2253  * "analyze" will not get done on the toast table. This is good, because
2254  * the toaster always uses hardcoded index access and statistics are
2255  * totally unimportant for toast relations.
2256  */
2257  if (toast_relid != InvalidOid)
2258  {
2259  VacuumParams toast_vacuum_params;
2260 
2261  /*
2262  * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise,
2263  * set toast_parent so that the privilege checks are done on the main
2264  * relation. NB: This is only safe to do because we hold a session
2265  * lock on the main relation that prevents concurrent deletion.
2266  */
2267  memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
2268  toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
2269  toast_vacuum_params.toast_parent = relid;
2270 
2271  vacuum_rel(toast_relid, NULL, &toast_vacuum_params, bstrategy);
2272  }
2273 
2274  /*
2275  * Now release the session-level lock on the main table.
2276  */
2277  UnlockRelationIdForSession(&lockrelid, lmode);
2278 
2279  /* Report that we really did it. */
2280  return true;
2281 }
2282 
2283 
2284 /*
2285  * Open all the vacuumable indexes of the given relation, obtaining the
2286  * specified kind of lock on each. Return an array of Relation pointers for
2287  * the indexes into *Irel, and the number of indexes into *nindexes.
2288  *
2289  * We consider an index vacuumable if it is marked insertable (indisready).
2290  * If it isn't, probably a CREATE INDEX CONCURRENTLY command failed early in
2291  * execution, and what we have is too corrupt to be processable. We will
2292  * vacuum even if the index isn't indisvalid; this is important because in a
2293  * unique index, uniqueness checks will be performed anyway and had better not
2294  * hit dangling index pointers.
2295  */
2296 void
2298  int *nindexes, Relation **Irel)
2299 {
2300  List *indexoidlist;
2301  ListCell *indexoidscan;
2302  int i;
2303 
2304  Assert(lockmode != NoLock);
2305 
2306  indexoidlist = RelationGetIndexList(relation);
2307 
2308  /* allocate enough memory for all indexes */
2309  i = list_length(indexoidlist);
2310 
2311  if (i > 0)
2312  *Irel = (Relation *) palloc(i * sizeof(Relation));
2313  else
2314  *Irel = NULL;
2315 
2316  /* collect just the ready indexes */
2317  i = 0;
2318  foreach(indexoidscan, indexoidlist)
2319  {
2320  Oid indexoid = lfirst_oid(indexoidscan);
2321  Relation indrel;
2322 
2323  indrel = index_open(indexoid, lockmode);
2324  if (indrel->rd_index->indisready)
2325  (*Irel)[i++] = indrel;
2326  else
2327  index_close(indrel, lockmode);
2328  }
2329 
2330  *nindexes = i;
2331 
2332  list_free(indexoidlist);
2333 }
2334 
2335 /*
2336  * Release the resources acquired by vac_open_indexes. Optionally release
2337  * the locks (say NoLock to keep 'em).
2338  */
2339 void
2340 vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
2341 {
2342  if (Irel == NULL)
2343  return;
2344 
2345  while (nindexes--)
2346  {
2347  Relation ind = Irel[nindexes];
2348 
2349  index_close(ind, lockmode);
2350  }
2351  pfree(Irel);
2352 }
2353 
2354 /*
2355  * vacuum_delay_point --- check for interrupts and cost-based delay.
2356  *
2357  * This should be called in each major loop of VACUUM processing,
2358  * typically once per page processed.
2359  */
2360 void
2362 {
2363  double msec = 0;
2364 
2365  /* Always check for interrupts */
2367 
2368  if (InterruptPending ||
2370  return;
2371 
2372  /*
2373  * Autovacuum workers should reload the configuration file if requested.
2374  * This allows changes to [autovacuum_]vacuum_cost_limit and
2375  * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2376  * vacuumed or analyzed.
2377  */
2379  {
2380  ConfigReloadPending = false;
2383  }
2384 
2385  /*
2386  * If we disabled cost-based delays after reloading the config file,
2387  * return.
2388  */
2389  if (!VacuumCostActive)
2390  return;
2391 
2392  /*
2393  * For parallel vacuum, the delay is computed based on the shared cost
2394  * balance. See compute_parallel_delay.
2395  */
2396  if (VacuumSharedCostBalance != NULL)
2397  msec = compute_parallel_delay();
2400 
2401  /* Nap if appropriate */
2402  if (msec > 0)
2403  {
2404  if (msec > vacuum_cost_delay * 4)
2405  msec = vacuum_cost_delay * 4;
2406 
2407  pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);
2408  pg_usleep(msec * 1000);
2410 
2411  /*
2412  * We don't want to ignore postmaster death during very long vacuums
2413  * with vacuum_cost_delay configured. We can't use the usual
2414  * WaitLatch() approach here because we want microsecond-based sleep
2415  * durations above.
2416  */
2418  exit(1);
2419 
2420  VacuumCostBalance = 0;
2421 
2422  /*
2423  * Balance and update limit values for autovacuum workers. We must do
2424  * this periodically, as the number of workers across which we are
2425  * balancing the limit may have changed.
2426  *
2427  * TODO: There may be better criteria for determining when to do this
2428  * besides "check after napping".
2429  */
2431 
2432  /* Might have gotten an interrupt while sleeping */
2434  }
2435 }
2436 
2437 /*
2438  * Computes the vacuum delay for parallel workers.
2439  *
2440  * The basic idea of a cost-based delay for parallel vacuum is to allow each
2441  * worker to sleep in proportion to the share of work it's done. We achieve this
2442  * by allowing all parallel vacuum workers including the leader process to
2443  * have a shared view of cost related parameters (mainly VacuumCostBalance).
2444  * We allow each worker to update it as and when it has incurred any cost and
2445  * then based on that decide whether it needs to sleep. We compute the time
2446  * to sleep for a worker based on the cost it has incurred
2447  * (VacuumCostBalanceLocal) and then reduce the VacuumSharedCostBalance by
2448  * that amount. This avoids putting to sleep those workers which have done less
2449  * I/O than other workers and therefore ensure that workers
2450  * which are doing more I/O got throttled more.
2451  *
2452  * We allow a worker to sleep only if it has performed I/O above a certain
2453  * threshold, which is calculated based on the number of active workers
2454  * (VacuumActiveNWorkers), and the overall cost balance is more than
2455  * VacuumCostLimit set by the system. Testing reveals that we achieve
2456  * the required throttling if we force a worker that has done more than 50%
2457  * of its share of work to sleep.
2458  */
2459 static double
2461 {
2462  double msec = 0;
2463  uint32 shared_balance;
2464  int nworkers;
2465 
2466  /* Parallel vacuum must be active */
2468 
2470 
2471  /* At least count itself */
2472  Assert(nworkers >= 1);
2473 
2474  /* Update the shared cost balance value atomically */
2476 
2477  /* Compute the total local balance for the current worker */
2479 
2480  if ((shared_balance >= vacuum_cost_limit) &&
2481  (VacuumCostBalanceLocal > 0.5 * ((double) vacuum_cost_limit / nworkers)))
2482  {
2483  /* Compute sleep time based on the local cost balance */
2487  }
2488 
2489  /*
2490  * Reset the local balance as we accumulated it into the shared value.
2491  */
2492  VacuumCostBalance = 0;
2493 
2494  return msec;
2495 }
2496 
2497 /*
2498  * A wrapper function of defGetBoolean().
2499  *
2500  * This function returns VACOPTVALUE_ENABLED and VACOPTVALUE_DISABLED instead
2501  * of true and false.
2502  */
2503 static VacOptValue
2505 {
2507 }
2508 
2509 /*
2510  * vac_bulkdel_one_index() -- bulk-deletion for index relation.
2511  *
2512  * Returns bulk delete stats derived from input stats
2513  */
2516  TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
2517 {
2518  /* Do bulk deletion */
2519  istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2520  (void *) dead_items);
2521 
2522  ereport(ivinfo->message_level,
2523  (errmsg("scanned index \"%s\" to remove %lld row versions",
2524  RelationGetRelationName(ivinfo->index),
2525  (long long) dead_items_info->num_items)));
2526 
2527  return istat;
2528 }
2529 
2530 /*
2531  * vac_cleanup_one_index() -- do post-vacuum cleanup for index relation.
2532  *
2533  * Returns bulk delete stats derived from input stats
2534  */
2537 {
2538  istat = index_vacuum_cleanup(ivinfo, istat);
2539 
2540  if (istat)
2541  ereport(ivinfo->message_level,
2542  (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2543  RelationGetRelationName(ivinfo->index),
2544  istat->num_index_tuples,
2545  istat->num_pages),
2546  errdetail("%.0f index row versions were removed.\n"
2547  "%u index pages were newly deleted.\n"
2548  "%u index pages are currently deleted, of which %u are currently reusable.",
2549  istat->tuples_removed,
2550  istat->pages_newly_deleted,
2551  istat->pages_deleted, istat->pages_free)));
2552 
2553  return istat;
2554 }
2555 
2556 /*
2557  * vac_tid_reaped() -- is a particular tid deletable?
2558  *
2559  * This has the right signature to be an IndexBulkDeleteCallback.
2560  */
2561 static bool
2563 {
2564  TidStore *dead_items = (TidStore *) state;
2565 
2566  return TidStoreIsMember(dead_items, itemptr);
2567 }
@ ACLCHECK_OK
Definition: acl.h:183
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4145
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4094
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
void VacuumUpdateCosts(void)
Definition: autovacuum.c:1635
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:128
int autovacuum_freeze_max_age
Definition: autovacuum.c:127
void AutoVacuumUpdateCostLimit(void)
Definition: autovacuum.c:1704
#define MAX_PARALLEL_WORKER_LIMIT
uint32 BlockNumber
Definition: block.h:31
@ BAS_VACUUM
Definition: bufmgr.h:39
#define NameStr(name)
Definition: c.h:737
unsigned int uint32
Definition: c.h:506
#define Min(x, y)
Definition: c.h:995
signed int int32
Definition: c.h:496
#define Max(x, y)
Definition: c.h:989
#define Assert(condition)
Definition: c.h:849
TransactionId MultiXactId
Definition: c.h:653
uint32 bits32
Definition: c.h:514
float float4
Definition: c.h:620
uint32 TransactionId
Definition: c.h:643
#define OidIsValid(objectId)
Definition: c.h:766
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition: clog.c:1000
void cluster_rel(Oid tableOid, Oid indexOid, ClusterParams *params)
Definition: cluster.c:310
#define CLUOPT_VERBOSE
Definition: cluster.h:23
void analyze_rel(Oid relid, RangeVar *relation, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:109
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
int32 defGetInt32(DefElem *def)
Definition: define.c:162
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
struct cursor * cur
Definition: ecpg.c:28
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errdetail(const char *fmt,...)
Definition: elog.c:1203
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 LOG
Definition: elog.h:31
#define PG_TRY(...)
Definition: elog.h:371
#define WARNING
Definition: elog.h:36
#define DEBUG2
Definition: elog.h:29
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_FINALLY(...)
Definition: elog.h:388
#define ereport(elevel,...)
Definition: elog.h:149
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584
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
volatile sig_atomic_t InterruptPending
Definition: globals.c:31
bool VacuumCostActive
Definition: globals.c:157
bool IsUnderPostmaster
Definition: globals.c:119
int VacuumCostBalance
Definition: globals.c:156
int VacuumBufferUsageLimit
Definition: globals.c:148
Oid MyDatabaseId
Definition: globals.c:93
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:2871
int NewGUCNestLevel(void)
Definition: guc.c:2235
#define newval
void RestrictSearchPath(void)
Definition: guc.c:2246
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2262
#define GUC_check_errdetail
Definition: guc.h:476
GucSource
Definition: guc.h:108
@ PGC_SIGHUP
Definition: guc.h:71
#define GUC_UNIT_KB
Definition: guc.h:228
void ProcessConfigFile(GucContext context)
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1243
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
int verbose
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *istat)
Definition: indexam.c:771
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition: indexam.c:750
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
int i
Definition: isn.c:72
exit(1)
List * lappend(List *list, void *datum)
Definition: list.c:339
void list_free(List *list)
Definition: list.c:1546
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:150
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:226
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:386
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:399
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition: lmgr.c:486
int LOCKMODE
Definition: lockdefs.h:26
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define AccessShareLock
Definition: lockdefs.h:36
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define ExclusiveLock
Definition: lockdefs.h:42
#define RowExclusiveLock
Definition: lockdefs.h:38
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:834
void pfree(void *pointer)
Definition: mcxt.c:1521
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define AmAutoVacuumWorkerProcess()
Definition: miscadmin.h:372
#define MIN_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:277
#define MAX_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:278
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:312
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:635
Oid GetUserId(void)
Definition: miscinit.c:514
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:642
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3317
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3331
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2362
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2660
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2978
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:771
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:3102
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define FirstMultiXactId
Definition: multixact.h:25
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
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
#define ACL_MAINTAIN
Definition: parsenodes.h:90
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
NameData relname
Definition: pg_class.h:38
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
FormData_pg_class
Definition: pg_class.h:142
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
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:255
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define lfirst_oid(lc)
Definition: pg_list.h:174
static rewind_source * source
Definition: pg_rewind.c:89
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
#define PostmasterIsAlive()
Definition: pmsignal.h:105
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define PROC_IN_VACUUM
Definition: proc.h:58
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:60
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:2005
MemoryContextSwitchTo(old_ctx)
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:3016
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
#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
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4767
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
@ ForwardScanDirection
Definition: sdir.h:28
void pg_usleep(long microsec)
Definition: signal.c:53
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
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
#define BTEqualStrategyNumber
Definition: stratnum.h:31
bits32 options
Definition: cluster.h:30
char * defname
Definition: parsenodes.h:817
ParseLoc location
Definition: parsenodes.h:821
Node * arg
Definition: parsenodes.h:818
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
Relation index
Definition: genam.h:46
int message_level
Definition: genam.h:51
Definition: pg_list.h:54
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
char * relname
Definition: primnodes.h:82
bool inh
Definition: primnodes.h:85
LockInfoData rd_lockInfo
Definition: rel.h:114
TriggerDesc * trigdesc
Definition: rel.h:117
Form_pg_index rd_index
Definition: rel.h:192
RuleLock * rd_rules
Definition: rel.h:115
bytea * rd_options
Definition: rel.h:175
Form_pg_class rd_rel
Definition: rel.h:111
int64 num_items
Definition: vacuum.h:288
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
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
RangeVar * relation
Definition: parsenodes.h:3883
List * options
Definition: parsenodes.h:3868
bool is_vacuumcmd
Definition: parsenodes.h:3870
List * rels
Definition: parsenodes.h:3869
Definition: regguts.h:323
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
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
static void table_relation_vacuum(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: tableam.h:1716
bool TidStoreIsMember(TidStore *ts, ItemPointer tid)
Definition: tidstore.c:429
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:315
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
static bool vac_tid_reaped(ItemPointer itemptr, void *state)
Definition: vacuum.c:2562
void ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
Definition: vacuum.c:146
IndexBulkDeleteResult * vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
Definition: vacuum.c:2515
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:102
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1804
int vacuum_freeze_min_age
Definition: vacuum.c:66
static List * expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context, int options)
Definition: vacuum.c:866
double vacuum_cost_delay
Definition: vacuum.c:79
static double compute_parallel_delay(void)
Definition: vacuum.c:2460
static VacOptValue get_vacoptval_from_boolean(DefElem *def)
Definition: vacuum.c:2504
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:2297
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition: vacuum.c:477
bool check_vacuum_buffer_usage_limit(int *newval, void **extra, GucSource source)
Definition: vacuum.c:124
int VacuumCostBalanceLocal
Definition: vacuum.c:103
static List * get_all_vacuum_rels(MemoryContext vac_context, int options)
Definition: vacuum.c:1021
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool *frozenxid_updated, bool *minmulti_updated, bool in_outer_xact)
Definition: vacuum.c:1409
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:69
int vacuum_freeze_table_age
Definition: vacuum.c:67
int vacuum_multixact_failsafe_age
Definition: vacuum.c:71
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:68
Relation vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:754
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuum.c:1973
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:2340
void vacuum_delay_point(void)
Definition: vacuum.c:2361
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1585
bool vacuum_get_cutoffs(Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
Definition: vacuum.c:1083
bool vacuum_xid_failsafe_check(const struct VacuumCutoffs *cutoffs)
Definition: vacuum.c:1251
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:101
bool VacuumFailsafeActive
Definition: vacuum.c:95
int vacuum_cost_limit
Definition: vacuum.c:80
int vacuum_failsafe_age
Definition: vacuum.c:70
double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:1313
IndexBulkDeleteResult * vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
Definition: vacuum.c:2536
bool vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple, bits32 options)
Definition: vacuum.c:702
#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
Definition: vacuum.h:201
@ VACOPTVALUE_AUTO
Definition: vacuum.h:203
@ VACOPTVALUE_ENABLED
Definition: vacuum.h:205
@ VACOPTVALUE_UNSPECIFIED
Definition: vacuum.h:202
@ VACOPTVALUE_DISABLED
Definition: vacuum.h:204
#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
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:372
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:517
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
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3771
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