PostgreSQL Source Code  git master
vacuum.h File Reference
#include "access/htup.h"
#include "catalog/pg_class.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "parser/parse_node.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
 

Macros

#define VACUUM_OPTION_NO_PARALLEL   0
 
#define VACUUM_OPTION_PARALLEL_BULKDEL   (1 << 0)
 
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP   (1 << 1)
 
#define VACUUM_OPTION_PARALLEL_CLEANUP   (1 << 2)
 
#define VACUUM_OPTION_MAX_VALID_VALUE   ((1 << 3) - 1)
 

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 enum VacuumOption VacuumOption
 
typedef enum VacOptTernaryValue VacOptTernaryValue
 
typedef struct VacuumParams VacuumParams
 

Enumerations

enum  VacuumOption {
  VACOPT_VACUUM = 1 << 0, VACOPT_ANALYZE = 1 << 1, VACOPT_VERBOSE = 1 << 2, VACOPT_FREEZE = 1 << 3,
  VACOPT_FULL = 1 << 4, VACOPT_SKIP_LOCKED = 1 << 5, VACOPT_SKIPTOAST = 1 << 6, VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7
}
 
enum  VacOptTernaryValue { VACOPT_TERNARY_DEFAULT = 0, VACOPT_TERNARY_DISABLED, VACOPT_TERNARY_ENABLED }
 

Functions

void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (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, 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)
 
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 analyze_rel (Oid relid, RangeVar *relation, 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
 
pg_atomic_uint32VacuumSharedCostBalance
 
pg_atomic_uint32VacuumActiveNWorkers
 
int VacuumCostBalanceLocal
 

Macro Definition Documentation

◆ VACUUM_OPTION_MAX_VALID_VALUE

#define VACUUM_OPTION_MAX_VALID_VALUE   ((1 << 3) - 1)

Definition at line 63 of file vacuum.h.

Referenced by begin_parallel_vacuum().

◆ VACUUM_OPTION_NO_PARALLEL

#define VACUUM_OPTION_NO_PARALLEL   0

Definition at line 39 of file vacuum.h.

Referenced by compute_parallel_vacuum_workers(), and dihandler().

◆ VACUUM_OPTION_PARALLEL_BULKDEL

#define VACUUM_OPTION_PARALLEL_BULKDEL   (1 << 0)

◆ VACUUM_OPTION_PARALLEL_CLEANUP

#define VACUUM_OPTION_PARALLEL_CLEANUP   (1 << 2)

◆ VACUUM_OPTION_PARALLEL_COND_CLEANUP

#define VACUUM_OPTION_PARALLEL_COND_CLEANUP   (1 << 1)

Typedef Documentation

◆ AnalyzeAttrComputeStatsFunc

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

Definition at line 105 of file vacuum.h.

◆ AnalyzeAttrFetchFunc

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

Definition at line 102 of file vacuum.h.

◆ VacAttrStats

typedef struct VacAttrStats VacAttrStats

◆ VacAttrStatsP

typedef struct VacAttrStats* VacAttrStatsP

Definition at line 100 of file vacuum.h.

◆ VacOptTernaryValue

◆ VacuumOption

typedef enum VacuumOption VacuumOption

◆ VacuumParams

typedef struct VacuumParams VacuumParams

Enumeration Type Documentation

◆ VacOptTernaryValue

Enumerator
VACOPT_TERNARY_DEFAULT 
VACOPT_TERNARY_DISABLED 
VACOPT_TERNARY_ENABLED 

Definition at line 195 of file vacuum.h.

◆ VacuumOption

Enumerator
VACOPT_VACUUM 
VACOPT_ANALYZE 
VACOPT_VERBOSE 
VACOPT_FREEZE 
VACOPT_FULL 
VACOPT_SKIP_LOCKED 
VACOPT_SKIPTOAST 
VACOPT_DISABLE_PAGE_SKIPPING 

Definition at line 177 of file vacuum.h.

178 {
179  VACOPT_VACUUM = 1 << 0, /* do VACUUM */
180  VACOPT_ANALYZE = 1 << 1, /* do ANALYZE */
181  VACOPT_VERBOSE = 1 << 2, /* print progress info */
182  VACOPT_FREEZE = 1 << 3, /* FREEZE option */
183  VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */
184  VACOPT_SKIP_LOCKED = 1 << 5, /* skip if cannot get lock */
185  VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */
186  VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */
187 } VacuumOption;
VacuumOption
Definition: vacuum.h:177

Function Documentation

◆ analyze_rel()

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

Definition at line 119 of file analyze.c.

References acquire_sample_rows(), FdwRoutine::AnalyzeForeignTable, CHECK_FOR_INTERRUPTS, DEBUG2, do_analyze_rel(), elevel, ereport, errmsg(), GetFdwRoutineForRelation(), INFO, VacuumParams::log_min_duration, NoLock, VacuumParams::options, pgstat_progress_end_command(), pgstat_progress_start_command(), PROGRESS_COMMAND_ANALYZE, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetNumberOfBlocks, RelationGetRelationName, RelationGetRelid, ShareUpdateExclusiveLock, VACOPT_ANALYZE, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum_is_relation_owner(), vacuum_open_relation(), and WARNING.

Referenced by vacuum().

122 {
123  Relation onerel;
124  int elevel;
125  AcquireSampleRowsFunc acquirefunc = NULL;
126  BlockNumber relpages = 0;
127 
128  /* Select logging level */
129  if (params->options & VACOPT_VERBOSE)
130  elevel = INFO;
131  else
132  elevel = DEBUG2;
133 
134  /* Set up static variables */
135  vac_strategy = bstrategy;
136 
137  /*
138  * Check for user-requested abort.
139  */
141 
142  /*
143  * Open the relation, getting ShareUpdateExclusiveLock to ensure that two
144  * ANALYZEs don't run on it concurrently. (This also locks out a
145  * concurrent VACUUM, which doesn't matter much at the moment but might
146  * matter if we ever try to accumulate stats on dead tuples.) If the rel
147  * has been dropped since we last saw it, we don't need to process it.
148  *
149  * Make sure to generate only logs for ANALYZE in this case.
150  */
151  onerel = vacuum_open_relation(relid, relation, params->options & ~(VACOPT_VACUUM),
152  params->log_min_duration >= 0,
154 
155  /* leave if relation could not be opened or locked */
156  if (!onerel)
157  return;
158 
159  /*
160  * Check if relation needs to be skipped based on ownership. This check
161  * happens also when building the relation list to analyze for a manual
162  * operation, and needs to be done additionally here as ANALYZE could
163  * happen across multiple transactions where relation ownership could have
164  * changed in-between. Make sure to generate only logs for ANALYZE in
165  * this case.
166  */
168  onerel->rd_rel,
169  params->options & VACOPT_ANALYZE))
170  {
172  return;
173  }
174 
175  /*
176  * Silently ignore tables that are temp tables of other backends ---
177  * trying to analyze these is rather pointless, since their contents are
178  * probably not up-to-date on disk. (We don't throw a warning here; it
179  * would just lead to chatter during a database-wide ANALYZE.)
180  */
181  if (RELATION_IS_OTHER_TEMP(onerel))
182  {
184  return;
185  }
186 
187  /*
188  * We can ANALYZE any table except pg_statistic. See update_attstats
189  */
190  if (RelationGetRelid(onerel) == StatisticRelationId)
191  {
193  return;
194  }
195 
196  /*
197  * Check that it's of an analyzable relkind, and set up appropriately.
198  */
199  if (onerel->rd_rel->relkind == RELKIND_RELATION ||
200  onerel->rd_rel->relkind == RELKIND_MATVIEW)
201  {
202  /* Regular table, so we'll use the regular row acquisition function */
203  acquirefunc = acquire_sample_rows;
204  /* Also get regular table's size */
205  relpages = RelationGetNumberOfBlocks(onerel);
206  }
207  else if (onerel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
208  {
209  /*
210  * For a foreign table, call the FDW's hook function to see whether it
211  * supports analysis.
212  */
213  FdwRoutine *fdwroutine;
214  bool ok = false;
215 
216  fdwroutine = GetFdwRoutineForRelation(onerel, false);
217 
218  if (fdwroutine->AnalyzeForeignTable != NULL)
219  ok = fdwroutine->AnalyzeForeignTable(onerel,
220  &acquirefunc,
221  &relpages);
222 
223  if (!ok)
224  {
226  (errmsg("skipping \"%s\" --- cannot analyze this foreign table",
227  RelationGetRelationName(onerel))));
229  return;
230  }
231  }
232  else if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
233  {
234  /*
235  * For partitioned tables, we want to do the recursive ANALYZE below.
236  */
237  }
238  else
239  {
240  /* No need for a WARNING if we already complained during VACUUM */
241  if (!(params->options & VACOPT_VACUUM))
243  (errmsg("skipping \"%s\" --- cannot analyze non-tables or special system tables",
244  RelationGetRelationName(onerel))));
246  return;
247  }
248 
249  /*
250  * OK, let's do it. First, initialize progress reporting.
251  */
253  RelationGetRelid(onerel));
254 
255  /*
256  * Do the normal non-recursive ANALYZE. We can skip this for partitioned
257  * tables, which don't contain any rows.
258  */
259  if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
260  do_analyze_rel(onerel, params, va_cols, acquirefunc,
261  relpages, false, in_outer_xact, elevel);
262 
263  /*
264  * If there are child tables, do recursive ANALYZE.
265  */
266  if (onerel->rd_rel->relhassubclass)
267  do_analyze_rel(onerel, params, va_cols, acquirefunc, relpages,
268  true, in_outer_xact, elevel);
269 
270  /*
271  * Close source relation now, but keep lock so that no one deletes it
272  * before we commit. (If someone did, they'd fail to clean up the entries
273  * we made in pg_statistic. Also, releasing the lock before commit would
274  * expose us to concurrent-update failures in update_attstats.)
275  */
276  relation_close(onerel, NoLock);
277 
279 }
static void do_analyze_rel(Relation onerel, VacuumParams *params, List *va_cols, AcquireSampleRowsFunc acquirefunc, BlockNumber relpages, bool inh, bool in_outer_xact, int elevel)
Definition: analyze.c:289
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
Definition: pgstat.c:3210
AnalyzeForeignTable_function AnalyzeForeignTable
Definition: fdwapi.h:234
#define INFO
Definition: elog.h:33
static BufferAccessStrategy vac_strategy
Definition: analyze.c:85
uint32 BlockNumber
Definition: block.h:31
Form_pg_class rd_rel
Definition: rel.h:109
bool vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, int options)
Definition: vacuum.c:533
#define DEBUG2
Definition: elog.h:24
#define NoLock
Definition: lockdefs.h:34
Relation vacuum_open_relation(Oid relid, RangeVar *relation, int options, bool verbose, LOCKMODE lmode)
Definition: vacuum.c:607
#define RelationGetRelationName(relation)
Definition: rel.h:490
#define WARNING
Definition: elog.h:40
static int elevel
Definition: vacuumlazy.c:330
void pgstat_progress_end_command(void)
Definition: pgstat.c:3282
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:211
#define ereport(elevel,...)
Definition: elog.h:144
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:593
int log_min_duration
Definition: vacuum.h:218
int(* AcquireSampleRowsFunc)(Relation relation, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: fdwapi.h:142
int errmsg(const char *fmt,...)
Definition: elog.c:824
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:427
int options
Definition: vacuum.h:210
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define RelationGetRelid(relation)
Definition: rel.h:456
static int acquire_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, double *totalrows, double *totaldeadrows)
Definition: analyze.c:1025

◆ anl_get_next_S()

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

Definition at line 288 of file sampling.c.

289 {
290  double result;
291 
292  oldrs.W = *stateptr;
293  result = reservoir_get_next_S(&oldrs, t, n);
294  *stateptr = oldrs.W;
295  return result;
296 }
static ReservoirStateData oldrs
Definition: sampling.c:263
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:146

◆ anl_init_selection_state()

double anl_init_selection_state ( int  n)

Definition at line 277 of file sampling.c.

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

◆ anl_random_fract()

double anl_random_fract ( void  )

Definition at line 266 of file sampling.c.

267 {
268  /* initialize if first time through */
269  if (oldrs.randstate[0] == 0)
271 
272  /* and compute a random fraction */
274 }
void sampler_random_init_state(long seed, SamplerRandomState randstate)
Definition: sampling.c:233
long random(void)
Definition: random.c:22
double sampler_random_fract(SamplerRandomState randstate)
Definition: sampling.c:242
static ReservoirStateData oldrs
Definition: sampling.c:263
SamplerRandomState randstate
Definition: sampling.h:50

◆ ExecVacuum()

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

Definition at line 98 of file vacuum.c.

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

Referenced by standard_ProcessUtility().

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

◆ std_typanalyze()

bool std_typanalyze ( VacAttrStats stats)

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

1681 {
1682  Form_pg_attribute attr = stats->attr;
1683  Oid ltopr;
1684  Oid eqopr;
1685  StdAnalyzeData *mystats;
1686 
1687  /* If the attstattarget column is negative, use the default value */
1688  /* NB: it is okay to scribble on stats->attr since it's a copy */
1689  if (attr->attstattarget < 0)
1690  attr->attstattarget = default_statistics_target;
1691 
1692  /* Look for default "<" and "=" operators for column's type */
1694  false, false, false,
1695  &ltopr, &eqopr, NULL,
1696  NULL);
1697 
1698  /* Save the operator info for compute_stats routines */
1699  mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData));
1700  mystats->eqopr = eqopr;
1701  mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
1702  mystats->ltopr = ltopr;
1703  stats->extra_data = mystats;
1704 
1705  /*
1706  * Determine which standard statistics algorithm to use
1707  */
1708  if (OidIsValid(eqopr) && OidIsValid(ltopr))
1709  {
1710  /* Seems to be a scalar datatype */
1712  /*--------------------
1713  * The following choice of minrows is based on the paper
1714  * "Random sampling for histogram construction: how much is enough?"
1715  * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
1716  * Proceedings of ACM SIGMOD International Conference on Management
1717  * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
1718  * says that for table size n, histogram size k, maximum relative
1719  * error in bin size f, and error probability gamma, the minimum
1720  * random sample size is
1721  * r = 4 * k * ln(2*n/gamma) / f^2
1722  * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
1723  * r = 305.82 * k
1724  * Note that because of the log function, the dependence on n is
1725  * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
1726  * bin size error with probability 0.99. So there's no real need to
1727  * scale for n, which is a good thing because we don't necessarily
1728  * know it at this point.
1729  *--------------------
1730  */
1731  stats->minrows = 300 * attr->attstattarget;
1732  }
1733  else if (OidIsValid(eqopr))
1734  {
1735  /* We can still recognize distinct values */
1737  /* Might as well use the same minrows as above */
1738  stats->minrows = 300 * attr->attstattarget;
1739  }
1740  else
1741  {
1742  /* Can't do much but the trivial stuff */
1744  /* Might as well use the same minrows as above */
1745  stats->minrows = 300 * attr->attstattarget;
1746  }
1747 
1748  return true;
1749 }
int minrows
Definition: vacuum.h:135
static void compute_scalar_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:2193
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
Form_pg_attribute attr
Definition: vacuum.h:123
Oid attrtypid
Definition: vacuum.h:124
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
static void compute_distinct_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:1850
#define InvalidOid
Definition: postgres_ext.h:36
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1202
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:1760
void * palloc(Size size)
Definition: mcxt.c:949
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:134
void * extra_data
Definition: vacuum.h:136
int default_statistics_target
Definition: analyze.c:81

◆ vac_close_indexes()

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

Definition at line 1994 of file vacuum.c.

References index_close(), and pfree().

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

1995 {
1996  if (Irel == NULL)
1997  return;
1998 
1999  while (nindexes--)
2000  {
2001  Relation ind = Irel[nindexes];
2002 
2003  index_close(ind, lockmode);
2004  }
2005  pfree(Irel);
2006 }
void pfree(void *pointer)
Definition: mcxt.c:1056
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158

◆ vac_estimate_reltuples()

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

Definition at line 1138 of file vacuum.c.

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

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

◆ vac_open_indexes()

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

Definition at line 1951 of file vacuum.c.

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

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

1953 {
1954  List *indexoidlist;
1955  ListCell *indexoidscan;
1956  int i;
1957 
1958  Assert(lockmode != NoLock);
1959 
1960  indexoidlist = RelationGetIndexList(relation);
1961 
1962  /* allocate enough memory for all indexes */
1963  i = list_length(indexoidlist);
1964 
1965  if (i > 0)
1966  *Irel = (Relation *) palloc(i * sizeof(Relation));
1967  else
1968  *Irel = NULL;
1969 
1970  /* collect just the ready indexes */
1971  i = 0;
1972  foreach(indexoidscan, indexoidlist)
1973  {
1974  Oid indexoid = lfirst_oid(indexoidscan);
1975  Relation indrel;
1976 
1977  indrel = index_open(indexoid, lockmode);
1978  if (indrel->rd_index->indisready)
1979  (*Irel)[i++] = indrel;
1980  else
1981  index_close(indrel, lockmode);
1982  }
1983 
1984  *nindexes = i;
1985 
1986  list_free(indexoidlist);
1987 }
unsigned int Oid
Definition: postgres_ext.h:31
Form_pg_index rd_index
Definition: rel.h:174
#define NoLock
Definition: lockdefs.h:34
#define Assert(condition)
Definition: c.h:745
static int list_length(const List *l)
Definition: pg_list.h:169
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4507
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:158
void * palloc(Size size)
Definition: mcxt.c:949
void list_free(List *list)
Definition: list.c:1376
int i
Definition: pg_list.h:50
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:132
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1350 of file vacuum.c.

References AccessShareLock, Assert, DATABASEOID, elog, ERROR, ForceTransactionIdLimitUpdate(), GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), GETSTRUCT, heap_freetuple(), heap_inplace_update(), HeapTupleIsValid, InvalidOid, MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ObjectIdGetDatum, 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().

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

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

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

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

◆ vacuum()

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

Definition at line 272 of file vacuum.c.

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

Referenced by autovacuum_do_vac_analyze(), and ExecVacuum().

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

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 2015 of file vacuum.c.

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

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

2016 {
2017  double msec = 0;
2018 
2019  /* Always check for interrupts */
2021 
2023  return;
2024 
2025  /*
2026  * For parallel vacuum, the delay is computed based on the shared cost
2027  * balance. See compute_parallel_delay.
2028  */
2029  if (VacuumSharedCostBalance != NULL)
2030  msec = compute_parallel_delay();
2031  else if (VacuumCostBalance >= VacuumCostLimit)
2033 
2034  /* Nap if appropriate */
2035  if (msec > 0)
2036  {
2037  if (msec > VacuumCostDelay * 4)
2038  msec = VacuumCostDelay * 4;
2039 
2041  pg_usleep((long) (msec * 1000));
2043 
2044  VacuumCostBalance = 0;
2045 
2046  /* update balance values for workers */
2048 
2049  /* Might have gotten an interrupt while sleeping */
2051  }
2052 }
static double compute_parallel_delay(void)
Definition: vacuum.c:2077
int VacuumCostBalance
Definition: globals.c:148
void pg_usleep(long microsec)
Definition: signal.c:53
int VacuumCostLimit
Definition: globals.c:141
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:76
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
double VacuumCostDelay
Definition: globals.c:142
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1759
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
bool VacuumCostActive
Definition: globals.c:149

◆ vacuum_is_relation_owner()

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

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

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

◆ vacuum_open_relation()

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

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

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

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

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

Referenced by copy_table_data(), and heap_vacuum_rel().

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

Variable Documentation

◆ default_statistics_target

PGDLLIMPORT int default_statistics_target

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 61 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_freeze_table_age

int vacuum_freeze_table_age

Definition at line 62 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_min_age

int vacuum_multixact_freeze_min_age

Definition at line 63 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ vacuum_multixact_freeze_table_age

int vacuum_multixact_freeze_table_age

Definition at line 64 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_set_xid_limits().

◆ VacuumActiveNWorkers

◆ VacuumCostBalanceLocal

int VacuumCostBalanceLocal

◆ VacuumSharedCostBalance

pg_atomic_uint32* VacuumSharedCostBalance

Definition at line 76 of file vacuum.c.

Referenced by lazy_parallel_vacuum_indexes(), and parallel_vacuum_main().