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