PostgreSQL Source Code git master
Loading...
Searching...
No Matches
vacuum.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/tableam.h"
#include "access/transam.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_inherits.h"
#include "commands/async.h"
#include "commands/defrem.h"
#include "commands/progress.h"
#include "commands/repack.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/interrupt.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/guc_hooks.h"
#include "utils/injection_point.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/wait_event.h"
Include dependency graph for vacuum.c:

Go to the source code of this file.

Macros

#define PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS   (NS_PER_S)
 

Functions

static Listexpand_vacuum_rel (VacuumRelation *vrel, MemoryContext vac_context, int options)
 
static Listget_all_vacuum_rels (MemoryContext vac_context, int options)
 
static void vac_truncate_clog (TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
 
static bool vacuum_rel (Oid relid, RangeVar *relation, VacuumParams params, BufferAccessStrategy bstrategy, bool isTopLevel)
 
static double compute_parallel_delay (void)
 
static VacOptValue get_vacoptval_from_boolean (DefElem *def)
 
static bool vac_tid_reaped (ItemPointer itemptr, void *state)
 
bool check_vacuum_buffer_usage_limit (int *newval, void **extra, GucSource source)
 
void ExecVacuum (ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
 
void vacuum (List *relations, const VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
 
bool vacuum_is_permitted_for_relation (Oid relid, Form_pg_class reltuple, uint32 options)
 
Relation vacuum_open_relation (Oid relid, RangeVar *relation, uint32 options, bool verbose, LOCKMODE lmode)
 
bool vacuum_get_cutoffs (Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
 
bool vacuum_xid_failsafe_check (const struct VacuumCutoffs *cutoffs)
 
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, BlockNumber num_all_frozen_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool *frozenxid_updated, bool *minmulti_updated, bool in_outer_xact)
 
void vac_update_datfrozenxid (void)
 
void vac_open_indexes (Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
 
void vac_close_indexes (int nindexes, Relation *Irel, LOCKMODE lockmode)
 
void vacuum_delay_point (bool is_analyze)
 
IndexBulkDeleteResultvac_bulkdel_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
 
IndexBulkDeleteResultvac_cleanup_one_index (IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
 

Variables

int vacuum_freeze_min_age
 
int vacuum_freeze_table_age
 
int vacuum_multixact_freeze_min_age
 
int vacuum_multixact_freeze_table_age
 
int vacuum_failsafe_age
 
int vacuum_multixact_failsafe_age
 
double vacuum_max_eager_freeze_failure_rate
 
bool track_cost_delay_timing
 
bool vacuum_truncate
 
double vacuum_cost_delay = 0
 
int vacuum_cost_limit = 200
 
int64 parallel_vacuum_worker_delay_ns = 0
 
bool VacuumFailsafeActive = false
 
pg_atomic_uint32VacuumSharedCostBalance = NULL
 
pg_atomic_uint32VacuumActiveNWorkers = NULL
 
int VacuumCostBalanceLocal = 0
 

Macro Definition Documentation

◆ PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS

#define PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS   (NS_PER_S)

Definition at line 71 of file vacuum.c.

Function Documentation

◆ check_vacuum_buffer_usage_limit()

bool check_vacuum_buffer_usage_limit ( int newval,
void **  extra,
GucSource  source 
)

Definition at line 140 of file vacuum.c.

142{
143 /* Value upper and lower hard limits are inclusive */
144 if (*newval == 0 || (*newval >= MIN_BAS_VAC_RING_SIZE_KB &&
146 return true;
147
148 /* Value does not fall within any allowable range */
149 GUC_check_errdetail("\"%s\" must be 0 or between %d kB and %d kB.",
150 "vacuum_buffer_usage_limit",
152
153 return false;
154}
#define newval
#define GUC_check_errdetail
Definition guc.h:507
#define MIN_BAS_VAC_RING_SIZE_KB
Definition miscadmin.h:281
#define MAX_BAS_VAC_RING_SIZE_KB
Definition miscadmin.h:282

References GUC_check_errdetail, MAX_BAS_VAC_RING_SIZE_KB, MIN_BAS_VAC_RING_SIZE_KB, and newval.

◆ compute_parallel_delay()

static double compute_parallel_delay ( void  )
static

Definition at line 2608 of file vacuum.c.

2609{
2610 double msec = 0;
2612 int nworkers;
2613
2614 /* Parallel vacuum must be active */
2616
2618
2619 /* At least count itself */
2620 Assert(nworkers >= 1);
2621
2622 /* Update the shared cost balance value atomically */
2624
2625 /* Compute the total local balance for the current worker */
2627
2629 (VacuumCostBalanceLocal > 0.5 * ((double) vacuum_cost_limit / nworkers)))
2630 {
2631 /* Compute sleep time based on the local cost balance */
2635 }
2636
2637 /*
2638 * Reset the local balance as we accumulated it into the shared value.
2639 */
2641
2642 return msec;
2643}
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition atomics.h:439
static uint32 pg_atomic_add_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition atomics.h:424
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
int VacuumCostBalance
Definition globals.c:160
static int fb(int x)
pg_atomic_uint32 * VacuumActiveNWorkers
Definition vacuum.c:118
double vacuum_cost_delay
Definition vacuum.c:92
int VacuumCostBalanceLocal
Definition vacuum.c:119
pg_atomic_uint32 * VacuumSharedCostBalance
Definition vacuum.c:117
int vacuum_cost_limit
Definition vacuum.c:93

References Assert, fb(), pg_atomic_add_fetch_u32(), pg_atomic_read_u32(), pg_atomic_sub_fetch_u32(), vacuum_cost_delay, vacuum_cost_limit, VacuumActiveNWorkers, VacuumCostBalance, VacuumCostBalanceLocal, and VacuumSharedCostBalance.

Referenced by vacuum_delay_point().

◆ ExecVacuum()

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

Definition at line 163 of file vacuum.c.

164{
165 VacuumParams params;
166 BufferAccessStrategy bstrategy = NULL;
167 bool verbose = false;
168 bool skip_locked = false;
169 bool analyze = false;
170 bool freeze = false;
171 bool full = false;
172 bool disable_page_skipping = false;
173 bool process_main = true;
174 bool process_toast = true;
175 int ring_size;
176 bool skip_database_stats = false;
177 bool only_database_stats = false;
179 ListCell *lc;
180
181 /* index_cleanup and truncate values unspecified for now */
184
185 /* By default parallel vacuum is enabled */
186 params.nworkers = 0;
187
188 /* Will be set later if we recurse to a TOAST table. */
189 params.toast_parent = InvalidOid;
190
191 /*
192 * Set this to an invalid value so it is clear whether or not a
193 * BUFFER_USAGE_LIMIT was specified when making the access strategy.
194 */
195 ring_size = -1;
196
197 /* Parse options list */
198 foreach(lc, vacstmt->options)
199 {
200 DefElem *opt = (DefElem *) lfirst(lc);
201
202 /* Parse common options for VACUUM and ANALYZE */
203 if (strcmp(opt->defname, "verbose") == 0)
204 verbose = defGetBoolean(opt);
205 else if (strcmp(opt->defname, "skip_locked") == 0)
206 skip_locked = defGetBoolean(opt);
207 else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
208 {
209 const char *hintmsg;
210 int result;
211 char *vac_buffer_size;
212
214
215 /*
216 * Check that the specified value is valid and the size falls
217 * within the hard upper and lower limits if it is not 0.
218 */
220 (result != 0 &&
222 {
225 errmsg("%s option must be 0 or between %d kB and %d kB",
226 "BUFFER_USAGE_LIMIT",
228 hintmsg ? errhint_internal("%s", _(hintmsg)) : 0));
229 }
230
232 }
233 else if (!vacstmt->is_vacuumcmd)
236 errmsg("unrecognized %s option \"%s\"",
237 "ANALYZE", opt->defname),
238 parser_errposition(pstate, opt->location)));
239
240 /* Parse options available on VACUUM */
241 else if (strcmp(opt->defname, "analyze") == 0)
242 analyze = defGetBoolean(opt);
243 else if (strcmp(opt->defname, "freeze") == 0)
244 freeze = defGetBoolean(opt);
245 else if (strcmp(opt->defname, "full") == 0)
246 full = defGetBoolean(opt);
247 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
248 disable_page_skipping = defGetBoolean(opt);
249 else if (strcmp(opt->defname, "index_cleanup") == 0)
250 {
251 /* Interpret no string as the default, which is 'auto' */
252 if (!opt->arg)
254 else
255 {
256 char *sval = defGetString(opt);
257
258 /* Try matching on 'auto' string, or fall back on boolean */
259 if (pg_strcasecmp(sval, "auto") == 0)
261 else
263 }
264 }
265 else if (strcmp(opt->defname, "process_main") == 0)
266 process_main = defGetBoolean(opt);
267 else if (strcmp(opt->defname, "process_toast") == 0)
268 process_toast = defGetBoolean(opt);
269 else if (strcmp(opt->defname, "truncate") == 0)
271 else if (strcmp(opt->defname, "parallel") == 0)
272 {
273 int nworkers = defGetInt32(opt);
274
278 errmsg("%s option must be between 0 and %d",
279 "PARALLEL",
281 parser_errposition(pstate, opt->location)));
282
283 /*
284 * Disable parallel vacuum, if user has specified parallel degree
285 * as zero.
286 */
287 if (nworkers == 0)
288 params.nworkers = -1;
289 else
290 params.nworkers = nworkers;
291 }
292 else if (strcmp(opt->defname, "skip_database_stats") == 0)
293 skip_database_stats = defGetBoolean(opt);
294 else if (strcmp(opt->defname, "only_database_stats") == 0)
296 else
299 errmsg("unrecognized %s option \"%s\"",
300 "VACUUM", opt->defname),
301 parser_errposition(pstate, opt->location)));
302 }
303
304 /* Set vacuum options */
305 params.options =
306 (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
307 (verbose ? VACOPT_VERBOSE : 0) |
308 (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
309 (analyze ? VACOPT_ANALYZE : 0) |
310 (freeze ? VACOPT_FREEZE : 0) |
311 (full ? VACOPT_FULL : 0) |
312 (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
313 (process_main ? VACOPT_PROCESS_MAIN : 0) |
314 (process_toast ? VACOPT_PROCESS_TOAST : 0) |
315 (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
317
318 /* sanity checks on options */
320 Assert((params.options & VACOPT_VACUUM) ||
321 !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
322
323 if ((params.options & VACOPT_FULL) && params.nworkers > 0)
326 errmsg("VACUUM FULL cannot be performed in parallel")));
327
328 /*
329 * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
330 * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
331 * we'll permit that.
332 */
333 if (ring_size != -1 && (params.options & VACOPT_FULL) &&
334 !(params.options & VACOPT_ANALYZE))
337 errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
338
339 /*
340 * Make sure VACOPT_ANALYZE is specified if any column lists are present.
341 */
342 if (!(params.options & VACOPT_ANALYZE))
343 {
344 foreach(lc, vacstmt->rels)
345 {
347
348 if (vrel->va_cols != NIL)
351 errmsg("ANALYZE option must be specified when a column list is provided")));
352 }
353 }
354
355 /*
356 * Sanity check DISABLE_PAGE_SKIPPING option.
357 */
358 if ((params.options & VACOPT_FULL) != 0 &&
362 errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
363
364 /* sanity check for PROCESS_TOAST */
365 if ((params.options & VACOPT_FULL) != 0 &&
366 (params.options & VACOPT_PROCESS_TOAST) == 0)
369 errmsg("PROCESS_TOAST required with VACUUM FULL")));
370
371 /* sanity check for ONLY_DATABASE_STATS */
373 {
374 Assert(params.options & VACOPT_VACUUM);
375 if (vacstmt->rels != NIL)
378 errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
379 /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
380 if (params.options & ~(VACOPT_VACUUM |
387 errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
388 }
389
390 /*
391 * All freeze ages are zero if the FREEZE option is given; otherwise pass
392 * them as -1 which means to use the default values.
393 */
394 if (params.options & VACOPT_FREEZE)
395 {
396 params.freeze_min_age = 0;
397 params.freeze_table_age = 0;
398 params.multixact_freeze_min_age = 0;
400 }
401 else
402 {
403 params.freeze_min_age = -1;
404 params.freeze_table_age = -1;
405 params.multixact_freeze_min_age = -1;
406 params.multixact_freeze_table_age = -1;
407 }
408
409 /* user-invoked vacuum is never "for wraparound" */
410 params.is_wraparound = false;
411
412 /*
413 * user-invoked vacuum uses VACOPT_VERBOSE instead of
414 * log_vacuum_min_duration and log_analyze_min_duration
415 */
416 params.log_vacuum_min_duration = -1;
417 params.log_analyze_min_duration = -1;
418
419 /*
420 * Later, in vacuum_rel(), we check if a reloption override was specified.
421 */
423
424 /*
425 * Create special memory context for cross-transaction storage.
426 *
427 * Since it is a child of PortalContext, it will go away eventually even
428 * if we suffer an error; there's no need for special abort cleanup logic.
429 */
431 "Vacuum",
433
434 /*
435 * Make a buffer strategy object in the cross-transaction memory context.
436 * We needn't bother making this for VACUUM (FULL) or VACUUM
437 * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
438 * ANALYZE) is possible, so we'd better ensure that we make a strategy
439 * when we see ANALYZE.
440 */
441 if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
442 VACOPT_FULL)) == 0 ||
443 (params.options & VACOPT_ANALYZE) != 0)
444 {
445
447
448 Assert(ring_size >= -1);
449
450 /*
451 * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
452 * command, it overrides the value of VacuumBufferUsageLimit. Either
453 * value may be 0, in which case GetAccessStrategyWithSize() will
454 * return NULL, effectively allowing full use of shared buffers.
455 */
456 if (ring_size == -1)
458
460
462 }
463
464 /* Now go through the common routine */
465 vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
466
467 /* Finally, clean up the vacuum memory context */
469}
#define MAX_PARALLEL_WORKER_LIMIT
@ BAS_VACUUM
Definition bufmgr.h:40
uint32 result
int32 defGetInt32(DefElem *def)
Definition define.c:148
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
int errcode(int sqlerrcode)
Definition elog.c:875
#define _(x)
Definition elog.c:96
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
int int errhint_internal(const char *fmt,...) pg_attribute_printf(1
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition freelist.c:511
int VacuumBufferUsageLimit
Definition globals.c:152
bool parse_int(const char *value, int *result, int flags, const char **hintmsg)
Definition guc.c:2775
#define GUC_UNIT_KB
Definition guc.h:232
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:475
MemoryContext PortalContext
Definition mcxt.c:176
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
static char * errmsg
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
static int verbose
#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)
#define InvalidOid
static long analyze(struct nfa *nfa)
Definition regc_nfa.c:3051
char * defname
Definition parsenodes.h:860
ParseLoc location
Definition parsenodes.h:864
Node * arg
Definition parsenodes.h:861
int nworkers
Definition vacuum.h:250
int freeze_table_age
Definition vacuum.h:220
VacOptValue truncate
Definition vacuum.h:235
int freeze_min_age
Definition vacuum.h:219
int log_vacuum_min_duration
Definition vacuum.h:226
uint32 options
Definition vacuum.h:218
bool is_wraparound
Definition vacuum.h:225
int multixact_freeze_min_age
Definition vacuum.h:221
int multixact_freeze_table_age
Definition vacuum.h:223
Oid toast_parent
Definition vacuum.h:236
VacOptValue index_cleanup
Definition vacuum.h:234
int log_analyze_min_duration
Definition vacuum.h:230
double max_eager_freeze_failure_rate
Definition vacuum.h:243
void vacuum(List *relations, const VacuumParams *params, BufferAccessStrategy bstrategy, MemoryContext vac_context, bool isTopLevel)
Definition vacuum.c:494
double vacuum_max_eager_freeze_failure_rate
Definition vacuum.c:82
static VacOptValue get_vacoptval_from_boolean(DefElem *def)
Definition vacuum.c:2652
#define VACOPT_FREEZE
Definition vacuum.h:182
#define VACOPT_SKIP_LOCKED
Definition vacuum.h:184
#define VACOPT_VACUUM
Definition vacuum.h:179
#define VACOPT_VERBOSE
Definition vacuum.h:181
#define VACOPT_FULL
Definition vacuum.h:183
#define VACOPT_SKIP_DATABASE_STATS
Definition vacuum.h:188
@ VACOPTVALUE_AUTO
Definition vacuum.h:202
@ VACOPTVALUE_UNSPECIFIED
Definition vacuum.h:201
#define VACOPT_PROCESS_TOAST
Definition vacuum.h:186
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition vacuum.h:187
#define VACOPT_ONLY_DATABASE_STATS
Definition vacuum.h:189
#define VACOPT_PROCESS_MAIN
Definition vacuum.h:185
#define VACOPT_ANALYZE
Definition vacuum.h:180

References _, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, analyze(), DefElem::arg, Assert, BAS_VACUUM, defGetBoolean(), defGetInt32(), defGetString(), DefElem::defname, ereport, errcode(), errhint_internal(), errmsg, ERROR, fb(), VacuumParams::freeze_min_age, VacuumParams::freeze_table_age, get_vacoptval_from_boolean(), GetAccessStrategyWithSize(), GUC_UNIT_KB, VacuumParams::index_cleanup, InvalidOid, VacuumParams::is_wraparound, lfirst, lfirst_node, DefElem::location, VacuumParams::log_analyze_min_duration, VacuumParams::log_vacuum_min_duration, MAX_BAS_VAC_RING_SIZE_KB, VacuumParams::max_eager_freeze_failure_rate, 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, parse_int(), parser_errposition(), pg_strcasecmp(), PortalContext, result, VacuumParams::toast_parent, VacuumParams::truncate, 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(), vacuum_max_eager_freeze_failure_rate, VacuumBufferUsageLimit, and verbose.

Referenced by standard_ProcessUtility().

◆ expand_vacuum_rel()

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

Definition at line 884 of file vacuum.c.

886{
887 List *vacrels = NIL;
888 MemoryContext oldcontext;
889
890 /* If caller supplied OID, there's nothing we need do here. */
891 if (OidIsValid(vrel->oid))
892 {
895 MemoryContextSwitchTo(oldcontext);
896 }
897 else
898 {
899 /*
900 * Process a specific relation, and possibly partitions or child
901 * tables thereof.
902 */
903 Oid relid;
904 HeapTuple tuple;
906 bool include_children;
908 int rvr_opts;
909
910 /*
911 * Since autovacuum workers supply OIDs when calling vacuum(), no
912 * autovacuum worker should reach this code.
913 */
915
916 /*
917 * We transiently take AccessShareLock to protect the syscache lookup
918 * below, as well as find_all_inheritors's expectation that the caller
919 * holds some lock on the starting relation.
920 */
922 relid = RangeVarGetRelidExtended(vrel->relation,
924 rvr_opts,
925 NULL, NULL);
926
927 /*
928 * If the lock is unavailable, emit the same log statement that
929 * vacuum_rel() and analyze_rel() would.
930 */
931 if (!OidIsValid(relid))
932 {
936 errmsg("skipping vacuum of \"%s\" --- lock not available",
937 vrel->relation->relname)));
938 else
941 errmsg("skipping analyze of \"%s\" --- lock not available",
942 vrel->relation->relname)));
943 return vacrels;
944 }
945
946 /*
947 * To check whether the relation is a partitioned table and its
948 * ownership, fetch its syscache entry.
949 */
950 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
951 if (!HeapTupleIsValid(tuple))
952 elog(ERROR, "cache lookup failed for relation %u", relid);
954
955 /*
956 * Make a returnable VacuumRelation for this rel if the user has the
957 * required privileges.
958 */
960 {
963 relid,
964 vrel->va_cols));
965 MemoryContextSwitchTo(oldcontext);
966 }
967
968 /*
969 * Vacuuming a partitioned table with ONLY will not do anything since
970 * the partitioned table itself is empty. Issue a warning if the user
971 * requests this.
972 */
973 include_children = vrel->relation->inh;
977 (errmsg("VACUUM ONLY of partitioned table \"%s\" has no effect",
978 vrel->relation->relname)));
979
980 ReleaseSysCache(tuple);
981
982 /*
983 * Unless the user has specified ONLY, make relation list entries for
984 * its partitions or inheritance child tables. Note that the list
985 * returned by find_all_inheritors() includes the passed-in OID, so we
986 * have to skip that. There's no point in taking locks on the
987 * individual partitions or child tables yet, and doing so would just
988 * add unnecessary deadlock risk. For this last reason, we do not yet
989 * check the ownership of the partitions/tables, which get added to
990 * the list to process. Ownership will be checked later on anyway.
991 */
993 {
996
997 foreach(part_lc, part_oids)
998 {
1000
1001 if (part_oid == relid)
1002 continue; /* ignore original table */
1003
1004 /*
1005 * We omit a RangeVar since it wouldn't be appropriate to
1006 * complain about failure to open one of these relations
1007 * later.
1008 */
1009 oldcontext = MemoryContextSwitchTo(vac_context);
1011 part_oid,
1012 vrel->va_cols));
1013 MemoryContextSwitchTo(oldcontext);
1014 }
1015 }
1016
1017 /*
1018 * Release lock again. This means that by the time we actually try to
1019 * process the table, it might be gone or renamed. In the former case
1020 * we'll silently ignore it; in the latter case we'll process it
1021 * anyway, but we must beware that the RangeVar doesn't necessarily
1022 * identify it anymore. This isn't ideal, perhaps, but there's little
1023 * practical alternative, since we're typically going to commit this
1024 * transaction and begin a new one between now and then. Moreover,
1025 * holding locks on multiple relations would create significant risk
1026 * of deadlock.
1027 */
1029 }
1030
1031 return vacrels;
1032}
#define OidIsValid(objectId)
Definition c.h:858
#define WARNING
Definition elog.h:37
#define elog(elevel,...)
Definition elog.h:228
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
List * lappend(List *list, void *datum)
Definition list.c:339
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:229
#define NoLock
Definition lockdefs.h:34
#define AccessShareLock
Definition lockdefs.h:36
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
Definition makefuncs.c:907
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition namespace.c:442
@ RVR_SKIP_LOCKED
Definition namespace.h:92
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
#define lfirst_oid(lc)
Definition pg_list.h:174
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
unsigned int Oid
Definition pg_list.h:54
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
bool vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple, uint32 options)
Definition vacuum.c:720

References AccessShareLock, AmAutoVacuumWorkerProcess, Assert, elog, ereport, errcode(), errmsg, ERROR, fb(), find_all_inheritors(), GETSTRUCT(), HeapTupleIsValid, lappend(), lfirst_oid, makeVacuumRelation(), MemoryContextSwitchTo(), NIL, NoLock, ObjectIdGetDatum(), OidIsValid, RangeVarGetRelidExtended(), ReleaseSysCache(), RVR_SKIP_LOCKED, SearchSysCache1(), UnlockRelationOid(), VACOPT_SKIP_LOCKED, VACOPT_VACUUM, vacuum_is_permitted_for_relation(), and WARNING.

Referenced by vacuum().

◆ get_all_vacuum_rels()

static List * get_all_vacuum_rels ( MemoryContext  vac_context,
int  options 
)
static

Definition at line 1039 of file vacuum.c.

1040{
1041 List *vacrels = NIL;
1043 TableScanDesc scan;
1044 HeapTuple tuple;
1045
1047
1049
1050 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1051 {
1053 MemoryContext oldcontext;
1054 Oid relid = classForm->oid;
1055
1056 /*
1057 * We include partitioned tables here; depending on which operation is
1058 * to be performed, caller will decide whether to process or ignore
1059 * them.
1060 */
1061 if (classForm->relkind != RELKIND_RELATION &&
1062 classForm->relkind != RELKIND_MATVIEW &&
1064 continue;
1065
1066 /* Skip temp relations belonging to other sessions */
1067 if (classForm->relpersistence == RELPERSISTENCE_TEMP &&
1068 !isTempOrTempToastNamespace(classForm->relnamespace))
1069 continue;
1070
1071 /* check permissions of relation */
1073 continue;
1074
1075 /*
1076 * Build VacuumRelation(s) specifying the table OIDs to be processed.
1077 * We omit a RangeVar since it wouldn't be appropriate to complain
1078 * about failure to open one of these relations later.
1079 */
1080 oldcontext = MemoryContextSwitchTo(vac_context);
1082 relid,
1083 NIL));
1084 MemoryContextSwitchTo(oldcontext);
1085 }
1086
1087 table_endscan(scan);
1089
1090 return vacrels;
1091}
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
bool isTempOrTempToastNamespace(Oid namespaceId)
Definition namespace.c:3745
@ ForwardScanDirection
Definition sdir.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061

References AccessShareLock, fb(), ForwardScanDirection, GETSTRUCT(), heap_getnext(), isTempOrTempToastNamespace(), lappend(), makeVacuumRelation(), MemoryContextSwitchTo(), NIL, table_beginscan_catalog(), table_close(), table_endscan(), table_open(), and vacuum_is_permitted_for_relation().

Referenced by vacuum().

◆ get_vacoptval_from_boolean()

static VacOptValue get_vacoptval_from_boolean ( DefElem def)
static

Definition at line 2652 of file vacuum.c.

2653{
2655}
@ VACOPTVALUE_ENABLED
Definition vacuum.h:204
@ VACOPTVALUE_DISABLED
Definition vacuum.h:203

References defGetBoolean(), VACOPTVALUE_DISABLED, and VACOPTVALUE_ENABLED.

Referenced by ExecVacuum().

◆ vac_bulkdel_one_index()

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

Definition at line 2663 of file vacuum.c.

2665{
2666 /* Do bulk deletion */
2667 istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2668 dead_items);
2669
2670 ereport(ivinfo->message_level,
2671 (errmsg("scanned index \"%s\" to remove %" PRId64 " row versions",
2673 dead_items_info->num_items)));
2674
2675 return istat;
2676}
IndexBulkDeleteResult * index_bulk_delete(IndexVacuumInfo *info, IndexBulkDeleteResult *istat, IndexBulkDeleteCallback callback, void *callback_state)
Definition indexam.c:773
#define RelationGetRelationName(relation)
Definition rel.h:550
int64 num_items
Definition vacuum.h:299
static bool vac_tid_reaped(ItemPointer itemptr, void *state)
Definition vacuum.c:2710

References ereport, errmsg, fb(), index_bulk_delete(), 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 2684 of file vacuum.c.

2685{
2686 istat = index_vacuum_cleanup(ivinfo, istat);
2687
2688 if (istat)
2689 ereport(ivinfo->message_level,
2690 (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2692 istat->num_index_tuples,
2693 istat->num_pages),
2694 errdetail("%.0f index row versions were removed.\n"
2695 "%u index pages were newly deleted.\n"
2696 "%u index pages are currently deleted, of which %u are currently reusable.",
2697 istat->tuples_removed,
2698 istat->pages_newly_deleted,
2699 istat->pages_deleted, istat->pages_free)));
2700
2701 return istat;
2702}
int errdetail(const char *fmt,...) pg_attribute_printf(1
IndexBulkDeleteResult * index_vacuum_cleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *istat)
Definition indexam.c:794
BlockNumber pages_deleted
Definition genam.h:90
BlockNumber pages_newly_deleted
Definition genam.h:89
BlockNumber pages_free
Definition genam.h:91
BlockNumber num_pages
Definition genam.h:85
double tuples_removed
Definition genam.h:88
double num_index_tuples
Definition genam.h:87

References ereport, errdetail(), errmsg, fb(), index_vacuum_cleanup(), 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 2417 of file vacuum.c.

2418{
2419 if (Irel == NULL)
2420 return;
2421
2422 while (nindexes--)
2423 {
2424 Relation ind = Irel[nindexes];
2425
2426 index_close(ind, lockmode);
2427 }
2428 pfree(Irel);
2429}
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
void pfree(void *pointer)
Definition mcxt.c:1619

References fb(), 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 1336 of file vacuum.c.

1340{
1341 BlockNumber old_rel_pages = relation->rd_rel->relpages;
1342 double old_rel_tuples = relation->rd_rel->reltuples;
1343 double old_density;
1344 double unscanned_pages;
1345 double total_tuples;
1346
1347 /* If we did scan the whole table, just use the count as-is */
1348 if (scanned_pages >= total_pages)
1349 return scanned_tuples;
1350
1351 /*
1352 * When successive VACUUM commands scan the same few pages again and
1353 * again, without anything from the table really changing, there is a risk
1354 * that our beliefs about tuple density will gradually become distorted.
1355 * This might be caused by vacuumlazy.c implementation details, such as
1356 * its tendency to always scan the last heap page. Handle that here.
1357 *
1358 * If the relation is _exactly_ the same size according to the existing
1359 * pg_class entry, and only a few of its pages (less than 2%) were
1360 * scanned, keep the existing value of reltuples. Also keep the existing
1361 * value when only a subset of rel's pages <= a single page were scanned.
1362 *
1363 * (Note: we might be returning -1 here.)
1364 */
1365 if (old_rel_pages == total_pages &&
1366 scanned_pages < (double) total_pages * 0.02)
1367 return old_rel_tuples;
1368 if (scanned_pages <= 1)
1369 return old_rel_tuples;
1370
1371 /*
1372 * If old density is unknown, we can't do much except scale up
1373 * scanned_tuples to match total_pages.
1374 */
1375 if (old_rel_tuples < 0 || old_rel_pages == 0)
1376 return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1377
1378 /*
1379 * Okay, we've covered the corner cases. The normal calculation is to
1380 * convert the old measurement to a density (tuples per page), then
1381 * estimate the number of tuples in the unscanned pages using that figure,
1382 * and finally add on the number of tuples in the scanned pages.
1383 */
1385 unscanned_pages = (double) total_pages - (double) scanned_pages;
1386 total_tuples = old_density * unscanned_pages + scanned_tuples;
1387 return floor(total_tuples + 0.5);
1388}
uint32 BlockNumber
Definition block.h:31
Form_pg_class rd_rel
Definition rel.h:111

References fb(), and 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 2374 of file vacuum.c.

2376{
2379 int i;
2380
2381 Assert(lockmode != NoLock);
2382
2384
2385 /* allocate enough memory for all indexes */
2387
2388 if (i > 0)
2389 *Irel = (Relation *) palloc(i * sizeof(Relation));
2390 else
2391 *Irel = NULL;
2392
2393 /* collect just the ready indexes */
2394 i = 0;
2395 foreach(indexoidscan, indexoidlist)
2396 {
2397 Oid indexoid = lfirst_oid(indexoidscan);
2399
2400 indrel = index_open(indexoid, lockmode);
2401 if (indrel->rd_index->indisready)
2402 (*Irel)[i++] = indrel;
2403 else
2404 index_close(indrel, lockmode);
2405 }
2406
2407 *nindexes = i;
2408
2410}
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
int i
Definition isn.c:77
void list_free(List *list)
Definition list.c:1546
void * palloc(Size size)
Definition mcxt.c:1390
static int list_length(const List *l)
Definition pg_list.h:152
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4837

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

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

◆ vac_tid_reaped()

static bool vac_tid_reaped ( ItemPointer  itemptr,
void state 
)
static

Definition at line 2710 of file vacuum.c.

2711{
2712 TidStore *dead_items = (TidStore *) state;
2713
2714 return TidStoreIsMember(dead_items, itemptr);
2715}
bool TidStoreIsMember(TidStore *ts, const ItemPointerData *tid)
Definition tidstore.c:421

References TidStoreIsMember().

Referenced by vac_bulkdel_one_index().

◆ vac_truncate_clog()

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

Definition at line 1835 of file vacuum.c.

1839{
1841 Relation relation;
1842 TableScanDesc scan;
1843 HeapTuple tuple;
1846 bool bogus = false;
1847 bool frozenAlreadyWrapped = false;
1848
1849 /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
1851
1852 /* init oldest datoids to sync with my frozenXID/minMulti values */
1855
1856 /*
1857 * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1858 *
1859 * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1860 * the values could change while we look at them. Fetch each one just
1861 * once to ensure sane behavior of the comparison logic. (Here, as in
1862 * many other places, we assume that fetching or updating an XID in shared
1863 * storage is atomic.)
1864 *
1865 * Note: we need not worry about a race condition with new entries being
1866 * inserted by CREATE DATABASE. Any such entry will have a copy of some
1867 * existing DB's datfrozenxid, and that source DB cannot be ours because
1868 * of the interlock against copying a DB containing an active backend.
1869 * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1870 * concurrently modify the datfrozenxid's of different databases, the
1871 * worst possible outcome is that pg_xact is not truncated as aggressively
1872 * as it could be.
1873 */
1875
1876 scan = table_beginscan_catalog(relation, 0, NULL);
1877
1878 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1879 {
1881 volatile TransactionId *datfrozenxid_p = &dbform->datfrozenxid;
1882 volatile TransactionId *datminmxid_p = &dbform->datminmxid;
1885
1888
1889 /*
1890 * If database is in the process of getting dropped, or has been
1891 * interrupted while doing so, no connections to it are possible
1892 * anymore. Therefore we don't need to take it into account here.
1893 * Which is good, because it can't be processed by autovacuum either.
1894 */
1896 {
1897 elog(DEBUG2,
1898 "skipping invalid database \"%s\" while computing relfrozenxid",
1899 NameStr(dbform->datname));
1900 continue;
1901 }
1902
1903 /*
1904 * If things are working properly, no database should have a
1905 * datfrozenxid or datminmxid that is "in the future". However, such
1906 * cases have been known to arise due to bugs in pg_upgrade. If we
1907 * see any entries that are "in the future", chicken out and don't do
1908 * anything. This ensures we won't truncate clog before those
1909 * databases have been scanned and cleaned up. (We will issue the
1910 * "already wrapped" warning if appropriate, though.)
1911 */
1914 bogus = true;
1915
1917 frozenAlreadyWrapped = true;
1919 {
1921 oldestxid_datoid = dbform->oid;
1922 }
1923
1925 {
1927 minmulti_datoid = dbform->oid;
1928 }
1929 }
1930
1931 table_endscan(scan);
1932
1933 table_close(relation, AccessShareLock);
1934
1935 /*
1936 * Do not truncate CLOG if we seem to have suffered wraparound already;
1937 * the computed minimum XID might be bogus. This case should now be
1938 * impossible due to the defenses in GetNewTransactionId, but we keep the
1939 * test anyway.
1940 */
1942 {
1944 (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1945 errdetail("You might have already suffered transaction-wraparound data loss.")));
1947 return;
1948 }
1949
1950 /* chicken out if data is bogus in any other way */
1951 if (bogus)
1952 {
1954 return;
1955 }
1956
1957 /*
1958 * Freeze any old transaction IDs in the async notification queue before
1959 * CLOG truncation.
1960 */
1962
1963 /*
1964 * Advance the oldest value for commit timestamps before truncating, so
1965 * that if a user requests a timestamp for a transaction we're truncating
1966 * away right after this point, they get NULL instead of an ugly "file not
1967 * found" error from slru.c. This doesn't matter for xact/multixact
1968 * because they are not subject to arbitrary lookups from users.
1969 */
1971
1972 /*
1973 * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1974 */
1978
1979 /*
1980 * Update the wrap limit for GetNewTransactionId and creation of new
1981 * MultiXactIds. Note: these functions will also signal the postmaster
1982 * for an(other) autovac cycle if needed. XXX should we avoid possibly
1983 * signaling twice?
1984 */
1987
1989}
void AsyncNotifyFreezeXids(TransactionId newFrozenXid)
Definition async.c:2950
#define NameStr(name)
Definition c.h:835
uint32 TransactionId
Definition c.h:736
void TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
Definition clog.c:986
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition commit_ts.c:919
void TruncateCommitTs(TransactionId oldestXact)
Definition commit_ts.c:866
bool database_is_invalid_form(Form_pg_database datform)
#define DEBUG2
Definition elog.h:30
Oid MyDatabaseId
Definition globals.c:96
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2865
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition multixact.c:2678
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid)
Definition multixact.c:2085
#define MultiXactIdIsValid(multi)
Definition multixact.h:29
TransactionId datfrozenxid
Definition pg_database.h:64
TransactionId datminmxid
Definition pg_database.h:67
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
static TransactionId ReadNextTransactionId(void)
Definition transam.h:375
#define TransactionIdIsNormal(xid)
Definition transam.h:42
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition varsup.c:367

References AccessShareLock, AdvanceOldestCommitTsXid(), Assert, AsyncNotifyFreezeXids(), database_is_invalid_form(), datfrozenxid, datminmxid, DEBUG2, elog, ereport, errdetail(), errmsg, fb(), Form_pg_database, ForwardScanDirection, GETSTRUCT(), heap_getnext(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, NameStr, ReadNextTransactionId(), SetMultiXactIdLimit(), SetTransactionIdLimit(), table_beginscan_catalog(), table_close(), table_endscan(), table_open(), TransactionIdIsNormal, TransactionIdPrecedes(), TruncateCLOG(), TruncateCommitTs(), TruncateMultiXact(), and WARNING.

Referenced by vac_update_datfrozenxid().

◆ vac_update_datfrozenxid()

void vac_update_datfrozenxid ( void  )

Definition at line 1614 of file vacuum.c.

1615{
1616 HeapTuple tuple;
1618 Relation relation;
1619 SysScanDesc scan;
1625 bool bogus = false;
1626 bool dirty = false;
1627 ScanKeyData key[1];
1628 void *inplace_state;
1629
1630 /*
1631 * Restrict this task to one backend per database. This avoids race
1632 * conditions that would move datfrozenxid or datminmxid backward. It
1633 * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1634 * datfrozenxid passed to an earlier vac_truncate_clog() call.
1635 */
1637
1638 /*
1639 * Initialize the "min" calculation with
1640 * GetOldestNonRemovableTransactionId(), which is a reasonable
1641 * approximation to the minimum relfrozenxid for not-yet-committed
1642 * pg_class entries for new tables; see AddNewRelationTuple(). So we
1643 * cannot produce a wrong minimum by starting with this.
1644 */
1646
1647 /*
1648 * Similarly, initialize the MultiXact "min" with the value that would be
1649 * used on pg_class for new tables. See AddNewRelationTuple().
1650 */
1652
1653 /*
1654 * Identify the latest relfrozenxid and relminmxid values that we could
1655 * validly see during the scan. These are conservative values, but it's
1656 * not really worth trying to be more exact.
1657 */
1660
1661 /*
1662 * We must seqscan pg_class to find the minimum Xid, because there is no
1663 * index that can help us here.
1664 *
1665 * See vac_truncate_clog() for the race condition to prevent.
1666 */
1668
1669 scan = systable_beginscan(relation, InvalidOid, false,
1670 NULL, 0, NULL);
1671
1672 while ((classTup = systable_getnext(scan)) != NULL)
1673 {
1675 volatile TransactionId *relfrozenxid_p = &classForm->relfrozenxid;
1676 volatile TransactionId *relminmxid_p = &classForm->relminmxid;
1677 TransactionId relfrozenxid = *relfrozenxid_p;
1678 TransactionId relminmxid = *relminmxid_p;
1679
1680 /*
1681 * Only consider relations able to hold unfrozen XIDs (anything else
1682 * should have InvalidTransactionId in relfrozenxid anyway).
1683 */
1684 if (classForm->relkind != RELKIND_RELATION &&
1685 classForm->relkind != RELKIND_MATVIEW &&
1686 classForm->relkind != RELKIND_TOASTVALUE)
1687 {
1688 Assert(!TransactionIdIsValid(relfrozenxid));
1689 Assert(!MultiXactIdIsValid(relminmxid));
1690 continue;
1691 }
1692
1693 /*
1694 * Some table AMs might not need per-relation xid / multixid horizons.
1695 * It therefore seems reasonable to allow relfrozenxid and relminmxid
1696 * to not be set (i.e. set to their respective Invalid*Id)
1697 * independently. Thus validate and compute horizon for each only if
1698 * set.
1699 *
1700 * If things are working properly, no relation should have a
1701 * relfrozenxid or relminmxid that is "in the future". However, such
1702 * cases have been known to arise due to bugs in pg_upgrade. If we
1703 * see any entries that are "in the future", chicken out and don't do
1704 * anything. This ensures we won't truncate clog & multixact SLRUs
1705 * before those relations have been scanned and cleaned up.
1706 */
1707
1708 if (TransactionIdIsValid(relfrozenxid))
1709 {
1710 Assert(TransactionIdIsNormal(relfrozenxid));
1711
1712 /* check for values in the future */
1713 if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1714 {
1715 bogus = true;
1716 break;
1717 }
1718
1719 /* determine new horizon */
1720 if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1721 newFrozenXid = relfrozenxid;
1722 }
1723
1724 if (MultiXactIdIsValid(relminmxid))
1725 {
1726 /* check for values in the future */
1727 if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1728 {
1729 bogus = true;
1730 break;
1731 }
1732
1733 /* determine new horizon */
1734 if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1735 newMinMulti = relminmxid;
1736 }
1737 }
1738
1739 /* we're done with pg_class */
1740 systable_endscan(scan);
1741 table_close(relation, AccessShareLock);
1742
1743 /* chicken out if bogus data found */
1744 if (bogus)
1745 return;
1746
1749
1750 /* Now fetch the pg_database tuple we need to update. */
1752
1753 /*
1754 * Fetch a copy of the tuple to scribble on. We could check the syscache
1755 * tuple first. If that concluded !dirty, we'd avoid waiting on
1756 * concurrent heap_update() and would avoid exclusive-locking the buffer.
1757 * For now, don't optimize that.
1758 */
1759 ScanKeyInit(&key[0],
1763
1765 NULL, 1, key, &tuple, &inplace_state);
1766
1767 if (!HeapTupleIsValid(tuple))
1768 elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1769
1771
1772 /*
1773 * As in vac_update_relstats(), we ordinarily don't want to let
1774 * datfrozenxid go backward; but if it's "in the future" then it must be
1775 * corrupt and it seems best to overwrite it.
1776 */
1777 if (dbform->datfrozenxid != newFrozenXid &&
1778 (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1780 {
1781 dbform->datfrozenxid = newFrozenXid;
1782 dirty = true;
1783 }
1784 else
1785 newFrozenXid = dbform->datfrozenxid;
1786
1787 /* Ditto for datminmxid */
1788 if (dbform->datminmxid != newMinMulti &&
1789 (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1791 {
1792 dbform->datminmxid = newMinMulti;
1793 dirty = true;
1794 }
1795 else
1796 newMinMulti = dbform->datminmxid;
1797
1798 if (dirty)
1800 else
1802
1803 heap_freetuple(tuple);
1804 table_close(relation, RowExclusiveLock);
1805
1806 /*
1807 * If we were able to advance datfrozenxid or datminmxid, see if we can
1808 * truncate pg_xact and/or pg_multixact. Also do it if the shared
1809 * XID-wrap-limit info is stale, since this action will update that too.
1810 */
1811 if (dirty || ForceTransactionIdLimitUpdate())
1814}
TransactionId MultiXactId
Definition c.h:746
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:612
void systable_inplace_update_cancel(void *state)
Definition genam.c:913
void systable_inplace_update_begin(Relation relation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, const ScanKeyData *key, HeapTuple *oldtupcopy, void **state)
Definition genam.c:818
void systable_inplace_update_finish(void *state, HeapTuple tuple)
Definition genam.c:894
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:523
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
void LockDatabaseFrozenIds(LOCKMODE lockmode)
Definition lmgr.c:491
#define ExclusiveLock
Definition lockdefs.h:42
#define RowExclusiveLock
Definition lockdefs.h:38
MultiXactId GetOldestMultiXactId(void)
Definition multixact.c:2378
MultiXactId ReadNextMultiXactId(void)
Definition multixact.c:679
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition procarray.c:1944
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
#define BTEqualStrategyNumber
Definition stratnum.h:31
#define TransactionIdIsValid(xid)
Definition transam.h:41
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition vacuum.c:1835
bool ForceTransactionIdLimitUpdate(void)
Definition varsup.c:516

References AccessShareLock, Assert, BTEqualStrategyNumber, elog, ERROR, ExclusiveLock, fb(), ForceTransactionIdLimitUpdate(), Form_pg_database, GetOldestMultiXactId(), GetOldestNonRemovableTransactionId(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InvalidOid, LockDatabaseFrozenIds(), MultiXactIdIsValid, MultiXactIdPrecedes(), MyDatabaseId, ObjectIdGetDatum(), ReadNextMultiXactId(), ReadNextTransactionId(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), 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,
BlockNumber  num_all_frozen_pages,
bool  hasindex,
TransactionId  frozenxid,
MultiXactId  minmulti,
bool frozenxid_updated,
bool minmulti_updated,
bool  in_outer_xact 
)

Definition at line 1432 of file vacuum.c.

1440{
1441 Oid relid = RelationGetRelid(relation);
1442 Relation rd;
1443 ScanKeyData key[1];
1445 void *inplace_state;
1447 bool dirty,
1448 futurexid,
1449 futuremxid;
1452
1454
1455 /* Fetch a copy of the tuple to scribble on */
1456 ScanKeyInit(&key[0],
1459 ObjectIdGetDatum(relid));
1461 NULL, 1, key, &ctup, &inplace_state);
1462 if (!HeapTupleIsValid(ctup))
1463 elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1464 relid);
1466
1467 /* Apply statistical updates, if any, to copied tuple */
1468
1469 dirty = false;
1470 if (pgcform->relpages != (int32) num_pages)
1471 {
1472 pgcform->relpages = (int32) num_pages;
1473 dirty = true;
1474 }
1475 if (pgcform->reltuples != (float4) num_tuples)
1476 {
1477 pgcform->reltuples = (float4) num_tuples;
1478 dirty = true;
1479 }
1480 if (pgcform->relallvisible != (int32) num_all_visible_pages)
1481 {
1482 pgcform->relallvisible = (int32) num_all_visible_pages;
1483 dirty = true;
1484 }
1485 if (pgcform->relallfrozen != (int32) num_all_frozen_pages)
1486 {
1487 pgcform->relallfrozen = (int32) num_all_frozen_pages;
1488 dirty = true;
1489 }
1490
1491 /* Apply DDL updates, but not inside an outer transaction (see above) */
1492
1493 if (!in_outer_xact)
1494 {
1495 /*
1496 * If we didn't find any indexes, reset relhasindex.
1497 */
1498 if (pgcform->relhasindex && !hasindex)
1499 {
1500 pgcform->relhasindex = false;
1501 dirty = true;
1502 }
1503
1504 /* We also clear relhasrules and relhastriggers if needed */
1505 if (pgcform->relhasrules && relation->rd_rules == NULL)
1506 {
1507 pgcform->relhasrules = false;
1508 dirty = true;
1509 }
1510 if (pgcform->relhastriggers && relation->trigdesc == NULL)
1511 {
1512 pgcform->relhastriggers = false;
1513 dirty = true;
1514 }
1515 }
1516
1517 /*
1518 * Update relfrozenxid, unless caller passed InvalidTransactionId
1519 * indicating it has no new data.
1520 *
1521 * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1522 * stored relfrozenxid is "in the future" then it seems best to assume
1523 * it's corrupt, and overwrite with the oldest remaining XID in the table.
1524 * This should match vac_update_datfrozenxid() concerning what we consider
1525 * to be "in the future".
1526 */
1527 oldfrozenxid = pgcform->relfrozenxid;
1528 futurexid = false;
1530 *frozenxid_updated = false;
1531 if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1532 {
1533 bool update = false;
1534
1535 if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1536 update = true;
1538 futurexid = update = true;
1539
1540 if (update)
1541 {
1542 pgcform->relfrozenxid = frozenxid;
1543 dirty = true;
1545 *frozenxid_updated = true;
1546 }
1547 }
1548
1549 /* Similarly for relminmxid */
1550 oldminmulti = pgcform->relminmxid;
1551 futuremxid = false;
1552 if (minmulti_updated)
1553 *minmulti_updated = false;
1555 {
1556 bool update = false;
1557
1559 update = true;
1561 futuremxid = update = true;
1562
1563 if (update)
1564 {
1565 pgcform->relminmxid = minmulti;
1566 dirty = true;
1567 if (minmulti_updated)
1568 *minmulti_updated = true;
1569 }
1570 }
1571
1572 /* If anything changed, write out the tuple. */
1573 if (dirty)
1575 else
1577
1579
1580 if (futurexid)
1583 errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1584 oldfrozenxid, frozenxid,
1585 RelationGetRelationName(relation))));
1586 if (futuremxid)
1589 errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1591 RelationGetRelationName(relation))));
1592}
int32_t int32
Definition c.h:620
float float4
Definition c.h:713
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ERRCODE_DATA_CORRUPTED
#define RelationGetRelid(relation)
Definition rel.h:516
TriggerDesc * trigdesc
Definition rel.h:117
RuleLock * rd_rules
Definition rel.h:115

References BTEqualStrategyNumber, elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, fb(), GETSTRUCT(), HeapTupleIsValid, MultiXactIdIsValid, MultiXactIdPrecedes(), ObjectIdGetDatum(), RelationData::rd_rules, ReadNextMultiXactId(), ReadNextTransactionId(), RelationGetRelationName, RelationGetRelid, RowExclusiveLock, ScanKeyInit(), systable_inplace_update_begin(), systable_inplace_update_cancel(), systable_inplace_update_finish(), 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,
const VacuumParams params,
BufferAccessStrategy  bstrategy,
MemoryContext  vac_context,
bool  isTopLevel 
)

Definition at line 494 of file vacuum.c.

496{
497 static bool in_vacuum = false;
498
499 const char *stmttype;
500 volatile bool in_outer_xact,
502
503 stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
504
505 /*
506 * We cannot run VACUUM inside a user transaction block; if we were inside
507 * a transaction, then our commit- and start-transaction-command calls
508 * would not have the intended effect! There are numerous other subtle
509 * dependencies on this, too.
510 *
511 * ANALYZE (without VACUUM) can run either way.
512 */
513 if (params->options & VACOPT_VACUUM)
514 {
516 in_outer_xact = false;
517 }
518 else
520
521 /*
522 * Check for and disallow recursive calls. This could happen when VACUUM
523 * FULL or ANALYZE calls a hostile index expression that itself calls
524 * ANALYZE.
525 */
526 if (in_vacuum)
529 errmsg("%s cannot be executed from VACUUM or ANALYZE",
530 stmttype)));
531
532 /*
533 * Build list of relation(s) to process, putting any new data in
534 * vac_context for safekeeping.
535 */
537 {
538 /* We don't process any tables in this case */
539 Assert(relations == NIL);
540 }
541 else if (relations != NIL)
542 {
543 List *newrels = NIL;
544 ListCell *lc;
545
546 foreach(lc, relations)
547 {
549 List *sublist;
551
556 }
557 relations = newrels;
558 }
559 else
560 relations = get_all_vacuum_rels(vac_context, params->options);
561
562 /*
563 * Decide whether we need to start/commit our own transactions.
564 *
565 * For VACUUM (with or without ANALYZE): always do so, so that we can
566 * release locks as soon as possible. (We could possibly use the outer
567 * transaction for a one-table VACUUM, but handling TOAST tables would be
568 * problematic.)
569 *
570 * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
571 * start/commit our own transactions. Also, there's no need to do so if
572 * only processing one relation. For multiple relations when not within a
573 * transaction block, and also in an autovacuum worker, use own
574 * transactions so we can release locks sooner.
575 */
576 if (params->options & VACOPT_VACUUM)
577 use_own_xacts = true;
578 else
579 {
580 Assert(params->options & VACOPT_ANALYZE);
582 use_own_xacts = true;
583 else if (in_outer_xact)
584 use_own_xacts = false;
585 else if (list_length(relations) > 1)
586 use_own_xacts = true;
587 else
588 use_own_xacts = false;
589 }
590
591 /*
592 * vacuum_rel expects to be entered with no transaction active; it will
593 * start and commit its own transaction. But we are called by an SQL
594 * command, and so we are executing inside a transaction already. We
595 * commit the transaction started in PostgresMain() here, and start
596 * another one before exiting to match the commit waiting for us back in
597 * PostgresMain().
598 */
599 if (use_own_xacts)
600 {
602
603 /* ActiveSnapshot is not set by autovacuum */
604 if (ActiveSnapshotSet())
606
607 /* matches the StartTransaction in PostgresMain() */
609 }
610
611 /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
612 PG_TRY();
613 {
614 ListCell *cur;
615
616 in_vacuum = true;
617 VacuumFailsafeActive = false;
623
624 /*
625 * Loop to process each selected relation.
626 */
627 foreach(cur, relations)
628 {
630
631 if (params->options & VACOPT_VACUUM)
632 {
633 if (!vacuum_rel(vrel->oid, vrel->relation, *params, bstrategy,
634 isTopLevel))
635 continue;
636 }
637
638 if (params->options & VACOPT_ANALYZE)
639 {
640 /*
641 * If using separate xacts, start one for analyze. Otherwise,
642 * we can use the outer transaction.
643 */
644 if (use_own_xacts)
645 {
647 /* functions in indexes may want a snapshot set */
649 }
650
651 analyze_rel(vrel->oid, vrel->relation, params,
652 vrel->va_cols, in_outer_xact, bstrategy);
653
654 if (use_own_xacts)
655 {
657 /* standard_ProcessUtility() does CCI if !use_own_xacts */
660 }
661 else
662 {
663 /*
664 * If we're not using separate xacts, better separate the
665 * ANALYZE actions with CCIs. This avoids trouble if user
666 * says "ANALYZE t, t".
667 */
669 }
670 }
671
672 /*
673 * Ensure VacuumFailsafeActive has been reset before vacuuming the
674 * next relation.
675 */
676 VacuumFailsafeActive = false;
677 }
678 }
679 PG_FINALLY();
680 {
681 in_vacuum = false;
682 VacuumCostActive = false;
683 VacuumFailsafeActive = false;
685 }
686 PG_END_TRY();
687
688 /*
689 * Finish up processing.
690 */
691 if (use_own_xacts)
692 {
693 /* here, we are not in a transaction */
694
695 /*
696 * This matches the CommitTransaction waiting for us in
697 * PostgresMain().
698 */
700 }
701
702 if ((params->options & VACOPT_VACUUM) &&
704 {
705 /*
706 * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
707 */
709 }
710
711}
void VacuumUpdateCosts(void)
void analyze_rel(Oid relid, RangeVar *relation, const VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition analyze.c:110
struct cursor * cur
Definition ecpg.c:29
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_FINALLY(...)
Definition elog.h:391
bool VacuumCostActive
Definition globals.c:161
List * list_concat(List *list1, const List *list2)
Definition list.c:561
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
bool ActiveSnapshotSet(void)
Definition snapmgr.c:812
void PopActiveSnapshot(void)
Definition snapmgr.c:775
static List * expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context, int options)
Definition vacuum.c:884
static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams params, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition vacuum.c:2012
static List * get_all_vacuum_rels(MemoryContext vac_context, int options)
Definition vacuum.c:1039
void vac_update_datfrozenxid(void)
Definition vacuum.c:1614
bool VacuumFailsafeActive
Definition vacuum.c:111
bool IsInTransactionBlock(bool isTopLevel)
Definition xact.c:3820
void CommandCounterIncrement(void)
Definition xact.c:1130
void PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
Definition xact.c:3698
void StartTransactionCommand(void)
Definition xact.c:3109
void CommitTransactionCommand(void)
Definition xact.c:3207

References ActiveSnapshotSet(), AmAutoVacuumWorkerProcess, analyze_rel(), Assert, CommandCounterIncrement(), CommitTransactionCommand(), cur, ereport, errcode(), errmsg, ERROR, expand_vacuum_rel(), fb(), get_all_vacuum_rels(), GetTransactionSnapshot(), IsInTransactionBlock(), lfirst_node, list_concat(), list_length(), MemoryContextSwitchTo(), NIL, VacuumParams::options, PG_END_TRY, PG_FINALLY, PG_TRY, PopActiveSnapshot(), PreventInTransactionBlock(), PushActiveSnapshot(), StartTransactionCommand(), vac_update_datfrozenxid(), VACOPT_ANALYZE, VACOPT_ONLY_DATABASE_STATS, VACOPT_SKIP_DATABASE_STATS, VACOPT_VACUUM, vacuum_rel(), VacuumActiveNWorkers, VacuumCostActive, VacuumCostBalance, VacuumCostBalanceLocal, VacuumFailsafeActive, 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 ( bool  is_analyze)

Definition at line 2438 of file vacuum.c.

2439{
2440 double msec = 0;
2441
2442 /* Always check for interrupts */
2444
2445 if (InterruptPending)
2446 return;
2447
2448 if (IsParallelWorker())
2449 {
2450 /*
2451 * Update cost-based vacuum delay parameters for a parallel autovacuum
2452 * worker if any changes are detected. It might enable cost-based
2453 * delay so it needs to be called before VacuumCostActive check.
2454 */
2456 }
2457
2459 return;
2460
2461 /*
2462 * Autovacuum workers should reload the configuration file if requested.
2463 * This allows changes to [autovacuum_]vacuum_cost_limit and
2464 * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2465 * vacuumed or analyzed.
2466 */
2468 {
2469 ConfigReloadPending = false;
2472
2473 /*
2474 * Propagate cost-based vacuum delay parameters to shared memory if
2475 * any of them have changed during the config reload.
2476 */
2478 }
2479
2480 /*
2481 * If we disabled cost-based delays after reloading the config file,
2482 * return.
2483 */
2484 if (!VacuumCostActive)
2485 return;
2486
2487 /*
2488 * For parallel vacuum, the delay is computed based on the shared cost
2489 * balance. See compute_parallel_delay.
2490 */
2495
2496 /* Nap if appropriate */
2497 if (msec > 0)
2498 {
2500
2501 if (msec > vacuum_cost_delay * 4)
2502 msec = vacuum_cost_delay * 4;
2503
2506
2508 pg_usleep(msec * 1000);
2510
2512 {
2515
2519
2520 /*
2521 * For parallel workers, we only report the delay time every once
2522 * in a while to avoid overloading the leader with messages and
2523 * interrupts.
2524 */
2525 if (IsParallelWorker())
2526 {
2529
2531
2532 /* Accumulate the delay time */
2534
2535 /* Calculate interval since last report */
2538
2539 /* If we haven't reported in a while, do so now */
2542 {
2545
2546 /* Reset variables */
2549 }
2550 }
2551 else if (is_analyze)
2554 else
2557 }
2558
2559 /*
2560 * We don't want to ignore postmaster death during very long vacuums
2561 * with vacuum_cost_delay configured. We can't use the usual
2562 * WaitLatch() approach here because we want microsecond-based sleep
2563 * durations above.
2564 */
2566 exit(1);
2567
2569
2570 /*
2571 * Balance and update limit values for autovacuum workers. We must do
2572 * this periodically, as the number of workers across which we are
2573 * balancing the limit may have changed.
2574 *
2575 * TODO: There may be better criteria for determining when to do this
2576 * besides "check after napping".
2577 */
2579
2580 /* Might have gotten an interrupt while sleeping */
2582 }
2583}
void AutoVacuumUpdateCostLimit(void)
void pgstat_progress_parallel_incr_param(int index, int64 incr)
void pgstat_progress_incr_param(int index, int64 incr)
volatile sig_atomic_t InterruptPending
Definition globals.c:32
bool IsUnderPostmaster
Definition globals.c:122
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
#define IsParallelWorker()
Definition parallel.h:62
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:434
#define INSTR_TIME_GET_NANOSEC(t)
Definition instr_time.h:453
#define INSTR_TIME_SET_ZERO(t)
Definition instr_time.h:429
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition instr_time.h:447
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
#define PostmasterIsAlive()
Definition pmsignal.h:106
#define PROGRESS_VACUUM_DELAY_TIME
Definition progress.h:31
#define PROGRESS_ANALYZE_DELAY_TIME
Definition progress.h:62
void pg_usleep(long microsec)
Definition signal.c:53
bool track_cost_delay_timing
Definition vacuum.c:83
static double compute_parallel_delay(void)
Definition vacuum.c:2608
#define PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS
Definition vacuum.c:71
int64 parallel_vacuum_worker_delay_ns
Definition vacuum.c:96
void parallel_vacuum_update_shared_delay_params(void)
void parallel_vacuum_propagate_shared_delay_params(void)
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:67
static void pgstat_report_wait_end(void)
Definition wait_event.h:83

References AmAutoVacuumWorkerProcess, Assert, AutoVacuumUpdateCostLimit(), CHECK_FOR_INTERRUPTS, compute_parallel_delay(), ConfigReloadPending, fb(), INSTR_TIME_ACCUM_DIFF, INSTR_TIME_GET_NANOSEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, InterruptPending, IsParallelWorker, IsUnderPostmaster, PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS, parallel_vacuum_propagate_shared_delay_params(), parallel_vacuum_update_shared_delay_params(), parallel_vacuum_worker_delay_ns, pg_usleep(), PGC_SIGHUP, pgstat_progress_incr_param(), pgstat_progress_parallel_incr_param(), pgstat_report_wait_end(), pgstat_report_wait_start(), PostmasterIsAlive, ProcessConfigFile(), PROGRESS_ANALYZE_DELAY_TIME, PROGRESS_VACUUM_DELAY_TIME, track_cost_delay_timing, vacuum_cost_delay, vacuum_cost_limit, VacuumCostActive, VacuumCostBalance, VacuumSharedCostBalance, and VacuumUpdateCosts().

Referenced by acquire_sample_rows(), blbulkdelete(), blvacuumcleanup(), btvacuumpage(), btvacuumscan(), 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(), gistvacuumscan(), hashbucketcleanup(), lazy_scan_heap(), lazy_vacuum_heap_rel(), ProcessSingleRelationFork(), spgprocesspending(), and spgvacuumscan().

◆ vacuum_get_cutoffs()

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

Definition at line 1106 of file vacuum.c.

1108{
1109 int freeze_min_age,
1110 multixact_freeze_min_age,
1111 freeze_table_age,
1112 multixact_freeze_table_age,
1120
1121 /* Use mutable copies of freeze age parameters */
1122 freeze_min_age = params->freeze_min_age;
1123 multixact_freeze_min_age = params->multixact_freeze_min_age;
1124 freeze_table_age = params->freeze_table_age;
1125 multixact_freeze_table_age = params->multixact_freeze_table_age;
1126
1127 /* Set pg_class fields in cutoffs */
1128 cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1129 cutoffs->relminmxid = rel->rd_rel->relminmxid;
1130
1131 /*
1132 * Acquire OldestXmin.
1133 *
1134 * We can always ignore processes running lazy vacuum. This is because we
1135 * use these values only for deciding which tuples we must keep in the
1136 * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1137 * XID assigned), it's safe to ignore it. In theory it could be
1138 * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1139 * that only one vacuum process can be working on a particular table at
1140 * any time, and that each vacuum is always an independent transaction.
1141 */
1143
1145
1146 /* Acquire OldestMxact */
1147 cutoffs->OldestMxact = GetOldestMultiXactId();
1149
1150 /* Acquire next XID/next MXID values used to apply age-based settings */
1153
1154 /*
1155 * Also compute the multixact age for which freezing is urgent. This is
1156 * normally autovacuum_multixact_freeze_max_age, but may be less if
1157 * multixact members are bloated.
1158 */
1160
1161 /*
1162 * Almost ready to set freeze output parameters; check if OldestXmin or
1163 * OldestMxact are held back to an unsafe degree before we start on that
1164 */
1173 (errmsg("cutoff for removing and freezing tuples is far in the past"),
1174 errhint("Close open transactions soon to avoid wraparound problems.\n"
1175 "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1178 (errmsg("cutoff for freezing multixacts is far in the past"),
1179 errhint("Close open transactions soon to avoid wraparound problems.\n"
1180 "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1181
1182 /*
1183 * Determine the minimum freeze age to use: as specified by the caller, or
1184 * vacuum_freeze_min_age, but in any case not more than half
1185 * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1186 * wraparound won't occur too frequently.
1187 */
1188 if (freeze_min_age < 0)
1189 freeze_min_age = vacuum_freeze_min_age;
1190 freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1191 Assert(freeze_min_age >= 0);
1192
1193 /* Compute FreezeLimit, being careful to generate a normal XID */
1194 cutoffs->FreezeLimit = nextXID - freeze_min_age;
1195 if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1197 /* FreezeLimit must always be <= OldestXmin */
1198 if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1199 cutoffs->FreezeLimit = cutoffs->OldestXmin;
1200
1201 /*
1202 * Determine the minimum multixact freeze age to use: as specified by
1203 * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1204 * than half effective_multixact_freeze_max_age, so that autovacuums to
1205 * prevent MultiXact wraparound won't occur too frequently.
1206 */
1207 if (multixact_freeze_min_age < 0)
1208 multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1209 multixact_freeze_min_age = Min(multixact_freeze_min_age,
1211 Assert(multixact_freeze_min_age >= 0);
1212
1213 /* Compute MultiXactCutoff, being careful to generate a valid value */
1214 cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1215 if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1217 /* MultiXactCutoff must always be <= OldestMxact */
1218 if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1219 cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1220
1221 /*
1222 * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1223 *
1224 * Determine the table freeze age to use: as specified by the caller, or
1225 * the value of the vacuum_freeze_table_age GUC, but in any case not more
1226 * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1227 * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1228 * anti-wraparound autovacuum is launched.
1229 */
1230 if (freeze_table_age < 0)
1231 freeze_table_age = vacuum_freeze_table_age;
1232 freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1233 Assert(freeze_table_age >= 0);
1234 aggressiveXIDCutoff = nextXID - freeze_table_age;
1239 return true;
1240
1241 /*
1242 * Similar to the above, determine the table freeze age to use for
1243 * multixacts: as specified by the caller, or the value of the
1244 * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1245 * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1246 * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1247 * multixacts before anti-wraparound autovacuum is launched.
1248 */
1249 if (multixact_freeze_table_age < 0)
1250 multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1251 multixact_freeze_table_age =
1252 Min(multixact_freeze_table_age,
1254 Assert(multixact_freeze_table_age >= 0);
1255 aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1260 return true;
1261
1262 /* Non-aggressive VACUUM */
1263 return false;
1264}
int autovacuum_freeze_max_age
Definition autovacuum.c:135
#define Min(x, y)
Definition c.h:1091
int errhint(const char *fmt,...) pg_attribute_printf(1
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2879
int MultiXactMemberFreezeThreshold(void)
Definition multixact.c:2590
#define FirstMultiXactId
Definition multixact.h:26
TransactionId FreezeLimit
Definition vacuum.h:288
TransactionId OldestXmin
Definition vacuum.h:278
TransactionId relfrozenxid
Definition vacuum.h:262
MultiXactId relminmxid
Definition vacuum.h:263
MultiXactId MultiXactCutoff
Definition vacuum.h:289
MultiXactId OldestMxact
Definition vacuum.h:279
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282
#define FirstNormalTransactionId
Definition transam.h:34
int vacuum_freeze_min_age
Definition vacuum.c:76
int vacuum_multixact_freeze_table_age
Definition vacuum.c:79
int vacuum_freeze_table_age
Definition vacuum.c:77
int vacuum_multixact_freeze_min_age
Definition vacuum.c:78

References Assert, autovacuum_freeze_max_age, ereport, errhint(), errmsg, fb(), 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,
uint32  options 
)

Definition at line 720 of file vacuum.c.

722{
723 char *relname;
724
726
727 /*----------
728 * A role has privileges to vacuum or analyze the relation if any of the
729 * following are true:
730 * - the role owns the current database and the relation is not shared
731 * - the role has the MAINTAIN privilege on the relation
732 *----------
733 */
735 !reltuple->relisshared) ||
737 return true;
738
739 relname = NameStr(reltuple->relname);
740
741 if ((options & VACOPT_VACUUM) != 0)
742 {
744 (errmsg("permission denied to vacuum \"%s\", skipping it",
745 relname)));
746
747 /*
748 * For VACUUM ANALYZE, both logs could show up, but just generate
749 * information for VACUUM as that would be the first one to be
750 * processed.
751 */
752 return false;
753 }
754
755 if ((options & VACOPT_ANALYZE) != 0)
757 (errmsg("permission denied to analyze \"%s\", skipping it",
758 relname)));
759
760 return false;
761}
@ ACLCHECK_OK
Definition acl.h:184
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4134
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition aclchk.c:4083
Oid GetUserId(void)
Definition miscinit.c:470
#define ACL_MAINTAIN
Definition parsenodes.h:90
NameData relname
Definition pg_class.h:40

References ACL_MAINTAIN, ACLCHECK_OK, Assert, ereport, errmsg, fb(), 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,
uint32  options,
bool  verbose,
LOCKMODE  lmode 
)

Definition at line 772 of file vacuum.c.

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

References AmAutoVacuumWorkerProcess, Assert, ConditionalLockRelationOid(), ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg, fb(), 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_rel()

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

Definition at line 2012 of file vacuum.c.

2014{
2016 Relation rel;
2020 Oid save_userid;
2021 int save_sec_context;
2022 int save_nestlevel;
2024
2025 /*
2026 * This function scribbles on the parameters, so make a copy early to
2027 * avoid affecting the TOAST table (if we do end up recursing to it).
2028 */
2029 memcpy(&toast_vacuum_params, &params, sizeof(VacuumParams));
2030
2031 /* Begin a transaction for vacuuming this relation */
2033
2034 if (!(params.options & VACOPT_FULL))
2035 {
2036 /*
2037 * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
2038 * other concurrent VACUUMs know that they can ignore this one while
2039 * determining their OldestXmin. (The reason we don't set it during a
2040 * full VACUUM is exactly that we may have to run user-defined
2041 * functions for functional indexes, and we want to make sure that if
2042 * they use the snapshot set above, any tuples it requires can't get
2043 * removed from other tables. An index function that depends on the
2044 * contents of other tables is arguably broken, but we won't break it
2045 * here by violating transaction semantics.)
2046 *
2047 * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
2048 * autovacuum; it's used to avoid canceling a vacuum that was invoked
2049 * in an emergency.
2050 *
2051 * Note: these flags remain set until CommitTransaction or
2052 * AbortTransaction. We don't want to clear them until we reset
2053 * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
2054 * might appear to go backwards, which is probably Not Good. (We also
2055 * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
2056 * xmin doesn't become visible ahead of setting the flag.)
2057 */
2060 if (params.is_wraparound)
2064 }
2065
2066 /*
2067 * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
2068 * cutoff xids in local memory wrapping around, and to have updated xmin
2069 * horizons.
2070 */
2072
2073 /*
2074 * Check for user-requested abort. Note we want this to be inside a
2075 * transaction, so xact.c doesn't issue useless WARNING.
2076 */
2078
2079 /*
2080 * Determine the type of lock we want --- hard exclusive lock for a FULL
2081 * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
2082 * way, we can be sure that no other backend is vacuuming the same table.
2083 */
2084 lmode = (params.options & VACOPT_FULL) ?
2086
2087 /* open the relation and get the appropriate lock on it */
2088 rel = vacuum_open_relation(relid, relation, params.options,
2089 params.log_vacuum_min_duration >= 0, lmode);
2090
2091 /* leave if relation could not be opened or locked */
2092 if (!rel)
2093 {
2096 return false;
2097 }
2098
2099 /*
2100 * When recursing to a TOAST table, check privileges on the parent. NB:
2101 * This is only safe to do because we hold a session lock on the main
2102 * relation that prevents concurrent deletion.
2103 */
2104 if (OidIsValid(params.toast_parent))
2105 priv_relid = params.toast_parent;
2106 else
2108
2109 /*
2110 * Check if relation needs to be skipped based on privileges. This check
2111 * happens also when building the relation list to vacuum for a manual
2112 * operation, and needs to be done additionally here as VACUUM could
2113 * happen across multiple transactions where privileges could have changed
2114 * in-between. Make sure to only generate logs for VACUUM in this case.
2115 */
2117 rel->rd_rel,
2118 params.options & ~VACOPT_ANALYZE))
2119 {
2120 relation_close(rel, lmode);
2123 return false;
2124 }
2125
2126 /*
2127 * Check that it's of a vacuumable relkind.
2128 */
2129 if (rel->rd_rel->relkind != RELKIND_RELATION &&
2130 rel->rd_rel->relkind != RELKIND_MATVIEW &&
2131 rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
2132 rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2133 {
2135 (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
2137 relation_close(rel, lmode);
2140 return false;
2141 }
2142
2143 /*
2144 * Silently ignore tables that are temp tables of other backends ---
2145 * trying to vacuum these will lead to great unhappiness, since their
2146 * contents are probably not up-to-date on disk. (We don't throw a
2147 * warning here; it would just lead to chatter during a database-wide
2148 * VACUUM.)
2149 */
2150 if (RELATION_IS_OTHER_TEMP(rel))
2151 {
2152 relation_close(rel, lmode);
2155 return false;
2156 }
2157
2158 /*
2159 * Silently ignore partitioned tables as there is no work to be done. The
2160 * useful work is on their child partitions, which have been queued up for
2161 * us separately.
2162 */
2163 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2164 {
2165 relation_close(rel, lmode);
2168 /* It's OK to proceed with ANALYZE on this table */
2169 return true;
2170 }
2171
2172 /*
2173 * Get a session-level lock too. This will protect our access to the
2174 * relation across multiple transactions, so that we can vacuum the
2175 * relation's TOAST table (if any) secure in the knowledge that no one is
2176 * deleting the parent relation.
2177 *
2178 * NOTE: this cannot block, even if someone else is waiting for access,
2179 * because the lock manager knows that both lock requests are from the
2180 * same process.
2181 */
2184
2185 /*
2186 * Set index_cleanup option based on index_cleanup reloption if it wasn't
2187 * specified in VACUUM command, or when running in an autovacuum worker
2188 */
2190 {
2191 StdRdOptIndexCleanup vacuum_index_cleanup;
2192
2193 if (rel->rd_options == NULL)
2194 vacuum_index_cleanup = STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO;
2195 else
2196 vacuum_index_cleanup =
2197 ((StdRdOptions *) rel->rd_options)->vacuum_index_cleanup;
2198
2199 if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO)
2201 else if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON)
2203 else
2204 {
2205 Assert(vacuum_index_cleanup ==
2208 }
2209 }
2210
2211#ifdef USE_INJECTION_POINTS
2212 if (params.index_cleanup == VACOPTVALUE_AUTO)
2213 INJECTION_POINT("vacuum-index-cleanup-auto", NULL);
2214 else if (params.index_cleanup == VACOPTVALUE_DISABLED)
2215 INJECTION_POINT("vacuum-index-cleanup-disabled", NULL);
2216 else if (params.index_cleanup == VACOPTVALUE_ENABLED)
2217 INJECTION_POINT("vacuum-index-cleanup-enabled", NULL);
2218#endif
2219
2220 /*
2221 * Check if the vacuum_max_eager_freeze_failure_rate table storage
2222 * parameter was specified. This overrides the GUC value.
2223 */
2224 if (rel->rd_options != NULL &&
2225 ((StdRdOptions *) rel->rd_options)->vacuum_max_eager_freeze_failure_rate >= 0)
2227 ((StdRdOptions *) rel->rd_options)->vacuum_max_eager_freeze_failure_rate;
2228
2229 /*
2230 * Set truncate option based on truncate reloption or GUC if it wasn't
2231 * specified in VACUUM command, or when running in an autovacuum worker
2232 */
2233 if (params.truncate == VACOPTVALUE_UNSPECIFIED)
2234 {
2236
2237 if (opts && opts->vacuum_truncate != PG_TERNARY_UNSET)
2238 {
2239 if (opts->vacuum_truncate == PG_TERNARY_TRUE)
2241 else
2243 }
2244 else if (vacuum_truncate)
2246 else
2248 }
2249
2250#ifdef USE_INJECTION_POINTS
2251 if (params.truncate == VACOPTVALUE_AUTO)
2252 INJECTION_POINT("vacuum-truncate-auto", NULL);
2253 else if (params.truncate == VACOPTVALUE_DISABLED)
2254 INJECTION_POINT("vacuum-truncate-disabled", NULL);
2255 else if (params.truncate == VACOPTVALUE_ENABLED)
2256 INJECTION_POINT("vacuum-truncate-enabled", NULL);
2257#endif
2258
2259 /*
2260 * Remember the relation's TOAST relation for later, if the caller asked
2261 * us to process it. In VACUUM FULL, though, the toast table is
2262 * automatically rebuilt by cluster_rel so we shouldn't recurse to it,
2263 * unless PROCESS_MAIN is disabled.
2264 */
2265 if ((params.options & VACOPT_PROCESS_TOAST) != 0 &&
2266 ((params.options & VACOPT_FULL) == 0 ||
2267 (params.options & VACOPT_PROCESS_MAIN) == 0))
2268 toast_relid = rel->rd_rel->reltoastrelid;
2269 else
2271
2272 /*
2273 * Switch to the table owner's userid, so that any index functions are run
2274 * as that user. Also lock down security-restricted operations and
2275 * arrange to make GUC variable changes local to this command. (This is
2276 * unnecessary, but harmless, for lazy VACUUM.)
2277 */
2278 GetUserIdAndSecContext(&save_userid, &save_sec_context);
2279 SetUserIdAndSecContext(rel->rd_rel->relowner,
2280 save_sec_context | SECURITY_RESTRICTED_OPERATION);
2281 save_nestlevel = NewGUCNestLevel();
2283
2284 /*
2285 * If PROCESS_MAIN is set (the default), it's time to vacuum the main
2286 * relation. Otherwise, we can skip this part. If processing the TOAST
2287 * table is required (e.g., PROCESS_TOAST is set), we force PROCESS_MAIN
2288 * to be set when we recurse to the TOAST table.
2289 */
2290 if (params.options & VACOPT_PROCESS_MAIN)
2291 {
2292 /*
2293 * Do the actual work --- either FULL or "lazy" vacuum
2294 */
2295 if (params.options & VACOPT_FULL)
2296 {
2298
2299 if ((params.options & VACOPT_VERBOSE) != 0)
2301
2302 /* VACUUM FULL is a variant of REPACK; see repack.c */
2305 /* cluster_rel closes the relation, but keeps lock */
2306
2307 rel = NULL;
2308 }
2309 else
2310 table_relation_vacuum(rel, &params, bstrategy);
2311 }
2312
2313 /* Roll back any GUC changes executed by index functions */
2314 AtEOXact_GUC(false, save_nestlevel);
2315
2316 /* Restore userid and security context */
2317 SetUserIdAndSecContext(save_userid, save_sec_context);
2318
2319 /* all done with this class, but hold lock until commit */
2320 if (rel)
2321 relation_close(rel, NoLock);
2322
2323 /*
2324 * Complete the transaction and free all temporary memory used.
2325 */
2328
2329 /*
2330 * If the relation has a secondary toast rel, vacuum that too while we
2331 * still hold the session lock on the main table. Note however that
2332 * "analyze" will not get done on the toast table. This is good, because
2333 * the toaster always uses hardcoded index access and statistics are
2334 * totally unimportant for toast relations.
2335 */
2336 if (toast_relid != InvalidOid)
2337 {
2338 /*
2339 * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise,
2340 * set toast_parent so that the privilege checks are done on the main
2341 * relation. NB: This is only safe to do because we hold a session
2342 * lock on the main relation that prevents concurrent deletion.
2343 */
2345 toast_vacuum_params.toast_parent = relid;
2346
2348 isTopLevel);
2349 }
2350
2351 /*
2352 * Now release the session-level lock on the main table.
2353 */
2355
2356 /* Report that we really did it. */
2357 return true;
2358}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int NewGUCNestLevel(void)
Definition guc.c:2142
void RestrictSearchPath(void)
Definition guc.c:2153
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition guc.c:2169
#define INJECTION_POINT(name, arg)
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition lmgr.c:391
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition lmgr.c:404
int LOCKMODE
Definition lockdefs.h:26
#define AccessExclusiveLock
Definition lockdefs.h:43
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define SECURITY_RESTRICTED_OPERATION
Definition miscadmin.h:331
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition miscinit.c:613
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition miscinit.c:620
@ REPACK_COMMAND_VACUUMFULL
static AmcheckOptions opts
Definition pg_amcheck.c:112
@ PG_TERNARY_TRUE
Definition postgres.h:559
@ PG_TERNARY_UNSET
Definition postgres.h:560
#define PROC_IN_VACUUM
Definition proc.h:62
#define PROC_VACUUM_FOR_WRAPAROUND
Definition proc.h:64
#define RELATION_IS_OTHER_TEMP(relation)
Definition rel.h:678
StdRdOptIndexCleanup
Definition rel.h:337
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO
Definition rel.h:338
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF
Definition rel.h:339
@ STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON
Definition rel.h:340
void cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid, ClusterParams *params, bool isTopLevel)
Definition repack.c:511
#define CLUOPT_VERBOSE
Definition repack.h:25
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
PGPROC * MyProc
Definition proc.c:71
PROC_HDR * ProcGlobal
Definition proc.c:74
uint32 options
Definition repack.h:34
LockRelId lockRelId
Definition rel.h:46
uint8 statusFlags
Definition proc.h:210
int pgxactoff
Definition proc.h:207
uint8 * statusFlags
Definition proc.h:456
LockInfoData rd_lockInfo
Definition rel.h:114
bytea * rd_options
Definition rel.h:175
static void table_relation_vacuum(Relation rel, const VacuumParams *params, BufferAccessStrategy bstrategy)
Definition tableam.h:1777
bool vacuum_truncate
Definition vacuum.c:84
Relation vacuum_open_relation(Oid relid, RangeVar *relation, uint32 options, bool verbose, LOCKMODE lmode)
Definition vacuum.c:772

References AccessExclusiveLock, Assert, AtEOXact_GUC(), CHECK_FOR_INTERRUPTS, CLUOPT_VERBOSE, cluster_rel(), CommitTransactionCommand(), ereport, errmsg, fb(), GetTransactionSnapshot(), GetUserIdAndSecContext(), VacuumParams::index_cleanup, INJECTION_POINT, InvalidOid, VacuumParams::is_wraparound, LockRelationIdForSession(), LockInfoData::lockRelId, VacuumParams::log_vacuum_min_duration, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), VacuumParams::max_eager_freeze_failure_rate, memcpy(), MyProc, NewGUCNestLevel(), NoLock, OidIsValid, ClusterParams::options, VacuumParams::options, opts, PG_TERNARY_TRUE, PG_TERNARY_UNSET, PGPROC::pgxactoff, PopActiveSnapshot(), PROC_IN_VACUUM, PROC_VACUUM_FOR_WRAPAROUND, ProcGlobal, PushActiveSnapshot(), RelationData::rd_lockInfo, RelationData::rd_options, RelationData::rd_rel, relation_close(), RELATION_IS_OTHER_TEMP, RelationGetRelationName, RelationGetRelid, REPACK_COMMAND_VACUUMFULL, RestrictSearchPath(), SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), ShareUpdateExclusiveLock, StartTransactionCommand(), PGPROC::statusFlags, PROC_HDR::statusFlags, STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO, STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF, STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON, table_relation_vacuum(), VacuumParams::toast_parent, VacuumParams::truncate, UnlockRelationIdForSession(), VACOPT_ANALYZE, VACOPT_FULL, VACOPT_PROCESS_MAIN, VACOPT_PROCESS_TOAST, VACOPT_VERBOSE, VACOPTVALUE_AUTO, VACOPTVALUE_DISABLED, VACOPTVALUE_ENABLED, VACOPTVALUE_UNSPECIFIED, vacuum_is_permitted_for_relation(), vacuum_open_relation(), vacuum_rel(), vacuum_truncate, and WARNING.

Referenced by vacuum(), and vacuum_rel().

◆ vacuum_xid_failsafe_check()

bool vacuum_xid_failsafe_check ( const struct VacuumCutoffs cutoffs)

Definition at line 1274 of file vacuum.c.

1275{
1276 TransactionId relfrozenxid = cutoffs->relfrozenxid;
1277 MultiXactId relminmxid = cutoffs->relminmxid;
1281
1282 Assert(TransactionIdIsNormal(relfrozenxid));
1283 Assert(MultiXactIdIsValid(relminmxid));
1284
1285 /*
1286 * Determine the index skipping age to use. In any case no less than
1287 * autovacuum_freeze_max_age * 1.05.
1288 */
1290
1294
1295 if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1296 {
1297 /* The table's relfrozenxid is too old */
1298 return true;
1299 }
1300
1301 /*
1302 * Similar to above, determine the index skipping age to use for
1303 * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1304 * 1.05.
1305 */
1308
1312
1313 if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1314 {
1315 /* The table's relminmxid is too old */
1316 return true;
1317 }
1318
1319 return false;
1320}
int autovacuum_multixact_freeze_max_age
Definition autovacuum.c:136
#define Max(x, y)
Definition c.h:1085
int vacuum_multixact_failsafe_age
Definition vacuum.c:81
int vacuum_failsafe_age
Definition vacuum.c:80

References Assert, autovacuum_freeze_max_age, autovacuum_multixact_freeze_max_age, fb(), 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().

Variable Documentation

◆ parallel_vacuum_worker_delay_ns

int64 parallel_vacuum_worker_delay_ns = 0

Definition at line 96 of file vacuum.c.

Referenced by parallel_vacuum_main(), and vacuum_delay_point().

◆ track_cost_delay_timing

bool track_cost_delay_timing

Definition at line 83 of file vacuum.c.

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

◆ vacuum_cost_delay

◆ vacuum_cost_limit

◆ vacuum_failsafe_age

int vacuum_failsafe_age

Definition at line 80 of file vacuum.c.

Referenced by relation_needs_vacanalyze(), and vacuum_xid_failsafe_check().

◆ vacuum_freeze_min_age

int vacuum_freeze_min_age

Definition at line 76 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_freeze_table_age

int vacuum_freeze_table_age

Definition at line 77 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_max_eager_freeze_failure_rate

double vacuum_max_eager_freeze_failure_rate

Definition at line 82 of file vacuum.c.

Referenced by default_reloptions(), ExecVacuum(), and table_recheck_autovac().

◆ vacuum_multixact_failsafe_age

int vacuum_multixact_failsafe_age

Definition at line 81 of file vacuum.c.

Referenced by relation_needs_vacanalyze(), and vacuum_xid_failsafe_check().

◆ vacuum_multixact_freeze_min_age

int vacuum_multixact_freeze_min_age

Definition at line 78 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_multixact_freeze_table_age

int vacuum_multixact_freeze_table_age

Definition at line 79 of file vacuum.c.

Referenced by do_autovacuum(), and vacuum_get_cutoffs().

◆ vacuum_truncate

bool vacuum_truncate

Definition at line 84 of file vacuum.c.

Referenced by default_reloptions(), and vacuum_rel().

◆ VacuumActiveNWorkers

◆ VacuumCostBalanceLocal

int VacuumCostBalanceLocal = 0

◆ VacuumFailsafeActive

◆ VacuumSharedCostBalance