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 "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 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
 

Function Documentation

◆ ExecVacuum()

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

Definition at line 88 of file vacuum.c.

References analyze(), Assert, defGetBoolean(), 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, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, 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().

89 {
90  VacuumParams params;
91  bool verbose = false;
92  bool skip_locked = false;
93  bool analyze = false;
94  bool freeze = false;
95  bool full = false;
96  bool disable_page_skipping = false;
97  ListCell *lc;
98 
99  /* Set default value */
102 
103  /* Parse options list */
104  foreach(lc, vacstmt->options)
105  {
106  DefElem *opt = (DefElem *) lfirst(lc);
107 
108  /* Parse common options for VACUUM and ANALYZE */
109  if (strcmp(opt->defname, "verbose") == 0)
110  verbose = defGetBoolean(opt);
111  else if (strcmp(opt->defname, "skip_locked") == 0)
112  skip_locked = defGetBoolean(opt);
113  else if (!vacstmt->is_vacuumcmd)
114  ereport(ERROR,
115  (errcode(ERRCODE_SYNTAX_ERROR),
116  errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
117  parser_errposition(pstate, opt->location)));
118 
119  /* Parse options available on VACUUM */
120  else if (strcmp(opt->defname, "analyze") == 0)
121  analyze = defGetBoolean(opt);
122  else if (strcmp(opt->defname, "freeze") == 0)
123  freeze = defGetBoolean(opt);
124  else if (strcmp(opt->defname, "full") == 0)
125  full = defGetBoolean(opt);
126  else if (strcmp(opt->defname, "disable_page_skipping") == 0)
127  disable_page_skipping = defGetBoolean(opt);
128  else if (strcmp(opt->defname, "index_cleanup") == 0)
130  else if (strcmp(opt->defname, "truncate") == 0)
131  params.truncate = get_vacopt_ternary_value(opt);
132  else
133  ereport(ERROR,
134  (errcode(ERRCODE_SYNTAX_ERROR),
135  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
136  parser_errposition(pstate, opt->location)));
137  }
138 
139  /* Set vacuum options */
140  params.options =
141  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
142  (verbose ? VACOPT_VERBOSE : 0) |
143  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
144  (analyze ? VACOPT_ANALYZE : 0) |
145  (freeze ? VACOPT_FREEZE : 0) |
146  (full ? VACOPT_FULL : 0) |
147  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0);
148 
149  /* sanity checks on options */
150  Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
151  Assert((params.options & VACOPT_VACUUM) ||
152  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
153  Assert(!(params.options & VACOPT_SKIPTOAST));
154 
155  /*
156  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
157  */
158  if (!(params.options & VACOPT_ANALYZE))
159  {
160  ListCell *lc;
161 
162  foreach(lc, vacstmt->rels)
163  {
165 
166  if (vrel->va_cols != NIL)
167  ereport(ERROR,
168  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
169  errmsg("ANALYZE option must be specified when a column list is provided")));
170  }
171  }
172 
173  /*
174  * All freeze ages are zero if the FREEZE option is given; otherwise pass
175  * them as -1 which means to use the default values.
176  */
177  if (params.options & VACOPT_FREEZE)
178  {
179  params.freeze_min_age = 0;
180  params.freeze_table_age = 0;
181  params.multixact_freeze_min_age = 0;
182  params.multixact_freeze_table_age = 0;
183  }
184  else
185  {
186  params.freeze_min_age = -1;
187  params.freeze_table_age = -1;
188  params.multixact_freeze_min_age = -1;
189  params.multixact_freeze_table_age = -1;
190  }
191 
192  /* user-invoked vacuum is never "for wraparound" */
193  params.is_wraparound = false;
194 
195  /* user-invoked vacuum never uses this parameter */
196  params.log_min_duration = -1;
197 
198  /* Now go through the common routine */
199  vacuum(vacstmt->rels, &params, NULL, isTopLevel);
200 }
#define NIL
Definition: pg_list.h:65
int multixact_freeze_table_age
Definition: vacuum.h:177
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:222
bool is_vacuumcmd
Definition: parsenodes.h:3190
int errcode(int sqlerrcode)
Definition: elog.c:570
int freeze_table_age
Definition: vacuum.h:174
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
int freeze_min_age
Definition: vacuum.h:173
bool is_wraparound
Definition: vacuum.h:179
#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:183
static int verbose
Definition: pg_basebackup.c:90
VacOptTernaryValue truncate
Definition: vacuum.h:185
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
int log_min_duration
Definition: vacuum.h:180
int errmsg(const char *fmt,...)
Definition: elog.c:784
List * options
Definition: parsenodes.h:3188
int options
Definition: vacuum.h:172
char * defname
Definition: parsenodes.h:730
int multixact_freeze_min_age
Definition: vacuum.h:175
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:2816
List * rels
Definition: parsenodes.h:3189
static VacOptTernaryValue get_vacopt_ternary_value(DefElem *def)
Definition: vacuum.c:1980

◆ expand_vacuum_rel()

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

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

671 {
672  List *vacrels = NIL;
673  MemoryContext oldcontext;
674 
675  /* If caller supplied OID, there's nothing we need do here. */
676  if (OidIsValid(vrel->oid))
677  {
678  oldcontext = MemoryContextSwitchTo(vac_context);
679  vacrels = lappend(vacrels, vrel);
680  MemoryContextSwitchTo(oldcontext);
681  }
682  else
683  {
684  /* Process a specific relation, and possibly partitions thereof */
685  Oid relid;
686  HeapTuple tuple;
687  Form_pg_class classForm;
688  bool include_parts;
689  int rvr_opts;
690 
691  /*
692  * Since autovacuum workers supply OIDs when calling vacuum(), no
693  * autovacuum worker should reach this code.
694  */
696 
697  /*
698  * We transiently take AccessShareLock to protect the syscache lookup
699  * below, as well as find_all_inheritors's expectation that the caller
700  * holds some lock on the starting relation.
701  */
702  rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
703  relid = RangeVarGetRelidExtended(vrel->relation,
705  rvr_opts,
706  NULL, NULL);
707 
708  /*
709  * If the lock is unavailable, emit the same log statement that
710  * vacuum_rel() and analyze_rel() would.
711  */
712  if (!OidIsValid(relid))
713  {
714  if (options & VACOPT_VACUUM)
716  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
717  errmsg("skipping vacuum of \"%s\" --- lock not available",
718  vrel->relation->relname)));
719  else
721  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
722  errmsg("skipping analyze of \"%s\" --- lock not available",
723  vrel->relation->relname)));
724  return vacrels;
725  }
726 
727  /*
728  * To check whether the relation is a partitioned table and its
729  * ownership, fetch its syscache entry.
730  */
731  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
732  if (!HeapTupleIsValid(tuple))
733  elog(ERROR, "cache lookup failed for relation %u", relid);
734  classForm = (Form_pg_class) GETSTRUCT(tuple);
735 
736  /*
737  * Make a returnable VacuumRelation for this rel if user is a proper
738  * owner.
739  */
740  if (vacuum_is_relation_owner(relid, classForm, options))
741  {
742  oldcontext = MemoryContextSwitchTo(vac_context);
743  vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
744  relid,
745  vrel->va_cols));
746  MemoryContextSwitchTo(oldcontext);
747  }
748 
749 
750  include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
751  ReleaseSysCache(tuple);
752 
753  /*
754  * If it is, make relation list entries for its partitions. Note that
755  * the list returned by find_all_inheritors() includes the passed-in
756  * OID, so we have to skip that. There's no point in taking locks on
757  * the individual partitions yet, and doing so would just add
758  * unnecessary deadlock risk. For this last reason we do not check
759  * yet the ownership of the partitions, which get added to the list to
760  * process. Ownership will be checked later on anyway.
761  */
762  if (include_parts)
763  {
764  List *part_oids = find_all_inheritors(relid, NoLock, NULL);
765  ListCell *part_lc;
766 
767  foreach(part_lc, part_oids)
768  {
769  Oid part_oid = lfirst_oid(part_lc);
770 
771  if (part_oid == relid)
772  continue; /* ignore original table */
773 
774  /*
775  * We omit a RangeVar since it wouldn't be appropriate to
776  * complain about failure to open one of these relations
777  * later.
778  */
779  oldcontext = MemoryContextSwitchTo(vac_context);
780  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
781  part_oid,
782  vrel->va_cols));
783  MemoryContextSwitchTo(oldcontext);
784  }
785  }
786 
787  /*
788  * Release lock again. This means that by the time we actually try to
789  * process the table, it might be gone or renamed. In the former case
790  * we'll silently ignore it; in the latter case we'll process it
791  * anyway, but we must beware that the RangeVar doesn't necessarily
792  * identify it anymore. This isn't ideal, perhaps, but there's little
793  * practical alternative, since we're typically going to commit this
794  * transaction and begin a new one between now and then. Moreover,
795  * holding locks on multiple relations would create significant risk
796  * of deadlock.
797  */
799  }
800 
801  return vacrels;
802 }
#define NIL
Definition: pg_list.h:65
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
RangeVar * relation
Definition: parsenodes.h:3203
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:570
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
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:484
#define NoLock
Definition: lockdefs.h:34
static MemoryContext vac_context
Definition: vacuum.c:67
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:1124
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1172
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
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:784
#define elog(elevel,...)
Definition: elog.h:226
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 809 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().

810 {
811  List *vacrels = NIL;
812  Relation pgclass;
813  TableScanDesc scan;
814  HeapTuple tuple;
815 
816  pgclass = table_open(RelationRelationId, AccessShareLock);
817 
818  scan = table_beginscan_catalog(pgclass, 0, NULL);
819 
820  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
821  {
822  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
823  MemoryContext oldcontext;
824  Oid relid = classForm->oid;
825 
826  /* check permissions of relation */
827  if (!vacuum_is_relation_owner(relid, classForm, options))
828  continue;
829 
830  /*
831  * We include partitioned tables here; depending on which operation is
832  * to be performed, caller will decide whether to process or ignore
833  * them.
834  */
835  if (classForm->relkind != RELKIND_RELATION &&
836  classForm->relkind != RELKIND_MATVIEW &&
837  classForm->relkind != RELKIND_PARTITIONED_TABLE)
838  continue;
839 
840  /*
841  * Build VacuumRelation(s) specifying the table OIDs to be processed.
842  * We omit a RangeVar since it wouldn't be appropriate to complain
843  * about failure to open one of these relations later.
844  */
845  oldcontext = MemoryContextSwitchTo(vac_context);
846  vacrels = lappend(vacrels, makeVacuumRelation(NULL,
847  relid,
848  NIL));
849  MemoryContextSwitchTo(oldcontext);
850  }
851 
852  table_endscan(scan);
853  table_close(pgclass, AccessShareLock);
854 
855  return vacrels;
856 }
#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:484
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1290
static MemoryContext vac_context
Definition: vacuum.c:67
List * lappend(List *list, void *datum)
Definition: list.c:322
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:831
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 1980 of file vacuum.c.

References defGetBoolean(), VACOPT_TERNARY_DISABLED, and VACOPT_TERNARY_ENABLED.

Referenced by ExecVacuum().

1981 {
1983 }
bool defGetBoolean(DefElem *def)
Definition: define.c:111

◆ vac_close_indexes()

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

Definition at line 1925 of file vacuum.c.

References index_close(), and pfree().

Referenced by do_analyze_rel(), and heap_vacuum_rel().

1926 {
1927  if (Irel == NULL)
1928  return;
1929 
1930  while (nindexes--)
1931  {
1932  Relation ind = Irel[nindexes];
1933 
1934  index_close(ind, lockmode);
1935  }
1936  pfree(Irel);
1937 }
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 1072 of file vacuum.c.

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

1076 {
1077  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1078  double old_rel_tuples = relation->rd_rel->reltuples;
1079  double old_density;
1080  double unscanned_pages;
1081  double total_tuples;
1082 
1083  /* If we did scan the whole table, just use the count as-is */
1084  if (scanned_pages >= total_pages)
1085  return scanned_tuples;
1086 
1087  /*
1088  * If scanned_pages is zero but total_pages isn't, keep the existing value
1089  * of reltuples. (Note: callers should avoid updating the pg_class
1090  * statistics in this situation, since no new information has been
1091  * provided.)
1092  */
1093  if (scanned_pages == 0)
1094  return old_rel_tuples;
1095 
1096  /*
1097  * If old value of relpages is zero, old density is indeterminate; we
1098  * can't do much except scale up scanned_tuples to match total_pages.
1099  */
1100  if (old_rel_pages == 0)
1101  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1102 
1103  /*
1104  * Okay, we've covered the corner cases. The normal calculation is to
1105  * convert the old measurement to a density (tuples per page), then
1106  * estimate the number of tuples in the unscanned pages using that figure,
1107  * and finally add on the number of tuples in the scanned pages.
1108  */
1109  old_density = old_rel_tuples / old_rel_pages;
1110  unscanned_pages = (double) total_pages - (double) scanned_pages;
1111  total_tuples = old_density * unscanned_pages + scanned_tuples;
1112  return floor(total_tuples + 0.5);
1113 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:83

◆ vac_open_indexes()

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

Definition at line 1882 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(), and heap_vacuum_rel().

1884 {
1885  List *indexoidlist;
1886  ListCell *indexoidscan;
1887  int i;
1888 
1889  Assert(lockmode != NoLock);
1890 
1891  indexoidlist = RelationGetIndexList(relation);
1892 
1893  /* allocate enough memory for all indexes */
1894  i = list_length(indexoidlist);
1895 
1896  if (i > 0)
1897  *Irel = (Relation *) palloc(i * sizeof(Relation));
1898  else
1899  *Irel = NULL;
1900 
1901  /* collect just the ready indexes */
1902  i = 0;
1903  foreach(indexoidscan, indexoidlist)
1904  {
1905  Oid indexoid = lfirst_oid(indexoidscan);
1906  Relation indrel;
1907 
1908  indrel = index_open(indexoid, lockmode);
1909  if (indrel->rd_index->indisready)
1910  (*Irel)[i++] = indrel;
1911  else
1912  index_close(indrel, lockmode);
1913  }
1914 
1915  *nindexes = i;
1916 
1917  list_free(indexoidlist);
1918 }
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:143
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:732
static int list_length(const List *l)
Definition: pg_list.h:169
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4348
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 1473 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().

1477 {
1478  TransactionId nextXID = ReadNewTransactionId();
1479  Relation relation;
1480  TableScanDesc scan;
1481  HeapTuple tuple;
1482  Oid oldestxid_datoid;
1483  Oid minmulti_datoid;
1484  bool bogus = false;
1485  bool frozenAlreadyWrapped = false;
1486 
1487  /* init oldest datoids to sync with my frozenXID/minMulti values */
1488  oldestxid_datoid = MyDatabaseId;
1489  minmulti_datoid = MyDatabaseId;
1490 
1491  /*
1492  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1493  *
1494  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1495  * the values could change while we look at them. Fetch each one just
1496  * once to ensure sane behavior of the comparison logic. (Here, as in
1497  * many other places, we assume that fetching or updating an XID in shared
1498  * storage is atomic.)
1499  *
1500  * Note: we need not worry about a race condition with new entries being
1501  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1502  * existing DB's datfrozenxid, and that source DB cannot be ours because
1503  * of the interlock against copying a DB containing an active backend.
1504  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1505  * concurrently modify the datfrozenxid's of different databases, the
1506  * worst possible outcome is that pg_xact is not truncated as aggressively
1507  * as it could be.
1508  */
1509  relation = table_open(DatabaseRelationId, AccessShareLock);
1510 
1511  scan = table_beginscan_catalog(relation, 0, NULL);
1512 
1513  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1514  {
1515  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1516  TransactionId datfrozenxid = dbform->datfrozenxid;
1517  TransactionId datminmxid = dbform->datminmxid;
1518 
1519  Assert(TransactionIdIsNormal(datfrozenxid));
1520  Assert(MultiXactIdIsValid(datminmxid));
1521 
1522  /*
1523  * If things are working properly, no database should have a
1524  * datfrozenxid or datminmxid that is "in the future". However, such
1525  * cases have been known to arise due to bugs in pg_upgrade. If we
1526  * see any entries that are "in the future", chicken out and don't do
1527  * anything. This ensures we won't truncate clog before those
1528  * databases have been scanned and cleaned up. (We will issue the
1529  * "already wrapped" warning if appropriate, though.)
1530  */
1531  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1532  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1533  bogus = true;
1534 
1535  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1536  frozenAlreadyWrapped = true;
1537  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1538  {
1539  frozenXID = datfrozenxid;
1540  oldestxid_datoid = dbform->oid;
1541  }
1542 
1543  if (MultiXactIdPrecedes(datminmxid, minMulti))
1544  {
1545  minMulti = datminmxid;
1546  minmulti_datoid = dbform->oid;
1547  }
1548  }
1549 
1550  table_endscan(scan);
1551 
1552  table_close(relation, AccessShareLock);
1553 
1554  /*
1555  * Do not truncate CLOG if we seem to have suffered wraparound already;
1556  * the computed minimum XID might be bogus. This case should now be
1557  * impossible due to the defenses in GetNewTransactionId, but we keep the
1558  * test anyway.
1559  */
1560  if (frozenAlreadyWrapped)
1561  {
1562  ereport(WARNING,
1563  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1564  errdetail("You might have already suffered transaction-wraparound data loss.")));
1565  return;
1566  }
1567 
1568  /* chicken out if data is bogus in any other way */
1569  if (bogus)
1570  return;
1571 
1572  /*
1573  * Advance the oldest value for commit timestamps before truncating, so
1574  * that if a user requests a timestamp for a transaction we're truncating
1575  * away right after this point, they get NULL instead of an ugly "file not
1576  * found" error from slru.c. This doesn't matter for xact/multixact
1577  * because they are not subject to arbitrary lookups from users.
1578  */
1579  AdvanceOldestCommitTsXid(frozenXID);
1580 
1581  /*
1582  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1583  */
1584  TruncateCLOG(frozenXID, oldestxid_datoid);
1585  TruncateCommitTs(frozenXID);
1586  TruncateMultiXact(minMulti, minmulti_datoid);
1587 
1588  /*
1589  * Update the wrap limit for GetNewTransactionId and creation of new
1590  * MultiXactIds. Note: these functions will also signal the postmaster
1591  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1592  * signalling twice?
1593  */
1594  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1595  SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1596 }
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:906
uint32 TransactionId
Definition: c.h:507
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:860
#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:732
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:831
int errmsg(const char *fmt,...)
Definition: elog.c:784
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 1284 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().

1285 {
1286  HeapTuple tuple;
1287  Form_pg_database dbform;
1288  Relation relation;
1289  SysScanDesc scan;
1290  HeapTuple classTup;
1291  TransactionId newFrozenXid;
1292  MultiXactId newMinMulti;
1293  TransactionId lastSaneFrozenXid;
1294  MultiXactId lastSaneMinMulti;
1295  bool bogus = false;
1296  bool dirty = false;
1297 
1298  /*
1299  * Initialize the "min" calculation with GetOldestXmin, which is a
1300  * reasonable approximation to the minimum relfrozenxid for not-yet-
1301  * committed pg_class entries for new tables; see AddNewRelationTuple().
1302  * So we cannot produce a wrong minimum by starting with this.
1303  */
1304  newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
1305 
1306  /*
1307  * Similarly, initialize the MultiXact "min" with the value that would be
1308  * used on pg_class for new tables. See AddNewRelationTuple().
1309  */
1310  newMinMulti = GetOldestMultiXactId();
1311 
1312  /*
1313  * Identify the latest relfrozenxid and relminmxid values that we could
1314  * validly see during the scan. These are conservative values, but it's
1315  * not really worth trying to be more exact.
1316  */
1317  lastSaneFrozenXid = ReadNewTransactionId();
1318  lastSaneMinMulti = ReadNextMultiXactId();
1319 
1320  /*
1321  * We must seqscan pg_class to find the minimum Xid, because there is no
1322  * index that can help us here.
1323  */
1324  relation = table_open(RelationRelationId, AccessShareLock);
1325 
1326  scan = systable_beginscan(relation, InvalidOid, false,
1327  NULL, 0, NULL);
1328 
1329  while ((classTup = systable_getnext(scan)) != NULL)
1330  {
1331  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1332 
1333  /*
1334  * Only consider relations able to hold unfrozen XIDs (anything else
1335  * should have InvalidTransactionId in relfrozenxid anyway).
1336  */
1337  if (classForm->relkind != RELKIND_RELATION &&
1338  classForm->relkind != RELKIND_MATVIEW &&
1339  classForm->relkind != RELKIND_TOASTVALUE)
1340  {
1341  Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1342  Assert(!MultiXactIdIsValid(classForm->relminmxid));
1343  continue;
1344  }
1345 
1346  /*
1347  * Some table AMs might not need per-relation xid / multixid horizons.
1348  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1349  * to not be set (i.e. set to their respective Invalid*Id)
1350  * independently. Thus validate and compute horizon for each only if
1351  * set.
1352  *
1353  * If things are working properly, no relation should have a
1354  * relfrozenxid or relminmxid that is "in the future". However, such
1355  * cases have been known to arise due to bugs in pg_upgrade. If we
1356  * see any entries that are "in the future", chicken out and don't do
1357  * anything. This ensures we won't truncate clog & multixact SLRUs
1358  * before those relations have been scanned and cleaned up.
1359  */
1360 
1361  if (TransactionIdIsValid(classForm->relfrozenxid))
1362  {
1363  Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1364 
1365  /* check for values in the future */
1366  if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1367  {
1368  bogus = true;
1369  break;
1370  }
1371 
1372  /* determine new horizon */
1373  if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1374  newFrozenXid = classForm->relfrozenxid;
1375  }
1376 
1377  if (MultiXactIdIsValid(classForm->relminmxid))
1378  {
1379  /* check for values in the future */
1380  if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1381  {
1382  bogus = true;
1383  break;
1384  }
1385 
1386  /* determine new horizon */
1387  if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1388  newMinMulti = classForm->relminmxid;
1389  }
1390  }
1391 
1392  /* we're done with pg_class */
1393  systable_endscan(scan);
1394  table_close(relation, AccessShareLock);
1395 
1396  /* chicken out if bogus data found */
1397  if (bogus)
1398  return;
1399 
1400  Assert(TransactionIdIsNormal(newFrozenXid));
1401  Assert(MultiXactIdIsValid(newMinMulti));
1402 
1403  /* Now fetch the pg_database tuple we need to update. */
1404  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1405 
1406  /* Fetch a copy of the tuple to scribble on */
1408  if (!HeapTupleIsValid(tuple))
1409  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1410  dbform = (Form_pg_database) GETSTRUCT(tuple);
1411 
1412  /*
1413  * As in vac_update_relstats(), we ordinarily don't want to let
1414  * datfrozenxid go backward; but if it's "in the future" then it must be
1415  * corrupt and it seems best to overwrite it.
1416  */
1417  if (dbform->datfrozenxid != newFrozenXid &&
1418  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1419  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1420  {
1421  dbform->datfrozenxid = newFrozenXid;
1422  dirty = true;
1423  }
1424  else
1425  newFrozenXid = dbform->datfrozenxid;
1426 
1427  /* Ditto for datminmxid */
1428  if (dbform->datminmxid != newMinMulti &&
1429  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1430  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1431  {
1432  dbform->datminmxid = newMinMulti;
1433  dirty = true;
1434  }
1435  else
1436  newMinMulti = dbform->datminmxid;
1437 
1438  if (dirty)
1439  heap_inplace_update(relation, tuple);
1440 
1441  heap_freetuple(tuple);
1442  table_close(relation, RowExclusiveLock);
1443 
1444  /*
1445  * If we were able to advance datfrozenxid or datminmxid, see if we can
1446  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1447  * XID-wrap-limit info is stale, since this action will update that too.
1448  */
1449  if (dirty || ForceTransactionIdLimitUpdate())
1450  vac_truncate_clog(newFrozenXid, newMinMulti,
1451  lastSaneFrozenXid, lastSaneMinMulti);
1452 }
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:507
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:1304
TransactionId MultiXactId
Definition: c.h:517
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:732
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:1473
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:226
#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 1157 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 lazy_cleanup_index().

1163 {
1164  Oid relid = RelationGetRelid(relation);
1165  Relation rd;
1166  HeapTuple ctup;
1167  Form_pg_class pgcform;
1168  bool dirty;
1169 
1170  rd = table_open(RelationRelationId, RowExclusiveLock);
1171 
1172  /* Fetch a copy of the tuple to scribble on */
1174  if (!HeapTupleIsValid(ctup))
1175  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1176  relid);
1177  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1178 
1179  /* Apply statistical updates, if any, to copied tuple */
1180 
1181  dirty = false;
1182  if (pgcform->relpages != (int32) num_pages)
1183  {
1184  pgcform->relpages = (int32) num_pages;
1185  dirty = true;
1186  }
1187  if (pgcform->reltuples != (float4) num_tuples)
1188  {
1189  pgcform->reltuples = (float4) num_tuples;
1190  dirty = true;
1191  }
1192  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1193  {
1194  pgcform->relallvisible = (int32) num_all_visible_pages;
1195  dirty = true;
1196  }
1197 
1198  /* Apply DDL updates, but not inside an outer transaction (see above) */
1199 
1200  if (!in_outer_xact)
1201  {
1202  /*
1203  * If we didn't find any indexes, reset relhasindex.
1204  */
1205  if (pgcform->relhasindex && !hasindex)
1206  {
1207  pgcform->relhasindex = false;
1208  dirty = true;
1209  }
1210 
1211  /* We also clear relhasrules and relhastriggers if needed */
1212  if (pgcform->relhasrules && relation->rd_rules == NULL)
1213  {
1214  pgcform->relhasrules = false;
1215  dirty = true;
1216  }
1217  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1218  {
1219  pgcform->relhastriggers = false;
1220  dirty = true;
1221  }
1222  }
1223 
1224  /*
1225  * Update relfrozenxid, unless caller passed InvalidTransactionId
1226  * indicating it has no new data.
1227  *
1228  * Ordinarily, we don't let relfrozenxid go backwards: if things are
1229  * working correctly, the only way the new frozenxid could be older would
1230  * be if a previous VACUUM was done with a tighter freeze_min_age, in
1231  * which case we don't want to forget the work it already did. However,
1232  * if the stored relfrozenxid is "in the future", then it must be corrupt
1233  * and it seems best to overwrite it with the cutoff we used this time.
1234  * This should match vac_update_datfrozenxid() concerning what we consider
1235  * to be "in the future".
1236  */
1237  if (TransactionIdIsNormal(frozenxid) &&
1238  pgcform->relfrozenxid != frozenxid &&
1239  (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
1241  pgcform->relfrozenxid)))
1242  {
1243  pgcform->relfrozenxid = frozenxid;
1244  dirty = true;
1245  }
1246 
1247  /* Similarly for relminmxid */
1248  if (MultiXactIdIsValid(minmulti) &&
1249  pgcform->relminmxid != minmulti &&
1250  (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
1251  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
1252  {
1253  pgcform->relminmxid = minmulti;
1254  dirty = true;
1255  }
1256 
1257  /* If anything changed, write out the tuple. */
1258  if (dirty)
1259  heap_inplace_update(rd, ctup);
1260 
1262 }
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:346
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
TriggerDesc * trigdesc
Definition: rel.h:89
#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:490
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RuleLock * rd_rules
Definition: rel.h:87
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:226
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define RelationGetRelid(relation)
Definition: rel.h:419
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:723

◆ vacuum()

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

Definition at line 222 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_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgstat_vacuum_stat(), PopActiveSnapshot(), PortalContext, PreventInTransactionBlock(), PushActiveSnapshot(), VacuumRelation::relation, StartTransactionCommand(), VacuumRelation::va_cols, vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FULL, VACOPT_VACUUM, vacuum_rel(), VacuumCostActive, VacuumCostBalance, VacuumCostDelay, VacuumPageDirty, VacuumPageHit, and VacuumPageMiss.

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

224 {
225  static bool in_vacuum = false;
226 
227  const char *stmttype;
228  volatile bool in_outer_xact,
229  use_own_xacts;
230 
231  Assert(params != NULL);
232 
233  stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
234 
235  /*
236  * We cannot run VACUUM inside a user transaction block; if we were inside
237  * a transaction, then our commit- and start-transaction-command calls
238  * would not have the intended effect! There are numerous other subtle
239  * dependencies on this, too.
240  *
241  * ANALYZE (without VACUUM) can run either way.
242  */
243  if (params->options & VACOPT_VACUUM)
244  {
245  PreventInTransactionBlock(isTopLevel, stmttype);
246  in_outer_xact = false;
247  }
248  else
249  in_outer_xact = IsInTransactionBlock(isTopLevel);
250 
251  /*
252  * Due to static variables vac_context, anl_context and vac_strategy,
253  * vacuum() is not reentrant. This matters when VACUUM FULL or ANALYZE
254  * calls a hostile index expression that itself calls ANALYZE.
255  */
256  if (in_vacuum)
257  ereport(ERROR,
258  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259  errmsg("%s cannot be executed from VACUUM or ANALYZE",
260  stmttype)));
261 
262  /*
263  * Sanity check DISABLE_PAGE_SKIPPING option.
264  */
265  if ((params->options & VACOPT_FULL) != 0 &&
266  (params->options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
267  ereport(ERROR,
268  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
269  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
270 
271  /*
272  * Send info about dead objects to the statistics collector, unless we are
273  * in autovacuum --- autovacuum.c does this for itself.
274  */
275  if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
277 
278  /*
279  * Create special memory context for cross-transaction storage.
280  *
281  * Since it is a child of PortalContext, it will go away eventually even
282  * if we suffer an error; there's no need for special abort cleanup logic.
283  */
285  "Vacuum",
287 
288  /*
289  * If caller didn't give us a buffer strategy object, make one in the
290  * cross-transaction memory context.
291  */
292  if (bstrategy == NULL)
293  {
295 
296  bstrategy = GetAccessStrategy(BAS_VACUUM);
297  MemoryContextSwitchTo(old_context);
298  }
299  vac_strategy = bstrategy;
300 
301  /*
302  * Build list of relation(s) to process, putting any new data in
303  * vac_context for safekeeping.
304  */
305  if (relations != NIL)
306  {
307  List *newrels = NIL;
308  ListCell *lc;
309 
310  foreach(lc, relations)
311  {
313  List *sublist;
314  MemoryContext old_context;
315 
316  sublist = expand_vacuum_rel(vrel, params->options);
317  old_context = MemoryContextSwitchTo(vac_context);
318  newrels = list_concat(newrels, sublist);
319  MemoryContextSwitchTo(old_context);
320  }
321  relations = newrels;
322  }
323  else
324  relations = get_all_vacuum_rels(params->options);
325 
326  /*
327  * Decide whether we need to start/commit our own transactions.
328  *
329  * For VACUUM (with or without ANALYZE): always do so, so that we can
330  * release locks as soon as possible. (We could possibly use the outer
331  * transaction for a one-table VACUUM, but handling TOAST tables would be
332  * problematic.)
333  *
334  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
335  * start/commit our own transactions. Also, there's no need to do so if
336  * only processing one relation. For multiple relations when not within a
337  * transaction block, and also in an autovacuum worker, use own
338  * transactions so we can release locks sooner.
339  */
340  if (params->options & VACOPT_VACUUM)
341  use_own_xacts = true;
342  else
343  {
344  Assert(params->options & VACOPT_ANALYZE);
346  use_own_xacts = true;
347  else if (in_outer_xact)
348  use_own_xacts = false;
349  else if (list_length(relations) > 1)
350  use_own_xacts = true;
351  else
352  use_own_xacts = false;
353  }
354 
355  /*
356  * vacuum_rel expects to be entered with no transaction active; it will
357  * start and commit its own transaction. But we are called by an SQL
358  * command, and so we are executing inside a transaction already. We
359  * commit the transaction started in PostgresMain() here, and start
360  * another one before exiting to match the commit waiting for us back in
361  * PostgresMain().
362  */
363  if (use_own_xacts)
364  {
365  Assert(!in_outer_xact);
366 
367  /* ActiveSnapshot is not set by autovacuum */
368  if (ActiveSnapshotSet())
370 
371  /* matches the StartTransaction in PostgresMain() */
373  }
374 
375  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
376  PG_TRY();
377  {
378  ListCell *cur;
379 
380  in_vacuum = true;
382  VacuumCostBalance = 0;
383  VacuumPageHit = 0;
384  VacuumPageMiss = 0;
385  VacuumPageDirty = 0;
386 
387  /*
388  * Loop to process each selected relation.
389  */
390  foreach(cur, relations)
391  {
393 
394  if (params->options & VACOPT_VACUUM)
395  {
396  if (!vacuum_rel(vrel->oid, vrel->relation, params))
397  continue;
398  }
399 
400  if (params->options & VACOPT_ANALYZE)
401  {
402  /*
403  * If using separate xacts, start one for analyze. Otherwise,
404  * we can use the outer transaction.
405  */
406  if (use_own_xacts)
407  {
409  /* functions in indexes may want a snapshot set */
411  }
412 
413  analyze_rel(vrel->oid, vrel->relation, params,
414  vrel->va_cols, in_outer_xact, vac_strategy);
415 
416  if (use_own_xacts)
417  {
420  }
421  else
422  {
423  /*
424  * If we're not using separate xacts, better separate the
425  * ANALYZE actions with CCIs. This avoids trouble if user
426  * says "ANALYZE t, t".
427  */
429  }
430  }
431  }
432  }
433  PG_CATCH();
434  {
435  in_vacuum = false;
436  VacuumCostActive = false;
437  PG_RE_THROW();
438  }
439  PG_END_TRY();
440 
441  in_vacuum = false;
442  VacuumCostActive = false;
443 
444  /*
445  * Finish up processing.
446  */
447  if (use_own_xacts)
448  {
449  /* here, we are not in a transaction */
450 
451  /*
452  * This matches the CommitTransaction waiting for us in
453  * PostgresMain().
454  */
456  }
457 
458  if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
459  {
460  /*
461  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
462  * (autovacuum.c does this for itself.)
463  */
465  }
466 
467  /*
468  * Clean up working storage --- note we must do this after
469  * StartTransactionCommand, else we might be trying to delete the active
470  * context!
471  */
473  vac_context = NULL;
474 }
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:118
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
int VacuumCostBalance
Definition: globals.c:147
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
Definition: vacuum.c:1619
RangeVar * relation
Definition: parsenodes.h:3203
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1284
int VacuumPageHit
Definition: globals.c:143
void CommitTransactionCommand(void)
Definition: xact.c:2895
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:68
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:570
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:670
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:67
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3328
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:1023
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3442
int VacuumPageDirty
Definition: globals.c:145
void CommandCounterIncrement(void)
Definition: xact.c:1003
#define PG_CATCH()
Definition: elog.h:310
#define Assert(condition)
Definition: c.h:732
void StartTransactionCommand(void)
Definition: xact.c:2794
static int list_length(const List *l)
Definition: pg_list.h:169
#define PG_RE_THROW()
Definition: elog.h:331
int errmsg(const char *fmt,...)
Definition: elog.c:784
double VacuumCostDelay
Definition: globals.c:141
int options
Definition: vacuum.h:172
int VacuumPageMiss
Definition: globals.c:144
#define PG_TRY()
Definition: elog.h:301
Definition: pg_list.h:50
#define PG_END_TRY()
Definition: elog.h:317
bool VacuumCostActive
Definition: globals.c:148
static List * get_all_vacuum_rels(int options)
Definition: vacuum.c:809

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 1946 of file vacuum.c.

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

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

1947 {
1948  /* Always check for interrupts */
1950 
1951  /* Nap if appropriate */
1954  {
1955  double msec;
1956 
1958  if (msec > VacuumCostDelay * 4)
1959  msec = VacuumCostDelay * 4;
1960 
1961  pg_usleep((long) (msec * 1000));
1962 
1963  VacuumCostBalance = 0;
1964 
1965  /* update balance values for workers */
1967 
1968  /* Might have gotten an interrupt while sleeping */
1970  }
1971 }
int VacuumCostBalance
Definition: globals.c:147
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:140
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
double VacuumCostDelay
Definition: globals.c:141
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1760
#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 484 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().

485 {
486  char *relname;
487 
489 
490  /*
491  * Check permissions.
492  *
493  * We allow the user to vacuum or analyze a table if he is superuser, the
494  * table owner, or the database owner (but in the latter case, only if
495  * it's not a shared relation). pg_class_ownercheck includes the
496  * superuser case.
497  *
498  * Note we choose to treat permissions failure as a WARNING and keep
499  * trying to vacuum or analyze the rest of the DB --- is this appropriate?
500  */
501  if (pg_class_ownercheck(relid, GetUserId()) ||
502  (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !reltuple->relisshared))
503  return true;
504 
505  relname = NameStr(reltuple->relname);
506 
507  if ((options & VACOPT_VACUUM) != 0)
508  {
509  if (reltuple->relisshared)
511  (errmsg("skipping \"%s\" --- only superuser can vacuum it",
512  relname)));
513  else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
515  (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
516  relname)));
517  else
519  (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
520  relname)));
521 
522  /*
523  * For VACUUM ANALYZE, both logs could show up, but just generate
524  * information for VACUUM as that would be the first one to be
525  * processed.
526  */
527  return false;
528  }
529 
530  if ((options & VACOPT_ANALYZE) != 0)
531  {
532  if (reltuple->relisshared)
534  (errmsg("skipping \"%s\" --- only superuser can analyze it",
535  relname)));
536  else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
538  (errmsg("skipping \"%s\" --- only superuser or database owner can analyze it",
539  relname)));
540  else
542  (errmsg("skipping \"%s\" --- only table or database owner can analyze it",
543  relname)));
544  }
545 
546  return false;
547 }
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:5173
#define WARNING
Definition: elog.h:40
Oid MyDatabaseId
Definition: globals.c:85
#define Assert(condition)
Definition: c.h:732
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4755
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define NameStr(name)
Definition: c.h:609

◆ vacuum_open_relation()

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

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

560 {
561  Relation onerel;
562  bool rel_lock = true;
563  int elevel;
564 
566 
567  /*
568  * Open the relation and get the appropriate lock on it.
569  *
570  * There's a race condition here: the relation may have gone away since
571  * the last time we saw it. If so, we don't need to vacuum or analyze it.
572  *
573  * If we've been asked not to wait for the relation lock, acquire it first
574  * in non-blocking mode, before calling try_relation_open().
575  */
576  if (!(options & VACOPT_SKIP_LOCKED))
577  onerel = try_relation_open(relid, lmode);
578  else if (ConditionalLockRelationOid(relid, lmode))
579  onerel = try_relation_open(relid, NoLock);
580  else
581  {
582  onerel = NULL;
583  rel_lock = false;
584  }
585 
586  /* if relation is opened, leave */
587  if (onerel)
588  return onerel;
589 
590  /*
591  * Relation could not be opened, hence generate if possible a log
592  * informing on the situation.
593  *
594  * If the RangeVar is not defined, we do not have enough information to
595  * provide a meaningful log statement. Chances are that the caller has
596  * intentionally not provided this information so that this logging is
597  * skipped, anyway.
598  */
599  if (relation == NULL)
600  return NULL;
601 
602  /*
603  * Determine the log level.
604  *
605  * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
606  * statements in the permission checks; otherwise, only log if the caller
607  * so requested.
608  */
610  elevel = WARNING;
611  else if (verbose)
612  elevel = LOG;
613  else
614  return NULL;
615 
616  if ((options & VACOPT_VACUUM) != 0)
617  {
618  if (!rel_lock)
619  ereport(elevel,
620  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
621  errmsg("skipping vacuum of \"%s\" --- lock not available",
622  relation->relname)));
623  else
624  ereport(elevel,
626  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
627  relation->relname)));
628 
629  /*
630  * For VACUUM ANALYZE, both logs could show up, but just generate
631  * information for VACUUM as that would be the first one to be
632  * processed.
633  */
634  return NULL;
635  }
636 
637  if ((options & VACOPT_ANALYZE) != 0)
638  {
639  if (!rel_lock)
640  ereport(elevel,
641  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
642  errmsg("skipping analyze of \"%s\" --- lock not available",
643  relation->relname)));
644  else
645  ereport(elevel,
647  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
648  relation->relname)));
649  }
650 
651  return NULL;
652 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:72
int errcode(int sqlerrcode)
Definition: elog.c:570
#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
Definition: pg_basebackup.c:90
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:143
#define Assert(condition)
Definition: c.h:732
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ vacuum_rel()

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

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

1620 {
1621  LOCKMODE lmode;
1622  Relation onerel;
1623  LockRelId onerelid;
1624  Oid toast_relid;
1625  Oid save_userid;
1626  int save_sec_context;
1627  int save_nestlevel;
1628 
1629  Assert(params != NULL);
1630 
1631  /* Begin a transaction for vacuuming this relation */
1633 
1634  /*
1635  * Functions in indexes may want a snapshot set. Also, setting a snapshot
1636  * ensures that RecentGlobalXmin is kept truly recent.
1637  */
1639 
1640  if (!(params->options & VACOPT_FULL))
1641  {
1642  /*
1643  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1644  * other concurrent VACUUMs know that they can ignore this one while
1645  * determining their OldestXmin. (The reason we don't set it during a
1646  * full VACUUM is exactly that we may have to run user-defined
1647  * functions for functional indexes, and we want to make sure that if
1648  * they use the snapshot set above, any tuples it requires can't get
1649  * removed from other tables. An index function that depends on the
1650  * contents of other tables is arguably broken, but we won't break it
1651  * here by violating transaction semantics.)
1652  *
1653  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1654  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1655  * in an emergency.
1656  *
1657  * Note: these flags remain set until CommitTransaction or
1658  * AbortTransaction. We don't want to clear them until we reset
1659  * MyPgXact->xid/xmin, else OldestXmin might appear to go backwards,
1660  * which is probably Not Good.
1661  */
1662  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1664  if (params->is_wraparound)
1666  LWLockRelease(ProcArrayLock);
1667  }
1668 
1669  /*
1670  * Check for user-requested abort. Note we want this to be inside a
1671  * transaction, so xact.c doesn't issue useless WARNING.
1672  */
1674 
1675  /*
1676  * Determine the type of lock we want --- hard exclusive lock for a FULL
1677  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1678  * way, we can be sure that no other backend is vacuuming the same table.
1679  */
1680  lmode = (params->options & VACOPT_FULL) ?
1682 
1683  /* open the relation and get the appropriate lock on it */
1684  onerel = vacuum_open_relation(relid, relation, params->options,
1685  params->log_min_duration >= 0, lmode);
1686 
1687  /* leave if relation could not be opened or locked */
1688  if (!onerel)
1689  {
1692  return false;
1693  }
1694 
1695  /*
1696  * Check if relation needs to be skipped based on ownership. This check
1697  * happens also when building the relation list to vacuum for a manual
1698  * operation, and needs to be done additionally here as VACUUM could
1699  * happen across multiple transactions where relation ownership could have
1700  * changed in-between. Make sure to only generate logs for VACUUM in this
1701  * case.
1702  */
1704  onerel->rd_rel,
1705  params->options & VACOPT_VACUUM))
1706  {
1707  relation_close(onerel, lmode);
1710  return false;
1711  }
1712 
1713  /*
1714  * Check that it's of a vacuumable relkind.
1715  */
1716  if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1717  onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1718  onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1719  onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1720  {
1721  ereport(WARNING,
1722  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1723  RelationGetRelationName(onerel))));
1724  relation_close(onerel, lmode);
1727  return false;
1728  }
1729 
1730  /*
1731  * Silently ignore tables that are temp tables of other backends ---
1732  * trying to vacuum these will lead to great unhappiness, since their
1733  * contents are probably not up-to-date on disk. (We don't throw a
1734  * warning here; it would just lead to chatter during a database-wide
1735  * VACUUM.)
1736  */
1737  if (RELATION_IS_OTHER_TEMP(onerel))
1738  {
1739  relation_close(onerel, lmode);
1742  return false;
1743  }
1744 
1745  /*
1746  * Silently ignore partitioned tables as there is no work to be done. The
1747  * useful work is on their child partitions, which have been queued up for
1748  * us separately.
1749  */
1750  if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1751  {
1752  relation_close(onerel, lmode);
1755  /* It's OK to proceed with ANALYZE on this table */
1756  return true;
1757  }
1758 
1759  /*
1760  * Get a session-level lock too. This will protect our access to the
1761  * relation across multiple transactions, so that we can vacuum the
1762  * relation's TOAST table (if any) secure in the knowledge that no one is
1763  * deleting the parent relation.
1764  *
1765  * NOTE: this cannot block, even if someone else is waiting for access,
1766  * because the lock manager knows that both lock requests are from the
1767  * same process.
1768  */
1769  onerelid = onerel->rd_lockInfo.lockRelId;
1770  LockRelationIdForSession(&onerelid, lmode);
1771 
1772  /* Set index cleanup option based on reloptions if not yet */
1773  if (params->index_cleanup == VACOPT_TERNARY_DEFAULT)
1774  {
1775  if (onerel->rd_options == NULL ||
1776  ((StdRdOptions *) onerel->rd_options)->vacuum_index_cleanup)
1778  else
1780  }
1781 
1782  /* Set truncate option based on reloptions if not yet */
1783  if (params->truncate == VACOPT_TERNARY_DEFAULT)
1784  {
1785  if (onerel->rd_options == NULL ||
1786  ((StdRdOptions *) onerel->rd_options)->vacuum_truncate)
1787  params->truncate = VACOPT_TERNARY_ENABLED;
1788  else
1790  }
1791 
1792  /*
1793  * Remember the relation's TOAST relation for later, if the caller asked
1794  * us to process it. In VACUUM FULL, though, the toast table is
1795  * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1796  */
1797  if (!(params->options & VACOPT_SKIPTOAST) && !(params->options & VACOPT_FULL))
1798  toast_relid = onerel->rd_rel->reltoastrelid;
1799  else
1800  toast_relid = InvalidOid;
1801 
1802  /*
1803  * Switch to the table owner's userid, so that any index functions are run
1804  * as that user. Also lock down security-restricted operations and
1805  * arrange to make GUC variable changes local to this command. (This is
1806  * unnecessary, but harmless, for lazy VACUUM.)
1807  */
1808  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1809  SetUserIdAndSecContext(onerel->rd_rel->relowner,
1810  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1811  save_nestlevel = NewGUCNestLevel();
1812 
1813  /*
1814  * Do the actual work --- either FULL or "lazy" vacuum
1815  */
1816  if (params->options & VACOPT_FULL)
1817  {
1818  int cluster_options = 0;
1819 
1820  /* close relation before vacuuming, but hold lock until commit */
1821  relation_close(onerel, NoLock);
1822  onerel = NULL;
1823 
1824  if ((params->options & VACOPT_VERBOSE) != 0)
1825  cluster_options |= CLUOPT_VERBOSE;
1826 
1827  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1828  cluster_rel(relid, InvalidOid, cluster_options);
1829  }
1830  else
1831  table_relation_vacuum(onerel, params, vac_strategy);
1832 
1833  /* Roll back any GUC changes executed by index functions */
1834  AtEOXact_GUC(false, save_nestlevel);
1835 
1836  /* Restore userid and security context */
1837  SetUserIdAndSecContext(save_userid, save_sec_context);
1838 
1839  /* all done with this class, but hold lock until commit */
1840  if (onerel)
1841  relation_close(onerel, NoLock);
1842 
1843  /*
1844  * Complete the transaction and free all temporary memory used.
1845  */
1848 
1849  /*
1850  * If the relation has a secondary toast rel, vacuum that too while we
1851  * still hold the session lock on the master table. Note however that
1852  * "analyze" will not get done on the toast table. This is good, because
1853  * the toaster always uses hardcoded index access and statistics are
1854  * totally unimportant for toast relations.
1855  */
1856  if (toast_relid != InvalidOid)
1857  vacuum_rel(toast_relid, NULL, params);
1858 
1859  /*
1860  * Now release the session-level lock on the master table.
1861  */
1862  UnlockRelationIdForSession(&onerelid, lmode);
1863 
1864  /* Report that we really did it. */
1865  return true;
1866 }
LockRelId lockRelId
Definition: rel.h:43
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
Definition: vacuum.c:1619
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:300
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
int LOCKMODE
Definition: lockdefs.h:26
void CommitTransactionCommand(void)
Definition: xact.c:2895
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:56
static void table_relation_vacuum(Relation rel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: tableam.h:1429
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:68
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
Form_pg_class rd_rel
Definition: rel.h:83
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:266
PGXACT * MyPgXact
Definition: proc.c:69
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:35
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:369
bool is_wraparound
Definition: vacuum.h:179
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, int options)
Definition: vacuum.c:484
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:86
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:558
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:485
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define ereport(elevel, rest)
Definition: elog.h:141
VacOptTernaryValue index_cleanup
Definition: vacuum.h:183
#define WARNING
Definition: elog.h:40
#define InvalidOid
Definition: postgres_ext.h:36
VacOptTernaryValue truncate
Definition: vacuum.h:185
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:732
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
void StartTransactionCommand(void)
Definition: xact.c:2794
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int log_min_duration
Definition: vacuum.h:180
#define AccessExclusiveLock
Definition: lockdefs.h:45
int NewGUCNestLevel(void)
Definition: guc.c:5715
int errmsg(const char *fmt,...)
Definition: elog.c:784
int options
Definition: vacuum.h:172
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define RelationGetRelid(relation)
Definition: rel.h:419
bytea * rd_options
Definition: rel.h:126

◆ 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 880 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().

890 {
891  int freezemin;
892  int mxid_freezemin;
893  int effective_multixact_freeze_max_age;
894  TransactionId limit;
895  TransactionId safeLimit;
896  MultiXactId oldestMxact;
897  MultiXactId mxactLimit;
898  MultiXactId safeMxactLimit;
899 
900  /*
901  * We can always ignore processes running lazy vacuum. This is because we
902  * use these values only for deciding which tuples we must keep in the
903  * tables. Since lazy vacuum doesn't write its XID anywhere, it's safe to
904  * ignore it. In theory it could be problematic to ignore lazy vacuums in
905  * a full vacuum, but keep in mind that only one vacuum process can be
906  * working on a particular table at any time, and that each vacuum is
907  * always an independent transaction.
908  */
909  *oldestXmin =
911 
912  Assert(TransactionIdIsNormal(*oldestXmin));
913 
914  /*
915  * Determine the minimum freeze age to use: as specified by the caller, or
916  * vacuum_freeze_min_age, but in any case not more than half
917  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
918  * wraparound won't occur too frequently.
919  */
920  freezemin = freeze_min_age;
921  if (freezemin < 0)
922  freezemin = vacuum_freeze_min_age;
923  freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
924  Assert(freezemin >= 0);
925 
926  /*
927  * Compute the cutoff XID, being careful not to generate a "permanent" XID
928  */
929  limit = *oldestXmin - freezemin;
930  if (!TransactionIdIsNormal(limit))
931  limit = FirstNormalTransactionId;
932 
933  /*
934  * If oldestXmin is very far back (in practice, more than
935  * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
936  * freeze age of zero.
937  */
939  if (!TransactionIdIsNormal(safeLimit))
940  safeLimit = FirstNormalTransactionId;
941 
942  if (TransactionIdPrecedes(limit, safeLimit))
943  {
945  (errmsg("oldest xmin is far in the past"),
946  errhint("Close open transactions soon to avoid wraparound problems.\n"
947  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
948  limit = *oldestXmin;
949  }
950 
951  *freezeLimit = limit;
952 
953  /*
954  * Compute the multixact age for which freezing is urgent. This is
955  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
956  * short of multixact member space.
957  */
958  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
959 
960  /*
961  * Determine the minimum multixact freeze age to use: as specified by
962  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
963  * than half effective_multixact_freeze_max_age, so that autovacuums to
964  * prevent MultiXact wraparound won't occur too frequently.
965  */
966  mxid_freezemin = multixact_freeze_min_age;
967  if (mxid_freezemin < 0)
968  mxid_freezemin = vacuum_multixact_freeze_min_age;
969  mxid_freezemin = Min(mxid_freezemin,
970  effective_multixact_freeze_max_age / 2);
971  Assert(mxid_freezemin >= 0);
972 
973  /* compute the cutoff multi, being careful to generate a valid value */
974  oldestMxact = GetOldestMultiXactId();
975  mxactLimit = oldestMxact - mxid_freezemin;
976  if (mxactLimit < FirstMultiXactId)
977  mxactLimit = FirstMultiXactId;
978 
979  safeMxactLimit =
980  ReadNextMultiXactId() - effective_multixact_freeze_max_age;
981  if (safeMxactLimit < FirstMultiXactId)
982  safeMxactLimit = FirstMultiXactId;
983 
984  if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
985  {
987  (errmsg("oldest multixact is far in the past"),
988  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
989  /* Use the safe limit, unless an older mxact is still running */
990  if (MultiXactIdPrecedes(oldestMxact, safeMxactLimit))
991  mxactLimit = oldestMxact;
992  else
993  mxactLimit = safeMxactLimit;
994  }
995 
996  *multiXactCutoff = mxactLimit;
997 
998  if (xidFullScanLimit != NULL)
999  {
1000  int freezetable;
1001 
1002  Assert(mxactFullScanLimit != NULL);
1003 
1004  /*
1005  * Determine the table freeze age to use: as specified by the caller,
1006  * or vacuum_freeze_table_age, but in any case not more than
1007  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1008  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
1009  * before anti-wraparound autovacuum is launched.
1010  */
1011  freezetable = freeze_table_age;
1012  if (freezetable < 0)
1013  freezetable = vacuum_freeze_table_age;
1014  freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
1015  Assert(freezetable >= 0);
1016 
1017  /*
1018  * Compute XID limit causing a full-table vacuum, being careful not to
1019  * generate a "permanent" XID.
1020  */
1021  limit = ReadNewTransactionId() - freezetable;
1022  if (!TransactionIdIsNormal(limit))
1023  limit = FirstNormalTransactionId;
1024 
1025  *xidFullScanLimit = limit;
1026 
1027  /*
1028  * Similar to the above, determine the table freeze age to use for
1029  * multixacts: as specified by the caller, or
1030  * vacuum_multixact_freeze_table_age, but in any case not more than
1031  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
1032  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
1033  * freeze multixacts before anti-wraparound autovacuum is launched.
1034  */
1035  freezetable = multixact_freeze_table_age;
1036  if (freezetable < 0)
1037  freezetable = vacuum_multixact_freeze_table_age;
1038  freezetable = Min(freezetable,
1039  effective_multixact_freeze_max_age * 0.95);
1040  Assert(freezetable >= 0);
1041 
1042  /*
1043  * Compute MultiXact limit causing a full-table vacuum, being careful
1044  * to generate a valid MultiXact value.
1045  */
1046  mxactLimit = ReadNextMultiXactId() - freezetable;
1047  if (mxactLimit < FirstMultiXactId)
1048  mxactLimit = FirstMultiXactId;
1049 
1050  *mxactFullScanLimit = mxactLimit;
1051  }
1052  else
1053  {
1054  Assert(mxactFullScanLimit == NULL);
1055  }
1056 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:63
uint32 TransactionId
Definition: c.h:507
#define Min(x, y)
Definition: c.h:904
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:121
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:62
#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:1304
TransactionId MultiXactId
Definition: c.h:517
#define Assert(condition)
Definition: c.h:732
int vacuum_freeze_min_age
Definition: vacuum.c:60
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3142
int vacuum_freeze_table_age
Definition: vacuum.c:61
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
int errmsg(const char *fmt,...)
Definition: elog.c:784
#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 67 of file vacuum.c.

◆ vac_strategy

BufferAccessStrategy vac_strategy
static

Definition at line 68 of file vacuum.c.

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

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

Referenced by do_autovacuum(), and vacuum_set_xid_limits().