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/tableam.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_namespace.h"
#include "commands/cluster.h"
#include "commands/defrem.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/bgworker_internals.h"
#include "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 dependency graph for vacuum.c:

Go to the source code of this file.

Functions

static Listexpand_vacuum_rel (VacuumRelation *vrel, int options)
 
static Listget_all_vacuum_rels (int options)
 
static void vac_truncate_clog (TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
 
static bool vacuum_rel (Oid relid, RangeVar *relation, VacuumParams *params)
 
static double compute_parallel_delay (void)
 
static VacOptTernaryValue get_vacopt_ternary_value (DefElem *def)
 
void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
 
bool vacuum_is_relation_owner (Oid relid, Form_pg_class reltuple, bits32 options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
 
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
 
pg_atomic_uint32VacuumSharedCostBalance = NULL
 
pg_atomic_uint32VacuumActiveNWorkers = NULL
 
int VacuumCostBalanceLocal = 0
 

Function Documentation

◆ compute_parallel_delay()

static double compute_parallel_delay ( void  )
static

Definition at line 2120 of file vacuum.c.

References Assert, pg_atomic_add_fetch_u32(), pg_atomic_read_u32(), pg_atomic_sub_fetch_u32(), VacuumCostBalance, VacuumCostBalanceLocal, VacuumCostDelay, and VacuumCostLimit.

Referenced by vacuum_delay_point().

2121 {
2122  double msec = 0;
2123  uint32 shared_balance;
2124  int nworkers;
2125 
2126  /* Parallel vacuum must be active */
2128 
2130 
2131  /* At least count itself */
2132  Assert(nworkers >= 1);
2133 
2134  /* Update the shared cost balance value atomically */
2136 
2137  /* Compute the total local balance for the current worker */
2139 
2140  if ((shared_balance >= VacuumCostLimit) &&
2141  (VacuumCostBalanceLocal > 0.5 * ((double) VacuumCostLimit / nworkers)))
2142  {
2143  /* Compute sleep time based on the local cost balance */
2147  }
2148 
2149  /*
2150  * Reset the local balance as we accumulated it into the shared value.
2151  */
2152  VacuumCostBalance = 0;
2153 
2154  return msec;
2155 }
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:77
int VacuumCostBalance
Definition: globals.c:149
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:401
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:386
int VacuumCostLimit
Definition: globals.c:142
unsigned int uint32
Definition: c.h:441
int VacuumCostBalanceLocal
Definition: vacuum.c:78
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
#define Assert(condition)
Definition: c.h:804
double VacuumCostDelay
Definition: globals.c:143
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:241

◆ ExecVacuum()

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

Definition at line 98 of file vacuum.c.

References analyze(), DefElem::arg, Assert, defGetBoolean(), defGetInt32(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, get_vacopt_ternary_value(), VacuumParams::index_cleanup, VacuumStmt::is_vacuumcmd, VacuumParams::is_wraparound, lfirst, lfirst_node, DefElem::location, VacuumParams::log_min_duration, MAX_PARALLEL_WORKER_LIMIT, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, VacuumParams::nworkers, VacuumParams::options, VacuumStmt::options, parser_errposition(), VacuumStmt::rels, VacuumParams::truncate, VacuumRelation::va_cols, VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FREEZE, VACOPT_FULL, VACOPT_PROCESS_TOAST, VACOPT_SKIP_LOCKED, VACOPT_TERNARY_DEFAULT, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum(), and verbose.

Referenced by standard_ProcessUtility().

99 {
100  VacuumParams params;
101  bool verbose = false;
102  bool skip_locked = false;
103  bool analyze = false;
104  bool freeze = false;
105  bool full = false;
106  bool disable_page_skipping = false;
107  bool process_toast = true;
108  ListCell *lc;
109 
110  /* Set default value */
113 
114  /* By default parallel vacuum is enabled */
115  params.nworkers = 0;
116 
117  /* Parse options list */
118  foreach(lc, vacstmt->options)
119  {
120  DefElem *opt = (DefElem *) lfirst(lc);
121 
122  /* Parse common options for VACUUM and ANALYZE */
123  if (strcmp(opt->defname, "verbose") == 0)
124  verbose = defGetBoolean(opt);
125  else if (strcmp(opt->defname, "skip_locked") == 0)
126  skip_locked = defGetBoolean(opt);
127  else if (!vacstmt->is_vacuumcmd)
128  ereport(ERROR,
129  (errcode(ERRCODE_SYNTAX_ERROR),
130  errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
131  parser_errposition(pstate, opt->location)));
132 
133  /* Parse options available on VACUUM */
134  else if (strcmp(opt->defname, "analyze") == 0)
135  analyze = defGetBoolean(opt);
136  else if (strcmp(opt->defname, "freeze") == 0)
137  freeze = defGetBoolean(opt);
138  else if (strcmp(opt->defname, "full") == 0)
139  full = defGetBoolean(opt);
140  else if (strcmp(opt->defname, "disable_page_skipping") == 0)
141  disable_page_skipping = defGetBoolean(opt);
142  else if (strcmp(opt->defname, "index_cleanup") == 0)
144  else if (strcmp(opt->defname, "process_toast") == 0)
145  process_toast = defGetBoolean(opt);
146  else if (strcmp(opt->defname, "truncate") == 0)
147  params.truncate = get_vacopt_ternary_value(opt);
148  else if (strcmp(opt->defname, "parallel") == 0)
149  {
150  if (opt->arg == NULL)
151  {
152  ereport(ERROR,
153  (errcode(ERRCODE_SYNTAX_ERROR),
154  errmsg("parallel option requires a value between 0 and %d",
156  parser_errposition(pstate, opt->location)));
157  }
158  else
159  {
160  int nworkers;
161 
162  nworkers = defGetInt32(opt);
163  if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
164  ereport(ERROR,
165  (errcode(ERRCODE_SYNTAX_ERROR),
166  errmsg("parallel vacuum degree must be between 0 and %d",
167  MAX_PARALLEL_WORKER_LIMIT),
168  parser_errposition(pstate, opt->location)));
169 
170  /*
171  * Disable parallel vacuum, if user has specified parallel
172  * degree as zero.
173  */
174  if (nworkers == 0)
175  params.nworkers = -1;
176  else
177  params.nworkers = nworkers;
178  }
179  }
180  else
181  ereport(ERROR,
182  (errcode(ERRCODE_SYNTAX_ERROR),
183  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
184  parser_errposition(pstate, opt->location)));
185  }
186 
187  /* Set vacuum options */
188  params.options =
189  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
190  (verbose ? VACOPT_VERBOSE : 0) |
191  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
192  (analyze ? VACOPT_ANALYZE : 0) |
193  (freeze ? VACOPT_FREEZE : 0) |
194  (full ? VACOPT_FULL : 0) |
195  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
196  (process_toast ? VACOPT_PROCESS_TOAST : 0);
197 
198  /* sanity checks on options */
199  Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
200  Assert((params.options & VACOPT_VACUUM) ||
201  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
202 
203  if ((params.options & VACOPT_FULL) && params.nworkers > 0)
204  ereport(ERROR,
205  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
206  errmsg("VACUUM FULL cannot be performed in parallel")));
207 
208  /*
209  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
210  */
211  if (!(params.options & VACOPT_ANALYZE))
212  {
213  ListCell *lc;
214 
215  foreach(lc, vacstmt->rels)
216  {
218 
219  if (vrel->va_cols != NIL)
220  ereport(ERROR,
221  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
222  errmsg("ANALYZE option must be specified when a column list is provided")));
223  }
224  }
225 
226  /*
227  * All freeze ages are zero if the FREEZE option is given; otherwise pass
228  * them as -1 which means to use the default values.
229  */
230  if (params.options & VACOPT_FREEZE)
231  {
232  params.freeze_min_age = 0;
233  params.freeze_table_age = 0;
234  params.multixact_freeze_min_age = 0;
235  params.multixact_freeze_table_age = 0;
236  }
237  else
238  {
239  params.freeze_min_age = -1;
240  params.freeze_table_age = -1;
241  params.multixact_freeze_min_age = -1;
242  params.multixact_freeze_table_age = -1;
243  }
244 
245  /* user-invoked vacuum is never "for wraparound" */
246  params.is_wraparound = false;
247 
248  /* user-invoked vacuum never uses this parameter */
249  params.log_min_duration = -1;
250 
251  /* Now go through the common routine */
252  vacuum(vacstmt->rels, &params, NULL, isTopLevel);
253 }
#define NIL
Definition: pg_list.h:65
int multixact_freeze_table_age
Definition: vacuum.h:213
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:275
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition: vacuum.h:185
#define VACOPT_ANALYZE
Definition: vacuum.h:179
bool is_vacuumcmd
Definition: parsenodes.h:3265
int32 defGetInt32(DefElem *def)
Definition: define.c:166
int errcode(int sqlerrcode)
Definition: elog.c:694
int freeze_table_age
Definition: vacuum.h:210
#define VACOPT_PROCESS_TOAST
Definition: vacuum.h:184
bool defGetBoolean(DefElem *def)
Definition: define.c:111
bits32 options
Definition: vacuum.h:208
#define ERROR
Definition: elog.h:45
int freeze_min_age
Definition: vacuum.h:209
bool is_wraparound
Definition: vacuum.h:215
#define lfirst_node(type, lc)
Definition: pg_list.h:172
int location
Definition: parsenodes.h:736
#define VACOPT_FREEZE
Definition: vacuum.h:181
VacOptTernaryValue index_cleanup
Definition: vacuum.h:219
Node * arg
Definition: parsenodes.h:734
static int verbose
VacOptTernaryValue truncate
Definition: vacuum.h:221
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
#define lfirst(lc)
Definition: pg_list.h:169
#define VACOPT_FULL
Definition: vacuum.h:182
int nworkers
Definition: vacuum.h:229
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int log_min_duration
Definition: vacuum.h:216
#define VACOPT_VERBOSE
Definition: vacuum.h:180
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define VACOPT_SKIP_LOCKED
Definition: vacuum.h:183
List * options
Definition: parsenodes.h:3263
char * defname
Definition: parsenodes.h:733
#define MAX_PARALLEL_WORKER_LIMIT
int multixact_freeze_min_age
Definition: vacuum.h:211
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:2987
List * rels
Definition: parsenodes.h:3264
static VacOptTernaryValue get_vacopt_ternary_value(DefElem *def)
Definition: vacuum.c:2164

◆ expand_vacuum_rel()

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

Definition at line 729 of file vacuum.c.

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

Referenced by vacuum().

730 {
731  List *vacrels = NIL;
732  MemoryContext oldcontext;
733 
734  /* If caller supplied OID, there's nothing we need do here. */
735  if (OidIsValid(vrel->oid))
736  {
737  oldcontext = MemoryContextSwitchTo(vac_context);
738  vacrels = lappend(vacrels, vrel);
739  MemoryContextSwitchTo(oldcontext);
740  }
741  else
742  {
743  /* Process a specific relation, and possibly partitions thereof */
744  Oid relid;
745  HeapTuple tuple;
746  Form_pg_class classForm;
747  bool include_parts;
748  int rvr_opts;
749 
750  /*
751  * Since autovacuum workers supply OIDs when calling vacuum(), no
752  * autovacuum worker should reach this code.
753  */
755 
756  /*
757  * We transiently take AccessShareLock to protect the syscache lookup
758  * below, as well as find_all_inheritors's expectation that the caller
759  * holds some lock on the starting relation.
760  */
761  rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
762  relid = RangeVarGetRelidExtended(vrel->relation,
764  rvr_opts,
765  NULL, NULL);
766 
767  /*
768  * If the lock is unavailable, emit the same log statement that
769  * vacuum_rel() and analyze_rel() would.
770  */
771  if (!OidIsValid(relid))
772  {
773  if (options & VACOPT_VACUUM)
775  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
776  errmsg("skipping vacuum of \"%s\" --- lock not available",
777  vrel->relation->relname)));
778  else
780  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
781  errmsg("skipping analyze of \"%s\" --- lock not available",
782  vrel->relation->relname)));
783  return vacrels;
784  }
785 
786  /*
787  * To check whether the relation is a partitioned table and its
788  * ownership, fetch its syscache entry.
789  */
790  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
791  if (!HeapTupleIsValid(tuple))
792  elog(ERROR, "cache lookup failed for relation %u", relid);
793  classForm = (Form_pg_class) GETSTRUCT(tuple);
794 
795  /*
796  * Make a returnable VacuumRelation for this rel if user is a proper
797  * owner.
798  */
799  if (vacuum_is_relation_owner(relid, classForm, options))
800  {
801  oldcontext = MemoryContextSwitchTo(vac_context);
802  vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
803  relid,
804  vrel->va_cols));
805  MemoryContextSwitchTo(oldcontext);
806  }
807 
808 
809  include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
810  ReleaseSysCache(tuple);
811 
812  /*
813  * If it is, make relation list entries for its partitions. Note that
814  * the list returned by find_all_inheritors() includes the passed-in
815  * OID, so we have to skip that. There's no point in taking locks on
816  * the individual partitions yet, and doing so would just add
817  * unnecessary deadlock risk. For this last reason we do not check
818  * yet the ownership of the partitions, which get added to the list to
819  * process. Ownership will be checked later on anyway.
820  */
821  if (include_parts)
822  {
823  List *part_oids = find_all_inheritors(relid, NoLock, NULL);
824  ListCell *part_lc;
825 
826  foreach(part_lc, part_oids)
827  {
828  Oid part_oid = lfirst_oid(part_lc);
829 
830  if (part_oid == relid)
831  continue; /* ignore original table */
832 
833  /*
834  * We omit a RangeVar since it wouldn't be appropriate to
835  * complain about failure to open one of these relations
836  * later.
837  */
838  oldcontext = MemoryContextSwitchTo(vac_context);
839  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
840  part_oid,
841  vrel->va_cols));
842  MemoryContextSwitchTo(oldcontext);
843  }
844  }
845 
846  /*
847  * Release lock again. This means that by the time we actually try to
848  * process the table, it might be gone or renamed. In the former case
849  * we'll silently ignore it; in the latter case we'll process it
850  * anyway, but we must beware that the RangeVar doesn't necessarily
851  * identify it anymore. This isn't ideal, perhaps, but there's little
852  * practical alternative, since we're typically going to commit this
853  * transaction and begin a new one between now and then. Moreover,
854  * holding locks on multiple relations would create significant risk
855  * of deadlock.
856  */
858  }
859 
860  return vacrels;
861 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
RangeVar * relation
Definition: parsenodes.h:3278
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:809
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:694
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
char * relname
Definition: primnodes.h:68
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define NoLock
Definition: lockdefs.h:34
static MemoryContext vac_context
Definition: vacuum.c:68
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:236
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3395
List * lappend(List *list, void *datum)
Definition: list.c:336
#define WARNING
Definition: elog.h:40
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:155
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
Definition: pg_inherits.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define VACOPT_SKIP_LOCKED
Definition: vacuum.h:183
#define elog(elevel,...)
Definition: elog.h:227
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, bits32 options)
Definition: vacuum.c:543
Definition: pg_list.h:50
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ get_all_vacuum_rels()

static List * get_all_vacuum_rels ( int  options)
static

Definition at line 868 of file vacuum.c.

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

Referenced by vacuum().

869 {
870  List *vacrels = NIL;
871  Relation pgclass;
872  TableScanDesc scan;
873  HeapTuple tuple;
874 
875  pgclass = table_open(RelationRelationId, AccessShareLock);
876 
877  scan = table_beginscan_catalog(pgclass, 0, NULL);
878 
879  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
880  {
881  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
882  MemoryContext oldcontext;
883  Oid relid = classForm->oid;
884 
885  /* check permissions of relation */
886  if (!vacuum_is_relation_owner(relid, classForm, options))
887  continue;
888 
889  /*
890  * We include partitioned tables here; depending on which operation is
891  * to be performed, caller will decide whether to process or ignore
892  * them.
893  */
894  if (classForm->relkind != RELKIND_RELATION &&
895  classForm->relkind != RELKIND_MATVIEW &&
896  classForm->relkind != RELKIND_PARTITIONED_TABLE)
897  continue;
898 
899  /*
900  * Build VacuumRelation(s) specifying the table OIDs to be processed.
901  * We omit a RangeVar since it wouldn't be appropriate to complain
902  * about failure to open one of these relations later.
903  */
904  oldcontext = MemoryContextSwitchTo(vac_context);
905  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
906  relid,
907  NIL));
908  MemoryContextSwitchTo(oldcontext);
909  }
910 
911  table_endscan(scan);
912  table_close(pgclass, AccessShareLock);
913 
914  return vacrels;
915 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition: makefuncs.c:809
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1316
static MemoryContext vac_context
Definition: vacuum.c:68
List * lappend(List *list, void *datum)
Definition: list.c:336
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, bits32 options)
Definition: vacuum.c:543
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50

◆ get_vacopt_ternary_value()

static VacOptTernaryValue get_vacopt_ternary_value ( DefElem def)
static

Definition at line 2164 of file vacuum.c.

References defGetBoolean(), VACOPT_TERNARY_DISABLED, and VACOPT_TERNARY_ENABLED.

Referenced by ExecVacuum().

2165 {
2167 }
bool defGetBoolean(DefElem *def)
Definition: define.c:111

◆ vac_close_indexes()

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

Definition at line 2037 of file vacuum.c.

References index_close(), and pfree().

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

2038 {
2039  if (Irel == NULL)
2040  return;
2041 
2042  while (nindexes--)
2043  {
2044  Relation ind = Irel[nindexes];
2045 
2046  index_close(ind, lockmode);
2047  }
2048  pfree(Irel);
2049 }
void pfree(void *pointer)
Definition: mcxt.c:1057
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158

◆ vac_estimate_reltuples()

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

Definition at line 1151 of file vacuum.c.

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

1155 {
1156  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1157  double old_rel_tuples = relation->rd_rel->reltuples;
1158  double old_density;
1159  double unscanned_pages;
1160  double total_tuples;
1161 
1162  /* If we did scan the whole table, just use the count as-is */
1163  if (scanned_pages >= total_pages)
1164  return scanned_tuples;
1165 
1166  /*
1167  * If scanned_pages is zero but total_pages isn't, keep the existing value
1168  * of reltuples. (Note: we might be returning -1 in this case.)
1169  */
1170  if (scanned_pages == 0)
1171  return old_rel_tuples;
1172 
1173  /*
1174  * If old density is unknown, we can't do much except scale up
1175  * scanned_tuples to match total_pages.
1176  */
1177  if (old_rel_tuples < 0 || old_rel_pages == 0)
1178  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1179 
1180  /*
1181  * Okay, we've covered the corner cases. The normal calculation is to
1182  * convert the old measurement to a density (tuples per page), then
1183  * estimate the number of tuples in the unscanned pages using that figure,
1184  * and finally add on the number of tuples in the scanned pages.
1185  */
1186  old_density = old_rel_tuples / old_rel_pages;
1187  unscanned_pages = (double) total_pages - (double) scanned_pages;
1188  total_tuples = old_density * unscanned_pages + scanned_tuples;
1189  return floor(total_tuples + 0.5);
1190 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:110

◆ vac_open_indexes()

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

Definition at line 1994 of file vacuum.c.

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

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

1996 {
1997  List *indexoidlist;
1998  ListCell *indexoidscan;
1999  int i;
2000 
2001  Assert(lockmode != NoLock);
2002 
2003  indexoidlist = RelationGetIndexList(relation);
2004 
2005  /* allocate enough memory for all indexes */
2006  i = list_length(indexoidlist);
2007 
2008  if (i > 0)
2009  *Irel = (Relation *) palloc(i * sizeof(Relation));
2010  else
2011  *Irel = NULL;
2012 
2013  /* collect just the ready indexes */
2014  i = 0;
2015  foreach(indexoidscan, indexoidlist)
2016  {
2017  Oid indexoid = lfirst_oid(indexoidscan);
2018  Relation indrel;
2019 
2020  indrel = index_open(indexoid, lockmode);
2021  if (indrel->rd_index->indisready)
2022  (*Irel)[i++] = indrel;
2023  else
2024  index_close(indrel, lockmode);
2025  }
2026 
2027  *nindexes = i;
2028 
2029  list_free(indexoidlist);
2030 }
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:175
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4526
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
void * palloc(Size size)
Definition: mcxt.c:950
void list_free(List *list)
Definition: list.c:1391
int i
Definition: pg_list.h:50
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ vac_truncate_clog()

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

Definition at line 1575 of file vacuum.c.

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

Referenced by vac_update_datfrozenxid().

1579 {
1581  Relation relation;
1582  TableScanDesc scan;
1583  HeapTuple tuple;
1584  Oid oldestxid_datoid;
1585  Oid minmulti_datoid;
1586  bool bogus = false;
1587  bool frozenAlreadyWrapped = false;
1588 
1589  /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
1590  LWLockAcquire(WrapLimitsVacuumLock, LW_EXCLUSIVE);
1591 
1592  /* init oldest datoids to sync with my frozenXID/minMulti values */
1593  oldestxid_datoid = MyDatabaseId;
1594  minmulti_datoid = MyDatabaseId;
1595 
1596  /*
1597  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1598  *
1599  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1600  * the values could change while we look at them. Fetch each one just
1601  * once to ensure sane behavior of the comparison logic. (Here, as in
1602  * many other places, we assume that fetching or updating an XID in shared
1603  * storage is atomic.)
1604  *
1605  * Note: we need not worry about a race condition with new entries being
1606  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1607  * existing DB's datfrozenxid, and that source DB cannot be ours because
1608  * of the interlock against copying a DB containing an active backend.
1609  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1610  * concurrently modify the datfrozenxid's of different databases, the
1611  * worst possible outcome is that pg_xact is not truncated as aggressively
1612  * as it could be.
1613  */
1614  relation = table_open(DatabaseRelationId, AccessShareLock);
1615 
1616  scan = table_beginscan_catalog(relation, 0, NULL);
1617 
1618  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1619  {
1620  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1621  TransactionId datfrozenxid = dbform->datfrozenxid;
1622  TransactionId datminmxid = dbform->datminmxid;
1623 
1624  Assert(TransactionIdIsNormal(datfrozenxid));
1625  Assert(MultiXactIdIsValid(datminmxid));
1626 
1627  /*
1628  * If things are working properly, no database should have a
1629  * datfrozenxid or datminmxid that is "in the future". However, such
1630  * cases have been known to arise due to bugs in pg_upgrade. If we
1631  * see any entries that are "in the future", chicken out and don't do
1632  * anything. This ensures we won't truncate clog before those
1633  * databases have been scanned and cleaned up. (We will issue the
1634  * "already wrapped" warning if appropriate, though.)
1635  */
1636  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1637  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1638  bogus = true;
1639 
1640  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1641  frozenAlreadyWrapped = true;
1642  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1643  {
1644  frozenXID = datfrozenxid;
1645  oldestxid_datoid = dbform->oid;
1646  }
1647 
1648  if (MultiXactIdPrecedes(datminmxid, minMulti))
1649  {
1650  minMulti = datminmxid;
1651  minmulti_datoid = dbform->oid;
1652  }
1653  }
1654 
1655  table_endscan(scan);
1656 
1657  table_close(relation, AccessShareLock);
1658 
1659  /*
1660  * Do not truncate CLOG if we seem to have suffered wraparound already;
1661  * the computed minimum XID might be bogus. This case should now be
1662  * impossible due to the defenses in GetNewTransactionId, but we keep the
1663  * test anyway.
1664  */
1665  if (frozenAlreadyWrapped)
1666  {
1667  ereport(WARNING,
1668  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1669  errdetail("You might have already suffered transaction-wraparound data loss.")));
1670  return;
1671  }
1672 
1673  /* chicken out if data is bogus in any other way */
1674  if (bogus)
1675  return;
1676 
1677  /*
1678  * Advance the oldest value for commit timestamps before truncating, so
1679  * that if a user requests a timestamp for a transaction we're truncating
1680  * away right after this point, they get NULL instead of an ugly "file not
1681  * found" error from slru.c. This doesn't matter for xact/multixact
1682  * because they are not subject to arbitrary lookups from users.
1683  */
1684  AdvanceOldestCommitTsXid(frozenXID);
1685 
1686  /*
1687  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1688  */
1689  TruncateCLOG(frozenXID, oldestxid_datoid);
1690  TruncateCommitTs(frozenXID);
1691  TruncateMultiXact(minMulti, minmulti_datoid);
1692 
1693  /*
1694  * Update the wrap limit for GetNewTransactionId and creation of new
1695  * MultiXactIds. Note: these functions will also signal the postmaster
1696  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1697  * signaling twice?
1698  */
1699  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1700  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1701 
1702  LWLockRelease(WrapLimitsVacuumLock);
1703 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition: clog.c:874
uint32 TransactionId
Definition: c.h:587
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define AccessShareLock
Definition: lockdefs.h:36
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:308
unsigned int Oid
Definition: postgres_ext.h:31
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:920
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1316
int errdetail(const char *fmt,...)
Definition: elog.c:1038
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:867
#define WARNING
Definition: elog.h:40
Oid MyDatabaseId
Definition: globals.c:86
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2210
TransactionId datfrozenxid
Definition: pg_database.h:62
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:345
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1206
TransactionId datminmxid
Definition: pg_database.h:65
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3156
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
int errmsg(const char *fmt,...)
Definition: elog.c:905
FormData_pg_database
Definition: pg_database.h:74
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:2941
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1361 of file vacuum.c.

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

Referenced by do_autovacuum(), and vacuum().

1362 {
1363  HeapTuple tuple;
1364  Form_pg_database dbform;
1365  Relation relation;
1366  SysScanDesc scan;
1367  HeapTuple classTup;
1368  TransactionId newFrozenXid;
1369  MultiXactId newMinMulti;
1370  TransactionId lastSaneFrozenXid;
1371  MultiXactId lastSaneMinMulti;
1372  bool bogus = false;
1373  bool dirty = false;
1374  ScanKeyData key[1];
1375 
1376  /*
1377  * Restrict this task to one backend per database. This avoids race
1378  * conditions that would move datfrozenxid or datminmxid backward. It
1379  * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1380  * datfrozenxid passed to an earlier vac_truncate_clog() call.
1381  */
1383 
1384  /*
1385  * Initialize the "min" calculation with
1386  * GetOldestNonRemovableTransactionId(), which is a reasonable
1387  * approximation to the minimum relfrozenxid for not-yet-committed
1388  * pg_class entries for new tables; see AddNewRelationTuple(). So we
1389  * cannot produce a wrong minimum by starting with this.
1390  */
1391  newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
1392 
1393  /*
1394  * Similarly, initialize the MultiXact "min" with the value that would be
1395  * used on pg_class for new tables. See AddNewRelationTuple().
1396  */
1397  newMinMulti = GetOldestMultiXactId();
1398 
1399  /*
1400  * Identify the latest relfrozenxid and relminmxid values that we could
1401  * validly see during the scan. These are conservative values, but it's
1402  * not really worth trying to be more exact.
1403  */
1404  lastSaneFrozenXid = ReadNextTransactionId();
1405  lastSaneMinMulti = ReadNextMultiXactId();
1406 
1407  /*
1408  * We must seqscan pg_class to find the minimum Xid, because there is no
1409  * index that can help us here.
1410  */
1411  relation = table_open(RelationRelationId, AccessShareLock);
1412 
1413  scan = systable_beginscan(relation, InvalidOid, false,
1414  NULL, 0, NULL);
1415 
1416  while ((classTup = systable_getnext(scan)) != NULL)
1417  {
1418  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1419 
1420  /*
1421  * Only consider relations able to hold unfrozen XIDs (anything else
1422  * should have InvalidTransactionId in relfrozenxid anyway).
1423  */
1424  if (classForm->relkind != RELKIND_RELATION &&
1425  classForm->relkind != RELKIND_MATVIEW &&
1426  classForm->relkind != RELKIND_TOASTVALUE)
1427  {
1428  Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1429  Assert(!MultiXactIdIsValid(classForm->relminmxid));
1430  continue;
1431  }
1432 
1433  /*
1434  * Some table AMs might not need per-relation xid / multixid horizons.
1435  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1436  * to not be set (i.e. set to their respective Invalid*Id)
1437  * independently. Thus validate and compute horizon for each only if
1438  * set.
1439  *
1440  * If things are working properly, no relation should have a
1441  * relfrozenxid or relminmxid that is "in the future". However, such
1442  * cases have been known to arise due to bugs in pg_upgrade. If we
1443  * see any entries that are "in the future", chicken out and don't do
1444  * anything. This ensures we won't truncate clog & multixact SLRUs
1445  * before those relations have been scanned and cleaned up.
1446  */
1447 
1448  if (TransactionIdIsValid(classForm->relfrozenxid))
1449  {
1450  Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1451 
1452  /* check for values in the future */
1453  if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1454  {
1455  bogus = true;
1456  break;
1457  }
1458 
1459  /* determine new horizon */
1460  if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1461  newFrozenXid = classForm->relfrozenxid;
1462  }
1463 
1464  if (MultiXactIdIsValid(classForm->relminmxid))
1465  {
1466  /* check for values in the future */
1467  if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1468  {
1469  bogus = true;
1470  break;
1471  }
1472 
1473  /* determine new horizon */
1474  if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1475  newMinMulti = classForm->relminmxid;
1476  }
1477  }
1478 
1479  /* we're done with pg_class */
1480  systable_endscan(scan);
1481  table_close(relation, AccessShareLock);
1482 
1483  /* chicken out if bogus data found */
1484  if (bogus)
1485  return;
1486 
1487  Assert(TransactionIdIsNormal(newFrozenXid));
1488  Assert(MultiXactIdIsValid(newMinMulti));
1489 
1490  /* Now fetch the pg_database tuple we need to update. */
1491  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1492 
1493  /*
1494  * Get the pg_database tuple to scribble on. Note that this does not
1495  * directly rely on the syscache to avoid issues with flattened toast
1496  * values for the in-place update.
1497  */
1498  ScanKeyInit(&key[0],
1499  Anum_pg_database_oid,
1500  BTEqualStrategyNumber, F_OIDEQ,
1502 
1503  scan = systable_beginscan(relation, DatabaseOidIndexId, true,
1504  NULL, 1, key);
1505  tuple = systable_getnext(scan);
1506  tuple = heap_copytuple(tuple);
1507  systable_endscan(scan);
1508 
1509  if (!HeapTupleIsValid(tuple))
1510  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1511 
1512  dbform = (Form_pg_database) GETSTRUCT(tuple);
1513 
1514  /*
1515  * As in vac_update_relstats(), we ordinarily don't want to let
1516  * datfrozenxid go backward; but if it's "in the future" then it must be
1517  * corrupt and it seems best to overwrite it.
1518  */
1519  if (dbform->datfrozenxid != newFrozenXid &&
1520  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1521  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1522  {
1523  dbform->datfrozenxid = newFrozenXid;
1524  dirty = true;
1525  }
1526  else
1527  newFrozenXid = dbform->datfrozenxid;
1528 
1529  /* Ditto for datminmxid */
1530  if (dbform->datminmxid != newMinMulti &&
1531  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1532  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1533  {
1534  dbform->datminmxid = newMinMulti;
1535  dirty = true;
1536  }
1537  else
1538  newMinMulti = dbform->datminmxid;
1539 
1540  if (dirty)
1541  heap_inplace_update(relation, tuple);
1542 
1543  heap_freetuple(tuple);
1544  table_close(relation, RowExclusiveLock);
1545 
1546  /*
1547  * If we were able to advance datfrozenxid or datminmxid, see if we can
1548  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1549  * XID-wrap-limit info is stale, since this action will update that too.
1550  */
1551  if (dirty || ForceTransactionIdLimitUpdate())
1552  vac_truncate_clog(newFrozenXid, newMinMulti,
1553  lastSaneFrozenXid, lastSaneMinMulti);
1554 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
uint32 TransactionId
Definition: c.h:587
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define ExclusiveLock
Definition: lockdefs.h:44
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition: lmgr.c:469
#define AccessShareLock
Definition: lockdefs.h:36
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:308
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:490
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:1939
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define RowExclusiveLock
Definition: lockdefs.h:38
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define DatabaseOidIndexId
Definition: pg_database.h:90
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2503
Oid MyDatabaseId
Definition: globals.c:86
#define InvalidOid
Definition: postgres_ext.h:36
TransactionId MultiXactId
Definition: c.h:597
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3156
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1575
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6013
#define elog(elevel,...)
Definition: elog.h:227
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723

◆ 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 1234 of file vacuum.c.

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

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

1240 {
1241  Oid relid = RelationGetRelid(relation);
1242  Relation rd;
1243  HeapTuple ctup;
1244  Form_pg_class pgcform;
1245  bool dirty;
1246 
1247  rd = table_open(RelationRelationId, RowExclusiveLock);
1248 
1249  /* Fetch a copy of the tuple to scribble on */
1251  if (!HeapTupleIsValid(ctup))
1252  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1253  relid);
1254  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1255 
1256  /* Apply statistical updates, if any, to copied tuple */
1257 
1258  dirty = false;
1259  if (pgcform->relpages != (int32) num_pages)
1260  {
1261  pgcform->relpages = (int32) num_pages;
1262  dirty = true;
1263  }
1264  if (pgcform->reltuples != (float4) num_tuples)
1265  {
1266  pgcform->reltuples = (float4) num_tuples;
1267  dirty = true;
1268  }
1269  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1270  {
1271  pgcform->relallvisible = (int32) num_all_visible_pages;
1272  dirty = true;
1273  }
1274 
1275  /* Apply DDL updates, but not inside an outer transaction (see above) */
1276 
1277  if (!in_outer_xact)
1278  {
1279  /*
1280  * If we didn't find any indexes, reset relhasindex.
1281  */
1282  if (pgcform->relhasindex && !hasindex)
1283  {
1284  pgcform->relhasindex = false;
1285  dirty = true;
1286  }
1287 
1288  /* We also clear relhasrules and relhastriggers if needed */
1289  if (pgcform->relhasrules && relation->rd_rules == NULL)
1290  {
1291  pgcform->relhasrules = false;
1292  dirty = true;
1293  }
1294  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1295  {
1296  pgcform->relhastriggers = false;
1297  dirty = true;
1298  }
1299  }
1300 
1301  /*
1302  * Update relfrozenxid, unless caller passed InvalidTransactionId
1303  * indicating it has no new data.
1304  *
1305  * Ordinarily, we don't let relfrozenxid go backwards: if things are
1306  * working correctly, the only way the new frozenxid could be older would
1307  * be if a previous VACUUM was done with a tighter freeze_min_age, in
1308  * which case we don't want to forget the work it already did. However,
1309  * if the stored relfrozenxid is "in the future", then it must be corrupt
1310  * and it seems best to overwrite it with the cutoff we used this time.
1311  * This should match vac_update_datfrozenxid() concerning what we consider
1312  * to be "in the future".
1313  */
1314  if (TransactionIdIsNormal(frozenxid) &&
1315  pgcform->relfrozenxid != frozenxid &&
1316  (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
1318  pgcform->relfrozenxid)))
1319  {
1320  pgcform->relfrozenxid = frozenxid;
1321  dirty = true;
1322  }
1323 
1324  /* Similarly for relminmxid */
1325  if (MultiXactIdIsValid(minmulti) &&
1326  pgcform->relminmxid != minmulti &&
1327  (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
1328  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
1329  {
1330  pgcform->relminmxid = minmulti;
1331  dirty = true;
1332  }
1333 
1334  /* If anything changed, write out the tuple. */
1335  if (dirty)
1336  heap_inplace_update(rd, ctup);
1337 
1339 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:308
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
TriggerDesc * trigdesc
Definition: rel.h:116
#define RowExclusiveLock
Definition: lockdefs.h:38
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
float float4
Definition: c.h:564
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RuleLock * rd_rules
Definition: rel.h:114
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3156
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:175
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6013
#define elog(elevel,...)
Definition: elog.h:227
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define RelationGetRelid(relation)
Definition: rel.h:457
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723

◆ vacuum()

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

Definition at line 275 of file vacuum.c.

References ActiveSnapshotSet(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze_rel(), Assert, BAS_VACUUM, CommandCounterIncrement(), 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, VacuumParams::options, PG_END_TRY, PG_FINALLY, 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_PROCESS_TOAST, VACOPT_VACUUM, vacuum_rel(), VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumCostDelay, VacuumPageDirty, VacuumPageHit, and VacuumPageMiss.

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

277 {
278  static bool in_vacuum = false;
279 
280  const char *stmttype;
281  volatile bool in_outer_xact,
282  use_own_xacts;
283 
284  Assert(params != NULL);
285 
286  stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
287 
288  /*
289  * We cannot run VACUUM inside a user transaction block; if we were inside
290  * a transaction, then our commit- and start-transaction-command calls
291  * would not have the intended effect! There are numerous other subtle
292  * dependencies on this, too.
293  *
294  * ANALYZE (without VACUUM) can run either way.
295  */
296  if (params->options & VACOPT_VACUUM)
297  {
298  PreventInTransactionBlock(isTopLevel, stmttype);
299  in_outer_xact = false;
300  }
301  else
302  in_outer_xact = IsInTransactionBlock(isTopLevel);
303 
304  /*
305  * Due to static variables vac_context, anl_context and vac_strategy,
306  * vacuum() is not reentrant. This matters when VACUUM FULL or ANALYZE
307  * calls a hostile index expression that itself calls ANALYZE.
308  */
309  if (in_vacuum)
310  ereport(ERROR,
311  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
312  errmsg("%s cannot be executed from VACUUM or ANALYZE",
313  stmttype)));
314 
315  /*
316  * Sanity check DISABLE_PAGE_SKIPPING option.
317  */
318  if ((params->options & VACOPT_FULL) != 0 &&
319  (params->options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
320  ereport(ERROR,
321  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
322  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
323 
324  /* sanity check for PROCESS_TOAST */
325  if ((params->options & VACOPT_FULL) != 0 &&
326  (params->options & VACOPT_PROCESS_TOAST) == 0)
327  ereport(ERROR,
328  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
329  errmsg("PROCESS_TOAST required with VACUUM FULL")));
330 
331  /*
332  * Send info about dead objects to the statistics collector, unless we are
333  * in autovacuum --- autovacuum.c does this for itself.
334  */
335  if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
337 
338  /*
339  * Create special memory context for cross-transaction storage.
340  *
341  * Since it is a child of PortalContext, it will go away eventually even
342  * if we suffer an error; there's no need for special abort cleanup logic.
343  */
345  "Vacuum",
347 
348  /*
349  * If caller didn't give us a buffer strategy object, make one in the
350  * cross-transaction memory context.
351  */
352  if (bstrategy == NULL)
353  {
355 
356  bstrategy = GetAccessStrategy(BAS_VACUUM);
357  MemoryContextSwitchTo(old_context);
358  }
359  vac_strategy = bstrategy;
360 
361  /*
362  * Build list of relation(s) to process, putting any new data in
363  * vac_context for safekeeping.
364  */
365  if (relations != NIL)
366  {
367  List *newrels = NIL;
368  ListCell *lc;
369 
370  foreach(lc, relations)
371  {
373  List *sublist;
374  MemoryContext old_context;
375 
376  sublist = expand_vacuum_rel(vrel, params->options);
377  old_context = MemoryContextSwitchTo(vac_context);
378  newrels = list_concat(newrels, sublist);
379  MemoryContextSwitchTo(old_context);
380  }
381  relations = newrels;
382  }
383  else
384  relations = get_all_vacuum_rels(params->options);
385 
386  /*
387  * Decide whether we need to start/commit our own transactions.
388  *
389  * For VACUUM (with or without ANALYZE): always do so, so that we can
390  * release locks as soon as possible. (We could possibly use the outer
391  * transaction for a one-table VACUUM, but handling TOAST tables would be
392  * problematic.)
393  *
394  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
395  * start/commit our own transactions. Also, there's no need to do so if
396  * only processing one relation. For multiple relations when not within a
397  * transaction block, and also in an autovacuum worker, use own
398  * transactions so we can release locks sooner.
399  */
400  if (params->options & VACOPT_VACUUM)
401  use_own_xacts = true;
402  else
403  {
404  Assert(params->options & VACOPT_ANALYZE);
406  use_own_xacts = true;
407  else if (in_outer_xact)
408  use_own_xacts = false;
409  else if (list_length(relations) > 1)
410  use_own_xacts = true;
411  else
412  use_own_xacts = false;
413  }
414 
415  /*
416  * vacuum_rel expects to be entered with no transaction active; it will
417  * start and commit its own transaction. But we are called by an SQL
418  * command, and so we are executing inside a transaction already. We
419  * commit the transaction started in PostgresMain() here, and start
420  * another one before exiting to match the commit waiting for us back in
421  * PostgresMain().
422  */
423  if (use_own_xacts)
424  {
425  Assert(!in_outer_xact);
426 
427  /* ActiveSnapshot is not set by autovacuum */
428  if (ActiveSnapshotSet())
430 
431  /* matches the StartTransaction in PostgresMain() */
433  }
434 
435  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
436  PG_TRY();
437  {
438  ListCell *cur;
439 
440  in_vacuum = true;
442  VacuumCostBalance = 0;
443  VacuumPageHit = 0;
444  VacuumPageMiss = 0;
445  VacuumPageDirty = 0;
448  VacuumActiveNWorkers = NULL;
449 
450  /*
451  * Loop to process each selected relation.
452  */
453  foreach(cur, relations)
454  {
456 
457  if (params->options & VACOPT_VACUUM)
458  {
459  if (!vacuum_rel(vrel->oid, vrel->relation, params))
460  continue;
461  }
462 
463  if (params->options & VACOPT_ANALYZE)
464  {
465  /*
466  * If using separate xacts, start one for analyze. Otherwise,
467  * we can use the outer transaction.
468  */
469  if (use_own_xacts)
470  {
472  /* functions in indexes may want a snapshot set */
474  }
475 
476  analyze_rel(vrel->oid, vrel->relation, params,
477  vrel->va_cols, in_outer_xact, vac_strategy);
478 
479  if (use_own_xacts)
480  {
483  }
484  else
485  {
486  /*
487  * If we're not using separate xacts, better separate the
488  * ANALYZE actions with CCIs. This avoids trouble if user
489  * says "ANALYZE t, t".
490  */
492  }
493  }
494  }
495  }
496  PG_FINALLY();
497  {
498  in_vacuum = false;
499  VacuumCostActive = false;
500  }
501  PG_END_TRY();
502 
503  /*
504  * Finish up processing.
505  */
506  if (use_own_xacts)
507  {
508  /* here, we are not in a transaction */
509 
510  /*
511  * This matches the CommitTransaction waiting for us in
512  * PostgresMain().
513  */
515  }
516 
517  if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
518  {
519  /*
520  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
521  * (autovacuum.c does this for itself.)
522  */
524  }
525 
526  /*
527  * Clean up working storage --- note we must do this after
528  * StartTransactionCommand, else we might be trying to delete the active
529  * context!
530  */
532  vac_context = NULL;
533 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:542
#define NIL
Definition: pg_list.h:65
void analyze_rel(Oid relid, RangeVar *relation, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:119
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
int64 VacuumPageMiss
Definition: globals.c:146
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:77
int VacuumCostBalance
Definition: globals.c:149
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition: vacuum.h:185
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
Definition: vacuum.c:1726
RangeVar * relation
Definition: parsenodes.h:3278
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1361
#define VACOPT_ANALYZE
Definition: vacuum.h:179
void CommitTransactionCommand(void)
Definition: xact.c:2939
int64 VacuumPageHit
Definition: globals.c:145
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:69
struct cursor * cur
Definition: ecpg.c:28
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
int errcode(int sqlerrcode)
Definition: elog.c:694
int64 VacuumPageDirty
Definition: globals.c:147
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
static List * expand_vacuum_rel(VacuumRelation *vrel, int options)
Definition: vacuum.c:729
#define VACOPT_PROCESS_TOAST
Definition: vacuum.h:184
MemoryContext PortalContext
Definition: mcxt.c:53
bits32 options
Definition: vacuum.h:208
#define ERROR
Definition: elog.h:45
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
static MemoryContext vac_context
Definition: vacuum.c:68
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3379
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:798
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3395
void pgstat_vacuum_stat(void)
Definition: pgstat.c:1091
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3492
int VacuumCostBalanceLocal
Definition: vacuum.c:78
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
#define PG_FINALLY()
Definition: elog.h:325
void CommandCounterIncrement(void)
Definition: xact.c:1021
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
#define VACOPT_FULL
Definition: vacuum.h:182
void StartTransactionCommand(void)
Definition: xact.c:2838
static int list_length(const List *l)
Definition: pg_list.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:905
double VacuumCostDelay
Definition: globals.c:143
#define PG_TRY()
Definition: elog.h:308
Definition: pg_list.h:50
#define PG_END_TRY()
Definition: elog.h:333
bool VacuumCostActive
Definition: globals.c:150
static List * get_all_vacuum_rels(int options)
Definition: vacuum.c:868

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 2058 of file vacuum.c.

References AutoVacuumUpdateDelay(), CHECK_FOR_INTERRUPTS, compute_parallel_delay(), InterruptPending, pg_usleep(), pgstat_report_wait_end(), pgstat_report_wait_start(), VacuumCostActive, VacuumCostBalance, VacuumCostDelay, VacuumCostLimit, and WAIT_EVENT_VACUUM_DELAY.

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

2059 {
2060  double msec = 0;
2061 
2062  /* Always check for interrupts */
2064 
2066  return;
2067 
2068  /*
2069  * For parallel vacuum, the delay is computed based on the shared cost
2070  * balance. See compute_parallel_delay.
2071  */
2072  if (VacuumSharedCostBalance != NULL)
2073  msec = compute_parallel_delay();
2074  else if (VacuumCostBalance >= VacuumCostLimit)
2076 
2077  /* Nap if appropriate */
2078  if (msec > 0)
2079  {
2080  if (msec > VacuumCostDelay * 4)
2081  msec = VacuumCostDelay * 4;
2082 
2084  pg_usleep((long) (msec * 1000));
2086 
2087  VacuumCostBalance = 0;
2088 
2089  /* update balance values for workers */
2091 
2092  /* Might have gotten an interrupt while sleeping */
2094  }
2095 }
static double compute_parallel_delay(void)
Definition: vacuum.c:2120
int VacuumCostBalance
Definition: globals.c:149
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:142
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1512
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1488
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
double VacuumCostDelay
Definition: globals.c:143
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1779
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
bool VacuumCostActive
Definition: globals.c:150

◆ vacuum_is_relation_owner()

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

Definition at line 543 of file vacuum.c.

References Assert, ereport, errmsg(), GetUserId(), MyDatabaseId, NameStr, pg_class_ownercheck(), pg_database_ownercheck(), relname, VACOPT_ANALYZE, VACOPT_VACUUM, and WARNING.

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

544 {
545  char *relname;
546 
548 
549  /*
550  * Check permissions.
551  *
552  * We allow the user to vacuum or analyze a table if he is superuser, the
553  * table owner, or the database owner (but in the latter case, only if
554  * it's not a shared relation). pg_class_ownercheck includes the
555  * superuser case.
556  *
557  * Note we choose to treat permissions failure as a WARNING and keep
558  * trying to vacuum or analyze the rest of the DB --- is this appropriate?
559  */
560  if (pg_class_ownercheck(relid, GetUserId()) ||
561  (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !reltuple->relisshared))
562  return true;
563 
564  relname = NameStr(reltuple->relname);
565 
566  if ((options & VACOPT_VACUUM) != 0)
567  {
568  if (reltuple->relisshared)
570  (errmsg("skipping \"%s\" --- only superuser can vacuum it",
571  relname)));
572  else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
574  (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
575  relname)));
576  else
578  (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
579  relname)));
580 
581  /*
582  * For VACUUM ANALYZE, both logs could show up, but just generate
583  * information for VACUUM as that would be the first one to be
584  * processed.
585  */
586  return false;
587  }
588 
589  if ((options & VACOPT_ANALYZE) != 0)
590  {
591  if (reltuple->relisshared)
593  (errmsg("skipping \"%s\" --- only superuser can analyze it",
594  relname)));
595  else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
597  (errmsg("skipping \"%s\" --- only superuser or database owner can analyze it",
598  relname)));
599  else
601  (errmsg("skipping \"%s\" --- only table or database owner can analyze it",
602  relname)));
603  }
604 
605  return false;
606 }
Oid GetUserId(void)
Definition: miscinit.c:478
#define VACOPT_ANALYZE
Definition: vacuum.h:179
NameData relname
Definition: pg_class.h:38
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:5124
#define WARNING
Definition: elog.h:40
Oid MyDatabaseId
Definition: globals.c:86
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4706
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define NameStr(name)
Definition: c.h:681

◆ vacuum_open_relation()

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

Definition at line 617 of file vacuum.c.

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

Referenced by analyze_rel(), and vacuum_rel().

619 {
620  Relation onerel;
621  bool rel_lock = true;
622  int elevel;
623 
625 
626  /*
627  * Open the relation and get the appropriate lock on it.
628  *
629  * There's a race condition here: the relation may have gone away since
630  * the last time we saw it. If so, we don't need to vacuum or analyze it.
631  *
632  * If we've been asked not to wait for the relation lock, acquire it first
633  * in non-blocking mode, before calling try_relation_open().
634  */
635  if (!(options & VACOPT_SKIP_LOCKED))
636  onerel = try_relation_open(relid, lmode);
637  else if (ConditionalLockRelationOid(relid, lmode))
638  onerel = try_relation_open(relid, NoLock);
639  else
640  {
641  onerel = NULL;
642  rel_lock = false;
643  }
644 
645  /* if relation is opened, leave */
646  if (onerel)
647  return onerel;
648 
649  /*
650  * Relation could not be opened, hence generate if possible a log
651  * informing on the situation.
652  *
653  * If the RangeVar is not defined, we do not have enough information to
654  * provide a meaningful log statement. Chances are that the caller has
655  * intentionally not provided this information so that this logging is
656  * skipped, anyway.
657  */
658  if (relation == NULL)
659  return NULL;
660 
661  /*
662  * Determine the log level.
663  *
664  * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
665  * statements in the permission checks; otherwise, only log if the caller
666  * so requested.
667  */
669  elevel = WARNING;
670  else if (verbose)
671  elevel = LOG;
672  else
673  return NULL;
674 
675  if ((options & VACOPT_VACUUM) != 0)
676  {
677  if (!rel_lock)
678  ereport(elevel,
679  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
680  errmsg("skipping vacuum of \"%s\" --- lock not available",
681  relation->relname)));
682  else
683  ereport(elevel,
685  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
686  relation->relname)));
687 
688  /*
689  * For VACUUM ANALYZE, both logs could show up, but just generate
690  * information for VACUUM as that would be the first one to be
691  * processed.
692  */
693  return NULL;
694  }
695 
696  if ((options & VACOPT_ANALYZE) != 0)
697  {
698  if (!rel_lock)
699  ereport(elevel,
700  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
701  errmsg("skipping analyze of \"%s\" --- lock not available",
702  relation->relname)));
703  else
704  ereport(elevel,
706  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
707  relation->relname)));
708  }
709 
710  return NULL;
711 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:75
#define VACOPT_ANALYZE
Definition: vacuum.h:179
int errcode(int sqlerrcode)
Definition: elog.c:694
#define LOG
Definition: elog.h:26
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:89
char * relname
Definition: primnodes.h:68
#define NoLock
Definition: lockdefs.h:34
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3395
static int verbose
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:333
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define VACOPT_SKIP_LOCKED
Definition: vacuum.h:183

◆ vacuum_rel()

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

Definition at line 1726 of file vacuum.c.

References AccessExclusiveLock, Assert, AtEOXact_GUC(), CHECK_FOR_INTERRUPTS, CLUOPT_VERBOSE, cluster_rel(), CommitTransactionCommand(), ereport, errmsg(), GetTransactionSnapshot(), GetUserIdAndSecContext(), VacuumParams::index_cleanup, InvalidOid, VacuumParams::is_wraparound, LockRelationIdForSession(), LockInfoData::lockRelId, VacuumParams::log_min_duration, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, NewGUCNestLevel(), NoLock, ClusterParams::options, VacuumParams::options, PGPROC::pgxactoff, PopActiveSnapshot(), PROC_IN_VACUUM, PROC_VACUUM_FOR_WRAPAROUND, ProcGlobal, PushActiveSnapshot(), RelationData::rd_lockInfo, RelationData::rd_options, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetRelationName, RelationGetRelid, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, StartTransactionCommand(), PGPROC::statusFlags, PROC_HDR::statusFlags, table_relation_vacuum(), VacuumParams::truncate, UnlockRelationIdForSession(), VACOPT_FULL, VACOPT_PROCESS_TOAST, VACOPT_TERNARY_DEFAULT, VACOPT_TERNARY_DISABLED, VACOPT_TERNARY_ENABLED, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum_is_relation_owner(), vacuum_open_relation(), and WARNING.

Referenced by vacuum().

1727 {
1728  LOCKMODE lmode;
1729  Relation onerel;
1730  LockRelId onerelid;
1731  Oid toast_relid;
1732  Oid save_userid;
1733  int save_sec_context;
1734  int save_nestlevel;
1735 
1736  Assert(params != NULL);
1737 
1738  /* Begin a transaction for vacuuming this relation */
1740 
1741  if (!(params->options & VACOPT_FULL))
1742  {
1743  /*
1744  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1745  * other concurrent VACUUMs know that they can ignore this one while
1746  * determining their OldestXmin. (The reason we don't set it during a
1747  * full VACUUM is exactly that we may have to run user-defined
1748  * functions for functional indexes, and we want to make sure that if
1749  * they use the snapshot set above, any tuples it requires can't get
1750  * removed from other tables. An index function that depends on the
1751  * contents of other tables is arguably broken, but we won't break it
1752  * here by violating transaction semantics.)
1753  *
1754  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1755  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1756  * in an emergency.
1757  *
1758  * Note: these flags remain set until CommitTransaction or
1759  * AbortTransaction. We don't want to clear them until we reset
1760  * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
1761  * might appear to go backwards, which is probably Not Good. (We also
1762  * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
1763  * xmin doesn't become visible ahead of setting the flag.)
1764  */
1765  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1767  if (params->is_wraparound)
1770  LWLockRelease(ProcArrayLock);
1771  }
1772 
1773  /*
1774  * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
1775  * cutoff xids in local memory wrapping around, and to have updated xmin
1776  * horizons.
1777  */
1779 
1780  /*
1781  * Check for user-requested abort. Note we want this to be inside a
1782  * transaction, so xact.c doesn't issue useless WARNING.
1783  */
1785 
1786  /*
1787  * Determine the type of lock we want --- hard exclusive lock for a FULL
1788  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1789  * way, we can be sure that no other backend is vacuuming the same table.
1790  */
1791  lmode = (params->options & VACOPT_FULL) ?
1793 
1794  /* open the relation and get the appropriate lock on it */
1795  onerel = vacuum_open_relation(relid, relation, params->options,
1796  params->log_min_duration >= 0, lmode);
1797 
1798  /* leave if relation could not be opened or locked */
1799  if (!onerel)
1800  {
1803  return false;
1804  }
1805 
1806  /*
1807  * Check if relation needs to be skipped based on ownership. This check
1808  * happens also when building the relation list to vacuum for a manual
1809  * operation, and needs to be done additionally here as VACUUM could
1810  * happen across multiple transactions where relation ownership could have
1811  * changed in-between. Make sure to only generate logs for VACUUM in this
1812  * case.
1813  */
1815  onerel->rd_rel,
1816  params->options & VACOPT_VACUUM))
1817  {
1818  relation_close(onerel, lmode);
1821  return false;
1822  }
1823 
1824  /*
1825  * Check that it's of a vacuumable relkind.
1826  */
1827  if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1828  onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1829  onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1830  onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1831  {
1832  ereport(WARNING,
1833  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1834  RelationGetRelationName(onerel))));
1835  relation_close(onerel, lmode);
1838  return false;
1839  }
1840 
1841  /*
1842  * Silently ignore tables that are temp tables of other backends ---
1843  * trying to vacuum these will lead to great unhappiness, since their
1844  * contents are probably not up-to-date on disk. (We don't throw a
1845  * warning here; it would just lead to chatter during a database-wide
1846  * VACUUM.)
1847  */
1848  if (RELATION_IS_OTHER_TEMP(onerel))
1849  {
1850  relation_close(onerel, lmode);
1853  return false;
1854  }
1855 
1856  /*
1857  * Silently ignore partitioned tables as there is no work to be done. The
1858  * useful work is on their child partitions, which have been queued up for
1859  * us separately.
1860  */
1861  if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1862  {
1863  relation_close(onerel, lmode);
1866  /* It's OK to proceed with ANALYZE on this table */
1867  return true;
1868  }
1869 
1870  /*
1871  * Get a session-level lock too. This will protect our access to the
1872  * relation across multiple transactions, so that we can vacuum the
1873  * relation's TOAST table (if any) secure in the knowledge that no one is
1874  * deleting the parent relation.
1875  *
1876  * NOTE: this cannot block, even if someone else is waiting for access,
1877  * because the lock manager knows that both lock requests are from the
1878  * same process.
1879  */
1880  onerelid = onerel->rd_lockInfo.lockRelId;
1881  LockRelationIdForSession(&onerelid, lmode);
1882 
1883  /* Set index cleanup option based on reloptions if not yet */
1884  if (params->index_cleanup == VACOPT_TERNARY_DEFAULT)
1885  {
1886  if (onerel->rd_options == NULL ||
1887  ((StdRdOptions *) onerel->rd_options)->vacuum_index_cleanup)
1889  else
1891  }
1892 
1893  /* Set truncate option based on reloptions if not yet */
1894  if (params->truncate == VACOPT_TERNARY_DEFAULT)
1895  {
1896  if (onerel->rd_options == NULL ||
1897  ((StdRdOptions *) onerel->rd_options)->vacuum_truncate)
1898  params->truncate = VACOPT_TERNARY_ENABLED;
1899  else
1901  }
1902 
1903  /*
1904  * Remember the relation's TOAST relation for later, if the caller asked
1905  * us to process it. In VACUUM FULL, though, the toast table is
1906  * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1907  */
1908  if ((params->options & VACOPT_PROCESS_TOAST) != 0 &&
1909  (params->options & VACOPT_FULL) == 0)
1910  toast_relid = onerel->rd_rel->reltoastrelid;
1911  else
1912  toast_relid = InvalidOid;
1913 
1914  /*
1915  * Switch to the table owner's userid, so that any index functions are run
1916  * as that user. Also lock down security-restricted operations and
1917  * arrange to make GUC variable changes local to this command. (This is
1918  * unnecessary, but harmless, for lazy VACUUM.)
1919  */
1920  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1921  SetUserIdAndSecContext(onerel->rd_rel->relowner,
1922  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1923  save_nestlevel = NewGUCNestLevel();
1924 
1925  /*
1926  * Do the actual work --- either FULL or "lazy" vacuum
1927  */
1928  if (params->options & VACOPT_FULL)
1929  {
1930  ClusterParams cluster_params = {0};
1931 
1932  /* close relation before vacuuming, but hold lock until commit */
1933  relation_close(onerel, NoLock);
1934  onerel = NULL;
1935 
1936  if ((params->options & VACOPT_VERBOSE) != 0)
1937  cluster_params.options |= CLUOPT_VERBOSE;
1938 
1939  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1940  cluster_rel(relid, InvalidOid, &cluster_params);
1941  }
1942  else
1943  table_relation_vacuum(onerel, params, vac_strategy);
1944 
1945  /* Roll back any GUC changes executed by index functions */
1946  AtEOXact_GUC(false, save_nestlevel);
1947 
1948  /* Restore userid and security context */
1949  SetUserIdAndSecContext(save_userid, save_sec_context);
1950 
1951  /* all done with this class, but hold lock until commit */
1952  if (onerel)
1953  relation_close(onerel, NoLock);
1954 
1955  /*
1956  * Complete the transaction and free all temporary memory used.
1957  */
1960 
1961  /*
1962  * If the relation has a secondary toast rel, vacuum that too while we
1963  * still hold the session lock on the main table. Note however that
1964  * "analyze" will not get done on the toast table. This is good, because
1965  * the toaster always uses hardcoded index access and statistics are
1966  * totally unimportant for toast relations.
1967  */
1968  if (toast_relid != InvalidOid)
1969  vacuum_rel(toast_relid, NULL, params);
1970 
1971  /*
1972  * Now release the session-level lock on the main table.
1973  */
1974  UnlockRelationIdForSession(&onerelid, lmode);
1975 
1976  /* Report that we really did it. */
1977  return true;
1978 }
LockRelId lockRelId
Definition: rel.h:44
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
Definition: vacuum.c:1726
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:300
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:590
int LOCKMODE
Definition: lockdefs.h:26
PGPROC * MyProc
Definition: proc.c:68
void CommitTransactionCommand(void)
Definition: xact.c:2939
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:60
static void table_relation_vacuum(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: tableam.h:1675
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:69
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define CLUOPT_VERBOSE
Definition: cluster.h:24
uint8 statusFlags
Definition: proc.h:189
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
Form_pg_class rd_rel
Definition: rel.h:110
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define VACOPT_PROCESS_TOAST
Definition: vacuum.h:184
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
bits32 options
Definition: vacuum.h:208
#define PROC_IN_VACUUM
Definition: proc.h:55
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:382
Definition: rel.h:36
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:369
bool is_wraparound
Definition: vacuum.h:215
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:113
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:583
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6019
#define RelationGetRelationName(relation)
Definition: rel.h:491
void cluster_rel(Oid tableOid, Oid indexOid, ClusterParams *params)
Definition: cluster.c:277
VacOptTernaryValue index_cleanup
Definition: vacuum.h:219
#define WARNING
Definition: elog.h:40
bits32 options
Definition: cluster.h:29
#define InvalidOid
Definition: postgres_ext.h:36
VacOptTernaryValue truncate
Definition: vacuum.h:221
#define ereport(elevel,...)
Definition: elog.h:155
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:804
#define VACOPT_VACUUM
Definition: vacuum.h:178
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:594
#define VACOPT_FULL
Definition: vacuum.h:182
void StartTransactionCommand(void)
Definition: xact.c:2838
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1206
int log_min_duration
Definition: vacuum.h:216
#define VACOPT_VERBOSE
Definition: vacuum.h:180
#define AccessExclusiveLock
Definition: lockdefs.h:45
int NewGUCNestLevel(void)
Definition: guc.c:6005
int errmsg(const char *fmt,...)
Definition: elog.c:905
int pgxactoff
Definition: proc.h:148
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
Relation vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:617
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, bits32 options)
Definition: vacuum.c:543
uint8 * statusFlags
Definition: proc.h:333
#define RelationGetRelid(relation)
Definition: rel.h:457
bytea * rd_options
Definition: rel.h:158

◆ 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 941 of file vacuum.c.

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg(), FirstMultiXactId, FirstNormalTransactionId, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), Min, MultiXactIdPrecedes(), MultiXactMemberFreezeThreshold(), OldSnapshotThresholdActive(), ReadNextMultiXactId(), ReadNextTransactionId(), SetOldSnapshotThresholdTimestamp(), 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_table_data(), and heap_vacuum_rel().

951 {
952  int freezemin;
953  int mxid_freezemin;
954  int effective_multixact_freeze_max_age;
955  TransactionId limit;
956  TransactionId safeLimit;
957  MultiXactId oldestMxact;
958  MultiXactId mxactLimit;
959  MultiXactId safeMxactLimit;
960 
961  /*
962  * We can always ignore processes running lazy vacuum. This is because we
963  * use these values only for deciding which tuples we must keep in the
964  * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
965  * XID assigned), it's safe to ignore it. In theory it could be
966  * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
967  * that only one vacuum process can be working on a particular table at
968  * any time, and that each vacuum is always an independent transaction.
969  */
970  *oldestXmin = GetOldestNonRemovableTransactionId(rel);
971 
973  {
974  TransactionId limit_xmin;
975  TimestampTz limit_ts;
976 
977  if (TransactionIdLimitedForOldSnapshots(*oldestXmin, rel,
978  &limit_xmin, &limit_ts))
979  {
980  /*
981  * TODO: We should only set the threshold if we are pruning on the
982  * basis of the increased limits. Not as crucial here as it is
983  * for opportunistic pruning (which often happens at a much higher
984  * frequency), but would still be a significant improvement.
985  */
986  SetOldSnapshotThresholdTimestamp(limit_ts, limit_xmin);
987  *oldestXmin = limit_xmin;
988  }
989  }
990 
991  Assert(TransactionIdIsNormal(*oldestXmin));
992 
993  /*
994  * Determine the minimum freeze age to use: as specified by the caller, or
995  * vacuum_freeze_min_age, but in any case not more than half
996  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
997  * wraparound won't occur too frequently.
998  */
999  freezemin = freeze_min_age;
1000  if (freezemin < 0)
1001  freezemin = vacuum_freeze_min_age;
1002  freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
1003  Assert(freezemin >= 0);
1004 
1005  /*
1006  * Compute the cutoff XID, being careful not to generate a "permanent" XID
1007  */
1008  limit = *oldestXmin - freezemin;
1009  if (!TransactionIdIsNormal(limit))
1010  limit = FirstNormalTransactionId;
1011 
1012  /*
1013  * If oldestXmin is very far back (in practice, more than
1014  * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
1015  * freeze age of zero.
1016  */
1018  if (!TransactionIdIsNormal(safeLimit))
1019  safeLimit = FirstNormalTransactionId;
1020 
1021  if (TransactionIdPrecedes(limit, safeLimit))
1022  {
1023  ereport(WARNING,
1024  (errmsg("oldest xmin is far in the past"),
1025  errhint("Close open transactions soon to avoid wraparound problems.\n"
1026  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1027  limit = *oldestXmin;
1028  }
1029 
1030  *freezeLimit = limit;
1031 
1032  /*
1033  * Compute the multixact age for which freezing is urgent. This is
1034  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1035  * short of multixact member space.
1036  */
1037  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1038 
1039  /*
1040  * Determine the minimum multixact freeze age to use: as specified by
1041  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1042  * than half effective_multixact_freeze_max_age, so that autovacuums to
1043  * prevent MultiXact wraparound won't occur too frequently.
1044  */
1045  mxid_freezemin = multixact_freeze_min_age;
1046  if (mxid_freezemin < 0)
1047  mxid_freezemin = vacuum_multixact_freeze_min_age;
1048  mxid_freezemin = Min(mxid_freezemin,
1049  effective_multixact_freeze_max_age / 2);
1050  Assert(mxid_freezemin >= 0);
1051 
1052  /* compute the cutoff multi, being careful to generate a valid value */
1053  oldestMxact = GetOldestMultiXactId();
1054  mxactLimit = oldestMxact - mxid_freezemin;
1055  if (mxactLimit < FirstMultiXactId)
1056  mxactLimit = FirstMultiXactId;
1057 
1058  safeMxactLimit =
1059  ReadNextMultiXactId() - effective_multixact_freeze_max_age;
1060  if (safeMxactLimit < FirstMultiXactId)
1061  safeMxactLimit = FirstMultiXactId;
1062 
1063  if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
1064  {
1065  ereport(WARNING,
1066  (errmsg("oldest multixact is far in the past"),
1067  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
1068  /* Use the safe limit, unless an older mxact is still running */
1069  if (MultiXactIdPrecedes(oldestMxact, safeMxactLimit))
1070  mxactLimit = oldestMxact;
1071  else
1072  mxactLimit = safeMxactLimit;
1073  }
1074 
1075  *multiXactCutoff = mxactLimit;
1076 
1077  if (xidFullScanLimit != NULL)
1078  {
1079  int freezetable;
1080 
1081  Assert(mxactFullScanLimit != NULL);
1082 
1083  /*
1084  * Determine the table freeze age to use: as specified by the caller,
1085  * or vacuum_freeze_table_age, but in any case not more than
1086  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1087  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
1088  * before anti-wraparound autovacuum is launched.
1089  */
1090  freezetable = freeze_table_age;
1091  if (freezetable < 0)
1092  freezetable = vacuum_freeze_table_age;
1093  freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
1094  Assert(freezetable >= 0);
1095 
1096  /*
1097  * Compute XID limit causing a full-table vacuum, being careful not to
1098  * generate a "permanent" XID.
1099  */
1100  limit = ReadNextTransactionId() - freezetable;
1101  if (!TransactionIdIsNormal(limit))
1102  limit = FirstNormalTransactionId;
1103 
1104  *xidFullScanLimit = limit;
1105 
1106  /*
1107  * Similar to the above, determine the table freeze age to use for
1108  * multixacts: as specified by the caller, or
1109  * vacuum_multixact_freeze_table_age, but in any case not more than
1110  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
1111  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
1112  * freeze multixacts before anti-wraparound autovacuum is launched.
1113  */
1114  freezetable = multixact_freeze_table_age;
1115  if (freezetable < 0)
1116  freezetable = vacuum_multixact_freeze_table_age;
1117  freezetable = Min(freezetable,
1118  effective_multixact_freeze_max_age * 0.95);
1119  Assert(freezetable >= 0);
1120 
1121  /*
1122  * Compute MultiXact limit causing a full-table vacuum, being careful
1123  * to generate a valid MultiXact value.
1124  */
1125  mxactLimit = ReadNextMultiXactId() - freezetable;
1126  if (mxactLimit < FirstMultiXactId)
1127  mxactLimit = FirstMultiXactId;
1128 
1129  *mxactFullScanLimit = mxactLimit;
1130  }
1131  else
1132  {
1133  Assert(mxactFullScanLimit == NULL);
1134  }
1135 }
int errhint(const char *fmt,...)
Definition: elog.c:1152
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:64
uint32 TransactionId
Definition: c.h:587
int64 TimestampTz
Definition: timestamp.h:39
static bool OldSnapshotThresholdActive(void)
Definition: snapmgr.h:102
#define Min(x, y)
Definition: c.h:986
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:308
bool TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation, TransactionId *limit_xid, TimestampTz *limit_ts)
Definition: snapmgr.c:1751
void SetOldSnapshotThresholdTimestamp(TimestampTz ts, TransactionId xlimit)
Definition: snapmgr.c:1672
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:1939
#define FirstNormalTransactionId
Definition: transam.h:34
int autovacuum_freeze_max_age
Definition: autovacuum.c:124
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:63
#define FirstMultiXactId
Definition: multixact.h:25
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2825
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define WARNING
Definition: elog.h:40
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2503
#define ereport(elevel,...)
Definition: elog.h:155
TransactionId MultiXactId
Definition: c.h:597
#define Assert(condition)
Definition: c.h:804
int vacuum_freeze_min_age
Definition: vacuum.c:61
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3156
int vacuum_freeze_table_age
Definition: vacuum.c:62
int errmsg(const char *fmt,...)
Definition: elog.c:905
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723

Variable Documentation

◆ vac_context

MemoryContext vac_context = NULL
static

Definition at line 68 of file vacuum.c.

◆ vac_strategy

BufferAccessStrategy vac_strategy
static

Definition at line 69 of file vacuum.c.

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 61 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 62 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 63 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 64 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ VacuumActiveNWorkers

pg_atomic_uint32* VacuumActiveNWorkers = NULL

◆ VacuumCostBalanceLocal

int VacuumCostBalanceLocal = 0

◆ VacuumSharedCostBalance

pg_atomic_uint32* VacuumSharedCostBalance = NULL

Definition at line 76 of file vacuum.c.

Referenced by lazy_parallel_vacuum_indexes(), and parallel_vacuum_main().