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