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