PostgreSQL Source Code  git master
vacuum.h File Reference
#include "access/htup.h"
#include "access/genam.h"
#include "access/parallel.h"
#include "access/tidstore.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
 
struct  VacuumCutoffs
 
struct  VacDeadItemsInfo
 

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)
 
#define VACOPT_VACUUM   0x01 /* do VACUUM */
 
#define VACOPT_ANALYZE   0x02 /* do ANALYZE */
 
#define VACOPT_VERBOSE   0x04 /* output INFO instrumentation messages */
 
#define VACOPT_FREEZE   0x08 /* FREEZE option */
 
#define VACOPT_FULL   0x10 /* FULL (non-concurrent) vacuum */
 
#define VACOPT_SKIP_LOCKED   0x20 /* skip if cannot get lock */
 
#define VACOPT_PROCESS_MAIN   0x40 /* process main relation */
 
#define VACOPT_PROCESS_TOAST   0x80 /* process the TOAST table, if any */
 
#define VACOPT_DISABLE_PAGE_SKIPPING   0x100 /* don't skip any pages */
 
#define VACOPT_SKIP_DATABASE_STATS   0x200 /* skip vac_update_datfrozenxid() */
 
#define VACOPT_ONLY_DATABASE_STATS   0x400 /* only vac_update_datfrozenxid() */
 
#define MAX_STATISTICS_TARGET   10000
 

Typedefs

typedef struct ParallelVacuumState ParallelVacuumState
 
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 VacOptValue VacOptValue
 
typedef struct VacuumParams VacuumParams
 
typedef struct VacDeadItemsInfo VacDeadItemsInfo
 

Enumerations

enum  VacOptValue { VACOPTVALUE_UNSPECIFIED = 0 , VACOPTVALUE_AUTO , VACOPTVALUE_DISABLED , VACOPTVALUE_ENABLED }
 

Functions

void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, 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 *frozenxid_updated, bool *minmulti_updated, bool in_outer_xact)
 
bool vacuum_get_cutoffs (Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
 
bool vacuum_xid_failsafe_check (const struct VacuumCutoffs *cutoffs)
 
void vac_update_datfrozenxid (void)
 
void vacuum_delay_point (void)
 
bool vacuum_is_permitted_for_relation (Oid relid, Form_pg_class reltuple, bits32 options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, bits32 options, bool verbose, LOCKMODE lmode)
 
IndexBulkDeleteResultvac_bulkdel_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
 
IndexBulkDeleteResultvac_cleanup_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
 
void AutoVacuumUpdateCostLimit (void)
 
void VacuumUpdateCosts (void)
 
ParallelVacuumStateparallel_vacuum_init (Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, int elevel, BufferAccessStrategy bstrategy)
 
void parallel_vacuum_end (ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
 
TidStoreparallel_vacuum_get_dead_items (ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p)
 
void parallel_vacuum_reset_dead_items (ParallelVacuumState *pvs)
 
void parallel_vacuum_bulkdel_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans)
 
void parallel_vacuum_cleanup_all_indexes (ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count)
 
void parallel_vacuum_main (dsm_segment *seg, shm_toc *toc)
 
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
 
PGDLLIMPORT int vacuum_freeze_min_age
 
PGDLLIMPORT int vacuum_freeze_table_age
 
PGDLLIMPORT int vacuum_multixact_freeze_min_age
 
PGDLLIMPORT int vacuum_multixact_freeze_table_age
 
PGDLLIMPORT int vacuum_failsafe_age
 
PGDLLIMPORT int vacuum_multixact_failsafe_age
 
PGDLLIMPORT pg_atomic_uint32VacuumSharedCostBalance
 
PGDLLIMPORT pg_atomic_uint32VacuumActiveNWorkers
 
PGDLLIMPORT int VacuumCostBalanceLocal
 
PGDLLIMPORT bool VacuumFailsafeActive
 
PGDLLIMPORT double vacuum_cost_delay
 
PGDLLIMPORT int vacuum_cost_limit
 

Macro Definition Documentation

◆ MAX_STATISTICS_TARGET

#define MAX_STATISTICS_TARGET   10000

Definition at line 305 of file vacuum.h.

◆ VACOPT_ANALYZE

#define VACOPT_ANALYZE   0x02 /* do ANALYZE */

Definition at line 181 of file vacuum.h.

◆ VACOPT_DISABLE_PAGE_SKIPPING

#define VACOPT_DISABLE_PAGE_SKIPPING   0x100 /* don't skip any pages */

Definition at line 188 of file vacuum.h.

◆ VACOPT_FREEZE

#define VACOPT_FREEZE   0x08 /* FREEZE option */

Definition at line 183 of file vacuum.h.

◆ VACOPT_FULL

#define VACOPT_FULL   0x10 /* FULL (non-concurrent) vacuum */

Definition at line 184 of file vacuum.h.

◆ VACOPT_ONLY_DATABASE_STATS

#define VACOPT_ONLY_DATABASE_STATS   0x400 /* only vac_update_datfrozenxid() */

Definition at line 190 of file vacuum.h.

◆ VACOPT_PROCESS_MAIN

#define VACOPT_PROCESS_MAIN   0x40 /* process main relation */

Definition at line 186 of file vacuum.h.

◆ VACOPT_PROCESS_TOAST

#define VACOPT_PROCESS_TOAST   0x80 /* process the TOAST table, if any */

Definition at line 187 of file vacuum.h.

◆ VACOPT_SKIP_DATABASE_STATS

#define VACOPT_SKIP_DATABASE_STATS   0x200 /* skip vac_update_datfrozenxid() */

Definition at line 189 of file vacuum.h.

◆ VACOPT_SKIP_LOCKED

#define VACOPT_SKIP_LOCKED   0x20 /* skip if cannot get lock */

Definition at line 185 of file vacuum.h.

◆ VACOPT_VACUUM

#define VACOPT_VACUUM   0x01 /* do VACUUM */

Definition at line 180 of file vacuum.h.

◆ VACOPT_VERBOSE

#define VACOPT_VERBOSE   0x04 /* output INFO instrumentation messages */

Definition at line 182 of file vacuum.h.

◆ VACUUM_OPTION_MAX_VALID_VALUE

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

Definition at line 66 of file vacuum.h.

◆ VACUUM_OPTION_NO_PARALLEL

#define VACUUM_OPTION_NO_PARALLEL   0

Definition at line 42 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_BULKDEL

#define VACUUM_OPTION_PARALLEL_BULKDEL   (1 << 0)

Definition at line 48 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_CLEANUP

#define VACUUM_OPTION_PARALLEL_CLEANUP   (1 << 2)

Definition at line 63 of file vacuum.h.

◆ VACUUM_OPTION_PARALLEL_COND_CLEANUP

#define VACUUM_OPTION_PARALLEL_COND_CLEANUP   (1 << 1)

Definition at line 55 of file vacuum.h.

Typedef Documentation

◆ AnalyzeAttrComputeStatsFunc

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

Definition at line 111 of file vacuum.h.

◆ AnalyzeAttrFetchFunc

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

Definition at line 108 of file vacuum.h.

◆ ParallelVacuumState

Definition at line 1 of file vacuum.h.

◆ VacAttrStats

typedef struct VacAttrStats VacAttrStats

◆ VacAttrStatsP

typedef struct VacAttrStats* VacAttrStatsP

Definition at line 106 of file vacuum.h.

◆ VacDeadItemsInfo

◆ VacOptValue

typedef enum VacOptValue VacOptValue

◆ VacuumParams

typedef struct VacuumParams VacuumParams

Enumeration Type Documentation

◆ VacOptValue

Enumerator
VACOPTVALUE_UNSPECIFIED 
VACOPTVALUE_AUTO 
VACOPTVALUE_DISABLED 
VACOPTVALUE_ENABLED 

Definition at line 200 of file vacuum.h.

201 {
206 } VacOptValue;
VacOptValue
Definition: vacuum.h:201
@ VACOPTVALUE_AUTO
Definition: vacuum.h:203
@ VACOPTVALUE_ENABLED
Definition: vacuum.h:205
@ VACOPTVALUE_UNSPECIFIED
Definition: vacuum.h:202
@ VACOPTVALUE_DISABLED
Definition: vacuum.h:204

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 111 of file analyze.c.

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

References acquire_sample_rows(), FdwRoutine::AnalyzeForeignTable, CHECK_FOR_INTERRUPTS, DEBUG2, do_analyze_rel(), 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, vac_strategy, VACOPT_VACUUM, VACOPT_VERBOSE, vacuum_is_permitted_for_relation(), vacuum_open_relation(), and WARNING.

Referenced by vacuum().

◆ anl_get_next_S()

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

Definition at line 296 of file sampling.c.

297 {
298  double result;
299 
300  oldrs.W = *stateptr;
301  result = reservoir_get_next_S(&oldrs, t, n);
302  *stateptr = oldrs.W;
303  return result;
304 }
static ReservoirStateData oldrs
Definition: sampling.c:262
double reservoir_get_next_S(ReservoirState rs, double t, int n)
Definition: sampling.c:147

◆ anl_init_selection_state()

double anl_init_selection_state ( int  n)

Definition at line 281 of file sampling.c.

282 {
283  /* initialize if first time through */
285  {
287  &oldrs.randstate);
288  oldrs_initialized = true;
289  }
290 
291  /* Initial value of W (for use when Algorithm Z is first applied) */
292  return exp(-log(sampler_random_fract(&oldrs.randstate)) / n);
293 }
#define unlikely(x)
Definition: c.h:311
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:227
pg_prng_state pg_global_prng_state
Definition: pg_prng.c:34
static bool oldrs_initialized
Definition: sampling.c:263
double sampler_random_fract(pg_prng_state *randstate)
Definition: sampling.c:241
void sampler_random_init_state(uint32 seed, pg_prng_state *randstate)
Definition: sampling.c:234
pg_prng_state randstate
Definition: sampling.h:49

◆ anl_random_fract()

double anl_random_fract ( void  )

Definition at line 266 of file sampling.c.

267 {
268  /* initialize if first time through */
270  {
272  &oldrs.randstate);
273  oldrs_initialized = true;
274  }
275 
276  /* and compute a random fraction */
278 }

◆ AutoVacuumUpdateCostLimit()

void AutoVacuumUpdateCostLimit ( void  )

Definition at line 1703 of file autovacuum.c.

1704 {
1705  if (!MyWorkerInfo)
1706  return;
1707 
1708  /*
1709  * note: in cost_limit, zero also means use value from elsewhere, because
1710  * zero is not a valid value.
1711  */
1712 
1715  else
1716  {
1717  int nworkers_for_balance;
1718 
1719  if (autovacuum_vac_cost_limit > 0)
1721  else
1723 
1724  /* Only balance limit if no cost-related storage parameters specified */
1726  return;
1727 
1729 
1730  nworkers_for_balance = pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
1731 
1732  /* There is at least 1 autovac worker (this worker) */
1733  if (nworkers_for_balance <= 0)
1734  elog(ERROR, "nworkers_for_balance must be > 0");
1735 
1736  vacuum_cost_limit = Max(vacuum_cost_limit / nworkers_for_balance, 1);
1737  }
1738 }
static bool pg_atomic_unlocked_test_flag(volatile pg_atomic_flag *ptr)
Definition: atomics.h:189
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:232
int autovacuum_vac_cost_limit
Definition: autovacuum.c:131
static int av_storage_param_cost_limit
Definition: autovacuum.c:149
static AutoVacuumShmemStruct * AutoVacuumShmem
Definition: autovacuum.c:300
static WorkerInfo MyWorkerInfo
Definition: autovacuum.c:310
#define Max(x, y)
Definition: c.h:998
#define Assert(condition)
Definition: c.h:858
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
int VacuumCostLimit
Definition: globals.c:152
pg_atomic_uint32 av_nworkersForBalance
Definition: autovacuum.c:297
pg_atomic_flag wi_dobalance
Definition: autovacuum.c:235
int vacuum_cost_limit
Definition: vacuum.c:81

References Assert, autovacuum_vac_cost_limit, AutoVacuumShmem, AutoVacuumShmemStruct::av_nworkersForBalance, av_storage_param_cost_limit, elog, ERROR, Max, MyWorkerInfo, pg_atomic_read_u32(), pg_atomic_unlocked_test_flag(), vacuum_cost_limit, VacuumCostLimit, and WorkerInfoData::wi_dobalance.

Referenced by vacuum_delay_point(), and VacuumUpdateCosts().

◆ ExecVacuum()

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

Definition at line 147 of file vacuum.c.

148 {
149  VacuumParams params;
150  BufferAccessStrategy bstrategy = NULL;
151  bool verbose = false;
152  bool skip_locked = false;
153  bool analyze = false;
154  bool freeze = false;
155  bool full = false;
156  bool disable_page_skipping = false;
157  bool process_main = true;
158  bool process_toast = true;
159  int ring_size;
160  bool skip_database_stats = false;
161  bool only_database_stats = false;
162  MemoryContext vac_context;
163  ListCell *lc;
164 
165  /* index_cleanup and truncate values unspecified for now */
168 
169  /* By default parallel vacuum is enabled */
170  params.nworkers = 0;
171 
172  /* Will be set later if we recurse to a TOAST table. */
173  params.toast_parent = InvalidOid;
174 
175  /*
176  * Set this to an invalid value so it is clear whether or not a
177  * BUFFER_USAGE_LIMIT was specified when making the access strategy.
178  */
179  ring_size = -1;
180 
181  /* Parse options list */
182  foreach(lc, vacstmt->options)
183  {
184  DefElem *opt = (DefElem *) lfirst(lc);
185 
186  /* Parse common options for VACUUM and ANALYZE */
187  if (strcmp(opt->defname, "verbose") == 0)
188  verbose = defGetBoolean(opt);
189  else if (strcmp(opt->defname, "skip_locked") == 0)
190  skip_locked = defGetBoolean(opt);
191  else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
192  {
193  const char *hintmsg;
194  int result;
195  char *vac_buffer_size;
196 
197  vac_buffer_size = defGetString(opt);
198 
199  /*
200  * Check that the specified value is valid and the size falls
201  * within the hard upper and lower limits if it is not 0.
202  */
203  if (!parse_int(vac_buffer_size, &result, GUC_UNIT_KB, &hintmsg) ||
204  (result != 0 &&
205  (result < MIN_BAS_VAC_RING_SIZE_KB || result > MAX_BAS_VAC_RING_SIZE_KB)))
206  {
207  ereport(ERROR,
208  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
209  errmsg("BUFFER_USAGE_LIMIT option must be 0 or between %d kB and %d kB",
211  hintmsg ? errhint("%s", _(hintmsg)) : 0));
212  }
213 
214  ring_size = result;
215  }
216  else if (!vacstmt->is_vacuumcmd)
217  ereport(ERROR,
218  (errcode(ERRCODE_SYNTAX_ERROR),
219  errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
220  parser_errposition(pstate, opt->location)));
221 
222  /* Parse options available on VACUUM */
223  else if (strcmp(opt->defname, "analyze") == 0)
224  analyze = defGetBoolean(opt);
225  else if (strcmp(opt->defname, "freeze") == 0)
226  freeze = defGetBoolean(opt);
227  else if (strcmp(opt->defname, "full") == 0)
228  full = defGetBoolean(opt);
229  else if (strcmp(opt->defname, "disable_page_skipping") == 0)
230  disable_page_skipping = defGetBoolean(opt);
231  else if (strcmp(opt->defname, "index_cleanup") == 0)
232  {
233  /* Interpret no string as the default, which is 'auto' */
234  if (!opt->arg)
236  else
237  {
238  char *sval = defGetString(opt);
239 
240  /* Try matching on 'auto' string, or fall back on boolean */
241  if (pg_strcasecmp(sval, "auto") == 0)
243  else
245  }
246  }
247  else if (strcmp(opt->defname, "process_main") == 0)
248  process_main = defGetBoolean(opt);
249  else if (strcmp(opt->defname, "process_toast") == 0)
250  process_toast = defGetBoolean(opt);
251  else if (strcmp(opt->defname, "truncate") == 0)
252  params.truncate = get_vacoptval_from_boolean(opt);
253  else if (strcmp(opt->defname, "parallel") == 0)
254  {
255  if (opt->arg == NULL)
256  {
257  ereport(ERROR,
258  (errcode(ERRCODE_SYNTAX_ERROR),
259  errmsg("parallel option requires a value between 0 and %d",
261  parser_errposition(pstate, opt->location)));
262  }
263  else
264  {
265  int nworkers;
266 
267  nworkers = defGetInt32(opt);
268  if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
269  ereport(ERROR,
270  (errcode(ERRCODE_SYNTAX_ERROR),
271  errmsg("parallel workers for vacuum must be between 0 and %d",
273  parser_errposition(pstate, opt->location)));
274 
275  /*
276  * Disable parallel vacuum, if user has specified parallel
277  * degree as zero.
278  */
279  if (nworkers == 0)
280  params.nworkers = -1;
281  else
282  params.nworkers = nworkers;
283  }
284  }
285  else if (strcmp(opt->defname, "skip_database_stats") == 0)
286  skip_database_stats = defGetBoolean(opt);
287  else if (strcmp(opt->defname, "only_database_stats") == 0)
288  only_database_stats = defGetBoolean(opt);
289  else
290  ereport(ERROR,
291  (errcode(ERRCODE_SYNTAX_ERROR),
292  errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
293  parser_errposition(pstate, opt->location)));
294  }
295 
296  /* Set vacuum options */
297  params.options =
298  (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
299  (verbose ? VACOPT_VERBOSE : 0) |
300  (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
301  (analyze ? VACOPT_ANALYZE : 0) |
302  (freeze ? VACOPT_FREEZE : 0) |
303  (full ? VACOPT_FULL : 0) |
304  (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
305  (process_main ? VACOPT_PROCESS_MAIN : 0) |
306  (process_toast ? VACOPT_PROCESS_TOAST : 0) |
307  (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
308  (only_database_stats ? VACOPT_ONLY_DATABASE_STATS : 0);
309 
310  /* sanity checks on options */
312  Assert((params.options & VACOPT_VACUUM) ||
313  !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
314 
315  if ((params.options & VACOPT_FULL) && params.nworkers > 0)
316  ereport(ERROR,
317  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
318  errmsg("VACUUM FULL cannot be performed in parallel")));
319 
320  /*
321  * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
322  * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
323  * we'll permit that.
324  */
325  if (ring_size != -1 && (params.options & VACOPT_FULL) &&
326  !(params.options & VACOPT_ANALYZE))
327  ereport(ERROR,
328  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
329  errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
330 
331  /*
332  * Make sure VACOPT_ANALYZE is specified if any column lists are present.
333  */
334  if (!(params.options & VACOPT_ANALYZE))
335  {
336  foreach(lc, vacstmt->rels)
337  {
339 
340  if (vrel->va_cols != NIL)
341  ereport(ERROR,
342  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
343  errmsg("ANALYZE option must be specified when a column list is provided")));
344  }
345  }
346 
347 
348  /*
349  * Sanity check DISABLE_PAGE_SKIPPING option.
350  */
351  if ((params.options & VACOPT_FULL) != 0 &&
352  (params.options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
353  ereport(ERROR,
354  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
355  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
356 
357  /* sanity check for PROCESS_TOAST */
358  if ((params.options & VACOPT_FULL) != 0 &&
359  (params.options & VACOPT_PROCESS_TOAST) == 0)
360  ereport(ERROR,
361  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
362  errmsg("PROCESS_TOAST required with VACUUM FULL")));
363 
364  /* sanity check for ONLY_DATABASE_STATS */
365  if (params.options & VACOPT_ONLY_DATABASE_STATS)
366  {
367  Assert(params.options & VACOPT_VACUUM);
368  if (vacstmt->rels != NIL)
369  ereport(ERROR,
370  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
371  errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
372  /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
373  if (params.options & ~(VACOPT_VACUUM |
378  ereport(ERROR,
379  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
380  errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
381  }
382 
383  /*
384  * All freeze ages are zero if the FREEZE option is given; otherwise pass
385  * them as -1 which means to use the default values.
386  */
387  if (params.options & VACOPT_FREEZE)
388  {
389  params.freeze_min_age = 0;
390  params.freeze_table_age = 0;
391  params.multixact_freeze_min_age = 0;
392  params.multixact_freeze_table_age = 0;
393  }
394  else
395  {
396  params.freeze_min_age = -1;
397  params.freeze_table_age = -1;
398  params.multixact_freeze_min_age = -1;
399  params.multixact_freeze_table_age = -1;
400  }
401 
402  /* user-invoked vacuum is never "for wraparound" */
403  params.is_wraparound = false;
404 
405  /* user-invoked vacuum uses VACOPT_VERBOSE instead of log_min_duration */
406  params.log_min_duration = -1;
407 
408  /*
409  * Create special memory context for cross-transaction storage.
410  *
411  * Since it is a child of PortalContext, it will go away eventually even
412  * if we suffer an error; there's no need for special abort cleanup logic.
413  */
414  vac_context = AllocSetContextCreate(PortalContext,
415  "Vacuum",
417 
418  /*
419  * Make a buffer strategy object in the cross-transaction memory context.
420  * We needn't bother making this for VACUUM (FULL) or VACUUM
421  * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
422  * ANALYZE) is possible, so we'd better ensure that we make a strategy
423  * when we see ANALYZE.
424  */
425  if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
426  VACOPT_FULL)) == 0 ||
427  (params.options & VACOPT_ANALYZE) != 0)
428  {
429 
430  MemoryContext old_context = MemoryContextSwitchTo(vac_context);
431 
432  Assert(ring_size >= -1);
433 
434  /*
435  * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
436  * command, it overrides the value of VacuumBufferUsageLimit. Either
437  * value may be 0, in which case GetAccessStrategyWithSize() will
438  * return NULL, effectively allowing full use of shared buffers.
439  */
440  if (ring_size == -1)
441  ring_size = VacuumBufferUsageLimit;
442 
443  bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, ring_size);
444 
445  MemoryContextSwitchTo(old_context);
446  }
447 
448  /* Now go through the common routine */
449  vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
450 
451  /* Finally, clean up the vacuum memory context */
452  MemoryContextDelete(vac_context);
453 }
#define MAX_PARALLEL_WORKER_LIMIT
@ BAS_VACUUM
Definition: bufmgr.h:39
int32 defGetInt32(DefElem *def)
Definition: define.c:162
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
#define _(x)
Definition: elog.c:90
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:584
int VacuumBufferUsageLimit
Definition: globals.c:147
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition: guc.c:2870
#define GUC_UNIT_KB
Definition: guc.h:227
int verbose
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define MIN_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:276
#define MAX_BAS_VAC_RING_SIZE_KB
Definition: miscadmin.h:277
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define NIL
Definition: pg_list.h:68
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define InvalidOid
Definition: postgres_ext.h:36
MemoryContextSwitchTo(old_ctx)
static long analyze(struct nfa *nfa)
Definition: regc_nfa.c:3016
char * defname
Definition: parsenodes.h:815
ParseLoc location
Definition: parsenodes.h:819
Node * arg
Definition: parsenodes.h:816
int nworkers
Definition: vacuum.h:239
int freeze_table_age
Definition: vacuum.h:221
VacOptValue truncate
Definition: vacuum.h:231
int freeze_min_age
Definition: vacuum.h:220
bool is_wraparound
Definition: vacuum.h:226
int multixact_freeze_min_age
Definition: vacuum.h:222
int multixact_freeze_table_age
Definition: vacuum.h:224
Oid toast_parent
Definition: vacuum.h:232
VacOptValue index_cleanup
Definition: vacuum.h:230
List * options
Definition: parsenodes.h:3845
bool is_vacuumcmd
Definition: parsenodes.h:3847
List * rels
Definition: parsenodes.h:3846
static VacOptValue get_vacoptval_from_boolean(DefElem *def)
Definition: vacuum.c:2483
void vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition: vacuum.c:478
#define VACOPT_FREEZE
Definition: vacuum.h:183
#define VACOPT_SKIP_LOCKED
Definition: vacuum.h:185
#define VACOPT_FULL
Definition: vacuum.h:184
#define VACOPT_SKIP_DATABASE_STATS
Definition: vacuum.h:189
#define VACOPT_PROCESS_TOAST
Definition: vacuum.h:187
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition: vacuum.h:188
#define VACOPT_ONLY_DATABASE_STATS
Definition: vacuum.h:190
#define VACOPT_PROCESS_MAIN
Definition: vacuum.h:186
#define VACOPT_ANALYZE
Definition: vacuum.h:181

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze(), DefElem::arg, Assert, BAS_VACUUM, defGetBoolean(), defGetInt32(), defGetString(), DefElem::defname, ereport, errcode(), errhint(), errmsg(), ERROR, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, get_vacoptval_from_boolean(), GetAccessStrategyWithSize(), GUC_UNIT_KB, VacuumParams::index_cleanup, InvalidOid, VacuumStmt::is_vacuumcmd, VacuumParams::is_wraparound, lfirst, lfirst_node, DefElem::location, VacuumParams::log_min_duration, MAX_BAS_VAC_RING_SIZE_KB, MAX_PARALLEL_WORKER_LIMIT, MemoryContextDelete(), MemoryContextSwitchTo(), MIN_BAS_VAC_RING_SIZE_KB, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, NIL, VacuumParams::nworkers, VacuumParams::options, VacuumStmt::options, parse_int(), parser_errposition(), pg_strcasecmp(), PortalContext, VacuumStmt::rels, VacuumParams::toast_parent, VacuumParams::truncate, VacuumRelation::va_cols, VACOPT_ANALYZE, VACOPT_DISABLE_PAGE_SKIPPING, VACOPT_FREEZE, VACOPT_FULL, VACOPT_ONLY_DATABASE_STATS, VACOPT_PROCESS_MAIN, VACOPT_PROCESS_TOAST, VACOPT_SKIP_DATABASE_STATS, VACOPT_SKIP_LOCKED, VACOPT_VACUUM, VACOPT_VERBOSE, VACOPTVALUE_AUTO, VACOPTVALUE_UNSPECIFIED, vacuum(), VacuumBufferUsageLimit, and verbose.

Referenced by standard_ProcessUtility().

◆ parallel_vacuum_bulkdel_all_indexes()

void parallel_vacuum_bulkdel_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans 
)

Definition at line 499 of file vacuumparallel.c.

501 {
503 
504  /*
505  * We can only provide an approximate value of num_heap_tuples, at least
506  * for now.
507  */
508  pvs->shared->reltuples = num_table_tuples;
509  pvs->shared->estimated_count = true;
510 
511  parallel_vacuum_process_all_indexes(pvs, num_index_scans, true);
512 }
#define IsParallelWorker()
Definition: parallel.h:60
double reltuples
bool estimated_count
static void parallel_vacuum_process_all_indexes(ParallelVacuumState *pvs, int num_index_scans, bool vacuum)

References Assert, PVShared::estimated_count, IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_vacuum_all_indexes().

◆ parallel_vacuum_cleanup_all_indexes()

void parallel_vacuum_cleanup_all_indexes ( ParallelVacuumState pvs,
long  num_table_tuples,
int  num_index_scans,
bool  estimated_count 
)

Definition at line 518 of file vacuumparallel.c.

520 {
522 
523  /*
524  * We can provide a better estimate of total number of surviving tuples
525  * (we assume indexes are more interested in that than in the number of
526  * nominally live tuples).
527  */
528  pvs->shared->reltuples = num_table_tuples;
529  pvs->shared->estimated_count = estimated_count;
530 
531  parallel_vacuum_process_all_indexes(pvs, num_index_scans, false);
532 }

References Assert, PVShared::estimated_count, IsParallelWorker, parallel_vacuum_process_all_indexes(), PVShared::reltuples, and ParallelVacuumState::shared.

Referenced by lazy_cleanup_all_indexes().

◆ parallel_vacuum_end()

void parallel_vacuum_end ( ParallelVacuumState pvs,
IndexBulkDeleteResult **  istats 
)

Definition at line 434 of file vacuumparallel.c.

435 {
437 
438  /* Copy the updated statistics */
439  for (int i = 0; i < pvs->nindexes; i++)
440  {
441  PVIndStats *indstats = &(pvs->indstats[i]);
442 
443  if (indstats->istat_updated)
444  {
445  istats[i] = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
446  memcpy(istats[i], &indstats->istat, sizeof(IndexBulkDeleteResult));
447  }
448  else
449  istats[i] = NULL;
450  }
451 
453 
456 
458  pfree(pvs);
459 }
void DestroyParallelContext(ParallelContext *pcxt)
Definition: parallel.c:929
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
bool istat_updated
IndexBulkDeleteResult istat
ParallelContext * pcxt
PVIndStats * indstats
void TidStoreDestroy(TidStore *ts)
Definition: tidstore.c:325
void ExitParallelMode(void)
Definition: xact.c:1062

References Assert, ParallelVacuumState::dead_items, DestroyParallelContext(), ExitParallelMode(), i, ParallelVacuumState::indstats, IsParallelWorker, PVIndStats::istat, PVIndStats::istat_updated, ParallelVacuumState::nindexes, palloc0(), ParallelVacuumState::pcxt, pfree(), TidStoreDestroy(), and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_cleanup().

◆ parallel_vacuum_get_dead_items()

TidStore* parallel_vacuum_get_dead_items ( ParallelVacuumState pvs,
VacDeadItemsInfo **  dead_items_info_p 
)

Definition at line 465 of file vacuumparallel.c.

466 {
467  *dead_items_info_p = &(pvs->shared->dead_items_info);
468  return pvs->dead_items;
469 }
VacDeadItemsInfo dead_items_info

References ParallelVacuumState::dead_items, PVShared::dead_items_info, and ParallelVacuumState::shared.

Referenced by dead_items_alloc().

◆ parallel_vacuum_init()

ParallelVacuumState* parallel_vacuum_init ( Relation  rel,
Relation indrels,
int  nindexes,
int  nrequested_workers,
int  vac_work_mem,
int  elevel,
BufferAccessStrategy  bstrategy 
)

Definition at line 242 of file vacuumparallel.c.

245 {
246  ParallelVacuumState *pvs;
247  ParallelContext *pcxt;
248  PVShared *shared;
249  TidStore *dead_items;
250  PVIndStats *indstats;
251  BufferUsage *buffer_usage;
252  WalUsage *wal_usage;
253  bool *will_parallel_vacuum;
254  Size est_indstats_len;
255  Size est_shared_len;
256  int nindexes_mwm = 0;
257  int parallel_workers = 0;
258  int querylen;
259 
260  /*
261  * A parallel vacuum must be requested and there must be indexes on the
262  * relation
263  */
264  Assert(nrequested_workers >= 0);
265  Assert(nindexes > 0);
266 
267  /*
268  * Compute the number of parallel vacuum workers to launch
269  */
270  will_parallel_vacuum = (bool *) palloc0(sizeof(bool) * nindexes);
271  parallel_workers = parallel_vacuum_compute_workers(indrels, nindexes,
272  nrequested_workers,
273  will_parallel_vacuum);
274  if (parallel_workers <= 0)
275  {
276  /* Can't perform vacuum in parallel -- return NULL */
277  pfree(will_parallel_vacuum);
278  return NULL;
279  }
280 
282  pvs->indrels = indrels;
283  pvs->nindexes = nindexes;
284  pvs->will_parallel_vacuum = will_parallel_vacuum;
285  pvs->bstrategy = bstrategy;
286  pvs->heaprel = rel;
287 
289  pcxt = CreateParallelContext("postgres", "parallel_vacuum_main",
290  parallel_workers);
291  Assert(pcxt->nworkers > 0);
292  pvs->pcxt = pcxt;
293 
294  /* Estimate size for index vacuum stats -- PARALLEL_VACUUM_KEY_INDEX_STATS */
295  est_indstats_len = mul_size(sizeof(PVIndStats), nindexes);
296  shm_toc_estimate_chunk(&pcxt->estimator, est_indstats_len);
297  shm_toc_estimate_keys(&pcxt->estimator, 1);
298 
299  /* Estimate size for shared information -- PARALLEL_VACUUM_KEY_SHARED */
300  est_shared_len = sizeof(PVShared);
301  shm_toc_estimate_chunk(&pcxt->estimator, est_shared_len);
302  shm_toc_estimate_keys(&pcxt->estimator, 1);
303 
304  /*
305  * Estimate space for BufferUsage and WalUsage --
306  * PARALLEL_VACUUM_KEY_BUFFER_USAGE and PARALLEL_VACUUM_KEY_WAL_USAGE.
307  *
308  * If there are no extensions loaded that care, we could skip this. We
309  * have no way of knowing whether anyone's looking at pgBufferUsage or
310  * pgWalUsage, so do it unconditionally.
311  */
313  mul_size(sizeof(BufferUsage), pcxt->nworkers));
314  shm_toc_estimate_keys(&pcxt->estimator, 1);
316  mul_size(sizeof(WalUsage), pcxt->nworkers));
317  shm_toc_estimate_keys(&pcxt->estimator, 1);
318 
319  /* Finally, estimate PARALLEL_VACUUM_KEY_QUERY_TEXT space */
320  if (debug_query_string)
321  {
322  querylen = strlen(debug_query_string);
323  shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1);
324  shm_toc_estimate_keys(&pcxt->estimator, 1);
325  }
326  else
327  querylen = 0; /* keep compiler quiet */
328 
329  InitializeParallelDSM(pcxt);
330 
331  /* Prepare index vacuum stats */
332  indstats = (PVIndStats *) shm_toc_allocate(pcxt->toc, est_indstats_len);
333  MemSet(indstats, 0, est_indstats_len);
334  for (int i = 0; i < nindexes; i++)
335  {
336  Relation indrel = indrels[i];
337  uint8 vacoptions = indrel->rd_indam->amparallelvacuumoptions;
338 
339  /*
340  * Cleanup option should be either disabled, always performing in
341  * parallel or conditionally performing in parallel.
342  */
343  Assert(((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) == 0) ||
344  ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) == 0));
345  Assert(vacoptions <= VACUUM_OPTION_MAX_VALID_VALUE);
346 
347  if (!will_parallel_vacuum[i])
348  continue;
349 
350  if (indrel->rd_indam->amusemaintenanceworkmem)
351  nindexes_mwm++;
352 
353  /*
354  * Remember the number of indexes that support parallel operation for
355  * each phase.
356  */
357  if ((vacoptions & VACUUM_OPTION_PARALLEL_BULKDEL) != 0)
359  if ((vacoptions & VACUUM_OPTION_PARALLEL_CLEANUP) != 0)
361  if ((vacoptions & VACUUM_OPTION_PARALLEL_COND_CLEANUP) != 0)
363  }
365  pvs->indstats = indstats;
366 
367  /* Prepare shared information */
368  shared = (PVShared *) shm_toc_allocate(pcxt->toc, est_shared_len);
369  MemSet(shared, 0, est_shared_len);
370  shared->relid = RelationGetRelid(rel);
371  shared->elevel = elevel;
373  (nindexes_mwm > 0) ?
374  maintenance_work_mem / Min(parallel_workers, nindexes_mwm) :
376  shared->dead_items_info.max_bytes = vac_work_mem * 1024L;
377 
378  /* Prepare DSA space for dead items */
379  dead_items = TidStoreCreateShared(shared->dead_items_info.max_bytes,
381  pvs->dead_items = dead_items;
382  shared->dead_items_handle = TidStoreGetHandle(dead_items);
383  shared->dead_items_dsa_handle = dsa_get_handle(TidStoreGetDSA(dead_items));
384 
385  /* Use the same buffer size for all workers */
386  shared->ring_nbuffers = GetAccessStrategyBufferCount(bstrategy);
387 
388  pg_atomic_init_u32(&(shared->cost_balance), 0);
389  pg_atomic_init_u32(&(shared->active_nworkers), 0);
390  pg_atomic_init_u32(&(shared->idx), 0);
391 
393  pvs->shared = shared;
394 
395  /*
396  * Allocate space for each worker's BufferUsage and WalUsage; no need to
397  * initialize
398  */
399  buffer_usage = shm_toc_allocate(pcxt->toc,
400  mul_size(sizeof(BufferUsage), pcxt->nworkers));
402  pvs->buffer_usage = buffer_usage;
403  wal_usage = shm_toc_allocate(pcxt->toc,
404  mul_size(sizeof(WalUsage), pcxt->nworkers));
406  pvs->wal_usage = wal_usage;
407 
408  /* Store query string for workers */
409  if (debug_query_string)
410  {
411  char *sharedquery;
412 
413  sharedquery = (char *) shm_toc_allocate(pcxt->toc, querylen + 1);
414  memcpy(sharedquery, debug_query_string, querylen + 1);
415  sharedquery[querylen] = '\0';
416  shm_toc_insert(pcxt->toc,
417  PARALLEL_VACUUM_KEY_QUERY_TEXT, sharedquery);
418  }
419 
420  /* Success -- return parallel vacuum state */
421  return pvs;
422 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:214
void InitializeParallelDSM(ParallelContext *pcxt)
Definition: parallel.c:205
ParallelContext * CreateParallelContext(const char *library_name, const char *function_name, int nworkers)
Definition: parallel.c:167
#define Min(x, y)
Definition: c.h:1004
unsigned char uint8
Definition: c.h:504
#define MemSet(start, val, len)
Definition: c.h:1020
size_t Size
Definition: c.h:605
dsa_handle dsa_get_handle(dsa_area *area)
Definition: dsa.c:498
int GetAccessStrategyBufferCount(BufferAccessStrategy strategy)
Definition: freelist.c:624
int maintenance_work_mem
Definition: globals.c:131
@ LWTRANCHE_PARALLEL_VACUUM_DSA
Definition: lwlock.h:217
const char * debug_query_string
Definition: postgres.c:87
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
bool amusemaintenanceworkmem
Definition: amapi.h:252
uint8 amparallelvacuumoptions
Definition: amapi.h:256
pg_atomic_uint32 cost_balance
int maintenance_work_mem_worker
pg_atomic_uint32 active_nworkers
dsa_pointer dead_items_handle
dsa_handle dead_items_dsa_handle
int ring_nbuffers
pg_atomic_uint32 idx
shm_toc_estimator estimator
Definition: parallel.h:41
shm_toc * toc
Definition: parallel.h:44
BufferAccessStrategy bstrategy
BufferUsage * buffer_usage
struct IndexAmRoutine * rd_indam
Definition: rel.h:206
size_t max_bytes
Definition: vacuum.h:287
TidStore * TidStoreCreateShared(size_t max_bytes, int tranche_id)
Definition: tidstore.c:210
dsa_area * TidStoreGetDSA(TidStore *ts)
Definition: tidstore.c:552
dsa_pointer TidStoreGetHandle(TidStore *ts)
Definition: tidstore.c:560
#define VACUUM_OPTION_PARALLEL_CLEANUP
Definition: vacuum.h:63
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:48
#define VACUUM_OPTION_MAX_VALID_VALUE
Definition: vacuum.h:66
#define VACUUM_OPTION_PARALLEL_COND_CLEANUP
Definition: vacuum.h:55
static int parallel_vacuum_compute_workers(Relation *indrels, int nindexes, int nrequested, bool *will_parallel_vacuum)
#define PARALLEL_VACUUM_KEY_INDEX_STATS
#define PARALLEL_VACUUM_KEY_QUERY_TEXT
#define PARALLEL_VACUUM_KEY_BUFFER_USAGE
#define PARALLEL_VACUUM_KEY_SHARED
#define PARALLEL_VACUUM_KEY_WAL_USAGE
struct PVShared PVShared
void EnterParallelMode(void)
Definition: xact.c:1049

References PVShared::active_nworkers, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::amusemaintenanceworkmem, Assert, ParallelVacuumState::bstrategy, ParallelVacuumState::buffer_usage, PVShared::cost_balance, CreateParallelContext(), ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, debug_query_string, dsa_get_handle(), PVShared::elevel, EnterParallelMode(), ParallelContext::estimator, GetAccessStrategyBufferCount(), ParallelVacuumState::heaprel, i, PVShared::idx, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InitializeParallelDSM(), LWTRANCHE_PARALLEL_VACUUM_DSA, maintenance_work_mem, PVShared::maintenance_work_mem_worker, VacDeadItemsInfo::max_bytes, MemSet, Min, mul_size(), ParallelVacuumState::nindexes, ParallelVacuumState::nindexes_parallel_bulkdel, ParallelVacuumState::nindexes_parallel_cleanup, ParallelVacuumState::nindexes_parallel_condcleanup, ParallelContext::nworkers, palloc0(), parallel_vacuum_compute_workers(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, ParallelVacuumState::pcxt, pfree(), pg_atomic_init_u32(), RelationData::rd_indam, RelationGetRelid, PVShared::relid, PVShared::ring_nbuffers, ParallelVacuumState::shared, shm_toc_allocate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), TidStoreCreateShared(), TidStoreGetDSA(), TidStoreGetHandle(), ParallelContext::toc, VACUUM_OPTION_MAX_VALID_VALUE, VACUUM_OPTION_PARALLEL_BULKDEL, VACUUM_OPTION_PARALLEL_CLEANUP, VACUUM_OPTION_PARALLEL_COND_CLEANUP, ParallelVacuumState::wal_usage, and ParallelVacuumState::will_parallel_vacuum.

Referenced by dead_items_alloc().

◆ parallel_vacuum_main()

void parallel_vacuum_main ( dsm_segment seg,
shm_toc toc 
)

Definition at line 988 of file vacuumparallel.c.

989 {
991  Relation rel;
992  Relation *indrels;
993  PVIndStats *indstats;
994  PVShared *shared;
995  TidStore *dead_items;
996  BufferUsage *buffer_usage;
997  WalUsage *wal_usage;
998  int nindexes;
999  char *sharedquery;
1000  ErrorContextCallback errcallback;
1001 
1002  /*
1003  * A parallel vacuum worker must have only PROC_IN_VACUUM flag since we
1004  * don't support parallel vacuum for autovacuum as of now.
1005  */
1007 
1008  elog(DEBUG1, "starting parallel vacuum worker");
1009 
1010  shared = (PVShared *) shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_SHARED, false);
1011 
1012  /* Set debug_query_string for individual workers */
1013  sharedquery = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_QUERY_TEXT, true);
1014  debug_query_string = sharedquery;
1016 
1017  /*
1018  * Open table. The lock mode is the same as the leader process. It's
1019  * okay because the lock mode does not conflict among the parallel
1020  * workers.
1021  */
1022  rel = table_open(shared->relid, ShareUpdateExclusiveLock);
1023 
1024  /*
1025  * Open all indexes. indrels are sorted in order by OID, which should be
1026  * matched to the leader's one.
1027  */
1028  vac_open_indexes(rel, RowExclusiveLock, &nindexes, &indrels);
1029  Assert(nindexes > 0);
1030 
1031  if (shared->maintenance_work_mem_worker > 0)
1033 
1034  /* Set index statistics */
1035  indstats = (PVIndStats *) shm_toc_lookup(toc,
1037  false);
1038 
1039  /* Find dead_items in shared memory */
1040  dead_items = TidStoreAttach(shared->dead_items_dsa_handle,
1041  shared->dead_items_handle);
1042 
1043  /* Set cost-based vacuum delay */
1045  VacuumCostBalance = 0;
1046  VacuumPageHit = 0;
1047  VacuumPageMiss = 0;
1048  VacuumPageDirty = 0;
1052 
1053  /* Set parallel vacuum state */
1054  pvs.indrels = indrels;
1055  pvs.nindexes = nindexes;
1056  pvs.indstats = indstats;
1057  pvs.shared = shared;
1058  pvs.dead_items = dead_items;
1061  pvs.heaprel = rel;
1062 
1063  /* These fields will be filled during index vacuum or cleanup */
1064  pvs.indname = NULL;
1066 
1067  /* Each parallel VACUUM worker gets its own access strategy. */
1069  shared->ring_nbuffers * (BLCKSZ / 1024));
1070 
1071  /* Setup error traceback support for ereport() */
1073  errcallback.arg = &pvs;
1074  errcallback.previous = error_context_stack;
1075  error_context_stack = &errcallback;
1076 
1077  /* Prepare to track buffer usage during parallel execution */
1079 
1080  /* Process indexes to perform vacuum/cleanup */
1082 
1083  /* Report buffer/WAL usage during parallel execution */
1084  buffer_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_BUFFER_USAGE, false);
1085  wal_usage = shm_toc_lookup(toc, PARALLEL_VACUUM_KEY_WAL_USAGE, false);
1087  &wal_usage[ParallelWorkerNumber]);
1088 
1089  TidStoreDetach(dead_items);
1090 
1091  /* Pop the error context stack */
1092  error_context_stack = errcallback.previous;
1093 
1094  vac_close_indexes(nindexes, indrels, RowExclusiveLock);
1097 }
void VacuumUpdateCosts(void)
Definition: autovacuum.c:1634
int ParallelWorkerNumber
Definition: parallel.c:112
void pgstat_report_activity(BackendState state, const char *cmd_str)
@ STATE_RUNNING
ErrorContextCallback * error_context_stack
Definition: elog.c:94
#define DEBUG1
Definition: elog.h:30
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:681
int64 VacuumPageHit
Definition: globals.c:155
int64 VacuumPageMiss
Definition: globals.c:156
int64 VacuumPageDirty
Definition: globals.c:157
int VacuumCostBalance
Definition: globals.c:159
void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage)
Definition: instrument.c:208
void InstrStartParallelQuery(void)
Definition: instrument.c:200
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define PROC_IN_VACUUM
Definition: proc.h:58
#define RelationGetNamespace(relation)
Definition: rel.h:546
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
PGPROC * MyProc
Definition: proc.c:66
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
uint8 statusFlags
Definition: proc.h:237
PVIndVacStatus status
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TidStore * TidStoreAttach(dsa_handle area_handle, dsa_pointer handle)
Definition: tidstore.c:252
void TidStoreDetach(TidStore *ts)
Definition: tidstore.c:277
pg_atomic_uint32 * VacuumActiveNWorkers
Definition: vacuum.c:103
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:2276
int VacuumCostBalanceLocal
Definition: vacuum.c:104
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:2319
pg_atomic_uint32 * VacuumSharedCostBalance
Definition: vacuum.c:102
static void parallel_vacuum_error_callback(void *arg)
static void parallel_vacuum_process_safe_indexes(ParallelVacuumState *pvs)
@ PARALLEL_INDVAC_STATUS_INITIAL

References PVShared::active_nworkers, ErrorContextCallback::arg, Assert, BAS_VACUUM, ParallelVacuumState::bstrategy, ErrorContextCallback::callback, PVShared::cost_balance, ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, DEBUG1, debug_query_string, elog, error_context_stack, FreeAccessStrategy(), get_namespace_name(), GetAccessStrategyWithSize(), ParallelVacuumState::heaprel, ParallelVacuumState::indname, ParallelVacuumState::indrels, ParallelVacuumState::indstats, InstrEndParallelQuery(), InstrStartParallelQuery(), maintenance_work_mem, PVShared::maintenance_work_mem_worker, MyProc, ParallelVacuumState::nindexes, PARALLEL_INDVAC_STATUS_INITIAL, parallel_vacuum_error_callback(), PARALLEL_VACUUM_KEY_BUFFER_USAGE, PARALLEL_VACUUM_KEY_INDEX_STATS, PARALLEL_VACUUM_KEY_QUERY_TEXT, PARALLEL_VACUUM_KEY_SHARED, PARALLEL_VACUUM_KEY_WAL_USAGE, parallel_vacuum_process_safe_indexes(), ParallelWorkerNumber, pgstat_report_activity(), ErrorContextCallback::previous, PROC_IN_VACUUM, pstrdup(), RelationGetNamespace, RelationGetRelationName, PVShared::relid, ParallelVacuumState::relname, ParallelVacuumState::relnamespace, PVShared::ring_nbuffers, RowExclusiveLock, ParallelVacuumState::shared, ShareUpdateExclusiveLock, shm_toc_lookup(), STATE_RUNNING, ParallelVacuumState::status, PGPROC::statusFlags, table_close(), table_open(), TidStoreAttach(), TidStoreDetach(), vac_close_indexes(), vac_open_indexes(), VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, VacuumPageDirty, VacuumPageHit, VacuumPageMiss, VacuumSharedCostBalance, and VacuumUpdateCosts().

◆ parallel_vacuum_reset_dead_items()

void parallel_vacuum_reset_dead_items ( ParallelVacuumState pvs)

Definition at line 473 of file vacuumparallel.c.

474 {
475  TidStore *dead_items = pvs->dead_items;
476  VacDeadItemsInfo *dead_items_info = &(pvs->shared->dead_items_info);
477 
478  /*
479  * Free the current tidstore and return allocated DSA segments to the
480  * operating system. Then we recreate the tidstore with the same max_bytes
481  * limitation we just used.
482  */
483  TidStoreDestroy(dead_items);
484  pvs->dead_items = TidStoreCreateShared(dead_items_info->max_bytes,
486 
487  /* Update the DSA pointer for dead_items to the new one */
489  pvs->shared->dead_items_handle = TidStoreGetHandle(dead_items);
490 
491  /* Reset the counter */
492  dead_items_info->num_items = 0;
493 }
int64 num_items
Definition: vacuum.h:288

References ParallelVacuumState::dead_items, PVShared::dead_items_dsa_handle, PVShared::dead_items_handle, PVShared::dead_items_info, dsa_get_handle(), LWTRANCHE_PARALLEL_VACUUM_DSA, VacDeadItemsInfo::max_bytes, VacDeadItemsInfo::num_items, ParallelVacuumState::shared, TidStoreCreateShared(), TidStoreDestroy(), TidStoreGetDSA(), and TidStoreGetHandle().

Referenced by dead_items_reset().

◆ std_typanalyze()

bool std_typanalyze ( VacAttrStats stats)

Definition at line 1845 of file analyze.c.

1846 {
1847  Oid ltopr;
1848  Oid eqopr;
1849  StdAnalyzeData *mystats;
1850 
1851  /* If the attstattarget column is negative, use the default value */
1852  if (stats->attstattarget < 0)
1854 
1855  /* Look for default "<" and "=" operators for column's type */
1857  false, false, false,
1858  &ltopr, &eqopr, NULL,
1859  NULL);
1860 
1861  /* Save the operator info for compute_stats routines */
1862  mystats = (StdAnalyzeData *) palloc(sizeof(StdAnalyzeData));
1863  mystats->eqopr = eqopr;
1864  mystats->eqfunc = OidIsValid(eqopr) ? get_opcode(eqopr) : InvalidOid;
1865  mystats->ltopr = ltopr;
1866  stats->extra_data = mystats;
1867 
1868  /*
1869  * Determine which standard statistics algorithm to use
1870  */
1871  if (OidIsValid(eqopr) && OidIsValid(ltopr))
1872  {
1873  /* Seems to be a scalar datatype */
1875  /*--------------------
1876  * The following choice of minrows is based on the paper
1877  * "Random sampling for histogram construction: how much is enough?"
1878  * by Surajit Chaudhuri, Rajeev Motwani and Vivek Narasayya, in
1879  * Proceedings of ACM SIGMOD International Conference on Management
1880  * of Data, 1998, Pages 436-447. Their Corollary 1 to Theorem 5
1881  * says that for table size n, histogram size k, maximum relative
1882  * error in bin size f, and error probability gamma, the minimum
1883  * random sample size is
1884  * r = 4 * k * ln(2*n/gamma) / f^2
1885  * Taking f = 0.5, gamma = 0.01, n = 10^6 rows, we obtain
1886  * r = 305.82 * k
1887  * Note that because of the log function, the dependence on n is
1888  * quite weak; even at n = 10^12, a 300*k sample gives <= 0.66
1889  * bin size error with probability 0.99. So there's no real need to
1890  * scale for n, which is a good thing because we don't necessarily
1891  * know it at this point.
1892  *--------------------
1893  */
1894  stats->minrows = 300 * stats->attstattarget;
1895  }
1896  else if (OidIsValid(eqopr))
1897  {
1898  /* We can still recognize distinct values */
1900  /* Might as well use the same minrows as above */
1901  stats->minrows = 300 * stats->attstattarget;
1902  }
1903  else
1904  {
1905  /* Can't do much but the trivial stuff */
1907  /* Might as well use the same minrows as above */
1908  stats->minrows = 300 * stats->attstattarget;
1909  }
1910 
1911  return true;
1912 }
#define OidIsValid(objectId)
Definition: c.h:775
static void compute_scalar_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:2356
int default_statistics_target
Definition: analyze.c:73
static void compute_distinct_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:2013
static void compute_trivial_stats(VacAttrStatsP stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows)
Definition: analyze.c:1923
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1285
void * palloc(Size size)
Definition: mcxt.c:1317
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:180
unsigned int Oid
Definition: postgres_ext.h:31
Oid attrtypid
Definition: vacuum.h:126
int minrows
Definition: vacuum.h:137
int attstattarget
Definition: vacuum.h:125
void * extra_data
Definition: vacuum.h:138
AnalyzeAttrComputeStatsFunc compute_stats
Definition: vacuum.h:136

References VacAttrStats::attrtypid, VacAttrStats::attstattarget, 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(), examine_attribute(), and examine_expression().

◆ vac_bulkdel_one_index()

IndexBulkDeleteResult* vac_bulkdel_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat,
TidStore dead_items,
VacDeadItemsInfo dead_items_info 
)

Definition at line 2494 of file vacuum.c.

2496 {
2497  /* Do bulk deletion */
2498  istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2499  (void *) dead_items);
2500 
2501  ereport(ivinfo->message_level,
2502  (errmsg("scanned index \"%s\" to remove %lld row versions",
2503  RelationGetRelationName(ivinfo->index),
2504  (long long) dead_items_info->num_items)));
2505 
2506  return istat;
2507 }
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition: indexam.c:748
Relation index
Definition: genam.h:46
int message_level
Definition: genam.h:51
static bool vac_tid_reaped(ItemPointer itemptr, void *state)
Definition: vacuum.c:2541

References ereport, errmsg(), IndexVacuumInfo::index, index_bulk_delete(), IndexVacuumInfo::message_level, VacDeadItemsInfo::num_items, RelationGetRelationName, and vac_tid_reaped().

Referenced by lazy_vacuum_one_index(), and parallel_vacuum_process_one_index().

◆ vac_cleanup_one_index()

IndexBulkDeleteResult* vac_cleanup_one_index ( IndexVacuumInfo ivinfo,
IndexBulkDeleteResult istat 
)

Definition at line 2515 of file vacuum.c.

2516 {
2517  istat = index_vacuum_cleanup(ivinfo, istat);
2518 
2519  if (istat)
2520  ereport(ivinfo->message_level,
2521  (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2522  RelationGetRelationName(ivinfo->index),
2523  istat->num_index_tuples,
2524  istat->num_pages),
2525  errdetail("%.0f index row versions were removed.\n"
2526  "%u index pages were newly deleted.\n"
2527  "%u index pages are currently deleted, of which %u are currently reusable.",
2528  istat->tuples_removed,
2529  istat->pages_newly_deleted,
2530  istat->pages_deleted, istat->pages_free)));
2531 
2532  return istat;
2533 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *istat)
Definition: indexam.c:769
BlockNumber pages_deleted
Definition: genam.h:82
BlockNumber pages_newly_deleted
Definition: genam.h:81
BlockNumber pages_free
Definition: genam.h:83
BlockNumber num_pages
Definition: genam.h:77
double tuples_removed
Definition: genam.h:80
double num_index_tuples
Definition: genam.h:79

References ereport, errdetail(), errmsg(), IndexVacuumInfo::index, index_vacuum_cleanup(), IndexVacuumInfo::message_level, IndexBulkDeleteResult::num_index_tuples, IndexBulkDeleteResult::num_pages, IndexBulkDeleteResult::pages_deleted, IndexBulkDeleteResult::pages_free, IndexBulkDeleteResult::pages_newly_deleted, RelationGetRelationName, and IndexBulkDeleteResult::tuples_removed.

Referenced by lazy_cleanup_one_index(), and parallel_vacuum_process_one_index().

◆ vac_close_indexes()

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

Definition at line 2319 of file vacuum.c.

2320 {
2321  if (Irel == NULL)
2322  return;
2323 
2324  while (nindexes--)
2325  {
2326  Relation ind = Irel[nindexes];
2327 
2328  index_close(ind, lockmode);
2329  }
2330  pfree(Irel);
2331 }
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177

References index_close(), and pfree().

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

◆ vac_estimate_reltuples()

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

Definition at line 1302 of file vacuum.c.

1306 {
1307  BlockNumber old_rel_pages = relation->rd_rel->relpages;
1308  double old_rel_tuples = relation->rd_rel->reltuples;
1309  double old_density;
1310  double unscanned_pages;
1311  double total_tuples;
1312 
1313  /* If we did scan the whole table, just use the count as-is */
1314  if (scanned_pages >= total_pages)
1315  return scanned_tuples;
1316 
1317  /*
1318  * When successive VACUUM commands scan the same few pages again and
1319  * again, without anything from the table really changing, there is a risk
1320  * that our beliefs about tuple density will gradually become distorted.
1321  * This might be caused by vacuumlazy.c implementation details, such as
1322  * its tendency to always scan the last heap page. Handle that here.
1323  *
1324  * If the relation is _exactly_ the same size according to the existing
1325  * pg_class entry, and only a few of its pages (less than 2%) were
1326  * scanned, keep the existing value of reltuples. Also keep the existing
1327  * value when only a subset of rel's pages <= a single page were scanned.
1328  *
1329  * (Note: we might be returning -1 here.)
1330  */
1331  if (old_rel_pages == total_pages &&
1332  scanned_pages < (double) total_pages * 0.02)
1333  return old_rel_tuples;
1334  if (scanned_pages <= 1)
1335  return old_rel_tuples;
1336 
1337  /*
1338  * If old density is unknown, we can't do much except scale up
1339  * scanned_tuples to match total_pages.
1340  */
1341  if (old_rel_tuples < 0 || old_rel_pages == 0)
1342  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1343 
1344  /*
1345  * Okay, we've covered the corner cases. The normal calculation is to
1346  * convert the old measurement to a density (tuples per page), then
1347  * estimate the number of tuples in the unscanned pages using that figure,
1348  * and finally add on the number of tuples in the scanned pages.
1349  */
1350  old_density = old_rel_tuples / old_rel_pages;
1351  unscanned_pages = (double) total_pages - (double) scanned_pages;
1352  total_tuples = old_density * unscanned_pages + scanned_tuples;
1353  return floor(total_tuples + 0.5);
1354 }

References RelationData::rd_rel.

Referenced by lazy_scan_heap(), and statapprox_heap().

◆ vac_open_indexes()

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

Definition at line 2276 of file vacuum.c.

2278 {
2279  List *indexoidlist;
2280  ListCell *indexoidscan;
2281  int i;
2282 
2283  Assert(lockmode != NoLock);
2284 
2285  indexoidlist = RelationGetIndexList(relation);
2286 
2287  /* allocate enough memory for all indexes */
2288  i = list_length(indexoidlist);
2289 
2290  if (i > 0)
2291  *Irel = (Relation *) palloc(i * sizeof(Relation));
2292  else
2293  *Irel = NULL;
2294 
2295  /* collect just the ready indexes */
2296  i = 0;
2297  foreach(indexoidscan, indexoidlist)
2298  {
2299  Oid indexoid = lfirst_oid(indexoidscan);
2300  Relation indrel;
2301 
2302  indrel = index_open(indexoid, lockmode);
2303  if (indrel->rd_index->indisready)
2304  (*Irel)[i++] = indrel;
2305  else
2306  index_close(indrel, lockmode);
2307  }
2308 
2309  *nindexes = i;
2310 
2311  list_free(indexoidlist);
2312 }
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
void list_free(List *list)
Definition: list.c:1546
static int list_length(const List *l)
Definition: pg_list.h:152
#define lfirst_oid(lc)
Definition: pg_list.h:174
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4801
Definition: pg_list.h:54
Form_pg_index rd_index
Definition: rel.h:192

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

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1565 of file vacuum.c.

1566 {
1567  HeapTuple tuple;
1568  Form_pg_database dbform;
1569  Relation relation;
1570  SysScanDesc scan;
1571  HeapTuple classTup;
1572  TransactionId newFrozenXid;
1573  MultiXactId newMinMulti;
1574  TransactionId lastSaneFrozenXid;
1575  MultiXactId lastSaneMinMulti;
1576  bool bogus = false;
1577  bool dirty = false;
1578  ScanKeyData key[1];
1579 
1580  /*
1581  * Restrict this task to one backend per database. This avoids race
1582  * conditions that would move datfrozenxid or datminmxid backward. It
1583  * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1584  * datfrozenxid passed to an earlier vac_truncate_clog() call.
1585  */
1587 
1588  /*
1589  * Initialize the "min" calculation with
1590  * GetOldestNonRemovableTransactionId(), which is a reasonable
1591  * approximation to the minimum relfrozenxid for not-yet-committed
1592  * pg_class entries for new tables; see AddNewRelationTuple(). So we
1593  * cannot produce a wrong minimum by starting with this.
1594  */
1595  newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
1596 
1597  /*
1598  * Similarly, initialize the MultiXact "min" with the value that would be
1599  * used on pg_class for new tables. See AddNewRelationTuple().
1600  */
1601  newMinMulti = GetOldestMultiXactId();
1602 
1603  /*
1604  * Identify the latest relfrozenxid and relminmxid values that we could
1605  * validly see during the scan. These are conservative values, but it's
1606  * not really worth trying to be more exact.
1607  */
1608  lastSaneFrozenXid = ReadNextTransactionId();
1609  lastSaneMinMulti = ReadNextMultiXactId();
1610 
1611  /*
1612  * We must seqscan pg_class to find the minimum Xid, because there is no
1613  * index that can help us here.
1614  *
1615  * See vac_truncate_clog() for the race condition to prevent.
1616  */
1617  relation = table_open(RelationRelationId, AccessShareLock);
1618 
1619  scan = systable_beginscan(relation, InvalidOid, false,
1620  NULL, 0, NULL);
1621 
1622  while ((classTup = systable_getnext(scan)) != NULL)
1623  {
1624  volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1625  TransactionId relfrozenxid = classForm->relfrozenxid;
1626  TransactionId relminmxid = classForm->relminmxid;
1627 
1628  /*
1629  * Only consider relations able to hold unfrozen XIDs (anything else
1630  * should have InvalidTransactionId in relfrozenxid anyway).
1631  */
1632  if (classForm->relkind != RELKIND_RELATION &&
1633  classForm->relkind != RELKIND_MATVIEW &&
1634  classForm->relkind != RELKIND_TOASTVALUE)
1635  {
1636  Assert(!TransactionIdIsValid(relfrozenxid));
1637  Assert(!MultiXactIdIsValid(relminmxid));
1638  continue;
1639  }
1640 
1641  /*
1642  * Some table AMs might not need per-relation xid / multixid horizons.
1643  * It therefore seems reasonable to allow relfrozenxid and relminmxid
1644  * to not be set (i.e. set to their respective Invalid*Id)
1645  * independently. Thus validate and compute horizon for each only if
1646  * set.
1647  *
1648  * If things are working properly, no relation should have a
1649  * relfrozenxid or relminmxid that is "in the future". However, such
1650  * cases have been known to arise due to bugs in pg_upgrade. If we
1651  * see any entries that are "in the future", chicken out and don't do
1652  * anything. This ensures we won't truncate clog & multixact SLRUs
1653  * before those relations have been scanned and cleaned up.
1654  */
1655 
1656  if (TransactionIdIsValid(relfrozenxid))
1657  {
1658  Assert(TransactionIdIsNormal(relfrozenxid));
1659 
1660  /* check for values in the future */
1661  if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1662  {
1663  bogus = true;
1664  break;
1665  }
1666 
1667  /* determine new horizon */
1668  if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1669  newFrozenXid = relfrozenxid;
1670  }
1671 
1672  if (MultiXactIdIsValid(relminmxid))
1673  {
1674  /* check for values in the future */
1675  if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1676  {
1677  bogus = true;
1678  break;
1679  }
1680 
1681  /* determine new horizon */
1682  if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1683  newMinMulti = relminmxid;
1684  }
1685  }
1686 
1687  /* we're done with pg_class */
1688  systable_endscan(scan);
1689  table_close(relation, AccessShareLock);
1690 
1691  /* chicken out if bogus data found */
1692  if (bogus)
1693  return;
1694 
1695  Assert(TransactionIdIsNormal(newFrozenXid));
1696  Assert(MultiXactIdIsValid(newMinMulti));
1697 
1698  /* Now fetch the pg_database tuple we need to update. */
1699  relation = table_open(DatabaseRelationId, RowExclusiveLock);
1700 
1701  /*
1702  * Get the pg_database tuple to scribble on. Note that this does not
1703  * directly rely on the syscache to avoid issues with flattened toast
1704  * values for the in-place update.
1705  */
1706  ScanKeyInit(&key[0],
1707  Anum_pg_database_oid,
1708  BTEqualStrategyNumber, F_OIDEQ,
1710 
1711  scan = systable_beginscan(relation, DatabaseOidIndexId, true,
1712  NULL, 1, key);
1713  tuple = systable_getnext(scan);
1714  tuple = heap_copytuple(tuple);
1715  systable_endscan(scan);
1716 
1717  if (!HeapTupleIsValid(tuple))
1718  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1719 
1720  dbform = (Form_pg_database) GETSTRUCT(tuple);
1721 
1722  /*
1723  * As in vac_update_relstats(), we ordinarily don't want to let
1724  * datfrozenxid go backward; but if it's "in the future" then it must be
1725  * corrupt and it seems best to overwrite it.
1726  */
1727  if (dbform->datfrozenxid != newFrozenXid &&
1728  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1729  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1730  {
1731  dbform->datfrozenxid = newFrozenXid;
1732  dirty = true;
1733  }
1734  else
1735  newFrozenXid = dbform->datfrozenxid;
1736 
1737  /* Ditto for datminmxid */
1738  if (dbform->datminmxid != newMinMulti &&
1739  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1740  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1741  {
1742  dbform->datminmxid = newMinMulti;
1743  dirty = true;
1744  }
1745  else
1746  newMinMulti = dbform->datminmxid;
1747 
1748  if (dirty)
1749  heap_inplace_update(relation, tuple);
1750 
1751  heap_freetuple(tuple);
1752  table_close(relation, RowExclusiveLock);
1753 
1754  /*
1755  * If we were able to advance datfrozenxid or datminmxid, see if we can
1756  * truncate pg_xact and/or pg_multixact. Also do it if the shared
1757  * XID-wrap-limit info is stale, since this action will update that too.
1758  */
1759  if (dirty || ForceTransactionIdLimitUpdate())
1760  vac_truncate_clog(newFrozenXid, newMinMulti,
1761  lastSaneFrozenXid, lastSaneMinMulti);
1762 }
TransactionId MultiXactId
Definition: c.h:662
uint32 TransactionId
Definition: c.h:652
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
Oid MyDatabaseId
Definition: globals.c:92
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6063
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:776
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition: lmgr.c:487
#define AccessShareLock
Definition: lockdefs.h:36
#define ExclusiveLock
Definition: lockdefs.h:42
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3309
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2652
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:770
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
FormData_pg_class
Definition: pg_class.h:142
FormData_pg_database * Form_pg_database
Definition: pg_database.h:96
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:2005
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:315
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1783
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:517

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

Referenced by do_autovacuum(), and vacuum().

◆ 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 frozenxid_updated,
bool minmulti_updated,
bool  in_outer_xact 
)

Definition at line 1398 of file vacuum.c.

1405 {
1406  Oid relid = RelationGetRelid(relation);
1407  Relation rd;
1408  HeapTuple ctup;
1409  Form_pg_class pgcform;
1410  bool dirty,
1411  futurexid,
1412  futuremxid;
1413  TransactionId oldfrozenxid;
1414  MultiXactId oldminmulti;
1415 
1416  rd = table_open(RelationRelationId, RowExclusiveLock);
1417 
1418  /* Fetch a copy of the tuple to scribble on */
1419  ctup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
1420  if (!HeapTupleIsValid(ctup))
1421  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1422  relid);
1423  pgcform = (Form_pg_class) GETSTRUCT(ctup);
1424 
1425  /* Apply statistical updates, if any, to copied tuple */
1426 
1427  dirty = false;
1428  if (pgcform->relpages != (int32) num_pages)
1429  {
1430  pgcform->relpages = (int32) num_pages;
1431  dirty = true;
1432  }
1433  if (pgcform->reltuples != (float4) num_tuples)
1434  {
1435  pgcform->reltuples = (float4) num_tuples;
1436  dirty = true;
1437  }
1438  if (pgcform->relallvisible != (int32) num_all_visible_pages)
1439  {
1440  pgcform->relallvisible = (int32) num_all_visible_pages;
1441  dirty = true;
1442  }
1443 
1444  /* Apply DDL updates, but not inside an outer transaction (see above) */
1445 
1446  if (!in_outer_xact)
1447  {
1448  /*
1449  * If we didn't find any indexes, reset relhasindex.
1450  */
1451  if (pgcform->relhasindex && !hasindex)
1452  {
1453  pgcform->relhasindex = false;
1454  dirty = true;
1455  }
1456 
1457  /* We also clear relhasrules and relhastriggers if needed */
1458  if (pgcform->relhasrules && relation->rd_rules == NULL)
1459  {
1460  pgcform->relhasrules = false;
1461  dirty = true;
1462  }
1463  if (pgcform->relhastriggers && relation->trigdesc == NULL)
1464  {
1465  pgcform->relhastriggers = false;
1466  dirty = true;
1467  }
1468  }
1469 
1470  /*
1471  * Update relfrozenxid, unless caller passed InvalidTransactionId
1472  * indicating it has no new data.
1473  *
1474  * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1475  * stored relfrozenxid is "in the future" then it seems best to assume
1476  * it's corrupt, and overwrite with the oldest remaining XID in the table.
1477  * This should match vac_update_datfrozenxid() concerning what we consider
1478  * to be "in the future".
1479  */
1480  oldfrozenxid = pgcform->relfrozenxid;
1481  futurexid = false;
1482  if (frozenxid_updated)
1483  *frozenxid_updated = false;
1484  if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1485  {
1486  bool update = false;
1487 
1488  if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1489  update = true;
1490  else if (TransactionIdPrecedes(ReadNextTransactionId(), oldfrozenxid))
1491  futurexid = update = true;
1492 
1493  if (update)
1494  {
1495  pgcform->relfrozenxid = frozenxid;
1496  dirty = true;
1497  if (frozenxid_updated)
1498  *frozenxid_updated = true;
1499  }
1500  }
1501 
1502  /* Similarly for relminmxid */
1503  oldminmulti = pgcform->relminmxid;
1504  futuremxid = false;
1505  if (minmulti_updated)
1506  *minmulti_updated = false;
1507  if (MultiXactIdIsValid(minmulti) && oldminmulti != minmulti)
1508  {
1509  bool update = false;
1510 
1511  if (MultiXactIdPrecedes(oldminmulti, minmulti))
1512  update = true;
1513  else if (MultiXactIdPrecedes(ReadNextMultiXactId(), oldminmulti))
1514  futuremxid = update = true;
1515 
1516  if (update)
1517  {
1518  pgcform->relminmxid = minmulti;
1519  dirty = true;
1520  if (minmulti_updated)
1521  *minmulti_updated = true;
1522  }
1523  }
1524 
1525  /* If anything changed, write out the tuple. */
1526  if (dirty)
1527  heap_inplace_update(rd, ctup);
1528 
1530 
1531  if (futurexid)
1532  ereport(WARNING,
1534  errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1535  oldfrozenxid, frozenxid,
1536  RelationGetRelationName(relation))));
1537  if (futuremxid)
1538  ereport(WARNING,
1540  errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1541  oldminmulti, minmulti,
1542  RelationGetRelationName(relation))));
1543 }
signed int int32
Definition: c.h:494
float float4
Definition: c.h:629
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
TriggerDesc * trigdesc
Definition: rel.h:117
RuleLock * rd_rules
Definition: rel.h:115
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:86

References elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, GETSTRUCT, heap_inplace_update(), HeapTupleIsValid, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum(), RelationData::rd_rules, ReadNextMultiXactId(), ReadNextTransactionId(), RelationGetRelationName, RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy1, table_close(), table_open(), TransactionIdIsNormal, TransactionIdPrecedes(), RelationData::trigdesc, and WARNING.

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

◆ vacuum()

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

Definition at line 478 of file vacuum.c.

480 {
481  static bool in_vacuum = false;
482 
483  const char *stmttype;
484  volatile bool in_outer_xact,
485  use_own_xacts;
486 
487  Assert(params != NULL);
488 
489  stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
490 
491  /*
492  * We cannot run VACUUM inside a user transaction block; if we were inside
493  * a transaction, then our commit- and start-transaction-command calls
494  * would not have the intended effect! There are numerous other subtle
495  * dependencies on this, too.
496  *
497  * ANALYZE (without VACUUM) can run either way.
498  */
499  if (params->options & VACOPT_VACUUM)
500  {
501  PreventInTransactionBlock(isTopLevel, stmttype);
502  in_outer_xact = false;
503  }
504  else
505  in_outer_xact = IsInTransactionBlock(isTopLevel);
506 
507  /*
508  * Check for and disallow recursive calls. This could happen when VACUUM
509  * FULL or ANALYZE calls a hostile index expression that itself calls
510  * ANALYZE.
511  */
512  if (in_vacuum)
513  ereport(ERROR,
514  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
515  errmsg("%s cannot be executed from VACUUM or ANALYZE",
516  stmttype)));
517 
518  /*
519  * Build list of relation(s) to process, putting any new data in
520  * vac_context for safekeeping.
521  */
522  if (params->options & VACOPT_ONLY_DATABASE_STATS)
523  {
524  /* We don't process any tables in this case */
525  Assert(relations == NIL);
526  }
527  else if (relations != NIL)
528  {
529  List *newrels = NIL;
530  ListCell *lc;
531 
532  foreach(lc, relations)
533  {
535  List *sublist;
536  MemoryContext old_context;
537 
538  sublist = expand_vacuum_rel(vrel, vac_context, params->options);
539  old_context = MemoryContextSwitchTo(vac_context);
540  newrels = list_concat(newrels, sublist);
541  MemoryContextSwitchTo(old_context);
542  }
543  relations = newrels;
544  }
545  else
546  relations = get_all_vacuum_rels(vac_context, params->options);
547 
548  /*
549  * Decide whether we need to start/commit our own transactions.
550  *
551  * For VACUUM (with or without ANALYZE): always do so, so that we can
552  * release locks as soon as possible. (We could possibly use the outer
553  * transaction for a one-table VACUUM, but handling TOAST tables would be
554  * problematic.)
555  *
556  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
557  * start/commit our own transactions. Also, there's no need to do so if
558  * only processing one relation. For multiple relations when not within a
559  * transaction block, and also in an autovacuum worker, use own
560  * transactions so we can release locks sooner.
561  */
562  if (params->options & VACOPT_VACUUM)
563  use_own_xacts = true;
564  else
565  {
566  Assert(params->options & VACOPT_ANALYZE);
568  use_own_xacts = true;
569  else if (in_outer_xact)
570  use_own_xacts = false;
571  else if (list_length(relations) > 1)
572  use_own_xacts = true;
573  else
574  use_own_xacts = false;
575  }
576 
577  /*
578  * vacuum_rel expects to be entered with no transaction active; it will
579  * start and commit its own transaction. But we are called by an SQL
580  * command, and so we are executing inside a transaction already. We
581  * commit the transaction started in PostgresMain() here, and start
582  * another one before exiting to match the commit waiting for us back in
583  * PostgresMain().
584  */
585  if (use_own_xacts)
586  {
587  Assert(!in_outer_xact);
588 
589  /* ActiveSnapshot is not set by autovacuum */
590  if (ActiveSnapshotSet())
592 
593  /* matches the StartTransaction in PostgresMain() */
595  }
596 
597  /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
598  PG_TRY();
599  {
600  ListCell *cur;
601 
602  in_vacuum = true;
603  VacuumFailsafeActive = false;
605  VacuumCostBalance = 0;
606  VacuumPageHit = 0;
607  VacuumPageMiss = 0;
608  VacuumPageDirty = 0;
611  VacuumActiveNWorkers = NULL;
612 
613  /*
614  * Loop to process each selected relation.
615  */
616  foreach(cur, relations)
617  {
619 
620  if (params->options & VACOPT_VACUUM)
621  {
622  if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy))
623  continue;
624  }
625 
626  if (params->options & VACOPT_ANALYZE)
627  {
628  /*
629  * If using separate xacts, start one for analyze. Otherwise,
630  * we can use the outer transaction.
631  */
632  if (use_own_xacts)
633  {
635  /* functions in indexes may want a snapshot set */
637  }
638 
639  analyze_rel(vrel->oid, vrel->relation, params,
640  vrel->va_cols, in_outer_xact, bstrategy);
641 
642  if (use_own_xacts)
643  {
646  }
647  else
648  {
649  /*
650  * If we're not using separate xacts, better separate the
651  * ANALYZE actions with CCIs. This avoids trouble if user
652  * says "ANALYZE t, t".
653  */
655  }
656  }
657 
658  /*
659  * Ensure VacuumFailsafeActive has been reset before vacuuming the
660  * next relation.
661  */
662  VacuumFailsafeActive = false;
663  }
664  }
665  PG_FINALLY();
666  {
667  in_vacuum = false;
668  VacuumCostActive = false;
669  VacuumFailsafeActive = false;
670  VacuumCostBalance = 0;
671  }
672  PG_END_TRY();
673 
674  /*
675  * Finish up processing.
676  */
677  if (use_own_xacts)
678  {
679  /* here, we are not in a transaction */
680 
681  /*
682  * This matches the CommitTransaction waiting for us in
683  * PostgresMain().
684  */
686  }
687 
688  if ((params->options & VACOPT_VACUUM) &&
689  !(params->options & VACOPT_SKIP_DATABASE_STATS))
690  {
691  /*
692  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
693  */
695  }
696 
697 }
void analyze_rel(Oid relid, RangeVar *relation, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:111
struct cursor * cur
Definition: ecpg.c:28
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define PG_FINALLY(...)
Definition: elog.h:387
bool VacuumCostActive
Definition: globals.c:160
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define AmAutoVacuumWorkerProcess()
Definition: miscadmin.h:375
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:216
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:648
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:782
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
RangeVar * relation
Definition: parsenodes.h:3860
static List * expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context, int options)
Definition: vacuum.c:870
static List * get_all_vacuum_rels(MemoryContext vac_context, int options)
Definition: vacuum.c:1010
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuum.c:1952
void vac_update_datfrozenxid(void)
Definition: vacuum.c:1565
bool VacuumFailsafeActive
Definition: vacuum.c:96
bool IsInTransactionBlock(bool isTopLevel)
Definition: xact.c:3753
void CommandCounterIncrement(void)
Definition: xact.c:1098
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition: xact.c:3622
void StartTransactionCommand(void)
Definition: xact.c:3033
void CommitTransactionCommand(void)
Definition: xact.c:3131

References ActiveSnapshotSet(), AmAutoVacuumWorkerProcess, analyze_rel(), Assert, CommandCounterIncrement(), CommitTransactionCommand(), cur, ereport, errcode(), errmsg(), ERROR, expand_vacuum_rel(), get_all_vacuum_rels(), GetTransactionSnapshot(), IsInTransactionBlock(), lfirst_node, list_concat(), list_length(), MemoryContextSwitchTo(), NIL, VacuumRelation::oid, VacuumParams::options, PG_END_TRY, PG_FINALLY, PG_TRY, PopActiveSnapshot(), PreventInTransactionBlock(), PushActiveSnapshot(), VacuumRelation::relation, StartTransactionCommand(), VacuumRelation::va_cols, vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_ONLY_DATABASE_STATS, VACOPT_SKIP_DATABASE_STATS, VACOPT_VACUUM, vacuum_rel(), VacuumActiveNWorkers, VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumFailsafeActive, VacuumPageDirty, VacuumPageHit, VacuumPageMiss, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by autovacuum_do_vac_analyze(), ExecVacuum(), parallel_vacuum_index_is_parallel_safe(), and parallel_vacuum_process_all_indexes().

◆ vacuum_delay_point()

void vacuum_delay_point ( void  )

Definition at line 2340 of file vacuum.c.

2341 {
2342  double msec = 0;
2343 
2344  /* Always check for interrupts */
2346 
2347  if (InterruptPending ||
2349  return;
2350 
2351  /*
2352  * Autovacuum workers should reload the configuration file if requested.
2353  * This allows changes to [autovacuum_]vacuum_cost_limit and
2354  * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2355  * vacuumed or analyzed.
2356  */
2358  {
2359  ConfigReloadPending = false;
2362  }
2363 
2364  /*
2365  * If we disabled cost-based delays after reloading the config file,
2366  * return.
2367  */
2368  if (!VacuumCostActive)
2369  return;
2370 
2371  /*
2372  * For parallel vacuum, the delay is computed based on the shared cost
2373  * balance. See compute_parallel_delay.
2374  */
2375  if (VacuumSharedCostBalance != NULL)
2376  msec = compute_parallel_delay();
2379 
2380  /* Nap if appropriate */
2381  if (msec > 0)
2382  {
2383  if (msec > vacuum_cost_delay * 4)
2384  msec = vacuum_cost_delay * 4;
2385 
2386  pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);
2387  pg_usleep(msec * 1000);
2389 
2390  /*
2391  * We don't want to ignore postmaster death during very long vacuums
2392  * with vacuum_cost_delay configured. We can't use the usual
2393  * WaitLatch() approach here because we want microsecond-based sleep
2394  * durations above.
2395  */
2397  exit(1);
2398 
2399  VacuumCostBalance = 0;
2400 
2401  /*
2402  * Balance and update limit values for autovacuum workers. We must do
2403  * this periodically, as the number of workers across which we are
2404  * balancing the limit may have changed.
2405  *
2406  * TODO: There may be better criteria for determining when to do this
2407  * besides "check after napping".
2408  */
2410 
2411  /* Might have gotten an interrupt while sleeping */
2413  }
2414 }
void AutoVacuumUpdateCostLimit(void)
Definition: autovacuum.c:1703
volatile sig_atomic_t InterruptPending
Definition: globals.c:31
bool IsUnderPostmaster
Definition: globals.c:118
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
exit(1)
#define PostmasterIsAlive()
Definition: pmsignal.h:106
void pg_usleep(long microsec)
Definition: signal.c:53
double vacuum_cost_delay
Definition: vacuum.c:80
static double compute_parallel_delay(void)
Definition: vacuum.c:2439
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101

References AmAutoVacuumWorkerProcess, AutoVacuumUpdateCostLimit(), CHECK_FOR_INTERRUPTS, compute_parallel_delay(), ConfigReloadPending, exit(), InterruptPending, IsUnderPostmaster, pg_usleep(), PGC_SIGHUP, pgstat_report_wait_end(), pgstat_report_wait_start(), PostmasterIsAlive, ProcessConfigFile(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumSharedCostBalance, and VacuumUpdateCosts().

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_rel(), spgprocesspending(), and spgvacuumpage().

◆ vacuum_get_cutoffs()

bool vacuum_get_cutoffs ( Relation  rel,
const VacuumParams params,
struct VacuumCutoffs cutoffs 
)

Definition at line 1072 of file vacuum.c.

1074 {
1075  int freeze_min_age,
1076  multixact_freeze_min_age,
1077  freeze_table_age,
1078  multixact_freeze_table_age,
1079  effective_multixact_freeze_max_age;
1080  TransactionId nextXID,
1081  safeOldestXmin,
1082  aggressiveXIDCutoff;
1083  MultiXactId nextMXID,
1084  safeOldestMxact,
1085  aggressiveMXIDCutoff;
1086 
1087  /* Use mutable copies of freeze age parameters */
1088  freeze_min_age = params->freeze_min_age;
1089  multixact_freeze_min_age = params->multixact_freeze_min_age;
1090  freeze_table_age = params->freeze_table_age;
1091  multixact_freeze_table_age = params->multixact_freeze_table_age;
1092 
1093  /* Set pg_class fields in cutoffs */
1094  cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1095  cutoffs->relminmxid = rel->rd_rel->relminmxid;
1096 
1097  /*
1098  * Acquire OldestXmin.
1099  *
1100  * We can always ignore processes running lazy vacuum. This is because we
1101  * use these values only for deciding which tuples we must keep in the
1102  * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1103  * XID assigned), it's safe to ignore it. In theory it could be
1104  * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1105  * that only one vacuum process can be working on a particular table at
1106  * any time, and that each vacuum is always an independent transaction.
1107  */
1109 
1111 
1112  /* Acquire OldestMxact */
1113  cutoffs->OldestMxact = GetOldestMultiXactId();
1115 
1116  /* Acquire next XID/next MXID values used to apply age-based settings */
1117  nextXID = ReadNextTransactionId();
1118  nextMXID = ReadNextMultiXactId();
1119 
1120  /*
1121  * Also compute the multixact age for which freezing is urgent. This is
1122  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1123  * short of multixact member space.
1124  */
1125  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1126 
1127  /*
1128  * Almost ready to set freeze output parameters; check if OldestXmin or
1129  * OldestMxact are held back to an unsafe degree before we start on that
1130  */
1131  safeOldestXmin = nextXID - autovacuum_freeze_max_age;
1132  if (!TransactionIdIsNormal(safeOldestXmin))
1133  safeOldestXmin = FirstNormalTransactionId;
1134  safeOldestMxact = nextMXID - effective_multixact_freeze_max_age;
1135  if (safeOldestMxact < FirstMultiXactId)
1136  safeOldestMxact = FirstMultiXactId;
1137  if (TransactionIdPrecedes(cutoffs->OldestXmin, safeOldestXmin))
1138  ereport(WARNING,
1139  (errmsg("cutoff for removing and freezing tuples is far in the past"),
1140  errhint("Close open transactions soon to avoid wraparound problems.\n"
1141  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1142  if (MultiXactIdPrecedes(cutoffs->OldestMxact, safeOldestMxact))
1143  ereport(WARNING,
1144  (errmsg("cutoff for freezing multixacts is far in the past"),
1145  errhint("Close open transactions soon to avoid wraparound problems.\n"
1146  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1147 
1148  /*
1149  * Determine the minimum freeze age to use: as specified by the caller, or
1150  * vacuum_freeze_min_age, but in any case not more than half
1151  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1152  * wraparound won't occur too frequently.
1153  */
1154  if (freeze_min_age < 0)
1155  freeze_min_age = vacuum_freeze_min_age;
1156  freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1157  Assert(freeze_min_age >= 0);
1158 
1159  /* Compute FreezeLimit, being careful to generate a normal XID */
1160  cutoffs->FreezeLimit = nextXID - freeze_min_age;
1161  if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1163  /* FreezeLimit must always be <= OldestXmin */
1164  if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1165  cutoffs->FreezeLimit = cutoffs->OldestXmin;
1166 
1167  /*
1168  * Determine the minimum multixact freeze age to use: as specified by
1169  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1170  * than half effective_multixact_freeze_max_age, so that autovacuums to
1171  * prevent MultiXact wraparound won't occur too frequently.
1172  */
1173  if (multixact_freeze_min_age < 0)
1174  multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1175  multixact_freeze_min_age = Min(multixact_freeze_min_age,
1176  effective_multixact_freeze_max_age / 2);
1177  Assert(multixact_freeze_min_age >= 0);
1178 
1179  /* Compute MultiXactCutoff, being careful to generate a valid value */
1180  cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1181  if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1182  cutoffs->MultiXactCutoff = FirstMultiXactId;
1183  /* MultiXactCutoff must always be <= OldestMxact */
1184  if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1185  cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1186 
1187  /*
1188  * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1189  *
1190  * Determine the table freeze age to use: as specified by the caller, or
1191  * the value of the vacuum_freeze_table_age GUC, but in any case not more
1192  * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1193  * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1194  * anti-wraparound autovacuum is launched.
1195  */
1196  if (freeze_table_age < 0)
1197  freeze_table_age = vacuum_freeze_table_age;
1198  freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1199  Assert(freeze_table_age >= 0);
1200  aggressiveXIDCutoff = nextXID - freeze_table_age;
1201  if (!TransactionIdIsNormal(aggressiveXIDCutoff))
1202  aggressiveXIDCutoff = FirstNormalTransactionId;
1204  aggressiveXIDCutoff))
1205  return true;
1206 
1207  /*
1208  * Similar to the above, determine the table freeze age to use for
1209  * multixacts: as specified by the caller, or the value of the
1210  * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1211  * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1212  * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1213  * multixacts before anti-wraparound autovacuum is launched.
1214  */
1215  if (multixact_freeze_table_age < 0)
1216  multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1217  multixact_freeze_table_age =
1218  Min(multixact_freeze_table_age,
1219  effective_multixact_freeze_max_age * 0.95);
1220  Assert(multixact_freeze_table_age >= 0);
1221  aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1222  if (aggressiveMXIDCutoff < FirstMultiXactId)
1223  aggressiveMXIDCutoff = FirstMultiXactId;
1225  aggressiveMXIDCutoff))
1226  return true;
1227 
1228  /* Non-aggressive VACUUM */
1229  return false;
1230 }
int autovacuum_freeze_max_age
Definition: autovacuum.c:127
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3323
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2970
#define FirstMultiXactId
Definition: multixact.h:25
TransactionId FreezeLimit
Definition: vacuum.h:277
TransactionId OldestXmin
Definition: vacuum.h:267
TransactionId relfrozenxid
Definition: vacuum.h:251
MultiXactId relminmxid
Definition: vacuum.h:252
MultiXactId MultiXactCutoff
Definition: vacuum.h:278
MultiXactId OldestMxact
Definition: vacuum.h:268
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
#define FirstNormalTransactionId
Definition: transam.h:34
int vacuum_freeze_min_age
Definition: vacuum.c:67
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:70
int vacuum_freeze_table_age
Definition: vacuum.c:68
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:69

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg(), FirstMultiXactId, FirstNormalTransactionId, VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, VacuumCutoffs::FreezeLimit, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), Min, VacuumParams::multixact_freeze_min_age, VacuumParams::multixact_freeze_table_age, VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), MultiXactMemberFreezeThreshold(), VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, RelationData::rd_rel, ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), 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().

◆ vacuum_is_permitted_for_relation()

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

Definition at line 706 of file vacuum.c.

708 {
709  char *relname;
710 
712 
713  /*----------
714  * A role has privileges to vacuum or analyze the relation if any of the
715  * following are true:
716  * - the role owns the current database and the relation is not shared
717  * - the role has the MAINTAIN privilege on the relation
718  *----------
719  */
720  if ((object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()) &&
721  !reltuple->relisshared) ||
723  return true;
724 
725  relname = NameStr(reltuple->relname);
726 
727  if ((options & VACOPT_VACUUM) != 0)
728  {
730  (errmsg("permission denied to vacuum \"%s\", skipping it",
731  relname)));
732 
733  /*
734  * For VACUUM ANALYZE, both logs could show up, but just generate
735  * information for VACUUM as that would be the first one to be
736  * processed.
737  */
738  return false;
739  }
740 
741  if ((options & VACOPT_ANALYZE) != 0)
743  (errmsg("permission denied to analyze \"%s\", skipping it",
744  relname)));
745 
746  return false;
747 }
@ ACLCHECK_OK
Definition: acl.h:183
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4142
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4091
#define NameStr(name)
Definition: c.h:746
Oid GetUserId(void)
Definition: miscinit.c:514
#define ACL_MAINTAIN
Definition: parsenodes.h:90
NameData relname
Definition: pg_class.h:38

References ACL_MAINTAIN, ACLCHECK_OK, Assert, ereport, errmsg(), GetUserId(), MyDatabaseId, NameStr, object_ownercheck(), pg_class_aclcheck(), relname, VACOPT_ANALYZE, VACOPT_VACUUM, and WARNING.

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

◆ vacuum_open_relation()

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

Definition at line 758 of file vacuum.c.

760 {
761  Relation rel;
762  bool rel_lock = true;
763  int elevel;
764 
766 
767  /*
768  * Open the relation and get the appropriate lock on it.
769  *
770  * There's a race condition here: the relation may have gone away since
771  * the last time we saw it. If so, we don't need to vacuum or analyze it.
772  *
773  * If we've been asked not to wait for the relation lock, acquire it first
774  * in non-blocking mode, before calling try_relation_open().
775  */
776  if (!(options & VACOPT_SKIP_LOCKED))
777  rel = try_relation_open(relid, lmode);
778  else if (ConditionalLockRelationOid(relid, lmode))
779  rel = try_relation_open(relid, NoLock);
780  else
781  {
782  rel = NULL;
783  rel_lock = false;
784  }
785 
786  /* if relation is opened, leave */
787  if (rel)
788  return rel;
789 
790  /*
791  * Relation could not be opened, hence generate if possible a log
792  * informing on the situation.
793  *
794  * If the RangeVar is not defined, we do not have enough information to
795  * provide a meaningful log statement. Chances are that the caller has
796  * intentionally not provided this information so that this logging is
797  * skipped, anyway.
798  */
799  if (relation == NULL)
800  return NULL;
801 
802  /*
803  * Determine the log level.
804  *
805  * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
806  * statements in the permission checks; otherwise, only log if the caller
807  * so requested.
808  */
810  elevel = WARNING;
811  else if (verbose)
812  elevel = LOG;
813  else
814  return NULL;
815 
816  if ((options & VACOPT_VACUUM) != 0)
817  {
818  if (!rel_lock)
819  ereport(elevel,
820  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
821  errmsg("skipping vacuum of \"%s\" --- lock not available",
822  relation->relname)));
823  else
824  ereport(elevel,
826  errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
827  relation->relname)));
828 
829  /*
830  * For VACUUM ANALYZE, both logs could show up, but just generate
831  * information for VACUUM as that would be the first one to be
832  * processed.
833  */
834  return NULL;
835  }
836 
837  if ((options & VACOPT_ANALYZE) != 0)
838  {
839  if (!rel_lock)
840  ereport(elevel,
841  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
842  errmsg("skipping analyze of \"%s\" --- lock not available",
843  relation->relname)));
844  else
845  ereport(elevel,
847  errmsg("skipping analyze of \"%s\" --- relation no longer exists",
848  relation->relname)));
849  }
850 
851  return NULL;
852 }
#define LOG
Definition: elog.h:31
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:151
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:88
char * relname
Definition: primnodes.h:82

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

Referenced by analyze_rel(), and vacuum_rel().

◆ vacuum_xid_failsafe_check()

bool vacuum_xid_failsafe_check ( const struct VacuumCutoffs cutoffs)

Definition at line 1240 of file vacuum.c.

1241 {
1242  TransactionId relfrozenxid = cutoffs->relfrozenxid;
1243  MultiXactId relminmxid = cutoffs->relminmxid;
1244  TransactionId xid_skip_limit;
1245  MultiXactId multi_skip_limit;
1246  int skip_index_vacuum;
1247 
1248  Assert(TransactionIdIsNormal(relfrozenxid));
1249  Assert(MultiXactIdIsValid(relminmxid));
1250 
1251  /*
1252  * Determine the index skipping age to use. In any case no less than
1253  * autovacuum_freeze_max_age * 1.05.
1254  */
1255  skip_index_vacuum = Max(vacuum_failsafe_age, autovacuum_freeze_max_age * 1.05);
1256 
1257  xid_skip_limit = ReadNextTransactionId() - skip_index_vacuum;
1258  if (!TransactionIdIsNormal(xid_skip_limit))
1259  xid_skip_limit = FirstNormalTransactionId;
1260 
1261  if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1262  {
1263  /* The table's relfrozenxid is too old */
1264  return true;
1265  }
1266 
1267  /*
1268  * Similar to above, determine the index skipping age to use for
1269  * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1270  * 1.05.
1271  */
1272  skip_index_vacuum = Max(vacuum_multixact_failsafe_age,
1274 
1275  multi_skip_limit = ReadNextMultiXactId() - skip_index_vacuum;
1276  if (multi_skip_limit < FirstMultiXactId)
1277  multi_skip_limit = FirstMultiXactId;
1278 
1279  if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1280  {
1281  /* The table's relminmxid is too old */
1282  return true;
1283  }
1284 
1285  return false;
1286 }
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:128
int vacuum_multixact_failsafe_age
Definition: vacuum.c:72
int vacuum_failsafe_age
Definition: vacuum.c:71

References Assert, autovacuum_freeze_max_age, autovacuum_multixact_freeze_max_age, FirstMultiXactId, FirstNormalTransactionId, Max, MultiXactIdIsValid, MultiXactIdPrecedes(), ReadNextMultiXactId(), ReadNextTransactionId(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, TransactionIdIsNormal, TransactionIdPrecedes(), vacuum_failsafe_age, and vacuum_multixact_failsafe_age.

Referenced by lazy_check_wraparound_failsafe().

◆ VacuumUpdateCosts()

void VacuumUpdateCosts ( void  )

Definition at line 1634 of file autovacuum.c.

1635 {
1636  if (MyWorkerInfo)
1637  {
1638  if (av_storage_param_cost_delay >= 0)
1640  else if (autovacuum_vac_cost_delay >= 0)
1642  else
1643  /* fall back to VacuumCostDelay */
1645 
1647  }
1648  else
1649  {
1650  /* Must be explicit VACUUM or ANALYZE */
1653  }
1654 
1655  /*
1656  * If configuration changes are allowed to impact VacuumCostActive, make
1657  * sure it is updated.
1658  */
1661  else if (vacuum_cost_delay > 0)
1662  VacuumCostActive = true;
1663  else
1664  {
1665  VacuumCostActive = false;
1666  VacuumCostBalance = 0;
1667  }
1668 
1669  /*
1670  * Since the cost logging requires a lock, avoid rendering the log message
1671  * in case we are using a message level where the log wouldn't be emitted.
1672  */
1674  {
1675  Oid dboid,
1676  tableoid;
1677 
1678  Assert(!LWLockHeldByMe(AutovacuumLock));
1679 
1680  LWLockAcquire(AutovacuumLock, LW_SHARED);
1681  dboid = MyWorkerInfo->wi_dboid;
1682  tableoid = MyWorkerInfo->wi_tableoid;
1683  LWLockRelease(AutovacuumLock);
1684 
1685  elog(DEBUG2,
1686  "Autovacuum VacuumUpdateCosts(db=%u, rel=%u, dobalance=%s, cost_limit=%d, cost_delay=%g active=%s failsafe=%s)",
1687  dboid, tableoid, pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance) ? "no" : "yes",
1689  vacuum_cost_delay > 0 ? "yes" : "no",
1690  VacuumFailsafeActive ? "yes" : "no");
1691  }
1692 }
static double av_storage_param_cost_delay
Definition: autovacuum.c:148
double autovacuum_vac_cost_delay
Definition: autovacuum.c:130
bool message_level_is_interesting(int elevel)
Definition: elog.c:272
double VacuumCostDelay
Definition: globals.c:153
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1893
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_SHARED
Definition: lwlock.h:115

References Assert, autovacuum_vac_cost_delay, AutoVacuumUpdateCostLimit(), av_storage_param_cost_delay, DEBUG2, elog, LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), message_level_is_interesting(), MyWorkerInfo, pg_atomic_unlocked_test_flag(), vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumCostDelay, VacuumCostLimit, VacuumFailsafeActive, WorkerInfoData::wi_dboid, WorkerInfoData::wi_dobalance, and WorkerInfoData::wi_tableoid.

Referenced by do_autovacuum(), parallel_vacuum_main(), vacuum(), and vacuum_delay_point().

Variable Documentation

◆ default_statistics_target

PGDLLIMPORT int default_statistics_target
extern

◆ vacuum_cost_delay

PGDLLIMPORT double vacuum_cost_delay
extern

◆ vacuum_cost_limit

PGDLLIMPORT int vacuum_cost_limit
extern

◆ vacuum_failsafe_age

PGDLLIMPORT int vacuum_failsafe_age
extern

Definition at line 71 of file vacuum.c.

Referenced by vacuum_xid_failsafe_check().

◆ vacuum_freeze_min_age

PGDLLIMPORT int vacuum_freeze_min_age
extern

Definition at line 67 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_freeze_table_age

PGDLLIMPORT int vacuum_freeze_table_age
extern

Definition at line 68 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_failsafe_age

PGDLLIMPORT int vacuum_multixact_failsafe_age
extern

Definition at line 72 of file vacuum.c.

Referenced by vacuum_xid_failsafe_check().

◆ vacuum_multixact_freeze_min_age

PGDLLIMPORT int vacuum_multixact_freeze_min_age
extern

Definition at line 69 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_freeze_table_age

PGDLLIMPORT int vacuum_multixact_freeze_table_age
extern

Definition at line 70 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ VacuumActiveNWorkers

◆ VacuumCostBalanceLocal

PGDLLIMPORT int VacuumCostBalanceLocal
extern

◆ VacuumFailsafeActive

◆ VacuumSharedCostBalance