PostgreSQL Source Code  git master
vacuum.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_namespace.h"
#include "commands/cluster.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for vacuum.c:

Go to the source code of this file.

Functions

static Listexpand_vacuum_rel (VacuumRelation *vrel)
 
static Listget_all_vacuum_rels (void)
 
static void vac_truncate_clog (TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
 
static bool vacuum_rel (Oid relid, RangeVar *relation, int options, VacuumParams *params)
 
void ExecVacuum (VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (int options, List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
 
void vacuum_set_xid_limits (Relation rel, int freeze_min_age, int freeze_table_age, int multixact_freeze_min_age, int multixact_freeze_table_age, TransactionId *oldestXmin, TransactionId *freezeLimit, TransactionId *xidFullScanLimit, MultiXactId *multiXactCutoff, MultiXactId *mxactFullScanLimit)
 
double vac_estimate_reltuples (Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
 
void vac_update_relstats (Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool in_outer_xact)
 
void vac_update_datfrozenxid (void)
 
void vac_open_indexes (Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
 
void vac_close_indexes (int nindexes, Relation *Irel, LOCKMODE lockmode)
 
void vacuum_delay_point (void)
 

Variables

int vacuum_freeze_min_age
 
int vacuum_freeze_table_age
 
int vacuum_multixact_freeze_min_age
 
int vacuum_multixact_freeze_table_age
 
static MemoryContext vac_context = NULL
 
static BufferAccessStrategy vac_strategy
 

Function Documentation

◆ ExecVacuum()

void ExecVacuum ( VacuumStmt vacstmt,
bool  isTopLevel 
)

Definition at line 87 of file vacuum.c.

References Assert, ereport, errcode(), errmsg(), ERROR, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, VacuumParams::is_wraparound, lfirst_node, VacuumParams::log_min_duration, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, VacuumStmt::options, VacuumStmt::rels, VacuumRelation::va_cols, VACOPT_ANALYZE, VACOPT_FREEZE, VACOPT_FULL, VACOPT_SKIPTOAST, VACOPT_VACUUM, and vacuum().

Referenced by standard_ProcessUtility().

88 {
89  VacuumParams params;
90 
91  /* sanity checks on options */
93  Assert((vacstmt->options & VACOPT_VACUUM) ||
94  !(vacstmt->options & (VACOPT_FULL | VACOPT_FREEZE)));
95  Assert(!(vacstmt->options & VACOPT_SKIPTOAST));
96 
97  /*
98  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
99  */
100  if (!(vacstmt->options & VACOPT_ANALYZE))
101  {
102  ListCell *lc;
103 
104  foreach(lc, vacstmt->rels)
105  {
107 
108  if (vrel->va_cols != NIL)
109  ereport(ERROR,
110  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
111  errmsg("ANALYZE option must be specified when a column list is provided")));
112  }
113  }
114 
115  /*
116  * All freeze ages are zero if the FREEZE option is given; otherwise pass
117  * them as -1 which means to use the default values.
118  */
119  if (vacstmt->options & VACOPT_FREEZE)
120  {
121  params.freeze_min_age = 0;
122  params.freeze_table_age = 0;
123  params.multixact_freeze_min_age = 0;
124  params.multixact_freeze_table_age = 0;
125  }
126  else
127  {
128  params.freeze_min_age = -1;
129  params.freeze_table_age = -1;
130  params.multixact_freeze_min_age = -1;
131  params.multixact_freeze_table_age = -1;
132  }
133 
134  /* user-invoked vacuum is never "for wraparound" */
135  params.is_wraparound = false;
136 
137  /* user-invoked vacuum never uses this parameter */
138  params.log_min_duration = -1;
139 
140  /* Now go through the common routine */
141  vacuum(vacstmt->options, vacstmt->rels, &params, NULL, isTopLevel);
142 }
#define NIL
Definition: pg_list.h:69
int multixact_freeze_table_age
Definition: vacuum.h:142
int errcode(int sqlerrcode)
Definition: elog.c:575
int freeze_table_age
Definition: vacuum.h:139
void vacuum(int options, List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:166
#define ERROR
Definition: elog.h:43
int freeze_min_age
Definition: vacuum.h:138
bool is_wraparound
Definition: vacuum.h:144
#define lfirst_node(type, lc)
Definition: pg_list.h:109
#define ereport(elevel, rest)
Definition: elog.h:122
#define Assert(condition)
Definition: c.h:699
int log_min_duration
Definition: vacuum.h:145
int errmsg(const char *fmt,...)
Definition: elog.c:797
int multixact_freeze_min_age
Definition: vacuum.h:140
List * rels
Definition: parsenodes.h:3162

◆ expand_vacuum_rel()

static List * expand_vacuum_rel ( VacuumRelation vrel)
static

Definition at line 426 of file vacuum.c.

References AccessShareLock, elog, ERROR, find_all_inheritors(), GETSTRUCT, HeapTupleIsValid, lappend(), lfirst_oid, makeVacuumRelation(), MemoryContextSwitchTo(), NIL, NoLock, ObjectIdGetDatum, VacuumRelation::oid, OidIsValid, RangeVarGetRelid, VacuumRelation::relation, ReleaseSysCache(), RELOID, SearchSysCache1(), UnlockRelationOid(), and VacuumRelation::va_cols.

Referenced by vacuum().

427 {
428  List *vacrels = NIL;
429  MemoryContext oldcontext;
430 
431  /* If caller supplied OID, there's nothing we need do here. */
432  if (OidIsValid(vrel->oid))
433  {
434  oldcontext = MemoryContextSwitchTo(vac_context);
435  vacrels = lappend(vacrels, vrel);
436  MemoryContextSwitchTo(oldcontext);
437  }
438  else
439  {
440  /* Process a specific relation, and possibly partitions thereof */
441  Oid relid;
442  HeapTuple tuple;
443  Form_pg_class classForm;
444  bool include_parts;
445 
446  /*
447  * We transiently take AccessShareLock to protect the syscache lookup
448  * below, as well as find_all_inheritors's expectation that the caller
449  * holds some lock on the starting relation.
450  */
451  relid = RangeVarGetRelid(vrel->relation, AccessShareLock, false);
452 
453  /*
454  * Make a returnable VacuumRelation for this rel.
455  */
456  oldcontext = MemoryContextSwitchTo(vac_context);
457  vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
458  relid,
459  vrel->va_cols));
460  MemoryContextSwitchTo(oldcontext);
461 
462  /*
463  * To check whether the relation is a partitioned table, fetch its
464  * syscache entry.
465  */
466  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
467  if (!HeapTupleIsValid(tuple))
468  elog(ERROR, "cache lookup failed for relation %u", relid);
469  classForm = (Form_pg_class) GETSTRUCT(tuple);
470  include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
471  ReleaseSysCache(tuple);
472 
473  /*
474  * If it is, make relation list entries for its partitions. Note that
475  * the list returned by find_all_inheritors() includes the passed-in
476  * OID, so we have to skip that. There's no point in taking locks on
477  * the individual partitions yet, and doing so would just add
478  * unnecessary deadlock risk.
479  */
480  if (include_parts)
481  {
482  List *part_oids = find_all_inheritors(relid, NoLock, NULL);
483  ListCell *part_lc;
484 
485  foreach(part_lc, part_oids)
486  {
487  Oid part_oid = lfirst_oid(part_lc);
488 
489  if (part_oid == relid)
490  continue; /* ignore original table */
491 
492  /*
493  * We omit a RangeVar since it wouldn't be appropriate to
494  * complain about failure to open one of these relations
495  * later.
496  */
497  oldcontext = MemoryContextSwitchTo(vac_context);
498  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
499  part_oid,
500  vrel->va_cols));
501  MemoryContextSwitchTo(oldcontext);
502  }
503  }
504 
505  /*
506  * Release lock again. This means that by the time we actually try to
507  * process the table, it might be gone or renamed. In the former case
508  * we'll silently ignore it; in the latter case we'll process it
509  * anyway, but we must beware that the RangeVar doesn't necessarily
510  * identify it anymore. This isn't ideal, perhaps, but there's little
511  * practical alternative, since we're typically going to commit this
512  * transaction and begin a new one between now and then. Moreover,
513  * holding locks on multiple relations would create significant risk
514  * of deadlock.
515  */
517  }
518 
519  return vacrels;
520 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
RangeVar * relation
Definition: parsenodes.h:3153
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:622
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:63
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
static MemoryContext vac_context
Definition: vacuum.c:66
List * lappend(List *list, void *datum)
Definition: list.c:128
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:166
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ get_all_vacuum_rels()

static List * get_all_vacuum_rels ( void  )
static

Definition at line 527 of file vacuum.c.

References AccessShareLock, ForwardScanDirection, GETSTRUCT, heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleGetOid, lappend(), makeVacuumRelation(), MemoryContextSwitchTo(), and NIL.

Referenced by vacuum().

528 {
529  List *vacrels = NIL;
530  Relation pgclass;
531  HeapScanDesc scan;
532  HeapTuple tuple;
533 
534  pgclass = heap_open(RelationRelationId, AccessShareLock);
535 
536  scan = heap_beginscan_catalog(pgclass, 0, NULL);
537 
538  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
539  {
540  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
541  MemoryContext oldcontext;
542 
543  /*
544  * We include partitioned tables here; depending on which operation is
545  * to be performed, caller will decide whether to process or ignore
546  * them.
547  */
548  if (classForm->relkind != RELKIND_RELATION &&
549  classForm->relkind != RELKIND_MATVIEW &&
550  classForm->relkind != RELKIND_PARTITIONED_TABLE)
551  continue;
552 
553  /*
554  * Build VacuumRelation(s) specifying the table OIDs to be processed.
555  * We omit a RangeVar since it wouldn't be appropriate to complain
556  * about failure to open one of these relations later.
557  */
558  oldcontext = MemoryContextSwitchTo(vac_context);
559  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
560  HeapTupleGetOid(tuple),
561  NIL));
562  MemoryContextSwitchTo(oldcontext);
563  }
564 
565  heap_endscan(scan);
566  heap_close(pgclass, AccessShareLock);
567 
568  return vacrels;
569 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1572
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:622
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
static MemoryContext vac_context
Definition: vacuum.c:66
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1412
List * lappend(List *list, void *datum)
Definition: list.c:128
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1835
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707
Definition: pg_list.h:45

◆ vac_close_indexes()

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

Definition at line 1651 of file vacuum.c.

References index_close(), and pfree().

Referenced by do_analyze_rel(), and lazy_vacuum_rel().

1652 {
1653  if (Irel == NULL)
1654  return;
1655 
1656  while (nindexes--)
1657  {
1658  Relation ind = Irel[nindexes];
1659 
1660  index_close(ind, lockmode);
1661  }
1662  pfree(Irel);
1663 }
void pfree(void *pointer)
Definition: mcxt.c:1031
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176

◆ vac_estimate_reltuples()

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

Definition at line 779 of file vacuum.c.

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

783 {
784  BlockNumber old_rel_pages = relation->rd_rel->relpages;
785  double old_rel_tuples = relation->rd_rel->reltuples;
786  double old_density;
787  double unscanned_pages;
788  double total_tuples;
789 
790  /* If we did scan the whole table, just use the count as-is */
791  if (scanned_pages >= total_pages)
792  return scanned_tuples;
793 
794  /*
795  * If scanned_pages is zero but total_pages isn't, keep the existing value
796  * of reltuples. (Note: callers should avoid updating the pg_class
797  * statistics in this situation, since no new information has been
798  * provided.)
799  */
800  if (scanned_pages == 0)
801  return old_rel_tuples;
802 
803  /*
804  * If old value of relpages is zero, old density is indeterminate; we
805  * can't do much except scale up scanned_tuples to match total_pages.
806  */
807  if (old_rel_pages == 0)
808  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
809 
810  /*
811  * Okay, we've covered the corner cases. The normal calculation is to
812  * convert the old measurement to a density (tuples per page), then
813  * estimate the number of tuples in the unscanned pages using that figure,
814  * and finally add on the number of tuples in the scanned pages.
815  */
816  old_density = old_rel_tuples / old_rel_pages;
817  unscanned_pages = (double) total_pages - (double) scanned_pages;
818  total_tuples = old_density * unscanned_pages + scanned_tuples;
819  return floor(total_tuples + 0.5);
820 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:84

◆ vac_open_indexes()

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

Definition at line 1608 of file vacuum.c.

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

Referenced by do_analyze_rel(), and lazy_vacuum_rel().

1610 {
1611  List *indexoidlist;
1612  ListCell *indexoidscan;
1613  int i;
1614 
1615  Assert(lockmode != NoLock);
1616 
1617  indexoidlist = RelationGetIndexList(relation);
1618 
1619  /* allocate enough memory for all indexes */
1620  i = list_length(indexoidlist);
1621 
1622  if (i > 0)
1623  *Irel = (Relation *) palloc(i * sizeof(Relation));
1624  else
1625  *Irel = NULL;
1626 
1627  /* collect just the ready indexes */
1628  i = 0;
1629  foreach(indexoidscan, indexoidlist)
1630  {
1631  Oid indexoid = lfirst_oid(indexoidscan);
1632  Relation indrel;
1633 
1634  indrel = index_open(indexoid, lockmode);
1635  if (IndexIsReady(indrel->rd_index))
1636  (*Irel)[i++] = indrel;
1637  else
1638  index_close(indrel, lockmode);
1639  }
1640 
1641  *nindexes = i;
1642 
1643  list_free(indexoidlist);
1644 }
#define IndexIsReady(indexForm)
Definition: pg_index.h:86
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:131
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4260
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
void * palloc(Size size)
Definition: mcxt.c:924
void list_free(List *list)
Definition: list.c:1133
int i
Definition: pg_list.h:45
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:150
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ vac_truncate_clog()

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

Definition at line 1155 of file vacuum.c.

References AccessShareLock, AdvanceOldestCommitTsXid(), Assert, datfrozenxid, datminmxid, ereport, errdetail(), errmsg(), FormData_pg_database, ForwardScanDirection, GETSTRUCT, heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleGetOid, MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ReadNewTransactionId(), SetMultiXactIdLimit(), SetTransactionIdLimit(), TransactionIdIsNormal, TransactionIdPrecedes(), TruncateCLOG(), TruncateCommitTs(), TruncateMultiXact(), and WARNING.

Referenced by vac_update_datfrozenxid().

1159 {
1160  TransactionId nextXID = ReadNewTransactionId();
1161  Relation relation;
1162  HeapScanDesc scan;
1163  HeapTuple tuple;
1164  Oid oldestxid_datoid;
1165  Oid minmulti_datoid;
1166  bool bogus = false;
1167  bool frozenAlreadyWrapped = false;
1168 
1169  /* init oldest datoids to sync with my frozenXID/minMulti values */
1170  oldestxid_datoid = MyDatabaseId;
1171  minmulti_datoid = MyDatabaseId;
1172 
1173  /*
1174  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1175  *
1176  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1177  * the values could change while we look at them. Fetch each one just
1178  * once to ensure sane behavior of the comparison logic. (Here, as in
1179  * many other places, we assume that fetching or updating an XID in shared
1180  * storage is atomic.)
1181  *
1182  * Note: we need not worry about a race condition with new entries being
1183  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1184  * existing DB's datfrozenxid, and that source DB cannot be ours because
1185  * of the interlock against copying a DB containing an active backend.
1186  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1187  * concurrently modify the datfrozenxid's of different databases, the
1188  * worst possible outcome is that pg_xact is not truncated as aggressively
1189  * as it could be.
1190  */
1191  relation = heap_open(DatabaseRelationId, AccessShareLock);
1192 
1193  scan = heap_beginscan_catalog(relation, 0, NULL);
1194 
1195  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1196  {
1197  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1198  TransactionId datfrozenxid = dbform->datfrozenxid;
1199  TransactionId datminmxid = dbform->datminmxid;
1200 
1201  Assert(TransactionIdIsNormal(datfrozenxid));
1202  Assert(MultiXactIdIsValid(datminmxid));
1203 
1204  /*
1205  * If things are working properly, no database should have a
1206  * datfrozenxid or datminmxid that is "in the future". However, such
1207  * cases have been known to arise due to bugs in pg_upgrade. If we
1208  * see any entries that are "in the future", chicken out and don't do
1209  * anything. This ensures we won't truncate clog before those
1210  * databases have been scanned and cleaned up. (We will issue the
1211  * "already wrapped" warning if appropriate, though.)
1212  */
1213  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1214  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1215  bogus = true;
1216 
1217  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1218  frozenAlreadyWrapped = true;
1219  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1220  {
1221  frozenXID = datfrozenxid;
1222  oldestxid_datoid = HeapTupleGetOid(tuple);
1223  }
1224 
1225  if (MultiXactIdPrecedes(datminmxid, minMulti))
1226  {
1227  minMulti = datminmxid;
1228  minmulti_datoid = HeapTupleGetOid(tuple);
1229  }
1230  }
1231 
1232  heap_endscan(scan);
1233 
1234  heap_close(relation, AccessShareLock);
1235 
1236  /*
1237  * Do not truncate CLOG if we seem to have suffered wraparound already;
1238  * the computed minimum XID might be bogus. This case should now be
1239  * impossible due to the defenses in GetNewTransactionId, but we keep the
1240  * test anyway.
1241  */
1242  if (frozenAlreadyWrapped)
1243  {
1244  ereport(WARNING,
1245  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1246  errdetail("You might have already suffered transaction-wraparound data loss.")));
1247  return;
1248  }
1249 
1250  /* chicken out if data is bogus in any other way */
1251  if (bogus)
1252  return;
1253 
1254  /*
1255  * Advance the oldest value for commit timestamps before truncating, so
1256  * that if a user requests a timestamp for a transaction we're truncating
1257  * away right after this point, they get NULL instead of an ugly "file not
1258  * found" error from slru.c. This doesn't matter for xact/multixact
1259  * because they are not subject to arbitrary lookups from users.
1260  */
1261  AdvanceOldestCommitTsXid(frozenXID);
1262 
1263  /*
1264  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1265  */
1266  TruncateCLOG(frozenXID, oldestxid_datoid);
1267  TruncateCommitTs(frozenXID);
1268  TruncateMultiXact(minMulti, minmulti_datoid);
1269 
1270  /*
1271  * Update the wrap limit for GetNewTransactionId and creation of new
1272  * MultiXactIds. Note: these functions will also signal the postmaster
1273  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1274  * signalling twice?
1275  */
1276  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1277  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1278 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1572
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition: clog.c:906
uint32 TransactionId
Definition: c.h:474
FormData_pg_database * Form_pg_database
Definition: pg_database.h:54
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:876
int errdetail(const char *fmt,...)
Definition: elog.c:873
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1412
#define ereport(elevel, rest)
Definition: elog.h:122
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:823
#define WARNING
Definition: elog.h:40
Oid MyDatabaseId
Definition: globals.c:84
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1835
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2194
TransactionId datfrozenxid
Definition: pg_database.h:40
#define Assert(condition)
Definition: c.h:699
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:288
TransactionId datminmxid
Definition: pg_database.h:41
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3140
int errmsg(const char *fmt,...)
Definition: elog.c:797
FormData_pg_database
Definition: pg_database.h:47
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:707
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:2933

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 991 of file vacuum.c.

References AccessShareLock, Assert, DATABASEOID, elog, ERROR, ForceTransactionIdLimitUpdate(), GetOldestMultiXactId(), GetOldestXmin(), GETSTRUCT, heap_close, heap_freetuple(), heap_inplace_update(), heap_open(), HeapTupleIsValid, InvalidOid, MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ObjectIdGetDatum, PROCARRAY_FLAGS_VACUUM, ReadNewTransactionId(), ReadNextMultiXactId(), RowExclusiveLock, SearchSysCacheCopy1, systable_beginscan(), systable_endscan(), systable_getnext(), TransactionIdIsNormal, TransactionIdPrecedes(), and vac_truncate_clog().

Referenced by do_autovacuum(), and vacuum().

992 {
993  HeapTuple tuple;
994  Form_pg_database dbform;
995  Relation relation;
996  SysScanDesc scan;
997  HeapTuple classTup;
998  TransactionId newFrozenXid;
999  MultiXactId newMinMulti;
1000  TransactionId lastSaneFrozenXid;
1001  MultiXactId lastSaneMinMulti;
1002  bool bogus = false;
1003  bool dirty = false;
1004 
1005  /*
1006  * Initialize the "min" calculation with GetOldestXmin, which is a
1007  * reasonable approximation to the minimum relfrozenxid for not-yet-
1008  * committed pg_class entries for new tables; see AddNewRelationTuple().
1009  * So we cannot produce a wrong minimum by starting with this.
1010  */
1011  newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
1012 
1013  /*
1014  * Similarly, initialize the MultiXact "min" with the value that would be
1015  * used on pg_class for new tables. See AddNewRelationTuple().
1016  */
1017  newMinMulti = GetOldestMultiXactId();
1018 
1019  /*
1020  * Identify the latest relfrozenxid and relminmxid values that we could
1021  * validly see during the scan. These are conservative values, but it's
1022  * not really worth trying to be more exact.
1023  */
1024  lastSaneFrozenXid = ReadNewTransactionId();
1025  lastSaneMinMulti = ReadNextMultiXactId();
1026 
1027  /*
1028  * We must seqscan pg_class to find the minimum Xid, because there is no
1029  * index that can help us here.
1030  */
1031  relation = heap_open(RelationRelationId, AccessShareLock);
1032 
1033  scan = systable_beginscan(relation, InvalidOid, false,
1034  NULL, 0, NULL);
1035 
1036  while ((classTup = systable_getnext(scan)) != NULL)
1037  {
1038  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1039 
1040  /*
1041  * Only consider relations able to hold unfrozen XIDs (anything else
1042  * should have InvalidTransactionId in relfrozenxid anyway.)
1043  */
1044  if (classForm->relkind != RELKIND_RELATION &&
1045  classForm->relkind != RELKIND_MATVIEW &&
1046  classForm->relkind != RELKIND_TOASTVALUE)
1047  continue;
1048 
1049  Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1050  Assert(MultiXactIdIsValid(classForm->relminmxid));
1051 
1052  /*
1053  * If things are working properly, no relation should have a
1054  * relfrozenxid or relminmxid that is "in the future". However, such
1055  * cases have been known to arise due to bugs in pg_upgrade. If we
1056  * see any entries that are "in the future", chicken out and don't do
1057  * anything. This ensures we won't truncate clog before those
1058  * relations have been scanned and cleaned up.
1059  */
1060  if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid) ||
1061  MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1062  {
1063  bogus = true;
1064  break;
1065  }
1066 
1067  if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1068  newFrozenXid = classForm->relfrozenxid;
1069 
1070  if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1071  newMinMulti = classForm->relminmxid;
1072  }
1073 
1074  /* we're done with pg_class */
1075  systable_endscan(scan);
1076  heap_close(relation, AccessShareLock);
1077 
1078  /* chicken out if bogus data found */
1079  if (bogus)
1080  return;
1081 
1082  Assert(TransactionIdIsNormal(newFrozenXid));
1083  Assert(MultiXactIdIsValid(newMinMulti));
1084 
1085  /* Now fetch the pg_database tuple we need to update. */
1086  relation = heap_open(DatabaseRelationId, RowExclusiveLock);
1087 
1088  /* Fetch a copy of the tuple to scribble on */
1090  if (!HeapTupleIsValid(tuple))
1091  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1092  dbform = (Form_pg_database) GETSTRUCT(tuple);
1093 
1094  /*
1095  * As in vac_update_relstats(), we ordinarily don't want to let
1096  * datfrozenxid go backward; but if it's "in the future" then it must be
1097  * corrupt and it seems best to overwrite it.
1098  */
1099  if (dbform->datfrozenxid != newFrozenXid &&
1100  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1101  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1102  {
1103  dbform->datfrozenxid = newFrozenXid;
1104  dirty = true;
1105  }
1106  else
1107  newFrozenXid = dbform->datfrozenxid;
1108 
1109  /* Ditto for datminmxid */
1110  if (dbform->datminmxid != newMinMulti &&
1111  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1112  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1113  {
1114  dbform->datminmxid = newMinMulti;
1115  dirty = true;
1116  }
1117  else
1118  newMinMulti = dbform->datminmxid;
1119 
1120  if (dirty)
1121  heap_inplace_update(relation, tuple);
1122 
1123  heap_freetuple(tuple);
1124  heap_close(relation, RowExclusiveLock);
1125 
1126  /*
1127  * If we were able to advance datfrozenxid or datminmxid, see if we can
1128  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1129  * XID-wrap-limit info is stale, since this action will update that too.
1130  */
1131  if (dirty || ForceTransactionIdLimitUpdate())
1132  vac_truncate_clog(newFrozenXid, newMinMulti,
1133  lastSaneFrozenXid, lastSaneMinMulti);
1134 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:502
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
uint32 TransactionId
Definition: c.h:474
FormData_pg_database * Form_pg_database
Definition: pg_database.h:54
#define AccessShareLock
Definition: lockdefs.h:36
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:331
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:429
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:419
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2491
Oid MyDatabaseId
Definition: globals.c:84
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define InvalidOid
Definition: postgres_ext.h:36
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1312
TransactionId MultiXactId
Definition: c.h:484
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:699
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3140
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1155
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6390
#define elog
Definition: elog.h:219
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:721

◆ vac_update_relstats()

void vac_update_relstats ( Relation  relation,
BlockNumber  num_pages,
double  num_tuples,
BlockNumber  num_all_visible_pages,
bool  hasindex,
TransactionId  frozenxid,
MultiXactId  minmulti,
bool  in_outer_xact 
)

Definition at line 864 of file vacuum.c.

References elog, ERROR, GETSTRUCT, heap_close, heap_inplace_update(), heap_open(), HeapTupleIsValid, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum, RelationData::rd_rules, ReadNewTransactionId(), ReadNextMultiXactId(), RelationGetRelid, RELOID, RowExclusiveLock, SearchSysCacheCopy1, TransactionIdIsNormal, TransactionIdPrecedes(), and RelationData::trigdesc.

Referenced by do_analyze_rel(), lazy_cleanup_index(), and lazy_vacuum_rel().

870 {
871  Oid relid = RelationGetRelid(relation);
872  Relation rd;
873  HeapTuple ctup;
874  Form_pg_class pgcform;
875  bool dirty;
876 
877  rd = heap_open(RelationRelationId, RowExclusiveLock);
878 
879  /* Fetch a copy of the tuple to scribble on */
881  if (!HeapTupleIsValid(ctup))
882  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
883  relid);
884  pgcform = (Form_pg_class) GETSTRUCT(ctup);
885 
886  /* Apply statistical updates, if any, to copied tuple */
887 
888  dirty = false;
889  if (pgcform->relpages != (int32) num_pages)
890  {
891  pgcform->relpages = (int32) num_pages;
892  dirty = true;
893  }
894  if (pgcform->reltuples != (float4) num_tuples)
895  {
896  pgcform->reltuples = (float4) num_tuples;
897  dirty = true;
898  }
899  if (pgcform->relallvisible != (int32) num_all_visible_pages)
900  {
901  pgcform->relallvisible = (int32) num_all_visible_pages;
902  dirty = true;
903  }
904 
905  /* Apply DDL updates, but not inside an outer transaction (see above) */
906 
907  if (!in_outer_xact)
908  {
909  /*
910  * If we didn't find any indexes, reset relhasindex.
911  */
912  if (pgcform->relhasindex && !hasindex)
913  {
914  pgcform->relhasindex = false;
915  dirty = true;
916  }
917 
918  /* We also clear relhasrules and relhastriggers if needed */
919  if (pgcform->relhasrules && relation->rd_rules == NULL)
920  {
921  pgcform->relhasrules = false;
922  dirty = true;
923  }
924  if (pgcform->relhastriggers && relation->trigdesc == NULL)
925  {
926  pgcform->relhastriggers = false;
927  dirty = true;
928  }
929  }
930 
931  /*
932  * Update relfrozenxid, unless caller passed InvalidTransactionId
933  * indicating it has no new data.
934  *
935  * Ordinarily, we don't let relfrozenxid go backwards: if things are
936  * working correctly, the only way the new frozenxid could be older would
937  * be if a previous VACUUM was done with a tighter freeze_min_age, in
938  * which case we don't want to forget the work it already did. However,
939  * if the stored relfrozenxid is "in the future", then it must be corrupt
940  * and it seems best to overwrite it with the cutoff we used this time.
941  * This should match vac_update_datfrozenxid() concerning what we consider
942  * to be "in the future".
943  */
944  if (TransactionIdIsNormal(frozenxid) &&
945  pgcform->relfrozenxid != frozenxid &&
946  (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
948  pgcform->relfrozenxid)))
949  {
950  pgcform->relfrozenxid = frozenxid;
951  dirty = true;
952  }
953 
954  /* Similarly for relminmxid */
955  if (MultiXactIdIsValid(minmulti) &&
956  pgcform->relminmxid != minmulti &&
957  (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
958  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
959  {
960  pgcform->relminmxid = minmulti;
961  dirty = true;
962  }
963 
964  /* If anything changed, write out the tuple. */
965  if (dirty)
966  heap_inplace_update(rd, ctup);
967 
969 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
#define heap_close(r, l)
Definition: heapam.h:97
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:313
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
TriggerDesc * trigdesc
Definition: rel.h:90
#define RowExclusiveLock
Definition: lockdefs.h:38
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
float float4
Definition: c.h:457
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RuleLock * rd_rules
Definition: rel.h:88
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3140
FormData_pg_class * Form_pg_class
Definition: pg_class.h:93
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:173
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6390
#define elog
Definition: elog.h:219
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
#define RelationGetRelid(relation)
Definition: rel.h:407
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:721

◆ vacuum()

void vacuum ( int  options,
List relations,
VacuumParams params,
BufferAccessStrategy  bstrategy,
bool  isTopLevel 
)

Definition at line 166 of file vacuum.c.

References ActiveSnapshotSet(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze_rel(), Assert, BAS_VACUUM, CommitTransactionCommand(), cur, ereport, errcode(), errmsg(), ERROR, expand_vacuum_rel(), get_all_vacuum_rels(), GetAccessStrategy(), GetTransactionSnapshot(), IsAutoVacuumWorkerProcess(), IsInTransactionBlock(), lfirst_node, list_concat(), list_length(), MemoryContextDelete(), MemoryContextSwitchTo(), NIL, VacuumRelation::oid, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgstat_vacuum_stat(), PopActiveSnapshot(), PortalContext, PreventInTransactionBlock(), PushActiveSnapshot(), VacuumRelation::relation, StartTransactionCommand(), VacuumRelation::va_cols, vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FULL, VACOPT_VACUUM, vacuum_rel(), VacuumCostActive, VacuumCostBalance, VacuumCostDelay, VacuumPageDirty, VacuumPageHit, and VacuumPageMiss.

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

168 {
169  static bool in_vacuum = false;
170 
171  const char *stmttype;
172  volatile bool in_outer_xact,
173  use_own_xacts;
174 
175  Assert(params != NULL);
176 
177  stmttype = (options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
178 
179  /*
180  * We cannot run VACUUM inside a user transaction block; if we were inside
181  * a transaction, then our commit- and start-transaction-command calls
182  * would not have the intended effect! There are numerous other subtle
183  * dependencies on this, too.
184  *
185  * ANALYZE (without VACUUM) can run either way.
186  */
187  if (options & VACOPT_VACUUM)
188  {
189  PreventInTransactionBlock(isTopLevel, stmttype);
190  in_outer_xact = false;
191  }
192  else
193  in_outer_xact = IsInTransactionBlock(isTopLevel);
194 
195  /*
196  * Due to static variables vac_context, anl_context and vac_strategy,
197  * vacuum() is not reentrant. This matters when VACUUM FULL or ANALYZE
198  * calls a hostile index expression that itself calls ANALYZE.
199  */
200  if (in_vacuum)
201  ereport(ERROR,
202  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
203  errmsg("%s cannot be executed from VACUUM or ANALYZE",
204  stmttype)));
205 
206  /*
207  * Sanity check DISABLE_PAGE_SKIPPING option.
208  */
209  if ((options & VACOPT_FULL) != 0 &&
211  ereport(ERROR,
212  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
213  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
214 
215  /*
216  * Send info about dead objects to the statistics collector, unless we are
217  * in autovacuum --- autovacuum.c does this for itself.
218  */
219  if ((options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
221 
222  /*
223  * Create special memory context for cross-transaction storage.
224  *
225  * Since it is a child of PortalContext, it will go away eventually even
226  * if we suffer an error; there's no need for special abort cleanup logic.
227  */
229  "Vacuum",
231 
232  /*
233  * If caller didn't give us a buffer strategy object, make one in the
234  * cross-transaction memory context.
235  */
236  if (bstrategy == NULL)
237  {
239 
240  bstrategy = GetAccessStrategy(BAS_VACUUM);
241  MemoryContextSwitchTo(old_context);
242  }
243  vac_strategy = bstrategy;
244 
245  /*
246  * Build list of relation(s) to process, putting any new data in
247  * vac_context for safekeeping.
248  */
249  if (relations != NIL)
250  {
251  List *newrels = NIL;
252  ListCell *lc;
253 
254  foreach(lc, relations)
255  {
257  List *sublist;
258  MemoryContext old_context;
259 
260  sublist = expand_vacuum_rel(vrel);
261  old_context = MemoryContextSwitchTo(vac_context);
262  newrels = list_concat(newrels, sublist);
263  MemoryContextSwitchTo(old_context);
264  }
265  relations = newrels;
266  }
267  else
268  relations = get_all_vacuum_rels();
269 
270  /*
271  * Decide whether we need to start/commit our own transactions.
272  *
273  * For VACUUM (with or without ANALYZE): always do so, so that we can
274  * release locks as soon as possible. (We could possibly use the outer
275  * transaction for a one-table VACUUM, but handling TOAST tables would be
276  * problematic.)
277  *
278  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
279  * start/commit our own transactions. Also, there's no need to do so if
280  * only processing one relation. For multiple relations when not within a
281  * transaction block, and also in an autovacuum worker, use own
282  * transactions so we can release locks sooner.
283  */
284  if (options & VACOPT_VACUUM)
285  use_own_xacts = true;
286  else
287  {
290  use_own_xacts = true;
291  else if (in_outer_xact)
292  use_own_xacts = false;
293  else if (list_length(relations) > 1)
294  use_own_xacts = true;
295  else
296  use_own_xacts = false;
297  }
298 
299  /*
300  * vacuum_rel expects to be entered with no transaction active; it will
301  * start and commit its own transaction. But we are called by an SQL
302  * command, and so we are executing inside a transaction already. We
303  * commit the transaction started in PostgresMain() here, and start
304  * another one before exiting to match the commit waiting for us back in
305  * PostgresMain().
306  */
307  if (use_own_xacts)
308  {
309  Assert(!in_outer_xact);
310 
311  /* ActiveSnapshot is not set by autovacuum */
312  if (ActiveSnapshotSet())
314 
315  /* matches the StartTransaction in PostgresMain() */
317  }
318 
319  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
320  PG_TRY();
321  {
322  ListCell *cur;
323 
324  in_vacuum = true;
326  VacuumCostBalance = 0;
327  VacuumPageHit = 0;
328  VacuumPageMiss = 0;
329  VacuumPageDirty = 0;
330 
331  /*
332  * Loop to process each selected relation.
333  */
334  foreach(cur, relations)
335  {
337 
338  if (options & VACOPT_VACUUM)
339  {
340  if (!vacuum_rel(vrel->oid, vrel->relation, options, params))
341  continue;
342  }
343 
344  if (options & VACOPT_ANALYZE)
345  {
346  /*
347  * If using separate xacts, start one for analyze. Otherwise,
348  * we can use the outer transaction.
349  */
350  if (use_own_xacts)
351  {
353  /* functions in indexes may want a snapshot set */
355  }
356 
357  analyze_rel(vrel->oid, vrel->relation, options, params,
358  vrel->va_cols, in_outer_xact, vac_strategy);
359 
360  if (use_own_xacts)
361  {
364  }
365  }
366  }
367  }
368  PG_CATCH();
369  {
370  in_vacuum = false;
371  VacuumCostActive = false;
372  PG_RE_THROW();
373  }
374  PG_END_TRY();
375 
376  in_vacuum = false;
377  VacuumCostActive = false;
378 
379  /*
380  * Finish up processing.
381  */
382  if (use_own_xacts)
383  {
384  /* here, we are not in a transaction */
385 
386  /*
387  * This matches the CommitTransaction waiting for us in
388  * PostgresMain().
389  */
391  }
392 
393  if ((options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
394  {
395  /*
396  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
397  * (autovacuum.c does this for itself.)
398  */
400  }
401 
402  /*
403  * Clean up working storage --- note we must do this after
404  * StartTransactionCommand, else we might be trying to delete the active
405  * context!
406  */
408  vac_context = NULL;
409 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define NIL
Definition: pg_list.h:69
static List * get_all_vacuum_rels(void)
Definition: vacuum.c:527
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
int VacuumCostBalance
Definition: globals.c:146
RangeVar * relation
Definition: parsenodes.h:3153
void vac_update_datfrozenxid(void)
Definition: vacuum.c:991
int VacuumPageHit
Definition: globals.c:142
void CommitTransactionCommand(void)
Definition: xact.c:2744
void analyze_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:115
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:67
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
List * list_concat(List *list1, List *list2)
Definition: list.c:321
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
MemoryContext PortalContext
Definition: mcxt.c:53
#define ERROR
Definition: elog.h:43
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static List * expand_vacuum_rel(VacuumRelation *vrel)
Definition: vacuum.c:426
#define lfirst_node(type, lc)
Definition: pg_list.h:109
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
static MemoryContext vac_context
Definition: vacuum.c:66
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3153
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:851
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3295
#define ereport(elevel, rest)
Definition: elog.h:122
void pgstat_vacuum_stat(void)
Definition: pgstat.c:1022
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3267
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
int VacuumPageDirty
Definition: globals.c:144
int VacuumCostDelay
Definition: globals.c:140
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:699
static bool vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
Definition: vacuum.c:1301
void StartTransactionCommand(void)
Definition: xact.c:2673
static int list_length(const List *l)
Definition: pg_list.h:89
#define PG_RE_THROW()
Definition: elog.h:314
int errmsg(const char *fmt,...)
Definition: elog.c:797
int VacuumPageMiss
Definition: globals.c:143
#define PG_TRY()
Definition: elog.h:284
Definition: pg_list.h:45
#define PG_END_TRY()
Definition: elog.h:300
bool VacuumCostActive
Definition: globals.c:147

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 1672 of file vacuum.c.

References AutoVacuumUpdateDelay(), CHECK_FOR_INTERRUPTS, InterruptPending, pg_usleep(), VacuumCostActive, VacuumCostBalance, VacuumCostDelay, and VacuumCostLimit.

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

1673 {
1674  /* Always check for interrupts */
1676 
1677  /* Nap if appropriate */
1680  {
1681  int msec;
1682 
1684  if (msec > VacuumCostDelay * 4)
1685  msec = VacuumCostDelay * 4;
1686 
1687  pg_usleep(msec * 1000L);
1688 
1689  VacuumCostBalance = 0;
1690 
1691  /* update balance values for workers */
1693 
1694  /* Might have gotten an interrupt while sleeping */
1696  }
1697 }
int VacuumCostBalance
Definition: globals.c:146
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:139
int VacuumCostDelay
Definition: globals.c:140
volatile bool InterruptPending
Definition: globals.c:30
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1773
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
bool VacuumCostActive
Definition: globals.c:147

◆ vacuum_rel()

static bool vacuum_rel ( Oid  relid,
RangeVar relation,
int  options,
VacuumParams params 
)
static

Definition at line 1301 of file vacuum.c.

References AccessExclusiveLock, Assert, AtEOXact_GUC(), CHECK_FOR_INTERRUPTS, cluster_rel(), CommitTransactionCommand(), ConditionalLockRelationOid(), elevel, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), GetTransactionSnapshot(), GetUserId(), GetUserIdAndSecContext(), InvalidOid, VacuumParams::is_wraparound, IsAutoVacuumWorkerProcess(), lazy_vacuum_rel(), LockRelationIdForSession(), LockInfoData::lockRelId, LOG, VacuumParams::log_min_duration, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyDatabaseId, MyPgXact, NewGUCNestLevel(), NoLock, pg_class_ownercheck(), pg_database_ownercheck(), PopActiveSnapshot(), PROC_IN_VACUUM, PROC_VACUUM_FOR_WRAPAROUND, PushActiveSnapshot(), RelationData::rd_lockInfo, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetRelationName, RelationGetRelid, RangeVar::relname, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, StartTransactionCommand(), try_relation_open(), UnlockRelationIdForSession(), VACOPT_FULL, VACOPT_NOWAIT, VACOPT_SKIPTOAST, VACOPT_VERBOSE, PGXACT::vacuumFlags, and WARNING.

Referenced by vacuum().

1302 {
1303  LOCKMODE lmode;
1304  Relation onerel;
1305  LockRelId onerelid;
1306  Oid toast_relid;
1307  Oid save_userid;
1308  int save_sec_context;
1309  int save_nestlevel;
1310  bool rel_lock = true;
1311 
1312  Assert(params != NULL);
1313 
1314  /* Begin a transaction for vacuuming this relation */
1316 
1317  /*
1318  * Functions in indexes may want a snapshot set. Also, setting a snapshot
1319  * ensures that RecentGlobalXmin is kept truly recent.
1320  */
1322 
1323  if (!(options & VACOPT_FULL))
1324  {
1325  /*
1326  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1327  * other concurrent VACUUMs know that they can ignore this one while
1328  * determining their OldestXmin. (The reason we don't set it during a
1329  * full VACUUM is exactly that we may have to run user-defined
1330  * functions for functional indexes, and we want to make sure that if
1331  * they use the snapshot set above, any tuples it requires can't get
1332  * removed from other tables. An index function that depends on the
1333  * contents of other tables is arguably broken, but we won't break it
1334  * here by violating transaction semantics.)
1335  *
1336  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1337  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1338  * in an emergency.
1339  *
1340  * Note: these flags remain set until CommitTransaction or
1341  * AbortTransaction. We don't want to clear them until we reset
1342  * MyPgXact->xid/xmin, else OldestXmin might appear to go backwards,
1343  * which is probably Not Good.
1344  */
1345  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1347  if (params->is_wraparound)
1349  LWLockRelease(ProcArrayLock);
1350  }
1351 
1352  /*
1353  * Check for user-requested abort. Note we want this to be inside a
1354  * transaction, so xact.c doesn't issue useless WARNING.
1355  */
1357 
1358  /*
1359  * Determine the type of lock we want --- hard exclusive lock for a FULL
1360  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1361  * way, we can be sure that no other backend is vacuuming the same table.
1362  */
1364 
1365  /*
1366  * Open the relation and get the appropriate lock on it.
1367  *
1368  * There's a race condition here: the rel may have gone away since the
1369  * last time we saw it. If so, we don't need to vacuum it.
1370  *
1371  * If we've been asked not to wait for the relation lock, acquire it first
1372  * in non-blocking mode, before calling try_relation_open().
1373  */
1374  if (!(options & VACOPT_NOWAIT))
1375  onerel = try_relation_open(relid, lmode);
1376  else if (ConditionalLockRelationOid(relid, lmode))
1377  onerel = try_relation_open(relid, NoLock);
1378  else
1379  {
1380  onerel = NULL;
1381  rel_lock = false;
1382  }
1383 
1384  /*
1385  * If we failed to open or lock the relation, emit a log message before
1386  * exiting.
1387  */
1388  if (!onerel)
1389  {
1390  int elevel = 0;
1391 
1392  /*
1393  * Determine the log level.
1394  *
1395  * If the RangeVar is not defined, we do not have enough information
1396  * to provide a meaningful log statement. Chances are that
1397  * vacuum_rel's caller has intentionally not provided this information
1398  * so that this logging is skipped, anyway.
1399  *
1400  * Otherwise, for autovacuum logs, we emit a LOG if
1401  * log_autovacuum_min_duration is not disabled. For manual VACUUM, we
1402  * emit a WARNING to match the log statements in the permission
1403  * checks.
1404  */
1405  if (relation != NULL)
1406  {
1408  elevel = WARNING;
1409  else if (params->log_min_duration >= 0)
1410  elevel = LOG;
1411  }
1412 
1413  if (elevel != 0)
1414  {
1415  if (!rel_lock)
1416  ereport(elevel,
1417  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
1418  errmsg("skipping vacuum of \"%s\" --- lock not available",
1419  relation->relname)));
1420  else
1421  ereport(elevel,
1423  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
1424  relation->relname)));
1425  }
1426 
1429  return false;
1430  }
1431 
1432  /*
1433  * Check permissions.
1434  *
1435  * We allow the user to vacuum a table if he is superuser, the table
1436  * owner, or the database owner (but in the latter case, only if it's not
1437  * a shared relation). pg_class_ownercheck includes the superuser case.
1438  *
1439  * Note we choose to treat permissions failure as a WARNING and keep
1440  * trying to vacuum the rest of the DB --- is this appropriate?
1441  */
1442  if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
1443  (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !onerel->rd_rel->relisshared)))
1444  {
1445  if (onerel->rd_rel->relisshared)
1446  ereport(WARNING,
1447  (errmsg("skipping \"%s\" --- only superuser can vacuum it",
1448  RelationGetRelationName(onerel))));
1449  else if (onerel->rd_rel->relnamespace == PG_CATALOG_NAMESPACE)
1450  ereport(WARNING,
1451  (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
1452  RelationGetRelationName(onerel))));
1453  else
1454  ereport(WARNING,
1455  (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
1456  RelationGetRelationName(onerel))));
1457  relation_close(onerel, lmode);
1460  return false;
1461  }
1462 
1463  /*
1464  * Check that it's of a vacuumable relkind.
1465  */
1466  if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1467  onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1468  onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1469  onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1470  {
1471  ereport(WARNING,
1472  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1473  RelationGetRelationName(onerel))));
1474  relation_close(onerel, lmode);
1477  return false;
1478  }
1479 
1480  /*
1481  * Silently ignore tables that are temp tables of other backends ---
1482  * trying to vacuum these will lead to great unhappiness, since their
1483  * contents are probably not up-to-date on disk. (We don't throw a
1484  * warning here; it would just lead to chatter during a database-wide
1485  * VACUUM.)
1486  */
1487  if (RELATION_IS_OTHER_TEMP(onerel))
1488  {
1489  relation_close(onerel, lmode);
1492  return false;
1493  }
1494 
1495  /*
1496  * Silently ignore partitioned tables as there is no work to be done. The
1497  * useful work is on their child partitions, which have been queued up for
1498  * us separately.
1499  */
1500  if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1501  {
1502  relation_close(onerel, lmode);
1505  /* It's OK to proceed with ANALYZE on this table */
1506  return true;
1507  }
1508 
1509  /*
1510  * Get a session-level lock too. This will protect our access to the
1511  * relation across multiple transactions, so that we can vacuum the
1512  * relation's TOAST table (if any) secure in the knowledge that no one is
1513  * deleting the parent relation.
1514  *
1515  * NOTE: this cannot block, even if someone else is waiting for access,
1516  * because the lock manager knows that both lock requests are from the
1517  * same process.
1518  */
1519  onerelid = onerel->rd_lockInfo.lockRelId;
1520  LockRelationIdForSession(&onerelid, lmode);
1521 
1522  /*
1523  * Remember the relation's TOAST relation for later, if the caller asked
1524  * us to process it. In VACUUM FULL, though, the toast table is
1525  * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1526  */
1527  if (!(options & VACOPT_SKIPTOAST) && !(options & VACOPT_FULL))
1528  toast_relid = onerel->rd_rel->reltoastrelid;
1529  else
1530  toast_relid = InvalidOid;
1531 
1532  /*
1533  * Switch to the table owner's userid, so that any index functions are run
1534  * as that user. Also lock down security-restricted operations and
1535  * arrange to make GUC variable changes local to this command. (This is
1536  * unnecessary, but harmless, for lazy VACUUM.)
1537  */
1538  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1539  SetUserIdAndSecContext(onerel->rd_rel->relowner,
1540  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1541  save_nestlevel = NewGUCNestLevel();
1542 
1543  /*
1544  * Do the actual work --- either FULL or "lazy" vacuum
1545  */
1546  if (options & VACOPT_FULL)
1547  {
1548  /* close relation before vacuuming, but hold lock until commit */
1549  relation_close(onerel, NoLock);
1550  onerel = NULL;
1551 
1552  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1553  cluster_rel(relid, InvalidOid, false,
1554  (options & VACOPT_VERBOSE) != 0);
1555  }
1556  else
1557  lazy_vacuum_rel(onerel, options, params, vac_strategy);
1558 
1559  /* Roll back any GUC changes executed by index functions */
1560  AtEOXact_GUC(false, save_nestlevel);
1561 
1562  /* Restore userid and security context */
1563  SetUserIdAndSecContext(save_userid, save_sec_context);
1564 
1565  /* all done with this class, but hold lock until commit */
1566  if (onerel)
1567  relation_close(onerel, NoLock);
1568 
1569  /*
1570  * Complete the transaction and free all temporary memory used.
1571  */
1574 
1575  /*
1576  * If the relation has a secondary toast rel, vacuum that too while we
1577  * still hold the session lock on the master table. Note however that
1578  * "analyze" will not get done on the toast table. This is good, because
1579  * the toaster always uses hardcoded index access and statistics are
1580  * totally unimportant for toast relations.
1581  */
1582  if (toast_relid != InvalidOid)
1583  vacuum_rel(toast_relid, NULL, options, params);
1584 
1585  /*
1586  * Now release the session-level lock on the master table.
1587  */
1588  UnlockRelationIdForSession(&onerelid, lmode);
1589 
1590  /* Report that we really did it. */
1591  return true;
1592 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
LockRelId lockRelId
Definition: rel.h:44
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:62
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:298
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:491
int LOCKMODE
Definition: lockdefs.h:26
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1157
Oid GetUserId(void)
Definition: miscinit.c:379
void CommitTransactionCommand(void)
Definition: xact.c:2744
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:56
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:67
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1270
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:230
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
char * relname
Definition: primnodes.h:68
#define PROC_IN_VACUUM
Definition: proc.h:54
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:312
void cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose)
Definition: cluster.c:268
Definition: rel.h:36
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:299
void lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuumlazy.c:190
bool is_wraparound
Definition: vacuum.h:144
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:87
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:484
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5322
#define RelationGetRelationName(relation)
Definition: rel.h:441
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3295
#define ereport(elevel, rest)
Definition: elog.h:122
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:5169
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:144
Oid MyDatabaseId
Definition: globals.c:84
#define InvalidOid
Definition: postgres_ext.h:36
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define Assert(condition)
Definition: c.h:699
static bool vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
Definition: vacuum.c:1301
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:538
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4751
void StartTransactionCommand(void)
Definition: xact.c:2673
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int log_min_duration
Definition: vacuum.h:145
#define AccessExclusiveLock
Definition: lockdefs.h:45
int NewGUCNestLevel(void)
Definition: guc.c:5308
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define RelationGetRelid(relation)
Definition: rel.h:407

◆ vacuum_set_xid_limits()

void vacuum_set_xid_limits ( Relation  rel,
int  freeze_min_age,
int  freeze_table_age,
int  multixact_freeze_min_age,
int  multixact_freeze_table_age,
TransactionId oldestXmin,
TransactionId freezeLimit,
TransactionId xidFullScanLimit,
MultiXactId multiXactCutoff,
MultiXactId mxactFullScanLimit 
)

Definition at line 593 of file vacuum.c.

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg(), FirstMultiXactId, FirstNormalTransactionId, GetOldestMultiXactId(), GetOldestXmin(), Min, MultiXactIdPrecedes(), MultiXactMemberFreezeThreshold(), PROCARRAY_FLAGS_VACUUM, ReadNewTransactionId(), ReadNextMultiXactId(), TransactionIdIsNormal, TransactionIdLimitedForOldSnapshots(), TransactionIdPrecedes(), vacuum_freeze_min_age, vacuum_freeze_table_age, vacuum_multixact_freeze_min_age, vacuum_multixact_freeze_table_age, and WARNING.

Referenced by copy_heap_data(), and lazy_vacuum_rel().

603 {
604  int freezemin;
605  int mxid_freezemin;
606  int effective_multixact_freeze_max_age;
607  TransactionId limit;
608  TransactionId safeLimit;
609  MultiXactId mxactLimit;
610  MultiXactId safeMxactLimit;
611 
612  /*
613  * We can always ignore processes running lazy vacuum. This is because we
614  * use these values only for deciding which tuples we must keep in the
615  * tables. Since lazy vacuum doesn't write its XID anywhere, it's safe to
616  * ignore it. In theory it could be problematic to ignore lazy vacuums in
617  * a full vacuum, but keep in mind that only one vacuum process can be
618  * working on a particular table at any time, and that each vacuum is
619  * always an independent transaction.
620  */
621  *oldestXmin =
623 
624  Assert(TransactionIdIsNormal(*oldestXmin));
625 
626  /*
627  * Determine the minimum freeze age to use: as specified by the caller, or
628  * vacuum_freeze_min_age, but in any case not more than half
629  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
630  * wraparound won't occur too frequently.
631  */
632  freezemin = freeze_min_age;
633  if (freezemin < 0)
634  freezemin = vacuum_freeze_min_age;
635  freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
636  Assert(freezemin >= 0);
637 
638  /*
639  * Compute the cutoff XID, being careful not to generate a "permanent" XID
640  */
641  limit = *oldestXmin - freezemin;
642  if (!TransactionIdIsNormal(limit))
643  limit = FirstNormalTransactionId;
644 
645  /*
646  * If oldestXmin is very far back (in practice, more than
647  * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
648  * freeze age of zero.
649  */
651  if (!TransactionIdIsNormal(safeLimit))
652  safeLimit = FirstNormalTransactionId;
653 
654  if (TransactionIdPrecedes(limit, safeLimit))
655  {
657  (errmsg("oldest xmin is far in the past"),
658  errhint("Close open transactions soon to avoid wraparound problems.\n"
659  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
660  limit = *oldestXmin;
661  }
662 
663  *freezeLimit = limit;
664 
665  /*
666  * Compute the multixact age for which freezing is urgent. This is
667  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
668  * short of multixact member space.
669  */
670  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
671 
672  /*
673  * Determine the minimum multixact freeze age to use: as specified by
674  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
675  * than half effective_multixact_freeze_max_age, so that autovacuums to
676  * prevent MultiXact wraparound won't occur too frequently.
677  */
678  mxid_freezemin = multixact_freeze_min_age;
679  if (mxid_freezemin < 0)
680  mxid_freezemin = vacuum_multixact_freeze_min_age;
681  mxid_freezemin = Min(mxid_freezemin,
682  effective_multixact_freeze_max_age / 2);
683  Assert(mxid_freezemin >= 0);
684 
685  /* compute the cutoff multi, being careful to generate a valid value */
686  mxactLimit = GetOldestMultiXactId() - mxid_freezemin;
687  if (mxactLimit < FirstMultiXactId)
688  mxactLimit = FirstMultiXactId;
689 
690  safeMxactLimit =
691  ReadNextMultiXactId() - effective_multixact_freeze_max_age;
692  if (safeMxactLimit < FirstMultiXactId)
693  safeMxactLimit = FirstMultiXactId;
694 
695  if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
696  {
698  (errmsg("oldest multixact is far in the past"),
699  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
700  mxactLimit = safeMxactLimit;
701  }
702 
703  *multiXactCutoff = mxactLimit;
704 
705  if (xidFullScanLimit != NULL)
706  {
707  int freezetable;
708 
709  Assert(mxactFullScanLimit != NULL);
710 
711  /*
712  * Determine the table freeze age to use: as specified by the caller,
713  * or vacuum_freeze_table_age, but in any case not more than
714  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
715  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
716  * before anti-wraparound autovacuum is launched.
717  */
718  freezetable = freeze_table_age;
719  if (freezetable < 0)
720  freezetable = vacuum_freeze_table_age;
721  freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
722  Assert(freezetable >= 0);
723 
724  /*
725  * Compute XID limit causing a full-table vacuum, being careful not to
726  * generate a "permanent" XID.
727  */
728  limit = ReadNewTransactionId() - freezetable;
729  if (!TransactionIdIsNormal(limit))
730  limit = FirstNormalTransactionId;
731 
732  *xidFullScanLimit = limit;
733 
734  /*
735  * Similar to the above, determine the table freeze age to use for
736  * multixacts: as specified by the caller, or
737  * vacuum_multixact_freeze_table_age, but in any case not more than
738  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
739  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
740  * freeze multixacts before anti-wraparound autovacuum is launched.
741  */
742  freezetable = multixact_freeze_table_age;
743  if (freezetable < 0)
744  freezetable = vacuum_multixact_freeze_table_age;
745  freezetable = Min(freezetable,
746  effective_multixact_freeze_max_age * 0.95);
747  Assert(freezetable >= 0);
748 
749  /*
750  * Compute MultiXact limit causing a full-table vacuum, being careful
751  * to generate a valid MultiXact value.
752  */
753  mxactLimit = ReadNextMultiXactId() - freezetable;
754  if (mxactLimit < FirstMultiXactId)
755  mxactLimit = FirstMultiXactId;
756 
757  *mxactFullScanLimit = mxactLimit;
758  }
759  else
760  {
761  Assert(mxactFullScanLimit == NULL);
762  }
763 }
int errhint(const char *fmt,...)
Definition: elog.c:987
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:62
uint32 TransactionId
Definition: c.h:474
#define Min(x, y)
Definition: c.h:857
TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation)
Definition: snapmgr.c:1741
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
#define FirstNormalTransactionId
Definition: transam.h:34
int autovacuum_freeze_max_age
Definition: autovacuum.c:121
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:61
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
#define FirstMultiXactId
Definition: multixact.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2817
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define WARNING
Definition: elog.h:40
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2491
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1312
TransactionId MultiXactId
Definition: c.h:484
#define Assert(condition)
Definition: c.h:699
int vacuum_freeze_min_age
Definition: vacuum.c:59
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3140
int vacuum_freeze_table_age
Definition: vacuum.c:60
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:721

Variable Documentation

◆ vac_context

MemoryContext vac_context = NULL
static

Definition at line 66 of file vacuum.c.

◆ vac_strategy

BufferAccessStrategy vac_strategy
static

Definition at line 67 of file vacuum.c.

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 59 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_freeze_table_age

int vacuum_freeze_table_age

Definition at line 60 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_min_age

int vacuum_multixact_freeze_min_age

Definition at line 61 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_table_age

int vacuum_multixact_freeze_table_age

Definition at line 62 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().