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, int options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, int 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 2057 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().

2058 {
2059  double msec = 0;
2060  uint32 shared_balance;
2061  int nworkers;
2062 
2063  /* Parallel vacuum must be active */
2065 
2067 
2068  /* At least count itself */
2069  Assert(nworkers >= 1);
2070 
2071  /* Update the shared cost balance value atomically */
2073 
2074  /* Compute the total local balance for the current worker */
2076 
2077  if ((shared_balance >= VacuumCostLimit) &&
2078  (VacuumCostBalanceLocal > 0.5 * (VacuumCostLimit / nworkers)))
2079  {
2080  /* Compute sleep time based on the local cost balance */
2084  }
2085 
2086  /*
2087  * Reset the local balance as we accumulated it into the shared value.
2088  */
2089  VacuumCostBalance = 0;
2090 
2091  return msec;
2092 }
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:77
int VacuumCostBalance
Definition: globals.c:147
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:140
unsigned int uint32
Definition: c.h:359
int VacuumCostBalanceLocal
Definition: vacuum.c:78
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
#define Assert(condition)
Definition: c.h:739
double VacuumCostDelay
Definition: globals.c:141
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_SKIP_LOCKED, VACOPT_SKIPTOAST, 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 parallel_option = false;
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, "truncate") == 0)
145  params.truncate = get_vacopt_ternary_value(opt);
146  else if (strcmp(opt->defname, "parallel") == 0)
147  {
148  parallel_option = true;
149  if (opt->arg == NULL)
150  {
151  ereport(ERROR,
152  (errcode(ERRCODE_SYNTAX_ERROR),
153  errmsg("parallel option requires a value between 0 and %d",
155  parser_errposition(pstate, opt->location)));
156  }
157  else
158  {
159  int nworkers;
160 
161  nworkers = defGetInt32(opt);
162  if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
163  ereport(ERROR,
164  (errcode(ERRCODE_SYNTAX_ERROR),
165  errmsg("parallel vacuum degree must be between 0 and %d",
166  MAX_PARALLEL_WORKER_LIMIT),
167  parser_errposition(pstate, opt->location)));
168 
169  /*
170  * Disable parallel vacuum, if user has specified parallel
171  * degree as zero.
172  */
173  if (nworkers == 0)
174  params.nworkers = -1;
175  else
176  params.nworkers = nworkers;
177  }
178  }
179  else
180  ereport(ERROR,
181  (errcode(ERRCODE_SYNTAX_ERROR),
182  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
183  parser_errposition(pstate, opt->location)));
184  }
185 
186  /* Set vacuum options */
187  params.options =
188  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
189  (verbose ? VACOPT_VERBOSE : 0) |
190  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
191  (analyze ? VACOPT_ANALYZE : 0) |
192  (freeze ? VACOPT_FREEZE : 0) |
193  (full ? VACOPT_FULL : 0) |
194  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0);
195 
196  /* sanity checks on options */
197  Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
198  Assert((params.options & VACOPT_VACUUM) ||
199  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
200  Assert(!(params.options & VACOPT_SKIPTOAST));
201 
202  if ((params.options & VACOPT_FULL) && parallel_option)
203  ereport(ERROR,
204  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
205  errmsg("cannot specify both FULL and PARALLEL options")));
206 
207  /*
208  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
209  */
210  if (!(params.options & VACOPT_ANALYZE))
211  {
212  ListCell *lc;
213 
214  foreach(lc, vacstmt->rels)
215  {
217 
218  if (vrel->va_cols != NIL)
219  ereport(ERROR,
220  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
221  errmsg("ANALYZE option must be specified when a column list is provided")));
222  }
223  }
224 
225  /*
226  * All freeze ages are zero if the FREEZE option is given; otherwise pass
227  * them as -1 which means to use the default values.
228  */
229  if (params.options & VACOPT_FREEZE)
230  {
231  params.freeze_min_age = 0;
232  params.freeze_table_age = 0;
233  params.multixact_freeze_min_age = 0;
234  params.multixact_freeze_table_age = 0;
235  }
236  else
237  {
238  params.freeze_min_age = -1;
239  params.freeze_table_age = -1;
240  params.multixact_freeze_min_age = -1;
241  params.multixact_freeze_table_age = -1;
242  }
243 
244  /* user-invoked vacuum is never "for wraparound" */
245  params.is_wraparound = false;
246 
247  /* user-invoked vacuum never uses this parameter */
248  params.log_min_duration = -1;
249 
250  /* Now go through the common routine */
251  vacuum(vacstmt->rels, &params, NULL, isTopLevel);
252 }
#define NIL
Definition: pg_list.h:65
int multixact_freeze_table_age
Definition: vacuum.h:215
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:274
bool is_vacuumcmd
Definition: parsenodes.h:3211
int32 defGetInt32(DefElem *def)
Definition: define.c:166
int errcode(int sqlerrcode)
Definition: elog.c:608
int freeze_table_age
Definition: vacuum.h:212
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
int freeze_min_age
Definition: vacuum.h:211
bool is_wraparound
Definition: vacuum.h:217
#define lfirst_node(type, lc)
Definition: pg_list.h:193
int location
Definition: parsenodes.h:733
#define ereport(elevel, rest)
Definition: elog.h:141
VacOptTernaryValue index_cleanup
Definition: vacuum.h:221
Node * arg
Definition: parsenodes.h:731
static int verbose
VacOptTernaryValue truncate
Definition: vacuum.h:223
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
int nworkers
Definition: vacuum.h:231
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
int log_min_duration
Definition: vacuum.h:218
int errmsg(const char *fmt,...)
Definition: elog.c:822
List * options
Definition: parsenodes.h:3209
int options
Definition: vacuum.h:210
char * defname
Definition: parsenodes.h:730
#define MAX_PARALLEL_WORKER_LIMIT
int multixact_freeze_min_age
Definition: vacuum.h:213
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:2816
List * rels
Definition: parsenodes.h:3210
static VacOptTernaryValue get_vacopt_ternary_value(DefElem *def)
Definition: vacuum.c:2101

◆ expand_vacuum_rel()

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

Definition at line 721 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().

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

◆ get_all_vacuum_rels()

static List * get_all_vacuum_rels ( int  options)
static

Definition at line 860 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().

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

References defGetBoolean(), VACOPT_TERNARY_DISABLED, and VACOPT_TERNARY_ENABLED.

Referenced by ExecVacuum().

2102 {
2104 }
bool defGetBoolean(DefElem *def)
Definition: define.c:111

◆ vac_close_indexes()

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

Definition at line 1976 of file vacuum.c.

References index_close(), and pfree().

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

1977 {
1978  if (Irel == NULL)
1979  return;
1980 
1981  while (nindexes--)
1982  {
1983  Relation ind = Irel[nindexes];
1984 
1985  index_close(ind, lockmode);
1986  }
1987  pfree(Irel);
1988 }
void pfree(void *pointer)
Definition: mcxt.c:1056
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:152

◆ vac_estimate_reltuples()

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

Definition at line 1123 of file vacuum.c.

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

1127 {
1128  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1129  double old_rel_tuples = relation->rd_rel->reltuples;
1130  double old_density;
1131  double unscanned_pages;
1132  double total_tuples;
1133 
1134  /* If we did scan the whole table, just use the count as-is */
1135  if (scanned_pages >= total_pages)
1136  return scanned_tuples;
1137 
1138  /*
1139  * If scanned_pages is zero but total_pages isn't, keep the existing value
1140  * of reltuples. (Note: callers should avoid updating the pg_class
1141  * statistics in this situation, since no new information has been
1142  * provided.)
1143  */
1144  if (scanned_pages == 0)
1145  return old_rel_tuples;
1146 
1147  /*
1148  * If old value of relpages is zero, old density is indeterminate; we
1149  * can't do much except scale up scanned_tuples to match total_pages.
1150  */
1151  if (old_rel_pages == 0)
1152  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1153 
1154  /*
1155  * Okay, we've covered the corner cases. The normal calculation is to
1156  * convert the old measurement to a density (tuples per page), then
1157  * estimate the number of tuples in the unscanned pages using that figure,
1158  * and finally add on the number of tuples in the scanned pages.
1159  */
1160  old_density = old_rel_tuples / old_rel_pages;
1161  unscanned_pages = (double) total_pages - (double) scanned_pages;
1162  total_tuples = old_density * unscanned_pages + scanned_tuples;
1163  return floor(total_tuples + 0.5);
1164 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:84

◆ vac_open_indexes()

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

Definition at line 1933 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().

1935 {
1936  List *indexoidlist;
1937  ListCell *indexoidscan;
1938  int i;
1939 
1940  Assert(lockmode != NoLock);
1941 
1942  indexoidlist = RelationGetIndexList(relation);
1943 
1944  /* allocate enough memory for all indexes */
1945  i = list_length(indexoidlist);
1946 
1947  if (i > 0)
1948  *Irel = (Relation *) palloc(i * sizeof(Relation));
1949  else
1950  *Irel = NULL;
1951 
1952  /* collect just the ready indexes */
1953  i = 0;
1954  foreach(indexoidscan, indexoidlist)
1955  {
1956  Oid indexoid = lfirst_oid(indexoidscan);
1957  Relation indrel;
1958 
1959  indrel = index_open(indexoid, lockmode);
1960  if (indrel->rd_index->indisready)
1961  (*Irel)[i++] = indrel;
1962  else
1963  index_close(indrel, lockmode);
1964  }
1965 
1966  *nindexes = i;
1967 
1968  list_free(indexoidlist);
1969 }
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:149
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:739
static int list_length(const List *l)
Definition: pg_list.h:169
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4330
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:152
void * palloc(Size size)
Definition: mcxt.c:949
void list_free(List *list)
Definition: list.c:1377
int i
Definition: pg_list.h:50
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:126
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ vac_truncate_clog()

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

Definition at line 1524 of file vacuum.c.

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

Referenced by vac_update_datfrozenxid().

1528 {
1529  TransactionId nextXID = ReadNewTransactionId();
1530  Relation relation;
1531  TableScanDesc scan;
1532  HeapTuple tuple;
1533  Oid oldestxid_datoid;
1534  Oid minmulti_datoid;
1535  bool bogus = false;
1536  bool frozenAlreadyWrapped = false;
1537 
1538  /* init oldest datoids to sync with my frozenXID/minMulti values */
1539  oldestxid_datoid = MyDatabaseId;
1540  minmulti_datoid = MyDatabaseId;
1541 
1542  /*
1543  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1544  *
1545  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1546  * the values could change while we look at them. Fetch each one just
1547  * once to ensure sane behavior of the comparison logic. (Here, as in
1548  * many other places, we assume that fetching or updating an XID in shared
1549  * storage is atomic.)
1550  *
1551  * Note: we need not worry about a race condition with new entries being
1552  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1553  * existing DB's datfrozenxid, and that source DB cannot be ours because
1554  * of the interlock against copying a DB containing an active backend.
1555  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1556  * concurrently modify the datfrozenxid's of different databases, the
1557  * worst possible outcome is that pg_xact is not truncated as aggressively
1558  * as it could be.
1559  */
1560  relation = table_open(DatabaseRelationId, AccessShareLock);
1561 
1562  scan = table_beginscan_catalog(relation, 0, NULL);
1563 
1564  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1565  {
1566  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1567  TransactionId datfrozenxid = dbform->datfrozenxid;
1568  TransactionId datminmxid = dbform->datminmxid;
1569 
1570  Assert(TransactionIdIsNormal(datfrozenxid));
1571  Assert(MultiXactIdIsValid(datminmxid));
1572 
1573  /*
1574  * If things are working properly, no database should have a
1575  * datfrozenxid or datminmxid that is "in the future". However, such
1576  * cases have been known to arise due to bugs in pg_upgrade. If we
1577  * see any entries that are "in the future", chicken out and don't do
1578  * anything. This ensures we won't truncate clog before those
1579  * databases have been scanned and cleaned up. (We will issue the
1580  * "already wrapped" warning if appropriate, though.)
1581  */
1582  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1583  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1584  bogus = true;
1585 
1586  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1587  frozenAlreadyWrapped = true;
1588  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1589  {
1590  frozenXID = datfrozenxid;
1591  oldestxid_datoid = dbform->oid;
1592  }
1593 
1594  if (MultiXactIdPrecedes(datminmxid, minMulti))
1595  {
1596  minMulti = datminmxid;
1597  minmulti_datoid = dbform->oid;
1598  }
1599  }
1600 
1601  table_endscan(scan);
1602 
1603  table_close(relation, AccessShareLock);
1604 
1605  /*
1606  * Do not truncate CLOG if we seem to have suffered wraparound already;
1607  * the computed minimum XID might be bogus. This case should now be
1608  * impossible due to the defenses in GetNewTransactionId, but we keep the
1609  * test anyway.
1610  */
1611  if (frozenAlreadyWrapped)
1612  {
1613  ereport(WARNING,
1614  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1615  errdetail("You might have already suffered transaction-wraparound data loss.")));
1616  return;
1617  }
1618 
1619  /* chicken out if data is bogus in any other way */
1620  if (bogus)
1621  return;
1622 
1623  /*
1624  * Advance the oldest value for commit timestamps before truncating, so
1625  * that if a user requests a timestamp for a transaction we're truncating
1626  * away right after this point, they get NULL instead of an ugly "file not
1627  * found" error from slru.c. This doesn't matter for xact/multixact
1628  * because they are not subject to arbitrary lookups from users.
1629  */
1630  AdvanceOldestCommitTsXid(frozenXID);
1631 
1632  /*
1633  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1634  */
1635  TruncateCLOG(frozenXID, oldestxid_datoid);
1636  TruncateCommitTs(frozenXID);
1637  TruncateMultiXact(minMulti, minmulti_datoid);
1638 
1639  /*
1640  * Update the wrap limit for GetNewTransactionId and creation of new
1641  * MultiXactIds. Note: these functions will also signal the postmaster
1642  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1643  * signalling twice?
1644  */
1645  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1646  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1647 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition: clog.c:899
uint32 TransactionId
Definition: c.h:514
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:98
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:875
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
int errdetail(const char *fmt,...)
Definition: elog.c:955
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
#define ereport(elevel, rest)
Definition: elog.h:141
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:822
#define WARNING
Definition: elog.h:40
Oid MyDatabaseId
Definition: globals.c:85
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2196
TransactionId datfrozenxid
Definition: pg_database.h:62
#define Assert(condition)
Definition: c.h:739
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:330
TransactionId datminmxid
Definition: pg_database.h:65
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3142
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:849
int errmsg(const char *fmt,...)
Definition: elog.c:822
FormData_pg_database
Definition: pg_database.h:74
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:2935
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1335 of file vacuum.c.

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

Referenced by do_autovacuum(), and vacuum().

1336 {
1337  HeapTuple tuple;
1338  Form_pg_database dbform;
1339  Relation relation;
1340  SysScanDesc scan;
1341  HeapTuple classTup;
1342  TransactionId newFrozenXid;
1343  MultiXactId newMinMulti;
1344  TransactionId lastSaneFrozenXid;
1345  MultiXactId lastSaneMinMulti;
1346  bool bogus = false;
1347  bool dirty = false;
1348 
1349  /*
1350  * Initialize the "min" calculation with GetOldestXmin, which is a
1351  * reasonable approximation to the minimum relfrozenxid for not-yet-
1352  * committed pg_class entries for new tables; see AddNewRelationTuple().
1353  * So we cannot produce a wrong minimum by starting with this.
1354  */
1355  newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
1356 
1357  /*
1358  * Similarly, initialize the MultiXact "min" with the value that would be
1359  * used on pg_class for new tables. See AddNewRelationTuple().
1360  */
1361  newMinMulti = GetOldestMultiXactId();
1362 
1363  /*
1364  * Identify the latest relfrozenxid and relminmxid values that we could
1365  * validly see during the scan. These are conservative values, but it's
1366  * not really worth trying to be more exact.
1367  */
1368  lastSaneFrozenXid = ReadNewTransactionId();
1369  lastSaneMinMulti = ReadNextMultiXactId();
1370 
1371  /*
1372  * We must seqscan pg_class to find the minimum Xid, because there is no
1373  * index that can help us here.
1374  */
1375  relation = table_open(RelationRelationId, AccessShareLock);
1376 
1377  scan = systable_beginscan(relation, InvalidOid, false,
1378  NULL, 0, NULL);
1379 
1380  while ((classTup = systable_getnext(scan)) != NULL)
1381  {
1382  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1383 
1384  /*
1385  * Only consider relations able to hold unfrozen XIDs (anything else
1386  * should have InvalidTransactionId in relfrozenxid anyway).
1387  */
1388  if (classForm->relkind != RELKIND_RELATION &&
1389  classForm->relkind != RELKIND_MATVIEW &&
1390  classForm->relkind != RELKIND_TOASTVALUE)
1391  {
1392  Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1393  Assert(!MultiXactIdIsValid(classForm->relminmxid));
1394  continue;
1395  }
1396 
1397  /*
1398  * Some table AMs might not need per-relation xid / multixid horizons.
1399  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1400  * to not be set (i.e. set to their respective Invalid*Id)
1401  * independently. Thus validate and compute horizon for each only if
1402  * set.
1403  *
1404  * If things are working properly, no relation should have a
1405  * relfrozenxid or relminmxid that is "in the future". However, such
1406  * cases have been known to arise due to bugs in pg_upgrade. If we
1407  * see any entries that are "in the future", chicken out and don't do
1408  * anything. This ensures we won't truncate clog & multixact SLRUs
1409  * before those relations have been scanned and cleaned up.
1410  */
1411 
1412  if (TransactionIdIsValid(classForm->relfrozenxid))
1413  {
1414  Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1415 
1416  /* check for values in the future */
1417  if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1418  {
1419  bogus = true;
1420  break;
1421  }
1422 
1423  /* determine new horizon */
1424  if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1425  newFrozenXid = classForm->relfrozenxid;
1426  }
1427 
1428  if (MultiXactIdIsValid(classForm->relminmxid))
1429  {
1430  /* check for values in the future */
1431  if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1432  {
1433  bogus = true;
1434  break;
1435  }
1436 
1437  /* determine new horizon */
1438  if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1439  newMinMulti = classForm->relminmxid;
1440  }
1441  }
1442 
1443  /* we're done with pg_class */
1444  systable_endscan(scan);
1445  table_close(relation, AccessShareLock);
1446 
1447  /* chicken out if bogus data found */
1448  if (bogus)
1449  return;
1450 
1451  Assert(TransactionIdIsNormal(newFrozenXid));
1452  Assert(MultiXactIdIsValid(newMinMulti));
1453 
1454  /* Now fetch the pg_database tuple we need to update. */
1455  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1456 
1457  /* Fetch a copy of the tuple to scribble on */
1459  if (!HeapTupleIsValid(tuple))
1460  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1461  dbform = (Form_pg_database) GETSTRUCT(tuple);
1462 
1463  /*
1464  * As in vac_update_relstats(), we ordinarily don't want to let
1465  * datfrozenxid go backward; but if it's "in the future" then it must be
1466  * corrupt and it seems best to overwrite it.
1467  */
1468  if (dbform->datfrozenxid != newFrozenXid &&
1469  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1470  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1471  {
1472  dbform->datfrozenxid = newFrozenXid;
1473  dirty = true;
1474  }
1475  else
1476  newFrozenXid = dbform->datfrozenxid;
1477 
1478  /* Ditto for datminmxid */
1479  if (dbform->datminmxid != newMinMulti &&
1480  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1481  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1482  {
1483  dbform->datminmxid = newMinMulti;
1484  dirty = true;
1485  }
1486  else
1487  newMinMulti = dbform->datminmxid;
1488 
1489  if (dirty)
1490  heap_inplace_update(relation, tuple);
1491 
1492  heap_freetuple(tuple);
1493  table_close(relation, RowExclusiveLock);
1494 
1495  /*
1496  * If we were able to advance datfrozenxid or datminmxid, see if we can
1497  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1498  * XID-wrap-limit info is stale, since this action will update that too.
1499  */
1500  if (dirty || ForceTransactionIdLimitUpdate())
1501  vac_truncate_clog(newFrozenXid, newMinMulti,
1502  lastSaneFrozenXid, lastSaneMinMulti);
1503 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
uint32 TransactionId
Definition: c.h:514
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define AccessShareLock
Definition: lockdefs.h:36
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
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:352
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:471
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2493
Oid MyDatabaseId
Definition: globals.c:85
#define InvalidOid
Definition: postgres_ext.h:36
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1306
TransactionId MultiXactId
Definition: c.h:524
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3142
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1524
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:5707
#define elog(elevel,...)
Definition: elog.h:228
#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
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 1208 of file vacuum.c.

References elog, ERROR, GETSTRUCT, heap_inplace_update(), HeapTupleIsValid, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum, RelationData::rd_rules, ReadNewTransactionId(), ReadNextMultiXactId(), 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().

1214 {
1215  Oid relid = RelationGetRelid(relation);
1216  Relation rd;
1217  HeapTuple ctup;
1218  Form_pg_class pgcform;
1219  bool dirty;
1220 
1221  rd = table_open(RelationRelationId, RowExclusiveLock);
1222 
1223  /* Fetch a copy of the tuple to scribble on */
1225  if (!HeapTupleIsValid(ctup))
1226  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1227  relid);
1228  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1229 
1230  /* Apply statistical updates, if any, to copied tuple */
1231 
1232  dirty = false;
1233  if (pgcform->relpages != (int32) num_pages)
1234  {
1235  pgcform->relpages = (int32) num_pages;
1236  dirty = true;
1237  }
1238  if (pgcform->reltuples != (float4) num_tuples)
1239  {
1240  pgcform->reltuples = (float4) num_tuples;
1241  dirty = true;
1242  }
1243  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1244  {
1245  pgcform->relallvisible = (int32) num_all_visible_pages;
1246  dirty = true;
1247  }
1248 
1249  /* Apply DDL updates, but not inside an outer transaction (see above) */
1250 
1251  if (!in_outer_xact)
1252  {
1253  /*
1254  * If we didn't find any indexes, reset relhasindex.
1255  */
1256  if (pgcform->relhasindex && !hasindex)
1257  {
1258  pgcform->relhasindex = false;
1259  dirty = true;
1260  }
1261 
1262  /* We also clear relhasrules and relhastriggers if needed */
1263  if (pgcform->relhasrules && relation->rd_rules == NULL)
1264  {
1265  pgcform->relhasrules = false;
1266  dirty = true;
1267  }
1268  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1269  {
1270  pgcform->relhastriggers = false;
1271  dirty = true;
1272  }
1273  }
1274 
1275  /*
1276  * Update relfrozenxid, unless caller passed InvalidTransactionId
1277  * indicating it has no new data.
1278  *
1279  * Ordinarily, we don't let relfrozenxid go backwards: if things are
1280  * working correctly, the only way the new frozenxid could be older would
1281  * be if a previous VACUUM was done with a tighter freeze_min_age, in
1282  * which case we don't want to forget the work it already did. However,
1283  * if the stored relfrozenxid is "in the future", then it must be corrupt
1284  * and it seems best to overwrite it with the cutoff we used this time.
1285  * This should match vac_update_datfrozenxid() concerning what we consider
1286  * to be "in the future".
1287  */
1288  if (TransactionIdIsNormal(frozenxid) &&
1289  pgcform->relfrozenxid != frozenxid &&
1290  (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
1292  pgcform->relfrozenxid)))
1293  {
1294  pgcform->relfrozenxid = frozenxid;
1295  dirty = true;
1296  }
1297 
1298  /* Similarly for relminmxid */
1299  if (MultiXactIdIsValid(minmulti) &&
1300  pgcform->relminmxid != minmulti &&
1301  (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
1302  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
1303  {
1304  pgcform->relminmxid = minmulti;
1305  dirty = true;
1306  }
1307 
1308  /* If anything changed, write out the tuple. */
1309  if (dirty)
1310  heap_inplace_update(rd, ctup);
1311 
1313 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:347
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TriggerDesc * trigdesc
Definition: rel.h:90
#define RowExclusiveLock
Definition: lockdefs.h:38
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
float float4
Definition: c.h:491
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RuleLock * rd_rules
Definition: rel.h:88
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3142
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
FormData_pg_class * Form_pg_class
Definition: pg_class.h:150
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:174
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:5707
#define elog(elevel,...)
Definition: elog.h:228
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define RelationGetRelid(relation)
Definition: rel.h:428
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723

◆ vacuum()

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

Definition at line 274 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_VACUUM, vacuum_rel(), VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumCostDelay, VacuumPageDirty, VacuumPageHit, and VacuumPageMiss.

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

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

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 1997 of file vacuum.c.

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

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

1998 {
1999  double msec = 0;
2000 
2001  /* Always check for interrupts */
2003 
2005  return;
2006 
2007  /*
2008  * For parallel vacuum, the delay is computed based on the shared cost
2009  * balance. See compute_parallel_delay.
2010  */
2011  if (VacuumSharedCostBalance != NULL)
2012  msec = compute_parallel_delay();
2013  else if (VacuumCostBalance >= VacuumCostLimit)
2015 
2016  /* Nap if appropriate */
2017  if (msec > 0)
2018  {
2019  if (msec > VacuumCostDelay * 4)
2020  msec = VacuumCostDelay * 4;
2021 
2022  pg_usleep((long) (msec * 1000));
2023 
2024  VacuumCostBalance = 0;
2025 
2026  /* update balance values for workers */
2028 
2029  /* Might have gotten an interrupt while sleeping */
2031  }
2032 }
static double compute_parallel_delay(void)
Definition: vacuum.c:2057
int VacuumCostBalance
Definition: globals.c:147
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:140
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
double VacuumCostDelay
Definition: globals.c:141
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1758
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
bool VacuumCostActive
Definition: globals.c:148

◆ vacuum_is_relation_owner()

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

Definition at line 535 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().

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

◆ vacuum_open_relation()

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

Definition at line 609 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().

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

◆ vacuum_rel()

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

Definition at line 1670 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(), MyPgXact, NewGUCNestLevel(), NoLock, VacuumParams::options, PopActiveSnapshot(), PROC_IN_VACUUM, PROC_VACUUM_FOR_WRAPAROUND, PushActiveSnapshot(), RelationData::rd_lockInfo, RelationData::rd_options, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetRelationName, RelationGetRelid, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, StartTransactionCommand(), table_relation_vacuum(), VacuumParams::truncate, UnlockRelationIdForSession(), VACOPT_FULL, VACOPT_SKIPTOAST, VACOPT_TERNARY_DEFAULT, VACOPT_TERNARY_DISABLED, VACOPT_TERNARY_ENABLED, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum_is_relation_owner(), vacuum_open_relation(), PGXACT::vacuumFlags, and WARNING.

Referenced by vacuum().

1671 {
1672  LOCKMODE lmode;
1673  Relation onerel;
1674  LockRelId onerelid;
1675  Oid toast_relid;
1676  Oid save_userid;
1677  int save_sec_context;
1678  int save_nestlevel;
1679 
1680  Assert(params != NULL);
1681 
1682  /* Begin a transaction for vacuuming this relation */
1684 
1685  /*
1686  * Functions in indexes may want a snapshot set. Also, setting a snapshot
1687  * ensures that RecentGlobalXmin is kept truly recent.
1688  */
1690 
1691  if (!(params->options & VACOPT_FULL))
1692  {
1693  /*
1694  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1695  * other concurrent VACUUMs know that they can ignore this one while
1696  * determining their OldestXmin. (The reason we don't set it during a
1697  * full VACUUM is exactly that we may have to run user-defined
1698  * functions for functional indexes, and we want to make sure that if
1699  * they use the snapshot set above, any tuples it requires can't get
1700  * removed from other tables. An index function that depends on the
1701  * contents of other tables is arguably broken, but we won't break it
1702  * here by violating transaction semantics.)
1703  *
1704  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1705  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1706  * in an emergency.
1707  *
1708  * Note: these flags remain set until CommitTransaction or
1709  * AbortTransaction. We don't want to clear them until we reset
1710  * MyPgXact->xid/xmin, else OldestXmin might appear to go backwards,
1711  * which is probably Not Good.
1712  */
1713  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1715  if (params->is_wraparound)
1717  LWLockRelease(ProcArrayLock);
1718  }
1719 
1720  /*
1721  * Check for user-requested abort. Note we want this to be inside a
1722  * transaction, so xact.c doesn't issue useless WARNING.
1723  */
1725 
1726  /*
1727  * Determine the type of lock we want --- hard exclusive lock for a FULL
1728  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1729  * way, we can be sure that no other backend is vacuuming the same table.
1730  */
1731  lmode = (params->options & VACOPT_FULL) ?
1733 
1734  /* open the relation and get the appropriate lock on it */
1735  onerel = vacuum_open_relation(relid, relation, params->options,
1736  params->log_min_duration >= 0, lmode);
1737 
1738  /* leave if relation could not be opened or locked */
1739  if (!onerel)
1740  {
1743  return false;
1744  }
1745 
1746  /*
1747  * Check if relation needs to be skipped based on ownership. This check
1748  * happens also when building the relation list to vacuum for a manual
1749  * operation, and needs to be done additionally here as VACUUM could
1750  * happen across multiple transactions where relation ownership could have
1751  * changed in-between. Make sure to only generate logs for VACUUM in this
1752  * case.
1753  */
1755  onerel->rd_rel,
1756  params->options & VACOPT_VACUUM))
1757  {
1758  relation_close(onerel, lmode);
1761  return false;
1762  }
1763 
1764  /*
1765  * Check that it's of a vacuumable relkind.
1766  */
1767  if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1768  onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1769  onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1770  onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1771  {
1772  ereport(WARNING,
1773  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1774  RelationGetRelationName(onerel))));
1775  relation_close(onerel, lmode);
1778  return false;
1779  }
1780 
1781  /*
1782  * Silently ignore tables that are temp tables of other backends ---
1783  * trying to vacuum these will lead to great unhappiness, since their
1784  * contents are probably not up-to-date on disk. (We don't throw a
1785  * warning here; it would just lead to chatter during a database-wide
1786  * VACUUM.)
1787  */
1788  if (RELATION_IS_OTHER_TEMP(onerel))
1789  {
1790  relation_close(onerel, lmode);
1793  return false;
1794  }
1795 
1796  /*
1797  * Silently ignore partitioned tables as there is no work to be done. The
1798  * useful work is on their child partitions, which have been queued up for
1799  * us separately.
1800  */
1801  if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1802  {
1803  relation_close(onerel, lmode);
1806  /* It's OK to proceed with ANALYZE on this table */
1807  return true;
1808  }
1809 
1810  /*
1811  * Get a session-level lock too. This will protect our access to the
1812  * relation across multiple transactions, so that we can vacuum the
1813  * relation's TOAST table (if any) secure in the knowledge that no one is
1814  * deleting the parent relation.
1815  *
1816  * NOTE: this cannot block, even if someone else is waiting for access,
1817  * because the lock manager knows that both lock requests are from the
1818  * same process.
1819  */
1820  onerelid = onerel->rd_lockInfo.lockRelId;
1821  LockRelationIdForSession(&onerelid, lmode);
1822 
1823  /* Set index cleanup option based on reloptions if not yet */
1824  if (params->index_cleanup == VACOPT_TERNARY_DEFAULT)
1825  {
1826  if (onerel->rd_options == NULL ||
1827  ((StdRdOptions *) onerel->rd_options)->vacuum_index_cleanup)
1829  else
1831  }
1832 
1833  /* Set truncate option based on reloptions if not yet */
1834  if (params->truncate == VACOPT_TERNARY_DEFAULT)
1835  {
1836  if (onerel->rd_options == NULL ||
1837  ((StdRdOptions *) onerel->rd_options)->vacuum_truncate)
1838  params->truncate = VACOPT_TERNARY_ENABLED;
1839  else
1841  }
1842 
1843  /*
1844  * Remember the relation's TOAST relation for later, if the caller asked
1845  * us to process it. In VACUUM FULL, though, the toast table is
1846  * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1847  */
1848  if (!(params->options & VACOPT_SKIPTOAST) && !(params->options & VACOPT_FULL))
1849  toast_relid = onerel->rd_rel->reltoastrelid;
1850  else
1851  toast_relid = InvalidOid;
1852 
1853  /*
1854  * Switch to the table owner's userid, so that any index functions are run
1855  * as that user. Also lock down security-restricted operations and
1856  * arrange to make GUC variable changes local to this command. (This is
1857  * unnecessary, but harmless, for lazy VACUUM.)
1858  */
1859  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1860  SetUserIdAndSecContext(onerel->rd_rel->relowner,
1861  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1862  save_nestlevel = NewGUCNestLevel();
1863 
1864  /*
1865  * Do the actual work --- either FULL or "lazy" vacuum
1866  */
1867  if (params->options & VACOPT_FULL)
1868  {
1869  int cluster_options = 0;
1870 
1871  /* close relation before vacuuming, but hold lock until commit */
1872  relation_close(onerel, NoLock);
1873  onerel = NULL;
1874 
1875  if ((params->options & VACOPT_VERBOSE) != 0)
1876  cluster_options |= CLUOPT_VERBOSE;
1877 
1878  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1879  cluster_rel(relid, InvalidOid, cluster_options);
1880  }
1881  else
1882  table_relation_vacuum(onerel, params, vac_strategy);
1883 
1884  /* Roll back any GUC changes executed by index functions */
1885  AtEOXact_GUC(false, save_nestlevel);
1886 
1887  /* Restore userid and security context */
1888  SetUserIdAndSecContext(save_userid, save_sec_context);
1889 
1890  /* all done with this class, but hold lock until commit */
1891  if (onerel)
1892  relation_close(onerel, NoLock);
1893 
1894  /*
1895  * Complete the transaction and free all temporary memory used.
1896  */
1899 
1900  /*
1901  * If the relation has a secondary toast rel, vacuum that too while we
1902  * still hold the session lock on the master table. Note however that
1903  * "analyze" will not get done on the toast table. This is good, because
1904  * the toaster always uses hardcoded index access and statistics are
1905  * totally unimportant for toast relations.
1906  */
1907  if (toast_relid != InvalidOid)
1908  vacuum_rel(toast_relid, NULL, params);
1909 
1910  /*
1911  * Now release the session-level lock on the master table.
1912  */
1913  UnlockRelationIdForSession(&onerelid, lmode);
1914 
1915  /* Report that we really did it. */
1916  return true;
1917 }
LockRelId lockRelId
Definition: rel.h:44
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
Definition: vacuum.c:1670
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:298
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
int LOCKMODE
Definition: lockdefs.h:26
void CommitTransactionCommand(void)
Definition: xact.c:2898
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:56
static void table_relation_vacuum(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: tableam.h:1447
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:69
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
void cluster_rel(Oid tableOid, Oid indexOid, int options)
Definition: cluster.c:265
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:233
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define PROC_IN_VACUUM
Definition: proc.h:54
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:217
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, int options)
Definition: vacuum.c:535
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:87
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:735
Relation vacuum_open_relation(Oid relid, RangeVar *relation, int options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:609
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:485
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5805
#define RelationGetRelationName(relation)
Definition: rel.h:462
#define ereport(elevel, rest)
Definition: elog.h:141
VacOptTernaryValue index_cleanup
Definition: vacuum.h:221
#define WARNING
Definition: elog.h:40
#define InvalidOid
Definition: postgres_ext.h:36
VacOptTernaryValue truncate
Definition: vacuum.h:223
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:739
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:558
void StartTransactionCommand(void)
Definition: xact.c:2797
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int log_min_duration
Definition: vacuum.h:218
#define AccessExclusiveLock
Definition: lockdefs.h:45
int NewGUCNestLevel(void)
Definition: guc.c:5791
int errmsg(const char *fmt,...)
Definition: elog.c:822
int options
Definition: vacuum.h:210
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define RelationGetRelid(relation)
Definition: rel.h:428
bytea * rd_options
Definition: rel.h:132

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

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

Referenced by copy_table_data(), and heap_vacuum_rel().

941 {
942  int freezemin;
943  int mxid_freezemin;
944  int effective_multixact_freeze_max_age;
945  TransactionId limit;
946  TransactionId safeLimit;
947  MultiXactId oldestMxact;
948  MultiXactId mxactLimit;
949  MultiXactId safeMxactLimit;
950 
951  /*
952  * We can always ignore processes running lazy vacuum. This is because we
953  * use these values only for deciding which tuples we must keep in the
954  * tables. Since lazy vacuum doesn't write its XID anywhere, it's safe to
955  * ignore it. In theory it could be problematic to ignore lazy vacuums in
956  * a full vacuum, but keep in mind that only one vacuum process can be
957  * working on a particular table at any time, and that each vacuum is
958  * always an independent transaction.
959  */
960  *oldestXmin =
962 
963  Assert(TransactionIdIsNormal(*oldestXmin));
964 
965  /*
966  * Determine the minimum freeze age to use: as specified by the caller, or
967  * vacuum_freeze_min_age, but in any case not more than half
968  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
969  * wraparound won't occur too frequently.
970  */
971  freezemin = freeze_min_age;
972  if (freezemin < 0)
973  freezemin = vacuum_freeze_min_age;
974  freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
975  Assert(freezemin >= 0);
976 
977  /*
978  * Compute the cutoff XID, being careful not to generate a "permanent" XID
979  */
980  limit = *oldestXmin - freezemin;
981  if (!TransactionIdIsNormal(limit))
982  limit = FirstNormalTransactionId;
983 
984  /*
985  * If oldestXmin is very far back (in practice, more than
986  * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
987  * freeze age of zero.
988  */
990  if (!TransactionIdIsNormal(safeLimit))
991  safeLimit = FirstNormalTransactionId;
992 
993  if (TransactionIdPrecedes(limit, safeLimit))
994  {
996  (errmsg("oldest xmin is far in the past"),
997  errhint("Close open transactions soon to avoid wraparound problems.\n"
998  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
999  limit = *oldestXmin;
1000  }
1001 
1002  *freezeLimit = limit;
1003 
1004  /*
1005  * Compute the multixact age for which freezing is urgent. This is
1006  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1007  * short of multixact member space.
1008  */
1009  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1010 
1011  /*
1012  * Determine the minimum multixact freeze age to use: as specified by
1013  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1014  * than half effective_multixact_freeze_max_age, so that autovacuums to
1015  * prevent MultiXact wraparound won't occur too frequently.
1016  */
1017  mxid_freezemin = multixact_freeze_min_age;
1018  if (mxid_freezemin < 0)
1019  mxid_freezemin = vacuum_multixact_freeze_min_age;
1020  mxid_freezemin = Min(mxid_freezemin,
1021  effective_multixact_freeze_max_age / 2);
1022  Assert(mxid_freezemin >= 0);
1023 
1024  /* compute the cutoff multi, being careful to generate a valid value */
1025  oldestMxact = GetOldestMultiXactId();
1026  mxactLimit = oldestMxact - mxid_freezemin;
1027  if (mxactLimit < FirstMultiXactId)
1028  mxactLimit = FirstMultiXactId;
1029 
1030  safeMxactLimit =
1031  ReadNextMultiXactId() - effective_multixact_freeze_max_age;
1032  if (safeMxactLimit < FirstMultiXactId)
1033  safeMxactLimit = FirstMultiXactId;
1034 
1035  if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
1036  {
1037  ereport(WARNING,
1038  (errmsg("oldest multixact is far in the past"),
1039  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
1040  /* Use the safe limit, unless an older mxact is still running */
1041  if (MultiXactIdPrecedes(oldestMxact, safeMxactLimit))
1042  mxactLimit = oldestMxact;
1043  else
1044  mxactLimit = safeMxactLimit;
1045  }
1046 
1047  *multiXactCutoff = mxactLimit;
1048 
1049  if (xidFullScanLimit != NULL)
1050  {
1051  int freezetable;
1052 
1053  Assert(mxactFullScanLimit != NULL);
1054 
1055  /*
1056  * Determine the table freeze age to use: as specified by the caller,
1057  * or vacuum_freeze_table_age, but in any case not more than
1058  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1059  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
1060  * before anti-wraparound autovacuum is launched.
1061  */
1062  freezetable = freeze_table_age;
1063  if (freezetable < 0)
1064  freezetable = vacuum_freeze_table_age;
1065  freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
1066  Assert(freezetable >= 0);
1067 
1068  /*
1069  * Compute XID limit causing a full-table vacuum, being careful not to
1070  * generate a "permanent" XID.
1071  */
1072  limit = ReadNewTransactionId() - freezetable;
1073  if (!TransactionIdIsNormal(limit))
1074  limit = FirstNormalTransactionId;
1075 
1076  *xidFullScanLimit = limit;
1077 
1078  /*
1079  * Similar to the above, determine the table freeze age to use for
1080  * multixacts: as specified by the caller, or
1081  * vacuum_multixact_freeze_table_age, but in any case not more than
1082  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
1083  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
1084  * freeze multixacts before anti-wraparound autovacuum is launched.
1085  */
1086  freezetable = multixact_freeze_table_age;
1087  if (freezetable < 0)
1088  freezetable = vacuum_multixact_freeze_table_age;
1089  freezetable = Min(freezetable,
1090  effective_multixact_freeze_max_age * 0.95);
1091  Assert(freezetable >= 0);
1092 
1093  /*
1094  * Compute MultiXact limit causing a full-table vacuum, being careful
1095  * to generate a valid MultiXact value.
1096  */
1097  mxactLimit = ReadNextMultiXactId() - freezetable;
1098  if (mxactLimit < FirstMultiXactId)
1099  mxactLimit = FirstMultiXactId;
1100 
1101  *mxactFullScanLimit = mxactLimit;
1102  }
1103  else
1104  {
1105  Assert(mxactFullScanLimit == NULL);
1106  }
1107 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:64
uint32 TransactionId
Definition: c.h:514
#define Min(x, y)
Definition: c.h:911
TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation)
Definition: snapmgr.c:1775
#define PROCARRAY_FLAGS_VACUUM
Definition: procarray.h:52
#define FirstNormalTransactionId
Definition: transam.h:34
int autovacuum_freeze_max_age
Definition: autovacuum.c:122
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:63
#define FirstMultiXactId
Definition: multixact.h:24
#define ereport(elevel, rest)
Definition: elog.h:141
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2819
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define WARNING
Definition: elog.h:40
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2493
TransactionId GetOldestXmin(Relation rel, int flags)
Definition: procarray.c:1306
TransactionId MultiXactId
Definition: c.h:524
#define Assert(condition)
Definition: c.h:739
int vacuum_freeze_min_age
Definition: vacuum.c:61
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3142
int vacuum_freeze_table_age
Definition: vacuum.c:62
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
int errmsg(const char *fmt,...)
Definition: elog.c:822
#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().