PostgreSQL Source Code  git master
vacuum.h File Reference
#include "access/htup.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "nodes/parsenodes.h"
#include "storage/buf.h"
#include "storage/lock.h"
#include "utils/relcache.h"
Include dependency graph for vacuum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  VacAttrStats
 
struct  VacuumParams
 

Typedefs

typedef struct VacAttrStatsVacAttrStatsP
 
typedef Datum(* AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, bool *isNull)
 
typedef void(* AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
 
typedef struct VacAttrStats VacAttrStats
 
typedef struct VacuumParams VacuumParams
 

Functions

void ExecVacuum (VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (int options, List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel)
 
void vac_open_indexes (Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
 
void vac_close_indexes (int nindexes, Relation *Irel, LOCKMODE lockmode)
 
double vac_estimate_reltuples (Relation relation, bool is_analyze, 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 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)
 
void vac_update_datfrozenxid (void)
 
void vacuum_delay_point (void)
 
void lazy_vacuum_rel (Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy)
 
void analyze_rel (Oid relid, RangeVar *relation, int options, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
 
bool std_typanalyze (VacAttrStats *stats)
 
double anl_random_fract (void)
 
double anl_init_selection_state (int n)
 
double anl_get_next_S (double t, int n, double *stateptr)
 

Variables

PGDLLIMPORT int default_statistics_target
 
int vacuum_freeze_min_age
 
int vacuum_freeze_table_age
 
int vacuum_multixact_freeze_min_age
 
int vacuum_multixact_freeze_table_age
 

Typedef Documentation

◆ AnalyzeAttrComputeStatsFunc

typedef void(* AnalyzeAttrComputeStatsFunc) (VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)

Definition at line 64 of file vacuum.h.

◆ AnalyzeAttrFetchFunc

typedef Datum(* AnalyzeAttrFetchFunc) (VacAttrStatsP stats, int rownum, bool *isNull)

Definition at line 61 of file vacuum.h.

◆ VacAttrStats

◆ VacAttrStatsP

Definition at line 59 of file vacuum.h.

◆ VacuumParams

Function Documentation

◆ analyze_rel()

void analyze_rel ( Oid  relid,
RangeVar relation,
int  options,
VacuumParams params,
List va_cols,
bool  in_outer_xact,
BufferAccessStrategy  bstrategy 
)

Definition at line 115 of file analyze.c.

References acquire_sample_rows(), FdwRoutine::AnalyzeForeignTable, CHECK_FOR_INTERRUPTS, ConditionalLockRelationOid(), DEBUG2, do_analyze_rel(), elevel, ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), GetFdwRoutineForRelation(), GetUserId(), INFO, IsAutoVacuumWorkerProcess(), LOG, VacuumParams::log_min_duration, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyDatabaseId, MyPgXact, NoLock, PG_CATALOG_NAMESPACE, pg_class_ownercheck(), pg_database_ownercheck(), PROC_IN_ANALYZE, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetRelid, RELKIND_FOREIGN_TABLE, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, RangeVar::relname, ShareUpdateExclusiveLock, StatisticRelationId, try_relation_open(), VACOPT_NOWAIT, VACOPT_VACUUM, VACOPT_VERBOSE, PGXACT::vacuumFlags, and WARNING.

Referenced by vacuum().

118 {
119  Relation onerel;
120  int elevel;
121  AcquireSampleRowsFunc acquirefunc = NULL;
122  BlockNumber relpages = 0;
123  bool rel_lock = true;
124 
125  /* Select logging level */
126  if (options & VACOPT_VERBOSE)
127  elevel = INFO;
128  else
129  elevel = DEBUG2;
130 
131  /* Set up static variables */
132  vac_strategy = bstrategy;
133 
134  /*
135  * Check for user-requested abort.
136  */
138 
139  /*
140  * Open the relation, getting ShareUpdateExclusiveLock to ensure that two
141  * ANALYZEs don't run on it concurrently. (This also locks out a
142  * concurrent VACUUM, which doesn't matter much at the moment but might
143  * matter if we ever try to accumulate stats on dead tuples.) If the rel
144  * has been dropped since we last saw it, we don't need to process it.
145  */
146  if (!(options & VACOPT_NOWAIT))
149  onerel = try_relation_open(relid, NoLock);
150  else
151  {
152  onerel = NULL;
153  rel_lock = false;
154  }
155 
156  /*
157  * If we failed to open or lock the relation, emit a log message before
158  * exiting.
159  */
160  if (!onerel)
161  {
162  /*
163  * If the RangeVar is not defined, we do not have enough information
164  * to provide a meaningful log statement. Chances are that
165  * analyze_rel's caller has intentionally not provided this
166  * information so that this logging is skipped, anyway.
167  */
168  if (relation == NULL)
169  return;
170 
171  /*
172  * Determine the log level. For autovacuum logs, we emit a LOG if
173  * log_autovacuum_min_duration is not disabled. For manual ANALYZE,
174  * we emit a WARNING to match the log statements in the permissions
175  * checks.
176  */
178  elevel = WARNING;
179  else if (params->log_min_duration >= 0)
180  elevel = LOG;
181  else
182  return;
183 
184  if (!rel_lock)
185  ereport(elevel,
186  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
187  errmsg("skipping analyze of \"%s\" --- lock not available",
188  relation->relname)));
189  else
190  ereport(elevel,
192  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
193  relation->relname)));
194 
195  return;
196  }
197 
198  /*
199  * Check permissions --- this should match vacuum's check!
200  */
201  if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
202  (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !onerel->rd_rel->relisshared)))
203  {
204  /* No need for a WARNING if we already complained during VACUUM */
205  if (!(options & VACOPT_VACUUM))
206  {
207  if (onerel->rd_rel->relisshared)
209  (errmsg("skipping \"%s\" --- only superuser can analyze it",
210  RelationGetRelationName(onerel))));
211  else if (onerel->rd_rel->relnamespace == PG_CATALOG_NAMESPACE)
213  (errmsg("skipping \"%s\" --- only superuser or database owner can analyze it",
214  RelationGetRelationName(onerel))));
215  else
217  (errmsg("skipping \"%s\" --- only table or database owner can analyze it",
218  RelationGetRelationName(onerel))));
219  }
221  return;
222  }
223 
224  /*
225  * Silently ignore tables that are temp tables of other backends ---
226  * trying to analyze these is rather pointless, since their contents are
227  * probably not up-to-date on disk. (We don't throw a warning here; it
228  * would just lead to chatter during a database-wide ANALYZE.)
229  */
230  if (RELATION_IS_OTHER_TEMP(onerel))
231  {
233  return;
234  }
235 
236  /*
237  * We can ANALYZE any table except pg_statistic. See update_attstats
238  */
239  if (RelationGetRelid(onerel) == StatisticRelationId)
240  {
242  return;
243  }
244 
245  /*
246  * Check that it's of an analyzable relkind, and set up appropriately.
247  */
248  if (onerel->rd_rel->relkind == RELKIND_RELATION ||
249  onerel->rd_rel->relkind == RELKIND_MATVIEW)
250  {
251  /* Regular table, so we'll use the regular row acquisition function */
252  acquirefunc = acquire_sample_rows;
253  /* Also get regular table's size */
254  relpages = RelationGetNumberOfBlocks(onerel);
255  }
256  else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
257  {
258  /*
259  * For a foreign table, call the FDW's hook function to see whether it
260  * supports analysis.
261  */
262  FdwRoutine *fdwroutine;
263  bool ok = false;
264 
265  fdwroutine = GetFdwRoutineForRelation(onerel, false);
266 
267  if (fdwroutine->AnalyzeForeignTable != NULL)
268  ok = fdwroutine->AnalyzeForeignTable(onerel,
269  &acquirefunc,
270  &relpages);
271 
272  if (!ok)
273  {
275  (errmsg("skipping \"%s\" --- cannot analyze this foreign table",
276  RelationGetRelationName(onerel))));
278  return;
279  }
280  }
281  else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
282  {
283  /*
284  * For partitioned tables, we want to do the recursive ANALYZE below.
285  */
286  }
287  else
288  {
289  /* No need for a WARNING if we already complained during VACUUM */
290  if (!(options & VACOPT_VACUUM))
292  (errmsg("skipping \"%s\" --- cannot analyze non-tables or special system tables",
293  RelationGetRelationName(onerel))));
295  return;
296  }
297 
298  /*
299  * OK, let's do it. First let other backends know I'm in ANALYZE.
300  */
301  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
303  LWLockRelease(ProcArrayLock);
304 
305  /*
306  * Do the normal non-recursive ANALYZE. We can skip this for partitioned
307  * tables, which don't contain any rows.
308  */
309  if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
310  do_analyze_rel(onerel, options, params, va_cols, acquirefunc,
311  relpages, false, in_outer_xact, elevel);
312 
313  /*
314  * If there are child tables, do recursive ANALYZE.
315  */
316  if (onerel->rd_rel->relhassubclass)
317  do_analyze_rel(onerel, options, params, va_cols, acquirefunc, relpages,
318  true, in_outer_xact, elevel);
319 
320  /*
321  * Close source relation now, but keep lock so that no one deletes it
322  * before we commit. (If someone did, they'd fail to clean up the entries
323  * we made in pg_statistic. Also, releasing the lock before commit would
324  * expose us to concurrent-update failures in update_attstats.)
325  */
326  relation_close(onerel, NoLock);
327 
328  /*
329  * Reset my PGXACT flag. Note: we need this here, and not in vacuum_rel,
330  * because the vacuum flag is cleared by the end-of-xact code.
331  */
332  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
334  LWLockRelease(ProcArrayLock);
335 }
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:61
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1153
Oid GetUserId(void)
Definition: miscinit.c:284
AnalyzeForeignTable_function AnalyzeForeignTable
Definition: fdwapi.h:224
#define RELKIND_MATVIEW
Definition: pg_class.h:165
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1266
#define INFO
Definition: elog.h:33
static BufferAccessStrategy vac_strategy
Definition: analyze.c:81
uint32 BlockNumber
Definition: block.h:31
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:114
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:230
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
char * relname
Definition: primnodes.h:68
#define DEBUG2
Definition: elog.h:24
#define NoLock
Definition: lockdefs.h:34
#define RelationGetRelationName(relation)
Definition: rel.h:445
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3255
#define ereport(elevel, rest)
Definition: elog.h:122
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:5170
#define WARNING
Definition: elog.h:40
static void do_analyze_rel(Relation onerel, int options, VacuumParams *params, List *va_cols, AcquireSampleRowsFunc acquirefunc, BlockNumber relpages, bool inh, bool in_outer_xact, int elevel)
Definition: analyze.c:345
static int elevel
Definition: vacuumlazy.c:136
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
#define StatisticRelationId
Definition: pg_statistic.h:29
Oid MyDatabaseId
Definition: globals.c:77
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:542
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4752
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
int log_min_duration
Definition: vacuum.h:145
int(* AcquireSampleRowsFunc)(Relation relation, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: fdwapi.h:134
int errmsg(const char *fmt,...)
Definition: elog.c:797
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
#define PROC_IN_ANALYZE
Definition: proc.h:55
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define RELKIND_RELATION
Definition: pg_class.h:160
#define RelationGetRelid(relation)
Definition: rel.h:425
static int acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: analyze.c:1042

◆ anl_get_next_S()

double anl_get_next_S ( double  t,
int  n,
double *  stateptr 
)

Definition at line 284 of file sampling.c.

285 {
286  double result;
287 
288  oldrs.W = *stateptr;
289  result = reservoir_get_next_S(&oldrs, t, n);
290  *stateptr = oldrs.W;
291  return result;
292 }
static ReservoirStateData oldrs
Definition: sampling.c:259
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:142

◆ anl_init_selection_state()

double anl_init_selection_state ( int  n)

Definition at line 273 of file sampling.c.

274 {
275  /* initialize if first time through */
276  if (oldrs.randstate[0] == 0)
278 
279  /* Initial value of W (for use when Algorithm Z is first applied) */
280  return exp(-log(sampler_random_fract(oldrs.randstate)) / n);
281 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:229
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
static ReservoirStateData oldrs
Definition: sampling.c:259
SamplerRandomState randstate
Definition: sampling.h:50

◆ anl_random_fract()

double anl_random_fract ( void  )

Definition at line 262 of file sampling.c.

263 {
264  /* initialize if first time through */
265  if (oldrs.randstate[0] == 0)
267 
268  /* and compute a random fraction */
270 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:229
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:238
static ReservoirStateData oldrs
Definition: sampling.c:259
SamplerRandomState randstate
Definition: sampling.h:50

◆ ExecVacuum()

void ExecVacuum ( VacuumStmt vacstmt,
bool  isTopLevel 
)

Definition at line 87 of file vacuum.c.

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

Referenced by standard_ProcessUtility().

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

◆ lazy_vacuum_rel()

void lazy_vacuum_rel ( Relation  onerel,
int  options,
VacuumParams params,
BufferAccessStrategy  bstrategy 
)

Definition at line 182 of file vacuumlazy.c.

References _, appendStringInfo(), Assert, buf, StringInfoData::data, DEBUG2, elevel, ereport, errmsg_internal(), FreeSpaceMapVacuum(), VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, FreezeLimit, LVRelStats::frozenskipped_pages, get_database_name(), get_namespace_name(), GetCurrentTimestamp(), LVRelStats::hasindex, INFO, initStringInfo(), InvalidMultiXactId, InvalidTransactionId, IsAutoVacuumWorkerProcess(), lazy_scan_heap(), lazy_truncate_heap(), LVRelStats::lock_waiter_detected, LOG, VacuumParams::log_min_duration, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, MultiXactCutoff, MultiXactIdPrecedesOrEquals(), MyDatabaseId, LVRelStats::new_dead_tuples, LVRelStats::new_rel_tuples, NoLock, LVRelStats::num_index_scans, LVRelStats::old_rel_pages, LVRelStats::old_rel_tuples, OldestXmin, LVRelStats::pages_removed, palloc0(), pfree(), pg_rusage_init(), pg_rusage_show(), pgstat_progress_end_command(), pgstat_progress_start_command(), pgstat_progress_update_param(), pgstat_report_vacuum(), LVRelStats::pinskipped_pages, PROGRESS_COMMAND_VACUUM, PROGRESS_VACUUM_PHASE, PROGRESS_VACUUM_PHASE_FINAL_CLEANUP, RelationData::rd_rel, LVRelStats::rel_pages, RelationGetNamespace, RelationGetRelationName, RelationGetRelid, RowExclusiveLock, LVRelStats::scanned_pages, should_attempt_truncation(), TimestampDifference(), TimestampDifferenceExceeds(), TransactionIdPrecedesOrEquals(), LVRelStats::tupcount_pages, LVRelStats::tuples_deleted, vac_close_indexes(), vac_open_indexes(), vac_update_relstats(), VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_VERBOSE, vacuum_set_xid_limits(), VacuumPageDirty, VacuumPageHit, VacuumPageMiss, and visibilitymap_count().

Referenced by vacuum_rel().

184 {
185  LVRelStats *vacrelstats;
186  Relation *Irel;
187  int nindexes;
188  PGRUsage ru0;
189  TimestampTz starttime = 0;
190  long secs;
191  int usecs;
192  double read_rate,
193  write_rate;
194  bool aggressive; /* should we scan all unfrozen pages? */
195  bool scanned_all_unfrozen; /* actually scanned all such pages? */
196  TransactionId xidFullScanLimit;
197  MultiXactId mxactFullScanLimit;
198  BlockNumber new_rel_pages;
199  double new_rel_tuples;
200  BlockNumber new_rel_allvisible;
201  double new_live_tuples;
202  TransactionId new_frozen_xid;
203  MultiXactId new_min_multi;
204 
205  Assert(params != NULL);
206 
207  /* measure elapsed time iff autovacuum logging requires it */
208  if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
209  {
210  pg_rusage_init(&ru0);
211  starttime = GetCurrentTimestamp();
212  }
213 
214  if (options & VACOPT_VERBOSE)
215  elevel = INFO;
216  else
217  elevel = DEBUG2;
218 
220  RelationGetRelid(onerel));
221 
222  vac_strategy = bstrategy;
223 
224  vacuum_set_xid_limits(onerel,
225  params->freeze_min_age,
226  params->freeze_table_age,
227  params->multixact_freeze_min_age,
229  &OldestXmin, &FreezeLimit, &xidFullScanLimit,
230  &MultiXactCutoff, &mxactFullScanLimit);
231 
232  /*
233  * We request an aggressive scan if the table's frozen Xid is now older
234  * than or equal to the requested Xid full-table scan limit; or if the
235  * table's minimum MultiXactId is older than or equal to the requested
236  * mxid full-table scan limit; or if DISABLE_PAGE_SKIPPING was specified.
237  */
238  aggressive = TransactionIdPrecedesOrEquals(onerel->rd_rel->relfrozenxid,
239  xidFullScanLimit);
240  aggressive |= MultiXactIdPrecedesOrEquals(onerel->rd_rel->relminmxid,
241  mxactFullScanLimit);
243  aggressive = true;
244 
245  vacrelstats = (LVRelStats *) palloc0(sizeof(LVRelStats));
246 
247  vacrelstats->old_rel_pages = onerel->rd_rel->relpages;
248  vacrelstats->old_rel_tuples = onerel->rd_rel->reltuples;
249  vacrelstats->num_index_scans = 0;
250  vacrelstats->pages_removed = 0;
251  vacrelstats->lock_waiter_detected = false;
252 
253  /* Open all indexes of the relation */
254  vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel);
255  vacrelstats->hasindex = (nindexes > 0);
256 
257  /* Do the vacuuming */
258  lazy_scan_heap(onerel, options, vacrelstats, Irel, nindexes, aggressive);
259 
260  /* Done with indexes */
261  vac_close_indexes(nindexes, Irel, NoLock);
262 
263  /*
264  * Compute whether we actually scanned the all unfrozen pages. If we did,
265  * we can adjust relfrozenxid and relminmxid.
266  *
267  * NB: We need to check this before truncating the relation, because that
268  * will change ->rel_pages.
269  */
270  if ((vacrelstats->scanned_pages + vacrelstats->frozenskipped_pages)
271  < vacrelstats->rel_pages)
272  {
273  Assert(!aggressive);
274  scanned_all_unfrozen = false;
275  }
276  else
277  scanned_all_unfrozen = true;
278 
279  /*
280  * Optionally truncate the relation.
281  */
282  if (should_attempt_truncation(vacrelstats))
283  lazy_truncate_heap(onerel, vacrelstats);
284 
285  /* Report that we are now doing final cleanup */
288 
289  /* Vacuum the Free Space Map */
290  FreeSpaceMapVacuum(onerel);
291 
292  /*
293  * Update statistics in pg_class.
294  *
295  * A corner case here is that if we scanned no pages at all because every
296  * page is all-visible, we should not update relpages/reltuples, because
297  * we have no new information to contribute. In particular this keeps us
298  * from replacing relpages=reltuples=0 (which means "unknown tuple
299  * density") with nonzero relpages and reltuples=0 (which means "zero
300  * tuple density") unless there's some actual evidence for the latter.
301  *
302  * It's important that we use tupcount_pages and not scanned_pages for the
303  * check described above; scanned_pages counts pages where we could not
304  * get cleanup lock, and which were processed only for frozenxid purposes.
305  *
306  * We do update relallvisible even in the corner case, since if the table
307  * is all-visible we'd definitely like to know that. But clamp the value
308  * to be not more than what we're setting relpages to.
309  *
310  * Also, don't change relfrozenxid/relminmxid if we skipped any pages,
311  * since then we don't know for certain that all tuples have a newer xmin.
312  */
313  new_rel_pages = vacrelstats->rel_pages;
314  new_rel_tuples = vacrelstats->new_rel_tuples;
315  if (vacrelstats->tupcount_pages == 0 && new_rel_pages > 0)
316  {
317  new_rel_pages = vacrelstats->old_rel_pages;
318  new_rel_tuples = vacrelstats->old_rel_tuples;
319  }
320 
321  visibilitymap_count(onerel, &new_rel_allvisible, NULL);
322  if (new_rel_allvisible > new_rel_pages)
323  new_rel_allvisible = new_rel_pages;
324 
325  new_frozen_xid = scanned_all_unfrozen ? FreezeLimit : InvalidTransactionId;
326  new_min_multi = scanned_all_unfrozen ? MultiXactCutoff : InvalidMultiXactId;
327 
328  vac_update_relstats(onerel,
329  new_rel_pages,
330  new_rel_tuples,
331  new_rel_allvisible,
332  vacrelstats->hasindex,
333  new_frozen_xid,
334  new_min_multi,
335  false);
336 
337  /* report results to the stats collector, too */
338  new_live_tuples = new_rel_tuples - vacrelstats->new_dead_tuples;
339  if (new_live_tuples < 0)
340  new_live_tuples = 0; /* just in case */
341 
343  onerel->rd_rel->relisshared,
344  new_live_tuples,
345  vacrelstats->new_dead_tuples);
347 
348  /* and log the action if appropriate */
349  if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
350  {
351  TimestampTz endtime = GetCurrentTimestamp();
352 
353  if (params->log_min_duration == 0 ||
354  TimestampDifferenceExceeds(starttime, endtime,
355  params->log_min_duration))
356  {
358  char *msgfmt;
359 
360  TimestampDifference(starttime, endtime, &secs, &usecs);
361 
362  read_rate = 0;
363  write_rate = 0;
364  if ((secs > 0) || (usecs > 0))
365  {
366  read_rate = (double) BLCKSZ * VacuumPageMiss / (1024 * 1024) /
367  (secs + usecs / 1000000.0);
368  write_rate = (double) BLCKSZ * VacuumPageDirty / (1024 * 1024) /
369  (secs + usecs / 1000000.0);
370  }
371 
372  /*
373  * This is pretty messy, but we split it up so that we can skip
374  * emitting individual parts of the message when not applicable.
375  */
376  initStringInfo(&buf);
377  if (aggressive)
378  msgfmt = _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n");
379  else
380  msgfmt = _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n");
381  appendStringInfo(&buf, msgfmt,
384  RelationGetRelationName(onerel),
385  vacrelstats->num_index_scans);
386  appendStringInfo(&buf, _("pages: %u removed, %u remain, %u skipped due to pins, %u skipped frozen\n"),
387  vacrelstats->pages_removed,
388  vacrelstats->rel_pages,
389  vacrelstats->pinskipped_pages,
390  vacrelstats->frozenskipped_pages);
391  appendStringInfo(&buf,
392  _("tuples: %.0f removed, %.0f remain, %.0f are dead but not yet removable, oldest xmin: %u\n"),
393  vacrelstats->tuples_deleted,
394  vacrelstats->new_rel_tuples,
395  vacrelstats->new_dead_tuples,
396  OldestXmin);
397  appendStringInfo(&buf,
398  _("buffer usage: %d hits, %d misses, %d dirtied\n"),
402  appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
403  read_rate, write_rate);
404  appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
405 
406  ereport(LOG,
407  (errmsg_internal("%s", buf.data)));
408  pfree(buf.data);
409  }
410  }
411 }
double new_rel_tuples
Definition: vacuumlazy.c:119
int multixact_freeze_table_age
Definition: vacuum.h:142
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:1675
BlockNumber rel_pages
Definition: vacuumlazy.c:112
uint32 TransactionId
Definition: c.h:463
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
Definition: pgstat.c:3072
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
double tuples_deleted
Definition: vacuumlazy.c:122
int64 TimestampTz
Definition: timestamp.h:39
int VacuumPageHit
Definition: globals.c:135
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3093
double old_rel_tuples
Definition: vacuumlazy.c:118
BlockNumber tupcount_pages
Definition: vacuumlazy.c:116
static void lazy_scan_heap(Relation onerel, int options, LVRelStats *vacrelstats, Relation *Irel, int nindexes, bool aggressive)
Definition: vacuumlazy.c:463
BlockNumber scanned_pages
Definition: vacuumlazy.c:113
#define INFO
Definition: elog.h:33
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: vacuum.c:593
uint32 BlockNumber
Definition: block.h:31
BlockNumber pinskipped_pages
Definition: vacuumlazy.c:114
void visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:114
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
int freeze_table_age
Definition: vacuum.h:139
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
BlockNumber old_rel_pages
Definition: vacuumlazy.c:111
void pfree(void *pointer)
Definition: mcxt.c:936
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:319
int freeze_min_age
Definition: vacuum.h:138
char * get_database_name(Oid dbid)
Definition: dbcommands.c:2056
#define DEBUG2
Definition: elog.h:24
static TransactionId FreezeLimit
Definition: vacuumlazy.c:139
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3051
#define NoLock
Definition: lockdefs.h:34
static char * buf
Definition: pg_test_fsync.c:67
#define RowExclusiveLock
Definition: lockdefs.h:38
static MultiXactId MultiXactCutoff
Definition: vacuumlazy.c:140
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
#define InvalidTransactionId
Definition: transam.h:31
#define RelationGetRelationName(relation)
Definition: rel.h:445
static TransactionId OldestXmin
Definition: vacuumlazy.c:138
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3255
#define ereport(elevel, rest)
Definition: elog.h:122
void FreeSpaceMapVacuum(Relation rel)
Definition: freespace.c:379
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:1632
static int elevel
Definition: vacuumlazy.c:136
bool hasindex
Definition: vacuumlazy.c:109
int VacuumPageDirty
Definition: globals.c:137
void * palloc0(Size size)
Definition: mcxt.c:864
void pgstat_progress_end_command(void)
Definition: pgstat.c:3144
Oid MyDatabaseId
Definition: globals.c:77
#define InvalidMultiXactId
Definition: multixact.h:23
static bool should_attempt_truncation(LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1716
#define PROGRESS_VACUUM_PHASE_FINAL_CLEANUP
Definition: progress.h:35
int num_index_scans
Definition: vacuumlazy.c:129
TransactionId MultiXactId
Definition: c.h:473
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
static BufferAccessStrategy vac_strategy
Definition: vacuumlazy.c:142
#define Assert(condition)
Definition: c.h:688
double new_dead_tuples
Definition: vacuumlazy.c:120
#define PROGRESS_VACUUM_PHASE
Definition: progress.h:21
int log_min_duration
Definition: vacuum.h:145
void pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
Definition: pgstat.c:1405
BlockNumber pages_removed
Definition: vacuumlazy.c:121
BlockNumber frozenskipped_pages
Definition: vacuumlazy.c:115
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3154
int VacuumPageMiss
Definition: globals.c:136
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1624
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: vacuum.c:878
#define _(x)
Definition: elog.c:84
#define RelationGetRelid(relation)
Definition: rel.h:425
int multixact_freeze_min_age
Definition: vacuum.h:140
static void lazy_truncate_heap(Relation onerel, LVRelStats *vacrelstats)
Definition: vacuumlazy.c:1734
#define RelationGetNamespace(relation)
Definition: rel.h:452
bool lock_waiter_detected
Definition: vacuumlazy.c:131

◆ std_typanalyze()

bool std_typanalyze ( VacAttrStats stats)

Definition at line 1775 of file analyze.c.

References VacAttrStats::attr, VacAttrStats::attrtypid, compute_distinct_stats(), compute_scalar_stats(), VacAttrStats::compute_stats, compute_trivial_stats(), default_statistics_target, StdAnalyzeData::eqfunc, StdAnalyzeData::eqopr, VacAttrStats::extra_data, get_opcode(), get_sort_group_operators(), InvalidOid, StdAnalyzeData::ltopr, VacAttrStats::minrows, OidIsValid, and palloc().

Referenced by array_typanalyze(), and examine_attribute().

1776 {
1777  Form_pg_attribute attr = stats->attr;
1778  Oid ltopr;
1779  Oid eqopr;
1780  StdAnalyzeData *mystats;
1781 
1782  /* If the attstattarget column is negative, use the default value */
1783  /* NB: it is okay to scribble on stats->attr since it's a copy */
1784  if (attr->attstattarget < 0)
1785  attr->attstattarget = default_statistics_target;
1786 
1787  /* Look for default "<" and "=" operators for column's type */
1789  false, false, false,
1790  &ltopr, &eqopr, NULL,
1791  NULL);
1792 
1793  /* Save the operator info for compute_stats routines */
1794  mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData));
1795  mystats->eqopr = eqopr;
1796  mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
1797  mystats->ltopr = ltopr;
1798  stats->extra_data = mystats;
1799 
1800  /*
1801  * Determine which standard statistics algorithm to use
1802  */
1803  if (OidIsValid(eqopr) && OidIsValid(ltopr))
1804  {
1805  /* Seems to be a scalar datatype */
1807  /*--------------------
1808  * The following choice of minrows is based on the paper
1809  * "Random sampling for histogram construction: how much is enough?"
1810  * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
1811  * Proceedings of ACM SIGMOD International Conference on Management
1812  * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
1813  * says that for table size n, histogram size k, maximum relative
1814  * error in bin size f, and error probability gamma, the minimum
1815  * random sample size is
1816  * r = 4 * k * ln(2*n/gamma) / f^2
1817  * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
1818  * r = 305.82 * k
1819  * Note that because of the log function, the dependence on n is
1820  * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
1821  * bin size error with probability 0.99. So there's no real need to
1822  * scale for n, which is a good thing because we don't necessarily
1823  * know it at this point.
1824  *--------------------
1825  */
1826  stats->minrows = 300 * attr->attstattarget;
1827  }
1828  else if (OidIsValid(eqopr))
1829  {
1830  /* We can still recognize distinct values */
1832  /* Might as well use the same minrows as above */
1833  stats->minrows = 300 * attr->attstattarget;
1834  }
1835  else
1836  {
1837  /* Can't do much but the trivial stuff */
1839  /* Might as well use the same minrows as above */
1840  stats->minrows = 300 * attr->attstattarget;
1841  }
1842 
1843  return true;
1844 }
int minrows
Definition: vacuum.h:92
static void compute_scalar_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:2297
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:594
Form_pg_attribute attr
Definition: vacuum.h:81
Oid attrtypid
Definition: vacuum.h:82
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
static void compute_distinct_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:1945
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1079
void get_sort_group_operators(Oid argtype, bool needLT, bool needEQ, bool needGT, Oid *ltOpr, Oid *eqOpr, Oid *gtOpr, bool *isHashable)
Definition: parse_oper.c:187
static void compute_trivial_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:1855
void * palloc(Size size)
Definition: mcxt.c:835
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:91
void * extra_data
Definition: vacuum.h:93
int default_statistics_target
Definition: analyze.c:77

◆ vac_close_indexes()

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

Definition at line 1675 of file vacuum.c.

References index_close(), and pfree().

Referenced by do_analyze_rel(), and lazy_vacuum_rel().

1676 {
1677  if (Irel == NULL)
1678  return;
1679 
1680  while (nindexes--)
1681  {
1682  Relation ind = Irel[nindexes];
1683 
1684  index_close(ind, lockmode);
1685  }
1686  pfree(Irel);
1687 }
void pfree(void *pointer)
Definition: mcxt.c:936
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177

◆ vac_estimate_reltuples()

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

Definition at line 778 of file vacuum.c.

References RelationData::rd_rel.

Referenced by acquire_sample_rows(), lazy_scan_heap(), and statapprox_heap().

782 {
783  BlockNumber old_rel_pages = relation->rd_rel->relpages;
784  double old_rel_tuples = relation->rd_rel->reltuples;
785  double old_density;
786  double new_density;
787  double multiplier;
788  double updated_density;
789 
790  /* If we did scan the whole table, just use the count as-is */
791  if (scanned_pages >= total_pages)
792  return scanned_tuples;
793 
794  /*
795  * If scanned_pages is zero but total_pages isn't, keep the existing value
796  * of reltuples. (Note: callers should avoid updating the pg_class
797  * statistics in this situation, since no new information has been
798  * provided.)
799  */
800  if (scanned_pages == 0)
801  return old_rel_tuples;
802 
803  /*
804  * If old value of relpages is zero, old density is indeterminate; we
805  * can't do much except scale up scanned_tuples to match total_pages.
806  */
807  if (old_rel_pages == 0)
808  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
809 
810  /*
811  * Okay, we've covered the corner cases. The normal calculation is to
812  * convert the old measurement to a density (tuples per page), then update
813  * the density using an exponential-moving-average approach, and finally
814  * compute reltuples as updated_density * total_pages.
815  *
816  * For ANALYZE, the moving average multiplier is just the fraction of the
817  * table's pages we scanned. This is equivalent to assuming that the
818  * tuple density in the unscanned pages didn't change. Of course, it
819  * probably did, if the new density measurement is different. But over
820  * repeated cycles, the value of reltuples will converge towards the
821  * correct value, if repeated measurements show the same new density.
822  *
823  * For VACUUM, the situation is a bit different: we have looked at a
824  * nonrandom sample of pages, but we know for certain that the pages we
825  * didn't look at are precisely the ones that haven't changed lately.
826  * Thus, there is a reasonable argument for doing exactly the same thing
827  * as for the ANALYZE case, that is use the old density measurement as the
828  * value for the unscanned pages.
829  *
830  * This logic could probably use further refinement.
831  */
832  old_density = old_rel_tuples / old_rel_pages;
833  new_density = scanned_tuples / scanned_pages;
834  multiplier = (double) scanned_pages / (double) total_pages;
835  updated_density = old_density + (new_density - old_density) * multiplier;
836  return floor(updated_density * total_pages + 0.5);
837 }
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:114

◆ vac_open_indexes()

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

Definition at line 1632 of file vacuum.c.

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

Referenced by do_analyze_rel(), and lazy_vacuum_rel().

1634 {
1635  List *indexoidlist;
1636  ListCell *indexoidscan;
1637  int i;
1638 
1639  Assert(lockmode != NoLock);
1640 
1641  indexoidlist = RelationGetIndexList(relation);
1642 
1643  /* allocate enough memory for all indexes */
1644  i = list_length(indexoidlist);
1645 
1646  if (i > 0)
1647  *Irel = (Relation *) palloc(i * sizeof(Relation));
1648  else
1649  *Irel = NULL;
1650 
1651  /* collect just the ready indexes */
1652  i = 0;
1653  foreach(indexoidscan, indexoidlist)
1654  {
1655  Oid indexoid = lfirst_oid(indexoidscan);
1656  Relation indrel;
1657 
1658  indrel = index_open(indexoid, lockmode);
1659  if (IndexIsReady(indrel->rd_index))
1660  (*Irel)[i++] = indrel;
1661  else
1662  index_close(indrel, lockmode);
1663  }
1664 
1665  *nindexes = i;
1666 
1667  list_free(indexoidlist);
1668 }
#define IndexIsReady(indexForm)
Definition: pg_index.h:108
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:159
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:688
static int list_length(const List *l)
Definition: pg_list.h:89
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4329
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
void * palloc(Size size)
Definition: mcxt.c:835
void list_free(List *list)
Definition: list.c:1133
int i
Definition: pg_list.h:45
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
#define lfirst_oid(lc)
Definition: pg_list.h:108

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1015 of file vacuum.c.

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

Referenced by do_autovacuum(), and vacuum().

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

◆ vac_update_relstats()

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

Definition at line 878 of file vacuum.c.

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

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

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

◆ vacuum()

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

Definition at line 166 of file vacuum.c.

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

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

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

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 1696 of file vacuum.c.

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

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

1697 {
1698  /* Always check for interrupts */
1700 
1701  /* Nap if appropriate */
1704  {
1705  int msec;
1706 
1708  if (msec > VacuumCostDelay * 4)
1709  msec = VacuumCostDelay * 4;
1710 
1711  pg_usleep(msec * 1000L);
1712 
1713  VacuumCostBalance = 0;
1714 
1715  /* update balance values for workers */
1717 
1718  /* Might have gotten an interrupt while sleeping */
1720  }
1721 }
int VacuumCostBalance
Definition: globals.c:139
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:132
int VacuumCostDelay
Definition: globals.c:133
volatile bool InterruptPending
Definition: globals.c:29
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1759
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
bool VacuumCostActive
Definition: globals.c:140

◆ vacuum_set_xid_limits()

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

Definition at line 593 of file vacuum.c.

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

Referenced by copy_heap_data(), and lazy_vacuum_rel().

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

Variable Documentation

◆ default_statistics_target

PGDLLIMPORT int default_statistics_target

Definition at line 77 of file analyze.c.

Referenced by range_typanalyze(), std_typanalyze(), and ts_typanalyze().

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 59 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_freeze_table_age

int vacuum_freeze_table_age

Definition at line 60 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_min_age

int vacuum_multixact_freeze_min_age

Definition at line 61 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_table_age

int vacuum_multixact_freeze_table_age

Definition at line 62 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().