Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * vacuumlazy.c
4 : : * Concurrent ("lazy") vacuuming.
5 : : *
6 : : * Heap relations are vacuumed in three main phases. In phase I, vacuum scans
7 : : * relation pages, pruning and freezing tuples and saving dead tuples' TIDs in
8 : : * a TID store. If that TID store fills up or vacuum finishes scanning the
9 : : * relation, it progresses to phase II: index vacuuming. Index vacuuming
10 : : * deletes the dead index entries referenced in the TID store. In phase III,
11 : : * vacuum scans the blocks of the relation referred to by the TIDs in the TID
12 : : * store and reaps the corresponding dead items, freeing that space for future
13 : : * tuples.
14 : : *
15 : : * If there are no indexes or index scanning is disabled, phase II may be
16 : : * skipped. If phase I identified very few dead index entries or if vacuum's
17 : : * failsafe mechanism has triggered (to avoid transaction ID wraparound),
18 : : * vacuum may skip phases II and III.
19 : : *
20 : : * If the TID store fills up in phase I, vacuum suspends phase I and proceeds
21 : : * to phases II and III, cleaning up the dead tuples referenced in the current
22 : : * TID store. This empties the TID store, allowing vacuum to resume phase I.
23 : : *
24 : : * In a way, the phases are more like states in a state machine, but they have
25 : : * been referred to colloquially as phases for so long that they are referred
26 : : * to as such here.
27 : : *
28 : : * Manually invoked VACUUMs may scan indexes during phase II in parallel. For
29 : : * more information on this, see the comment at the top of vacuumparallel.c.
30 : : *
31 : : * In between phases, vacuum updates the freespace map (every
32 : : * VACUUM_FSM_EVERY_PAGES).
33 : : *
34 : : * After completing all three phases, vacuum may truncate the relation if it
35 : : * has emptied pages at the end. Finally, vacuum updates relation statistics
36 : : * in pg_class and the cumulative statistics subsystem.
37 : : *
38 : : * Relation Scanning:
39 : : *
40 : : * Vacuum scans the heap relation, starting at the beginning and progressing
41 : : * to the end, skipping pages as permitted by their visibility status, vacuum
42 : : * options, and various other requirements.
43 : : *
44 : : * Vacuums are either aggressive or normal. Aggressive vacuums must scan every
45 : : * unfrozen tuple in order to advance relfrozenxid and avoid transaction ID
46 : : * wraparound. Normal vacuums may scan otherwise skippable pages for one of
47 : : * two reasons:
48 : : *
49 : : * When page skipping is not disabled, a normal vacuum may scan pages that are
50 : : * marked all-visible (and even all-frozen) in the visibility map if the range
51 : : * of skippable pages is below SKIP_PAGES_THRESHOLD. This is primarily for the
52 : : * benefit of kernel readahead (see comment in heap_vac_scan_next_block()).
53 : : *
54 : : * A normal vacuum may also scan skippable pages in an effort to freeze them
55 : : * and decrease the backlog of all-visible but not all-frozen pages that have
56 : : * to be processed by the next aggressive vacuum. These are referred to as
57 : : * eagerly scanned pages. Pages scanned due to SKIP_PAGES_THRESHOLD do not
58 : : * count as eagerly scanned pages.
59 : : *
60 : : * Eagerly scanned pages that are set all-frozen in the VM are successful
61 : : * eager freezes and those not set all-frozen in the VM are failed eager
62 : : * freezes.
63 : : *
64 : : * Because we want to amortize the overhead of freezing pages over multiple
65 : : * vacuums, normal vacuums cap the number of successful eager freezes to
66 : : * MAX_EAGER_FREEZE_SUCCESS_RATE of the number of all-visible but not
67 : : * all-frozen pages at the beginning of the vacuum. Since eagerly frozen pages
68 : : * may be unfrozen before the next aggressive vacuum, capping the number of
69 : : * successful eager freezes also caps the downside of eager freezing:
70 : : * potentially wasted work.
71 : : *
72 : : * Once the success cap has been hit, eager scanning is disabled for the
73 : : * remainder of the vacuum of the relation.
74 : : *
75 : : * Success is capped globally because we don't want to limit our successes if
76 : : * old data happens to be concentrated in a particular part of the table. This
77 : : * is especially likely to happen for append-mostly workloads where the oldest
78 : : * data is at the beginning of the unfrozen portion of the relation.
79 : : *
80 : : * On the assumption that different regions of the table are likely to contain
81 : : * similarly aged data, normal vacuums use a localized eager freeze failure
82 : : * cap. The failure count is reset for each region of the table -- comprised
83 : : * of EAGER_SCAN_REGION_SIZE blocks. In each region, we tolerate
84 : : * vacuum_max_eager_freeze_failure_rate of EAGER_SCAN_REGION_SIZE failures
85 : : * before suspending eager scanning until the end of the region.
86 : : * vacuum_max_eager_freeze_failure_rate is configurable both globally and per
87 : : * table.
88 : : *
89 : : * Aggressive vacuums must examine every unfrozen tuple and thus are not
90 : : * subject to any of the limits imposed by the eager scanning algorithm.
91 : : *
92 : : * Once vacuum has decided to scan a given block, it must read the block and
93 : : * obtain a cleanup lock to prune tuples on the page. A non-aggressive vacuum
94 : : * may choose to skip pruning and freezing if it cannot acquire a cleanup lock
95 : : * on the buffer right away. In this case, it may miss cleaning up dead tuples
96 : : * and their associated index entries (though it is free to reap any existing
97 : : * dead items on the page).
98 : : *
99 : : * After pruning and freezing, pages that are newly all-visible and all-frozen
100 : : * are marked as such in the visibility map.
101 : : *
102 : : * Dead TID Storage:
103 : : *
104 : : * The major space usage for vacuuming is storage for the dead tuple IDs that
105 : : * are to be removed from indexes. We want to ensure we can vacuum even the
106 : : * very largest relations with finite memory space usage. To do that, we set
107 : : * upper bounds on the memory that can be used for keeping track of dead TIDs
108 : : * at once.
109 : : *
110 : : * We are willing to use at most maintenance_work_mem (or perhaps
111 : : * autovacuum_work_mem) memory space to keep track of dead TIDs. If the
112 : : * TID store is full, we must call lazy_vacuum to vacuum indexes (and to vacuum
113 : : * the pages that we've pruned). This frees up the memory space dedicated to
114 : : * store dead TIDs.
115 : : *
116 : : * In practice VACUUM will often complete its initial pass over the target
117 : : * heap relation without ever running out of space to store TIDs. This means
118 : : * that there only needs to be one call to lazy_vacuum, after the initial pass
119 : : * completes.
120 : : *
121 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
122 : : * Portions Copyright (c) 1994, Regents of the University of California
123 : : *
124 : : *
125 : : * IDENTIFICATION
126 : : * src/backend/access/heap/vacuumlazy.c
127 : : *
128 : : *-------------------------------------------------------------------------
129 : : */
130 : : #include "postgres.h"
131 : :
132 : : #include "access/genam.h"
133 : : #include "access/heapam.h"
134 : : #include "access/htup_details.h"
135 : : #include "access/multixact.h"
136 : : #include "access/tidstore.h"
137 : : #include "access/transam.h"
138 : : #include "access/visibilitymap.h"
139 : : #include "access/xloginsert.h"
140 : : #include "catalog/storage.h"
141 : : #include "commands/progress.h"
142 : : #include "commands/vacuum.h"
143 : : #include "common/int.h"
144 : : #include "common/pg_prng.h"
145 : : #include "executor/instrument.h"
146 : : #include "miscadmin.h"
147 : : #include "pgstat.h"
148 : : #include "portability/instr_time.h"
149 : : #include "postmaster/autovacuum.h"
150 : : #include "storage/bufmgr.h"
151 : : #include "storage/freespace.h"
152 : : #include "storage/latch.h"
153 : : #include "storage/lmgr.h"
154 : : #include "storage/read_stream.h"
155 : : #include "utils/injection_point.h"
156 : : #include "utils/lsyscache.h"
157 : : #include "utils/pg_rusage.h"
158 : : #include "utils/timestamp.h"
159 : : #include "utils/wait_event.h"
160 : :
161 : :
162 : : /*
163 : : * Space/time tradeoff parameters: do these need to be user-tunable?
164 : : *
165 : : * To consider truncating the relation, we want there to be at least
166 : : * REL_TRUNCATE_MINIMUM or (relsize / REL_TRUNCATE_FRACTION) (whichever
167 : : * is less) potentially-freeable pages.
168 : : */
169 : : #define REL_TRUNCATE_MINIMUM 1000
170 : : #define REL_TRUNCATE_FRACTION 16
171 : :
172 : : /*
173 : : * Timing parameters for truncate locking heuristics.
174 : : *
175 : : * These were not exposed as user tunable GUC values because it didn't seem
176 : : * that the potential for improvement was great enough to merit the cost of
177 : : * supporting them.
178 : : */
179 : : #define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL 20 /* ms */
180 : : #define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL 50 /* ms */
181 : : #define VACUUM_TRUNCATE_LOCK_TIMEOUT 5000 /* ms */
182 : :
183 : : /*
184 : : * Threshold that controls whether we bypass index vacuuming and heap
185 : : * vacuuming as an optimization
186 : : */
187 : : #define BYPASS_THRESHOLD_PAGES 0.02 /* i.e. 2% of rel_pages */
188 : :
189 : : /*
190 : : * Perform a failsafe check each time we scan another 4GB of pages.
191 : : * (Note that this is deliberately kept to a power-of-two, usually 2^19.)
192 : : */
193 : : #define FAILSAFE_EVERY_PAGES \
194 : : ((BlockNumber) (((uint64) 4 * 1024 * 1024 * 1024) / BLCKSZ))
195 : :
196 : : /*
197 : : * When a table has no indexes, vacuum the FSM after every 8GB, approximately
198 : : * (it won't be exact because we only vacuum FSM after processing a heap page
199 : : * that has some removable tuples). When there are indexes, this is ignored,
200 : : * and we vacuum FSM after each index/heap cleaning pass.
201 : : */
202 : : #define VACUUM_FSM_EVERY_PAGES \
203 : : ((BlockNumber) (((uint64) 8 * 1024 * 1024 * 1024) / BLCKSZ))
204 : :
205 : : /*
206 : : * Before we consider skipping a page that's marked as clean in
207 : : * visibility map, we must've seen at least this many clean pages.
208 : : */
209 : : #define SKIP_PAGES_THRESHOLD ((BlockNumber) 32)
210 : :
211 : : /*
212 : : * Size of the prefetch window for lazy vacuum backwards truncation scan.
213 : : * Needs to be a power of 2.
214 : : */
215 : : #define PREFETCH_SIZE ((BlockNumber) 32)
216 : :
217 : : /*
218 : : * Macro to check if we are in a parallel vacuum. If true, we are in the
219 : : * parallel mode and the DSM segment is initialized.
220 : : */
221 : : #define ParallelVacuumIsActive(vacrel) ((vacrel)->pvs != NULL)
222 : :
223 : : /* Phases of vacuum during which we report error context. */
224 : : typedef enum
225 : : {
226 : : VACUUM_ERRCB_PHASE_UNKNOWN,
227 : : VACUUM_ERRCB_PHASE_SCAN_HEAP,
228 : : VACUUM_ERRCB_PHASE_VACUUM_INDEX,
229 : : VACUUM_ERRCB_PHASE_VACUUM_HEAP,
230 : : VACUUM_ERRCB_PHASE_INDEX_CLEANUP,
231 : : VACUUM_ERRCB_PHASE_TRUNCATE,
232 : : } VacErrPhase;
233 : :
234 : : /*
235 : : * An eager scan of a page that is set all-frozen in the VM is considered
236 : : * "successful". To spread out freezing overhead across multiple normal
237 : : * vacuums, we limit the number of successful eager page freezes. The maximum
238 : : * number of eager page freezes is calculated as a ratio of the all-visible
239 : : * but not all-frozen pages at the beginning of the vacuum.
240 : : */
241 : : #define MAX_EAGER_FREEZE_SUCCESS_RATE 0.2
242 : :
243 : : /*
244 : : * On the assumption that different regions of the table tend to have
245 : : * similarly aged data, once vacuum fails to freeze
246 : : * vacuum_max_eager_freeze_failure_rate of the blocks in a region of size
247 : : * EAGER_SCAN_REGION_SIZE, it suspends eager scanning until it has progressed
248 : : * to another region of the table with potentially older data.
249 : : */
250 : : #define EAGER_SCAN_REGION_SIZE 4096
251 : :
252 : : typedef struct LVRelState
253 : : {
254 : : /* Target heap relation and its indexes */
255 : : Relation rel;
256 : : Relation *indrels;
257 : : int nindexes;
258 : :
259 : : /* Buffer access strategy and parallel vacuum state */
260 : : BufferAccessStrategy bstrategy;
261 : : ParallelVacuumState *pvs;
262 : :
263 : : /* Aggressive VACUUM? (must set relfrozenxid >= FreezeLimit) */
264 : : bool aggressive;
265 : : /* Use visibility map to skip? (disabled by DISABLE_PAGE_SKIPPING) */
266 : : bool skipwithvm;
267 : : /* Consider index vacuuming bypass optimization? */
268 : : bool consider_bypass_optimization;
269 : :
270 : : /* Doing index vacuuming, index cleanup, rel truncation? */
271 : : bool do_index_vacuuming;
272 : : bool do_index_cleanup;
273 : : bool do_rel_truncate;
274 : :
275 : : /* VACUUM operation's cutoffs for freezing and pruning */
276 : : struct VacuumCutoffs cutoffs;
277 : : GlobalVisState *vistest;
278 : : /* Tracks oldest extant XID/MXID for setting relfrozenxid/relminmxid */
279 : : TransactionId NewRelfrozenXid;
280 : : MultiXactId NewRelminMxid;
281 : : bool skippedallvis;
282 : :
283 : : /* Error reporting state */
284 : : char *dbname;
285 : : char *relnamespace;
286 : : char *relname;
287 : : char *indname; /* Current index name */
288 : : BlockNumber blkno; /* used only for heap operations */
289 : : OffsetNumber offnum; /* used only for heap operations */
290 : : VacErrPhase phase;
291 : : bool verbose; /* VACUUM VERBOSE? */
292 : :
293 : : /*
294 : : * dead_items stores TIDs whose index tuples are deleted by index
295 : : * vacuuming. Each TID points to an LP_DEAD line pointer from a heap page
296 : : * that has been processed by lazy_scan_prune. Also needed by
297 : : * lazy_vacuum_heap_rel, which marks the same LP_DEAD line pointers as
298 : : * LP_UNUSED during second heap pass.
299 : : *
300 : : * Both dead_items and dead_items_info are allocated in shared memory in
301 : : * parallel vacuum cases.
302 : : */
303 : : TidStore *dead_items; /* TIDs whose index tuples we'll delete */
304 : : VacDeadItemsInfo *dead_items_info;
305 : :
306 : : BlockNumber rel_pages; /* total number of pages */
307 : : BlockNumber scanned_pages; /* # pages examined (not skipped via VM) */
308 : :
309 : : /*
310 : : * Count of all-visible blocks eagerly scanned (for logging only). This
311 : : * does not include skippable blocks scanned due to SKIP_PAGES_THRESHOLD.
312 : : */
313 : : BlockNumber eager_scanned_pages;
314 : :
315 : : BlockNumber removed_pages; /* # pages removed by relation truncation */
316 : : BlockNumber new_frozen_tuple_pages; /* # pages with newly frozen tuples */
317 : :
318 : : /* # pages newly set all-visible in the VM */
319 : : BlockNumber new_all_visible_pages;
320 : :
321 : : /*
322 : : * # pages newly set all-visible and all-frozen in the VM. This is a
323 : : * subset of new_all_visible_pages. That is, new_all_visible_pages
324 : : * includes all pages set all-visible, but
325 : : * new_all_visible_all_frozen_pages includes only those which were also
326 : : * set all-frozen.
327 : : */
328 : : BlockNumber new_all_visible_all_frozen_pages;
329 : :
330 : : /* # all-visible pages newly set all-frozen in the VM */
331 : : BlockNumber new_all_frozen_pages;
332 : :
333 : : BlockNumber lpdead_item_pages; /* # pages with LP_DEAD items */
334 : : BlockNumber missed_dead_pages; /* # pages with missed dead tuples */
335 : : BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */
336 : :
337 : : /* Statistics output by us, for table */
338 : : double new_rel_tuples; /* new estimated total # of tuples */
339 : : double new_live_tuples; /* new estimated total # of live tuples */
340 : : /* Statistics output by index AMs */
341 : : IndexBulkDeleteResult **indstats;
342 : :
343 : : /* Instrumentation counters */
344 : : int num_index_scans;
345 : : int num_dead_items_resets;
346 : : Size total_dead_items_bytes;
347 : :
348 : : /*
349 : : * Total number of planned and actually launched parallel workers for
350 : : * index vacuuming and index cleanup.
351 : : */
352 : : PVWorkerUsage worker_usage;
353 : :
354 : : /* Counters that follow are only for scanned_pages */
355 : : int64 tuples_deleted; /* # deleted from table */
356 : : int64 tuples_frozen; /* # newly frozen */
357 : : int64 lpdead_items; /* # deleted from indexes */
358 : : int64 live_tuples; /* # live tuples remaining */
359 : : int64 recently_dead_tuples; /* # dead, but not yet removable */
360 : : int64 missed_dead_tuples; /* # removable, but not removed */
361 : :
362 : : /* State maintained by heap_vac_scan_next_block() */
363 : : BlockNumber current_block; /* last block returned */
364 : : BlockNumber next_unskippable_block; /* next unskippable block */
365 : : bool next_unskippable_eager_scanned; /* if it was eagerly scanned */
366 : : Buffer next_unskippable_vmbuffer; /* buffer containing its VM bit */
367 : :
368 : : /* State related to managing eager scanning of all-visible pages */
369 : :
370 : : /*
371 : : * A normal vacuum that has failed to freeze too many eagerly scanned
372 : : * blocks in a region suspends eager scanning.
373 : : * next_eager_scan_region_start is the block number of the first block
374 : : * eligible for resumed eager scanning.
375 : : *
376 : : * When eager scanning is permanently disabled, either initially
377 : : * (including for aggressive vacuum) or due to hitting the success cap,
378 : : * this is set to InvalidBlockNumber.
379 : : */
380 : : BlockNumber next_eager_scan_region_start;
381 : :
382 : : /*
383 : : * The remaining number of blocks a normal vacuum will consider eager
384 : : * scanning when it is successful. When eager scanning is enabled, this is
385 : : * initialized to MAX_EAGER_FREEZE_SUCCESS_RATE of the total number of
386 : : * all-visible but not all-frozen pages. For each eager freeze success,
387 : : * this is decremented. Once it hits 0, eager scanning is permanently
388 : : * disabled. It is initialized to 0 if eager scanning starts out disabled
389 : : * (including for aggressive vacuum).
390 : : */
391 : : BlockNumber eager_scan_remaining_successes;
392 : :
393 : : /*
394 : : * The maximum number of blocks which may be eagerly scanned and not
395 : : * frozen before eager scanning is temporarily suspended. This is
396 : : * configurable both globally, via the
397 : : * vacuum_max_eager_freeze_failure_rate GUC, and per table, with a table
398 : : * storage parameter of the same name. It is calculated as
399 : : * vacuum_max_eager_freeze_failure_rate of EAGER_SCAN_REGION_SIZE blocks.
400 : : * It is 0 when eager scanning is disabled.
401 : : */
402 : : BlockNumber eager_scan_max_fails_per_region;
403 : :
404 : : /*
405 : : * The number of eagerly scanned blocks vacuum failed to freeze (due to
406 : : * age) in the current eager scan region. Vacuum resets it to
407 : : * eager_scan_max_fails_per_region each time it enters a new region of the
408 : : * relation. If eager_scan_remaining_fails hits 0, eager scanning is
409 : : * suspended until the next region. It is also 0 if eager scanning has
410 : : * been permanently disabled.
411 : : */
412 : : BlockNumber eager_scan_remaining_fails;
413 : : } LVRelState;
414 : :
415 : :
416 : : /* Struct for saving and restoring vacuum error information. */
417 : : typedef struct LVSavedErrInfo
418 : : {
419 : : BlockNumber blkno;
420 : : OffsetNumber offnum;
421 : : VacErrPhase phase;
422 : : } LVSavedErrInfo;
423 : :
424 : :
425 : : /* non-export function prototypes */
426 : : static void lazy_scan_heap(LVRelState *vacrel);
427 : : static void heap_vacuum_eager_scan_setup(LVRelState *vacrel,
428 : : const VacuumParams *params);
429 : : static BlockNumber heap_vac_scan_next_block(ReadStream *stream,
430 : : void *callback_private_data,
431 : : void *per_buffer_data);
432 : : static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis);
433 : : static bool lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf,
434 : : BlockNumber blkno, Page page,
435 : : bool sharelock, Buffer vmbuffer);
436 : : static int lazy_scan_prune(LVRelState *vacrel, Buffer buf,
437 : : BlockNumber blkno, Page page,
438 : : Buffer vmbuffer,
439 : : bool *has_lpdead_items, bool *vm_page_frozen);
440 : : static bool lazy_scan_noprune(LVRelState *vacrel, Buffer buf,
441 : : BlockNumber blkno, Page page,
442 : : bool *has_lpdead_items);
443 : : static void lazy_vacuum(LVRelState *vacrel);
444 : : static bool lazy_vacuum_all_indexes(LVRelState *vacrel);
445 : : static void lazy_vacuum_heap_rel(LVRelState *vacrel);
446 : : static void lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno,
447 : : Buffer buffer, OffsetNumber *deadoffsets,
448 : : int num_offsets, Buffer vmbuffer);
449 : : static bool lazy_check_wraparound_failsafe(LVRelState *vacrel);
450 : : static void lazy_cleanup_all_indexes(LVRelState *vacrel);
451 : : static IndexBulkDeleteResult *lazy_vacuum_one_index(Relation indrel,
452 : : IndexBulkDeleteResult *istat,
453 : : double reltuples,
454 : : LVRelState *vacrel);
455 : : static IndexBulkDeleteResult *lazy_cleanup_one_index(Relation indrel,
456 : : IndexBulkDeleteResult *istat,
457 : : double reltuples,
458 : : bool estimated_count,
459 : : LVRelState *vacrel);
460 : : static bool should_attempt_truncation(LVRelState *vacrel);
461 : : static void lazy_truncate_heap(LVRelState *vacrel);
462 : : static BlockNumber count_nondeletable_pages(LVRelState *vacrel,
463 : : bool *lock_waiter_detected);
464 : : static void dead_items_alloc(LVRelState *vacrel, int nworkers);
465 : : static void dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets,
466 : : int num_offsets);
467 : : static void dead_items_reset(LVRelState *vacrel);
468 : : static void dead_items_cleanup(LVRelState *vacrel);
469 : :
470 : : static bool heap_page_would_be_all_visible(Relation rel, Buffer buf,
471 : : GlobalVisState *vistest,
472 : : bool allow_update_vistest,
473 : : OffsetNumber *deadoffsets,
474 : : int ndeadoffsets,
475 : : bool *all_frozen,
476 : : TransactionId *newest_live_xid,
477 : : OffsetNumber *logging_offnum);
478 : : static void update_relstats_all_indexes(LVRelState *vacrel);
479 : : static void vacuum_error_callback(void *arg);
480 : : static void update_vacuum_error_info(LVRelState *vacrel,
481 : : LVSavedErrInfo *saved_vacrel,
482 : : int phase, BlockNumber blkno,
483 : : OffsetNumber offnum);
484 : : static void restore_vacuum_error_info(LVRelState *vacrel,
485 : : const LVSavedErrInfo *saved_vacrel);
486 : :
487 : :
488 : :
489 : : /*
490 : : * Helper to set up the eager scanning state for vacuuming a single relation.
491 : : * Initializes the eager scan management related members of the LVRelState.
492 : : *
493 : : * Caller provides whether or not an aggressive vacuum is required due to
494 : : * vacuum options or for relfrozenxid/relminmxid advancement.
495 : : */
496 : : static void
149 nathan@postgresql.or 497 :CBC 16325 : heap_vacuum_eager_scan_setup(LVRelState *vacrel, const VacuumParams *params)
498 : : {
499 : : uint32 randseed;
500 : : BlockNumber allvisible;
501 : : BlockNumber allfrozen;
502 : : float first_region_ratio;
562 melanieplageman@gmai 503 : 16325 : bool oldest_unfrozen_before_cutoff = false;
504 : :
505 : : /*
506 : : * Initialize eager scan management fields to their disabled values.
507 : : * Aggressive vacuums, normal vacuums of small tables, and normal vacuums
508 : : * of tables without sufficiently old tuples disable eager scanning.
509 : : */
510 : 16325 : vacrel->next_eager_scan_region_start = InvalidBlockNumber;
511 : 16325 : vacrel->eager_scan_max_fails_per_region = 0;
512 : 16325 : vacrel->eager_scan_remaining_fails = 0;
513 : 16325 : vacrel->eager_scan_remaining_successes = 0;
514 : :
515 : : /* If eager scanning is explicitly disabled, just return. */
149 nathan@postgresql.or 516 [ - + ]: 16325 : if (params->max_eager_freeze_failure_rate == 0)
562 melanieplageman@gmai 517 : 16325 : return;
518 : :
519 : : /*
520 : : * The caller will have determined whether or not an aggressive vacuum is
521 : : * required by either the vacuum parameters or the relative age of the
522 : : * oldest unfrozen transaction IDs. An aggressive vacuum must scan every
523 : : * all-visible page to safely advance the relfrozenxid and/or relminmxid,
524 : : * so scans of all-visible pages are not considered eager.
525 : : */
526 [ + + ]: 16325 : if (vacrel->aggressive)
527 : 9216 : return;
528 : :
529 : : /*
530 : : * Aggressively vacuuming a small relation shouldn't take long, so it
531 : : * isn't worth amortizing. We use two times the region size as the size
532 : : * cutoff because the eager scan start block is a random spot somewhere in
533 : : * the first region, making the second region the first to be eager
534 : : * scanned normally.
535 : : */
536 [ + - ]: 7109 : if (vacrel->rel_pages < 2 * EAGER_SCAN_REGION_SIZE)
537 : 7109 : return;
538 : :
539 : : /*
540 : : * We only want to enable eager scanning if we are likely to be able to
541 : : * freeze some of the pages in the relation.
542 : : *
543 : : * Tuples with XIDs older than OldestXmin or MXIDs older than OldestMxact
544 : : * are technically freezable, but we won't freeze them unless the criteria
545 : : * for opportunistic freezing is met. Only tuples with XIDs/MXIDs older
546 : : * than the FreezeLimit/MultiXactCutoff are frozen in the common case.
547 : : *
548 : : * So, as a heuristic, we wait until the FreezeLimit has advanced past the
549 : : * relfrozenxid or the MultiXactCutoff has advanced past the relminmxid to
550 : : * enable eager scanning.
551 : : */
562 melanieplageman@gmai 552 [ # # # # ]:UBC 0 : if (TransactionIdIsNormal(vacrel->cutoffs.relfrozenxid) &&
553 : 0 : TransactionIdPrecedes(vacrel->cutoffs.relfrozenxid,
554 : : vacrel->cutoffs.FreezeLimit))
555 : 0 : oldest_unfrozen_before_cutoff = true;
556 : :
557 [ # # ]: 0 : if (!oldest_unfrozen_before_cutoff &&
558 [ # # # # ]: 0 : MultiXactIdIsValid(vacrel->cutoffs.relminmxid) &&
559 : 0 : MultiXactIdPrecedes(vacrel->cutoffs.relminmxid,
560 : : vacrel->cutoffs.MultiXactCutoff))
561 : 0 : oldest_unfrozen_before_cutoff = true;
562 : :
563 [ # # ]: 0 : if (!oldest_unfrozen_before_cutoff)
564 : 0 : return;
565 : :
566 : : /* We have met the criteria to eagerly scan some pages. */
567 : :
568 : : /*
569 : : * Our success cap is MAX_EAGER_FREEZE_SUCCESS_RATE of the number of
570 : : * all-visible but not all-frozen blocks in the relation.
571 : : */
572 : 0 : visibilitymap_count(vacrel->rel, &allvisible, &allfrozen);
573 : :
574 : 0 : vacrel->eager_scan_remaining_successes =
575 : 0 : (BlockNumber) (MAX_EAGER_FREEZE_SUCCESS_RATE *
576 : 0 : (allvisible - allfrozen));
577 : :
578 : : /* If every all-visible page is frozen, eager scanning is disabled. */
579 [ # # ]: 0 : if (vacrel->eager_scan_remaining_successes == 0)
580 : 0 : return;
581 : :
582 : : /*
583 : : * Now calculate the bounds of the first eager scan region. Its end block
584 : : * will be a random spot somewhere in the first EAGER_SCAN_REGION_SIZE
585 : : * blocks. This affects the bounds of all subsequent regions and avoids
586 : : * eager scanning and failing to freeze the same blocks each vacuum of the
587 : : * relation.
588 : : */
589 : 0 : randseed = pg_prng_uint32(&pg_global_prng_state);
590 : :
591 : 0 : vacrel->next_eager_scan_region_start = randseed % EAGER_SCAN_REGION_SIZE;
592 : :
149 nathan@postgresql.or 593 [ # # # # ]: 0 : Assert(params->max_eager_freeze_failure_rate > 0 &&
594 : : params->max_eager_freeze_failure_rate <= 1);
595 : :
562 melanieplageman@gmai 596 : 0 : vacrel->eager_scan_max_fails_per_region =
149 nathan@postgresql.or 597 : 0 : params->max_eager_freeze_failure_rate *
598 : : EAGER_SCAN_REGION_SIZE;
599 : :
600 : : /*
601 : : * The first region will be smaller than subsequent regions. As such,
602 : : * adjust the eager freeze failures tolerated for this region.
603 : : */
562 melanieplageman@gmai 604 : 0 : first_region_ratio = 1 - (float) vacrel->next_eager_scan_region_start /
605 : : EAGER_SCAN_REGION_SIZE;
606 : :
607 : 0 : vacrel->eager_scan_remaining_fails =
608 : 0 : vacrel->eager_scan_max_fails_per_region *
609 : : first_region_ratio;
610 : : }
611 : :
612 : : /*
613 : : * heap_vacuum_rel() -- perform VACUUM for one heap relation
614 : : *
615 : : * This routine sets things up for and then calls lazy_scan_heap, where
616 : : * almost all work actually takes place. Finalizes everything after call
617 : : * returns by managing relation truncation and updating rel's pg_class
618 : : * entry. (Also updates pg_class entries for any indexes that need it.)
619 : : *
620 : : * At entry, we have already established a transaction and opened
621 : : * and locked the relation.
622 : : */
623 : : void
149 nathan@postgresql.or 624 :CBC 16325 : heap_vacuum_rel(Relation rel, const VacuumParams *params,
625 : : BufferAccessStrategy bstrategy)
626 : : {
627 : : LVRelState *vacrel;
628 : : bool verbose,
629 : : instrument,
630 : : skipwithvm,
631 : : frozenxid_updated,
632 : : minmulti_updated;
633 : : BlockNumber orig_rel_pages,
634 : : new_rel_pages,
635 : : new_rel_allvisible,
636 : : new_rel_allfrozen;
637 : : PGRUsage ru0;
6860 bruce@momjian.us 638 : 16325 : TimestampTz starttime = 0;
1595 pg@bowt.ie 639 : 16325 : PgStat_Counter startreadtime = 0,
1568 tgl@sss.pgh.pa.us 640 : 16325 : startwritetime = 0;
1595 pg@bowt.ie 641 : 16325 : WalUsage startwalusage = pgWalUsage;
848 msawada@postgresql.o 642 : 16325 : BufferUsage startbufferusage = pgBufferUsage;
643 : : ErrorContextCallback errcallback;
1608 pg@bowt.ie 644 : 16325 : char **indnames = NULL;
240 msawada@postgresql.o 645 : 16325 : Size dead_items_max_bytes = 0;
646 : :
149 nathan@postgresql.or 647 : 16325 : verbose = (params->options & VACOPT_VERBOSE) != 0;
906 heikki.linnakangas@i 648 [ + + + + ]: 16799 : instrument = (verbose || (AmAutoVacuumWorkerProcess() &&
149 nathan@postgresql.or 649 [ + - ]: 474 : params->log_vacuum_min_duration >= 0));
1686 pg@bowt.ie 650 [ + + ]: 16325 : if (instrument)
651 : : {
5488 rhaas@postgresql.org 652 : 487 : pg_rusage_init(&ru0);
1990 sfrost@snowman.net 653 [ - + ]: 487 : if (track_io_timing)
654 : : {
1990 sfrost@snowman.net 655 :UBC 0 : startreadtime = pgStatBlockReadTime;
656 : 0 : startwritetime = pgStatBlockWriteTime;
657 : : }
658 : : }
659 : :
660 : : /* Used for instrumentation and stats report */
576 michael@paquier.xyz 661 :CBC 16325 : starttime = GetCurrentTimestamp();
662 : :
3823 rhaas@postgresql.org 663 : 16325 : pgstat_progress_start_command(PROGRESS_COMMAND_VACUUM,
664 : : RelationGetRelid(rel));
261 msawada@postgresql.o 665 [ + + ]: 16325 : if (AmAutoVacuumWorkerProcess())
666 : 474 : pgstat_progress_update_param(PROGRESS_VACUUM_STARTED_BY,
149 nathan@postgresql.or 667 [ - + ]: 474 : params->is_wraparound
668 : : ? PROGRESS_VACUUM_STARTED_BY_AUTOVACUUM_WRAPAROUND
669 : : : PROGRESS_VACUUM_STARTED_BY_AUTOVACUUM);
670 : : else
261 msawada@postgresql.o 671 : 15851 : pgstat_progress_update_param(PROGRESS_VACUUM_STARTED_BY,
672 : : PROGRESS_VACUUM_STARTED_BY_MANUAL);
673 : :
674 : : /*
675 : : * Setup error traceback support for ereport() first. The idea is to set
676 : : * up an error context callback to display additional information on any
677 : : * error during a vacuum. During different phases of vacuum, we update
678 : : * the state so that the error context callback always display current
679 : : * information.
680 : : *
681 : : * Copy the names of heap rel into local memory for error reporting
682 : : * purposes, too. It isn't always safe to assume that we can get the name
683 : : * of each rel. It's convenient for code in lazy_scan_heap to always use
684 : : * these temp copies.
685 : : */
260 michael@paquier.xyz 686 : 16325 : vacrel = palloc0_object(LVRelState);
1332 pg@bowt.ie 687 : 16325 : vacrel->dbname = get_database_name(MyDatabaseId);
1686 688 : 16325 : vacrel->relnamespace = get_namespace_name(RelationGetNamespace(rel));
689 : 16325 : vacrel->relname = pstrdup(RelationGetRelationName(rel));
690 : 16325 : vacrel->indname = NULL;
691 : 16325 : vacrel->phase = VACUUM_ERRCB_PHASE_UNKNOWN;
692 : 16325 : vacrel->verbose = verbose;
693 : 16325 : errcallback.callback = vacuum_error_callback;
694 : 16325 : errcallback.arg = vacrel;
695 : 16325 : errcallback.previous = error_context_stack;
696 : 16325 : error_context_stack = &errcallback;
697 : :
698 : : /* Set up high level stuff about rel and its indexes */
1970 699 : 16325 : vacrel->rel = rel;
700 : 16325 : vac_open_indexes(vacrel->rel, RowExclusiveLock, &vacrel->nindexes,
701 : : &vacrel->indrels);
1344 702 : 16325 : vacrel->bstrategy = bstrategy;
1686 703 [ + + + + ]: 16325 : if (instrument && vacrel->nindexes > 0)
704 : : {
705 : : /* Copy index names used by instrumentation (not error reporting) */
260 michael@paquier.xyz 706 : 436 : indnames = palloc_array(char *, vacrel->nindexes);
1686 pg@bowt.ie 707 [ + + ]: 1330 : for (int i = 0; i < vacrel->nindexes; i++)
708 : 894 : indnames[i] = pstrdup(RelationGetRelationName(vacrel->indrels[i]));
709 : : }
710 : :
711 : : /*
712 : : * The index_cleanup param either disables index vacuuming and cleanup or
713 : : * forces it to go ahead when we would otherwise apply the index bypass
714 : : * optimization. The default is 'auto', which leaves the final decision
715 : : * up to lazy_vacuum().
716 : : *
717 : : * The truncate param allows user to avoid attempting relation truncation,
718 : : * though it can't force truncation to happen.
719 : : */
149 nathan@postgresql.or 720 [ - + ]: 16325 : Assert(params->index_cleanup != VACOPTVALUE_UNSPECIFIED);
721 [ + - - + ]: 16325 : Assert(params->truncate != VACOPTVALUE_UNSPECIFIED &&
722 : : params->truncate != VACOPTVALUE_AUTO);
723 : :
724 : : /*
725 : : * While VacuumFailSafeActive is reset to false before calling this, we
726 : : * still need to reset it here due to recursive calls.
727 : : */
1217 dgustafsson@postgres 728 : 16325 : VacuumFailsafeActive = false;
1686 pg@bowt.ie 729 : 16325 : vacrel->consider_bypass_optimization = true;
1969 730 : 16325 : vacrel->do_index_vacuuming = true;
731 : 16325 : vacrel->do_index_cleanup = true;
149 nathan@postgresql.or 732 : 16325 : vacrel->do_rel_truncate = (params->truncate != VACOPTVALUE_DISABLED);
733 [ + + ]: 16325 : if (params->index_cleanup == VACOPTVALUE_DISABLED)
734 : : {
735 : : /* Force disable index vacuuming up-front */
1969 pg@bowt.ie 736 : 150 : vacrel->do_index_vacuuming = false;
737 : 150 : vacrel->do_index_cleanup = false;
738 : : }
149 nathan@postgresql.or 739 [ + + ]: 16175 : else if (params->index_cleanup == VACOPTVALUE_ENABLED)
740 : : {
741 : : /* Force index vacuuming. Note that failsafe can still bypass. */
1896 pg@bowt.ie 742 : 27 : vacrel->consider_bypass_optimization = false;
743 : : }
744 : : else
745 : : {
746 : : /* Default/auto, make all decisions dynamically */
149 nathan@postgresql.or 747 [ - + ]: 16148 : Assert(params->index_cleanup == VACOPTVALUE_AUTO);
748 : : }
749 : :
750 : : /* Initialize page counters explicitly (be tidy) */
1629 pg@bowt.ie 751 : 16325 : vacrel->scanned_pages = 0;
562 melanieplageman@gmai 752 : 16325 : vacrel->eager_scanned_pages = 0;
1629 pg@bowt.ie 753 : 16325 : vacrel->removed_pages = 0;
618 melanieplageman@gmai 754 : 16325 : vacrel->new_frozen_tuple_pages = 0;
1629 pg@bowt.ie 755 : 16325 : vacrel->lpdead_item_pages = 0;
756 : 16325 : vacrel->missed_dead_pages = 0;
757 : 16325 : vacrel->nonempty_pages = 0;
758 : : /* dead_items_alloc allocates vacrel->dead_items later on */
759 : :
760 : : /* Allocate/initialize output statistics state */
761 : 16325 : vacrel->new_rel_tuples = 0;
762 : 16325 : vacrel->new_live_tuples = 0;
10 michael@paquier.xyz 763 :GNC 16325 : vacrel->indstats = palloc0_array(IndexBulkDeleteResult *, vacrel->nindexes);
764 : :
765 : : /* Initialize remaining counters (be tidy) */
1629 pg@bowt.ie 766 :CBC 16325 : vacrel->num_index_scans = 0;
240 msawada@postgresql.o 767 : 16325 : vacrel->num_dead_items_resets = 0;
768 : 16325 : vacrel->total_dead_items_bytes = 0;
1629 pg@bowt.ie 769 : 16325 : vacrel->tuples_deleted = 0;
1449 770 : 16325 : vacrel->tuples_frozen = 0;
1629 771 : 16325 : vacrel->lpdead_items = 0;
772 : 16325 : vacrel->live_tuples = 0;
773 : 16325 : vacrel->recently_dead_tuples = 0;
774 : 16325 : vacrel->missed_dead_tuples = 0;
775 : :
182 melanieplageman@gmai 776 : 16325 : vacrel->new_all_visible_pages = 0;
777 : 16325 : vacrel->new_all_visible_all_frozen_pages = 0;
778 : 16325 : vacrel->new_all_frozen_pages = 0;
779 : :
161 msawada@postgresql.o 780 : 16325 : vacrel->worker_usage.vacuum.nlaunched = 0;
781 : 16325 : vacrel->worker_usage.vacuum.nplanned = 0;
782 : 16325 : vacrel->worker_usage.cleanup.nlaunched = 0;
783 : 16325 : vacrel->worker_usage.cleanup.nplanned = 0;
784 : :
785 : : /*
786 : : * Get cutoffs that determine which deleted tuples are considered DEAD,
787 : : * not just RECENTLY_DEAD, and which XIDs/MXIDs to freeze. Then determine
788 : : * the extent of the blocks that we'll scan in lazy_scan_heap. It has to
789 : : * happen in this order to ensure that the OldestXmin cutoff field works
790 : : * as an upper bound on the XIDs stored in the pages we'll actually scan
791 : : * (NewRelfrozenXid tracking must never be allowed to miss unfrozen XIDs).
792 : : *
793 : : * Next acquire vistest, a related cutoff that's used in pruning. We use
794 : : * vistest in combination with OldestXmin to ensure that
795 : : * heap_page_prune_and_freeze() always removes any deleted tuple whose
796 : : * xmax is < OldestXmin. lazy_scan_prune must never become confused about
797 : : * whether a tuple should be frozen or removed. (In the future we might
798 : : * want to teach lazy_scan_prune to recompute vistest from time to time,
799 : : * to increase the number of dead tuples it can prune away.)
800 : : */
1344 pg@bowt.ie 801 : 16325 : vacrel->aggressive = vacuum_get_cutoffs(rel, params, &vacrel->cutoffs);
451 melanieplageman@gmai 802 : 16325 : vacrel->rel_pages = orig_rel_pages = RelationGetNumberOfBlocks(rel);
1629 pg@bowt.ie 803 : 16325 : vacrel->vistest = GlobalVisTestFor(rel);
804 : :
805 : : /* Initialize state used to track oldest extant XID/MXID */
1344 806 : 16325 : vacrel->NewRelfrozenXid = vacrel->cutoffs.OldestXmin;
807 : 16325 : vacrel->NewRelminMxid = vacrel->cutoffs.OldestMxact;
808 : :
809 : : /*
810 : : * Initialize state related to tracking all-visible page skipping. This is
811 : : * very important to determine whether or not it is safe to advance the
812 : : * relfrozenxid/relminmxid.
813 : : */
1607 814 : 16325 : vacrel->skippedallvis = false;
1344 815 : 16325 : skipwithvm = true;
149 nathan@postgresql.or 816 [ + + ]: 16325 : if (params->options & VACOPT_DISABLE_PAGE_SKIPPING)
817 : : {
818 : : /*
819 : : * Force aggressive mode, and disable skipping blocks using the
820 : : * visibility map (even those set all-frozen)
821 : : */
1344 pg@bowt.ie 822 : 198 : vacrel->aggressive = true;
823 : 198 : skipwithvm = false;
824 : : }
825 : :
826 : 16325 : vacrel->skipwithvm = skipwithvm;
827 : :
828 : : /*
829 : : * Set up eager scan tracking state. This must happen after determining
830 : : * whether or not the vacuum must be aggressive, because only normal
831 : : * vacuums use the eager scan algorithm.
832 : : */
562 melanieplageman@gmai 833 : 16325 : heap_vacuum_eager_scan_setup(vacrel, params);
834 : :
835 : : /* Report the vacuum mode: 'normal' or 'aggressive' */
261 msawada@postgresql.o 836 : 16325 : pgstat_progress_update_param(PROGRESS_VACUUM_MODE,
837 [ + + ]: 16325 : vacrel->aggressive
838 : : ? PROGRESS_VACUUM_MODE_AGGRESSIVE
839 : : : PROGRESS_VACUUM_MODE_NORMAL);
840 : :
1344 pg@bowt.ie 841 [ + + ]: 16325 : if (verbose)
842 : : {
843 [ + + ]: 13 : if (vacrel->aggressive)
844 [ + - ]: 1 : ereport(INFO,
845 : : (errmsg("aggressively vacuuming \"%s.%s.%s\"",
846 : : vacrel->dbname, vacrel->relnamespace,
847 : : vacrel->relname)));
848 : : else
849 [ + - ]: 12 : ereport(INFO,
850 : : (errmsg("vacuuming \"%s.%s.%s\"",
851 : : vacrel->dbname, vacrel->relnamespace,
852 : : vacrel->relname)));
853 : : }
854 : :
855 : : /*
856 : : * Allocate dead_items memory using dead_items_alloc. This handles
857 : : * parallel VACUUM initialization as part of allocating shared memory
858 : : * space used for dead_items. (But do a failsafe precheck first, to
859 : : * ensure that parallel VACUUM won't be attempted at all when relfrozenxid
860 : : * is already dangerously old.)
861 : : */
1606 862 : 16325 : lazy_check_wraparound_failsafe(vacrel);
149 nathan@postgresql.or 863 : 16325 : dead_items_alloc(vacrel, params->nworkers);
864 : :
865 : : #ifdef USE_INJECTION_POINTS
866 : :
867 : : /*
868 : : * Used by tests to pause before parallel vacuum is launched, allowing
869 : : * test code to modify configuration that the leader then propagates to
870 : : * workers.
871 : : */
143 msawada@postgresql.o 872 [ + + + + ]: 16325 : if (AmAutoVacuumWorkerProcess() && ParallelVacuumIsActive(vacrel))
873 : 2 : INJECTION_POINT("autovacuum-start-parallel-vacuum", NULL);
874 : : #endif
875 : :
876 : : /*
877 : : * Call lazy_scan_heap to perform all required heap pruning, index
878 : : * vacuuming, and heap vacuuming (plus related processing)
879 : : */
1606 pg@bowt.ie 880 : 16325 : lazy_scan_heap(vacrel);
881 : :
882 : : /*
883 : : * Save dead items max_bytes and update the memory usage statistics before
884 : : * cleanup, they are freed in parallel vacuum cases during
885 : : * dead_items_cleanup().
886 : : */
240 msawada@postgresql.o 887 : 16323 : dead_items_max_bytes = vacrel->dead_items_info->max_bytes;
888 : 16323 : vacrel->total_dead_items_bytes += TidStoreMemoryUsage(vacrel->dead_items);
889 : :
890 : : /*
891 : : * Free resources managed by dead_items_alloc. This ends parallel mode in
892 : : * passing when necessary.
893 : : */
1606 pg@bowt.ie 894 : 16323 : dead_items_cleanup(vacrel);
895 [ - + ]: 16323 : Assert(!IsInParallelMode());
896 : :
897 : : /*
898 : : * Update pg_class entries for each of rel's indexes where appropriate.
899 : : *
900 : : * Unlike the later update to rel's pg_class entry, this is not critical.
901 : : * Maintains relpages/reltuples statistics used by the planner only.
902 : : */
1629 903 [ + + ]: 16323 : if (vacrel->do_index_cleanup)
904 : 16173 : update_relstats_all_indexes(vacrel);
905 : :
906 : : /* Done with rel's indexes */
907 : 16323 : vac_close_indexes(vacrel->nindexes, vacrel->indrels, NoLock);
908 : :
909 : : /* Optionally truncate rel */
1896 910 [ + + ]: 16323 : if (should_attempt_truncation(vacrel))
1970 911 : 250 : lazy_truncate_heap(vacrel);
912 : :
913 : : /* Pop the error context stack */
2341 akapila@postgresql.o 914 : 16323 : error_context_stack = errcallback.previous;
915 : :
916 : : /* Report that we are now doing final cleanup */
3817 rhaas@postgresql.org 917 : 16323 : pgstat_progress_update_param(PROGRESS_VACUUM_PHASE,
918 : : PROGRESS_VACUUM_PHASE_FINAL_CLEANUP);
919 : :
920 : : /*
921 : : * Prepare to update rel's pg_class entry.
922 : : *
923 : : * Aggressive VACUUMs must always be able to advance relfrozenxid to a
924 : : * value >= FreezeLimit, and relminmxid to a value >= MultiXactCutoff.
925 : : * Non-aggressive VACUUMs may advance them by any amount, or not at all.
926 : : */
1344 pg@bowt.ie 927 [ + + + + : 16323 : Assert(vacrel->NewRelfrozenXid == vacrel->cutoffs.OldestXmin ||
- + ]
928 : : TransactionIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.FreezeLimit :
929 : : vacrel->cutoffs.relfrozenxid,
930 : : vacrel->NewRelfrozenXid));
931 [ + + - + : 16323 : Assert(vacrel->NewRelminMxid == vacrel->cutoffs.OldestMxact ||
- + ]
932 : : MultiXactIdPrecedesOrEquals(vacrel->aggressive ? vacrel->cutoffs.MultiXactCutoff :
933 : : vacrel->cutoffs.relminmxid,
934 : : vacrel->NewRelminMxid));
1607 935 [ + + ]: 16323 : if (vacrel->skippedallvis)
936 : : {
937 : : /*
938 : : * Must keep original relfrozenxid in a non-aggressive VACUUM that
939 : : * chose to skip an all-visible page range. The state that tracks new
940 : : * values will have missed unfrozen XIDs from the pages we skipped.
941 : : */
1344 942 [ - + ]: 123 : Assert(!vacrel->aggressive);
1607 943 : 123 : vacrel->NewRelfrozenXid = InvalidTransactionId;
944 : 123 : vacrel->NewRelminMxid = InvalidMultiXactId;
945 : : }
946 : :
947 : : /*
948 : : * For safety, clamp relallvisible to be not more than what we're setting
949 : : * pg_class.relpages to
950 : : */
1658 951 : 16323 : new_rel_pages = vacrel->rel_pages; /* After possible rel truncation */
542 melanieplageman@gmai 952 : 16323 : visibilitymap_count(rel, &new_rel_allvisible, &new_rel_allfrozen);
5431 tgl@sss.pgh.pa.us 953 [ - + ]: 16323 : if (new_rel_allvisible > new_rel_pages)
5431 tgl@sss.pgh.pa.us 954 :UBC 0 : new_rel_allvisible = new_rel_pages;
955 : :
956 : : /*
957 : : * An all-frozen block _must_ be all-visible. As such, clamp the count of
958 : : * all-frozen blocks to the count of all-visible blocks. This matches the
959 : : * clamping of relallvisible above.
960 : : */
542 melanieplageman@gmai 961 [ - + ]:CBC 16323 : if (new_rel_allfrozen > new_rel_allvisible)
542 melanieplageman@gmai 962 :UBC 0 : new_rel_allfrozen = new_rel_allvisible;
963 : :
964 : : /*
965 : : * Now actually update rel's pg_class entry.
966 : : *
967 : : * In principle new_live_tuples could be -1 indicating that we (still)
968 : : * don't know the tuple count. In practice that can't happen, since we
969 : : * scan every page that isn't skipped using the visibility map.
970 : : */
1607 pg@bowt.ie 971 :CBC 16323 : vac_update_relstats(rel, new_rel_pages, vacrel->new_live_tuples,
972 : : new_rel_allvisible, new_rel_allfrozen,
542 melanieplageman@gmai 973 : 16323 : vacrel->nindexes > 0,
974 : : vacrel->NewRelfrozenXid, vacrel->NewRelminMxid,
975 : : &frozenxid_updated, &minmulti_updated, false);
976 : :
977 : : /*
978 : : * Report results to the cumulative stats system, too.
979 : : *
980 : : * Deliberately avoid telling the stats system about LP_DEAD items that
981 : : * remain in the table due to VACUUM bypassing index and heap vacuuming.
982 : : * ANALYZE will consider the remaining LP_DEAD items to be dead "tuples".
983 : : * It seems like a good idea to err on the side of not vacuuming again too
984 : : * soon in cases where the failsafe prevented significant amounts of heap
985 : : * vacuuming.
986 : : */
253 michael@paquier.xyz 987 : 16323 : pgstat_report_vacuum(rel,
1608 pg@bowt.ie 988 : 6663 : Max(vacrel->new_live_tuples, 0),
1658 989 : 16323 : vacrel->recently_dead_tuples +
576 michael@paquier.xyz 990 [ + + ]: 16323 : vacrel->missed_dead_tuples,
991 : : starttime);
3823 rhaas@postgresql.org 992 : 16323 : pgstat_progress_end_command();
993 : :
1686 pg@bowt.ie 994 [ + + ]: 16323 : if (instrument)
995 : : {
5191 bruce@momjian.us 996 : 486 : TimestampTz endtime = GetCurrentTimestamp();
997 : :
149 nathan@postgresql.or 998 [ + + + + : 824 : if (verbose || params->log_vacuum_min_duration == 0 ||
- + ]
5389 alvherre@alvh.no-ip. 999 : 338 : TimestampDifferenceExceeds(starttime, endtime,
149 nathan@postgresql.or 1000 : 338 : params->log_vacuum_min_duration))
1001 : : {
1002 : : long secs_dur;
1003 : : int usecs_dur;
1004 : : WalUsage walusage;
1005 : : BufferUsage bufferusage;
1006 : : StringInfoData buf;
1007 : : char *msgfmt;
1008 : : int32 diff;
1595 pg@bowt.ie 1009 : 148 : double read_rate = 0,
1010 : 148 : write_rate = 0;
1011 : : int64 total_blks_hit;
1012 : : int64 total_blks_read;
1013 : : int64 total_blks_dirtied;
1014 : :
1015 : 148 : TimestampDifference(starttime, endtime, &secs_dur, &usecs_dur);
2334 akapila@postgresql.o 1016 : 148 : memset(&walusage, 0, sizeof(WalUsage));
1595 pg@bowt.ie 1017 : 148 : WalUsageAccumDiff(&walusage, &pgWalUsage, &startwalusage);
848 msawada@postgresql.o 1018 : 148 : memset(&bufferusage, 0, sizeof(BufferUsage));
1019 : 148 : BufferUsageAccumDiff(&bufferusage, &pgBufferUsage, &startbufferusage);
1020 : :
744 1021 : 148 : total_blks_hit = bufferusage.shared_blks_hit +
1022 : 148 : bufferusage.local_blks_hit;
1023 : 148 : total_blks_read = bufferusage.shared_blks_read +
1024 : 148 : bufferusage.local_blks_read;
1025 : 148 : total_blks_dirtied = bufferusage.shared_blks_dirtied +
1026 : 148 : bufferusage.local_blks_dirtied;
1027 : :
4270 alvherre@alvh.no-ip. 1028 : 148 : initStringInfo(&buf);
1686 pg@bowt.ie 1029 [ + + ]: 148 : if (verbose)
1030 : : {
1031 : : /*
1032 : : * Aggressiveness already reported earlier, in dedicated
1033 : : * VACUUM VERBOSE ereport
1034 : : */
149 nathan@postgresql.or 1035 [ - + ]: 13 : Assert(!params->is_wraparound);
1686 pg@bowt.ie 1036 : 13 : msgfmt = _("finished vacuuming \"%s.%s.%s\": index scans: %d\n");
1037 : : }
149 nathan@postgresql.or 1038 [ - + ]: 135 : else if (params->is_wraparound)
1039 : : {
1040 : : /*
1041 : : * While it's possible for a VACUUM to be both is_wraparound
1042 : : * and !aggressive, that's just a corner-case -- is_wraparound
1043 : : * implies aggressive. Produce distinct output for the corner
1044 : : * case all the same, just in case.
1045 : : */
1344 pg@bowt.ie 1046 [ # # ]:UBC 0 : if (vacrel->aggressive)
2340 michael@paquier.xyz 1047 : 0 : msgfmt = _("automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
1048 : : else
1049 : 0 : msgfmt = _("automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
1050 : : }
1051 : : else
1052 : : {
1344 pg@bowt.ie 1053 [ + + ]:CBC 135 : if (vacrel->aggressive)
2904 michael@paquier.xyz 1054 : 8 : msgfmt = _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n");
1055 : : else
1056 : 127 : msgfmt = _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n");
1057 : : }
3227 rhaas@postgresql.org 1058 : 148 : appendStringInfo(&buf, msgfmt,
1059 : : vacrel->dbname,
1060 : : vacrel->relnamespace,
1061 : : vacrel->relname,
1062 : : vacrel->num_index_scans);
562 melanieplageman@gmai 1063 [ + + ]: 294 : appendStringInfo(&buf, _("pages: %u removed, %u remain, %u scanned (%.2f%% of total), %u eagerly scanned\n"),
1064 : : vacrel->removed_pages,
1065 : : new_rel_pages,
1066 : : vacrel->scanned_pages,
1067 : : orig_rel_pages == 0 ? 100.0 :
1068 : 146 : 100.0 * vacrel->scanned_pages /
1069 : : orig_rel_pages,
1070 : : vacrel->eager_scanned_pages);
4270 alvherre@alvh.no-ip. 1071 : 148 : appendStringInfo(&buf,
516 peter@eisentraut.org 1072 : 148 : _("tuples: %" PRId64 " removed, %" PRId64 " remain, %" PRId64 " are dead but not yet removable\n"),
1073 : : vacrel->tuples_deleted,
1074 : 148 : (int64) vacrel->new_rel_tuples,
1075 : : vacrel->recently_dead_tuples);
1658 pg@bowt.ie 1076 [ + + ]: 148 : if (vacrel->missed_dead_tuples > 0)
1077 : 1 : appendStringInfo(&buf,
516 peter@eisentraut.org 1078 : 1 : _("tuples missed: %" PRId64 " dead from %u pages not removed due to cleanup lock contention\n"),
1079 : : vacrel->missed_dead_tuples,
1080 : : vacrel->missed_dead_pages);
1344 pg@bowt.ie 1081 : 148 : diff = (int32) (ReadNextTransactionId() -
1082 : 148 : vacrel->cutoffs.OldestXmin);
1658 1083 : 148 : appendStringInfo(&buf,
1595 1084 : 148 : _("removable cutoff: %u, which was %d XIDs old when operation ended\n"),
1085 : : vacrel->cutoffs.OldestXmin, diff);
1658 1086 [ + + ]: 148 : if (frozenxid_updated)
1087 : : {
1344 1088 : 93 : diff = (int32) (vacrel->NewRelfrozenXid -
1089 : 93 : vacrel->cutoffs.relfrozenxid);
1658 1090 : 93 : appendStringInfo(&buf,
1595 1091 : 93 : _("new relfrozenxid: %u, which is %d XIDs ahead of previous value\n"),
1092 : : vacrel->NewRelfrozenXid, diff);
1093 : : }
1658 1094 [ + + ]: 148 : if (minmulti_updated)
1095 : : {
1344 1096 : 75 : diff = (int32) (vacrel->NewRelminMxid -
1097 : 75 : vacrel->cutoffs.relminmxid);
1658 1098 : 75 : appendStringInfo(&buf,
1595 1099 : 75 : _("new relminmxid: %u, which is %d MXIDs ahead of previous value\n"),
1100 : : vacrel->NewRelminMxid, diff);
1101 : : }
516 peter@eisentraut.org 1102 [ + + ]: 294 : appendStringInfo(&buf, _("frozen: %u pages from table (%.2f%% of total) had %" PRId64 " tuples frozen\n"),
1103 : : vacrel->new_frozen_tuple_pages,
1104 : : orig_rel_pages == 0 ? 100.0 :
618 melanieplageman@gmai 1105 : 146 : 100.0 * vacrel->new_frozen_tuple_pages /
1106 : : orig_rel_pages,
1107 : : vacrel->tuples_frozen);
1108 : :
1109 : 148 : appendStringInfo(&buf,
1110 : 148 : _("visibility map: %u pages set all-visible, %u pages set all-frozen (%u were all-visible)\n"),
1111 : : vacrel->new_all_visible_pages,
182 1112 : 148 : vacrel->new_all_visible_all_frozen_pages +
1113 : 148 : vacrel->new_all_frozen_pages,
1114 : : vacrel->new_all_frozen_pages);
1595 pg@bowt.ie 1115 [ + - ]: 148 : if (vacrel->do_index_vacuuming)
1116 : : {
1117 [ + + + + ]: 148 : if (vacrel->nindexes == 0 || vacrel->num_index_scans == 0)
1118 : 34 : appendStringInfoString(&buf, _("index scan not needed: "));
1119 : : else
1120 : 114 : appendStringInfoString(&buf, _("index scan needed: "));
1121 : :
516 peter@eisentraut.org 1122 : 148 : msgfmt = _("%u pages from table (%.2f%% of total) had %" PRId64 " dead item identifiers removed\n");
1123 : : }
1124 : : else
1125 : : {
1238 dgustafsson@postgres 1126 [ # # ]:LBC (1) : if (!VacuumFailsafeActive)
1595 pg@bowt.ie 1127 : (1) : appendStringInfoString(&buf, _("index scan bypassed: "));
1128 : : else
1595 pg@bowt.ie 1129 :UBC 0 : appendStringInfoString(&buf, _("index scan bypassed by failsafe: "));
1130 : :
516 peter@eisentraut.org 1131 :LBC (1) : msgfmt = _("%u pages from table (%.2f%% of total) have %" PRId64 " dead item identifiers\n");
1132 : : }
1595 pg@bowt.ie 1133 [ + + ]:CBC 294 : appendStringInfo(&buf, msgfmt,
1134 : : vacrel->lpdead_item_pages,
1135 : : orig_rel_pages == 0 ? 100.0 :
1136 : 146 : 100.0 * vacrel->lpdead_item_pages / orig_rel_pages,
1137 : : vacrel->lpdead_items);
1138 : :
161 msawada@postgresql.o 1139 [ + + ]: 148 : if (vacrel->worker_usage.vacuum.nplanned > 0)
1140 : 1 : appendStringInfo(&buf,
1141 : 1 : _("parallel workers: index vacuum: %d planned, %d launched in total\n"),
1142 : : vacrel->worker_usage.vacuum.nplanned,
1143 : : vacrel->worker_usage.vacuum.nlaunched);
1144 : :
1145 [ - + ]: 148 : if (vacrel->worker_usage.cleanup.nplanned > 0)
161 msawada@postgresql.o 1146 :UBC 0 : appendStringInfo(&buf,
1147 : 0 : _("parallel workers: index cleanup: %d planned, %d launched\n"),
1148 : : vacrel->worker_usage.cleanup.nplanned,
1149 : : vacrel->worker_usage.cleanup.nlaunched);
1150 : :
1970 pg@bowt.ie 1151 [ + + ]:CBC 431 : for (int i = 0; i < vacrel->nindexes; i++)
1152 : : {
1153 : 283 : IndexBulkDeleteResult *istat = vacrel->indstats[i];
1154 : :
1155 [ + + ]: 283 : if (!istat)
1983 michael@paquier.xyz 1156 : 14 : continue;
1157 : :
1158 : 269 : appendStringInfo(&buf,
1982 1159 : 269 : _("index \"%s\": pages: %u in total, %u newly deleted, %u currently deleted, %u reusable\n"),
1983 1160 : 269 : indnames[i],
1161 : : istat->num_pages,
1162 : : istat->pages_newly_deleted,
1163 : : istat->pages_deleted,
1164 : : istat->pages_free);
1165 : : }
559 nathan@postgresql.or 1166 [ - + ]: 148 : if (track_cost_delay_timing)
1167 : : {
1168 : : /*
1169 : : * We bypass the changecount mechanism because this value is
1170 : : * only updated by the calling process. We also rely on the
1171 : : * above call to pgstat_progress_end_command() to not clear
1172 : : * the st_progress_param array.
1173 : : */
559 nathan@postgresql.or 1174 :UBC 0 : appendStringInfo(&buf, _("delay time: %.3f ms\n"),
1175 : 0 : (double) MyBEEntry->st_progress_param[PROGRESS_VACUUM_DELAY_TIME] / 1000000.0);
1176 : : }
1990 sfrost@snowman.net 1177 [ - + ]:CBC 148 : if (track_io_timing)
1178 : : {
1826 pg@bowt.ie 1179 :UBC 0 : double read_ms = (double) (pgStatBlockReadTime - startreadtime) / 1000;
1180 : 0 : double write_ms = (double) (pgStatBlockWriteTime - startwritetime) / 1000;
1181 : :
1182 : 0 : appendStringInfo(&buf, _("I/O timings: read: %.3f ms, write: %.3f ms\n"),
1183 : : read_ms, write_ms);
1184 : : }
1595 pg@bowt.ie 1185 [ + + + - ]:CBC 148 : if (secs_dur > 0 || usecs_dur > 0)
1186 : : {
744 msawada@postgresql.o 1187 : 148 : read_rate = (double) BLCKSZ * total_blks_read /
848 1188 : 148 : (1024 * 1024) / (secs_dur + usecs_dur / 1000000.0);
744 1189 : 148 : write_rate = (double) BLCKSZ * total_blks_dirtied /
848 1190 : 148 : (1024 * 1024) / (secs_dur + usecs_dur / 1000000.0);
1191 : : }
1826 pg@bowt.ie 1192 : 148 : appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
1193 : : read_rate, write_rate);
1194 : 148 : appendStringInfo(&buf,
516 peter@eisentraut.org 1195 : 148 : _("buffer usage: %" PRId64 " hits, %" PRId64 " reads, %" PRId64 " dirtied\n"),
1196 : : total_blks_hit,
1197 : : total_blks_read,
1198 : : total_blks_dirtied);
2334 akapila@postgresql.o 1199 : 148 : appendStringInfo(&buf,
297 michael@paquier.xyz 1200 : 148 : _("WAL usage: %" PRId64 " records, %" PRId64 " full page images, %" PRIu64 " bytes, %" PRIu64 " full page image bytes, %" PRId64 " buffers full\n"),
1201 : : walusage.wal_records,
1202 : : walusage.wal_fpi,
1203 : : walusage.wal_bytes,
1204 : : walusage.wal_fpi_bytes,
1205 : : walusage.wal_buffers_full);
1206 : :
1207 : : /*
1208 : : * Report the dead items memory usage.
1209 : : *
1210 : : * The num_dead_items_resets counter increases when we reset the
1211 : : * collected dead items, so the counter is non-zero if at least
1212 : : * one dead items are collected, even if index vacuuming is
1213 : : * disabled.
1214 : : */
240 msawada@postgresql.o 1215 : 148 : appendStringInfo(&buf,
1216 : 148 : ngettext("memory usage: dead item storage %.2f MB accumulated across %d reset (limit %.2f MB each)\n",
1217 : : "memory usage: dead item storage %.2f MB accumulated across %d resets (limit %.2f MB each)\n",
1218 : 148 : vacrel->num_dead_items_resets),
1219 : 148 : (double) vacrel->total_dead_items_bytes / (1024 * 1024),
1220 : : vacrel->num_dead_items_resets,
1221 : 148 : (double) dead_items_max_bytes / (1024 * 1024));
1826 pg@bowt.ie 1222 : 148 : appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
1223 : :
1686 1224 [ + + + - ]: 148 : ereport(verbose ? INFO : LOG,
1225 : : (errmsg_internal("%s", buf.data)));
4270 alvherre@alvh.no-ip. 1226 : 148 : pfree(buf.data);
1227 : : }
1228 : : }
1229 : :
1230 : : /* Cleanup index statistics and index names */
1970 pg@bowt.ie 1231 [ + + ]: 39952 : for (int i = 0; i < vacrel->nindexes; i++)
1232 : : {
1233 [ + + ]: 23629 : if (vacrel->indstats[i])
1234 : 2277 : pfree(vacrel->indstats[i]);
1235 : :
1686 1236 [ + + ]: 23629 : if (instrument)
1983 michael@paquier.xyz 1237 : 890 : pfree(indnames[i]);
1238 : : }
9176 tgl@sss.pgh.pa.us 1239 : 16323 : }
1240 : :
1241 : : /*
1242 : : * lazy_scan_heap() -- workhorse function for VACUUM
1243 : : *
1244 : : * This routine prunes each page in the heap, and considers the need to
1245 : : * freeze remaining tuples with storage (not including pages that can be
1246 : : * skipped using the visibility map). Also performs related maintenance
1247 : : * of the FSM and visibility map. These steps all take place during an
1248 : : * initial pass over the target heap relation.
1249 : : *
1250 : : * Also invokes lazy_vacuum_all_indexes to vacuum indexes, which largely
1251 : : * consists of deleting index tuples that point to LP_DEAD items left in
1252 : : * heap pages following pruning. Earlier initial pass over the heap will
1253 : : * have collected the TIDs whose index tuples need to be removed.
1254 : : *
1255 : : * Finally, invokes lazy_vacuum_heap_rel to vacuum heap pages, which
1256 : : * largely consists of marking LP_DEAD items (from vacrel->dead_items)
1257 : : * as LP_UNUSED. This has to happen in a second, final pass over the
1258 : : * heap, to preserve a basic invariant that all index AMs rely on: no
1259 : : * extant index tuple can ever be allowed to contain a TID that points to
1260 : : * an LP_UNUSED line pointer in the heap. We must disallow premature
1261 : : * recycling of line pointers to avoid index scans that get confused
1262 : : * about which TID points to which tuple immediately after recycling.
1263 : : * (Actually, this isn't a concern when target heap relation happens to
1264 : : * have no indexes, which allows us to safely apply the one-pass strategy
1265 : : * as an optimization).
1266 : : *
1267 : : * In practice we often have enough space to fit all TIDs, and so won't
1268 : : * need to call lazy_vacuum more than once, after our initial pass over
1269 : : * the heap has totally finished. Otherwise things are slightly more
1270 : : * complicated: our "initial pass" over the heap applies only to those
1271 : : * pages that were pruned before we needed to call lazy_vacuum, and our
1272 : : * "final pass" over the heap only vacuums these same heap pages.
1273 : : * However, we process indexes in full every time lazy_vacuum is called,
1274 : : * which makes index processing very inefficient when memory is in short
1275 : : * supply.
1276 : : */
1277 : : static void
1606 pg@bowt.ie 1278 : 16325 : lazy_scan_heap(LVRelState *vacrel)
1279 : : {
1280 : : ReadStream *stream;
1629 1281 : 16325 : BlockNumber rel_pages = vacrel->rel_pages,
559 melanieplageman@gmai 1282 : 16325 : blkno = 0,
1606 pg@bowt.ie 1283 : 16325 : next_fsm_block_to_vacuum = 0;
562 melanieplageman@gmai 1284 : 16325 : BlockNumber orig_eager_scan_success_limit =
1285 : : vacrel->eager_scan_remaining_successes; /* for logging */
6476 heikki.linnakangas@i 1286 : 16325 : Buffer vmbuffer = InvalidBuffer;
20 melanieplageman@gmai 1287 : 16325 : bool strategy_cleared = false;
3817 rhaas@postgresql.org 1288 : 16325 : const int initprog_index[] = {
1289 : : PROGRESS_VACUUM_PHASE,
1290 : : PROGRESS_VACUUM_TOTAL_HEAP_BLKS,
1291 : : PROGRESS_VACUUM_MAX_DEAD_TUPLE_BYTES
1292 : : };
1293 : : int64 initprog_val[3];
1294 : :
1295 : : /* Report that we're scanning the heap, advertising total # of blocks */
1296 : 16325 : initprog_val[0] = PROGRESS_VACUUM_PHASE_SCAN_HEAP;
1629 pg@bowt.ie 1297 : 16325 : initprog_val[1] = rel_pages;
631 john.naylor@postgres 1298 : 16325 : initprog_val[2] = vacrel->dead_items_info->max_bytes;
3817 rhaas@postgresql.org 1299 : 16325 : pgstat_progress_update_multi_param(3, initprog_index, initprog_val);
1300 : :
1301 : : /* Initialize for the first heap_vac_scan_next_block() call */
899 heikki.linnakangas@i 1302 : 16325 : vacrel->current_block = InvalidBlockNumber;
1303 : 16325 : vacrel->next_unskippable_block = InvalidBlockNumber;
562 melanieplageman@gmai 1304 : 16325 : vacrel->next_unskippable_eager_scanned = false;
899 heikki.linnakangas@i 1305 : 16325 : vacrel->next_unskippable_vmbuffer = InvalidBuffer;
1306 : :
1307 : : /*
1308 : : * Set up the read stream for vacuum's first pass through the heap.
1309 : : *
1310 : : * This could be made safe for READ_STREAM_USE_BATCHING, but only with
1311 : : * explicit work in heap_vac_scan_next_block.
1312 : : */
559 melanieplageman@gmai 1313 : 16325 : stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE,
1314 : : vacrel->bstrategy,
1315 : : vacrel->rel,
1316 : : MAIN_FORKNUM,
1317 : : heap_vac_scan_next_block,
1318 : : vacrel,
1319 : : sizeof(bool));
1320 : :
1321 : : while (true)
9176 tgl@sss.pgh.pa.us 1322 : 97428 : {
1323 : : Buffer buf;
1324 : : Page page;
213 melanieplageman@gmai 1325 : 113753 : bool was_eager_scanned = false;
422 msawada@postgresql.o 1326 : 113753 : int ndeleted = 0;
1327 : : bool has_lpdead_items;
555 melanieplageman@gmai 1328 : 113753 : void *per_buffer_data = NULL;
562 1329 : 113753 : bool vm_page_frozen = false;
944 rhaas@postgresql.org 1330 : 113753 : bool got_cleanup_lock = false;
1331 : :
562 nathan@postgresql.or 1332 : 113753 : vacuum_delay_point(false);
1333 : :
1334 : : /*
1335 : : * Regularly check if wraparound failsafe should trigger.
1336 : : *
1337 : : * There is a similar check inside lazy_vacuum_all_indexes(), but
1338 : : * relfrozenxid might start to look dangerously old before we reach
1339 : : * that point. This check also provides failsafe coverage for the
1340 : : * one-pass strategy, and the two-pass strategy with the index_cleanup
1341 : : * param set to 'off'.
1342 : : */
559 melanieplageman@gmai 1343 [ + + ]: 113753 : if (vacrel->scanned_pages > 0 &&
1344 [ - + ]: 97428 : vacrel->scanned_pages % FAILSAFE_EVERY_PAGES == 0)
1921 pg@bowt.ie 1345 :UBC 0 : lazy_check_wraparound_failsafe(vacrel);
1346 : :
1347 : : /*
1348 : : * Consider if we definitely have enough space to process TIDs on page
1349 : : * already. If we are close to overrunning the available space for
1350 : : * dead_items TIDs, pause and do a cycle of vacuuming before we tackle
1351 : : * this page. However, let's force at least one page-worth of tuples
1352 : : * to be stored as to ensure we do at least some work when the memory
1353 : : * configured is so low that we run out before storing anything.
1354 : : */
527 msawada@postgresql.o 1355 [ + + ]:CBC 113753 : if (vacrel->dead_items_info->num_items > 0 &&
1356 [ + + ]: 37929 : TidStoreMemoryUsage(vacrel->dead_items) > vacrel->dead_items_info->max_bytes)
1357 : : {
1358 : : /*
1359 : : * Before beginning index vacuuming, we release any pin we may
1360 : : * hold on the visibility map page. This isn't necessary for
1361 : : * correctness, but we do it anyway to avoid holding the pin
1362 : : * across a lengthy, unrelated operation.
1363 : : */
5239 rhaas@postgresql.org 1364 [ + - ]: 7 : if (BufferIsValid(vmbuffer))
1365 : : {
1366 : 7 : ReleaseBuffer(vmbuffer);
1367 : 7 : vmbuffer = InvalidBuffer;
1368 : : }
1369 : :
1370 : : /* Perform a round of index and heap vacuuming */
1896 pg@bowt.ie 1371 : 7 : vacrel->consider_bypass_optimization = false;
1372 : 7 : lazy_vacuum(vacrel);
1373 : :
1374 : : /*
1375 : : * Vacuum the Free Space Map to make newly-freed space visible on
1376 : : * upper-level FSM pages. Note that blkno is the previously
1377 : : * processed block.
1378 : : */
1970 1379 : 7 : FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
1380 : : blkno + 1);
3073 tgl@sss.pgh.pa.us 1381 : 7 : next_fsm_block_to_vacuum = blkno;
1382 : :
1383 : : /* Report that we are once again scanning the heap */
3817 rhaas@postgresql.org 1384 : 7 : pgstat_progress_update_param(PROGRESS_VACUUM_PHASE,
1385 : : PROGRESS_VACUUM_PHASE_SCAN_HEAP);
1386 : : }
1387 : :
1388 : : /*
1389 : : * If the wraparound failsafe has engaged -- either via the check
1390 : : * above or during index vacuuming invoked from this loop -- stop
1391 : : * using the buffer access strategy so that the rest of the vacuum may
1392 : : * use all of shared buffers. Failsafe mode stays engaged once
1393 : : * triggered, so we only need to do this once.
1394 : : */
20 melanieplageman@gmai 1395 [ - + - - ]: 113753 : if (unlikely(VacuumFailsafeActive) && !strategy_cleared)
1396 : : {
21 melanieplageman@gmai 1397 :UBC 0 : read_stream_clear_strategy(stream);
20 1398 : 0 : strategy_cleared = true;
1399 : : }
1400 : :
559 melanieplageman@gmai 1401 :CBC 113753 : buf = read_stream_next_buffer(stream, &per_buffer_data);
1402 : :
1403 : : /* The relation is exhausted. */
1404 [ + + ]: 113753 : if (!BufferIsValid(buf))
1405 : 16325 : break;
1406 : :
213 1407 : 97428 : was_eager_scanned = *((bool *) per_buffer_data);
559 1408 : 97428 : CheckBufferIsPinnedOnce(buf);
1409 : 97428 : page = BufferGetPage(buf);
1410 : 97428 : blkno = BufferGetBlockNumber(buf);
1411 : :
1412 : 97428 : vacrel->scanned_pages++;
213 1413 [ - + ]: 97428 : if (was_eager_scanned)
559 melanieplageman@gmai 1414 :UBC 0 : vacrel->eager_scanned_pages++;
1415 : :
1416 : : /* Report as block scanned, update error traceback information */
559 melanieplageman@gmai 1417 :CBC 97428 : pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED, blkno);
1418 : 97428 : update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_SCAN_HEAP,
1419 : : blkno, InvalidOffsetNumber);
1420 : :
1421 : : /*
1422 : : * Pin the visibility map page in case we need to mark the page
1423 : : * all-visible. In most cases this will be very cheap, because we'll
1424 : : * already have the correct page pinned anyway.
1425 : : */
1970 pg@bowt.ie 1426 : 97428 : visibilitymap_pin(vacrel->rel, blkno, &vmbuffer);
1427 : :
1428 : : /*
1429 : : * We need a buffer cleanup lock to prune HOT chains and defragment
1430 : : * the page in lazy_scan_prune. But when it's not possible to acquire
1431 : : * a cleanup lock right away, we may be able to settle for reduced
1432 : : * processing using lazy_scan_noprune.
1433 : : */
944 rhaas@postgresql.org 1434 : 97428 : got_cleanup_lock = ConditionalLockBufferForCleanup(buf);
1435 : :
1436 [ + + ]: 97428 : if (!got_cleanup_lock)
1437 : 64 : LockBuffer(buf, BUFFER_LOCK_SHARE);
1438 : :
1439 : : /* Check for new or empty pages before lazy_scan_[no]prune call */
1440 [ + + ]: 97428 : if (lazy_scan_new_or_empty(vacrel, buf, blkno, page, !got_cleanup_lock,
1441 : 97428 : vmbuffer))
1442 : : {
1443 : : /* Processed as new/empty page (lock and pin released) */
1444 : 2019 : continue;
1445 : : }
1446 : :
1447 : : /*
1448 : : * If we didn't get the cleanup lock, we can still collect LP_DEAD
1449 : : * items in the dead_items area for later vacuuming, count live and
1450 : : * recently dead tuples for vacuum logging, and determine if this
1451 : : * block could later be truncated. If we encounter any xid/mxids that
1452 : : * require advancing the relfrozenxid/relminxid, we'll have to wait
1453 : : * for a cleanup lock and call lazy_scan_prune().
1454 : : */
1455 [ + + ]: 95409 : if (!got_cleanup_lock &&
1456 [ + + ]: 64 : !lazy_scan_noprune(vacrel, buf, blkno, page, &has_lpdead_items))
1457 : : {
1458 : : /*
1459 : : * lazy_scan_noprune could not do all required processing. Wait
1460 : : * for a cleanup lock, and call lazy_scan_prune in the usual way.
1461 : : */
1658 pg@bowt.ie 1462 [ - + ]: 50 : Assert(vacrel->aggressive);
1463 : 50 : LockBuffer(buf, BUFFER_LOCK_UNLOCK);
1464 : 50 : LockBufferForCleanup(buf);
944 rhaas@postgresql.org 1465 : 50 : got_cleanup_lock = true;
1466 : : }
1467 : :
1468 : : /*
1469 : : * If we have a cleanup lock, we must now prune, freeze, and count
1470 : : * tuples. We may have acquired the cleanup lock originally, or we may
1471 : : * have gone back and acquired it after lazy_scan_noprune() returned
1472 : : * false. Either way, the page hasn't been processed yet.
1473 : : *
1474 : : * Like lazy_scan_noprune(), lazy_scan_prune() will count
1475 : : * recently_dead_tuples and live tuples for vacuum logging, determine
1476 : : * if the block can later be truncated, and accumulate the details of
1477 : : * remaining LP_DEAD line pointers on the page into dead_items. These
1478 : : * dead items include those pruned by lazy_scan_prune() as well as
1479 : : * line pointers previously marked LP_DEAD.
1480 : : */
1481 [ + + ]: 95409 : if (got_cleanup_lock)
422 msawada@postgresql.o 1482 : 95395 : ndeleted = lazy_scan_prune(vacrel, buf, blkno, page,
1483 : : vmbuffer,
1484 : : &has_lpdead_items, &vm_page_frozen);
1485 : :
1486 : : /*
1487 : : * Count an eagerly scanned page as a failure or a success.
1488 : : *
1489 : : * Only lazy_scan_prune() freezes pages, so if we didn't get the
1490 : : * cleanup lock, we won't have frozen the page. However, we only count
1491 : : * pages that were too new to require freezing as eager freeze
1492 : : * failures.
1493 : : *
1494 : : * We could gather more information from lazy_scan_noprune() about
1495 : : * whether or not there were tuples with XIDs or MXIDs older than the
1496 : : * FreezeLimit or MultiXactCutoff. However, for simplicity, we simply
1497 : : * exclude pages skipped due to cleanup lock contention from eager
1498 : : * freeze algorithm caps.
1499 : : */
213 melanieplageman@gmai 1500 [ + + - + ]: 95409 : if (got_cleanup_lock && was_eager_scanned)
1501 : : {
1502 : : /* Aggressive vacuums do not eager scan. */
562 melanieplageman@gmai 1503 [ # # ]:UBC 0 : Assert(!vacrel->aggressive);
1504 : :
1505 [ # # ]: 0 : if (vm_page_frozen)
1506 : : {
457 msawada@postgresql.o 1507 [ # # ]: 0 : if (vacrel->eager_scan_remaining_successes > 0)
1508 : 0 : vacrel->eager_scan_remaining_successes--;
1509 : :
562 melanieplageman@gmai 1510 [ # # ]: 0 : if (vacrel->eager_scan_remaining_successes == 0)
1511 : : {
1512 : : /*
1513 : : * Report only once that we disabled eager scanning. We
1514 : : * may eagerly read ahead blocks in excess of the success
1515 : : * or failure caps before attempting to freeze them, so we
1516 : : * could reach here even after disabling additional eager
1517 : : * scanning.
1518 : : */
457 msawada@postgresql.o 1519 [ # # ]: 0 : if (vacrel->eager_scan_max_fails_per_region > 0)
1520 [ # # # # ]: 0 : ereport(vacrel->verbose ? INFO : DEBUG2,
1521 : : (errmsg("disabling eager scanning after freezing %u eagerly scanned blocks of relation \"%s.%s.%s\"",
1522 : : orig_eager_scan_success_limit,
1523 : : vacrel->dbname, vacrel->relnamespace,
1524 : : vacrel->relname)));
1525 : :
1526 : : /*
1527 : : * If we hit our success cap, permanently disable eager
1528 : : * scanning by setting the other eager scan management
1529 : : * fields to their disabled values.
1530 : : */
562 melanieplageman@gmai 1531 : 0 : vacrel->eager_scan_remaining_fails = 0;
1532 : 0 : vacrel->next_eager_scan_region_start = InvalidBlockNumber;
1533 : 0 : vacrel->eager_scan_max_fails_per_region = 0;
1534 : : }
1535 : : }
457 msawada@postgresql.o 1536 [ # # ]: 0 : else if (vacrel->eager_scan_remaining_fails > 0)
562 melanieplageman@gmai 1537 : 0 : vacrel->eager_scan_remaining_fails--;
1538 : : }
1539 : :
1540 : : /*
1541 : : * Now drop the buffer lock and, potentially, update the FSM.
1542 : : *
1543 : : * Our goal is to update the freespace map the last time we touch the
1544 : : * page. If we'll process a block in the second pass, we may free up
1545 : : * additional space on the page, so it is better to update the FSM
1546 : : * after the second pass. If the relation has no indexes, or if index
1547 : : * vacuuming is disabled, there will be no second heap pass; if this
1548 : : * particular page has no dead items, the second heap pass will not
1549 : : * touch this page. So, in those cases, update the FSM now.
1550 : : *
1551 : : * Note: In corner cases, it's possible to miss updating the FSM
1552 : : * entirely. If index vacuuming is currently enabled, we'll skip the
1553 : : * FSM update now. But if failsafe mode is later activated, or there
1554 : : * are so few dead tuples that index vacuuming is bypassed, there will
1555 : : * also be no opportunity to update the FSM later, because we'll never
1556 : : * revisit this page. Since updating the FSM is desirable but not
1557 : : * absolutely required, that's OK.
1558 : : */
952 rhaas@postgresql.org 1559 [ + + ]:CBC 95409 : if (vacrel->nindexes == 0
1560 [ + + ]: 86893 : || !vacrel->do_index_vacuuming
1561 [ + + ]: 86441 : || !has_lpdead_items)
1969 pg@bowt.ie 1562 : 71903 : {
1563 : 71903 : Size freespace = PageGetHeapFreeSpace(page);
1564 : :
1565 : 71903 : UnlockReleaseBuffer(buf);
1970 1566 : 71903 : RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1567 : :
1568 : : /*
1569 : : * Periodically perform FSM vacuuming to make newly-freed space
1570 : : * visible on upper FSM pages. This is done after vacuuming if the
1571 : : * table has indexes. There will only be newly-freed space if we
1572 : : * held the cleanup lock and lazy_scan_prune() was called.
1573 : : */
422 msawada@postgresql.o 1574 [ + + + + : 71903 : if (got_cleanup_lock && vacrel->nindexes == 0 && ndeleted > 0 &&
+ + ]
952 rhaas@postgresql.org 1575 [ - + ]: 476 : blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES)
1576 : : {
952 rhaas@postgresql.org 1577 :UBC 0 : FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
1578 : : blkno);
1579 : 0 : next_fsm_block_to_vacuum = blkno;
1580 : : }
1581 : : }
1582 : : else
952 rhaas@postgresql.org 1583 :CBC 23506 : UnlockReleaseBuffer(buf);
1584 : : }
1585 : :
1606 pg@bowt.ie 1586 : 16325 : vacrel->blkno = InvalidBlockNumber;
1587 [ + + ]: 16325 : if (BufferIsValid(vmbuffer))
1588 : 6784 : ReleaseBuffer(vmbuffer);
1589 : :
1590 : : /*
1591 : : * Report that everything is now scanned. We never skip scanning the last
1592 : : * block in the relation, so we can pass rel_pages here.
1593 : : */
559 melanieplageman@gmai 1594 : 16325 : pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_SCANNED,
1595 : : rel_pages);
1596 : :
1597 : : /* now we can compute the new value for pg_class.reltuples */
1629 pg@bowt.ie 1598 : 32650 : vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages,
1599 : : vacrel->scanned_pages,
1969 1600 : 16325 : vacrel->live_tuples);
1601 : :
1602 : : /*
1603 : : * Also compute the total number of surviving heap entries. In the
1604 : : * (unlikely) scenario that new_live_tuples is -1, take it as zero.
1605 : : */
1970 1606 : 16325 : vacrel->new_rel_tuples =
1658 1607 [ + + ]: 16325 : Max(vacrel->new_live_tuples, 0) + vacrel->recently_dead_tuples +
1608 : 16325 : vacrel->missed_dead_tuples;
1609 : :
559 melanieplageman@gmai 1610 : 16325 : read_stream_end(stream);
1611 : :
1612 : : /*
1613 : : * Do index vacuuming (call each index's ambulkdelete routine), then do
1614 : : * related heap vacuuming
1615 : : */
631 john.naylor@postgres 1616 [ + + ]: 16325 : if (vacrel->dead_items_info->num_items > 0)
1896 pg@bowt.ie 1617 : 1043 : lazy_vacuum(vacrel);
1618 : :
1619 : : /*
1620 : : * Vacuum the remainder of the Free Space Map. We must do this whether or
1621 : : * not there were indexes, and whether or not we bypassed index vacuuming.
1622 : : * We can pass rel_pages here because we never skip scanning the last
1623 : : * block of the relation.
1624 : : */
559 melanieplageman@gmai 1625 [ + + ]: 16323 : if (rel_pages > next_fsm_block_to_vacuum)
1626 : 6783 : FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, rel_pages);
1627 : :
1628 : : /* report all blocks vacuumed */
1629 : 16323 : pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, rel_pages);
1630 : :
1631 : : /* Do final index cleanup (call each index's amvacuumcleanup routine) */
1969 pg@bowt.ie 1632 [ + + + + ]: 16323 : if (vacrel->nindexes > 0 && vacrel->do_index_cleanup)
1970 1633 : 14784 : lazy_cleanup_all_indexes(vacrel);
9176 tgl@sss.pgh.pa.us 1634 : 16323 : }
1635 : :
1636 : : /*
1637 : : * heap_vac_scan_next_block() -- read stream callback to get the next block
1638 : : * for vacuum to process
1639 : : *
1640 : : * Every time lazy_scan_heap() needs a new block to process during its first
1641 : : * phase, it invokes read_stream_next_buffer() with a stream set up to call
1642 : : * heap_vac_scan_next_block() to get the next block.
1643 : : *
1644 : : * heap_vac_scan_next_block() uses the visibility map, vacuum options, and
1645 : : * various thresholds to skip blocks which do not need to be processed and
1646 : : * returns the next block to process or InvalidBlockNumber if there are no
1647 : : * remaining blocks.
1648 : : *
1649 : : * The visibility status of the next block to process and whether or not it
1650 : : * was eager scanned is set in the per_buffer_data.
1651 : : *
1652 : : * callback_private_data contains a reference to the LVRelState, passed to the
1653 : : * read stream API during stream setup. The LVRelState is an in/out parameter
1654 : : * here (locally named `vacrel`). Vacuum options and information about the
1655 : : * relation are read from it. vacrel->skippedallvis is set if we skip a block
1656 : : * that's all-visible but not all-frozen (to ensure that we don't update
1657 : : * relfrozenxid in that case). vacrel also holds information about the next
1658 : : * unskippable block -- as bookkeeping for this function.
1659 : : */
1660 : : static BlockNumber
559 melanieplageman@gmai 1661 : 113753 : heap_vac_scan_next_block(ReadStream *stream,
1662 : : void *callback_private_data,
1663 : : void *per_buffer_data)
1664 : : {
1665 : : BlockNumber next_block;
1666 : 113753 : LVRelState *vacrel = callback_private_data;
1667 : :
1668 : : /* relies on InvalidBlockNumber + 1 overflowing to 0 on first call */
899 heikki.linnakangas@i 1669 : 113753 : next_block = vacrel->current_block + 1;
1670 : :
1671 : : /* Have we reached the end of the relation? */
1672 [ + + ]: 113753 : if (next_block >= vacrel->rel_pages)
1673 : : {
1674 [ + + ]: 16325 : if (BufferIsValid(vacrel->next_unskippable_vmbuffer))
1675 : : {
1676 : 5096 : ReleaseBuffer(vacrel->next_unskippable_vmbuffer);
1677 : 5096 : vacrel->next_unskippable_vmbuffer = InvalidBuffer;
1678 : : }
559 melanieplageman@gmai 1679 : 16325 : return InvalidBlockNumber;
1680 : : }
1681 : :
1682 : : /*
1683 : : * We must be in one of the three following states:
1684 : : */
899 heikki.linnakangas@i 1685 [ + + ]: 97428 : if (next_block > vacrel->next_unskippable_block ||
1686 [ + + ]: 19949 : vacrel->next_unskippable_block == InvalidBlockNumber)
1687 : : {
1688 : : /*
1689 : : * 1. We have just processed an unskippable block (or we're at the
1690 : : * beginning of the scan). Find the next unskippable block using the
1691 : : * visibility map.
1692 : : */
1693 : : bool skipsallvis;
1694 : :
1695 : 84264 : find_next_unskippable_block(vacrel, &skipsallvis);
1696 : :
1697 : : /*
1698 : : * We now know the next block that we must process. It can be the
1699 : : * next block after the one we just processed, or something further
1700 : : * ahead. If it's further ahead, we can jump to it, but we choose to
1701 : : * do so only if we can skip at least SKIP_PAGES_THRESHOLD consecutive
1702 : : * pages. Since we're reading sequentially, the OS should be doing
1703 : : * readahead for us, so there's no gain in skipping a page now and
1704 : : * then. Skipping such a range might even discourage sequential
1705 : : * detection.
1706 : : *
1707 : : * This test also enables more frequent relfrozenxid advancement
1708 : : * during non-aggressive VACUUMs. If the range has any all-visible
1709 : : * pages then skipping makes updating relfrozenxid unsafe, which is a
1710 : : * real downside.
1711 : : */
1712 [ + + ]: 84264 : if (vacrel->next_unskippable_block - next_block >= SKIP_PAGES_THRESHOLD)
1713 : : {
1714 : 399 : next_block = vacrel->next_unskippable_block;
1715 [ + + ]: 399 : if (skipsallvis)
1716 : 137 : vacrel->skippedallvis = true;
1717 : : }
1718 : : }
1719 : :
1720 : : /* Now we must be in one of the two remaining states: */
1721 [ + + ]: 97428 : if (next_block < vacrel->next_unskippable_block)
1722 : : {
1723 : : /*
1724 : : * 2. We are processing a range of blocks that we could have skipped
1725 : : * but chose not to. We know that they are all-visible in the VM,
1726 : : * otherwise they would've been unskippable.
1727 : : */
559 melanieplageman@gmai 1728 : 13164 : vacrel->current_block = next_block;
1729 : : /* Block was not eager scanned */
213 1730 : 13164 : *((bool *) per_buffer_data) = false;
559 1731 : 13164 : return vacrel->current_block;
1732 : : }
1733 : : else
1734 : : {
1735 : : /*
1736 : : * 3. We reached the next unskippable block. Process it. On next
1737 : : * iteration, we will be back in state 1.
1738 : : */
899 heikki.linnakangas@i 1739 [ - + ]: 84264 : Assert(next_block == vacrel->next_unskippable_block);
1740 : :
559 melanieplageman@gmai 1741 : 84264 : vacrel->current_block = next_block;
213 1742 : 84264 : *((bool *) per_buffer_data) = vacrel->next_unskippable_eager_scanned;
559 1743 : 84264 : return vacrel->current_block;
1744 : : }
1745 : : }
1746 : :
1747 : : /*
1748 : : * Find the next unskippable block in a vacuum scan using the visibility map.
1749 : : * The next unskippable block and its visibility information is updated in
1750 : : * vacrel.
1751 : : *
1752 : : * Note: our opinion of which blocks can be skipped can go stale immediately.
1753 : : * It's okay if caller "misses" a page whose all-visible or all-frozen marking
1754 : : * was concurrently cleared, though. All that matters is that caller scan all
1755 : : * pages whose tuples might contain XIDs < OldestXmin, or MXIDs < OldestMxact.
1756 : : * (Actually, non-aggressive VACUUMs can choose to skip all-visible pages with
1757 : : * older XIDs/MXIDs. The *skippedallvis flag will be set here when the choice
1758 : : * to skip such a range is actually made, making everything safe.)
1759 : : */
1760 : : static void
899 heikki.linnakangas@i 1761 : 84264 : find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis)
1762 : : {
1763 : 84264 : BlockNumber rel_pages = vacrel->rel_pages;
1764 : 84264 : BlockNumber next_unskippable_block = vacrel->next_unskippable_block + 1;
1765 : 84264 : Buffer next_unskippable_vmbuffer = vacrel->next_unskippable_vmbuffer;
562 melanieplageman@gmai 1766 : 84264 : bool next_unskippable_eager_scanned = false;
1767 : :
899 heikki.linnakangas@i 1768 : 84264 : *skipsallvis = false;
1769 : :
562 melanieplageman@gmai 1770 : 44816 : for (;; next_unskippable_block++)
1607 pg@bowt.ie 1771 : 44816 : {
1772 : 129080 : uint8 mapbits = visibilitymap_get_status(vacrel->rel,
1773 : : next_unskippable_block,
1774 : : &next_unskippable_vmbuffer);
1775 : :
1776 : :
1777 : : /*
1778 : : * At the start of each eager scan region, normal vacuums with eager
1779 : : * scanning enabled reset the failure counter, allowing vacuum to
1780 : : * resume eager scanning if it had been suspended in the previous
1781 : : * region.
1782 : : */
562 melanieplageman@gmai 1783 [ - + ]: 129080 : if (next_unskippable_block >= vacrel->next_eager_scan_region_start)
1784 : : {
562 melanieplageman@gmai 1785 :UBC 0 : vacrel->eager_scan_remaining_fails =
1786 : 0 : vacrel->eager_scan_max_fails_per_region;
1787 : 0 : vacrel->next_eager_scan_region_start += EAGER_SCAN_REGION_SIZE;
1788 : : }
1789 : :
1790 : : /*
1791 : : * A block is unskippable if it is not all visible according to the
1792 : : * visibility map.
1793 : : */
213 melanieplageman@gmai 1794 [ + + ]:CBC 129080 : if ((mapbits & VISIBILITYMAP_ALL_VISIBLE) == 0)
1795 : : {
1607 pg@bowt.ie 1796 [ - + ]: 77600 : Assert((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0);
1797 : 77600 : break;
1798 : : }
1799 : :
1800 : : /*
1801 : : * Caller must scan the last page to determine whether it has tuples
1802 : : * (caller must have the opportunity to set vacrel->nonempty_pages).
1803 : : * This rule avoids having lazy_truncate_heap() take access-exclusive
1804 : : * lock on rel to attempt a truncation that fails anyway, just because
1805 : : * there are tuples on the last page (it is likely that there will be
1806 : : * tuples on other nearby pages as well, but those can be skipped).
1807 : : *
1808 : : * Implement this by always treating the last block as unsafe to skip.
1809 : : */
1810 [ + + ]: 51480 : if (next_unskippable_block == rel_pages - 1)
1811 : 2788 : break;
1812 : :
1813 : : /* DISABLE_PAGE_SKIPPING makes all skipping unsafe */
1814 [ + + ]: 48692 : if (!vacrel->skipwithvm)
1815 : 418 : break;
1816 : :
1817 : : /*
1818 : : * All-frozen pages cannot contain XIDs < OldestXmin (XIDs that aren't
1819 : : * already frozen by now), so this page can be skipped.
1820 : : */
562 melanieplageman@gmai 1821 [ + + ]: 48274 : if ((mapbits & VISIBILITYMAP_ALL_FROZEN) != 0)
1822 : 33537 : continue;
1823 : :
1824 : : /*
1825 : : * Aggressive vacuums cannot skip any all-visible pages that are not
1826 : : * also all-frozen.
1827 : : */
1828 [ + + ]: 14737 : if (vacrel->aggressive)
1829 : 3458 : break;
1830 : :
1831 : : /*
1832 : : * Normal vacuums with eager scanning enabled only skip all-visible
1833 : : * but not all-frozen pages if they have hit the failure limit for the
1834 : : * current eager scan region.
1835 : : */
1836 [ - + ]: 11279 : if (vacrel->eager_scan_remaining_fails > 0)
1837 : : {
562 melanieplageman@gmai 1838 :UBC 0 : next_unskippable_eager_scanned = true;
1839 : 0 : break;
1840 : : }
1841 : :
1842 : : /*
1843 : : * All-visible blocks are safe to skip in a normal vacuum. But
1844 : : * remember that the final range contains such a block for later.
1845 : : */
562 melanieplageman@gmai 1846 :CBC 11279 : *skipsallvis = true;
1847 : : }
1848 : :
1849 : : /* write the local variables back to vacrel */
899 heikki.linnakangas@i 1850 : 84264 : vacrel->next_unskippable_block = next_unskippable_block;
562 melanieplageman@gmai 1851 : 84264 : vacrel->next_unskippable_eager_scanned = next_unskippable_eager_scanned;
899 heikki.linnakangas@i 1852 : 84264 : vacrel->next_unskippable_vmbuffer = next_unskippable_vmbuffer;
1607 pg@bowt.ie 1853 : 84264 : }
1854 : :
1855 : : /*
1856 : : * lazy_scan_new_or_empty() -- lazy_scan_heap() new/empty page handling.
1857 : : *
1858 : : * Must call here to handle both new and empty pages before calling
1859 : : * lazy_scan_prune or lazy_scan_noprune, since they're not prepared to deal
1860 : : * with new or empty pages.
1861 : : *
1862 : : * It's necessary to consider new pages as a special case, since the rules for
1863 : : * maintaining the visibility map and FSM with empty pages are a little
1864 : : * different (though new pages can be truncated away during rel truncation).
1865 : : *
1866 : : * Empty pages are not really a special case -- they're just heap pages that
1867 : : * have no allocated tuples (including even LP_UNUSED items). You might
1868 : : * wonder why we need to handle them here all the same. It's only necessary
1869 : : * because of a corner-case involving a hard crash during heap relation
1870 : : * extension. If we ever make relation-extension crash safe, then it should
1871 : : * no longer be necessary to deal with empty pages here (or new pages, for
1872 : : * that matter).
1873 : : *
1874 : : * Caller must hold at least a shared lock. We might need to escalate the
1875 : : * lock in that case, so the type of lock caller holds needs to be specified
1876 : : * using 'sharelock' argument.
1877 : : *
1878 : : * Returns false in common case where caller should go on to call
1879 : : * lazy_scan_prune (or lazy_scan_noprune). Otherwise returns true, indicating
1880 : : * that lazy_scan_heap is done processing the page, releasing lock on caller's
1881 : : * behalf.
1882 : : *
1883 : : * No vm_page_frozen output parameter (like that passed to lazy_scan_prune())
1884 : : * is passed here because neither empty nor new pages can be eagerly frozen.
1885 : : * New pages are never frozen. Empty pages are always set frozen in the VM at
1886 : : * the same time that they are set all-visible, and we don't eagerly scan
1887 : : * frozen pages.
1888 : : */
1889 : : static bool
1658 1890 : 97428 : lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, BlockNumber blkno,
1891 : : Page page, bool sharelock, Buffer vmbuffer)
1892 : : {
1893 : : Size freespace;
1894 : :
1895 [ + + ]: 97428 : if (PageIsNew(page))
1896 : : {
1897 : : /*
1898 : : * All-zeroes pages can be left over if either a backend extends the
1899 : : * relation by a single page, but crashes before the newly initialized
1900 : : * page has been written out, or when bulk-extending the relation
1901 : : * (which creates a number of empty pages at the tail end of the
1902 : : * relation), and then enters them into the FSM.
1903 : : *
1904 : : * Note we do not enter the page into the visibilitymap. That has the
1905 : : * downside that we repeatedly visit this page in subsequent vacuums,
1906 : : * but otherwise we'll never discover the space on a promoted standby.
1907 : : * The harm of repeated checking ought to normally not be too bad. The
1908 : : * space usually should be used at some point, otherwise there
1909 : : * wouldn't be any regular vacuums.
1910 : : *
1911 : : * Make sure these pages are in the FSM, to ensure they can be reused.
1912 : : * Do that by testing if there's any space recorded for the page. If
1913 : : * not, enter it. We do so after releasing the lock on the heap page,
1914 : : * the FSM is approximate, after all.
1915 : : */
1916 : 1985 : UnlockReleaseBuffer(buf);
1917 : :
1918 [ + + ]: 1985 : if (GetRecordedFreeSpace(vacrel->rel, blkno) == 0)
1919 : : {
1920 : 758 : freespace = BLCKSZ - SizeOfPageHeaderData;
1921 : :
1922 : 758 : RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1923 : : }
1924 : :
1925 : 1985 : return true;
1926 : : }
1927 : :
1928 [ + + ]: 95443 : if (PageIsEmpty(page))
1929 : : {
1930 : : /*
1931 : : * It seems likely that caller will always be able to get a cleanup
1932 : : * lock on an empty page. But don't take any chances -- escalate to
1933 : : * an exclusive lock (still don't need a cleanup lock, though).
1934 : : */
1935 [ - + ]: 34 : if (sharelock)
1936 : : {
1658 pg@bowt.ie 1937 :UBC 0 : LockBuffer(buf, BUFFER_LOCK_UNLOCK);
1938 : 0 : LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
1939 : :
1940 [ # # ]: 0 : if (!PageIsEmpty(page))
1941 : : {
1942 : : /* page isn't new or empty -- keep lock and pin for now */
1943 : 0 : return false;
1944 : : }
1945 : : }
1946 : : else
1947 : : {
1948 : : /* Already have a full cleanup lock (which is more than enough) */
1949 : : }
1950 : :
1951 : : /*
1952 : : * Unlike new pages, empty pages are always set all-visible and
1953 : : * all-frozen.
1954 : : */
1658 pg@bowt.ie 1955 [ - + ]:CBC 34 : if (!PageIsAllVisible(page))
1956 : : {
1957 : : /* Lock vmbuffer before entering critical section */
156 melanieplageman@gmai 1958 :UBC 0 : LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE);
1959 : :
1658 pg@bowt.ie 1960 : 0 : START_CRIT_SECTION();
1961 : :
1962 : : /* mark buffer dirty before writing a WAL record */
1963 : 0 : MarkBufferDirty(buf);
1964 : :
156 melanieplageman@gmai 1965 : 0 : PageSetAllVisible(page);
1966 : 0 : PageClearPrunable(page);
1967 : 0 : visibilitymap_set(blkno,
1968 : : vmbuffer,
1969 : : VISIBILITYMAP_ALL_VISIBLE |
1970 : : VISIBILITYMAP_ALL_FROZEN,
1971 : 0 : vacrel->rel->rd_locator);
1972 : :
1973 : : /*
1974 : : * Emit WAL for setting PD_ALL_VISIBLE on the heap page and
1975 : : * setting the VM.
1976 : : */
1977 [ # # # # : 0 : if (RelationNeedsWAL(vacrel->rel))
# # # # ]
1978 : 0 : log_heap_prune_and_freeze(vacrel->rel, buf,
1979 : : vmbuffer,
1980 : : VISIBILITYMAP_ALL_VISIBLE |
1981 : : VISIBILITYMAP_ALL_FROZEN,
1982 : : InvalidTransactionId, /* conflict xid */
1983 : : false, /* cleanup lock */
1984 : : PRUNE_VACUUM_SCAN, /* reason */
1985 : : NULL, 0,
1986 : : NULL, 0,
1987 : : NULL, 0,
1988 : : NULL, 0);
1989 : :
1658 pg@bowt.ie 1990 [ # # ]: 0 : END_CRIT_SECTION();
1991 : :
156 melanieplageman@gmai 1992 : 0 : LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
1993 : :
1994 : : /* Count the newly all-frozen pages for logging */
182 1995 : 0 : vacrel->new_all_visible_pages++;
1996 : 0 : vacrel->new_all_visible_all_frozen_pages++;
1997 : : }
1998 : :
1658 pg@bowt.ie 1999 :CBC 34 : freespace = PageGetHeapFreeSpace(page);
2000 : 34 : UnlockReleaseBuffer(buf);
2001 : 34 : RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
2002 : 34 : return true;
2003 : : }
2004 : :
2005 : : /* page isn't new or empty -- keep lock and pin */
2006 : 95409 : return false;
2007 : : }
2008 : :
2009 : : /* qsort comparator for sorting OffsetNumbers */
2010 : : static int
876 heikki.linnakangas@i 2011 : 5821424 : cmpOffsetNumbers(const void *a, const void *b)
2012 : : {
2013 : 5821424 : return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
2014 : : }
2015 : :
2016 : : /*
2017 : : * lazy_scan_prune() -- lazy_scan_heap() pruning and freezing.
2018 : : *
2019 : : * Caller must hold pin and buffer cleanup lock on the buffer.
2020 : : *
2021 : : * vmbuffer is the buffer containing the VM block with visibility information
2022 : : * for the heap block, blkno.
2023 : : *
2024 : : * *has_lpdead_items is set to true or false depending on whether, upon return
2025 : : * from this function, any LP_DEAD items are still present on the page.
2026 : : *
2027 : : * *vm_page_frozen is set to true if the page is newly set all-frozen in the
2028 : : * VM. The caller currently only uses this for determining whether an eagerly
2029 : : * scanned page was successfully set all-frozen.
2030 : : *
2031 : : * Returns the number of tuples deleted from the page during HOT pruning.
2032 : : */
2033 : : static int
1969 pg@bowt.ie 2034 : 95395 : lazy_scan_prune(LVRelState *vacrel,
2035 : : Buffer buf,
2036 : : BlockNumber blkno,
2037 : : Page page,
2038 : : Buffer vmbuffer,
2039 : : bool *has_lpdead_items,
2040 : : bool *vm_page_frozen)
2041 : : {
2042 : 95395 : Relation rel = vacrel->rel;
2043 : : PruneFreezeResult presult;
280 melanieplageman@gmai 2044 : 95395 : PruneFreezeParams params = {
2045 : : .relation = rel,
2046 : : .buffer = buf,
2047 : : .vmbuffer = vmbuffer,
2048 : : .reason = PRUNE_VACUUM_SCAN,
2049 : : .options = HEAP_PAGE_PRUNE_FREEZE | HEAP_PAGE_PRUNE_SET_VM,
2050 : 95395 : .vistest = vacrel->vistest,
2051 : 95395 : .cutoffs = &vacrel->cutoffs,
2052 : : };
2053 : :
1658 pg@bowt.ie 2054 [ - + ]: 95395 : Assert(BufferGetBlockNumber(buf) == blkno);
2055 : :
2056 : : /*
2057 : : * Prune all HOT-update chains and potentially freeze tuples on this page.
2058 : : *
2059 : : * If the relation has no indexes, we can immediately mark would-be dead
2060 : : * items LP_UNUSED.
2061 : : *
2062 : : * The number of tuples removed from the page is returned in
2063 : : * presult.ndeleted. It should not be confused with presult.lpdead_items;
2064 : : * presult.lpdead_items's final value can be thought of as the number of
2065 : : * tuples that were deleted from indexes.
2066 : : *
2067 : : * We will update the VM after collecting LP_DEAD items and freezing
2068 : : * tuples. Pruning will have determined whether or not the page is
2069 : : * all-visible.
2070 : : */
876 heikki.linnakangas@i 2071 [ + + ]: 95395 : if (vacrel->nindexes == 0)
280 melanieplageman@gmai 2072 : 8516 : params.options |= HEAP_PAGE_PRUNE_MARK_UNUSED_NOW;
2073 : :
2074 : : /*
2075 : : * Allow skipping full inspection of pages that the VM indicates are
2076 : : * already all-frozen (which may be scanned due to SKIP_PAGES_THRESHOLD).
2077 : : * However, if DISABLE_PAGE_SKIPPING was specified, we can't trust the VM,
2078 : : * so we must examine the page to make sure it is truly all-frozen and fix
2079 : : * it otherwise.
2080 : : */
158 2081 [ + + ]: 95395 : if (vacrel->skipwithvm)
2082 : 94081 : params.options |= HEAP_PAGE_PRUNE_ALLOW_FAST_PATH;
2083 : :
280 2084 : 95395 : heap_page_prune_and_freeze(¶ms,
2085 : : &presult,
2086 : : &vacrel->offnum,
2087 : : &vacrel->NewRelfrozenXid, &vacrel->NewRelminMxid);
2088 : :
876 heikki.linnakangas@i 2089 [ - + ]: 95395 : Assert(MultiXactIdIsValid(vacrel->NewRelminMxid));
2090 [ - + ]: 95395 : Assert(TransactionIdIsValid(vacrel->NewRelfrozenXid));
2091 : :
2092 [ + + ]: 95395 : if (presult.nfrozen > 0)
2093 : : {
2094 : : /*
2095 : : * We don't increment the new_frozen_tuple_pages instrumentation
2096 : : * counter when nfrozen == 0, since it only counts pages with newly
2097 : : * frozen tuples (don't confuse that with pages newly set all-frozen
2098 : : * in VM).
2099 : : */
618 melanieplageman@gmai 2100 : 25193 : vacrel->new_frozen_tuple_pages++;
2101 : : }
2102 : :
2103 : : /*
2104 : : * Now save details of the LP_DEAD items from the page in vacrel
2105 : : */
876 heikki.linnakangas@i 2106 [ + + ]: 95395 : if (presult.lpdead_items > 0)
2107 : : {
1969 pg@bowt.ie 2108 : 23578 : vacrel->lpdead_item_pages++;
2109 : :
2110 : : /*
2111 : : * deadoffsets are collected incrementally in
2112 : : * heap_page_prune_and_freeze() as each dead line pointer is recorded,
2113 : : * with an indeterminate order, but dead_items_add requires them to be
2114 : : * sorted.
2115 : : */
876 heikki.linnakangas@i 2116 : 23578 : qsort(presult.deadoffsets, presult.lpdead_items, sizeof(OffsetNumber),
2117 : : cmpOffsetNumbers);
2118 : :
2119 : 23578 : dead_items_add(vacrel, blkno, presult.deadoffsets, presult.lpdead_items);
2120 : : }
2121 : :
2122 : : /* Finally, add page-local counts to whole-VACUUM counts */
156 melanieplageman@gmai 2123 [ + + ]: 95395 : if (presult.newly_all_visible)
2124 : 43934 : vacrel->new_all_visible_pages++;
2125 [ + + ]: 95395 : if (presult.newly_all_visible_frozen)
2126 : 29651 : vacrel->new_all_visible_all_frozen_pages++;
2127 [ + + ]: 95395 : if (presult.newly_all_frozen)
2128 : 3484 : vacrel->new_all_frozen_pages++;
2129 : :
2130 : : /* Capture if the page was newly set frozen */
2131 [ + + ]: 161139 : *vm_page_frozen = presult.newly_all_visible_frozen ||
2132 [ + + ]: 65744 : presult.newly_all_frozen;
2133 : :
1064 rhaas@postgresql.org 2134 : 95395 : vacrel->tuples_deleted += presult.ndeleted;
876 heikki.linnakangas@i 2135 : 95395 : vacrel->tuples_frozen += presult.nfrozen;
2136 : 95395 : vacrel->lpdead_items += presult.lpdead_items;
2137 : 95395 : vacrel->live_tuples += presult.live_tuples;
2138 : 95395 : vacrel->recently_dead_tuples += presult.recently_dead_tuples;
2139 : :
2140 : : /* Can't truncate this page */
2141 [ + + ]: 95395 : if (presult.hastup)
959 rhaas@postgresql.org 2142 : 83645 : vacrel->nonempty_pages = blkno + 1;
2143 : :
2144 : : /* Did we find LP_DEAD items? */
876 heikki.linnakangas@i 2145 : 95395 : *has_lpdead_items = (presult.lpdead_items > 0);
2146 : :
422 msawada@postgresql.o 2147 : 95395 : return presult.ndeleted;
2148 : : }
2149 : :
2150 : : /*
2151 : : * lazy_scan_noprune() -- lazy_scan_prune() without pruning or freezing
2152 : : *
2153 : : * Caller need only hold a pin and share lock on the buffer, unlike
2154 : : * lazy_scan_prune, which requires a full cleanup lock. While pruning isn't
2155 : : * performed here, it's quite possible that an earlier opportunistic pruning
2156 : : * operation left LP_DEAD items behind. We'll at least collect any such items
2157 : : * in dead_items for removal from indexes.
2158 : : *
2159 : : * For aggressive VACUUM callers, we may return false to indicate that a full
2160 : : * cleanup lock is required for processing by lazy_scan_prune. This is only
2161 : : * necessary when the aggressive VACUUM needs to freeze some tuple XIDs from
2162 : : * one or more tuples on the page. We always return true for non-aggressive
2163 : : * callers.
2164 : : *
2165 : : * If this function returns true, *has_lpdead_items gets set to true or false
2166 : : * depending on whether, upon return from this function, any LP_DEAD items are
2167 : : * present on the page. If this function returns false, *has_lpdead_items
2168 : : * is not updated.
2169 : : */
2170 : : static bool
1658 pg@bowt.ie 2171 : 64 : lazy_scan_noprune(LVRelState *vacrel,
2172 : : Buffer buf,
2173 : : BlockNumber blkno,
2174 : : Page page,
2175 : : bool *has_lpdead_items)
2176 : : {
2177 : : OffsetNumber offnum,
2178 : : maxoff;
2179 : : int lpdead_items,
2180 : : live_tuples,
2181 : : recently_dead_tuples,
2182 : : missed_dead_tuples;
2183 : : bool hastup;
2184 : : HeapTupleHeader tupleheader;
1338 2185 : 64 : TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid;
2186 : 64 : MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid;
2187 : : OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
2188 : :
1658 2189 [ - + ]: 64 : Assert(BufferGetBlockNumber(buf) == blkno);
2190 : :
959 rhaas@postgresql.org 2191 : 64 : hastup = false; /* for now */
2192 : :
1658 pg@bowt.ie 2193 : 64 : lpdead_items = 0;
2194 : 64 : live_tuples = 0;
2195 : 64 : recently_dead_tuples = 0;
2196 : 64 : missed_dead_tuples = 0;
2197 : :
2198 : 64 : maxoff = PageGetMaxOffsetNumber(page);
2199 : 64 : for (offnum = FirstOffsetNumber;
2200 [ + + ]: 1360 : offnum <= maxoff;
2201 : 1296 : offnum = OffsetNumberNext(offnum))
2202 : : {
2203 : : ItemId itemid;
2204 : : HeapTupleData tuple;
2205 : :
2206 : 1346 : vacrel->offnum = offnum;
2207 : 1346 : itemid = PageGetItemId(page, offnum);
2208 : :
2209 [ - + ]: 1346 : if (!ItemIdIsUsed(itemid))
2210 : 577 : continue;
2211 : :
2212 [ - + ]: 1346 : if (ItemIdIsRedirected(itemid))
2213 : : {
959 rhaas@postgresql.org 2214 :UBC 0 : hastup = true;
1658 pg@bowt.ie 2215 : 0 : continue;
2216 : : }
2217 : :
1658 pg@bowt.ie 2218 [ + + ]:CBC 1346 : if (ItemIdIsDead(itemid))
2219 : : {
2220 : : /*
2221 : : * Deliberately don't set hastup=true here. See same point in
2222 : : * lazy_scan_prune for an explanation.
2223 : : */
2224 : 577 : deadoffsets[lpdead_items++] = offnum;
2225 : 577 : continue;
2226 : : }
2227 : :
959 rhaas@postgresql.org 2228 : 769 : hastup = true; /* page prevents rel truncation */
1658 pg@bowt.ie 2229 : 769 : tupleheader = (HeapTupleHeader) PageGetItem(page, itemid);
1338 2230 [ + + ]: 769 : if (heap_tuple_should_freeze(tupleheader, &vacrel->cutoffs,
2231 : : &NoFreezePageRelfrozenXid,
2232 : : &NoFreezePageRelminMxid))
2233 : : {
2234 : : /* Tuple with XID < FreezeLimit (or MXID < MultiXactCutoff) */
1658 2235 [ + + ]: 114 : if (vacrel->aggressive)
2236 : : {
2237 : : /*
2238 : : * Aggressive VACUUMs must always be able to advance rel's
2239 : : * relfrozenxid to a value >= FreezeLimit (and be able to
2240 : : * advance rel's relminmxid to a value >= MultiXactCutoff).
2241 : : * The ongoing aggressive VACUUM won't be able to do that
2242 : : * unless it can freeze an XID (or MXID) from this tuple now.
2243 : : *
2244 : : * The only safe option is to have caller perform processing
2245 : : * of this page using lazy_scan_prune. Caller might have to
2246 : : * wait a while for a cleanup lock, but it can't be helped.
2247 : : */
2248 : 50 : vacrel->offnum = InvalidOffsetNumber;
2249 : 50 : return false;
2250 : : }
2251 : :
2252 : : /*
2253 : : * Non-aggressive VACUUMs are under no obligation to advance
2254 : : * relfrozenxid (even by one XID). We can be much laxer here.
2255 : : *
2256 : : * Currently we always just accept an older final relfrozenxid
2257 : : * and/or relminmxid value. We never make caller wait or work a
2258 : : * little harder, even when it likely makes sense to do so.
2259 : : */
2260 : : }
2261 : :
2262 : 719 : ItemPointerSet(&(tuple.t_self), blkno, offnum);
2263 : 719 : tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
2264 : 719 : tuple.t_len = ItemIdGetLength(itemid);
2265 : 719 : tuple.t_tableOid = RelationGetRelid(vacrel->rel);
2266 : :
1344 2267 [ + + + + : 719 : switch (HeapTupleSatisfiesVacuum(&tuple, vacrel->cutoffs.OldestXmin,
- ]
2268 : : buf))
2269 : : {
1658 2270 : 645 : case HEAPTUPLE_DELETE_IN_PROGRESS:
2271 : : case HEAPTUPLE_LIVE:
2272 : :
2273 : : /*
2274 : : * Count both cases as live, just like lazy_scan_prune
2275 : : */
2276 : 645 : live_tuples++;
2277 : :
2278 : 645 : break;
2279 : 3 : case HEAPTUPLE_DEAD:
2280 : :
2281 : : /*
2282 : : * There is some useful work for pruning to do, that won't be
2283 : : * done due to failure to get a cleanup lock.
2284 : : */
2285 : 3 : missed_dead_tuples++;
2286 : 3 : break;
2287 : 5 : case HEAPTUPLE_RECENTLY_DEAD:
2288 : :
2289 : : /*
2290 : : * Count in recently_dead_tuples, just like lazy_scan_prune
2291 : : */
2292 : 5 : recently_dead_tuples++;
2293 : 5 : break;
2294 : 66 : case HEAPTUPLE_INSERT_IN_PROGRESS:
2295 : :
2296 : : /*
2297 : : * Do not count these rows as live, just like lazy_scan_prune
2298 : : */
2299 : 66 : break;
1658 pg@bowt.ie 2300 :UBC 0 : default:
2301 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
2302 : : break;
2303 : : }
2304 : : }
2305 : :
1658 pg@bowt.ie 2306 :CBC 14 : vacrel->offnum = InvalidOffsetNumber;
2307 : :
2308 : : /*
2309 : : * By here we know for sure that caller can put off freezing and pruning
2310 : : * this particular page until the next VACUUM. Remember its details now.
2311 : : * (lazy_scan_prune expects a clean slate, so we have to do this last.)
2312 : : */
1338 2313 : 14 : vacrel->NewRelfrozenXid = NoFreezePageRelfrozenXid;
2314 : 14 : vacrel->NewRelminMxid = NoFreezePageRelminMxid;
2315 : :
2316 : : /* Save any LP_DEAD items found on the page in dead_items */
1658 2317 [ - + ]: 14 : if (vacrel->nindexes == 0)
2318 : : {
2319 : : /* Using one-pass strategy (since table has no indexes) */
1658 pg@bowt.ie 2320 [ # # ]:UBC 0 : if (lpdead_items > 0)
2321 : : {
2322 : : /*
2323 : : * Perfunctory handling for the corner case where a single pass
2324 : : * strategy VACUUM cannot get a cleanup lock, and it turns out
2325 : : * that there is one or more LP_DEAD items: just count the LP_DEAD
2326 : : * items as missed_dead_tuples instead. (This is a bit dishonest,
2327 : : * but it beats having to maintain specialized heap vacuuming code
2328 : : * forever, for vanishingly little benefit.)
2329 : : */
959 rhaas@postgresql.org 2330 : 0 : hastup = true;
1658 pg@bowt.ie 2331 : 0 : missed_dead_tuples += lpdead_items;
2332 : : }
2333 : : }
954 rhaas@postgresql.org 2334 [ + + ]:CBC 14 : else if (lpdead_items > 0)
2335 : : {
2336 : : /*
2337 : : * Page has LP_DEAD items, and so any references/TIDs that remain in
2338 : : * indexes will be deleted during index vacuuming (and then marked
2339 : : * LP_UNUSED in the heap)
2340 : : */
1658 pg@bowt.ie 2341 : 3 : vacrel->lpdead_item_pages++;
2342 : :
877 msawada@postgresql.o 2343 : 3 : dead_items_add(vacrel, blkno, deadoffsets, lpdead_items);
2344 : :
1658 pg@bowt.ie 2345 : 3 : vacrel->lpdead_items += lpdead_items;
2346 : : }
2347 : :
2348 : : /*
2349 : : * Finally, add relevant page-local counts to whole-VACUUM counts
2350 : : */
1645 2351 : 14 : vacrel->live_tuples += live_tuples;
1658 2352 : 14 : vacrel->recently_dead_tuples += recently_dead_tuples;
2353 : 14 : vacrel->missed_dead_tuples += missed_dead_tuples;
2354 [ + + ]: 14 : if (missed_dead_tuples > 0)
2355 : 3 : vacrel->missed_dead_pages++;
2356 : :
2357 : : /* Can't truncate this page */
959 rhaas@postgresql.org 2358 [ + + ]: 14 : if (hastup)
2359 : 12 : vacrel->nonempty_pages = blkno + 1;
2360 : :
2361 : : /* Did we find LP_DEAD items? */
954 2362 : 14 : *has_lpdead_items = (lpdead_items > 0);
2363 : :
2364 : : /* Caller won't need to call lazy_scan_prune with same page */
1658 pg@bowt.ie 2365 : 14 : return true;
2366 : : }
2367 : :
2368 : : /*
2369 : : * Main entry point for index vacuuming and heap vacuuming.
2370 : : *
2371 : : * Removes items collected in dead_items from table's indexes, then marks the
2372 : : * same items LP_UNUSED in the heap. See the comments above lazy_scan_heap
2373 : : * for full details.
2374 : : *
2375 : : * Also empties dead_items, freeing up space for later TIDs.
2376 : : *
2377 : : * We may choose to bypass index vacuuming at this point, though only when the
2378 : : * ongoing VACUUM operation will definitely only have one index scan/round of
2379 : : * index vacuuming.
2380 : : */
2381 : : static void
1896 2382 : 1050 : lazy_vacuum(LVRelState *vacrel)
2383 : : {
2384 : : bool bypass;
2385 : :
2386 : : /* Should not end up here with no indexes */
1969 2387 [ - + ]: 1050 : Assert(vacrel->nindexes > 0);
2388 [ - + ]: 1050 : Assert(vacrel->lpdead_item_pages > 0);
2389 : :
2390 [ + + ]: 1050 : if (!vacrel->do_index_vacuuming)
2391 : : {
2392 [ - + ]: 10 : Assert(!vacrel->do_index_cleanup);
877 msawada@postgresql.o 2393 : 10 : dead_items_reset(vacrel);
1969 pg@bowt.ie 2394 : 10 : return;
2395 : : }
2396 : :
2397 : : /*
2398 : : * Consider bypassing index vacuuming (and heap vacuuming) entirely.
2399 : : *
2400 : : * We currently only do this in cases where the number of LP_DEAD items
2401 : : * for the entire VACUUM operation is close to zero. This avoids sharp
2402 : : * discontinuities in the duration and overhead of successive VACUUM
2403 : : * operations that run against the same table with a fixed workload.
2404 : : * Ideally, successive VACUUM operations will behave as if there are
2405 : : * exactly zero LP_DEAD items in cases where there are close to zero.
2406 : : *
2407 : : * This is likely to be helpful with a table that is continually affected
2408 : : * by UPDATEs that can mostly apply the HOT optimization, but occasionally
2409 : : * have small aberrations that lead to just a few heap pages retaining
2410 : : * only one or two LP_DEAD items. This is pretty common; even when the
2411 : : * DBA goes out of their way to make UPDATEs use HOT, it is practically
2412 : : * impossible to predict whether HOT will be applied in 100% of cases.
2413 : : * It's far easier to ensure that 99%+ of all UPDATEs against a table use
2414 : : * HOT through careful tuning.
2415 : : */
1896 2416 : 1040 : bypass = false;
2417 [ + + + - ]: 1040 : if (vacrel->consider_bypass_optimization && vacrel->rel_pages > 0)
2418 : : {
2419 : : BlockNumber threshold;
2420 : :
1968 2421 [ - + ]: 1016 : Assert(vacrel->num_index_scans == 0);
877 msawada@postgresql.o 2422 [ - + ]: 1016 : Assert(vacrel->lpdead_items == vacrel->dead_items_info->num_items);
1968 pg@bowt.ie 2423 [ - + ]: 1016 : Assert(vacrel->do_index_vacuuming);
2424 [ - + ]: 1016 : Assert(vacrel->do_index_cleanup);
2425 : :
2426 : : /*
2427 : : * This crossover point at which we'll start to do index vacuuming is
2428 : : * expressed as a percentage of the total number of heap pages in the
2429 : : * table that are known to have at least one LP_DEAD item. This is
2430 : : * much more important than the total number of LP_DEAD items, since
2431 : : * it's a proxy for the number of heap pages whose visibility map bits
2432 : : * cannot be set on account of bypassing index and heap vacuuming.
2433 : : *
2434 : : * We apply one further precautionary test: the space currently used
2435 : : * to store the TIDs (TIDs that now all point to LP_DEAD items) must
2436 : : * not exceed 32MB. This limits the risk that we will bypass index
2437 : : * vacuuming again and again until eventually there is a VACUUM whose
2438 : : * dead_items space is not CPU cache resident.
2439 : : *
2440 : : * We don't take any special steps to remember the LP_DEAD items (such
2441 : : * as counting them in our final update to the stats system) when the
2442 : : * optimization is applied. Though the accounting used in analyze.c's
2443 : : * acquire_sample_rows() will recognize the same LP_DEAD items as dead
2444 : : * rows in its own stats report, that's okay. The discrepancy should
2445 : : * be negligible. If this optimization is ever expanded to cover more
2446 : : * cases then this may need to be reconsidered.
2447 : : */
2448 : 1016 : threshold = (double) vacrel->rel_pages * BYPASS_THRESHOLD_PAGES;
1896 2449 [ + + + - ]: 1020 : bypass = (vacrel->lpdead_item_pages < threshold &&
573 tgl@sss.pgh.pa.us 2450 : 4 : TidStoreMemoryUsage(vacrel->dead_items) < 32 * 1024 * 1024);
2451 : : }
2452 : :
1896 pg@bowt.ie 2453 [ + + ]: 1040 : if (bypass)
2454 : : {
2455 : : /*
2456 : : * There are almost zero TIDs. Behave as if there were precisely
2457 : : * zero: bypass index vacuuming, but do index cleanup.
2458 : : *
2459 : : * We expect that the ongoing VACUUM operation will finish very
2460 : : * quickly, so there is no point in considering speeding up as a
2461 : : * failsafe against wraparound failure. (Index cleanup is expected to
2462 : : * finish very quickly in cases where there were no ambulkdelete()
2463 : : * calls.)
2464 : : */
1968 2465 : 4 : vacrel->do_index_vacuuming = false;
2466 : : }
2467 [ + - ]: 1036 : else if (lazy_vacuum_all_indexes(vacrel))
2468 : : {
2469 : : /*
2470 : : * We successfully completed a round of index vacuuming. Do related
2471 : : * heap vacuuming now.
2472 : : */
2473 : 1034 : lazy_vacuum_heap_rel(vacrel);
2474 : : }
2475 : : else
2476 : : {
2477 : : /*
2478 : : * Failsafe case.
2479 : : *
2480 : : * We attempted index vacuuming, but didn't finish a full round/full
2481 : : * index scan. This happens when relfrozenxid or relminmxid is too
2482 : : * far in the past.
2483 : : *
2484 : : * From this point on the VACUUM operation will do no further index
2485 : : * vacuuming or heap vacuuming. This VACUUM operation won't end up
2486 : : * back here again.
2487 : : */
1238 dgustafsson@postgres 2488 [ # # ]:UBC 0 : Assert(VacuumFailsafeActive);
2489 : : }
2490 : :
2491 : : /*
2492 : : * Forget the LP_DEAD items that we just vacuumed (or just decided to not
2493 : : * vacuum)
2494 : : */
877 msawada@postgresql.o 2495 :CBC 1038 : dead_items_reset(vacrel);
2496 : : }
2497 : :
2498 : : /*
2499 : : * lazy_vacuum_all_indexes() -- Main entry for index vacuuming
2500 : : *
2501 : : * Returns true in the common case when all indexes were successfully
2502 : : * vacuumed. Returns false in rare cases where we determined that the ongoing
2503 : : * VACUUM operation is at risk of taking too long to finish, leading to
2504 : : * wraparound failure.
2505 : : */
2506 : : static bool
1970 pg@bowt.ie 2507 : 1036 : lazy_vacuum_all_indexes(LVRelState *vacrel)
2508 : : {
1968 2509 : 1036 : bool allindexes = true;
1344 2510 : 1036 : double old_live_tuples = vacrel->rel->rd_rel->reltuples;
1143 msawada@postgresql.o 2511 : 1036 : const int progress_start_index[] = {
2512 : : PROGRESS_VACUUM_PHASE,
2513 : : PROGRESS_VACUUM_INDEXES_TOTAL
2514 : : };
2515 : 1036 : const int progress_end_index[] = {
2516 : : PROGRESS_VACUUM_INDEXES_TOTAL,
2517 : : PROGRESS_VACUUM_INDEXES_PROCESSED,
2518 : : PROGRESS_VACUUM_NUM_INDEX_VACUUMS
2519 : : };
2520 : : int64 progress_start_val[2];
2521 : : int64 progress_end_val[3];
2522 : :
1970 pg@bowt.ie 2523 [ - + ]: 1036 : Assert(vacrel->nindexes > 0);
1969 2524 [ - + ]: 1036 : Assert(vacrel->do_index_vacuuming);
2525 [ - + ]: 1036 : Assert(vacrel->do_index_cleanup);
2526 : :
2527 : : /* Precheck for XID wraparound emergencies */
1968 2528 [ - + ]: 1036 : if (lazy_check_wraparound_failsafe(vacrel))
2529 : : {
2530 : : /* Wraparound emergency -- don't even start an index scan */
1968 pg@bowt.ie 2531 :UBC 0 : return false;
2532 : : }
2533 : :
2534 : : /*
2535 : : * Report that we are now vacuuming indexes and the number of indexes to
2536 : : * vacuum.
2537 : : */
1143 msawada@postgresql.o 2538 :CBC 1036 : progress_start_val[0] = PROGRESS_VACUUM_PHASE_VACUUM_INDEX;
2539 : 1036 : progress_start_val[1] = vacrel->nindexes;
2540 : 1036 : pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val);
2541 : :
1970 pg@bowt.ie 2542 [ + + ]: 1036 : if (!ParallelVacuumIsActive(vacrel))
2543 : : {
2544 [ + + ]: 3067 : for (int idx = 0; idx < vacrel->nindexes; idx++)
2545 : : {
2546 : 2046 : Relation indrel = vacrel->indrels[idx];
2547 : 2046 : IndexBulkDeleteResult *istat = vacrel->indstats[idx];
2548 : :
1344 2549 : 2046 : vacrel->indstats[idx] = lazy_vacuum_one_index(indrel, istat,
2550 : : old_live_tuples,
2551 : : vacrel);
2552 : :
2553 : : /* Report the number of indexes vacuumed */
1143 msawada@postgresql.o 2554 : 2045 : pgstat_progress_update_param(PROGRESS_VACUUM_INDEXES_PROCESSED,
2555 : 2045 : idx + 1);
2556 : :
1968 pg@bowt.ie 2557 [ - + ]: 2045 : if (lazy_check_wraparound_failsafe(vacrel))
2558 : : {
2559 : : /* Wraparound emergency -- end current index scan */
1968 pg@bowt.ie 2560 :UBC 0 : allindexes = false;
2561 : 0 : break;
2562 : : }
2563 : : }
2564 : : }
2565 : : else
2566 : : {
2567 : : /* Outsource everything to parallel variant */
1344 pg@bowt.ie 2568 :CBC 14 : parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples,
2569 : : vacrel->num_index_scans,
2570 : : &(vacrel->worker_usage.vacuum));
2571 : :
2572 : : /*
2573 : : * Do a postcheck to consider applying wraparound failsafe now. Note
2574 : : * that parallel VACUUM only gets the precheck and this postcheck.
2575 : : */
1968 2576 [ - + ]: 13 : if (lazy_check_wraparound_failsafe(vacrel))
1968 pg@bowt.ie 2577 :UBC 0 : allindexes = false;
2578 : : }
2579 : :
2580 : : /*
2581 : : * We delete all LP_DEAD items from the first heap pass in all indexes on
2582 : : * each call here (except calls where we choose to do the failsafe). This
2583 : : * makes the next call to lazy_vacuum_heap_rel() safe (except in the event
2584 : : * of the failsafe triggering, which prevents the next call from taking
2585 : : * place).
2586 : : */
1969 pg@bowt.ie 2587 [ + + - + ]:CBC 1034 : Assert(vacrel->num_index_scans > 0 ||
2588 : : vacrel->dead_items_info->num_items == vacrel->lpdead_items);
1238 dgustafsson@postgres 2589 [ - + - - ]: 1034 : Assert(allindexes || VacuumFailsafeActive);
2590 : :
2591 : : /*
2592 : : * Increase and report the number of index scans. Also, we reset
2593 : : * PROGRESS_VACUUM_INDEXES_TOTAL and PROGRESS_VACUUM_INDEXES_PROCESSED.
2594 : : *
2595 : : * We deliberately include the case where we started a round of bulk
2596 : : * deletes that we weren't able to finish due to the failsafe triggering.
2597 : : */
1970 pg@bowt.ie 2598 : 1034 : vacrel->num_index_scans++;
1143 msawada@postgresql.o 2599 : 1034 : progress_end_val[0] = 0;
2600 : 1034 : progress_end_val[1] = 0;
2601 : 1034 : progress_end_val[2] = vacrel->num_index_scans;
2602 : 1034 : pgstat_progress_update_multi_param(3, progress_end_index, progress_end_val);
2603 : :
1968 pg@bowt.ie 2604 : 1034 : return allindexes;
2605 : : }
2606 : :
2607 : : /*
2608 : : * Read stream callback for vacuum's third phase (second pass over the heap).
2609 : : * Gets the next block from the TID store and returns it or InvalidBlockNumber
2610 : : * if there are no further blocks to vacuum.
2611 : : *
2612 : : * NB: Assumed to be safe to use with READ_STREAM_USE_BATCHING.
2613 : : */
2614 : : static BlockNumber
559 melanieplageman@gmai 2615 : 24392 : vacuum_reap_lp_read_stream_next(ReadStream *stream,
2616 : : void *callback_private_data,
2617 : : void *per_buffer_data)
2618 : : {
2619 : 24392 : TidStoreIter *iter = callback_private_data;
2620 : : TidStoreIterResult *iter_result;
2621 : :
2622 : 24392 : iter_result = TidStoreIterateNext(iter);
2623 [ + + ]: 24392 : if (iter_result == NULL)
2624 : 1034 : return InvalidBlockNumber;
2625 : :
2626 : : /*
2627 : : * Save the TidStoreIterResult for later, so we can extract the offsets.
2628 : : * It is safe to copy the result, according to TidStoreIterateNext().
2629 : : */
2630 : 23358 : memcpy(per_buffer_data, iter_result, sizeof(*iter_result));
2631 : :
2632 : 23358 : return iter_result->blkno;
2633 : : }
2634 : :
2635 : : /*
2636 : : * lazy_vacuum_heap_rel() -- second pass over the heap for two pass strategy
2637 : : *
2638 : : * This routine marks LP_DEAD items in vacrel->dead_items as LP_UNUSED. Pages
2639 : : * that never had lazy_scan_prune record LP_DEAD items are not visited at all.
2640 : : *
2641 : : * We may also be able to truncate the line pointer array of the heap pages we
2642 : : * visit. If there is a contiguous group of LP_UNUSED items at the end of the
2643 : : * array, it can be reclaimed as free space. These LP_UNUSED items usually
2644 : : * start out as LP_DEAD items recorded by lazy_scan_prune (we set items from
2645 : : * each page to LP_UNUSED, and then consider if it's possible to truncate the
2646 : : * page's line pointer array).
2647 : : *
2648 : : * Note: the reason for doing this as a second pass is we cannot remove the
2649 : : * tuples until we've removed their index entries, and we want to process
2650 : : * index entry removal in batches as large as possible.
2651 : : */
2652 : : static void
1970 pg@bowt.ie 2653 : 1034 : lazy_vacuum_heap_rel(LVRelState *vacrel)
2654 : : {
2655 : : ReadStream *stream;
1324 2656 : 1034 : BlockNumber vacuumed_pages = 0;
4943 heikki.linnakangas@i 2657 : 1034 : Buffer vmbuffer = InvalidBuffer;
2658 : : LVSavedErrInfo saved_err_info;
2659 : : TidStoreIter *iter;
2660 : :
1969 pg@bowt.ie 2661 [ - + ]: 1034 : Assert(vacrel->do_index_vacuuming);
2662 [ - + ]: 1034 : Assert(vacrel->do_index_cleanup);
2663 [ - + ]: 1034 : Assert(vacrel->num_index_scans > 0);
2664 : :
2665 : : /* Report that we are now vacuuming the heap */
2436 michael@paquier.xyz 2666 : 1034 : pgstat_progress_update_param(PROGRESS_VACUUM_PHASE,
2667 : : PROGRESS_VACUUM_PHASE_VACUUM_HEAP);
2668 : :
2669 : : /* Update error traceback information */
1970 pg@bowt.ie 2670 : 1034 : update_vacuum_error_info(vacrel, &saved_err_info,
2671 : : VACUUM_ERRCB_PHASE_VACUUM_HEAP,
2672 : : InvalidBlockNumber, InvalidOffsetNumber);
2673 : :
877 msawada@postgresql.o 2674 : 1034 : iter = TidStoreBeginIterate(vacrel->dead_items);
2675 : :
2676 : : /*
2677 : : * Set up the read stream for vacuum's second pass through the heap.
2678 : : *
2679 : : * It is safe to use batchmode, as vacuum_reap_lp_read_stream_next() does
2680 : : * not need to wait for IO and does not perform locking. Once we support
2681 : : * parallelism it should still be fine, as presumably the holder of locks
2682 : : * would never be blocked by IO while holding the lock.
2683 : : */
515 andres@anarazel.de 2684 : 1034 : stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE |
2685 : : READ_STREAM_USE_BATCHING,
2686 : : vacrel->bstrategy,
2687 : : vacrel->rel,
2688 : : MAIN_FORKNUM,
2689 : : vacuum_reap_lp_read_stream_next,
2690 : : iter,
2691 : : sizeof(TidStoreIterResult));
2692 : :
2693 : : while (true)
9176 tgl@sss.pgh.pa.us 2694 : 23358 : {
2695 : : BlockNumber blkno;
2696 : : Buffer buf;
2697 : : Page page;
2698 : : TidStoreIterResult *iter_result;
2699 : : Size freespace;
2700 : : OffsetNumber offsets[MaxOffsetNumber];
2701 : : int num_offsets;
2702 : :
562 nathan@postgresql.or 2703 : 24392 : vacuum_delay_point(false);
2704 : :
559 melanieplageman@gmai 2705 : 24392 : buf = read_stream_next_buffer(stream, (void **) &iter_result);
2706 : :
2707 : : /* The relation is exhausted */
2708 [ + + ]: 24392 : if (!BufferIsValid(buf))
2709 : 1034 : break;
2710 : :
2711 : 23358 : vacrel->blkno = blkno = BufferGetBlockNumber(buf);
2712 : :
2713 [ - + ]: 23358 : Assert(iter_result);
764 tmunro@postgresql.or 2714 : 23358 : num_offsets = TidStoreGetBlockOffsets(iter_result, offsets, lengthof(offsets));
2715 [ - + ]: 23358 : Assert(num_offsets <= lengthof(offsets));
2716 : :
2717 : : /*
2718 : : * Pin the visibility map page in case we need to mark the page
2719 : : * all-visible. In most cases this will be very cheap, because we'll
2720 : : * already have the correct page pinned anyway.
2721 : : */
1319 pg@bowt.ie 2722 : 23358 : visibilitymap_pin(vacrel->rel, blkno, &vmbuffer);
2723 : :
2724 : : /* We need a non-cleanup exclusive lock to mark dead_items unused */
1969 2725 : 23358 : LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
764 tmunro@postgresql.or 2726 : 23358 : lazy_vacuum_heap_page(vacrel, blkno, buf, offsets,
2727 : : num_offsets, vmbuffer);
2728 : :
2729 : : /* Now that we've vacuumed the page, record its available space */
3781 kgrittn@postgresql.o 2730 : 23358 : page = BufferGetPage(buf);
6540 heikki.linnakangas@i 2731 : 23358 : freespace = PageGetHeapFreeSpace(page);
2732 : :
7454 tgl@sss.pgh.pa.us 2733 : 23358 : UnlockReleaseBuffer(buf);
1324 pg@bowt.ie 2734 : 23358 : RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1970 2735 : 23358 : vacuumed_pages++;
2736 : : }
2737 : :
559 melanieplageman@gmai 2738 : 1034 : read_stream_end(stream);
877 msawada@postgresql.o 2739 : 1034 : TidStoreEndIterate(iter);
2740 : :
1970 pg@bowt.ie 2741 : 1034 : vacrel->blkno = InvalidBlockNumber;
4943 heikki.linnakangas@i 2742 [ + - ]: 1034 : if (BufferIsValid(vmbuffer))
2743 : 1034 : ReleaseBuffer(vmbuffer);
2744 : :
2745 : : /*
2746 : : * We set all LP_DEAD items from the first heap pass to LP_UNUSED during
2747 : : * the second heap pass. No more, no less.
2748 : : */
1969 pg@bowt.ie 2749 [ + + + - : 1034 : Assert(vacrel->num_index_scans > 1 ||
- + ]
2750 : : (vacrel->dead_items_info->num_items == vacrel->lpdead_items &&
2751 : : vacuumed_pages == vacrel->lpdead_item_pages));
2752 : :
1686 2753 [ + + ]: 1034 : ereport(DEBUG2,
2754 : : (errmsg("table \"%s\": removed %" PRId64 " dead item identifiers in %u pages",
2755 : : vacrel->relname, vacrel->dead_items_info->num_items,
2756 : : vacuumed_pages)));
2757 : :
2758 : : /* Revert to the previous phase information for error traceback */
1970 2759 : 1034 : restore_vacuum_error_info(vacrel, &saved_err_info);
9176 tgl@sss.pgh.pa.us 2760 : 1034 : }
2761 : :
2762 : : /*
2763 : : * lazy_vacuum_heap_page() -- free page's LP_DEAD items listed in the
2764 : : * vacrel->dead_items store.
2765 : : *
2766 : : * Caller must have an exclusive buffer lock on the buffer (though a full
2767 : : * cleanup lock is also acceptable). vmbuffer must be valid and already have
2768 : : * a pin on blkno's visibility map page.
2769 : : */
2770 : : static void
1970 pg@bowt.ie 2771 : 23358 : lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer,
2772 : : OffsetNumber *deadoffsets, int num_offsets,
2773 : : Buffer vmbuffer)
2774 : : {
3781 kgrittn@postgresql.o 2775 : 23358 : Page page = BufferGetPage(buffer);
2776 : : OffsetNumber unused[MaxHeapTuplesPerPage];
1324 pg@bowt.ie 2777 : 23358 : int nunused = 0;
2778 : : TransactionId newest_live_xid;
318 melanieplageman@gmai 2779 : 23358 : TransactionId conflict_xid = InvalidTransactionId;
2780 : : bool all_frozen;
2781 : : LVSavedErrInfo saved_err_info;
2782 : 23358 : uint8 vmflags = 0;
2783 : :
952 rhaas@postgresql.org 2784 [ - + ]: 23358 : Assert(vacrel->do_index_vacuuming);
2785 : :
3817 2786 : 23358 : pgstat_progress_update_param(PROGRESS_VACUUM_HEAP_BLKS_VACUUMED, blkno);
2787 : :
2788 : : /* Update error traceback information */
1970 pg@bowt.ie 2789 : 23358 : update_vacuum_error_info(vacrel, &saved_err_info,
2790 : : VACUUM_ERRCB_PHASE_VACUUM_HEAP, blkno,
2791 : : InvalidOffsetNumber);
2792 : :
2793 : : /*
2794 : : * Before marking dead items unused, check whether the page will become
2795 : : * all-visible once that change is applied. This lets us reap the tuples
2796 : : * and mark the page all-visible within the same critical section,
2797 : : * enabling both changes to be emitted in a single WAL record. Since the
2798 : : * visibility checks may perform I/O and allocate memory, they must be
2799 : : * done outside the critical section.
2800 : : */
317 melanieplageman@gmai 2801 [ + + ]: 23358 : if (heap_page_would_be_all_visible(vacrel->rel, buffer,
2802 : : vacrel->vistest, true,
2803 : : deadoffsets, num_offsets,
2804 : : &all_frozen, &newest_live_xid,
2805 : : &vacrel->offnum))
2806 : : {
318 2807 : 22719 : vmflags |= VISIBILITYMAP_ALL_VISIBLE;
2808 [ + + ]: 22719 : if (all_frozen)
2809 : : {
2810 : 16373 : vmflags |= VISIBILITYMAP_ALL_FROZEN;
156 2811 [ - + ]: 16373 : Assert(!TransactionIdIsValid(newest_live_xid));
2812 : : }
2813 : :
2814 : : /*
2815 : : * Take the lock on the vmbuffer before entering a critical section.
2816 : : * The heap page lock must also be held while updating the VM to
2817 : : * ensure consistency.
2818 : : */
318 2819 : 22719 : LockBuffer(vmbuffer, BUFFER_LOCK_EXCLUSIVE);
2820 : : }
2821 : :
9176 tgl@sss.pgh.pa.us 2822 : 23358 : START_CRIT_SECTION();
2823 : :
877 msawada@postgresql.o 2824 [ + + ]: 1447837 : for (int i = 0; i < num_offsets; i++)
2825 : : {
2826 : : ItemId itemid;
2827 : 1424479 : OffsetNumber toff = deadoffsets[i];
2828 : :
9176 tgl@sss.pgh.pa.us 2829 : 1424479 : itemid = PageGetItemId(page, toff);
2830 : :
1969 pg@bowt.ie 2831 [ + - - + ]: 1424479 : Assert(ItemIdIsDead(itemid) && !ItemIdHasStorage(itemid));
6924 tgl@sss.pgh.pa.us 2832 : 1424479 : ItemIdSetUnused(itemid);
1324 pg@bowt.ie 2833 : 1424479 : unused[nunused++] = toff;
2834 : : }
2835 : :
2836 [ - + ]: 23358 : Assert(nunused > 0);
2837 : :
2838 : : /* Attempt to truncate line pointer array now */
1968 2839 : 23358 : PageTruncateLinePointerArray(page);
2840 : :
318 melanieplageman@gmai 2841 [ + + ]: 23358 : if ((vmflags & VISIBILITYMAP_VALID_BITS) != 0)
2842 : : {
2843 : : /*
2844 : : * The page is guaranteed to have had dead line pointers, so we always
2845 : : * set PD_ALL_VISIBLE.
2846 : : */
2847 : 22719 : PageSetAllVisible(page);
178 2848 : 22719 : PageClearPrunable(page);
156 2849 : 22719 : visibilitymap_set(blkno,
2850 : : vmbuffer, vmflags,
2851 : 22719 : vacrel->rel->rd_locator);
2852 : 22719 : conflict_xid = newest_live_xid;
2853 : : }
2854 : :
2855 : : /*
2856 : : * Mark buffer dirty before we write WAL.
2857 : : */
4867 simon@2ndQuadrant.co 2858 : 23358 : MarkBufferDirty(buffer);
2859 : :
2860 : : /* XLOG stuff */
1970 pg@bowt.ie 2861 [ + + + + : 23358 : if (RelationNeedsWAL(vacrel->rel))
+ - + - ]
2862 : : {
885 heikki.linnakangas@i 2863 [ + + ]: 22073 : log_heap_prune_and_freeze(vacrel->rel, buffer,
2864 : : vmflags != 0 ? vmbuffer : InvalidBuffer,
2865 : : vmflags,
2866 : : conflict_xid,
2867 : : false, /* no cleanup lock required */
2868 : : PRUNE_VACUUM_CLEANUP,
2869 : : NULL, 0, /* frozen */
2870 : : NULL, 0, /* redirected */
2871 : : NULL, 0, /* dead */
2872 : : unused, nunused);
2873 : : }
2874 : :
4451 andres@anarazel.de 2875 [ - + ]: 23358 : END_CRIT_SECTION();
2876 : :
318 melanieplageman@gmai 2877 [ + + ]: 23358 : if ((vmflags & VISIBILITYMAP_ALL_VISIBLE) != 0)
2878 : : {
2879 : : /* Count the newly set VM page for logging */
2880 : 22719 : LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
182 2881 : 22719 : vacrel->new_all_visible_pages++;
427 2882 [ + + ]: 22719 : if (all_frozen)
182 2883 : 16373 : vacrel->new_all_visible_all_frozen_pages++;
2884 : : }
2885 : :
2886 : : /* Revert to the previous phase information for error traceback */
1970 pg@bowt.ie 2887 : 23358 : restore_vacuum_error_info(vacrel, &saved_err_info);
9176 tgl@sss.pgh.pa.us 2888 : 23358 : }
2889 : :
2890 : : /*
2891 : : * Trigger the failsafe to avoid wraparound failure when vacrel table has a
2892 : : * relfrozenxid and/or relminmxid that is dangerously far in the past.
2893 : : * Triggering the failsafe makes the ongoing VACUUM bypass any further index
2894 : : * vacuuming and heap vacuuming. Truncating the heap is also bypassed.
2895 : : *
2896 : : * Any remaining work (work that VACUUM cannot just bypass) is typically sped
2897 : : * up when the failsafe triggers. VACUUM stops applying any cost-based delay
2898 : : * that it started out with.
2899 : : *
2900 : : * Returns true when failsafe has been triggered.
2901 : : */
2902 : : static bool
1968 pg@bowt.ie 2903 : 19419 : lazy_check_wraparound_failsafe(LVRelState *vacrel)
2904 : : {
2905 : : /* Don't warn more than once per VACUUM */
1238 dgustafsson@postgres 2906 [ - + ]: 19419 : if (VacuumFailsafeActive)
1968 pg@bowt.ie 2907 :UBC 0 : return true;
2908 : :
1344 pg@bowt.ie 2909 [ - + ]:CBC 19419 : if (unlikely(vacuum_xid_failsafe_check(&vacrel->cutoffs)))
2910 : : {
1143 msawada@postgresql.o 2911 :UBC 0 : const int progress_index[] = {
2912 : : PROGRESS_VACUUM_INDEXES_TOTAL,
2913 : : PROGRESS_VACUUM_INDEXES_PROCESSED,
2914 : : PROGRESS_VACUUM_MODE
2915 : : };
261 2916 : 0 : int64 progress_val[3] = {0, 0, PROGRESS_VACUUM_MODE_FAILSAFE};
2917 : :
1238 dgustafsson@postgres 2918 : 0 : VacuumFailsafeActive = true;
2919 : :
2920 : : /*
2921 : : * We abandon use of the strategy in failsafe mode to allow use of all
2922 : : * of shared buffers. vacrel->bstrategy is not the source of truth for
2923 : : * an ongoing heap scan, but clear it just for tidiness. Any ongoing
2924 : : * phase I heap scan has its own references to the strategy and clears
2925 : : * them separately (see lazy_scan_heap()). And none of the other
2926 : : * vacuum phases will read from vacrel->bstrategy once failsafe mode
2927 : : * is engaged. The phase I read stream clears the strategy references
2928 : : * held by the ReadBuffersOperations outside of this function because
2929 : : * lazy_check_wraparound_failsafe() may be called from any phase of
2930 : : * vacuum, including when the phase I stream is inactive.
2931 : : *
2932 : : * We assume the caller who allocated the memory for the
2933 : : * BufferAccessStrategy will free it.
2934 : : */
1242 drowley@postgresql.o 2935 : 0 : vacrel->bstrategy = NULL;
2936 : :
2937 : : /* Disable index vacuuming, index cleanup, and heap rel truncation */
1968 pg@bowt.ie 2938 : 0 : vacrel->do_index_vacuuming = false;
2939 : 0 : vacrel->do_index_cleanup = false;
1896 2940 : 0 : vacrel->do_rel_truncate = false;
2941 : :
2942 : : /* Reset the progress counters and set the failsafe mode */
261 msawada@postgresql.o 2943 : 0 : pgstat_progress_update_multi_param(3, progress_index, progress_val);
2944 : :
1968 pg@bowt.ie 2945 [ # # ]: 0 : ereport(WARNING,
2946 : : (errmsg("bypassing nonessential maintenance of table \"%s.%s.%s\" as a failsafe after %d index scans",
2947 : : vacrel->dbname, vacrel->relnamespace, vacrel->relname,
2948 : : vacrel->num_index_scans),
2949 : : errdetail("The table's relfrozenxid or relminmxid is too far in the past."),
2950 : : errhint("Consider increasing configuration parameter \"maintenance_work_mem\" or \"autovacuum_work_mem\".\n"
2951 : : "You might also need to consider other ways for VACUUM to keep up with the allocation of transaction IDs.")));
2952 : :
2953 : : /* Stop applying cost limits from this point on */
2954 : 0 : VacuumCostActive = false;
2955 : 0 : VacuumCostBalance = 0;
2956 : :
2957 : 0 : return true;
2958 : : }
2959 : :
1968 pg@bowt.ie 2960 :CBC 19419 : return false;
2961 : : }
2962 : :
2963 : : /*
2964 : : * lazy_cleanup_all_indexes() -- cleanup all indexes of relation.
2965 : : */
2966 : : static void
1970 2967 : 14784 : lazy_cleanup_all_indexes(LVRelState *vacrel)
2968 : : {
1629 2969 : 14784 : double reltuples = vacrel->new_rel_tuples;
2970 : 14784 : bool estimated_count = vacrel->scanned_pages < vacrel->rel_pages;
1143 msawada@postgresql.o 2971 : 14784 : const int progress_start_index[] = {
2972 : : PROGRESS_VACUUM_PHASE,
2973 : : PROGRESS_VACUUM_INDEXES_TOTAL
2974 : : };
2975 : 14784 : const int progress_end_index[] = {
2976 : : PROGRESS_VACUUM_INDEXES_TOTAL,
2977 : : PROGRESS_VACUUM_INDEXES_PROCESSED
2978 : : };
2979 : : int64 progress_start_val[2];
2980 : 14784 : int64 progress_end_val[2] = {0, 0};
2981 : :
1629 pg@bowt.ie 2982 [ - + ]: 14784 : Assert(vacrel->do_index_cleanup);
1970 2983 [ - + ]: 14784 : Assert(vacrel->nindexes > 0);
2984 : :
2985 : : /*
2986 : : * Report that we are now cleaning up indexes and the number of indexes to
2987 : : * cleanup.
2988 : : */
1143 msawada@postgresql.o 2989 : 14784 : progress_start_val[0] = PROGRESS_VACUUM_PHASE_INDEX_CLEANUP;
2990 : 14784 : progress_start_val[1] = vacrel->nindexes;
2991 : 14784 : pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val);
2992 : :
1970 pg@bowt.ie 2993 [ + + ]: 14784 : if (!ParallelVacuumIsActive(vacrel))
2994 : : {
2995 [ + + ]: 38057 : for (int idx = 0; idx < vacrel->nindexes; idx++)
2996 : : {
2997 : 23297 : Relation indrel = vacrel->indrels[idx];
2998 : 23297 : IndexBulkDeleteResult *istat = vacrel->indstats[idx];
2999 : :
3000 : 46594 : vacrel->indstats[idx] =
3001 : 23297 : lazy_cleanup_one_index(indrel, istat, reltuples,
3002 : : estimated_count, vacrel);
3003 : :
3004 : : /* Report the number of indexes cleaned up */
1143 msawada@postgresql.o 3005 : 23297 : pgstat_progress_update_param(PROGRESS_VACUUM_INDEXES_PROCESSED,
3006 : 23297 : idx + 1);
3007 : : }
3008 : : }
3009 : : else
3010 : : {
3011 : : /* Outsource everything to parallel variant */
1629 pg@bowt.ie 3012 : 24 : parallel_vacuum_cleanup_all_indexes(vacrel->pvs, reltuples,
3013 : : vacrel->num_index_scans,
3014 : : estimated_count,
3015 : : &(vacrel->worker_usage.cleanup));
3016 : : }
3017 : :
3018 : : /* Reset the progress counters */
1143 msawada@postgresql.o 3019 : 14784 : pgstat_progress_update_multi_param(2, progress_end_index, progress_end_val);
2411 akapila@postgresql.o 3020 : 14784 : }
3021 : :
3022 : : /*
3023 : : * lazy_vacuum_one_index() -- vacuum index relation.
3024 : : *
3025 : : * Delete all the index tuples containing a TID collected in
3026 : : * vacrel->dead_items. Also update running statistics. Exact
3027 : : * details depend on index AM's ambulkdelete routine.
3028 : : *
3029 : : * reltuples is the number of heap tuples to be passed to the
3030 : : * bulkdelete callback. It's always assumed to be estimated.
3031 : : * See indexam.sgml for more info.
3032 : : *
3033 : : * Returns bulk delete stats derived from input stats
3034 : : */
3035 : : static IndexBulkDeleteResult *
1970 pg@bowt.ie 3036 : 2046 : lazy_vacuum_one_index(Relation indrel, IndexBulkDeleteResult *istat,
3037 : : double reltuples, LVRelState *vacrel)
3038 : : {
3039 : : IndexVacuumInfo ivinfo;
3040 : : LVSavedErrInfo saved_err_info;
3041 : :
7422 tgl@sss.pgh.pa.us 3042 : 2046 : ivinfo.index = indrel;
1242 pg@bowt.ie 3043 : 2046 : ivinfo.heaprel = vacrel->rel;
6365 tgl@sss.pgh.pa.us 3044 : 2046 : ivinfo.analyze_only = false;
2703 alvherre@alvh.no-ip. 3045 : 2046 : ivinfo.report_progress = false;
6291 tgl@sss.pgh.pa.us 3046 : 2046 : ivinfo.estimated_count = true;
1686 pg@bowt.ie 3047 : 2046 : ivinfo.message_level = DEBUG2;
2411 akapila@postgresql.o 3048 : 2046 : ivinfo.num_heap_tuples = reltuples;
1970 pg@bowt.ie 3049 : 2046 : ivinfo.strategy = vacrel->bstrategy;
3050 : :
3051 : : /*
3052 : : * Update error traceback information.
3053 : : *
3054 : : * The index name is saved during this phase and restored immediately
3055 : : * after this phase. See vacuum_error_callback.
3056 : : */
3057 [ - + ]: 2046 : Assert(vacrel->indname == NULL);
3058 : 2046 : vacrel->indname = pstrdup(RelationGetRelationName(indrel));
3059 : 2046 : update_vacuum_error_info(vacrel, &saved_err_info,
3060 : : VACUUM_ERRCB_PHASE_VACUUM_INDEX,
3061 : : InvalidBlockNumber, InvalidOffsetNumber);
3062 : :
3063 : : /* Do bulk deletion */
637 peter@eisentraut.org 3064 : 2046 : istat = vac_bulkdel_one_index(&ivinfo, istat, vacrel->dead_items,
3065 : : vacrel->dead_items_info);
3066 : :
3067 : : /* Revert to the previous phase information for error traceback */
1970 pg@bowt.ie 3068 : 2045 : restore_vacuum_error_info(vacrel, &saved_err_info);
3069 : 2045 : pfree(vacrel->indname);
3070 : 2045 : vacrel->indname = NULL;
3071 : :
3072 : 2045 : return istat;
3073 : : }
3074 : :
3075 : : /*
3076 : : * lazy_cleanup_one_index() -- do post-vacuum cleanup for index relation.
3077 : : *
3078 : : * Calls index AM's amvacuumcleanup routine. reltuples is the number
3079 : : * of heap tuples and estimated_count is true if reltuples is an
3080 : : * estimated value. See indexam.sgml for more info.
3081 : : *
3082 : : * Returns bulk delete stats derived from input stats
3083 : : */
3084 : : static IndexBulkDeleteResult *
3085 : 23297 : lazy_cleanup_one_index(Relation indrel, IndexBulkDeleteResult *istat,
3086 : : double reltuples, bool estimated_count,
3087 : : LVRelState *vacrel)
3088 : : {
3089 : : IndexVacuumInfo ivinfo;
3090 : : LVSavedErrInfo saved_err_info;
3091 : :
7422 tgl@sss.pgh.pa.us 3092 : 23297 : ivinfo.index = indrel;
1242 pg@bowt.ie 3093 : 23297 : ivinfo.heaprel = vacrel->rel;
6365 tgl@sss.pgh.pa.us 3094 : 23297 : ivinfo.analyze_only = false;
2703 alvherre@alvh.no-ip. 3095 : 23297 : ivinfo.report_progress = false;
2411 akapila@postgresql.o 3096 : 23297 : ivinfo.estimated_count = estimated_count;
1686 pg@bowt.ie 3097 : 23297 : ivinfo.message_level = DEBUG2;
3098 : :
2411 akapila@postgresql.o 3099 : 23297 : ivinfo.num_heap_tuples = reltuples;
1970 pg@bowt.ie 3100 : 23297 : ivinfo.strategy = vacrel->bstrategy;
3101 : :
3102 : : /*
3103 : : * Update error traceback information.
3104 : : *
3105 : : * The index name is saved during this phase and restored immediately
3106 : : * after this phase. See vacuum_error_callback.
3107 : : */
3108 [ - + ]: 23297 : Assert(vacrel->indname == NULL);
3109 : 23297 : vacrel->indname = pstrdup(RelationGetRelationName(indrel));
3110 : 23297 : update_vacuum_error_info(vacrel, &saved_err_info,
3111 : : VACUUM_ERRCB_PHASE_INDEX_CLEANUP,
3112 : : InvalidBlockNumber, InvalidOffsetNumber);
3113 : :
1709 akapila@postgresql.o 3114 : 23297 : istat = vac_cleanup_one_index(&ivinfo, istat);
3115 : :
3116 : : /* Revert to the previous phase information for error traceback */
1970 pg@bowt.ie 3117 : 23297 : restore_vacuum_error_info(vacrel, &saved_err_info);
3118 : 23297 : pfree(vacrel->indname);
3119 : 23297 : vacrel->indname = NULL;
3120 : :
3121 : 23297 : return istat;
3122 : : }
3123 : :
3124 : : /*
3125 : : * should_attempt_truncation - should we attempt to truncate the heap?
3126 : : *
3127 : : * Don't even think about it unless we have a shot at releasing a goodly
3128 : : * number of pages. Otherwise, the time taken isn't worth it, mainly because
3129 : : * an AccessExclusive lock must be replayed on any hot standby, where it can
3130 : : * be particularly disruptive.
3131 : : *
3132 : : * Also don't attempt it if wraparound failsafe is in effect. The entire
3133 : : * system might be refusing to allocate new XIDs at this point. The system
3134 : : * definitely won't return to normal unless and until VACUUM actually advances
3135 : : * the oldest relfrozenxid -- which hasn't happened for target rel just yet.
3136 : : * If lazy_truncate_heap attempted to acquire an AccessExclusiveLock to
3137 : : * truncate the table under these circumstances, an XID exhaustion error might
3138 : : * make it impossible for VACUUM to fix the underlying XID exhaustion problem.
3139 : : * There is very little chance of truncation working out when the failsafe is
3140 : : * in effect in any case. lazy_scan_prune makes the optimistic assumption
3141 : : * that any LP_DEAD items it encounters will always be LP_UNUSED by the time
3142 : : * we're called.
3143 : : */
3144 : : static bool
1896 3145 : 16323 : should_attempt_truncation(LVRelState *vacrel)
3146 : : {
3147 : : BlockNumber possibly_freeable;
3148 : :
1087 tmunro@postgresql.or 3149 [ + + - + ]: 16323 : if (!vacrel->do_rel_truncate || VacuumFailsafeActive)
1962 pg@bowt.ie 3150 : 160 : return false;
3151 : :
1970 3152 : 16163 : possibly_freeable = vacrel->rel_pages - vacrel->nonempty_pages;
3893 tgl@sss.pgh.pa.us 3153 [ + + + + ]: 16163 : if (possibly_freeable > 0 &&
3154 : 275 : (possibly_freeable >= REL_TRUNCATE_MINIMUM ||
1629 pg@bowt.ie 3155 [ + + ]: 275 : possibly_freeable >= vacrel->rel_pages / REL_TRUNCATE_FRACTION))
3893 tgl@sss.pgh.pa.us 3156 : 250 : return true;
3157 : :
1629 pg@bowt.ie 3158 : 15913 : return false;
3159 : : }
3160 : :
3161 : : /*
3162 : : * lazy_truncate_heap - try to truncate off any empty pages at the end
3163 : : */
3164 : : static void
1970 3165 : 250 : lazy_truncate_heap(LVRelState *vacrel)
3166 : : {
1822 3167 : 250 : BlockNumber orig_rel_pages = vacrel->rel_pages;
3168 : : BlockNumber new_rel_pages;
3169 : : bool lock_waiter_detected;
3170 : : int lock_retry;
3171 : :
3172 : : /* Report that we are now truncating */
3817 rhaas@postgresql.org 3173 : 250 : pgstat_progress_update_param(PROGRESS_VACUUM_PHASE,
3174 : : PROGRESS_VACUUM_PHASE_TRUNCATE);
3175 : :
3176 : : /* Update error traceback information one last time */
1629 pg@bowt.ie 3177 : 250 : update_vacuum_error_info(vacrel, NULL, VACUUM_ERRCB_PHASE_TRUNCATE,
3178 : : vacrel->nonempty_pages, InvalidOffsetNumber);
3179 : :
3180 : : /*
3181 : : * Loop until no more truncating can be done.
3182 : : */
3183 : : do
3184 : : {
3185 : : /*
3186 : : * We need full exclusive lock on the relation in order to do
3187 : : * truncation. If we can't get it, give up rather than waiting --- we
3188 : : * don't want to block other backends, and we don't want to deadlock
3189 : : * (which is quite possible considering we already hold a lower-grade
3190 : : * lock).
3191 : : */
1899 3192 : 250 : lock_waiter_detected = false;
5007 kgrittn@postgresql.o 3193 : 250 : lock_retry = 0;
3194 : : while (true)
3195 : : {
1970 pg@bowt.ie 3196 [ + + ]: 457 : if (ConditionalLockRelation(vacrel->rel, AccessExclusiveLock))
5007 kgrittn@postgresql.o 3197 : 248 : break;
3198 : :
3199 : : /*
3200 : : * Check for interrupts while trying to (re-)acquire the exclusive
3201 : : * lock.
3202 : : */
3203 [ - + ]: 209 : CHECK_FOR_INTERRUPTS();
3204 : :
4868 3205 [ + + ]: 209 : if (++lock_retry > (VACUUM_TRUNCATE_LOCK_TIMEOUT /
3206 : : VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL))
3207 : : {
3208 : : /*
3209 : : * We failed to establish the lock in the specified number of
3210 : : * retries. This means we give up truncating.
3211 : : */
1686 pg@bowt.ie 3212 [ + - + - ]: 2 : ereport(vacrel->verbose ? INFO : DEBUG2,
3213 : : (errmsg("\"%s\": stopping truncate due to conflicting lock request",
3214 : : vacrel->relname)));
5007 kgrittn@postgresql.o 3215 : 5 : return;
3216 : : }
3217 : :
1882 michael@paquier.xyz 3218 : 207 : (void) WaitLatch(MyLatch,
3219 : : WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
3220 : : VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL,
3221 : : WAIT_EVENT_VACUUM_TRUNCATE);
3222 : 207 : ResetLatch(MyLatch);
3223 : : }
3224 : :
3225 : : /*
3226 : : * Now that we have exclusive lock, look to see if the rel has grown
3227 : : * whilst we were vacuuming with non-exclusive lock. If so, give up;
3228 : : * the newly added pages presumably contain non-deletable tuples.
3229 : : */
1970 pg@bowt.ie 3230 : 248 : new_rel_pages = RelationGetNumberOfBlocks(vacrel->rel);
1822 3231 [ - + ]: 248 : if (new_rel_pages != orig_rel_pages)
3232 : : {
3233 : : /*
3234 : : * Note: we intentionally don't update vacrel->rel_pages with the
3235 : : * new rel size here. If we did, it would amount to assuming that
3236 : : * the new pages are empty, which is unlikely. Leaving the numbers
3237 : : * alone amounts to assuming that the new pages have the same
3238 : : * tuple density as existing ones, which is less unlikely.
3239 : : */
1970 pg@bowt.ie 3240 :UBC 0 : UnlockRelation(vacrel->rel, AccessExclusiveLock);
5007 kgrittn@postgresql.o 3241 : 0 : return;
3242 : : }
3243 : :
3244 : : /*
3245 : : * Scan backwards from the end to verify that the end pages actually
3246 : : * contain no tuples. This is *necessary*, not optional, because
3247 : : * other backends could have added tuples to these pages whilst we
3248 : : * were vacuuming.
3249 : : */
1899 pg@bowt.ie 3250 :CBC 248 : new_rel_pages = count_nondeletable_pages(vacrel, &lock_waiter_detected);
1970 3251 : 248 : vacrel->blkno = new_rel_pages;
3252 : :
1822 3253 [ + + ]: 248 : if (new_rel_pages >= orig_rel_pages)
3254 : : {
3255 : : /* can't do anything after all */
1970 3256 : 3 : UnlockRelation(vacrel->rel, AccessExclusiveLock);
5007 kgrittn@postgresql.o 3257 : 3 : return;
3258 : : }
3259 : :
3260 : : /*
3261 : : * Okay to truncate.
3262 : : */
1970 pg@bowt.ie 3263 : 245 : RelationTruncate(vacrel->rel, new_rel_pages);
3264 : :
3265 : : /*
3266 : : * We can release the exclusive lock as soon as we have truncated.
3267 : : * Other backends can't safely access the relation until they have
3268 : : * processed the smgr invalidation that smgrtruncate sent out ... but
3269 : : * that should happen as part of standard invalidation processing once
3270 : : * they acquire lock on the relation.
3271 : : */
3272 : 245 : UnlockRelation(vacrel->rel, AccessExclusiveLock);
3273 : :
3274 : : /*
3275 : : * Update statistics. Here, it *is* correct to adjust rel_pages
3276 : : * without also touching reltuples, since the tuple count wasn't
3277 : : * changed by the truncation.
3278 : : */
1672 3279 : 245 : vacrel->removed_pages += orig_rel_pages - new_rel_pages;
1970 3280 : 245 : vacrel->rel_pages = new_rel_pages;
3281 : :
1686 3282 [ + + + + ]: 245 : ereport(vacrel->verbose ? INFO : DEBUG2,
3283 : : (errmsg("table \"%s\": truncated %u to %u pages",
3284 : : vacrel->relname,
3285 : : orig_rel_pages, new_rel_pages)));
1822 3286 : 245 : orig_rel_pages = new_rel_pages;
1899 3287 [ + + - + ]: 245 : } while (new_rel_pages > vacrel->nonempty_pages && lock_waiter_detected);
3288 : : }
3289 : :
3290 : : /*
3291 : : * Rescan end pages to verify that they are (still) empty of tuples.
3292 : : *
3293 : : * Returns number of nondeletable pages (last nonempty page + 1).
3294 : : */
3295 : : static BlockNumber
3296 : 248 : count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected)
3297 : : {
3298 : : StaticAssertDecl((PREFETCH_SIZE & (PREFETCH_SIZE - 1)) == 0,
3299 : : "prefetch size must be power of 2");
3300 : :
3301 : : BlockNumber blkno;
3302 : : BlockNumber prefetchedUntil;
3303 : : instr_time starttime;
3304 : :
3305 : : /* Initialize the starttime if we check for conflicting lock requests */
5007 kgrittn@postgresql.o 3306 : 248 : INSTR_TIME_SET_CURRENT(starttime);
3307 : :
3308 : : /*
3309 : : * Start checking blocks at what we believe relation end to be and move
3310 : : * backwards. (Strange coding of loop control is needed because blkno is
3311 : : * unsigned.) To make the scan faster, we prefetch a few blocks at a time
3312 : : * in forward direction, so that OS-level readahead can kick in.
3313 : : */
1970 pg@bowt.ie 3314 : 248 : blkno = vacrel->rel_pages;
3503 alvherre@alvh.no-ip. 3315 : 248 : prefetchedUntil = InvalidBlockNumber;
1970 pg@bowt.ie 3316 [ + + ]: 4699 : while (blkno > vacrel->nonempty_pages)
3317 : : {
3318 : : Buffer buf;
3319 : : Page page;
3320 : : OffsetNumber offnum,
3321 : : maxoff;
3322 : : bool hastup;
3323 : :
3324 : : /*
3325 : : * Check if another process requests a lock on our relation. We are
3326 : : * holding an AccessExclusiveLock here, so they will be waiting. We
3327 : : * only do this once per VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL, and we
3328 : : * only check if that interval has elapsed once every 32 blocks to
3329 : : * keep the number of system calls and actual shared lock table
3330 : : * lookups to a minimum.
3331 : : */
5007 kgrittn@postgresql.o 3332 [ + + ]: 4465 : if ((blkno % 32) == 0)
3333 : : {
3334 : : instr_time currenttime;
3335 : : instr_time elapsed;
3336 : :
3337 : 157 : INSTR_TIME_SET_CURRENT(currenttime);
3338 : 157 : elapsed = currenttime;
3339 : 157 : INSTR_TIME_SUBTRACT(elapsed, starttime);
3340 [ - + ]: 157 : if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000)
3341 : : >= VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL)
3342 : : {
1970 pg@bowt.ie 3343 [ # # ]:UBC 0 : if (LockHasWaitersRelation(vacrel->rel, AccessExclusiveLock))
3344 : : {
1686 3345 [ # # # # ]: 0 : ereport(vacrel->verbose ? INFO : DEBUG2,
3346 : : (errmsg("table \"%s\": suspending truncate due to conflicting lock request",
3347 : : vacrel->relname)));
3348 : :
1899 3349 : 0 : *lock_waiter_detected = true;
5007 kgrittn@postgresql.o 3350 : 0 : return blkno;
3351 : : }
3352 : 0 : starttime = currenttime;
3353 : : }
3354 : : }
3355 : :
3356 : : /*
3357 : : * We don't insert a vacuum delay point here, because we have an
3358 : : * exclusive lock on the table which we want to hold for as short a
3359 : : * time as possible. We still need to check for interrupts however.
3360 : : */
6924 alvherre@alvh.no-ip. 3361 [ - + ]:CBC 4465 : CHECK_FOR_INTERRUPTS();
3362 : :
9176 tgl@sss.pgh.pa.us 3363 : 4465 : blkno--;
3364 : :
3365 : : /* If we haven't prefetched this lot yet, do so now. */
3503 alvherre@alvh.no-ip. 3366 [ + + ]: 4465 : if (prefetchedUntil > blkno)
3367 : : {
3368 : : BlockNumber prefetchStart;
3369 : : BlockNumber pblkno;
3370 : :
3371 : 361 : prefetchStart = blkno & ~(PREFETCH_SIZE - 1);
3372 [ + + ]: 6834 : for (pblkno = prefetchStart; pblkno <= blkno; pblkno++)
3373 : : {
1970 pg@bowt.ie 3374 : 6473 : PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, pblkno);
3503 alvherre@alvh.no-ip. 3375 [ - + ]: 6473 : CHECK_FOR_INTERRUPTS();
3376 : : }
3377 : 361 : prefetchedUntil = prefetchStart;
3378 : : }
3379 : :
1970 pg@bowt.ie 3380 : 4465 : buf = ReadBufferExtended(vacrel->rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
3381 : : vacrel->bstrategy);
3382 : :
3383 : : /* In this phase we only need shared access to the buffer */
9176 tgl@sss.pgh.pa.us 3384 : 4465 : LockBuffer(buf, BUFFER_LOCK_SHARE);
3385 : :
3781 kgrittn@postgresql.o 3386 : 4465 : page = BufferGetPage(buf);
3387 : :
9176 tgl@sss.pgh.pa.us 3388 [ + + + + ]: 4465 : if (PageIsNew(page) || PageIsEmpty(page))
3389 : : {
7454 3390 : 2175 : UnlockReleaseBuffer(buf);
9176 3391 : 2175 : continue;
3392 : : }
3393 : :
3394 : 2290 : hastup = false;
3395 : 2290 : maxoff = PageGetMaxOffsetNumber(page);
3396 : 2290 : for (offnum = FirstOffsetNumber;
3397 [ + + ]: 5669 : offnum <= maxoff;
3398 : 3379 : offnum = OffsetNumberNext(offnum))
3399 : : {
3400 : : ItemId itemid;
3401 : :
3402 : 3393 : itemid = PageGetItemId(page, offnum);
3403 : :
3404 : : /*
3405 : : * Note: any non-unused item should be taken as a reason to keep
3406 : : * this page. Even an LP_DEAD item makes truncation unsafe, since
3407 : : * we must not have cleaned out its index entries.
3408 : : */
6920 3409 [ + + ]: 3393 : if (ItemIdIsUsed(itemid))
3410 : : {
9176 3411 : 14 : hastup = true;
3412 : 14 : break; /* can stop scanning */
3413 : : }
3414 : : } /* scan along page */
3415 : :
7454 3416 : 2290 : UnlockReleaseBuffer(buf);
3417 : :
3418 : : /* Done scanning if we found a tuple here */
9176 3419 [ + + ]: 2290 : if (hastup)
3420 : 14 : return blkno + 1;
3421 : : }
3422 : :
3423 : : /*
3424 : : * If we fall out of the loop, all the previously-thought-to-be-empty
3425 : : * pages still are; we need not bother to look at the last known-nonempty
3426 : : * page.
3427 : : */
1970 pg@bowt.ie 3428 : 234 : return vacrel->nonempty_pages;
3429 : : }
3430 : :
3431 : : /*
3432 : : * Allocate dead_items and dead_items_info (either using palloc, or in dynamic
3433 : : * shared memory). Sets both in vacrel for caller.
3434 : : *
3435 : : * Also handles parallel initialization as part of allocating dead_items in
3436 : : * DSM when required.
3437 : : */
3438 : : static void
1732 3439 : 16325 : dead_items_alloc(LVRelState *vacrel, int nworkers)
3440 : : {
3441 : : VacDeadItemsInfo *dead_items_info;
877 msawada@postgresql.o 3442 : 33124 : int vac_work_mem = AmAutoVacuumWorkerProcess() &&
3443 [ - + ]: 474 : autovacuum_work_mem != -1 ?
3444 [ + + ]: 16799 : autovacuum_work_mem : maintenance_work_mem;
3445 : :
3446 : : /*
3447 : : * Initialize state for a parallel vacuum. As of now, only one worker can
3448 : : * be used for an index, so we invoke parallelism only if there are at
3449 : : * least two indexes on a table.
3450 : : */
1968 pg@bowt.ie 3451 [ + + + + : 16325 : if (nworkers >= 0 && vacrel->nindexes > 1 && vacrel->do_index_vacuuming)
+ + ]
3452 : : {
3453 : : /*
3454 : : * Since parallel workers cannot access data in temporary tables, we
3455 : : * can't perform parallel vacuum on them.
3456 : : */
1970 3457 [ + + ]: 6759 : if (RelationUsesLocalBuffers(vacrel->rel))
3458 : : {
3459 : : /*
3460 : : * Give warning only if the user explicitly tries to perform a
3461 : : * parallel vacuum on the temporary table.
3462 : : */
3463 [ + - ]: 4 : if (nworkers > 0)
3464 [ + - ]: 4 : ereport(WARNING,
3465 : : (errmsg("disabling parallel option of vacuum on \"%s\" --- cannot vacuum temporary tables in parallel",
3466 : : vacrel->relname)));
3467 : : }
3468 : : else
1708 akapila@postgresql.o 3469 : 6755 : vacrel->pvs = parallel_vacuum_init(vacrel->rel, vacrel->indrels,
3470 : : vacrel->nindexes, nworkers,
3471 : : vac_work_mem,
1686 pg@bowt.ie 3472 [ + + ]: 6755 : vacrel->verbose ? INFO : DEBUG2,
3473 : : vacrel->bstrategy);
3474 : :
3475 : : /*
3476 : : * If parallel mode started, dead_items and dead_items_info spaces are
3477 : : * allocated in DSM.
3478 : : */
1970 3479 [ + + ]: 6759 : if (ParallelVacuumIsActive(vacrel))
3480 : : {
877 msawada@postgresql.o 3481 : 25 : vacrel->dead_items = parallel_vacuum_get_dead_items(vacrel->pvs,
3482 : : &vacrel->dead_items_info);
1970 pg@bowt.ie 3483 : 25 : return;
3484 : : }
3485 : : }
3486 : :
3487 : : /*
3488 : : * Serial VACUUM case. Allocate both dead_items and dead_items_info
3489 : : * locally.
3490 : : */
3491 : :
260 michael@paquier.xyz 3492 : 16300 : dead_items_info = palloc_object(VacDeadItemsInfo);
573 tgl@sss.pgh.pa.us 3493 : 16300 : dead_items_info->max_bytes = vac_work_mem * (Size) 1024;
877 msawada@postgresql.o 3494 : 16300 : dead_items_info->num_items = 0;
3495 : 16300 : vacrel->dead_items_info = dead_items_info;
3496 : :
872 john.naylor@postgres 3497 : 16300 : vacrel->dead_items = TidStoreCreateLocal(dead_items_info->max_bytes, true);
3498 : : }
3499 : :
3500 : : /*
3501 : : * Add the given block number and offset numbers to dead_items.
3502 : : */
3503 : : static void
877 msawada@postgresql.o 3504 : 23581 : dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets,
3505 : : int num_offsets)
3506 : : {
804 3507 : 23581 : const int prog_index[2] = {
3508 : : PROGRESS_VACUUM_NUM_DEAD_ITEM_IDS,
3509 : : PROGRESS_VACUUM_DEAD_TUPLE_BYTES
3510 : : };
3511 : : int64 prog_val[2];
3512 : :
631 john.naylor@postgres 3513 : 23581 : TidStoreSetBlockOffsets(vacrel->dead_items, blkno, offsets, num_offsets);
877 msawada@postgresql.o 3514 : 23581 : vacrel->dead_items_info->num_items += num_offsets;
3515 : :
3516 : : /* update the progress information */
804 3517 : 23581 : prog_val[0] = vacrel->dead_items_info->num_items;
631 john.naylor@postgres 3518 : 23581 : prog_val[1] = TidStoreMemoryUsage(vacrel->dead_items);
804 msawada@postgresql.o 3519 : 23581 : pgstat_progress_update_multi_param(2, prog_index, prog_val);
877 3520 : 23581 : }
3521 : :
3522 : : /*
3523 : : * Forget all collected dead items.
3524 : : */
3525 : : static void
3526 : 1048 : dead_items_reset(LVRelState *vacrel)
3527 : : {
3528 : : /* Update statistics for dead items */
240 3529 : 1048 : vacrel->num_dead_items_resets++;
3530 : 1048 : vacrel->total_dead_items_bytes += TidStoreMemoryUsage(vacrel->dead_items);
3531 : :
877 3532 [ + + ]: 1048 : if (ParallelVacuumIsActive(vacrel))
3533 : : {
3534 : 13 : parallel_vacuum_reset_dead_items(vacrel->pvs);
328 john.naylor@postgres 3535 : 13 : vacrel->dead_items = parallel_vacuum_get_dead_items(vacrel->pvs,
3536 : : &vacrel->dead_items_info);
877 msawada@postgresql.o 3537 : 13 : return;
3538 : : }
3539 : :
3540 : : /* Recreate the tidstore with the same max_bytes limitation */
631 john.naylor@postgres 3541 : 1035 : TidStoreDestroy(vacrel->dead_items);
872 3542 : 1035 : vacrel->dead_items = TidStoreCreateLocal(vacrel->dead_items_info->max_bytes, true);
3543 : :
3544 : : /* Reset the counter */
877 msawada@postgresql.o 3545 : 1035 : vacrel->dead_items_info->num_items = 0;
3546 : : }
3547 : :
3548 : : /*
3549 : : * Perform cleanup for resources allocated in dead_items_alloc
3550 : : */
3551 : : static void
1732 pg@bowt.ie 3552 : 16323 : dead_items_cleanup(LVRelState *vacrel)
3553 : : {
1970 3554 [ + + ]: 16323 : if (!ParallelVacuumIsActive(vacrel))
3555 : : {
3556 : : /* Don't bother with pfree here */
3557 : 16299 : return;
3558 : : }
3559 : :
3560 : : /* End parallel mode */
1708 akapila@postgresql.o 3561 : 24 : parallel_vacuum_end(vacrel->pvs, vacrel->indstats);
3562 : 24 : vacrel->pvs = NULL;
3563 : : }
3564 : :
3565 : : #ifdef USE_ASSERT_CHECKING
3566 : :
3567 : : /*
3568 : : * Wrapper for heap_page_would_be_all_visible() which can be used for callers
3569 : : * that expect no LP_DEAD on the page. Currently assert-only, but there is no
3570 : : * reason not to use it outside of asserts.
3571 : : */
3572 : : bool
317 melanieplageman@gmai 3573 : 63710 : heap_page_is_all_visible(Relation rel, Buffer buf,
3574 : : GlobalVisState *vistest,
3575 : : bool *all_frozen,
3576 : : TransactionId *newest_live_xid,
3577 : : OffsetNumber *logging_offnum)
3578 : : {
3579 : : /*
3580 : : * Pass allow_update_vistest as false so that the GlobalVisState
3581 : : * boundaries used here match those used by the pruning code we are
3582 : : * cross-checking. Allowing an update could move the boundaries between
3583 : : * the two calls, causing a spurious assertion failure.
3584 : : */
3585 : 63710 : return heap_page_would_be_all_visible(rel, buf,
3586 : : vistest, false,
3587 : : NULL, 0,
3588 : : all_frozen,
3589 : : newest_live_xid,
3590 : : logging_offnum);
3591 : : }
3592 : : #endif
3593 : :
3594 : : /*
3595 : : * Check whether the heap page in buf is all-visible except for the dead
3596 : : * tuples referenced in the deadoffsets array.
3597 : : *
3598 : : * Vacuum uses this to check if a page would become all-visible after reaping
3599 : : * known dead tuples. This function does not remove the dead items.
3600 : : *
3601 : : * This cannot be called in a critical section, as the visibility checks may
3602 : : * perform IO and allocate memory.
3603 : : *
3604 : : * Returns true if the page is all-visible other than the provided
3605 : : * deadoffsets and false otherwise.
3606 : : *
3607 : : * vistest is used to determine visibility. If allow_update_vistest is true,
3608 : : * the boundaries of the GlobalVisState may be updated when checking the
3609 : : * visibility of the newest live XID on the page.
3610 : : *
3611 : : * Output parameters:
3612 : : *
3613 : : * - *all_frozen: true if every tuple on the page is frozen
3614 : : * - *newest_live_xid: newest xmin of live tuples on the page
3615 : : * - *logging_offnum: OffsetNumber of current tuple being processed;
3616 : : * used by vacuum's error callback system.
3617 : : *
3618 : : * Callers looking to verify that the page is already all-visible can call
3619 : : * heap_page_is_all_visible().
3620 : : *
3621 : : * This logic is closely related to heap_prune_record_unchanged_lp_normal().
3622 : : * If you modify this function, ensure consistency with that code. An
3623 : : * assertion cross-checks that both remain in agreement. Do not introduce new
3624 : : * side-effects.
3625 : : */
3626 : : static bool
3627 : 87068 : heap_page_would_be_all_visible(Relation rel, Buffer buf,
3628 : : GlobalVisState *vistest,
3629 : : bool allow_update_vistest,
3630 : : OffsetNumber *deadoffsets,
3631 : : int ndeadoffsets,
3632 : : bool *all_frozen,
3633 : : TransactionId *newest_live_xid,
3634 : : OffsetNumber *logging_offnum)
3635 : : {
3781 kgrittn@postgresql.o 3636 : 87068 : Page page = BufferGetPage(buf);
4114 bruce@momjian.us 3637 : 87068 : BlockNumber blockno = BufferGetBlockNumber(buf);
3638 : : OffsetNumber offnum,
3639 : : maxoff;
4838 3640 : 87068 : bool all_visible = true;
318 melanieplageman@gmai 3641 : 87068 : int matched_dead_count = 0;
3642 : :
156 3643 : 87068 : *newest_live_xid = InvalidTransactionId;
3831 rhaas@postgresql.org 3644 : 87068 : *all_frozen = true;
3645 : :
318 melanieplageman@gmai 3646 [ + + - + ]: 87068 : Assert(ndeadoffsets == 0 || deadoffsets);
3647 : :
3648 : : #ifdef USE_ASSERT_CHECKING
3649 : : /* Confirm input deadoffsets[] is strictly sorted */
3650 [ + + ]: 87068 : if (ndeadoffsets > 1)
3651 : : {
3652 [ + + ]: 1423187 : for (int i = 1; i < ndeadoffsets; i++)
3653 [ - + ]: 1401121 : Assert(deadoffsets[i - 1] < deadoffsets[i]);
3654 : : }
3655 : : #endif
3656 : :
4943 heikki.linnakangas@i 3657 : 87068 : maxoff = PageGetMaxOffsetNumber(page);
3658 : 87068 : for (offnum = FirstOffsetNumber;
4838 bruce@momjian.us 3659 [ + + + + ]: 7771570 : offnum <= maxoff && all_visible;
3660 : 7684502 : offnum = OffsetNumberNext(offnum))
3661 : : {
3662 : : ItemId itemid;
3663 : : HeapTupleData tuple;
3664 : : TransactionId dead_after;
3665 : :
3666 : : /*
3667 : : * Set the offset number so that we can display it along with any
3668 : : * error that occurred while processing this tuple.
3669 : : */
317 melanieplageman@gmai 3670 : 7684505 : *logging_offnum = offnum;
4943 heikki.linnakangas@i 3671 : 7684505 : itemid = PageGetItemId(page, offnum);
3672 : :
3673 : : /* Unused or redirect line pointers are of no interest */
3674 [ + + + + ]: 7684505 : if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid))
3675 : 1593713 : continue;
3676 : :
4304 andres@anarazel.de 3677 : 7510831 : ItemPointerSet(&(tuple.t_self), blockno, offnum);
3678 : :
3679 : : /*
3680 : : * Dead line pointers can have index pointers pointing to them. So
3681 : : * they can't be treated as visible
3682 : : */
4943 heikki.linnakangas@i 3683 [ + + ]: 7510831 : if (ItemIdIsDead(itemid))
3684 : : {
318 melanieplageman@gmai 3685 [ + - + + ]: 1420042 : if (!deadoffsets ||
3686 : 1420040 : matched_dead_count >= ndeadoffsets ||
3687 [ + + ]: 1420040 : deadoffsets[matched_dead_count] != offnum)
3688 : : {
3689 : 3 : *all_frozen = all_visible = false;
3690 : 3 : break;
3691 : : }
3692 : 1420039 : matched_dead_count++;
3693 : 1420039 : continue;
3694 : : }
3695 : :
4943 heikki.linnakangas@i 3696 [ - + ]: 6090789 : Assert(ItemIdIsNormal(itemid));
3697 : :
3698 : 6090789 : tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
4784 rhaas@postgresql.org 3699 : 6090789 : tuple.t_len = ItemIdGetLength(itemid);
317 melanieplageman@gmai 3700 : 6090789 : tuple.t_tableOid = RelationGetRelid(rel);
3701 : :
3702 : : /* Visibility checks may do IO or allocate memory */
318 3703 [ - + ]: 6090789 : Assert(CritSectionCount == 0);
182 3704 [ + + - ]: 6090789 : switch (HeapTupleSatisfiesVacuumHorizon(&tuple, buf, &dead_after))
3705 : : {
4943 heikki.linnakangas@i 3706 : 6090220 : case HEAPTUPLE_LIVE:
3707 : : {
3708 : : TransactionId xmin;
3709 : :
3710 : : /* Check heap_prune_record_unchanged_lp_normal comments */
4631 rhaas@postgresql.org 3711 [ - + ]: 6090220 : if (!HeapTupleHeaderXminCommitted(tuple.t_data))
3712 : : {
4943 heikki.linnakangas@i 3713 :UBC 0 : all_visible = false;
3737 rhaas@postgresql.org 3714 : 0 : *all_frozen = false;
4943 heikki.linnakangas@i 3715 : 0 : break;
3716 : : }
3717 : :
3718 : : /*
3719 : : * The inserter definitely committed. But we don't know if
3720 : : * it is old enough that everyone sees it as committed.
3721 : : * Don't check that now.
3722 : : *
3723 : : * If we scan all tuples without finding one that prevents
3724 : : * the page from being all-visible, we then check whether
3725 : : * any snapshot still considers the newest XID on the page
3726 : : * to be running. In that case, the page is not considered
3727 : : * all-visible.
3728 : : */
4943 heikki.linnakangas@i 3729 :CBC 6090220 : xmin = HeapTupleHeaderGetXmin(tuple.t_data);
3730 : :
3731 : : /* Track newest xmin on page. */
156 melanieplageman@gmai 3732 [ + + + + ]: 6090220 : if (TransactionIdFollows(xmin, *newest_live_xid) &&
3733 : : TransactionIdIsNormal(xmin))
3734 : 89031 : *newest_live_xid = xmin;
3735 : :
3736 : : /* Check whether this tuple is already frozen or not */
3831 rhaas@postgresql.org 3737 [ + - + + : 8259497 : if (all_visible && *all_frozen &&
+ + ]
3738 : 2169277 : heap_tuple_needs_eventual_freeze(tuple.t_data))
3739 : 36788 : *all_frozen = false;
3740 : : }
4943 heikki.linnakangas@i 3741 : 6090220 : break;
3742 : :
3743 : 569 : case HEAPTUPLE_DEAD:
3744 : : case HEAPTUPLE_RECENTLY_DEAD:
3745 : : case HEAPTUPLE_INSERT_IN_PROGRESS:
3746 : : case HEAPTUPLE_DELETE_IN_PROGRESS:
3747 : : {
3737 rhaas@postgresql.org 3748 : 569 : all_visible = false;
3749 : 569 : *all_frozen = false;
3750 : 569 : break;
3751 : : }
4943 heikki.linnakangas@i 3752 :UBC 0 : default:
3753 [ # # ]: 0 : elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
3754 : : break;
3755 : : }
3756 : : } /* scan along page */
3757 : :
3758 : : /*
3759 : : * After processing all the live tuples on the page, if the newest xmin
3760 : : * among them may still be considered running by any snapshot, the page
3761 : : * cannot be all-visible.
3762 : : */
156 melanieplageman@gmai 3763 [ + + ]:CBC 87068 : if (all_visible &&
3764 [ + + + + ]: 122989 : TransactionIdIsNormal(*newest_live_xid) &&
3765 : 36493 : GlobalVisTestXidConsideredRunning(vistest, *newest_live_xid,
3766 : : allow_update_vistest))
3767 : : {
3768 : 67 : all_visible = false;
3769 : 67 : *all_frozen = false;
3770 : : }
3771 : :
3772 : : /* Clear the offset information once we have processed the given page. */
317 3773 : 87068 : *logging_offnum = InvalidOffsetNumber;
3774 : :
4943 heikki.linnakangas@i 3775 : 87068 : return all_visible;
3776 : : }
3777 : :
3778 : : /*
3779 : : * Update index statistics in pg_class if the statistics are accurate.
3780 : : */
3781 : : static void
1629 pg@bowt.ie 3782 : 16173 : update_relstats_all_indexes(LVRelState *vacrel)
3783 : : {
1970 3784 : 16173 : Relation *indrels = vacrel->indrels;
3785 : 16173 : int nindexes = vacrel->nindexes;
3786 : 16173 : IndexBulkDeleteResult **indstats = vacrel->indstats;
3787 : :
1629 3788 [ - + ]: 16173 : Assert(vacrel->do_index_cleanup);
3789 : :
1970 3790 [ + + ]: 39556 : for (int idx = 0; idx < nindexes; idx++)
3791 : : {
3792 : 23383 : Relation indrel = indrels[idx];
3793 : 23383 : IndexBulkDeleteResult *istat = indstats[idx];
3794 : :
3795 [ + + + + ]: 23383 : if (istat == NULL || istat->estimated_count)
2411 akapila@postgresql.o 3796 : 21119 : continue;
3797 : :
3798 : : /* Update index statistics */
1970 pg@bowt.ie 3799 : 2264 : vac_update_relstats(indrel,
3800 : : istat->num_pages,
3801 : : istat->num_index_tuples,
3802 : : 0, 0,
3803 : : false,
3804 : : InvalidTransactionId,
3805 : : InvalidMultiXactId,
3806 : : NULL, NULL, false);
3807 : : }
2411 akapila@postgresql.o 3808 : 16173 : }
3809 : :
3810 : : /*
3811 : : * Error context callback for errors occurring during vacuum. The error
3812 : : * context messages for index phases should match the messages set in parallel
3813 : : * vacuum. If you change this function for those phases, change
3814 : : * parallel_vacuum_error_callback() as well.
3815 : : */
3816 : : static void
2341 3817 : 33 : vacuum_error_callback(void *arg)
3818 : : {
1970 pg@bowt.ie 3819 : 33 : LVRelState *errinfo = arg;
3820 : :
2341 akapila@postgresql.o 3821 [ + + + - : 33 : switch (errinfo->phase)
+ + ]
3822 : : {
3823 : 9 : case VACUUM_ERRCB_PHASE_SCAN_HEAP:
3824 [ - + ]: 9 : if (BlockNumberIsValid(errinfo->blkno))
3825 : : {
2192 akapila@postgresql.o 3826 [ # # # # :UBC 0 : if (OffsetNumberIsValid(errinfo->offnum))
# # ]
1846 peter@eisentraut.org 3827 : 0 : errcontext("while scanning block %u offset %u of relation \"%s.%s\"",
2192 akapila@postgresql.o 3828 : 0 : errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname);
3829 : : else
3830 : 0 : errcontext("while scanning block %u of relation \"%s.%s\"",
3831 : : errinfo->blkno, errinfo->relnamespace, errinfo->relname);
3832 : : }
3833 : : else
2194 akapila@postgresql.o 3834 :CBC 9 : errcontext("while scanning relation \"%s.%s\"",
3835 : : errinfo->relnamespace, errinfo->relname);
2341 3836 : 9 : break;
3837 : :
3838 : 1 : case VACUUM_ERRCB_PHASE_VACUUM_HEAP:
3839 [ - + ]: 1 : if (BlockNumberIsValid(errinfo->blkno))
3840 : : {
2192 akapila@postgresql.o 3841 [ # # # # :UBC 0 : if (OffsetNumberIsValid(errinfo->offnum))
# # ]
1846 peter@eisentraut.org 3842 : 0 : errcontext("while vacuuming block %u offset %u of relation \"%s.%s\"",
2192 akapila@postgresql.o 3843 : 0 : errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname);
3844 : : else
3845 : 0 : errcontext("while vacuuming block %u of relation \"%s.%s\"",
3846 : : errinfo->blkno, errinfo->relnamespace, errinfo->relname);
3847 : : }
3848 : : else
2194 akapila@postgresql.o 3849 :CBC 1 : errcontext("while vacuuming relation \"%s.%s\"",
3850 : : errinfo->relnamespace, errinfo->relname);
2341 3851 : 1 : break;
3852 : :
3853 : 2 : case VACUUM_ERRCB_PHASE_VACUUM_INDEX:
3854 : 2 : errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"",
3855 : : errinfo->indname, errinfo->relnamespace, errinfo->relname);
3856 : 2 : break;
3857 : :
2341 akapila@postgresql.o 3858 :LBC (1) : case VACUUM_ERRCB_PHASE_INDEX_CLEANUP:
3859 : (1) : errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"",
3860 : : errinfo->indname, errinfo->relnamespace, errinfo->relname);
3861 : (1) : break;
3862 : :
2341 akapila@postgresql.o 3863 :CBC 3 : case VACUUM_ERRCB_PHASE_TRUNCATE:
3864 [ + - ]: 3 : if (BlockNumberIsValid(errinfo->blkno))
3865 : 3 : errcontext("while truncating relation \"%s.%s\" to %u blocks",
3866 : : errinfo->relnamespace, errinfo->relname, errinfo->blkno);
3867 : 3 : break;
3868 : :
3869 : 18 : case VACUUM_ERRCB_PHASE_UNKNOWN:
3870 : : default:
3871 : 18 : return; /* do nothing; the errinfo may not be
3872 : : * initialized */
3873 : : }
3874 : : }
3875 : :
3876 : : /*
3877 : : * Updates the information required for vacuum error callback. This also saves
3878 : : * the current information which can be later restored via restore_vacuum_error_info.
3879 : : */
3880 : : static void
1970 pg@bowt.ie 3881 : 147413 : update_vacuum_error_info(LVRelState *vacrel, LVSavedErrInfo *saved_vacrel,
3882 : : int phase, BlockNumber blkno, OffsetNumber offnum)
3883 : : {
3884 [ + + ]: 147413 : if (saved_vacrel)
3885 : : {
3886 : 49735 : saved_vacrel->offnum = vacrel->offnum;
3887 : 49735 : saved_vacrel->blkno = vacrel->blkno;
3888 : 49735 : saved_vacrel->phase = vacrel->phase;
3889 : : }
3890 : :
3891 : 147413 : vacrel->blkno = blkno;
3892 : 147413 : vacrel->offnum = offnum;
3893 : 147413 : vacrel->phase = phase;
2248 akapila@postgresql.o 3894 : 147413 : }
3895 : :
3896 : : /*
3897 : : * Restores the vacuum information saved via a prior call to update_vacuum_error_info.
3898 : : */
3899 : : static void
1970 pg@bowt.ie 3900 : 49734 : restore_vacuum_error_info(LVRelState *vacrel,
3901 : : const LVSavedErrInfo *saved_vacrel)
3902 : : {
3903 : 49734 : vacrel->blkno = saved_vacrel->blkno;
3904 : 49734 : vacrel->offnum = saved_vacrel->offnum;
3905 : 49734 : vacrel->phase = saved_vacrel->phase;
2341 akapila@postgresql.o 3906 : 49734 : }
|