Debian Codegraph @

src/backend/commands/vacuum.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * vacuum.c
4 * The postgres vacuum cleaner.
5 *
6 * This file includes (a) control and dispatch code for VACUUM and ANALYZE
7 * commands, (b) code to compute various vacuum thresholds, and (c) index
8 * vacuum code.
9 *
10 * VACUUM for heap AM is implemented in vacuumlazy.c, parallel vacuum in
11 * vacuumparallel.c, ANALYZE in analyze.c, and VACUUM FULL is a variant of
12 * CLUSTER, handled in cluster.c.
13 *
14 *
15 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
16 * Portions Copyright (c) 1994, Regents of the University of California
17 *
18 *
19 * IDENTIFICATION
20 * src/backend/commands/vacuum.c
21 *
22 *-------------------------------------------------------------------------
23 */
24 #include "postgres.h"
25
26 #include <math.h>
27
28 #include "access/clog.h"
29 #include "access/commit_ts.h"
30 #include "access/genam.h"
31 #include "access/heapam.h"
32 #include "access/htup_details.h"
33 #include "access/multixact.h"
34 #include "access/tableam.h"
35 #include "access/transam.h"
36 #include "access/xact.h"
37 #include "catalog/namespace.h"
38 #include "catalog/pg_database.h"
39 #include "catalog/pg_inherits.h"
40 #include "commands/async.h"
41 #include "commands/cluster.h"
42 #include "commands/defrem.h"
43 #include "commands/progress.h"
44 #include "commands/vacuum.h"
45 #include "miscadmin.h"
46 #include "nodes/makefuncs.h"
47 #include "pgstat.h"
48 #include "postmaster/autovacuum.h"
49 #include "postmaster/bgworker_internals.h"
50 #include "postmaster/interrupt.h"
51 #include "storage/bufmgr.h"
52 #include "storage/lmgr.h"
53 #include "storage/pmsignal.h"
54 #include "storage/proc.h"
55 #include "storage/procarray.h"
56 #include "utils/acl.h"
57 #include "utils/fmgroids.h"
58 #include "utils/guc.h"
59 #include "utils/guc_hooks.h"
60 #include "utils/injection_point.h"
61 #include "utils/memutils.h"
62 #include "utils/snapmgr.h"
63 #include "utils/syscache.h"
64
65 /*
66 * Minimum interval for cost-based vacuum delay reports from a parallel worker.
67 * This aims to avoid sending too many messages and waking up the leader too
68 * frequently.
69 */
70 #define PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS (NS_PER_S)
71
72 /*
73 * GUC parameters
74 */
75 int vacuum_freeze_min_age;
76 int vacuum_freeze_table_age;
77 int vacuum_multixact_freeze_min_age;
78 int vacuum_multixact_freeze_table_age;
79 int vacuum_failsafe_age;
80 int vacuum_multixact_failsafe_age;
81 double vacuum_max_eager_freeze_failure_rate;
82 bool track_cost_delay_timing;
83 bool vacuum_truncate;
84
85 /*
86 * Variables for cost-based vacuum delay. The defaults differ between
87 * autovacuum and vacuum. They should be set with the appropriate GUC value in
88 * vacuum code. They are initialized here to the defaults for client backends
89 * executing VACUUM or ANALYZE.
90 */
91 double vacuum_cost_delay = 0;
92 int vacuum_cost_limit = 200;
93
94 /* Variable for reporting cost-based vacuum delay from parallel workers. */
95 int64 parallel_vacuum_worker_delay_ns = 0;
96
97 /*
98 * VacuumFailsafeActive is a defined as a global so that we can determine
99 * whether or not to re-enable cost-based vacuum delay when vacuuming a table.
100 * If failsafe mode has been engaged, we will not re-enable cost-based delay
101 * for the table until after vacuuming has completed, regardless of other
102 * settings.
103 *
104 * Only VACUUM code should inspect this variable and only table access methods
105 * should set it to true. In Table AM-agnostic VACUUM code, this variable is
106 * inspected to determine whether or not to allow cost-based delays. Table AMs
107 * are free to set it if they desire this behavior, but it is false by default
108 * and reset to false in between vacuuming each relation.
109 */
110 bool VacuumFailsafeActive = false;
111
112 /*
113 * Variables for cost-based parallel vacuum. See comments atop
114 * compute_parallel_delay to understand how it works.
115 */
116 pg_atomic_uint32 *VacuumSharedCostBalance = NULL;
117 pg_atomic_uint32 *VacuumActiveNWorkers = NULL;
118 int VacuumCostBalanceLocal = 0;
119
120 /* non-export function prototypes */
121 static List *expand_vacuum_rel(VacuumRelation *vrel,
122 MemoryContext vac_context, int options);
123 static List *get_all_vacuum_rels(MemoryContext vac_context, int options);
124 static void vac_truncate_clog(TransactionId frozenXID,
125 MultiXactId minMulti,
126 TransactionId lastSaneFrozenXid,
127 MultiXactId lastSaneMinMulti);
128 static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
129 BufferAccessStrategy bstrategy);
130 static double compute_parallel_delay(void);
131 static VacOptValue get_vacoptval_from_boolean(DefElem *def);
132 static bool vac_tid_reaped(ItemPointer itemptr, void *state);
133
134 /*
135 * GUC check function to ensure GUC value specified is within the allowable
136 * range.
137 */
138 bool
139 check_vacuum_buffer_usage_limit(int *newval, void **extra,
140 GucSource source)
141 {
142 /* Value upper and lower hard limits are inclusive */
143 if (*newval == 0 || (*newval >= MIN_BAS_VAC_RING_SIZE_KB &&
144 *newval <= MAX_BAS_VAC_RING_SIZE_KB))
145 return true;
146
147 /* Value does not fall within any allowable range */
148 GUC_check_errdetail("\"%s\" must be 0 or between %d kB and %d kB.",
149 "vacuum_buffer_usage_limit",
150 MIN_BAS_VAC_RING_SIZE_KB, MAX_BAS_VAC_RING_SIZE_KB);
151
152 return false;
153 }
154
155 /*
156 * Primary entry point for manual VACUUM and ANALYZE commands
157 *
158 * This is mainly a preparation wrapper for the real operations that will
159 * happen in vacuum().
160 */
161 void
162 ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
163 {
164 VacuumParams params;
165 BufferAccessStrategy bstrategy = NULL;
166 bool verbose = false;
167 bool skip_locked = false;
168 bool analyze = false;
169 bool freeze = false;
170 bool full = false;
171 bool disable_page_skipping = false;
172 bool process_main = true;
173 bool process_toast = true;
174 int ring_size;
175 bool skip_database_stats = false;
176 bool only_database_stats = false;
177 MemoryContext vac_context;
178 ListCell *lc;
179
180 /* index_cleanup and truncate values unspecified for now */
181 params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
182 params.truncate = VACOPTVALUE_UNSPECIFIED;
183
184 /* By default parallel vacuum is enabled */
185 params.nworkers = 0;
186
187 /* Will be set later if we recurse to a TOAST table. */
188 params.toast_parent = InvalidOid;
189
190 /*
191 * Set this to an invalid value so it is clear whether or not a
192 * BUFFER_USAGE_LIMIT was specified when making the access strategy.
193 */
194 ring_size = -1;
195
196 /* Parse options list */
197 foreach(lc, vacstmt->options)
198 {
199 DefElem *opt = (DefElem *) lfirst(lc);
200
201 /* Parse common options for VACUUM and ANALYZE */
202 if (strcmp(opt->defname, "verbose") == 0)
203 verbose = defGetBoolean(opt);
204 else if (strcmp(opt->defname, "skip_locked") == 0)
205 skip_locked = defGetBoolean(opt);
206 else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
207 {
208 const char *hintmsg;
209 int result;
210 char *vac_buffer_size;
211
212 vac_buffer_size = defGetString(opt);
213
214 /*
215 * Check that the specified value is valid and the size falls
216 * within the hard upper and lower limits if it is not 0.
217 */
218 if (!parse_int(vac_buffer_size, &result, GUC_UNIT_KB, &hintmsg) ||
219 (result != 0 &&
220 (result < MIN_BAS_VAC_RING_SIZE_KB || result > MAX_BAS_VAC_RING_SIZE_KB)))
221 {
222 ereport(ERROR,
223 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
224 errmsg("BUFFER_USAGE_LIMIT option must be 0 or between %d kB and %d kB",
225 MIN_BAS_VAC_RING_SIZE_KB, MAX_BAS_VAC_RING_SIZE_KB),
226 hintmsg ? errhint("%s", _(hintmsg)) : 0));
227 }
228
229 ring_size = result;
230 }
231 else if (!vacstmt->is_vacuumcmd)
232 ereport(ERROR,
233 (errcode(ERRCODE_SYNTAX_ERROR),
234 errmsg("unrecognized %s option \"%s\"",
235 "ANALYZE", opt->defname),
236 parser_errposition(pstate, opt->location)));
237
238 /* Parse options available on VACUUM */
239 else if (strcmp(opt->defname, "analyze") == 0)
240 analyze = defGetBoolean(opt);
241 else if (strcmp(opt->defname, "freeze") == 0)
242 freeze = defGetBoolean(opt);
243 else if (strcmp(opt->defname, "full") == 0)
244 full = defGetBoolean(opt);
245 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
246 disable_page_skipping = defGetBoolean(opt);
247 else if (strcmp(opt->defname, "index_cleanup") == 0)
248 {
249 /* Interpret no string as the default, which is 'auto' */
250 if (!opt->arg)
251 params.index_cleanup = VACOPTVALUE_AUTO;
252 else
253 {
254 char *sval = defGetString(opt);
255
256 /* Try matching on 'auto' string, or fall back on boolean */
257 if (pg_strcasecmp(sval, "auto") == 0)
258 params.index_cleanup = VACOPTVALUE_AUTO;
259 else
260 params.index_cleanup = get_vacoptval_from_boolean(opt);
261 }
262 }
263 else if (strcmp(opt->defname, "process_main") == 0)
264 process_main = defGetBoolean(opt);
265 else if (strcmp(opt->defname, "process_toast") == 0)
266 process_toast = defGetBoolean(opt);
267 else if (strcmp(opt->defname, "truncate") == 0)
268 params.truncate = get_vacoptval_from_boolean(opt);
269 else if (strcmp(opt->defname, "parallel") == 0)
270 {
271 if (opt->arg == NULL)
272 {
273 ereport(ERROR,
274 (errcode(ERRCODE_SYNTAX_ERROR),
275 errmsg("parallel option requires a value between 0 and %d",
276 MAX_PARALLEL_WORKER_LIMIT),
277 parser_errposition(pstate, opt->location)));
278 }
279 else
280 {
281 int nworkers;
282
283 nworkers = defGetInt32(opt);
284 if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
285 ereport(ERROR,
286 (errcode(ERRCODE_SYNTAX_ERROR),
287 errmsg("parallel workers for vacuum must be between 0 and %d",
288 MAX_PARALLEL_WORKER_LIMIT),
289 parser_errposition(pstate, opt->location)));
290
291 /*
292 * Disable parallel vacuum, if user has specified parallel
293 * degree as zero.
294 */
295 if (nworkers == 0)
296 params.nworkers = -1;
297 else
298 params.nworkers = nworkers;
299 }
300 }
301 else if (strcmp(opt->defname, "skip_database_stats") == 0)
302 skip_database_stats = defGetBoolean(opt);
303 else if (strcmp(opt->defname, "only_database_stats") == 0)
304 only_database_stats = defGetBoolean(opt);
305 else
306 ereport(ERROR,
307 (errcode(ERRCODE_SYNTAX_ERROR),
308 errmsg("unrecognized %s option \"%s\"",
309 "VACUUM", opt->defname),
310 parser_errposition(pstate, opt->location)));
311 }
312
313 /* Set vacuum options */
314 params.options =
315 (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
316 (verbose ? VACOPT_VERBOSE : 0) |
317 (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
318 (analyze ? VACOPT_ANALYZE : 0) |
319 (freeze ? VACOPT_FREEZE : 0) |
320 (full ? VACOPT_FULL : 0) |
321 (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
322 (process_main ? VACOPT_PROCESS_MAIN : 0) |
323 (process_toast ? VACOPT_PROCESS_TOAST : 0) |
324 (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
325 (only_database_stats ? VACOPT_ONLY_DATABASE_STATS : 0);
326
327 /* sanity checks on options */
328 Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
329 Assert((params.options & VACOPT_VACUUM) ||
330 !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
331
332 if ((params.options & VACOPT_FULL) && params.nworkers > 0)
333 ereport(ERROR,
334 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
335 errmsg("VACUUM FULL cannot be performed in parallel")));
336
337 /*
338 * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
339 * ERROR for that case. VACUUM (FULL, ANALYZE) does make use of it, so
340 * we'll permit that.
341 */
342 if (ring_size != -1 && (params.options & VACOPT_FULL) &&
343 !(params.options & VACOPT_ANALYZE))
344 ereport(ERROR,
345 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
346 errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
347
348 /*
349 * Make sure VACOPT_ANALYZE is specified if any column lists are present.
350 */
351 if (!(params.options & VACOPT_ANALYZE))
352 {
353 foreach(lc, vacstmt->rels)
354 {
355 VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
356
357 if (vrel->va_cols != NIL)
358 ereport(ERROR,
359 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
360 errmsg("ANALYZE option must be specified when a column list is provided")));
361 }
362 }
363
364
365 /*
366 * Sanity check DISABLE_PAGE_SKIPPING option.
367 */
368 if ((params.options & VACOPT_FULL) != 0 &&
369 (params.options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
370 ereport(ERROR,
371 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
372 errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
373
374 /* sanity check for PROCESS_TOAST */
375 if ((params.options & VACOPT_FULL) != 0 &&
376 (params.options & VACOPT_PROCESS_TOAST) == 0)
377 ereport(ERROR,
378 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
379 errmsg("PROCESS_TOAST required with VACUUM FULL")));
380
381 /* sanity check for ONLY_DATABASE_STATS */
382 if (params.options & VACOPT_ONLY_DATABASE_STATS)
383 {
384 Assert(params.options & VACOPT_VACUUM);
385 if (vacstmt->rels != NIL)
386 ereport(ERROR,
387 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
388 errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
389 /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
390 if (params.options & ~(VACOPT_VACUUM |
391 VACOPT_VERBOSE |
392 VACOPT_PROCESS_MAIN |
393 VACOPT_PROCESS_TOAST |
394 VACOPT_ONLY_DATABASE_STATS))
395 ereport(ERROR,
396 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
397 errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
398 }
399
400 /*
401 * All freeze ages are zero if the FREEZE option is given; otherwise pass
402 * them as -1 which means to use the default values.
403 */
404 if (params.options & VACOPT_FREEZE)
405 {
406 params.freeze_min_age = 0;
407 params.freeze_table_age = 0;
408 params.multixact_freeze_min_age = 0;
409 params.multixact_freeze_table_age = 0;
410 }
411 else
412 {
413 params.freeze_min_age = -1;
414 params.freeze_table_age = -1;
415 params.multixact_freeze_min_age = -1;
416 params.multixact_freeze_table_age = -1;
417 }
418
419 /* user-invoked vacuum is never "for wraparound" */
420 params.is_wraparound = false;
421
422 /* user-invoked vacuum uses VACOPT_VERBOSE instead of log_min_duration */
423 params.log_min_duration = -1;
424
425 /*
426 * Later, in vacuum_rel(), we check if a reloption override was specified.
427 */
428 params.max_eager_freeze_failure_rate = vacuum_max_eager_freeze_failure_rate;
429
430 /*
431 * Create special memory context for cross-transaction storage.
432 *
433 * Since it is a child of PortalContext, it will go away eventually even
434 * if we suffer an error; there's no need for special abort cleanup logic.
435 */
436 vac_context = AllocSetContextCreate(PortalContext,
437 "Vacuum",
438 ALLOCSET_DEFAULT_SIZES);
439
440 /*
441 * Make a buffer strategy object in the cross-transaction memory context.
442 * We needn't bother making this for VACUUM (FULL) or VACUUM
443 * (ONLY_DATABASE_STATS) as they'll not make use of it. VACUUM (FULL,
444 * ANALYZE) is possible, so we'd better ensure that we make a strategy
445 * when we see ANALYZE.
446 */
447 if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
448 VACOPT_FULL)) == 0 ||
449 (params.options & VACOPT_ANALYZE) != 0)
450 {
451
452 MemoryContext old_context = MemoryContextSwitchTo(vac_context);
453
454 Assert(ring_size >= -1);
455
456 /*
457 * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
458 * command, it overrides the value of VacuumBufferUsageLimit. Either
459 * value may be 0, in which case GetAccessStrategyWithSize() will
460 * return NULL, effectively allowing full use of shared buffers.
461 */
462 if (ring_size == -1)
463 ring_size = VacuumBufferUsageLimit;
464
465 bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, ring_size);
466
467 MemoryContextSwitchTo(old_context);
468 }
469
470 /* Now go through the common routine */
471 vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
472
473 /* Finally, clean up the vacuum memory context */
474 MemoryContextDelete(vac_context);
475 }
476
477 /*
478 * Internal entry point for autovacuum and the VACUUM / ANALYZE commands.
479 *
480 * relations, if not NIL, is a list of VacuumRelation to process; otherwise,
481 * we process all relevant tables in the database. For each VacuumRelation,
482 * if a valid OID is supplied, the table with that OID is what to process;
483 * otherwise, the VacuumRelation's RangeVar indicates what to process.
484 *
485 * params contains a set of parameters that can be used to customize the
486 * behavior.
487 *
488 * bstrategy may be passed in as NULL when the caller does not want to
489 * restrict the number of shared_buffers that VACUUM / ANALYZE can use,
490 * otherwise, the caller must build a BufferAccessStrategy with the number of
491 * shared_buffers that VACUUM / ANALYZE should try to limit themselves to
492 * using.
493 *
494 * isTopLevel should be passed down from ProcessUtility.
495 *
496 * It is the caller's responsibility that all parameters are allocated in a
497 * memory context that will not disappear at transaction commit.
498 */
499 void
500 vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
501 MemoryContext vac_context, bool isTopLevel)
502 {
503 static bool in_vacuum = false;
504
505 const char *stmttype;
506 volatile bool in_outer_xact,
507 use_own_xacts;
508
509 Assert(params != NULL);
510
511 stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
512
513 /*
514 * We cannot run VACUUM inside a user transaction block; if we were inside
515 * a transaction, then our commit- and start-transaction-command calls
516 * would not have the intended effect! There are numerous other subtle
517 * dependencies on this, too.
518 *
519 * ANALYZE (without VACUUM) can run either way.
520 */
521 if (params->options & VACOPT_VACUUM)
522 {
523 PreventInTransactionBlock(isTopLevel, stmttype);
524 in_outer_xact = false;
525 }
526 else
527 in_outer_xact = IsInTransactionBlock(isTopLevel);
528
529 /*
530 * Check for and disallow recursive calls. This could happen when VACUUM
531 * FULL or ANALYZE calls a hostile index expression that itself calls
532 * ANALYZE.
533 */
534 if (in_vacuum)
535 ereport(ERROR,
536 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
537 errmsg("%s cannot be executed from VACUUM or ANALYZE",
538 stmttype)));
539
540 /*
541 * Build list of relation(s) to process, putting any new data in
542 * vac_context for safekeeping.
543 */
544 if (params->options & VACOPT_ONLY_DATABASE_STATS)
545 {
546 /* We don't process any tables in this case */
547 Assert(relations == NIL);
548 }
549 else if (relations != NIL)
550 {
551 List *newrels = NIL;
552 ListCell *lc;
553
554 foreach(lc, relations)
555 {
556 VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
557 List *sublist;
558 MemoryContext old_context;
559
560 sublist = expand_vacuum_rel(vrel, vac_context, params->options);
561 old_context = MemoryContextSwitchTo(vac_context);
562 newrels = list_concat(newrels, sublist);
563 MemoryContextSwitchTo(old_context);
564 }
565 relations = newrels;
566 }
567 else
568 relations = get_all_vacuum_rels(vac_context, params->options);
569
570 /*
571 * Decide whether we need to start/commit our own transactions.
572 *
573 * For VACUUM (with or without ANALYZE): always do so, so that we can
574 * release locks as soon as possible. (We could possibly use the outer
575 * transaction for a one-table VACUUM, but handling TOAST tables would be
576 * problematic.)
577 *
578 * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
579 * start/commit our own transactions. Also, there's no need to do so if
580 * only processing one relation. For multiple relations when not within a
581 * transaction block, and also in an autovacuum worker, use own
582 * transactions so we can release locks sooner.
583 */
584 if (params->options & VACOPT_VACUUM)
585 use_own_xacts = true;
586 else
587 {
588 Assert(params->options & VACOPT_ANALYZE);
589 if (AmAutoVacuumWorkerProcess())
590 use_own_xacts = true;
591 else if (in_outer_xact)
592 use_own_xacts = false;
593 else if (list_length(relations) > 1)
594 use_own_xacts = true;
595 else
596 use_own_xacts = false;
597 }
598
599 /*
600 * vacuum_rel expects to be entered with no transaction active; it will
601 * start and commit its own transaction. But we are called by an SQL
602 * command, and so we are executing inside a transaction already. We
603 * commit the transaction started in PostgresMain() here, and start
604 * another one before exiting to match the commit waiting for us back in
605 * PostgresMain().
606 */
607 if (use_own_xacts)
608 {
609 Assert(!in_outer_xact);
610
611 /* ActiveSnapshot is not set by autovacuum */
612 if (ActiveSnapshotSet())
613 PopActiveSnapshot();
614
615 /* matches the StartTransaction in PostgresMain() */
616 CommitTransactionCommand();
617 }
618
619 /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
620 PG_TRY();
621 {
622 ListCell *cur;
623
624 in_vacuum = true;
625 VacuumFailsafeActive = false;
626 VacuumUpdateCosts();
627 VacuumCostBalance = 0;
628 VacuumCostBalanceLocal = 0;
629 VacuumSharedCostBalance = NULL;
630 VacuumActiveNWorkers = NULL;
631
632 /*
633 * Loop to process each selected relation.
634 */
635 foreach(cur, relations)
636 {
637 VacuumRelation *vrel = lfirst_node(VacuumRelation, cur);
638
639 if (params->options & VACOPT_VACUUM)
640 {
641 VacuumParams params_copy;
642
643 /*
644 * vacuum_rel() scribbles on the parameters, so give it a copy
645 * to avoid affecting other relations.
646 */
647 memcpy(&params_copy, params, sizeof(VacuumParams));
648
649 if (!vacuum_rel(vrel->oid, vrel->relation, &params_copy, bstrategy))
650 continue;
651 }
652
653 if (params->options & VACOPT_ANALYZE)
654 {
655 /*
656 * If using separate xacts, start one for analyze. Otherwise,
657 * we can use the outer transaction.
658 */
659 if (use_own_xacts)
660 {
661 StartTransactionCommand();
662 /* functions in indexes may want a snapshot set */
663 PushActiveSnapshot(GetTransactionSnapshot());
664 }
665
666 analyze_rel(vrel->oid, vrel->relation, params,
667 vrel->va_cols, in_outer_xact, bstrategy);
668
669 if (use_own_xacts)
670 {
671 PopActiveSnapshot();
672 /* standard_ProcessUtility() does CCI if !use_own_xacts */
673 CommandCounterIncrement();
674 CommitTransactionCommand();
675 }
676 else
677 {
678 /*
679 * If we're not using separate xacts, better separate the
680 * ANALYZE actions with CCIs. This avoids trouble if user
681 * says "ANALYZE t, t".
682 */
683 CommandCounterIncrement();
684 }
685 }
686
687 /*
688 * Ensure VacuumFailsafeActive has been reset before vacuuming the
689 * next relation.
690 */
691 VacuumFailsafeActive = false;
692 }
693 }
694 PG_FINALLY();
695 {
696 in_vacuum = false;
697 VacuumCostActive = false;
698 VacuumFailsafeActive = false;
699 VacuumCostBalance = 0;
700 }
701 PG_END_TRY();
702
703 /*
704 * Finish up processing.
705 */
706 if (use_own_xacts)
707 {
708 /* here, we are not in a transaction */
709
710 /*
711 * This matches the CommitTransaction waiting for us in
712 * PostgresMain().
713 */
714 StartTransactionCommand();
715 }
716
717 if ((params->options & VACOPT_VACUUM) &&
718 !(params->options & VACOPT_SKIP_DATABASE_STATS))
719 {
720 /*
721 * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
722 */
723 vac_update_datfrozenxid();
724 }
725
726 }
727
728 /*
729 * Check if the current user has privileges to vacuum or analyze the relation.
730 * If not, issue a WARNING log message and return false to let the caller
731 * decide what to do with this relation. This routine is used to decide if a
732 * relation can be processed for VACUUM or ANALYZE.
733 */
734 bool
735 vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple,
736 bits32 options)
737 {
738 char *relname;
739
740 Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
741
742 /*----------
743 * A role has privileges to vacuum or analyze the relation if any of the
744 * following are true:
745 * - the role owns the current database and the relation is not shared
746 * - the role has the MAINTAIN privilege on the relation
747 *----------
748 */
749 if ((object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()) &&
750 !reltuple->relisshared) ||
751 pg_class_aclcheck(relid, GetUserId(), ACL_MAINTAIN) == ACLCHECK_OK)
752 return true;
753
754 relname = NameStr(reltuple->relname);
755
756 if ((options & VACOPT_VACUUM) != 0)
757 {
758 ereport(WARNING,
759 (errmsg("permission denied to vacuum \"%s\", skipping it",
760 relname)));
761
762 /*
763 * For VACUUM ANALYZE, both logs could show up, but just generate
764 * information for VACUUM as that would be the first one to be
765 * processed.
766 */
767 return false;
768 }
769
770 if ((options & VACOPT_ANALYZE) != 0)
771 ereport(WARNING,
772 (errmsg("permission denied to analyze \"%s\", skipping it",
773 relname)));
774
775 return false;
776 }
777
778
779 /*
780 * vacuum_open_relation
781 *
782 * This routine is used for attempting to open and lock a relation which
783 * is going to be vacuumed or analyzed. If the relation cannot be opened
784 * or locked, a log is emitted if possible.
785 */
786 Relation
787 vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options,
788 bool verbose, LOCKMODE lmode)
789 {
790 Relation rel;
791 bool rel_lock = true;
792 int elevel;
793
794 Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
795
796 /*
797 * Open the relation and get the appropriate lock on it.
798 *
799 * There's a race condition here: the relation may have gone away since
800 * the last time we saw it. If so, we don't need to vacuum or analyze it.
801 *
802 * If we've been asked not to wait for the relation lock, acquire it first
803 * in non-blocking mode, before calling try_relation_open().
804 */
805 if (!(options & VACOPT_SKIP_LOCKED))
806 rel = try_relation_open(relid, lmode);
807 else if (ConditionalLockRelationOid(relid, lmode))
808 rel = try_relation_open(relid, NoLock);
809 else
810 {
811 rel = NULL;
812 rel_lock = false;
813 }
814
815 /* if relation is opened, leave */
816 if (rel)
817 return rel;
818
819 /*
820 * Relation could not be opened, hence generate if possible a log
821 * informing on the situation.
822 *
823 * If the RangeVar is not defined, we do not have enough information to
824 * provide a meaningful log statement. Chances are that the caller has
825 * intentionally not provided this information so that this logging is
826 * skipped, anyway.
827 */
828 if (relation == NULL)
829 return NULL;
830
831 /*
832 * Determine the log level.
833 *
834 * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
835 * statements in the permission checks; otherwise, only log if the caller
836 * so requested.
837 */
838 if (!AmAutoVacuumWorkerProcess())
839 elevel = WARNING;
840 else if (verbose)
841 elevel = LOG;
842 else
843 return NULL;
844
845 if ((options & VACOPT_VACUUM) != 0)
846 {
847 if (!rel_lock)
848 ereport(elevel,
849 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
850 errmsg("skipping vacuum of \"%s\" --- lock not available",
851 relation->relname)));
852 else
853 ereport(elevel,
854 (errcode(ERRCODE_UNDEFINED_TABLE),
855 errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
856 relation->relname)));
857
858 /*
859 * For VACUUM ANALYZE, both logs could show up, but just generate
860 * information for VACUUM as that would be the first one to be
861 * processed.
862 */
863 return NULL;
864 }
865
866 if ((options & VACOPT_ANALYZE) != 0)
867 {
868 if (!rel_lock)
869 ereport(elevel,
870 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
871 errmsg("skipping analyze of \"%s\" --- lock not available",
872 relation->relname)));
873 else
874 ereport(elevel,
875 (errcode(ERRCODE_UNDEFINED_TABLE),
876 errmsg("skipping analyze of \"%s\" --- relation no longer exists",
877 relation->relname)));
878 }
879
880 return NULL;
881 }
882
883
884 /*
885 * Given a VacuumRelation, fill in the table OID if it wasn't specified,
886 * and optionally add VacuumRelations for partitions or inheritance children.
887 *
888 * If a VacuumRelation does not have an OID supplied and is a partitioned
889 * table, an extra entry will be added to the output for each partition.
890 * Presently, only autovacuum supplies OIDs when calling vacuum(), and
891 * it does not want us to expand partitioned tables.
892 *
893 * We take care not to modify the input data structure, but instead build
894 * new VacuumRelation(s) to return. (But note that they will reference
895 * unmodified parts of the input, eg column lists.) New data structures
896 * are made in vac_context.
897 */
898 static List *
899 expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context,
900 int options)
901 {
902 List *vacrels = NIL;
903 MemoryContext oldcontext;
904
905 /* If caller supplied OID, there's nothing we need do here. */
906 if (OidIsValid(vrel->oid))
907 {
908 oldcontext = MemoryContextSwitchTo(vac_context);
909 vacrels = lappend(vacrels, vrel);
910 MemoryContextSwitchTo(oldcontext);
911 }
912 else
913 {
914 /*
915 * Process a specific relation, and possibly partitions or child
916 * tables thereof.
917 */
918 Oid relid;
919 HeapTuple tuple;
920 Form_pg_class classForm;
921 bool include_children;
922 bool is_partitioned_table;
923 int rvr_opts;
924
925 /*
926 * Since autovacuum workers supply OIDs when calling vacuum(), no
927 * autovacuum worker should reach this code.
928 */
929 Assert(!AmAutoVacuumWorkerProcess());
930
931 /*
932 * We transiently take AccessShareLock to protect the syscache lookup
933 * below, as well as find_all_inheritors's expectation that the caller
934 * holds some lock on the starting relation.
935 */
936 rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
937 relid = RangeVarGetRelidExtended(vrel->relation,
938 AccessShareLock,
939 rvr_opts,
940 NULL, NULL);
941
942 /*
943 * If the lock is unavailable, emit the same log statement that
944 * vacuum_rel() and analyze_rel() would.
945 */
946 if (!OidIsValid(relid))
947 {
948 if (options & VACOPT_VACUUM)
949 ereport(WARNING,
950 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
951 errmsg("skipping vacuum of \"%s\" --- lock not available",
952 vrel->relation->relname)));
953 else
954 ereport(WARNING,
955 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
956 errmsg("skipping analyze of \"%s\" --- lock not available",
957 vrel->relation->relname)));
958 return vacrels;
959 }
960
961 /*
962 * To check whether the relation is a partitioned table and its
963 * ownership, fetch its syscache entry.
964 */
965 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
966 if (!HeapTupleIsValid(tuple))
967 elog(ERROR, "cache lookup failed for relation %u", relid);
968 classForm = (Form_pg_class) GETSTRUCT(tuple);
969
970 /*
971 * Make a returnable VacuumRelation for this rel if the user has the
972 * required privileges.
973 */
974 if (vacuum_is_permitted_for_relation(relid, classForm, options))
975 {
976 oldcontext = MemoryContextSwitchTo(vac_context);
977 vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
978 relid,
979 vrel->va_cols));
980 MemoryContextSwitchTo(oldcontext);
981 }
982
983 /*
984 * Vacuuming a partitioned table with ONLY will not do anything since
985 * the partitioned table itself is empty. Issue a warning if the user
986 * requests this.
987 */
988 include_children = vrel->relation->inh;
989 is_partitioned_table = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
990 if ((options & VACOPT_VACUUM) && is_partitioned_table && !include_children)
991 ereport(WARNING,
992 (errmsg("VACUUM ONLY of partitioned table \"%s\" has no effect",
993 vrel->relation->relname)));
994
995 ReleaseSysCache(tuple);
996
997 /*
998 * Unless the user has specified ONLY, make relation list entries for
999 * its partitions or inheritance child tables. Note that the list
1000 * returned by find_all_inheritors() includes the passed-in OID, so we
1001 * have to skip that. There's no point in taking locks on the
1002 * individual partitions or child tables yet, and doing so would just
1003 * add unnecessary deadlock risk. For this last reason, we do not yet
1004 * check the ownership of the partitions/tables, which get added to
1005 * the list to process. Ownership will be checked later on anyway.
1006 */
1007 if (include_children)
1008 {
1009 List *part_oids = find_all_inheritors(relid, NoLock, NULL);
1010 ListCell *part_lc;
1011
1012 foreach(part_lc, part_oids)
1013 {
1014 Oid part_oid = lfirst_oid(part_lc);
1015
1016 if (part_oid == relid)
1017 continue; /* ignore original table */
1018
1019 /*
1020 * We omit a RangeVar since it wouldn't be appropriate to
1021 * complain about failure to open one of these relations
1022 * later.
1023 */
1024 oldcontext = MemoryContextSwitchTo(vac_context);
1025 vacrels = lappend(vacrels, makeVacuumRelation(NULL,
1026 part_oid,
1027 vrel->va_cols));
1028 MemoryContextSwitchTo(oldcontext);
1029 }
1030 }
1031
1032 /*
1033 * Release lock again. This means that by the time we actually try to
1034 * process the table, it might be gone or renamed. In the former case
1035 * we'll silently ignore it; in the latter case we'll process it
1036 * anyway, but we must beware that the RangeVar doesn't necessarily
1037 * identify it anymore. This isn't ideal, perhaps, but there's little
1038 * practical alternative, since we're typically going to commit this
1039 * transaction and begin a new one between now and then. Moreover,
1040 * holding locks on multiple relations would create significant risk
1041 * of deadlock.
1042 */
1043 UnlockRelationOid(relid, AccessShareLock);
1044 }
1045
1046 return vacrels;
1047 }
1048
1049 /*
1050 * Construct a list of VacuumRelations for all vacuumable rels in
1051 * the current database. The list is built in vac_context.
1052 */
1053 static List *
1054 get_all_vacuum_rels(MemoryContext vac_context, int options)
1055 {
1056 List *vacrels = NIL;
1057 Relation pgclass;
1058 TableScanDesc scan;
1059 HeapTuple tuple;
1060
1061 pgclass = table_open(RelationRelationId, AccessShareLock);
1062
1063 scan = table_beginscan_catalog(pgclass, 0, NULL);
1064
1065 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1066 {
1067 Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
1068 MemoryContext oldcontext;
1069 Oid relid = classForm->oid;
1070
1071 /*
1072 * We include partitioned tables here; depending on which operation is
1073 * to be performed, caller will decide whether to process or ignore
1074 * them.
1075 */
1076 if (classForm->relkind != RELKIND_RELATION &&
1077 classForm->relkind != RELKIND_MATVIEW &&
1078 classForm->relkind != RELKIND_PARTITIONED_TABLE)
1079 continue;
1080
1081 /* check permissions of relation */
1082 if (!vacuum_is_permitted_for_relation(relid, classForm, options))
1083 continue;
1084
1085 /*
1086 * Build VacuumRelation(s) specifying the table OIDs to be processed.
1087 * We omit a RangeVar since it wouldn't be appropriate to complain
1088 * about failure to open one of these relations later.
1089 */
1090 oldcontext = MemoryContextSwitchTo(vac_context);
1091 vacrels = lappend(vacrels, makeVacuumRelation(NULL,
1092 relid,
1093 NIL));
1094 MemoryContextSwitchTo(oldcontext);
1095 }
1096
1097 table_endscan(scan);
1098 table_close(pgclass, AccessShareLock);
1099
1100 return vacrels;
1101 }
1102
1103 /*
1104 * vacuum_get_cutoffs() -- compute OldestXmin and freeze cutoff points
1105 *
1106 * The target relation and VACUUM parameters are our inputs.
1107 *
1108 * Output parameters are the cutoffs that VACUUM caller should use.
1109 *
1110 * Return value indicates if vacuumlazy.c caller should make its VACUUM
1111 * operation aggressive. An aggressive VACUUM must advance relfrozenxid up to
1112 * FreezeLimit (at a minimum), and relminmxid up to MultiXactCutoff (at a
1113 * minimum).
1114 */
1115 bool
1116 vacuum_get_cutoffs(Relation rel, const VacuumParams *params,
1117 struct VacuumCutoffs *cutoffs)
1118 {
1119 int freeze_min_age,
1120 multixact_freeze_min_age,
1121 freeze_table_age,
1122 multixact_freeze_table_age,
1123 effective_multixact_freeze_max_age;
1124 TransactionId nextXID,
1125 safeOldestXmin,
1126 aggressiveXIDCutoff;
1127 MultiXactId nextMXID,
1128 safeOldestMxact,
1129 aggressiveMXIDCutoff;
1130
1131 /* Use mutable copies of freeze age parameters */
1132 freeze_min_age = params->freeze_min_age;
1133 multixact_freeze_min_age = params->multixact_freeze_min_age;
1134 freeze_table_age = params->freeze_table_age;
1135 multixact_freeze_table_age = params->multixact_freeze_table_age;
1136
1137 /* Set pg_class fields in cutoffs */
1138 cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
1139 cutoffs->relminmxid = rel->rd_rel->relminmxid;
1140
1141 /*
1142 * Acquire OldestXmin.
1143 *
1144 * We can always ignore processes running lazy vacuum. This is because we
1145 * use these values only for deciding which tuples we must keep in the
1146 * tables. Since lazy vacuum doesn't write its XID anywhere (usually no
1147 * XID assigned), it's safe to ignore it. In theory it could be
1148 * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
1149 * that only one vacuum process can be working on a particular table at
1150 * any time, and that each vacuum is always an independent transaction.
1151 */
1152 cutoffs->OldestXmin = GetOldestNonRemovableTransactionId(rel);
1153
1154 Assert(TransactionIdIsNormal(cutoffs->OldestXmin));
1155
1156 /* Acquire OldestMxact */
1157 cutoffs->OldestMxact = GetOldestMultiXactId();
1158 Assert(MultiXactIdIsValid(cutoffs->OldestMxact));
1159
1160 /* Acquire next XID/next MXID values used to apply age-based settings */
1161 nextXID = ReadNextTransactionId();
1162 nextMXID = ReadNextMultiXactId();
1163
1164 /*
1165 * Also compute the multixact age for which freezing is urgent. This is
1166 * normally autovacuum_multixact_freeze_max_age, but may be less if we are
1167 * short of multixact member space.
1168 */
1169 effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
1170
1171 /*
1172 * Almost ready to set freeze output parameters; check if OldestXmin or
1173 * OldestMxact are held back to an unsafe degree before we start on that
1174 */
1175 safeOldestXmin = nextXID - autovacuum_freeze_max_age;
1176 if (!TransactionIdIsNormal(safeOldestXmin))
1177 safeOldestXmin = FirstNormalTransactionId;
1178 safeOldestMxact = nextMXID - effective_multixact_freeze_max_age;
1179 if (safeOldestMxact < FirstMultiXactId)
1180 safeOldestMxact = FirstMultiXactId;
1181 if (TransactionIdPrecedes(cutoffs->OldestXmin, safeOldestXmin))
1182 ereport(WARNING,
1183 (errmsg("cutoff for removing and freezing tuples is far in the past"),
1184 errhint("Close open transactions soon to avoid wraparound problems.\n"
1185 "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1186 if (MultiXactIdPrecedes(cutoffs->OldestMxact, safeOldestMxact))
1187 ereport(WARNING,
1188 (errmsg("cutoff for freezing multixacts is far in the past"),
1189 errhint("Close open transactions soon to avoid wraparound problems.\n"
1190 "You might also need to commit or roll back old prepared transactions.")));
1191
1192 /*
1193 * Determine the minimum freeze age to use: as specified by the caller, or
1194 * vacuum_freeze_min_age, but in any case not more than half
1195 * autovacuum_freeze_max_age, so that autovacuums to prevent XID
1196 * wraparound won't occur too frequently.
1197 */
1198 if (freeze_min_age < 0)
1199 freeze_min_age = vacuum_freeze_min_age;
1200 freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
1201 Assert(freeze_min_age >= 0);
1202
1203 /* Compute FreezeLimit, being careful to generate a normal XID */
1204 cutoffs->FreezeLimit = nextXID - freeze_min_age;
1205 if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
1206 cutoffs->FreezeLimit = FirstNormalTransactionId;
1207 /* FreezeLimit must always be <= OldestXmin */
1208 if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
1209 cutoffs->FreezeLimit = cutoffs->OldestXmin;
1210
1211 /*
1212 * Determine the minimum multixact freeze age to use: as specified by
1213 * caller, or vacuum_multixact_freeze_min_age, but in any case not more
1214 * than half effective_multixact_freeze_max_age, so that autovacuums to
1215 * prevent MultiXact wraparound won't occur too frequently.
1216 */
1217 if (multixact_freeze_min_age < 0)
1218 multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
1219 multixact_freeze_min_age = Min(multixact_freeze_min_age,
1220 effective_multixact_freeze_max_age / 2);
1221 Assert(multixact_freeze_min_age >= 0);
1222
1223 /* Compute MultiXactCutoff, being careful to generate a valid value */
1224 cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
1225 if (cutoffs->MultiXactCutoff < FirstMultiXactId)
1226 cutoffs->MultiXactCutoff = FirstMultiXactId;
1227 /* MultiXactCutoff must always be <= OldestMxact */
1228 if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
1229 cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
1230
1231 /*
1232 * Finally, figure out if caller needs to do an aggressive VACUUM or not.
1233 *
1234 * Determine the table freeze age to use: as specified by the caller, or
1235 * the value of the vacuum_freeze_table_age GUC, but in any case not more
1236 * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
1237 * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
1238 * anti-wraparound autovacuum is launched.
1239 */
1240 if (freeze_table_age < 0)
1241 freeze_table_age = vacuum_freeze_table_age;
1242 freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
1243 Assert(freeze_table_age >= 0);
1244 aggressiveXIDCutoff = nextXID - freeze_table_age;
1245 if (!TransactionIdIsNormal(aggressiveXIDCutoff))
1246 aggressiveXIDCutoff = FirstNormalTransactionId;
1247 if (TransactionIdPrecedesOrEquals(cutoffs->relfrozenxid,
1248 aggressiveXIDCutoff))
1249 return true;
1250
1251 /*
1252 * Similar to the above, determine the table freeze age to use for
1253 * multixacts: as specified by the caller, or the value of the
1254 * vacuum_multixact_freeze_table_age GUC, but in any case not more than
1255 * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
1256 * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
1257 * multixacts before anti-wraparound autovacuum is launched.
1258 */
1259 if (multixact_freeze_table_age < 0)
1260 multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
1261 multixact_freeze_table_age =
1262 Min(multixact_freeze_table_age,
1263 effective_multixact_freeze_max_age * 0.95);
1264 Assert(multixact_freeze_table_age >= 0);
1265 aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
1266 if (aggressiveMXIDCutoff < FirstMultiXactId)
1267 aggressiveMXIDCutoff = FirstMultiXactId;
1268 if (MultiXactIdPrecedesOrEquals(cutoffs->relminmxid,
1269 aggressiveMXIDCutoff))
1270 return true;
1271
1272 /* Non-aggressive VACUUM */
1273 return false;
1274 }
1275
1276 /*
1277 * vacuum_xid_failsafe_check() -- Used by VACUUM's wraparound failsafe
1278 * mechanism to determine if its table's relfrozenxid and relminmxid are now
1279 * dangerously far in the past.
1280 *
1281 * When we return true, VACUUM caller triggers the failsafe.
1282 */
1283 bool
1284 vacuum_xid_failsafe_check(const struct VacuumCutoffs *cutoffs)
1285 {
1286 TransactionId relfrozenxid = cutoffs->relfrozenxid;
1287 MultiXactId relminmxid = cutoffs->relminmxid;
1288 TransactionId xid_skip_limit;
1289 MultiXactId multi_skip_limit;
1290 int skip_index_vacuum;
1291
1292 Assert(TransactionIdIsNormal(relfrozenxid));
1293 Assert(MultiXactIdIsValid(relminmxid));
1294
1295 /*
1296 * Determine the index skipping age to use. In any case no less than
1297 * autovacuum_freeze_max_age * 1.05.
1298 */
1299 skip_index_vacuum = Max(vacuum_failsafe_age, autovacuum_freeze_max_age * 1.05);
1300
1301 xid_skip_limit = ReadNextTransactionId() - skip_index_vacuum;
1302 if (!TransactionIdIsNormal(xid_skip_limit))
1303 xid_skip_limit = FirstNormalTransactionId;
1304
1305 if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
1306 {
1307 /* The table's relfrozenxid is too old */
1308 return true;
1309 }
1310
1311 /*
1312 * Similar to above, determine the index skipping age to use for
1313 * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
1314 * 1.05.
1315 */
1316 skip_index_vacuum = Max(vacuum_multixact_failsafe_age,
1317 autovacuum_multixact_freeze_max_age * 1.05);
1318
1319 multi_skip_limit = ReadNextMultiXactId() - skip_index_vacuum;
1320 if (multi_skip_limit < FirstMultiXactId)
1321 multi_skip_limit = FirstMultiXactId;
1322
1323 if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
1324 {
1325 /* The table's relminmxid is too old */
1326 return true;
1327 }
1328
1329 return false;
1330 }
1331
1332 /*
1333 * vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
1334 *
1335 * If we scanned the whole relation then we should just use the count of
1336 * live tuples seen; but if we did not, we should not blindly extrapolate
1337 * from that number, since VACUUM may have scanned a quite nonrandom
1338 * subset of the table. When we have only partial information, we take
1339 * the old value of pg_class.reltuples/pg_class.relpages as a measurement
1340 * of the tuple density in the unscanned pages.
1341 *
1342 * Note: scanned_tuples should count only *live* tuples, since
1343 * pg_class.reltuples is defined that way.
1344 */
1345 double
1346 vac_estimate_reltuples(Relation relation,
1347 BlockNumber total_pages,
1348 BlockNumber scanned_pages,
1349 double scanned_tuples)
1350 {
1351 BlockNumber old_rel_pages = relation->rd_rel->relpages;
1352 double old_rel_tuples = relation->rd_rel->reltuples;
1353 double old_density;
1354 double unscanned_pages;
1355 double total_tuples;
1356
1357 /* If we did scan the whole table, just use the count as-is */
1358 if (scanned_pages >= total_pages)
1359 return scanned_tuples;
1360
1361 /*
1362 * When successive VACUUM commands scan the same few pages again and
1363 * again, without anything from the table really changing, there is a risk
1364 * that our beliefs about tuple density will gradually become distorted.
1365 * This might be caused by vacuumlazy.c implementation details, such as
1366 * its tendency to always scan the last heap page. Handle that here.
1367 *
1368 * If the relation is _exactly_ the same size according to the existing
1369 * pg_class entry, and only a few of its pages (less than 2%) were
1370 * scanned, keep the existing value of reltuples. Also keep the existing
1371 * value when only a subset of rel's pages <= a single page were scanned.
1372 *
1373 * (Note: we might be returning -1 here.)
1374 */
1375 if (old_rel_pages == total_pages &&
1376 scanned_pages < (double) total_pages * 0.02)
1377 return old_rel_tuples;
1378 if (scanned_pages <= 1)
1379 return old_rel_tuples;
1380
1381 /*
1382 * If old density is unknown, we can't do much except scale up
1383 * scanned_tuples to match total_pages.
1384 */
1385 if (old_rel_tuples < 0 || old_rel_pages == 0)
1386 return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1387
1388 /*
1389 * Okay, we've covered the corner cases. The normal calculation is to
1390 * convert the old measurement to a density (tuples per page), then
1391 * estimate the number of tuples in the unscanned pages using that figure,
1392 * and finally add on the number of tuples in the scanned pages.
1393 */
1394 old_density = old_rel_tuples / old_rel_pages;
1395 unscanned_pages = (double) total_pages - (double) scanned_pages;
1396 total_tuples = old_density * unscanned_pages + scanned_tuples;
1397 return floor(total_tuples + 0.5);
1398 }
1399
1400
1401 /*
1402 * vac_update_relstats() -- update statistics for one relation
1403 *
1404 * Update the whole-relation statistics that are kept in its pg_class
1405 * row. There are additional stats that will be updated if we are
1406 * doing ANALYZE, but we always update these stats. This routine works
1407 * for both index and heap relation entries in pg_class.
1408 *
1409 * We violate transaction semantics here by overwriting the rel's
1410 * existing pg_class tuple with the new values. This is reasonably
1411 * safe as long as we're sure that the new values are correct whether or
1412 * not this transaction commits. The reason for doing this is that if
1413 * we updated these tuples in the usual way, vacuuming pg_class itself
1414 * wouldn't work very well --- by the time we got done with a vacuum
1415 * cycle, most of the tuples in pg_class would've been obsoleted. Of
1416 * course, this only works for fixed-size not-null columns, but these are.
1417 *
1418 * Another reason for doing it this way is that when we are in a lazy
1419 * VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
1420 * Somebody vacuuming pg_class might think they could delete a tuple
1421 * marked with xmin = our xid.
1422 *
1423 * In addition to fundamentally nontransactional statistics such as
1424 * relpages and relallvisible, we try to maintain certain lazily-updated
1425 * DDL flags such as relhasindex, by clearing them if no longer correct.
1426 * It's safe to do this in VACUUM, which can't run in parallel with
1427 * CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
1428 * However, it's *not* safe to do it in an ANALYZE that's within an
1429 * outer transaction, because for example the current transaction might
1430 * have dropped the last index; then we'd think relhasindex should be
1431 * cleared, but if the transaction later rolls back this would be wrong.
1432 * So we refrain from updating the DDL flags if we're inside an outer
1433 * transaction. This is OK since postponing the flag maintenance is
1434 * always allowable.
1435 *
1436 * Note: num_tuples should count only *live* tuples, since
1437 * pg_class.reltuples is defined that way.
1438 *
1439 * This routine is shared by VACUUM and ANALYZE.
1440 */
1441 void
1442 vac_update_relstats(Relation relation,
1443 BlockNumber num_pages, double num_tuples,
1444 BlockNumber num_all_visible_pages,
1445 BlockNumber num_all_frozen_pages,
1446 bool hasindex, TransactionId frozenxid,
1447 MultiXactId minmulti,
1448 bool *frozenxid_updated, bool *minmulti_updated,
1449 bool in_outer_xact)
1450 {
1451 Oid relid = RelationGetRelid(relation);
1452 Relation rd;
1453 ScanKeyData key[1];
1454 HeapTuple ctup;
1455 void *inplace_state;
1456 Form_pg_class pgcform;
1457 bool dirty,
1458 futurexid,
1459 futuremxid;
1460 TransactionId oldfrozenxid;
1461 MultiXactId oldminmulti;
1462
1463 rd = table_open(RelationRelationId, RowExclusiveLock);
1464
1465 /* Fetch a copy of the tuple to scribble on */
1466 ScanKeyInit(&key[0],
1467 Anum_pg_class_oid,
1468 BTEqualStrategyNumber, F_OIDEQ,
1469 ObjectIdGetDatum(relid));
1470 systable_inplace_update_begin(rd, ClassOidIndexId, true,
1471 NULL, 1, key, &ctup, &inplace_state);
1472 if (!HeapTupleIsValid(ctup))
1473 elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1474 relid);
1475 pgcform = (Form_pg_class) GETSTRUCT(ctup);
1476
1477 /* Apply statistical updates, if any, to copied tuple */
1478
1479 dirty = false;
1480 if (pgcform->relpages != (int32) num_pages)
1481 {
1482 pgcform->relpages = (int32) num_pages;
1483 dirty = true;
1484 }
1485 if (pgcform->reltuples != (float4) num_tuples)
1486 {
1487 pgcform->reltuples = (float4) num_tuples;
1488 dirty = true;
1489 }
1490 if (pgcform->relallvisible != (int32) num_all_visible_pages)
1491 {
1492 pgcform->relallvisible = (int32) num_all_visible_pages;
1493 dirty = true;
1494 }
1495 if (pgcform->relallfrozen != (int32) num_all_frozen_pages)
1496 {
1497 pgcform->relallfrozen = (int32) num_all_frozen_pages;
1498 dirty = true;
1499 }
1500
1501 /* Apply DDL updates, but not inside an outer transaction (see above) */
1502
1503 if (!in_outer_xact)
1504 {
1505 /*
1506 * If we didn't find any indexes, reset relhasindex.
1507 */
1508 if (pgcform->relhasindex && !hasindex)
1509 {
1510 pgcform->relhasindex = false;
1511 dirty = true;
1512 }
1513
1514 /* We also clear relhasrules and relhastriggers if needed */
1515 if (pgcform->relhasrules && relation->rd_rules == NULL)
1516 {
1517 pgcform->relhasrules = false;
1518 dirty = true;
1519 }
1520 if (pgcform->relhastriggers && relation->trigdesc == NULL)
1521 {
1522 pgcform->relhastriggers = false;
1523 dirty = true;
1524 }
1525 }
1526
1527 /*
1528 * Update relfrozenxid, unless caller passed InvalidTransactionId
1529 * indicating it has no new data.
1530 *
1531 * Ordinarily, we don't let relfrozenxid go backwards. However, if the
1532 * stored relfrozenxid is "in the future" then it seems best to assume
1533 * it's corrupt, and overwrite with the oldest remaining XID in the table.
1534 * This should match vac_update_datfrozenxid() concerning what we consider
1535 * to be "in the future".
1536 */
1537 oldfrozenxid = pgcform->relfrozenxid;
1538 futurexid = false;
1539 if (frozenxid_updated)
1540 *frozenxid_updated = false;
1541 if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
1542 {
1543 bool update = false;
1544
1545 if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
1546 update = true;
1547 else if (TransactionIdPrecedes(ReadNextTransactionId(), oldfrozenxid))
1548 futurexid = update = true;
1549
1550 if (update)
1551 {
1552 pgcform->relfrozenxid = frozenxid;
1553 dirty = true;
1554 if (frozenxid_updated)
1555 *frozenxid_updated = true;
1556 }
1557 }
1558
1559 /* Similarly for relminmxid */
1560 oldminmulti = pgcform->relminmxid;
1561 futuremxid = false;
1562 if (minmulti_updated)
1563 *minmulti_updated = false;
1564 if (MultiXactIdIsValid(minmulti) && oldminmulti != minmulti)
1565 {
1566 bool update = false;
1567
1568 if (MultiXactIdPrecedes(oldminmulti, minmulti))
1569 update = true;
1570 else if (MultiXactIdPrecedes(ReadNextMultiXactId(), oldminmulti))
1571 futuremxid = update = true;
1572
1573 if (update)
1574 {
1575 pgcform->relminmxid = minmulti;
1576 dirty = true;
1577 if (minmulti_updated)
1578 *minmulti_updated = true;
1579 }
1580 }
1581
1582 /* If anything changed, write out the tuple. */
1583 if (dirty)
1584 systable_inplace_update_finish(inplace_state, ctup);
1585 else
1586 systable_inplace_update_cancel(inplace_state);
1587
1588 table_close(rd, RowExclusiveLock);
1589
1590 if (futurexid)
1591 ereport(WARNING,
1592 (errcode(ERRCODE_DATA_CORRUPTED),
1593 errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
1594 oldfrozenxid, frozenxid,
1595 RelationGetRelationName(relation))));
1596 if (futuremxid)
1597 ereport(WARNING,
1598 (errcode(ERRCODE_DATA_CORRUPTED),
1599 errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
1600 oldminmulti, minmulti,
1601 RelationGetRelationName(relation))));
1602 }
1603
1604
1605 /*
1606 * vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
1607 *
1608 * Update pg_database's datfrozenxid entry for our database to be the
1609 * minimum of the pg_class.relfrozenxid values.
1610 *
1611 * Similarly, update our datminmxid to be the minimum of the
1612 * pg_class.relminmxid values.
1613 *
1614 * If we are able to advance either pg_database value, also try to
1615 * truncate pg_xact and pg_multixact.
1616 *
1617 * We violate transaction semantics here by overwriting the database's
1618 * existing pg_database tuple with the new values. This is reasonably
1619 * safe since the new values are correct whether or not this transaction
1620 * commits. As with vac_update_relstats, this avoids leaving dead tuples
1621 * behind after a VACUUM.
1622 */
1623 void
1624 vac_update_datfrozenxid(void)
1625 {
1626 HeapTuple tuple;
1627 Form_pg_database dbform;
1628 Relation relation;
1629 SysScanDesc scan;
1630 HeapTuple classTup;
1631 TransactionId newFrozenXid;
1632 MultiXactId newMinMulti;
1633 TransactionId lastSaneFrozenXid;
1634 MultiXactId lastSaneMinMulti;
1635 bool bogus = false;
1636 bool dirty = false;
1637 ScanKeyData key[1];
1638 void *inplace_state;
1639
1640 /*
1641 * Restrict this task to one backend per database. This avoids race
1642 * conditions that would move datfrozenxid or datminmxid backward. It
1643 * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
1644 * datfrozenxid passed to an earlier vac_truncate_clog() call.
1645 */
1646 LockDatabaseFrozenIds(ExclusiveLock);
1647
1648 /*
1649 * Initialize the "min" calculation with
1650 * GetOldestNonRemovableTransactionId(), which is a reasonable
1651 * approximation to the minimum relfrozenxid for not-yet-committed
1652 * pg_class entries for new tables; see AddNewRelationTuple(). So we
1653 * cannot produce a wrong minimum by starting with this.
1654 */
1655 newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
1656
1657 /*
1658 * Similarly, initialize the MultiXact "min" with the value that would be
1659 * used on pg_class for new tables. See AddNewRelationTuple().
1660 */
1661 newMinMulti = GetOldestMultiXactId();
1662
1663 /*
1664 * Identify the latest relfrozenxid and relminmxid values that we could
1665 * validly see during the scan. These are conservative values, but it's
1666 * not really worth trying to be more exact.
1667 */
1668 lastSaneFrozenXid = ReadNextTransactionId();
1669 lastSaneMinMulti = ReadNextMultiXactId();
1670
1671 /*
1672 * We must seqscan pg_class to find the minimum Xid, because there is no
1673 * index that can help us here.
1674 *
1675 * See vac_truncate_clog() for the race condition to prevent.
1676 */
1677 relation = table_open(RelationRelationId, AccessShareLock);
1678
1679 scan = systable_beginscan(relation, InvalidOid, false,
1680 NULL, 0, NULL);
1681
1682 while ((classTup = systable_getnext(scan)) != NULL)
1683 {
1684 volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1685 TransactionId relfrozenxid = classForm->relfrozenxid;
1686 TransactionId relminmxid = classForm->relminmxid;
1687
1688 /*
1689 * Only consider relations able to hold unfrozen XIDs (anything else
1690 * should have InvalidTransactionId in relfrozenxid anyway).
1691 */
1692 if (classForm->relkind != RELKIND_RELATION &&
1693 classForm->relkind != RELKIND_MATVIEW &&
1694 classForm->relkind != RELKIND_TOASTVALUE)
1695 {
1696 Assert(!TransactionIdIsValid(relfrozenxid));
1697 Assert(!MultiXactIdIsValid(relminmxid));
1698 continue;
1699 }
1700
1701 /*
1702 * Some table AMs might not need per-relation xid / multixid horizons.
1703 * It therefore seems reasonable to allow relfrozenxid and relminmxid
1704 * to not be set (i.e. set to their respective Invalid*Id)
1705 * independently. Thus validate and compute horizon for each only if
1706 * set.
1707 *
1708 * If things are working properly, no relation should have a
1709 * relfrozenxid or relminmxid that is "in the future". However, such
1710 * cases have been known to arise due to bugs in pg_upgrade. If we
1711 * see any entries that are "in the future", chicken out and don't do
1712 * anything. This ensures we won't truncate clog & multixact SLRUs
1713 * before those relations have been scanned and cleaned up.
1714 */
1715
1716 if (TransactionIdIsValid(relfrozenxid))
1717 {
1718 Assert(TransactionIdIsNormal(relfrozenxid));
1719
1720 /* check for values in the future */
1721 if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
1722 {
1723 bogus = true;
1724 break;
1725 }
1726
1727 /* determine new horizon */
1728 if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1729 newFrozenXid = relfrozenxid;
1730 }
1731
1732 if (MultiXactIdIsValid(relminmxid))
1733 {
1734 /* check for values in the future */
1735 if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
1736 {
1737 bogus = true;
1738 break;
1739 }
1740
1741 /* determine new horizon */
1742 if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1743 newMinMulti = relminmxid;
1744 }
1745 }
1746
1747 /* we're done with pg_class */
1748 systable_endscan(scan);
1749 table_close(relation, AccessShareLock);
1750
1751 /* chicken out if bogus data found */
1752 if (bogus)
1753 return;
1754
1755 Assert(TransactionIdIsNormal(newFrozenXid));
1756 Assert(MultiXactIdIsValid(newMinMulti));
1757
1758 /* Now fetch the pg_database tuple we need to update. */
1759 relation = table_open(DatabaseRelationId, RowExclusiveLock);
1760
1761 /*
1762 * Fetch a copy of the tuple to scribble on. We could check the syscache
1763 * tuple first. If that concluded !dirty, we'd avoid waiting on
1764 * concurrent heap_update() and would avoid exclusive-locking the buffer.
1765 * For now, don't optimize that.
1766 */
1767 ScanKeyInit(&key[0],
1768 Anum_pg_database_oid,
1769 BTEqualStrategyNumber, F_OIDEQ,
1770 ObjectIdGetDatum(MyDatabaseId));
1771
1772 systable_inplace_update_begin(relation, DatabaseOidIndexId, true,
1773 NULL, 1, key, &tuple, &inplace_state);
1774
1775 if (!HeapTupleIsValid(tuple))
1776 elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1777
1778 dbform = (Form_pg_database) GETSTRUCT(tuple);
1779
1780 /*
1781 * As in vac_update_relstats(), we ordinarily don't want to let
1782 * datfrozenxid go backward; but if it's "in the future" then it must be
1783 * corrupt and it seems best to overwrite it.
1784 */
1785 if (dbform->datfrozenxid != newFrozenXid &&
1786 (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1787 TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1788 {
1789 dbform->datfrozenxid = newFrozenXid;
1790 dirty = true;
1791 }
1792 else
1793 newFrozenXid = dbform->datfrozenxid;
1794
1795 /* Ditto for datminmxid */
1796 if (dbform->datminmxid != newMinMulti &&
1797 (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1798 MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1799 {
1800 dbform->datminmxid = newMinMulti;
1801 dirty = true;
1802 }
1803 else
1804 newMinMulti = dbform->datminmxid;
1805
1806 if (dirty)
1807 systable_inplace_update_finish(inplace_state, tuple);
1808 else
1809 systable_inplace_update_cancel(inplace_state);
1810
1811 heap_freetuple(tuple);
1812 table_close(relation, RowExclusiveLock);
1813
1814 /*
1815 * If we were able to advance datfrozenxid or datminmxid, see if we can
1816 * truncate pg_xact and/or pg_multixact. Also do it if the shared
1817 * XID-wrap-limit info is stale, since this action will update that too.
1818 */
1819 if (dirty || ForceTransactionIdLimitUpdate())
1820 vac_truncate_clog(newFrozenXid, newMinMulti,
1821 lastSaneFrozenXid, lastSaneMinMulti);
1822 }
1823
1824
1825 /*
1826 * vac_truncate_clog() -- attempt to truncate the commit log
1827 *
1828 * Scan pg_database to determine the system-wide oldest datfrozenxid,
1829 * and use it to truncate the transaction commit log (pg_xact).
1830 * Also update the XID wrap limit info maintained by varsup.c.
1831 * Likewise for datminmxid.
1832 *
1833 * The passed frozenXID and minMulti are the updated values for my own
1834 * pg_database entry. They're used to initialize the "min" calculations.
1835 * The caller also passes the "last sane" XID and MXID, since it has
1836 * those at hand already.
1837 *
1838 * This routine is only invoked when we've managed to change our
1839 * DB's datfrozenxid/datminmxid values, or we found that the shared
1840 * XID-wrap-limit info is stale.
1841 */
1842 static void
1843 vac_truncate_clog(TransactionId frozenXID,
1844 MultiXactId minMulti,
1845 TransactionId lastSaneFrozenXid,
1846 MultiXactId lastSaneMinMulti)
1847 {
1848 TransactionId nextXID = ReadNextTransactionId();
1849 Relation relation;
1850 TableScanDesc scan;
1851 HeapTuple tuple;
1852 Oid oldestxid_datoid;
1853 Oid minmulti_datoid;
1854 bool bogus = false;
1855 bool frozenAlreadyWrapped = false;
1856
1857 /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
1858 LWLockAcquire(WrapLimitsVacuumLock, LW_EXCLUSIVE);
1859
1860 /* init oldest datoids to sync with my frozenXID/minMulti values */
1861 oldestxid_datoid = MyDatabaseId;
1862 minmulti_datoid = MyDatabaseId;
1863
1864 /*
1865 * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1866 *
1867 * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1868 * the values could change while we look at them. Fetch each one just
1869 * once to ensure sane behavior of the comparison logic. (Here, as in
1870 * many other places, we assume that fetching or updating an XID in shared
1871 * storage is atomic.)
1872 *
1873 * Note: we need not worry about a race condition with new entries being
1874 * inserted by CREATE DATABASE. Any such entry will have a copy of some
1875 * existing DB's datfrozenxid, and that source DB cannot be ours because
1876 * of the interlock against copying a DB containing an active backend.
1877 * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1878 * concurrently modify the datfrozenxid's of different databases, the
1879 * worst possible outcome is that pg_xact is not truncated as aggressively
1880 * as it could be.
1881 */
1882 relation = table_open(DatabaseRelationId, AccessShareLock);
1883
1884 scan = table_beginscan_catalog(relation, 0, NULL);
1885
1886 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1887 {
1888 volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1889 TransactionId datfrozenxid = dbform->datfrozenxid;
1890 TransactionId datminmxid = dbform->datminmxid;
1891
1892 Assert(TransactionIdIsNormal(datfrozenxid));
1893 Assert(MultiXactIdIsValid(datminmxid));
1894
1895 /*
1896 * If database is in the process of getting dropped, or has been
1897 * interrupted while doing so, no connections to it are possible
1898 * anymore. Therefore we don't need to take it into account here.
1899 * Which is good, because it can't be processed by autovacuum either.
1900 */
1901 if (database_is_invalid_form((Form_pg_database) dbform))
1902 {
1903 elog(DEBUG2,
1904 "skipping invalid database \"%s\" while computing relfrozenxid",
1905 NameStr(dbform->datname));
1906 continue;
1907 }
1908
1909 /*
1910 * If things are working properly, no database should have a
1911 * datfrozenxid or datminmxid that is "in the future". However, such
1912 * cases have been known to arise due to bugs in pg_upgrade. If we
1913 * see any entries that are "in the future", chicken out and don't do
1914 * anything. This ensures we won't truncate clog before those
1915 * databases have been scanned and cleaned up. (We will issue the
1916 * "already wrapped" warning if appropriate, though.)
1917 */
1918 if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1919 MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1920 bogus = true;
1921
1922 if (TransactionIdPrecedes(nextXID, datfrozenxid))
1923 frozenAlreadyWrapped = true;
1924 else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1925 {
1926 frozenXID = datfrozenxid;
1927 oldestxid_datoid = dbform->oid;
1928 }
1929
1930 if (MultiXactIdPrecedes(datminmxid, minMulti))
1931 {
1932 minMulti = datminmxid;
1933 minmulti_datoid = dbform->oid;
1934 }
1935 }
1936
1937 table_endscan(scan);
1938
1939 table_close(relation, AccessShareLock);
1940
1941 /*
1942 * Do not truncate CLOG if we seem to have suffered wraparound already;
1943 * the computed minimum XID might be bogus. This case should now be
1944 * impossible due to the defenses in GetNewTransactionId, but we keep the
1945 * test anyway.
1946 */
1947 if (frozenAlreadyWrapped)
1948 {
1949 ereport(WARNING,
1950 (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1951 errdetail("You might have already suffered transaction-wraparound data loss.")));
1952 LWLockRelease(WrapLimitsVacuumLock);
1953 return;
1954 }
1955
1956 /* chicken out if data is bogus in any other way */
1957 if (bogus)
1958 {
1959 LWLockRelease(WrapLimitsVacuumLock);
1960 return;
1961 }
1962
1963 /*
1964 * Freeze any old transaction IDs in the async notification queue before
1965 * CLOG truncation.
1966 */
1967 AsyncNotifyFreezeXids(frozenXID);
1968
1969 /*
1970 * Advance the oldest value for commit timestamps before truncating, so
1971 * that if a user requests a timestamp for a transaction we're truncating
1972 * away right after this point, they get NULL instead of an ugly "file not
1973 * found" error from slru.c. This doesn't matter for xact/multixact
1974 * because they are not subject to arbitrary lookups from users.
1975 */
1976 AdvanceOldestCommitTsXid(frozenXID);
1977
1978 /*
1979 * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1980 */
1981 TruncateCLOG(frozenXID, oldestxid_datoid);
1982 TruncateCommitTs(frozenXID);
1983 TruncateMultiXact(minMulti, minmulti_datoid);
1984
1985 /*
1986 * Update the wrap limit for GetNewTransactionId and creation of new
1987 * MultiXactIds. Note: these functions will also signal the postmaster
1988 * for an(other) autovac cycle if needed. XXX should we avoid possibly
1989 * signaling twice?
1990 */
1991 SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1992 SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1993
1994 LWLockRelease(WrapLimitsVacuumLock);
1995 }
1996
1997
1998 /*
1999 * vacuum_rel() -- vacuum one heap relation
2000 *
2001 * relid identifies the relation to vacuum. If relation is supplied,
2002 * use the name therein for reporting any failure to open/lock the rel;
2003 * do not use it once we've successfully opened the rel, since it might
2004 * be stale.
2005 *
2006 * Returns true if it's okay to proceed with a requested ANALYZE
2007 * operation on this table.
2008 *
2009 * Doing one heap at a time incurs extra overhead, since we need to
2010 * check that the heap exists again just before we vacuum it. The
2011 * reason that we do this is so that vacuuming can be spread across
2012 * many small transactions. Otherwise, two-phase locking would require
2013 * us to lock the entire database during one pass of the vacuum cleaner.
2014 *
2015 * At entry and exit, we are not inside a transaction.
2016 */
2017 static bool
2018 vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
2019 BufferAccessStrategy bstrategy)
2020 {
2021 LOCKMODE lmode;
2022 Relation rel;
2023 LockRelId lockrelid;
2024 Oid priv_relid;
2025 Oid toast_relid;
2026 Oid save_userid;
2027 int save_sec_context;
2028 int save_nestlevel;
2029 VacuumParams toast_vacuum_params;
2030
2031 Assert(params != NULL);
2032
2033 /*
2034 * This function scribbles on the parameters, so make a copy early to
2035 * avoid affecting the TOAST table (if we do end up recursing to it).
2036 */
2037 memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
2038
2039 /* Begin a transaction for vacuuming this relation */
2040 StartTransactionCommand();
2041
2042 if (!(params->options & VACOPT_FULL))
2043 {
2044 /*
2045 * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
2046 * other concurrent VACUUMs know that they can ignore this one while
2047 * determining their OldestXmin. (The reason we don't set it during a
2048 * full VACUUM is exactly that we may have to run user-defined
2049 * functions for functional indexes, and we want to make sure that if
2050 * they use the snapshot set above, any tuples it requires can't get
2051 * removed from other tables. An index function that depends on the
2052 * contents of other tables is arguably broken, but we won't break it
2053 * here by violating transaction semantics.)
2054 *
2055 * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
2056 * autovacuum; it's used to avoid canceling a vacuum that was invoked
2057 * in an emergency.
2058 *
2059 * Note: these flags remain set until CommitTransaction or
2060 * AbortTransaction. We don't want to clear them until we reset
2061 * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
2062 * might appear to go backwards, which is probably Not Good. (We also
2063 * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
2064 * xmin doesn't become visible ahead of setting the flag.)
2065 */
2066 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2067 MyProc->statusFlags |= PROC_IN_VACUUM;
2068 if (params->is_wraparound)
2069 MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND;
2070 ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
2071 LWLockRelease(ProcArrayLock);
2072 }
2073
2074 /*
2075 * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
2076 * cutoff xids in local memory wrapping around, and to have updated xmin
2077 * horizons.
2078 */
2079 PushActiveSnapshot(GetTransactionSnapshot());
2080
2081 /*
2082 * Check for user-requested abort. Note we want this to be inside a
2083 * transaction, so xact.c doesn't issue useless WARNING.
2084 */
2085 CHECK_FOR_INTERRUPTS();
2086
2087 /*
2088 * Determine the type of lock we want --- hard exclusive lock for a FULL
2089 * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
2090 * way, we can be sure that no other backend is vacuuming the same table.
2091 */
2092 lmode = (params->options & VACOPT_FULL) ?
2093 AccessExclusiveLock : ShareUpdateExclusiveLock;
2094
2095 /* open the relation and get the appropriate lock on it */
2096 rel = vacuum_open_relation(relid, relation, params->options,
2097 params->log_min_duration >= 0, lmode);
2098
2099 /* leave if relation could not be opened or locked */
2100 if (!rel)
2101 {
2102 PopActiveSnapshot();
2103 CommitTransactionCommand();
2104 return false;
2105 }
2106
2107 /*
2108 * When recursing to a TOAST table, check privileges on the parent. NB:
2109 * This is only safe to do because we hold a session lock on the main
2110 * relation that prevents concurrent deletion.
2111 */
2112 if (OidIsValid(params->toast_parent))
2113 priv_relid = params->toast_parent;
2114 else
2115 priv_relid = RelationGetRelid(rel);
2116
2117 /*
2118 * Check if relation needs to be skipped based on privileges. This check
2119 * happens also when building the relation list to vacuum for a manual
2120 * operation, and needs to be done additionally here as VACUUM could
2121 * happen across multiple transactions where privileges could have changed
2122 * in-between. Make sure to only generate logs for VACUUM in this case.
2123 */
2124 if (!vacuum_is_permitted_for_relation(priv_relid,
2125 rel->rd_rel,
2126 params->options & ~VACOPT_ANALYZE))
2127 {
2128 relation_close(rel, lmode);
2129 PopActiveSnapshot();
2130 CommitTransactionCommand();
2131 return false;
2132 }
2133
2134 /*
2135 * Check that it's of a vacuumable relkind.
2136 */
2137 if (rel->rd_rel->relkind != RELKIND_RELATION &&
2138 rel->rd_rel->relkind != RELKIND_MATVIEW &&
2139 rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
2140 rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2141 {
2142 ereport(WARNING,
2143 (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
2144 RelationGetRelationName(rel))));
2145 relation_close(rel, lmode);
2146 PopActiveSnapshot();
2147 CommitTransactionCommand();
2148 return false;
2149 }
2150
2151 /*
2152 * Silently ignore tables that are temp tables of other backends ---
2153 * trying to vacuum these will lead to great unhappiness, since their
2154 * contents are probably not up-to-date on disk. (We don't throw a
2155 * warning here; it would just lead to chatter during a database-wide
2156 * VACUUM.)
2157 */
2158 if (RELATION_IS_OTHER_TEMP(rel))
2159 {
2160 relation_close(rel, lmode);
2161 PopActiveSnapshot();
2162 CommitTransactionCommand();
2163 return false;
2164 }
2165
2166 /*
2167 * Silently ignore partitioned tables as there is no work to be done. The
2168 * useful work is on their child partitions, which have been queued up for
2169 * us separately.
2170 */
2171 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2172 {
2173 relation_close(rel, lmode);
2174 PopActiveSnapshot();
2175 CommitTransactionCommand();
2176 /* It's OK to proceed with ANALYZE on this table */
2177 return true;
2178 }
2179
2180 /*
2181 * Get a session-level lock too. This will protect our access to the
2182 * relation across multiple transactions, so that we can vacuum the
2183 * relation's TOAST table (if any) secure in the knowledge that no one is
2184 * deleting the parent relation.
2185 *
2186 * NOTE: this cannot block, even if someone else is waiting for access,
2187 * because the lock manager knows that both lock requests are from the
2188 * same process.
2189 */
2190 lockrelid = rel->rd_lockInfo.lockRelId;
2191 LockRelationIdForSession(&lockrelid, lmode);
2192
2193 /*
2194 * Set index_cleanup option based on index_cleanup reloption if it wasn't
2195 * specified in VACUUM command, or when running in an autovacuum worker
2196 */
2197 if (params->index_cleanup == VACOPTVALUE_UNSPECIFIED)
2198 {
2199 StdRdOptIndexCleanup vacuum_index_cleanup;
2200
2201 if (rel->rd_options == NULL)
2202 vacuum_index_cleanup = STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO;
2203 else
2204 vacuum_index_cleanup =
2205 ((StdRdOptions *) rel->rd_options)->vacuum_index_cleanup;
2206
2207 if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO)
2208 params->index_cleanup = VACOPTVALUE_AUTO;
2209 else if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON)
2210 params->index_cleanup = VACOPTVALUE_ENABLED;
2211 else
2212 {
2213 Assert(vacuum_index_cleanup ==
2214 STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF);
2215 params->index_cleanup = VACOPTVALUE_DISABLED;
2216 }
2217 }
2218
2219 #ifdef USE_INJECTION_POINTS
2220 if (params->index_cleanup == VACOPTVALUE_AUTO)
2221 INJECTION_POINT("vacuum-index-cleanup-auto", NULL);
2222 else if (params->index_cleanup == VACOPTVALUE_DISABLED)
2223 INJECTION_POINT("vacuum-index-cleanup-disabled", NULL);
2224 else if (params->index_cleanup == VACOPTVALUE_ENABLED)
2225 INJECTION_POINT("vacuum-index-cleanup-enabled", NULL);
2226 #endif
2227
2228 /*
2229 * Check if the vacuum_max_eager_freeze_failure_rate table storage
2230 * parameter was specified. This overrides the GUC value.
2231 */
2232 if (rel->rd_options != NULL &&
2233 ((StdRdOptions *) rel->rd_options)->vacuum_max_eager_freeze_failure_rate >= 0)
2234 params->max_eager_freeze_failure_rate =
2235 ((StdRdOptions *) rel->rd_options)->vacuum_max_eager_freeze_failure_rate;
2236
2237 /*
2238 * Set truncate option based on truncate reloption or GUC if it wasn't
2239 * specified in VACUUM command, or when running in an autovacuum worker
2240 */
2241 if (params->truncate == VACOPTVALUE_UNSPECIFIED)
2242 {
2243 StdRdOptions *opts = (StdRdOptions *) rel->rd_options;
2244
2245 if (opts && opts->vacuum_truncate_set)
2246 {
2247 if (opts->vacuum_truncate)
2248 params->truncate = VACOPTVALUE_ENABLED;
2249 else
2250 params->truncate = VACOPTVALUE_DISABLED;
2251 }
2252 else if (vacuum_truncate)
2253 params->truncate = VACOPTVALUE_ENABLED;
2254 else
2255 params->truncate = VACOPTVALUE_DISABLED;
2256 }
2257
2258 #ifdef USE_INJECTION_POINTS
2259 if (params->truncate == VACOPTVALUE_AUTO)
2260 INJECTION_POINT("vacuum-truncate-auto", NULL);
2261 else if (params->truncate == VACOPTVALUE_DISABLED)
2262 INJECTION_POINT("vacuum-truncate-disabled", NULL);
2263 else if (params->truncate == VACOPTVALUE_ENABLED)
2264 INJECTION_POINT("vacuum-truncate-enabled", NULL);
2265 #endif
2266
2267 /*
2268 * Remember the relation's TOAST relation for later, if the caller asked
2269 * us to process it. In VACUUM FULL, though, the toast table is
2270 * automatically rebuilt by cluster_rel so we shouldn't recurse to it,
2271 * unless PROCESS_MAIN is disabled.
2272 */
2273 if ((params->options & VACOPT_PROCESS_TOAST) != 0 &&
2274 ((params->options & VACOPT_FULL) == 0 ||
2275 (params->options & VACOPT_PROCESS_MAIN) == 0))
2276 toast_relid = rel->rd_rel->reltoastrelid;
2277 else
2278 toast_relid = InvalidOid;
2279
2280 /*
2281 * Switch to the table owner's userid, so that any index functions are run
2282 * as that user. Also lock down security-restricted operations and
2283 * arrange to make GUC variable changes local to this command. (This is
2284 * unnecessary, but harmless, for lazy VACUUM.)
2285 */
2286 GetUserIdAndSecContext(&save_userid, &save_sec_context);
2287 SetUserIdAndSecContext(rel->rd_rel->relowner,
2288 save_sec_context | SECURITY_RESTRICTED_OPERATION);
2289 save_nestlevel = NewGUCNestLevel();
2290 RestrictSearchPath();
2291
2292 /*
2293 * If PROCESS_MAIN is set (the default), it's time to vacuum the main
2294 * relation. Otherwise, we can skip this part. If processing the TOAST
2295 * table is required (e.g., PROCESS_TOAST is set), we force PROCESS_MAIN
2296 * to be set when we recurse to the TOAST table.
2297 */
2298 if (params->options & VACOPT_PROCESS_MAIN)
2299 {
2300 /*
2301 * Do the actual work --- either FULL or "lazy" vacuum
2302 */
2303 if (params->options & VACOPT_FULL)
2304 {
2305 ClusterParams cluster_params = {0};
2306
2307 if ((params->options & VACOPT_VERBOSE) != 0)
2308 cluster_params.options |= CLUOPT_VERBOSE;
2309
2310 /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
2311 cluster_rel(rel, InvalidOid, &cluster_params);
2312 /* cluster_rel closes the relation, but keeps lock */
2313
2314 rel = NULL;
2315 }
2316 else
2317 table_relation_vacuum(rel, params, bstrategy);
2318 }
2319
2320 /* Roll back any GUC changes executed by index functions */
2321 AtEOXact_GUC(false, save_nestlevel);
2322
2323 /* Restore userid and security context */
2324 SetUserIdAndSecContext(save_userid, save_sec_context);
2325
2326 /* all done with this class, but hold lock until commit */
2327 if (rel)
2328 relation_close(rel, NoLock);
2329
2330 /*
2331 * Complete the transaction and free all temporary memory used.
2332 */
2333 PopActiveSnapshot();
2334 CommitTransactionCommand();
2335
2336 /*
2337 * If the relation has a secondary toast rel, vacuum that too while we
2338 * still hold the session lock on the main table. Note however that
2339 * "analyze" will not get done on the toast table. This is good, because
2340 * the toaster always uses hardcoded index access and statistics are
2341 * totally unimportant for toast relations.
2342 */
2343 if (toast_relid != InvalidOid)
2344 {
2345 /*
2346 * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it. Likewise,
2347 * set toast_parent so that the privilege checks are done on the main
2348 * relation. NB: This is only safe to do because we hold a session
2349 * lock on the main relation that prevents concurrent deletion.
2350 */
2351 toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
2352 toast_vacuum_params.toast_parent = relid;
2353
2354 vacuum_rel(toast_relid, NULL, &toast_vacuum_params, bstrategy);
2355 }
2356
2357 /*
2358 * Now release the session-level lock on the main table.
2359 */
2360 UnlockRelationIdForSession(&lockrelid, lmode);
2361
2362 /* Report that we really did it. */
2363 return true;
2364 }
2365
2366
2367 /*
2368 * Open all the vacuumable indexes of the given relation, obtaining the
2369 * specified kind of lock on each. Return an array of Relation pointers for
2370 * the indexes into *Irel, and the number of indexes into *nindexes.
2371 *
2372 * We consider an index vacuumable if it is marked insertable (indisready).
2373 * If it isn't, probably a CREATE INDEX CONCURRENTLY command failed early in
2374 * execution, and what we have is too corrupt to be processable. We will
2375 * vacuum even if the index isn't indisvalid; this is important because in a
2376 * unique index, uniqueness checks will be performed anyway and had better not
2377 * hit dangling index pointers.
2378 */
2379 void
2380 vac_open_indexes(Relation relation, LOCKMODE lockmode,
2381 int *nindexes, Relation **Irel)
2382 {
2383 List *indexoidlist;
2384 ListCell *indexoidscan;
2385 int i;
2386
2387 Assert(lockmode != NoLock);
2388
2389 indexoidlist = RelationGetIndexList(relation);
2390
2391 /* allocate enough memory for all indexes */
2392 i = list_length(indexoidlist);
2393
2394 if (i > 0)
2395 *Irel = (Relation *) palloc(i * sizeof(Relation));
2396 else
2397 *Irel = NULL;
2398
2399 /* collect just the ready indexes */
2400 i = 0;
2401 foreach(indexoidscan, indexoidlist)
2402 {
2403 Oid indexoid = lfirst_oid(indexoidscan);
2404 Relation indrel;
2405
2406 indrel = index_open(indexoid, lockmode);
2407 if (indrel->rd_index->indisready)
2408 (*Irel)[i++] = indrel;
2409 else
2410 index_close(indrel, lockmode);
2411 }
2412
2413 *nindexes = i;
2414
2415 list_free(indexoidlist);
2416 }
2417
2418 /*
2419 * Release the resources acquired by vac_open_indexes. Optionally release
2420 * the locks (say NoLock to keep 'em).
2421 */
2422 void
2423 vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
2424 {
2425 if (Irel == NULL)
2426 return;
2427
2428 while (nindexes--)
2429 {
2430 Relation ind = Irel[nindexes];
2431
2432 index_close(ind, lockmode);
2433 }
2434 pfree(Irel);
2435 }
2436
2437 /*
2438 * vacuum_delay_point --- check for interrupts and cost-based delay.
2439 *
2440 * This should be called in each major loop of VACUUM processing,
2441 * typically once per page processed.
2442 */
2443 void
2444 vacuum_delay_point(bool is_analyze)
2445 {
2446 double msec = 0;
2447
2448 /* Always check for interrupts */
2449 CHECK_FOR_INTERRUPTS();
2450
2451 if (InterruptPending ||
2452 (!VacuumCostActive && !ConfigReloadPending))
2453 return;
2454
2455 /*
2456 * Autovacuum workers should reload the configuration file if requested.
2457 * This allows changes to [autovacuum_]vacuum_cost_limit and
2458 * [autovacuum_]vacuum_cost_delay to take effect while a table is being
2459 * vacuumed or analyzed.
2460 */
2461 if (ConfigReloadPending && AmAutoVacuumWorkerProcess())
2462 {
2463 ConfigReloadPending = false;
2464 ProcessConfigFile(PGC_SIGHUP);
2465 VacuumUpdateCosts();
2466 }
2467
2468 /*
2469 * If we disabled cost-based delays after reloading the config file,
2470 * return.
2471 */
2472 if (!VacuumCostActive)
2473 return;
2474
2475 /*
2476 * For parallel vacuum, the delay is computed based on the shared cost
2477 * balance. See compute_parallel_delay.
2478 */
2479 if (VacuumSharedCostBalance != NULL)
2480 msec = compute_parallel_delay();
2481 else if (VacuumCostBalance >= vacuum_cost_limit)
2482 msec = vacuum_cost_delay * VacuumCostBalance / vacuum_cost_limit;
2483
2484 /* Nap if appropriate */
2485 if (msec > 0)
2486 {
2487 instr_time delay_start;
2488
2489 if (msec > vacuum_cost_delay * 4)
2490 msec = vacuum_cost_delay * 4;
2491
2492 if (track_cost_delay_timing)
2493 INSTR_TIME_SET_CURRENT(delay_start);
2494
2495 pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);
2496 pg_usleep(msec * 1000);
2497 pgstat_report_wait_end();
2498
2499 if (track_cost_delay_timing)
2500 {
2501 instr_time delay_end;
2502 instr_time delay;
2503
2504 INSTR_TIME_SET_CURRENT(delay_end);
2505 INSTR_TIME_SET_ZERO(delay);
2506 INSTR_TIME_ACCUM_DIFF(delay, delay_end, delay_start);
2507
2508 /*
2509 * For parallel workers, we only report the delay time every once
2510 * in a while to avoid overloading the leader with messages and
2511 * interrupts.
2512 */
2513 if (IsParallelWorker())
2514 {
2515 static instr_time last_report_time;
2516 instr_time time_since_last_report;
2517
2518 Assert(!is_analyze);
2519
2520 /* Accumulate the delay time */
2521 parallel_vacuum_worker_delay_ns += INSTR_TIME_GET_NANOSEC(delay);
2522
2523 /* Calculate interval since last report */
2524 INSTR_TIME_SET_ZERO(time_since_last_report);
2525 INSTR_TIME_ACCUM_DIFF(time_since_last_report, delay_end, last_report_time);
2526
2527 /* If we haven't reported in a while, do so now */
2528 if (INSTR_TIME_GET_NANOSEC(time_since_last_report) >=
2529 PARALLEL_VACUUM_DELAY_REPORT_INTERVAL_NS)
2530 {
2531 pgstat_progress_parallel_incr_param(PROGRESS_VACUUM_DELAY_TIME,
2532 parallel_vacuum_worker_delay_ns);
2533
2534 /* Reset variables */
2535 last_report_time = delay_end;
2536 parallel_vacuum_worker_delay_ns = 0;
2537 }
2538 }
2539 else if (is_analyze)
2540 pgstat_progress_incr_param(PROGRESS_ANALYZE_DELAY_TIME,
2541 INSTR_TIME_GET_NANOSEC(delay));
2542 else
2543 pgstat_progress_incr_param(PROGRESS_VACUUM_DELAY_TIME,
2544 INSTR_TIME_GET_NANOSEC(delay));
2545 }
2546
2547 /*
2548 * We don't want to ignore postmaster death during very long vacuums
2549 * with vacuum_cost_delay configured. We can't use the usual
2550 * WaitLatch() approach here because we want microsecond-based sleep
2551 * durations above.
2552 */
2553 if (IsUnderPostmaster && !PostmasterIsAlive())
2554 exit(1);
2555
2556 VacuumCostBalance = 0;
2557
2558 /*
2559 * Balance and update limit values for autovacuum workers. We must do
2560 * this periodically, as the number of workers across which we are
2561 * balancing the limit may have changed.
2562 *
2563 * TODO: There may be better criteria for determining when to do this
2564 * besides "check after napping".
2565 */
2566 AutoVacuumUpdateCostLimit();
2567
2568 /* Might have gotten an interrupt while sleeping */
2569 CHECK_FOR_INTERRUPTS();
2570 }
2571 }
2572
2573 /*
2574 * Computes the vacuum delay for parallel workers.
2575 *
2576 * The basic idea of a cost-based delay for parallel vacuum is to allow each
2577 * worker to sleep in proportion to the share of work it's done. We achieve this
2578 * by allowing all parallel vacuum workers including the leader process to
2579 * have a shared view of cost related parameters (mainly VacuumCostBalance).
2580 * We allow each worker to update it as and when it has incurred any cost and
2581 * then based on that decide whether it needs to sleep. We compute the time
2582 * to sleep for a worker based on the cost it has incurred
2583 * (VacuumCostBalanceLocal) and then reduce the VacuumSharedCostBalance by
2584 * that amount. This avoids putting to sleep those workers which have done less
2585 * I/O than other workers and therefore ensure that workers
2586 * which are doing more I/O got throttled more.
2587 *
2588 * We allow a worker to sleep only if it has performed I/O above a certain
2589 * threshold, which is calculated based on the number of active workers
2590 * (VacuumActiveNWorkers), and the overall cost balance is more than
2591 * VacuumCostLimit set by the system. Testing reveals that we achieve
2592 * the required throttling if we force a worker that has done more than 50%
2593 * of its share of work to sleep.
2594 */
2595 static double
2596 compute_parallel_delay(void)
2597 {
2598 double msec = 0;
2599 uint32 shared_balance;
2600 int nworkers;
2601
2602 /* Parallel vacuum must be active */
2603 Assert(VacuumSharedCostBalance);
2604
2605 nworkers = pg_atomic_read_u32(VacuumActiveNWorkers);
2606
2607 /* At least count itself */
2608 Assert(nworkers >= 1);
2609
2610 /* Update the shared cost balance value atomically */
2611 shared_balance = pg_atomic_add_fetch_u32(VacuumSharedCostBalance, VacuumCostBalance);
2612
2613 /* Compute the total local balance for the current worker */
2614 VacuumCostBalanceLocal += VacuumCostBalance;
2615
2616 if ((shared_balance >= vacuum_cost_limit) &&
2617 (VacuumCostBalanceLocal > 0.5 * ((double) vacuum_cost_limit / nworkers)))
2618 {
2619 /* Compute sleep time based on the local cost balance */
2620 msec = vacuum_cost_delay * VacuumCostBalanceLocal / vacuum_cost_limit;
2621 pg_atomic_sub_fetch_u32(VacuumSharedCostBalance, VacuumCostBalanceLocal);
2622 VacuumCostBalanceLocal = 0;
2623 }
2624
2625 /*
2626 * Reset the local balance as we accumulated it into the shared value.
2627 */
2628 VacuumCostBalance = 0;
2629
2630 return msec;
2631 }
2632
2633 /*
2634 * A wrapper function of defGetBoolean().
2635 *
2636 * This function returns VACOPTVALUE_ENABLED and VACOPTVALUE_DISABLED instead
2637 * of true and false.
2638 */
2639 static VacOptValue
2640 get_vacoptval_from_boolean(DefElem *def)
2641 {
2642 return defGetBoolean(def) ? VACOPTVALUE_ENABLED : VACOPTVALUE_DISABLED;
2643 }
2644
2645 /*
2646 * vac_bulkdel_one_index() -- bulk-deletion for index relation.
2647 *
2648 * Returns bulk delete stats derived from input stats
2649 */
2650 IndexBulkDeleteResult *
2651 vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat,
2652 TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
2653 {
2654 /* Do bulk deletion */
2655 istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
2656 dead_items);
2657
2658 ereport(ivinfo->message_level,
2659 (errmsg("scanned index \"%s\" to remove %" PRId64 " row versions",
2660 RelationGetRelationName(ivinfo->index),
2661 dead_items_info->num_items)));
2662
2663 return istat;
2664 }
2665
2666 /*
2667 * vac_cleanup_one_index() -- do post-vacuum cleanup for index relation.
2668 *
2669 * Returns bulk delete stats derived from input stats
2670 */
2671 IndexBulkDeleteResult *
2672 vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
2673 {
2674 istat = index_vacuum_cleanup(ivinfo, istat);
2675
2676 if (istat)
2677 ereport(ivinfo->message_level,
2678 (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
2679 RelationGetRelationName(ivinfo->index),
2680 istat->num_index_tuples,
2681 istat->num_pages),
2682 errdetail("%.0f index row versions were removed.\n"
2683 "%u index pages were newly deleted.\n"
2684 "%u index pages are currently deleted, of which %u are currently reusable.",
2685 istat->tuples_removed,
2686 istat->pages_newly_deleted,
2687 istat->pages_deleted, istat->pages_free)));
2688
2689 return istat;
2690 }
2691
2692 /*
2693 * vac_tid_reaped() -- is a particular tid deletable?
2694 *
2695 * This has the right signature to be an IndexBulkDeleteCallback.
2696 */
2697 static bool
2698 vac_tid_reaped(ItemPointer itemptr, void *state)
2699 {
2700 TidStore *dead_items = (TidStore *) state;
2701
2702 return TidStoreIsMember(dead_items, itemptr);
2703 }