PostgreSQL Source Code  git master
vacuumlazy.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * vacuumlazy.c
4  * Concurrent ("lazy") vacuuming.
5  *
6  * The major space usage for vacuuming is storage for the dead tuple IDs that
7  * are to be removed from indexes. We want to ensure we can vacuum even the
8  * very largest relations with finite memory space usage. To do that, we set
9  * upper bounds on the memory that can be used for keeping track of dead TIDs
10  * at once.
11  *
12  * We are willing to use at most maintenance_work_mem (or perhaps
13  * autovacuum_work_mem) memory space to keep track of dead TIDs. If the
14  * TID store is full, we must call lazy_vacuum to vacuum indexes (and to vacuum
15  * the pages that we've pruned). This frees up the memory space dedicated to
16  * to store dead TIDs.
17  *
18  * In practice VACUUM will often complete its initial pass over the target
19  * heap relation without ever running out of space to store TIDs. This means
20  * that there only needs to be one call to lazy_vacuum, after the initial pass
21  * completes.
22  *
23  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
24  * Portions Copyright (c) 1994, Regents of the University of California
25  *
26  *
27  * IDENTIFICATION
28  * src/backend/access/heap/vacuumlazy.c
29  *
30  *-------------------------------------------------------------------------
31  */
32 #include "postgres.h"
33 
34 #include <math.h>
35 
36 #include "access/genam.h"
37 #include "access/heapam.h"
38 #include "access/heapam_xlog.h"
39 #include "access/htup_details.h"
40 #include "access/multixact.h"
41 #include "access/tidstore.h"
42 #include "access/transam.h"
43 #include "access/visibilitymap.h"
44 #include "access/xloginsert.h"
45 #include "catalog/storage.h"
46 #include "commands/dbcommands.h"
47 #include "commands/progress.h"
48 #include "commands/vacuum.h"
49 #include "common/int.h"
50 #include "executor/instrument.h"
51 #include "miscadmin.h"
52 #include "pgstat.h"
53 #include "portability/instr_time.h"
54 #include "postmaster/autovacuum.h"
55 #include "storage/bufmgr.h"
56 #include "storage/freespace.h"
57 #include "storage/lmgr.h"
58 #include "utils/lsyscache.h"
59 #include "utils/memutils.h"
60 #include "utils/pg_rusage.h"
61 #include "utils/timestamp.h"
62 
63 
64 /*
65  * Space/time tradeoff parameters: do these need to be user-tunable?
66  *
67  * To consider truncating the relation, we want there to be at least
68  * REL_TRUNCATE_MINIMUM or (relsize / REL_TRUNCATE_FRACTION) (whichever
69  * is less) potentially-freeable pages.
70  */
71 #define REL_TRUNCATE_MINIMUM 1000
72 #define REL_TRUNCATE_FRACTION 16
73 
74 /*
75  * Timing parameters for truncate locking heuristics.
76  *
77  * These were not exposed as user tunable GUC values because it didn't seem
78  * that the potential for improvement was great enough to merit the cost of
79  * supporting them.
80  */
81 #define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL 20 /* ms */
82 #define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL 50 /* ms */
83 #define VACUUM_TRUNCATE_LOCK_TIMEOUT 5000 /* ms */
84 
85 /*
86  * Threshold that controls whether we bypass index vacuuming and heap
87  * vacuuming as an optimization
88  */
89 #define BYPASS_THRESHOLD_PAGES 0.02 /* i.e. 2% of rel_pages */
90 
91 /*
92  * Perform a failsafe check each time we scan another 4GB of pages.
93  * (Note that this is deliberately kept to a power-of-two, usually 2^19.)
94  */
95 #define FAILSAFE_EVERY_PAGES \
96  ((BlockNumber) (((uint64) 4 * 1024 * 1024 * 1024) / BLCKSZ))
97 
98 /*
99  * When a table has no indexes, vacuum the FSM after every 8GB, approximately
100  * (it won't be exact because we only vacuum FSM after processing a heap page
101  * that has some removable tuples). When there are indexes, this is ignored,
102  * and we vacuum FSM after each index/heap cleaning pass.
103  */
104 #define VACUUM_FSM_EVERY_PAGES \
105  ((BlockNumber) (((uint64) 8 * 1024 * 1024 * 1024) / BLCKSZ))
106 
107 /*
108  * Before we consider skipping a page that's marked as clean in
109  * visibility map, we must've seen at least this many clean pages.
110  */
111 #define SKIP_PAGES_THRESHOLD ((BlockNumber) 32)
112 
113 /*
114  * Size of the prefetch window for lazy vacuum backwards truncation scan.
115  * Needs to be a power of 2.
116  */
117 #define PREFETCH_SIZE ((BlockNumber) 32)
118 
119 /*
120  * Macro to check if we are in a parallel vacuum. If true, we are in the
121  * parallel mode and the DSM segment is initialized.
122  */
123 #define ParallelVacuumIsActive(vacrel) ((vacrel)->pvs != NULL)
124 
125 /* Phases of vacuum during which we report error context. */
126 typedef enum
127 {
134 } VacErrPhase;
135 
136 typedef struct LVRelState
137 {
138  /* Target heap relation and its indexes */
141  int nindexes;
142 
143  /* Buffer access strategy and parallel vacuum state */
146 
147  /* Aggressive VACUUM? (must set relfrozenxid >= FreezeLimit) */
149  /* Use visibility map to skip? (disabled by DISABLE_PAGE_SKIPPING) */
151  /* Consider index vacuuming bypass optimization? */
153 
154  /* Doing index vacuuming, index cleanup, rel truncation? */
158 
159  /* VACUUM operation's cutoffs for freezing and pruning */
160  struct VacuumCutoffs cutoffs;
162  /* Tracks oldest extant XID/MXID for setting relfrozenxid/relminmxid */
166 
167  /* Error reporting state */
168  char *dbname;
170  char *relname;
171  char *indname; /* Current index name */
172  BlockNumber blkno; /* used only for heap operations */
173  OffsetNumber offnum; /* used only for heap operations */
175  bool verbose; /* VACUUM VERBOSE? */
176 
177  /*
178  * dead_items stores TIDs whose index tuples are deleted by index
179  * vacuuming. Each TID points to an LP_DEAD line pointer from a heap page
180  * that has been processed by lazy_scan_prune. Also needed by
181  * lazy_vacuum_heap_rel, which marks the same LP_DEAD line pointers as
182  * LP_UNUSED during second heap pass.
183  *
184  * Both dead_items and dead_items_info are allocated in shared memory in
185  * parallel vacuum cases.
186  */
187  TidStore *dead_items; /* TIDs whose index tuples we'll delete */
189 
190  BlockNumber rel_pages; /* total number of pages */
191  BlockNumber scanned_pages; /* # pages examined (not skipped via VM) */
192  BlockNumber removed_pages; /* # pages removed by relation truncation */
193  BlockNumber frozen_pages; /* # pages with newly frozen tuples */
194  BlockNumber lpdead_item_pages; /* # pages with LP_DEAD items */
195  BlockNumber missed_dead_pages; /* # pages with missed dead tuples */
196  BlockNumber nonempty_pages; /* actually, last nonempty page + 1 */
197 
198  /* Statistics output by us, for table */
199  double new_rel_tuples; /* new estimated total # of tuples */
200  double new_live_tuples; /* new estimated total # of live tuples */
201  /* Statistics output by index AMs */
203 
204  /* Instrumentation counters */
206  /* Counters that follow are only for scanned_pages */
207  int64 tuples_deleted; /* # deleted from table */
208  int64 tuples_frozen; /* # newly frozen */
209  int64 lpdead_items; /* # deleted from indexes */
210  int64 live_tuples; /* # live tuples remaining */
211  int64 recently_dead_tuples; /* # dead, but not yet removable */
212  int64 missed_dead_tuples; /* # removable, but not removed */
213 
214  /* State maintained by heap_vac_scan_next_block() */
215  BlockNumber current_block; /* last block returned */
216  BlockNumber next_unskippable_block; /* next unskippable block */
217  bool next_unskippable_allvis; /* its visibility status */
218  Buffer next_unskippable_vmbuffer; /* buffer containing its VM bit */
220 
221 /* Struct for saving and restoring vacuum error information. */
222 typedef struct LVSavedErrInfo
223 {
228 
229 
230 /* non-export function prototypes */
231 static void lazy_scan_heap(LVRelState *vacrel);
232 static bool heap_vac_scan_next_block(LVRelState *vacrel, BlockNumber *blkno,
233  bool *all_visible_according_to_vm);
234 static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis);
235 static bool lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf,
236  BlockNumber blkno, Page page,
237  bool sharelock, Buffer vmbuffer);
238 static void lazy_scan_prune(LVRelState *vacrel, Buffer buf,
239  BlockNumber blkno, Page page,
240  Buffer vmbuffer, bool all_visible_according_to_vm,
241  bool *has_lpdead_items);
242 static bool lazy_scan_noprune(LVRelState *vacrel, Buffer buf,
243  BlockNumber blkno, Page page,
244  bool *has_lpdead_items);
245 static void lazy_vacuum(LVRelState *vacrel);
246 static bool lazy_vacuum_all_indexes(LVRelState *vacrel);
247 static void lazy_vacuum_heap_rel(LVRelState *vacrel);
248 static void lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno,
249  Buffer buffer, OffsetNumber *offsets,
250  int num_offsets, Buffer vmbuffer);
251 static bool lazy_check_wraparound_failsafe(LVRelState *vacrel);
252 static void lazy_cleanup_all_indexes(LVRelState *vacrel);
254  IndexBulkDeleteResult *istat,
255  double reltuples,
256  LVRelState *vacrel);
258  IndexBulkDeleteResult *istat,
259  double reltuples,
260  bool estimated_count,
261  LVRelState *vacrel);
262 static bool should_attempt_truncation(LVRelState *vacrel);
263 static void lazy_truncate_heap(LVRelState *vacrel);
265  bool *lock_waiter_detected);
266 static void dead_items_alloc(LVRelState *vacrel, int nworkers);
267 static void dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets,
268  int num_offsets);
269 static void dead_items_reset(LVRelState *vacrel);
270 static void dead_items_cleanup(LVRelState *vacrel);
271 static bool heap_page_is_all_visible(LVRelState *vacrel, Buffer buf,
272  TransactionId *visibility_cutoff_xid, bool *all_frozen);
273 static void update_relstats_all_indexes(LVRelState *vacrel);
274 static void vacuum_error_callback(void *arg);
275 static void update_vacuum_error_info(LVRelState *vacrel,
276  LVSavedErrInfo *saved_vacrel,
277  int phase, BlockNumber blkno,
278  OffsetNumber offnum);
279 static void restore_vacuum_error_info(LVRelState *vacrel,
280  const LVSavedErrInfo *saved_vacrel);
281 
282 
283 /*
284  * heap_vacuum_rel() -- perform VACUUM for one heap relation
285  *
286  * This routine sets things up for and then calls lazy_scan_heap, where
287  * almost all work actually takes place. Finalizes everything after call
288  * returns by managing relation truncation and updating rel's pg_class
289  * entry. (Also updates pg_class entries for any indexes that need it.)
290  *
291  * At entry, we have already established a transaction and opened
292  * and locked the relation.
293  */
294 void
296  BufferAccessStrategy bstrategy)
297 {
298  LVRelState *vacrel;
299  bool verbose,
300  instrument,
301  skipwithvm,
302  frozenxid_updated,
303  minmulti_updated;
304  BlockNumber orig_rel_pages,
305  new_rel_pages,
306  new_rel_allvisible;
307  PGRUsage ru0;
308  TimestampTz starttime = 0;
309  PgStat_Counter startreadtime = 0,
310  startwritetime = 0;
311  WalUsage startwalusage = pgWalUsage;
312  int64 StartPageHit = VacuumPageHit,
313  StartPageMiss = VacuumPageMiss,
314  StartPageDirty = VacuumPageDirty;
315  ErrorContextCallback errcallback;
316  char **indnames = NULL;
317 
318  verbose = (params->options & VACOPT_VERBOSE) != 0;
319  instrument = (verbose || (AmAutoVacuumWorkerProcess() &&
320  params->log_min_duration >= 0));
321  if (instrument)
322  {
323  pg_rusage_init(&ru0);
324  starttime = GetCurrentTimestamp();
325  if (track_io_timing)
326  {
327  startreadtime = pgStatBlockReadTime;
328  startwritetime = pgStatBlockWriteTime;
329  }
330  }
331 
333  RelationGetRelid(rel));
334 
335  /*
336  * Setup error traceback support for ereport() first. The idea is to set
337  * up an error context callback to display additional information on any
338  * error during a vacuum. During different phases of vacuum, we update
339  * the state so that the error context callback always display current
340  * information.
341  *
342  * Copy the names of heap rel into local memory for error reporting
343  * purposes, too. It isn't always safe to assume that we can get the name
344  * of each rel. It's convenient for code in lazy_scan_heap to always use
345  * these temp copies.
346  */
347  vacrel = (LVRelState *) palloc0(sizeof(LVRelState));
350  vacrel->relname = pstrdup(RelationGetRelationName(rel));
351  vacrel->indname = NULL;
353  vacrel->verbose = verbose;
354  errcallback.callback = vacuum_error_callback;
355  errcallback.arg = vacrel;
356  errcallback.previous = error_context_stack;
357  error_context_stack = &errcallback;
358 
359  /* Set up high level stuff about rel and its indexes */
360  vacrel->rel = rel;
361  vac_open_indexes(vacrel->rel, RowExclusiveLock, &vacrel->nindexes,
362  &vacrel->indrels);
363  vacrel->bstrategy = bstrategy;
364  if (instrument && vacrel->nindexes > 0)
365  {
366  /* Copy index names used by instrumentation (not error reporting) */
367  indnames = palloc(sizeof(char *) * vacrel->nindexes);
368  for (int i = 0; i < vacrel->nindexes; i++)
369  indnames[i] = pstrdup(RelationGetRelationName(vacrel->indrels[i]));
370  }
371 
372  /*
373  * The index_cleanup param either disables index vacuuming and cleanup or
374  * forces it to go ahead when we would otherwise apply the index bypass
375  * optimization. The default is 'auto', which leaves the final decision
376  * up to lazy_vacuum().
377  *
378  * The truncate param allows user to avoid attempting relation truncation,
379  * though it can't force truncation to happen.
380  */
383  params->truncate != VACOPTVALUE_AUTO);
384 
385  /*
386  * While VacuumFailSafeActive is reset to false before calling this, we
387  * still need to reset it here due to recursive calls.
388  */
389  VacuumFailsafeActive = false;
390  vacrel->consider_bypass_optimization = true;
391  vacrel->do_index_vacuuming = true;
392  vacrel->do_index_cleanup = true;
393  vacrel->do_rel_truncate = (params->truncate != VACOPTVALUE_DISABLED);
394  if (params->index_cleanup == VACOPTVALUE_DISABLED)
395  {
396  /* Force disable index vacuuming up-front */
397  vacrel->do_index_vacuuming = false;
398  vacrel->do_index_cleanup = false;
399  }
400  else if (params->index_cleanup == VACOPTVALUE_ENABLED)
401  {
402  /* Force index vacuuming. Note that failsafe can still bypass. */
403  vacrel->consider_bypass_optimization = false;
404  }
405  else
406  {
407  /* Default/auto, make all decisions dynamically */
409  }
410 
411  /* Initialize page counters explicitly (be tidy) */
412  vacrel->scanned_pages = 0;
413  vacrel->removed_pages = 0;
414  vacrel->frozen_pages = 0;
415  vacrel->lpdead_item_pages = 0;
416  vacrel->missed_dead_pages = 0;
417  vacrel->nonempty_pages = 0;
418  /* dead_items_alloc allocates vacrel->dead_items later on */
419 
420  /* Allocate/initialize output statistics state */
421  vacrel->new_rel_tuples = 0;
422  vacrel->new_live_tuples = 0;
423  vacrel->indstats = (IndexBulkDeleteResult **)
424  palloc0(vacrel->nindexes * sizeof(IndexBulkDeleteResult *));
425 
426  /* Initialize remaining counters (be tidy) */
427  vacrel->num_index_scans = 0;
428  vacrel->tuples_deleted = 0;
429  vacrel->tuples_frozen = 0;
430  vacrel->lpdead_items = 0;
431  vacrel->live_tuples = 0;
432  vacrel->recently_dead_tuples = 0;
433  vacrel->missed_dead_tuples = 0;
434 
435  /*
436  * Get cutoffs that determine which deleted tuples are considered DEAD,
437  * not just RECENTLY_DEAD, and which XIDs/MXIDs to freeze. Then determine
438  * the extent of the blocks that we'll scan in lazy_scan_heap. It has to
439  * happen in this order to ensure that the OldestXmin cutoff field works
440  * as an upper bound on the XIDs stored in the pages we'll actually scan
441  * (NewRelfrozenXid tracking must never be allowed to miss unfrozen XIDs).
442  *
443  * Next acquire vistest, a related cutoff that's used in pruning. We
444  * expect vistest will always make heap_page_prune_and_freeze() remove any
445  * deleted tuple whose xmax is < OldestXmin. lazy_scan_prune must never
446  * become confused about whether a tuple should be frozen or removed. (In
447  * the future we might want to teach lazy_scan_prune to recompute vistest
448  * from time to time, to increase the number of dead tuples it can prune
449  * away.)
450  */
451  vacrel->aggressive = vacuum_get_cutoffs(rel, params, &vacrel->cutoffs);
452  vacrel->rel_pages = orig_rel_pages = RelationGetNumberOfBlocks(rel);
453  vacrel->vistest = GlobalVisTestFor(rel);
454  /* Initialize state used to track oldest extant XID/MXID */
455  vacrel->NewRelfrozenXid = vacrel->cutoffs.OldestXmin;
456  vacrel->NewRelminMxid = vacrel->cutoffs.OldestMxact;
457  vacrel->skippedallvis = false;
458  skipwithvm = true;
460  {
461  /*
462  * Force aggressive mode, and disable skipping blocks using the
463  * visibility map (even those set all-frozen)
464  */
465  vacrel->aggressive = true;
466  skipwithvm = false;
467  }
468 
469  vacrel->skipwithvm = skipwithvm;
470 
471  if (verbose)
472  {
473  if (vacrel->aggressive)
474  ereport(INFO,
475  (errmsg("aggressively vacuuming \"%s.%s.%s\"",
476  vacrel->dbname, vacrel->relnamespace,
477  vacrel->relname)));
478  else
479  ereport(INFO,
480  (errmsg("vacuuming \"%s.%s.%s\"",
481  vacrel->dbname, vacrel->relnamespace,
482  vacrel->relname)));
483  }
484 
485  /*
486  * Allocate dead_items memory using dead_items_alloc. This handles
487  * parallel VACUUM initialization as part of allocating shared memory
488  * space used for dead_items. (But do a failsafe precheck first, to
489  * ensure that parallel VACUUM won't be attempted at all when relfrozenxid
490  * is already dangerously old.)
491  */
493  dead_items_alloc(vacrel, params->nworkers);
494 
495  /*
496  * Call lazy_scan_heap to perform all required heap pruning, index
497  * vacuuming, and heap vacuuming (plus related processing)
498  */
499  lazy_scan_heap(vacrel);
500 
501  /*
502  * Free resources managed by dead_items_alloc. This ends parallel mode in
503  * passing when necessary.
504  */
505  dead_items_cleanup(vacrel);
507 
508  /*
509  * Update pg_class entries for each of rel's indexes where appropriate.
510  *
511  * Unlike the later update to rel's pg_class entry, this is not critical.
512  * Maintains relpages/reltuples statistics used by the planner only.
513  */
514  if (vacrel->do_index_cleanup)
516 
517  /* Done with rel's indexes */
518  vac_close_indexes(vacrel->nindexes, vacrel->indrels, NoLock);
519 
520  /* Optionally truncate rel */
521  if (should_attempt_truncation(vacrel))
522  lazy_truncate_heap(vacrel);
523 
524  /* Pop the error context stack */
525  error_context_stack = errcallback.previous;
526 
527  /* Report that we are now doing final cleanup */
530 
531  /*
532  * Prepare to update rel's pg_class entry.
533  *
534  * Aggressive VACUUMs must always be able to advance relfrozenxid to a
535  * value >= FreezeLimit, and relminmxid to a value >= MultiXactCutoff.
536  * Non-aggressive VACUUMs may advance them by any amount, or not at all.
537  */
538  Assert(vacrel->NewRelfrozenXid == vacrel->cutoffs.OldestXmin ||
540  vacrel->cutoffs.relfrozenxid,
541  vacrel->NewRelfrozenXid));
542  Assert(vacrel->NewRelminMxid == vacrel->cutoffs.OldestMxact ||
544  vacrel->cutoffs.relminmxid,
545  vacrel->NewRelminMxid));
546  if (vacrel->skippedallvis)
547  {
548  /*
549  * Must keep original relfrozenxid in a non-aggressive VACUUM that
550  * chose to skip an all-visible page range. The state that tracks new
551  * values will have missed unfrozen XIDs from the pages we skipped.
552  */
553  Assert(!vacrel->aggressive);
556  }
557 
558  /*
559  * For safety, clamp relallvisible to be not more than what we're setting
560  * pg_class.relpages to
561  */
562  new_rel_pages = vacrel->rel_pages; /* After possible rel truncation */
563  visibilitymap_count(rel, &new_rel_allvisible, NULL);
564  if (new_rel_allvisible > new_rel_pages)
565  new_rel_allvisible = new_rel_pages;
566 
567  /*
568  * Now actually update rel's pg_class entry.
569  *
570  * In principle new_live_tuples could be -1 indicating that we (still)
571  * don't know the tuple count. In practice that can't happen, since we
572  * scan every page that isn't skipped using the visibility map.
573  */
574  vac_update_relstats(rel, new_rel_pages, vacrel->new_live_tuples,
575  new_rel_allvisible, vacrel->nindexes > 0,
576  vacrel->NewRelfrozenXid, vacrel->NewRelminMxid,
577  &frozenxid_updated, &minmulti_updated, false);
578 
579  /*
580  * Report results to the cumulative stats system, too.
581  *
582  * Deliberately avoid telling the stats system about LP_DEAD items that
583  * remain in the table due to VACUUM bypassing index and heap vacuuming.
584  * ANALYZE will consider the remaining LP_DEAD items to be dead "tuples".
585  * It seems like a good idea to err on the side of not vacuuming again too
586  * soon in cases where the failsafe prevented significant amounts of heap
587  * vacuuming.
588  */
590  rel->rd_rel->relisshared,
591  Max(vacrel->new_live_tuples, 0),
592  vacrel->recently_dead_tuples +
593  vacrel->missed_dead_tuples);
595 
596  if (instrument)
597  {
598  TimestampTz endtime = GetCurrentTimestamp();
599 
600  if (verbose || params->log_min_duration == 0 ||
601  TimestampDifferenceExceeds(starttime, endtime,
602  params->log_min_duration))
603  {
604  long secs_dur;
605  int usecs_dur;
606  WalUsage walusage;
608  char *msgfmt;
609  int32 diff;
610  int64 PageHitOp = VacuumPageHit - StartPageHit,
611  PageMissOp = VacuumPageMiss - StartPageMiss,
612  PageDirtyOp = VacuumPageDirty - StartPageDirty;
613  double read_rate = 0,
614  write_rate = 0;
615 
616  TimestampDifference(starttime, endtime, &secs_dur, &usecs_dur);
617  memset(&walusage, 0, sizeof(WalUsage));
618  WalUsageAccumDiff(&walusage, &pgWalUsage, &startwalusage);
619 
621  if (verbose)
622  {
623  /*
624  * Aggressiveness already reported earlier, in dedicated
625  * VACUUM VERBOSE ereport
626  */
627  Assert(!params->is_wraparound);
628  msgfmt = _("finished vacuuming \"%s.%s.%s\": index scans: %d\n");
629  }
630  else if (params->is_wraparound)
631  {
632  /*
633  * While it's possible for a VACUUM to be both is_wraparound
634  * and !aggressive, that's just a corner-case -- is_wraparound
635  * implies aggressive. Produce distinct output for the corner
636  * case all the same, just in case.
637  */
638  if (vacrel->aggressive)
639  msgfmt = _("automatic aggressive vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
640  else
641  msgfmt = _("automatic vacuum to prevent wraparound of table \"%s.%s.%s\": index scans: %d\n");
642  }
643  else
644  {
645  if (vacrel->aggressive)
646  msgfmt = _("automatic aggressive vacuum of table \"%s.%s.%s\": index scans: %d\n");
647  else
648  msgfmt = _("automatic vacuum of table \"%s.%s.%s\": index scans: %d\n");
649  }
650  appendStringInfo(&buf, msgfmt,
651  vacrel->dbname,
652  vacrel->relnamespace,
653  vacrel->relname,
654  vacrel->num_index_scans);
655  appendStringInfo(&buf, _("pages: %u removed, %u remain, %u scanned (%.2f%% of total)\n"),
656  vacrel->removed_pages,
657  new_rel_pages,
658  vacrel->scanned_pages,
659  orig_rel_pages == 0 ? 100.0 :
660  100.0 * vacrel->scanned_pages / orig_rel_pages);
662  _("tuples: %lld removed, %lld remain, %lld are dead but not yet removable\n"),
663  (long long) vacrel->tuples_deleted,
664  (long long) vacrel->new_rel_tuples,
665  (long long) vacrel->recently_dead_tuples);
666  if (vacrel->missed_dead_tuples > 0)
668  _("tuples missed: %lld dead from %u pages not removed due to cleanup lock contention\n"),
669  (long long) vacrel->missed_dead_tuples,
670  vacrel->missed_dead_pages);
671  diff = (int32) (ReadNextTransactionId() -
672  vacrel->cutoffs.OldestXmin);
674  _("removable cutoff: %u, which was %d XIDs old when operation ended\n"),
675  vacrel->cutoffs.OldestXmin, diff);
676  if (frozenxid_updated)
677  {
678  diff = (int32) (vacrel->NewRelfrozenXid -
679  vacrel->cutoffs.relfrozenxid);
681  _("new relfrozenxid: %u, which is %d XIDs ahead of previous value\n"),
682  vacrel->NewRelfrozenXid, diff);
683  }
684  if (minmulti_updated)
685  {
686  diff = (int32) (vacrel->NewRelminMxid -
687  vacrel->cutoffs.relminmxid);
689  _("new relminmxid: %u, which is %d MXIDs ahead of previous value\n"),
690  vacrel->NewRelminMxid, diff);
691  }
692  appendStringInfo(&buf, _("frozen: %u pages from table (%.2f%% of total) had %lld tuples frozen\n"),
693  vacrel->frozen_pages,
694  orig_rel_pages == 0 ? 100.0 :
695  100.0 * vacrel->frozen_pages / orig_rel_pages,
696  (long long) vacrel->tuples_frozen);
697  if (vacrel->do_index_vacuuming)
698  {
699  if (vacrel->nindexes == 0 || vacrel->num_index_scans == 0)
700  appendStringInfoString(&buf, _("index scan not needed: "));
701  else
702  appendStringInfoString(&buf, _("index scan needed: "));
703 
704  msgfmt = _("%u pages from table (%.2f%% of total) had %lld dead item identifiers removed\n");
705  }
706  else
707  {
709  appendStringInfoString(&buf, _("index scan bypassed: "));
710  else
711  appendStringInfoString(&buf, _("index scan bypassed by failsafe: "));
712 
713  msgfmt = _("%u pages from table (%.2f%% of total) have %lld dead item identifiers\n");
714  }
715  appendStringInfo(&buf, msgfmt,
716  vacrel->lpdead_item_pages,
717  orig_rel_pages == 0 ? 100.0 :
718  100.0 * vacrel->lpdead_item_pages / orig_rel_pages,
719  (long long) vacrel->lpdead_items);
720  for (int i = 0; i < vacrel->nindexes; i++)
721  {
722  IndexBulkDeleteResult *istat = vacrel->indstats[i];
723 
724  if (!istat)
725  continue;
726 
728  _("index \"%s\": pages: %u in total, %u newly deleted, %u currently deleted, %u reusable\n"),
729  indnames[i],
730  istat->num_pages,
731  istat->pages_newly_deleted,
732  istat->pages_deleted,
733  istat->pages_free);
734  }
735  if (track_io_timing)
736  {
737  double read_ms = (double) (pgStatBlockReadTime - startreadtime) / 1000;
738  double write_ms = (double) (pgStatBlockWriteTime - startwritetime) / 1000;
739 
740  appendStringInfo(&buf, _("I/O timings: read: %.3f ms, write: %.3f ms\n"),
741  read_ms, write_ms);
742  }
743  if (secs_dur > 0 || usecs_dur > 0)
744  {
745  read_rate = (double) BLCKSZ * PageMissOp / (1024 * 1024) /
746  (secs_dur + usecs_dur / 1000000.0);
747  write_rate = (double) BLCKSZ * PageDirtyOp / (1024 * 1024) /
748  (secs_dur + usecs_dur / 1000000.0);
749  }
750  appendStringInfo(&buf, _("avg read rate: %.3f MB/s, avg write rate: %.3f MB/s\n"),
751  read_rate, write_rate);
753  _("buffer usage: %lld hits, %lld misses, %lld dirtied\n"),
754  (long long) PageHitOp,
755  (long long) PageMissOp,
756  (long long) PageDirtyOp);
758  _("WAL usage: %lld records, %lld full page images, %llu bytes\n"),
759  (long long) walusage.wal_records,
760  (long long) walusage.wal_fpi,
761  (unsigned long long) walusage.wal_bytes);
762  appendStringInfo(&buf, _("system usage: %s"), pg_rusage_show(&ru0));
763 
764  ereport(verbose ? INFO : LOG,
765  (errmsg_internal("%s", buf.data)));
766  pfree(buf.data);
767  }
768  }
769 
770  /* Cleanup index statistics and index names */
771  for (int i = 0; i < vacrel->nindexes; i++)
772  {
773  if (vacrel->indstats[i])
774  pfree(vacrel->indstats[i]);
775 
776  if (instrument)
777  pfree(indnames[i]);
778  }
779 }
780 
781 /*
782  * lazy_scan_heap() -- workhorse function for VACUUM
783  *
784  * This routine prunes each page in the heap, and considers the need to
785  * freeze remaining tuples with storage (not including pages that can be
786  * skipped using the visibility map). Also performs related maintenance
787  * of the FSM and visibility map. These steps all take place during an
788  * initial pass over the target heap relation.
789  *
790  * Also invokes lazy_vacuum_all_indexes to vacuum indexes, which largely
791  * consists of deleting index tuples that point to LP_DEAD items left in
792  * heap pages following pruning. Earlier initial pass over the heap will
793  * have collected the TIDs whose index tuples need to be removed.
794  *
795  * Finally, invokes lazy_vacuum_heap_rel to vacuum heap pages, which
796  * largely consists of marking LP_DEAD items (from vacrel->dead_items)
797  * as LP_UNUSED. This has to happen in a second, final pass over the
798  * heap, to preserve a basic invariant that all index AMs rely on: no
799  * extant index tuple can ever be allowed to contain a TID that points to
800  * an LP_UNUSED line pointer in the heap. We must disallow premature
801  * recycling of line pointers to avoid index scans that get confused
802  * about which TID points to which tuple immediately after recycling.
803  * (Actually, this isn't a concern when target heap relation happens to
804  * have no indexes, which allows us to safely apply the one-pass strategy
805  * as an optimization).
806  *
807  * In practice we often have enough space to fit all TIDs, and so won't
808  * need to call lazy_vacuum more than once, after our initial pass over
809  * the heap has totally finished. Otherwise things are slightly more
810  * complicated: our "initial pass" over the heap applies only to those
811  * pages that were pruned before we needed to call lazy_vacuum, and our
812  * "final pass" over the heap only vacuums these same heap pages.
813  * However, we process indexes in full every time lazy_vacuum is called,
814  * which makes index processing very inefficient when memory is in short
815  * supply.
816  */
817 static void
819 {
820  BlockNumber rel_pages = vacrel->rel_pages,
821  blkno,
822  next_fsm_block_to_vacuum = 0;
823  bool all_visible_according_to_vm;
824 
825  TidStore *dead_items = vacrel->dead_items;
826  VacDeadItemsInfo *dead_items_info = vacrel->dead_items_info;
827  Buffer vmbuffer = InvalidBuffer;
828  const int initprog_index[] = {
832  };
833  int64 initprog_val[3];
834 
835  /* Report that we're scanning the heap, advertising total # of blocks */
836  initprog_val[0] = PROGRESS_VACUUM_PHASE_SCAN_HEAP;
837  initprog_val[1] = rel_pages;
838  initprog_val[2] = dead_items_info->max_bytes;
839  pgstat_progress_update_multi_param(3, initprog_index, initprog_val);
840 
841  /* Initialize for the first heap_vac_scan_next_block() call */
844  vacrel->next_unskippable_allvis = false;
846 
847  while (heap_vac_scan_next_block(vacrel, &blkno, &all_visible_according_to_vm))
848  {
849  Buffer buf;
850  Page page;
851  bool has_lpdead_items;
852  bool got_cleanup_lock = false;
853 
854  vacrel->scanned_pages++;
855 
856  /* Report as block scanned, update error traceback information */
859  blkno, InvalidOffsetNumber);
860 
862 
863  /*
864  * Regularly check if wraparound failsafe should trigger.
865  *
866  * There is a similar check inside lazy_vacuum_all_indexes(), but
867  * relfrozenxid might start to look dangerously old before we reach
868  * that point. This check also provides failsafe coverage for the
869  * one-pass strategy, and the two-pass strategy with the index_cleanup
870  * param set to 'off'.
871  */
872  if (vacrel->scanned_pages % FAILSAFE_EVERY_PAGES == 0)
874 
875  /*
876  * Consider if we definitely have enough space to process TIDs on page
877  * already. If we are close to overrunning the available space for
878  * dead_items TIDs, pause and do a cycle of vacuuming before we tackle
879  * this page.
880  */
881  if (TidStoreMemoryUsage(dead_items) > dead_items_info->max_bytes)
882  {
883  /*
884  * Before beginning index vacuuming, we release any pin we may
885  * hold on the visibility map page. This isn't necessary for
886  * correctness, but we do it anyway to avoid holding the pin
887  * across a lengthy, unrelated operation.
888  */
889  if (BufferIsValid(vmbuffer))
890  {
891  ReleaseBuffer(vmbuffer);
892  vmbuffer = InvalidBuffer;
893  }
894 
895  /* Perform a round of index and heap vacuuming */
896  vacrel->consider_bypass_optimization = false;
897  lazy_vacuum(vacrel);
898 
899  /*
900  * Vacuum the Free Space Map to make newly-freed space visible on
901  * upper-level FSM pages. Note we have not yet processed blkno.
902  */
903  FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
904  blkno);
905  next_fsm_block_to_vacuum = blkno;
906 
907  /* Report that we are once again scanning the heap */
910  }
911 
912  /*
913  * Pin the visibility map page in case we need to mark the page
914  * all-visible. In most cases this will be very cheap, because we'll
915  * already have the correct page pinned anyway.
916  */
917  visibilitymap_pin(vacrel->rel, blkno, &vmbuffer);
918 
919  buf = ReadBufferExtended(vacrel->rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
920  vacrel->bstrategy);
921  page = BufferGetPage(buf);
922 
923  /*
924  * We need a buffer cleanup lock to prune HOT chains and defragment
925  * the page in lazy_scan_prune. But when it's not possible to acquire
926  * a cleanup lock right away, we may be able to settle for reduced
927  * processing using lazy_scan_noprune.
928  */
929  got_cleanup_lock = ConditionalLockBufferForCleanup(buf);
930 
931  if (!got_cleanup_lock)
933 
934  /* Check for new or empty pages before lazy_scan_[no]prune call */
935  if (lazy_scan_new_or_empty(vacrel, buf, blkno, page, !got_cleanup_lock,
936  vmbuffer))
937  {
938  /* Processed as new/empty page (lock and pin released) */
939  continue;
940  }
941 
942  /*
943  * If we didn't get the cleanup lock, we can still collect LP_DEAD
944  * items in the dead_items area for later vacuuming, count live and
945  * recently dead tuples for vacuum logging, and determine if this
946  * block could later be truncated. If we encounter any xid/mxids that
947  * require advancing the relfrozenxid/relminxid, we'll have to wait
948  * for a cleanup lock and call lazy_scan_prune().
949  */
950  if (!got_cleanup_lock &&
951  !lazy_scan_noprune(vacrel, buf, blkno, page, &has_lpdead_items))
952  {
953  /*
954  * lazy_scan_noprune could not do all required processing. Wait
955  * for a cleanup lock, and call lazy_scan_prune in the usual way.
956  */
957  Assert(vacrel->aggressive);
960  got_cleanup_lock = true;
961  }
962 
963  /*
964  * If we have a cleanup lock, we must now prune, freeze, and count
965  * tuples. We may have acquired the cleanup lock originally, or we may
966  * have gone back and acquired it after lazy_scan_noprune() returned
967  * false. Either way, the page hasn't been processed yet.
968  *
969  * Like lazy_scan_noprune(), lazy_scan_prune() will count
970  * recently_dead_tuples and live tuples for vacuum logging, determine
971  * if the block can later be truncated, and accumulate the details of
972  * remaining LP_DEAD line pointers on the page into dead_items. These
973  * dead items include those pruned by lazy_scan_prune() as well as
974  * line pointers previously marked LP_DEAD.
975  */
976  if (got_cleanup_lock)
977  lazy_scan_prune(vacrel, buf, blkno, page,
978  vmbuffer, all_visible_according_to_vm,
979  &has_lpdead_items);
980 
981  /*
982  * Now drop the buffer lock and, potentially, update the FSM.
983  *
984  * Our goal is to update the freespace map the last time we touch the
985  * page. If we'll process a block in the second pass, we may free up
986  * additional space on the page, so it is better to update the FSM
987  * after the second pass. If the relation has no indexes, or if index
988  * vacuuming is disabled, there will be no second heap pass; if this
989  * particular page has no dead items, the second heap pass will not
990  * touch this page. So, in those cases, update the FSM now.
991  *
992  * Note: In corner cases, it's possible to miss updating the FSM
993  * entirely. If index vacuuming is currently enabled, we'll skip the
994  * FSM update now. But if failsafe mode is later activated, or there
995  * are so few dead tuples that index vacuuming is bypassed, there will
996  * also be no opportunity to update the FSM later, because we'll never
997  * revisit this page. Since updating the FSM is desirable but not
998  * absolutely required, that's OK.
999  */
1000  if (vacrel->nindexes == 0
1001  || !vacrel->do_index_vacuuming
1002  || !has_lpdead_items)
1003  {
1004  Size freespace = PageGetHeapFreeSpace(page);
1005 
1007  RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1008 
1009  /*
1010  * Periodically perform FSM vacuuming to make newly-freed space
1011  * visible on upper FSM pages. This is done after vacuuming if the
1012  * table has indexes. There will only be newly-freed space if we
1013  * held the cleanup lock and lazy_scan_prune() was called.
1014  */
1015  if (got_cleanup_lock && vacrel->nindexes == 0 && has_lpdead_items &&
1016  blkno - next_fsm_block_to_vacuum >= VACUUM_FSM_EVERY_PAGES)
1017  {
1018  FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum,
1019  blkno);
1020  next_fsm_block_to_vacuum = blkno;
1021  }
1022  }
1023  else
1025  }
1026 
1027  vacrel->blkno = InvalidBlockNumber;
1028  if (BufferIsValid(vmbuffer))
1029  ReleaseBuffer(vmbuffer);
1030 
1031  /* report that everything is now scanned */
1033 
1034  /* now we can compute the new value for pg_class.reltuples */
1035  vacrel->new_live_tuples = vac_estimate_reltuples(vacrel->rel, rel_pages,
1036  vacrel->scanned_pages,
1037  vacrel->live_tuples);
1038 
1039  /*
1040  * Also compute the total number of surviving heap entries. In the
1041  * (unlikely) scenario that new_live_tuples is -1, take it as zero.
1042  */
1043  vacrel->new_rel_tuples =
1044  Max(vacrel->new_live_tuples, 0) + vacrel->recently_dead_tuples +
1045  vacrel->missed_dead_tuples;
1046 
1047  /*
1048  * Do index vacuuming (call each index's ambulkdelete routine), then do
1049  * related heap vacuuming
1050  */
1051  if (dead_items_info->num_items > 0)
1052  lazy_vacuum(vacrel);
1053 
1054  /*
1055  * Vacuum the remainder of the Free Space Map. We must do this whether or
1056  * not there were indexes, and whether or not we bypassed index vacuuming.
1057  */
1058  if (blkno > next_fsm_block_to_vacuum)
1059  FreeSpaceMapVacuumRange(vacrel->rel, next_fsm_block_to_vacuum, blkno);
1060 
1061  /* report all blocks vacuumed */
1063 
1064  /* Do final index cleanup (call each index's amvacuumcleanup routine) */
1065  if (vacrel->nindexes > 0 && vacrel->do_index_cleanup)
1066  lazy_cleanup_all_indexes(vacrel);
1067 }
1068 
1069 /*
1070  * heap_vac_scan_next_block() -- get next block for vacuum to process
1071  *
1072  * lazy_scan_heap() calls here every time it needs to get the next block to
1073  * prune and vacuum. The function uses the visibility map, vacuum options,
1074  * and various thresholds to skip blocks which do not need to be processed and
1075  * sets blkno to the next block to process.
1076  *
1077  * The block number and visibility status of the next block to process are set
1078  * in *blkno and *all_visible_according_to_vm. The return value is false if
1079  * there are no further blocks to process.
1080  *
1081  * vacrel is an in/out parameter here. Vacuum options and information about
1082  * the relation are read. vacrel->skippedallvis is set if we skip a block
1083  * that's all-visible but not all-frozen, to ensure that we don't update
1084  * relfrozenxid in that case. vacrel also holds information about the next
1085  * unskippable block, as bookkeeping for this function.
1086  */
1087 static bool
1089  bool *all_visible_according_to_vm)
1090 {
1091  BlockNumber next_block;
1092 
1093  /* relies on InvalidBlockNumber + 1 overflowing to 0 on first call */
1094  next_block = vacrel->current_block + 1;
1095 
1096  /* Have we reached the end of the relation? */
1097  if (next_block >= vacrel->rel_pages)
1098  {
1100  {
1103  }
1104  *blkno = vacrel->rel_pages;
1105  return false;
1106  }
1107 
1108  /*
1109  * We must be in one of the three following states:
1110  */
1111  if (next_block > vacrel->next_unskippable_block ||
1113  {
1114  /*
1115  * 1. We have just processed an unskippable block (or we're at the
1116  * beginning of the scan). Find the next unskippable block using the
1117  * visibility map.
1118  */
1119  bool skipsallvis;
1120 
1121  find_next_unskippable_block(vacrel, &skipsallvis);
1122 
1123  /*
1124  * We now know the next block that we must process. It can be the
1125  * next block after the one we just processed, or something further
1126  * ahead. If it's further ahead, we can jump to it, but we choose to
1127  * do so only if we can skip at least SKIP_PAGES_THRESHOLD consecutive
1128  * pages. Since we're reading sequentially, the OS should be doing
1129  * readahead for us, so there's no gain in skipping a page now and
1130  * then. Skipping such a range might even discourage sequential
1131  * detection.
1132  *
1133  * This test also enables more frequent relfrozenxid advancement
1134  * during non-aggressive VACUUMs. If the range has any all-visible
1135  * pages then skipping makes updating relfrozenxid unsafe, which is a
1136  * real downside.
1137  */
1138  if (vacrel->next_unskippable_block - next_block >= SKIP_PAGES_THRESHOLD)
1139  {
1140  next_block = vacrel->next_unskippable_block;
1141  if (skipsallvis)
1142  vacrel->skippedallvis = true;
1143  }
1144  }
1145 
1146  /* Now we must be in one of the two remaining states: */
1147  if (next_block < vacrel->next_unskippable_block)
1148  {
1149  /*
1150  * 2. We are processing a range of blocks that we could have skipped
1151  * but chose not to. We know that they are all-visible in the VM,
1152  * otherwise they would've been unskippable.
1153  */
1154  *blkno = vacrel->current_block = next_block;
1155  *all_visible_according_to_vm = true;
1156  return true;
1157  }
1158  else
1159  {
1160  /*
1161  * 3. We reached the next unskippable block. Process it. On next
1162  * iteration, we will be back in state 1.
1163  */
1164  Assert(next_block == vacrel->next_unskippable_block);
1165 
1166  *blkno = vacrel->current_block = next_block;
1167  *all_visible_according_to_vm = vacrel->next_unskippable_allvis;
1168  return true;
1169  }
1170 }
1171 
1172 /*
1173  * Find the next unskippable block in a vacuum scan using the visibility map.
1174  * The next unskippable block and its visibility information is updated in
1175  * vacrel.
1176  *
1177  * Note: our opinion of which blocks can be skipped can go stale immediately.
1178  * It's okay if caller "misses" a page whose all-visible or all-frozen marking
1179  * was concurrently cleared, though. All that matters is that caller scan all
1180  * pages whose tuples might contain XIDs < OldestXmin, or MXIDs < OldestMxact.
1181  * (Actually, non-aggressive VACUUMs can choose to skip all-visible pages with
1182  * older XIDs/MXIDs. The *skippedallvis flag will be set here when the choice
1183  * to skip such a range is actually made, making everything safe.)
1184  */
1185 static void
1186 find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis)
1187 {
1188  BlockNumber rel_pages = vacrel->rel_pages;
1189  BlockNumber next_unskippable_block = vacrel->next_unskippable_block + 1;
1190  Buffer next_unskippable_vmbuffer = vacrel->next_unskippable_vmbuffer;
1191  bool next_unskippable_allvis;
1192 
1193  *skipsallvis = false;
1194 
1195  for (;;)
1196  {
1197  uint8 mapbits = visibilitymap_get_status(vacrel->rel,
1198  next_unskippable_block,
1199  &next_unskippable_vmbuffer);
1200 
1201  next_unskippable_allvis = (mapbits & VISIBILITYMAP_ALL_VISIBLE) != 0;
1202 
1203  /*
1204  * A block is unskippable if it is not all visible according to the
1205  * visibility map.
1206  */
1207  if (!next_unskippable_allvis)
1208  {
1209  Assert((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0);
1210  break;
1211  }
1212 
1213  /*
1214  * Caller must scan the last page to determine whether it has tuples
1215  * (caller must have the opportunity to set vacrel->nonempty_pages).
1216  * This rule avoids having lazy_truncate_heap() take access-exclusive
1217  * lock on rel to attempt a truncation that fails anyway, just because
1218  * there are tuples on the last page (it is likely that there will be
1219  * tuples on other nearby pages as well, but those can be skipped).
1220  *
1221  * Implement this by always treating the last block as unsafe to skip.
1222  */
1223  if (next_unskippable_block == rel_pages - 1)
1224  break;
1225 
1226  /* DISABLE_PAGE_SKIPPING makes all skipping unsafe */
1227  if (!vacrel->skipwithvm)
1228  break;
1229 
1230  /*
1231  * Aggressive VACUUM caller can't skip pages just because they are
1232  * all-visible. They may still skip all-frozen pages, which can't
1233  * contain XIDs < OldestXmin (XIDs that aren't already frozen by now).
1234  */
1235  if ((mapbits & VISIBILITYMAP_ALL_FROZEN) == 0)
1236  {
1237  if (vacrel->aggressive)
1238  break;
1239 
1240  /*
1241  * All-visible block is safe to skip in non-aggressive case. But
1242  * remember that the final range contains such a block for later.
1243  */
1244  *skipsallvis = true;
1245  }
1246 
1247  next_unskippable_block++;
1248  }
1249 
1250  /* write the local variables back to vacrel */
1251  vacrel->next_unskippable_block = next_unskippable_block;
1252  vacrel->next_unskippable_allvis = next_unskippable_allvis;
1253  vacrel->next_unskippable_vmbuffer = next_unskippable_vmbuffer;
1254 }
1255 
1256 /*
1257  * lazy_scan_new_or_empty() -- lazy_scan_heap() new/empty page handling.
1258  *
1259  * Must call here to handle both new and empty pages before calling
1260  * lazy_scan_prune or lazy_scan_noprune, since they're not prepared to deal
1261  * with new or empty pages.
1262  *
1263  * It's necessary to consider new pages as a special case, since the rules for
1264  * maintaining the visibility map and FSM with empty pages are a little
1265  * different (though new pages can be truncated away during rel truncation).
1266  *
1267  * Empty pages are not really a special case -- they're just heap pages that
1268  * have no allocated tuples (including even LP_UNUSED items). You might
1269  * wonder why we need to handle them here all the same. It's only necessary
1270  * because of a corner-case involving a hard crash during heap relation
1271  * extension. If we ever make relation-extension crash safe, then it should
1272  * no longer be necessary to deal with empty pages here (or new pages, for
1273  * that matter).
1274  *
1275  * Caller must hold at least a shared lock. We might need to escalate the
1276  * lock in that case, so the type of lock caller holds needs to be specified
1277  * using 'sharelock' argument.
1278  *
1279  * Returns false in common case where caller should go on to call
1280  * lazy_scan_prune (or lazy_scan_noprune). Otherwise returns true, indicating
1281  * that lazy_scan_heap is done processing the page, releasing lock on caller's
1282  * behalf.
1283  */
1284 static bool
1286  Page page, bool sharelock, Buffer vmbuffer)
1287 {
1288  Size freespace;
1289 
1290  if (PageIsNew(page))
1291  {
1292  /*
1293  * All-zeroes pages can be left over if either a backend extends the
1294  * relation by a single page, but crashes before the newly initialized
1295  * page has been written out, or when bulk-extending the relation
1296  * (which creates a number of empty pages at the tail end of the
1297  * relation), and then enters them into the FSM.
1298  *
1299  * Note we do not enter the page into the visibilitymap. That has the
1300  * downside that we repeatedly visit this page in subsequent vacuums,
1301  * but otherwise we'll never discover the space on a promoted standby.
1302  * The harm of repeated checking ought to normally not be too bad. The
1303  * space usually should be used at some point, otherwise there
1304  * wouldn't be any regular vacuums.
1305  *
1306  * Make sure these pages are in the FSM, to ensure they can be reused.
1307  * Do that by testing if there's any space recorded for the page. If
1308  * not, enter it. We do so after releasing the lock on the heap page,
1309  * the FSM is approximate, after all.
1310  */
1312 
1313  if (GetRecordedFreeSpace(vacrel->rel, blkno) == 0)
1314  {
1315  freespace = BLCKSZ - SizeOfPageHeaderData;
1316 
1317  RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1318  }
1319 
1320  return true;
1321  }
1322 
1323  if (PageIsEmpty(page))
1324  {
1325  /*
1326  * It seems likely that caller will always be able to get a cleanup
1327  * lock on an empty page. But don't take any chances -- escalate to
1328  * an exclusive lock (still don't need a cleanup lock, though).
1329  */
1330  if (sharelock)
1331  {
1334 
1335  if (!PageIsEmpty(page))
1336  {
1337  /* page isn't new or empty -- keep lock and pin for now */
1338  return false;
1339  }
1340  }
1341  else
1342  {
1343  /* Already have a full cleanup lock (which is more than enough) */
1344  }
1345 
1346  /*
1347  * Unlike new pages, empty pages are always set all-visible and
1348  * all-frozen.
1349  */
1350  if (!PageIsAllVisible(page))
1351  {
1353 
1354  /* mark buffer dirty before writing a WAL record */
1356 
1357  /*
1358  * It's possible that another backend has extended the heap,
1359  * initialized the page, and then failed to WAL-log the page due
1360  * to an ERROR. Since heap extension is not WAL-logged, recovery
1361  * might try to replay our record setting the page all-visible and
1362  * find that the page isn't initialized, which will cause a PANIC.
1363  * To prevent that, check whether the page has been previously
1364  * WAL-logged, and if not, do that now.
1365  */
1366  if (RelationNeedsWAL(vacrel->rel) &&
1367  PageGetLSN(page) == InvalidXLogRecPtr)
1368  log_newpage_buffer(buf, true);
1369 
1370  PageSetAllVisible(page);
1371  visibilitymap_set(vacrel->rel, blkno, buf, InvalidXLogRecPtr,
1372  vmbuffer, InvalidTransactionId,
1374  END_CRIT_SECTION();
1375  }
1376 
1377  freespace = PageGetHeapFreeSpace(page);
1379  RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
1380  return true;
1381  }
1382 
1383  /* page isn't new or empty -- keep lock and pin */
1384  return false;
1385 }
1386 
1387 /* qsort comparator for sorting OffsetNumbers */
1388 static int
1389 cmpOffsetNumbers(const void *a, const void *b)
1390 {
1391  return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
1392 }
1393 
1394 /*
1395  * lazy_scan_prune() -- lazy_scan_heap() pruning and freezing.
1396  *
1397  * Caller must hold pin and buffer cleanup lock on the buffer.
1398  *
1399  * vmbuffer is the buffer containing the VM block with visibility information
1400  * for the heap block, blkno. all_visible_according_to_vm is the saved
1401  * visibility status of the heap block looked up earlier by the caller. We
1402  * won't rely entirely on this status, as it may be out of date.
1403  *
1404  * *has_lpdead_items is set to true or false depending on whether, upon return
1405  * from this function, any LP_DEAD items are still present on the page.
1406  */
1407 static void
1409  Buffer buf,
1410  BlockNumber blkno,
1411  Page page,
1412  Buffer vmbuffer,
1413  bool all_visible_according_to_vm,
1414  bool *has_lpdead_items)
1415 {
1416  Relation rel = vacrel->rel;
1417  PruneFreezeResult presult;
1418  int prune_options = 0;
1419 
1420  Assert(BufferGetBlockNumber(buf) == blkno);
1421 
1422  /*
1423  * Prune all HOT-update chains and potentially freeze tuples on this page.
1424  *
1425  * If the relation has no indexes, we can immediately mark would-be dead
1426  * items LP_UNUSED.
1427  *
1428  * The number of tuples removed from the page is returned in
1429  * presult.ndeleted. It should not be confused with presult.lpdead_items;
1430  * presult.lpdead_items's final value can be thought of as the number of
1431  * tuples that were deleted from indexes.
1432  *
1433  * We will update the VM after collecting LP_DEAD items and freezing
1434  * tuples. Pruning will have determined whether or not the page is
1435  * all-visible.
1436  */
1437  prune_options = HEAP_PAGE_PRUNE_FREEZE;
1438  if (vacrel->nindexes == 0)
1439  prune_options |= HEAP_PAGE_PRUNE_MARK_UNUSED_NOW;
1440 
1441  heap_page_prune_and_freeze(rel, buf, vacrel->vistest, prune_options,
1442  &vacrel->cutoffs, &presult, PRUNE_VACUUM_SCAN,
1443  &vacrel->offnum,
1444  &vacrel->NewRelfrozenXid, &vacrel->NewRelminMxid);
1445 
1448 
1449  if (presult.nfrozen > 0)
1450  {
1451  /*
1452  * We don't increment the frozen_pages instrumentation counter when
1453  * nfrozen == 0, since it only counts pages with newly frozen tuples
1454  * (don't confuse that with pages newly set all-frozen in VM).
1455  */
1456  vacrel->frozen_pages++;
1457  }
1458 
1459  /*
1460  * VACUUM will call heap_page_is_all_visible() during the second pass over
1461  * the heap to determine all_visible and all_frozen for the page -- this
1462  * is a specialized version of the logic from this function. Now that
1463  * we've finished pruning and freezing, make sure that we're in total
1464  * agreement with heap_page_is_all_visible() using an assertion.
1465  */
1466 #ifdef USE_ASSERT_CHECKING
1467  /* Note that all_frozen value does not matter when !all_visible */
1468  if (presult.all_visible)
1469  {
1470  TransactionId debug_cutoff;
1471  bool debug_all_frozen;
1472 
1473  Assert(presult.lpdead_items == 0);
1474 
1475  if (!heap_page_is_all_visible(vacrel, buf,
1476  &debug_cutoff, &debug_all_frozen))
1477  Assert(false);
1478 
1479  Assert(presult.all_frozen == debug_all_frozen);
1480 
1481  Assert(!TransactionIdIsValid(debug_cutoff) ||
1482  debug_cutoff == presult.vm_conflict_horizon);
1483  }
1484 #endif
1485 
1486  /*
1487  * Now save details of the LP_DEAD items from the page in vacrel
1488  */
1489  if (presult.lpdead_items > 0)
1490  {
1491  vacrel->lpdead_item_pages++;
1492 
1493  /*
1494  * deadoffsets are collected incrementally in
1495  * heap_page_prune_and_freeze() as each dead line pointer is recorded,
1496  * with an indeterminate order, but dead_items_add requires them to be
1497  * sorted.
1498  */
1499  qsort(presult.deadoffsets, presult.lpdead_items, sizeof(OffsetNumber),
1501 
1502  dead_items_add(vacrel, blkno, presult.deadoffsets, presult.lpdead_items);
1503  }
1504 
1505  /* Finally, add page-local counts to whole-VACUUM counts */
1506  vacrel->tuples_deleted += presult.ndeleted;
1507  vacrel->tuples_frozen += presult.nfrozen;
1508  vacrel->lpdead_items += presult.lpdead_items;
1509  vacrel->live_tuples += presult.live_tuples;
1510  vacrel->recently_dead_tuples += presult.recently_dead_tuples;
1511 
1512  /* Can't truncate this page */
1513  if (presult.hastup)
1514  vacrel->nonempty_pages = blkno + 1;
1515 
1516  /* Did we find LP_DEAD items? */
1517  *has_lpdead_items = (presult.lpdead_items > 0);
1518 
1519  Assert(!presult.all_visible || !(*has_lpdead_items));
1520 
1521  /*
1522  * Handle setting visibility map bit based on information from the VM (as
1523  * of last heap_vac_scan_next_block() call), and from all_visible and
1524  * all_frozen variables
1525  */
1526  if (!all_visible_according_to_vm && presult.all_visible)
1527  {
1529 
1530  if (presult.all_frozen)
1531  {
1533  flags |= VISIBILITYMAP_ALL_FROZEN;
1534  }
1535 
1536  /*
1537  * It should never be the case that the visibility map page is set
1538  * while the page-level bit is clear, but the reverse is allowed (if
1539  * checksums are not enabled). Regardless, set both bits so that we
1540  * get back in sync.
1541  *
1542  * NB: If the heap page is all-visible but the VM bit is not set, we
1543  * don't need to dirty the heap page. However, if checksums are
1544  * enabled, we do need to make sure that the heap page is dirtied
1545  * before passing it to visibilitymap_set(), because it may be logged.
1546  * Given that this situation should only happen in rare cases after a
1547  * crash, it is not worth optimizing.
1548  */
1549  PageSetAllVisible(page);
1551  visibilitymap_set(vacrel->rel, blkno, buf, InvalidXLogRecPtr,
1552  vmbuffer, presult.vm_conflict_horizon,
1553  flags);
1554  }
1555 
1556  /*
1557  * As of PostgreSQL 9.2, the visibility map bit should never be set if the
1558  * page-level bit is clear. However, it's possible that the bit got
1559  * cleared after heap_vac_scan_next_block() was called, so we must recheck
1560  * with buffer lock before concluding that the VM is corrupt.
1561  */
1562  else if (all_visible_according_to_vm && !PageIsAllVisible(page) &&
1563  visibilitymap_get_status(vacrel->rel, blkno, &vmbuffer) != 0)
1564  {
1565  elog(WARNING, "page is not marked all-visible but visibility map bit is set in relation \"%s\" page %u",
1566  vacrel->relname, blkno);
1567  visibilitymap_clear(vacrel->rel, blkno, vmbuffer,
1569  }
1570 
1571  /*
1572  * It's possible for the value returned by
1573  * GetOldestNonRemovableTransactionId() to move backwards, so it's not
1574  * wrong for us to see tuples that appear to not be visible to everyone
1575  * yet, while PD_ALL_VISIBLE is already set. The real safe xmin value
1576  * never moves backwards, but GetOldestNonRemovableTransactionId() is
1577  * conservative and sometimes returns a value that's unnecessarily small,
1578  * so if we see that contradiction it just means that the tuples that we
1579  * think are not visible to everyone yet actually are, and the
1580  * PD_ALL_VISIBLE flag is correct.
1581  *
1582  * There should never be LP_DEAD items on a page with PD_ALL_VISIBLE set,
1583  * however.
1584  */
1585  else if (presult.lpdead_items > 0 && PageIsAllVisible(page))
1586  {
1587  elog(WARNING, "page containing LP_DEAD items is marked as all-visible in relation \"%s\" page %u",
1588  vacrel->relname, blkno);
1589  PageClearAllVisible(page);
1591  visibilitymap_clear(vacrel->rel, blkno, vmbuffer,
1593  }
1594 
1595  /*
1596  * If the all-visible page is all-frozen but not marked as such yet, mark
1597  * it as all-frozen. Note that all_frozen is only valid if all_visible is
1598  * true, so we must check both all_visible and all_frozen.
1599  */
1600  else if (all_visible_according_to_vm && presult.all_visible &&
1601  presult.all_frozen && !VM_ALL_FROZEN(vacrel->rel, blkno, &vmbuffer))
1602  {
1603  /*
1604  * Avoid relying on all_visible_according_to_vm as a proxy for the
1605  * page-level PD_ALL_VISIBLE bit being set, since it might have become
1606  * stale -- even when all_visible is set
1607  */
1608  if (!PageIsAllVisible(page))
1609  {
1610  PageSetAllVisible(page);
1612  }
1613 
1614  /*
1615  * Set the page all-frozen (and all-visible) in the VM.
1616  *
1617  * We can pass InvalidTransactionId as our cutoff_xid, since a
1618  * snapshotConflictHorizon sufficient to make everything safe for REDO
1619  * was logged when the page's tuples were frozen.
1620  */
1622  visibilitymap_set(vacrel->rel, blkno, buf, InvalidXLogRecPtr,
1623  vmbuffer, InvalidTransactionId,
1626  }
1627 }
1628 
1629 /*
1630  * lazy_scan_noprune() -- lazy_scan_prune() without pruning or freezing
1631  *
1632  * Caller need only hold a pin and share lock on the buffer, unlike
1633  * lazy_scan_prune, which requires a full cleanup lock. While pruning isn't
1634  * performed here, it's quite possible that an earlier opportunistic pruning
1635  * operation left LP_DEAD items behind. We'll at least collect any such items
1636  * in dead_items for removal from indexes.
1637  *
1638  * For aggressive VACUUM callers, we may return false to indicate that a full
1639  * cleanup lock is required for processing by lazy_scan_prune. This is only
1640  * necessary when the aggressive VACUUM needs to freeze some tuple XIDs from
1641  * one or more tuples on the page. We always return true for non-aggressive
1642  * callers.
1643  *
1644  * If this function returns true, *has_lpdead_items gets set to true or false
1645  * depending on whether, upon return from this function, any LP_DEAD items are
1646  * present on the page. If this function returns false, *has_lpdead_items
1647  * is not updated.
1648  */
1649 static bool
1651  Buffer buf,
1652  BlockNumber blkno,
1653  Page page,
1654  bool *has_lpdead_items)
1655 {
1656  OffsetNumber offnum,
1657  maxoff;
1658  int lpdead_items,
1659  live_tuples,
1660  recently_dead_tuples,
1661  missed_dead_tuples;
1662  bool hastup;
1663  HeapTupleHeader tupleheader;
1664  TransactionId NoFreezePageRelfrozenXid = vacrel->NewRelfrozenXid;
1665  MultiXactId NoFreezePageRelminMxid = vacrel->NewRelminMxid;
1666  OffsetNumber deadoffsets[MaxHeapTuplesPerPage];
1667 
1668  Assert(BufferGetBlockNumber(buf) == blkno);
1669 
1670  hastup = false; /* for now */
1671 
1672  lpdead_items = 0;
1673  live_tuples = 0;
1674  recently_dead_tuples = 0;
1675  missed_dead_tuples = 0;
1676 
1677  maxoff = PageGetMaxOffsetNumber(page);
1678  for (offnum = FirstOffsetNumber;
1679  offnum <= maxoff;
1680  offnum = OffsetNumberNext(offnum))
1681  {
1682  ItemId itemid;
1683  HeapTupleData tuple;
1684 
1685  vacrel->offnum = offnum;
1686  itemid = PageGetItemId(page, offnum);
1687 
1688  if (!ItemIdIsUsed(itemid))
1689  continue;
1690 
1691  if (ItemIdIsRedirected(itemid))
1692  {
1693  hastup = true;
1694  continue;
1695  }
1696 
1697  if (ItemIdIsDead(itemid))
1698  {
1699  /*
1700  * Deliberately don't set hastup=true here. See same point in
1701  * lazy_scan_prune for an explanation.
1702  */
1703  deadoffsets[lpdead_items++] = offnum;
1704  continue;
1705  }
1706 
1707  hastup = true; /* page prevents rel truncation */
1708  tupleheader = (HeapTupleHeader) PageGetItem(page, itemid);
1709  if (heap_tuple_should_freeze(tupleheader, &vacrel->cutoffs,
1710  &NoFreezePageRelfrozenXid,
1711  &NoFreezePageRelminMxid))
1712  {
1713  /* Tuple with XID < FreezeLimit (or MXID < MultiXactCutoff) */
1714  if (vacrel->aggressive)
1715  {
1716  /*
1717  * Aggressive VACUUMs must always be able to advance rel's
1718  * relfrozenxid to a value >= FreezeLimit (and be able to
1719  * advance rel's relminmxid to a value >= MultiXactCutoff).
1720  * The ongoing aggressive VACUUM won't be able to do that
1721  * unless it can freeze an XID (or MXID) from this tuple now.
1722  *
1723  * The only safe option is to have caller perform processing
1724  * of this page using lazy_scan_prune. Caller might have to
1725  * wait a while for a cleanup lock, but it can't be helped.
1726  */
1727  vacrel->offnum = InvalidOffsetNumber;
1728  return false;
1729  }
1730 
1731  /*
1732  * Non-aggressive VACUUMs are under no obligation to advance
1733  * relfrozenxid (even by one XID). We can be much laxer here.
1734  *
1735  * Currently we always just accept an older final relfrozenxid
1736  * and/or relminmxid value. We never make caller wait or work a
1737  * little harder, even when it likely makes sense to do so.
1738  */
1739  }
1740 
1741  ItemPointerSet(&(tuple.t_self), blkno, offnum);
1742  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
1743  tuple.t_len = ItemIdGetLength(itemid);
1744  tuple.t_tableOid = RelationGetRelid(vacrel->rel);
1745 
1746  switch (HeapTupleSatisfiesVacuum(&tuple, vacrel->cutoffs.OldestXmin,
1747  buf))
1748  {
1750  case HEAPTUPLE_LIVE:
1751 
1752  /*
1753  * Count both cases as live, just like lazy_scan_prune
1754  */
1755  live_tuples++;
1756 
1757  break;
1758  case HEAPTUPLE_DEAD:
1759 
1760  /*
1761  * There is some useful work for pruning to do, that won't be
1762  * done due to failure to get a cleanup lock.
1763  */
1764  missed_dead_tuples++;
1765  break;
1767 
1768  /*
1769  * Count in recently_dead_tuples, just like lazy_scan_prune
1770  */
1771  recently_dead_tuples++;
1772  break;
1774 
1775  /*
1776  * Do not count these rows as live, just like lazy_scan_prune
1777  */
1778  break;
1779  default:
1780  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
1781  break;
1782  }
1783  }
1784 
1785  vacrel->offnum = InvalidOffsetNumber;
1786 
1787  /*
1788  * By here we know for sure that caller can put off freezing and pruning
1789  * this particular page until the next VACUUM. Remember its details now.
1790  * (lazy_scan_prune expects a clean slate, so we have to do this last.)
1791  */
1792  vacrel->NewRelfrozenXid = NoFreezePageRelfrozenXid;
1793  vacrel->NewRelminMxid = NoFreezePageRelminMxid;
1794 
1795  /* Save any LP_DEAD items found on the page in dead_items */
1796  if (vacrel->nindexes == 0)
1797  {
1798  /* Using one-pass strategy (since table has no indexes) */
1799  if (lpdead_items > 0)
1800  {
1801  /*
1802  * Perfunctory handling for the corner case where a single pass
1803  * strategy VACUUM cannot get a cleanup lock, and it turns out
1804  * that there is one or more LP_DEAD items: just count the LP_DEAD
1805  * items as missed_dead_tuples instead. (This is a bit dishonest,
1806  * but it beats having to maintain specialized heap vacuuming code
1807  * forever, for vanishingly little benefit.)
1808  */
1809  hastup = true;
1810  missed_dead_tuples += lpdead_items;
1811  }
1812  }
1813  else if (lpdead_items > 0)
1814  {
1815  /*
1816  * Page has LP_DEAD items, and so any references/TIDs that remain in
1817  * indexes will be deleted during index vacuuming (and then marked
1818  * LP_UNUSED in the heap)
1819  */
1820  vacrel->lpdead_item_pages++;
1821 
1822  dead_items_add(vacrel, blkno, deadoffsets, lpdead_items);
1823 
1824  vacrel->lpdead_items += lpdead_items;
1825  }
1826 
1827  /*
1828  * Finally, add relevant page-local counts to whole-VACUUM counts
1829  */
1830  vacrel->live_tuples += live_tuples;
1831  vacrel->recently_dead_tuples += recently_dead_tuples;
1832  vacrel->missed_dead_tuples += missed_dead_tuples;
1833  if (missed_dead_tuples > 0)
1834  vacrel->missed_dead_pages++;
1835 
1836  /* Can't truncate this page */
1837  if (hastup)
1838  vacrel->nonempty_pages = blkno + 1;
1839 
1840  /* Did we find LP_DEAD items? */
1841  *has_lpdead_items = (lpdead_items > 0);
1842 
1843  /* Caller won't need to call lazy_scan_prune with same page */
1844  return true;
1845 }
1846 
1847 /*
1848  * Main entry point for index vacuuming and heap vacuuming.
1849  *
1850  * Removes items collected in dead_items from table's indexes, then marks the
1851  * same items LP_UNUSED in the heap. See the comments above lazy_scan_heap
1852  * for full details.
1853  *
1854  * Also empties dead_items, freeing up space for later TIDs.
1855  *
1856  * We may choose to bypass index vacuuming at this point, though only when the
1857  * ongoing VACUUM operation will definitely only have one index scan/round of
1858  * index vacuuming.
1859  */
1860 static void
1862 {
1863  bool bypass;
1864 
1865  /* Should not end up here with no indexes */
1866  Assert(vacrel->nindexes > 0);
1867  Assert(vacrel->lpdead_item_pages > 0);
1868 
1869  if (!vacrel->do_index_vacuuming)
1870  {
1871  Assert(!vacrel->do_index_cleanup);
1872  dead_items_reset(vacrel);
1873  return;
1874  }
1875 
1876  /*
1877  * Consider bypassing index vacuuming (and heap vacuuming) entirely.
1878  *
1879  * We currently only do this in cases where the number of LP_DEAD items
1880  * for the entire VACUUM operation is close to zero. This avoids sharp
1881  * discontinuities in the duration and overhead of successive VACUUM
1882  * operations that run against the same table with a fixed workload.
1883  * Ideally, successive VACUUM operations will behave as if there are
1884  * exactly zero LP_DEAD items in cases where there are close to zero.
1885  *
1886  * This is likely to be helpful with a table that is continually affected
1887  * by UPDATEs that can mostly apply the HOT optimization, but occasionally
1888  * have small aberrations that lead to just a few heap pages retaining
1889  * only one or two LP_DEAD items. This is pretty common; even when the
1890  * DBA goes out of their way to make UPDATEs use HOT, it is practically
1891  * impossible to predict whether HOT will be applied in 100% of cases.
1892  * It's far easier to ensure that 99%+ of all UPDATEs against a table use
1893  * HOT through careful tuning.
1894  */
1895  bypass = false;
1896  if (vacrel->consider_bypass_optimization && vacrel->rel_pages > 0)
1897  {
1898  BlockNumber threshold;
1899 
1900  Assert(vacrel->num_index_scans == 0);
1901  Assert(vacrel->lpdead_items == vacrel->dead_items_info->num_items);
1902  Assert(vacrel->do_index_vacuuming);
1903  Assert(vacrel->do_index_cleanup);
1904 
1905  /*
1906  * This crossover point at which we'll start to do index vacuuming is
1907  * expressed as a percentage of the total number of heap pages in the
1908  * table that are known to have at least one LP_DEAD item. This is
1909  * much more important than the total number of LP_DEAD items, since
1910  * it's a proxy for the number of heap pages whose visibility map bits
1911  * cannot be set on account of bypassing index and heap vacuuming.
1912  *
1913  * We apply one further precautionary test: the space currently used
1914  * to store the TIDs (TIDs that now all point to LP_DEAD items) must
1915  * not exceed 32MB. This limits the risk that we will bypass index
1916  * vacuuming again and again until eventually there is a VACUUM whose
1917  * dead_items space is not CPU cache resident.
1918  *
1919  * We don't take any special steps to remember the LP_DEAD items (such
1920  * as counting them in our final update to the stats system) when the
1921  * optimization is applied. Though the accounting used in analyze.c's
1922  * acquire_sample_rows() will recognize the same LP_DEAD items as dead
1923  * rows in its own stats report, that's okay. The discrepancy should
1924  * be negligible. If this optimization is ever expanded to cover more
1925  * cases then this may need to be reconsidered.
1926  */
1927  threshold = (double) vacrel->rel_pages * BYPASS_THRESHOLD_PAGES;
1928  bypass = (vacrel->lpdead_item_pages < threshold &&
1929  (TidStoreMemoryUsage(vacrel->dead_items) < (32L * 1024L * 1024L)));
1930  }
1931 
1932  if (bypass)
1933  {
1934  /*
1935  * There are almost zero TIDs. Behave as if there were precisely
1936  * zero: bypass index vacuuming, but do index cleanup.
1937  *
1938  * We expect that the ongoing VACUUM operation will finish very
1939  * quickly, so there is no point in considering speeding up as a
1940  * failsafe against wraparound failure. (Index cleanup is expected to
1941  * finish very quickly in cases where there were no ambulkdelete()
1942  * calls.)
1943  */
1944  vacrel->do_index_vacuuming = false;
1945  }
1946  else if (lazy_vacuum_all_indexes(vacrel))
1947  {
1948  /*
1949  * We successfully completed a round of index vacuuming. Do related
1950  * heap vacuuming now.
1951  */
1952  lazy_vacuum_heap_rel(vacrel);
1953  }
1954  else
1955  {
1956  /*
1957  * Failsafe case.
1958  *
1959  * We attempted index vacuuming, but didn't finish a full round/full
1960  * index scan. This happens when relfrozenxid or relminmxid is too
1961  * far in the past.
1962  *
1963  * From this point on the VACUUM operation will do no further index
1964  * vacuuming or heap vacuuming. This VACUUM operation won't end up
1965  * back here again.
1966  */
1968  }
1969 
1970  /*
1971  * Forget the LP_DEAD items that we just vacuumed (or just decided to not
1972  * vacuum)
1973  */
1974  dead_items_reset(vacrel);
1975 }
1976 
1977 /*
1978  * lazy_vacuum_all_indexes() -- Main entry for index vacuuming
1979  *
1980  * Returns true in the common case when all indexes were successfully
1981  * vacuumed. Returns false in rare cases where we determined that the ongoing
1982  * VACUUM operation is at risk of taking too long to finish, leading to
1983  * wraparound failure.
1984  */
1985 static bool
1987 {
1988  bool allindexes = true;
1989  double old_live_tuples = vacrel->rel->rd_rel->reltuples;
1990  const int progress_start_index[] = {
1993  };
1994  const int progress_end_index[] = {
1998  };
1999  int64 progress_start_val[2];
2000  int64 progress_end_val[3];
2001 
2002  Assert(vacrel->nindexes > 0);
2003  Assert(vacrel->do_index_vacuuming);
2004  Assert(vacrel->do_index_cleanup);
2005 
2006  /* Precheck for XID wraparound emergencies */
2007  if (lazy_check_wraparound_failsafe(vacrel))
2008  {
2009  /* Wraparound emergency -- don't even start an index scan */
2010  return false;
2011  }
2012 
2013  /*
2014  * Report that we are now vacuuming indexes and the number of indexes to
2015  * vacuum.
2016  */
2017  progress_start_val[0] = PROGRESS_VACUUM_PHASE_VACUUM_INDEX;
2018  progress_start_val[1] = vacrel->nindexes;
2019  pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val);
2020 
2021  if (!ParallelVacuumIsActive(vacrel))
2022  {
2023  for (int idx = 0; idx < vacrel->nindexes; idx++)
2024  {
2025  Relation indrel = vacrel->indrels[idx];
2026  IndexBulkDeleteResult *istat = vacrel->indstats[idx];
2027 
2028  vacrel->indstats[idx] = lazy_vacuum_one_index(indrel, istat,
2029  old_live_tuples,
2030  vacrel);
2031 
2032  /* Report the number of indexes vacuumed */
2034  idx + 1);
2035 
2036  if (lazy_check_wraparound_failsafe(vacrel))
2037  {
2038  /* Wraparound emergency -- end current index scan */
2039  allindexes = false;
2040  break;
2041  }
2042  }
2043  }
2044  else
2045  {
2046  /* Outsource everything to parallel variant */
2047  parallel_vacuum_bulkdel_all_indexes(vacrel->pvs, old_live_tuples,
2048  vacrel->num_index_scans);
2049 
2050  /*
2051  * Do a postcheck to consider applying wraparound failsafe now. Note
2052  * that parallel VACUUM only gets the precheck and this postcheck.
2053  */
2054  if (lazy_check_wraparound_failsafe(vacrel))
2055  allindexes = false;
2056  }
2057 
2058  /*
2059  * We delete all LP_DEAD items from the first heap pass in all indexes on
2060  * each call here (except calls where we choose to do the failsafe). This
2061  * makes the next call to lazy_vacuum_heap_rel() safe (except in the event
2062  * of the failsafe triggering, which prevents the next call from taking
2063  * place).
2064  */
2065  Assert(vacrel->num_index_scans > 0 ||
2066  vacrel->dead_items_info->num_items == vacrel->lpdead_items);
2067  Assert(allindexes || VacuumFailsafeActive);
2068 
2069  /*
2070  * Increase and report the number of index scans. Also, we reset
2071  * PROGRESS_VACUUM_INDEXES_TOTAL and PROGRESS_VACUUM_INDEXES_PROCESSED.
2072  *
2073  * We deliberately include the case where we started a round of bulk
2074  * deletes that we weren't able to finish due to the failsafe triggering.
2075  */
2076  vacrel->num_index_scans++;
2077  progress_end_val[0] = 0;
2078  progress_end_val[1] = 0;
2079  progress_end_val[2] = vacrel->num_index_scans;
2080  pgstat_progress_update_multi_param(3, progress_end_index, progress_end_val);
2081 
2082  return allindexes;
2083 }
2084 
2085 /*
2086  * lazy_vacuum_heap_rel() -- second pass over the heap for two pass strategy
2087  *
2088  * This routine marks LP_DEAD items in vacrel->dead_items as LP_UNUSED. Pages
2089  * that never had lazy_scan_prune record LP_DEAD items are not visited at all.
2090  *
2091  * We may also be able to truncate the line pointer array of the heap pages we
2092  * visit. If there is a contiguous group of LP_UNUSED items at the end of the
2093  * array, it can be reclaimed as free space. These LP_UNUSED items usually
2094  * start out as LP_DEAD items recorded by lazy_scan_prune (we set items from
2095  * each page to LP_UNUSED, and then consider if it's possible to truncate the
2096  * page's line pointer array).
2097  *
2098  * Note: the reason for doing this as a second pass is we cannot remove the
2099  * tuples until we've removed their index entries, and we want to process
2100  * index entry removal in batches as large as possible.
2101  */
2102 static void
2104 {
2105  BlockNumber vacuumed_pages = 0;
2106  Buffer vmbuffer = InvalidBuffer;
2107  LVSavedErrInfo saved_err_info;
2108  TidStoreIter *iter;
2109  TidStoreIterResult *iter_result;
2110 
2111  Assert(vacrel->do_index_vacuuming);
2112  Assert(vacrel->do_index_cleanup);
2113  Assert(vacrel->num_index_scans > 0);
2114 
2115  /* Report that we are now vacuuming the heap */
2118 
2119  /* Update error traceback information */
2120  update_vacuum_error_info(vacrel, &saved_err_info,
2123 
2124  iter = TidStoreBeginIterate(vacrel->dead_items);
2125  while ((iter_result = TidStoreIterateNext(iter)) != NULL)
2126  {
2127  BlockNumber blkno;
2128  Buffer buf;
2129  Page page;
2130  Size freespace;
2131 
2133 
2134  blkno = iter_result->blkno;
2135  vacrel->blkno = blkno;
2136 
2137  /*
2138  * Pin the visibility map page in case we need to mark the page
2139  * all-visible. In most cases this will be very cheap, because we'll
2140  * already have the correct page pinned anyway.
2141  */
2142  visibilitymap_pin(vacrel->rel, blkno, &vmbuffer);
2143 
2144  /* We need a non-cleanup exclusive lock to mark dead_items unused */
2145  buf = ReadBufferExtended(vacrel->rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
2146  vacrel->bstrategy);
2148  lazy_vacuum_heap_page(vacrel, blkno, buf, iter_result->offsets,
2149  iter_result->num_offsets, vmbuffer);
2150 
2151  /* Now that we've vacuumed the page, record its available space */
2152  page = BufferGetPage(buf);
2153  freespace = PageGetHeapFreeSpace(page);
2154 
2156  RecordPageWithFreeSpace(vacrel->rel, blkno, freespace);
2157  vacuumed_pages++;
2158  }
2159  TidStoreEndIterate(iter);
2160 
2161  vacrel->blkno = InvalidBlockNumber;
2162  if (BufferIsValid(vmbuffer))
2163  ReleaseBuffer(vmbuffer);
2164 
2165  /*
2166  * We set all LP_DEAD items from the first heap pass to LP_UNUSED during
2167  * the second heap pass. No more, no less.
2168  */
2169  Assert(vacrel->num_index_scans > 1 ||
2170  (vacrel->dead_items_info->num_items == vacrel->lpdead_items &&
2171  vacuumed_pages == vacrel->lpdead_item_pages));
2172 
2173  ereport(DEBUG2,
2174  (errmsg("table \"%s\": removed %lld dead item identifiers in %u pages",
2175  vacrel->relname, (long long) vacrel->dead_items_info->num_items,
2176  vacuumed_pages)));
2177 
2178  /* Revert to the previous phase information for error traceback */
2179  restore_vacuum_error_info(vacrel, &saved_err_info);
2180 }
2181 
2182 /*
2183  * lazy_vacuum_heap_page() -- free page's LP_DEAD items listed in the
2184  * vacrel->dead_items store.
2185  *
2186  * Caller must have an exclusive buffer lock on the buffer (though a full
2187  * cleanup lock is also acceptable). vmbuffer must be valid and already have
2188  * a pin on blkno's visibility map page.
2189  */
2190 static void
2192  OffsetNumber *deadoffsets, int num_offsets,
2193  Buffer vmbuffer)
2194 {
2195  Page page = BufferGetPage(buffer);
2197  int nunused = 0;
2198  TransactionId visibility_cutoff_xid;
2199  bool all_frozen;
2200  LVSavedErrInfo saved_err_info;
2201 
2202  Assert(vacrel->do_index_vacuuming);
2203 
2205 
2206  /* Update error traceback information */
2207  update_vacuum_error_info(vacrel, &saved_err_info,
2210 
2212 
2213  for (int i = 0; i < num_offsets; i++)
2214  {
2215  ItemId itemid;
2216  OffsetNumber toff = deadoffsets[i];
2217 
2218  itemid = PageGetItemId(page, toff);
2219 
2220  Assert(ItemIdIsDead(itemid) && !ItemIdHasStorage(itemid));
2221  ItemIdSetUnused(itemid);
2222  unused[nunused++] = toff;
2223  }
2224 
2225  Assert(nunused > 0);
2226 
2227  /* Attempt to truncate line pointer array now */
2229 
2230  /*
2231  * Mark buffer dirty before we write WAL.
2232  */
2233  MarkBufferDirty(buffer);
2234 
2235  /* XLOG stuff */
2236  if (RelationNeedsWAL(vacrel->rel))
2237  {
2238  log_heap_prune_and_freeze(vacrel->rel, buffer,
2240  false, /* no cleanup lock required */
2242  NULL, 0, /* frozen */
2243  NULL, 0, /* redirected */
2244  NULL, 0, /* dead */
2245  unused, nunused);
2246  }
2247 
2248  /*
2249  * End critical section, so we safely can do visibility tests (which
2250  * possibly need to perform IO and allocate memory!). If we crash now the
2251  * page (including the corresponding vm bit) might not be marked all
2252  * visible, but that's fine. A later vacuum will fix that.
2253  */
2254  END_CRIT_SECTION();
2255 
2256  /*
2257  * Now that we have removed the LP_DEAD items from the page, once again
2258  * check if the page has become all-visible. The page is already marked
2259  * dirty, exclusively locked, and, if needed, a full page image has been
2260  * emitted.
2261  */
2262  Assert(!PageIsAllVisible(page));
2263  if (heap_page_is_all_visible(vacrel, buffer, &visibility_cutoff_xid,
2264  &all_frozen))
2265  {
2267 
2268  if (all_frozen)
2269  {
2270  Assert(!TransactionIdIsValid(visibility_cutoff_xid));
2271  flags |= VISIBILITYMAP_ALL_FROZEN;
2272  }
2273 
2274  PageSetAllVisible(page);
2275  visibilitymap_set(vacrel->rel, blkno, buffer, InvalidXLogRecPtr,
2276  vmbuffer, visibility_cutoff_xid, flags);
2277  }
2278 
2279  /* Revert to the previous phase information for error traceback */
2280  restore_vacuum_error_info(vacrel, &saved_err_info);
2281 }
2282 
2283 /*
2284  * Trigger the failsafe to avoid wraparound failure when vacrel table has a
2285  * relfrozenxid and/or relminmxid that is dangerously far in the past.
2286  * Triggering the failsafe makes the ongoing VACUUM bypass any further index
2287  * vacuuming and heap vacuuming. Truncating the heap is also bypassed.
2288  *
2289  * Any remaining work (work that VACUUM cannot just bypass) is typically sped
2290  * up when the failsafe triggers. VACUUM stops applying any cost-based delay
2291  * that it started out with.
2292  *
2293  * Returns true when failsafe has been triggered.
2294  */
2295 static bool
2297 {
2298  /* Don't warn more than once per VACUUM */
2300  return true;
2301 
2303  {
2304  const int progress_index[] = {
2307  };
2308  int64 progress_val[2] = {0, 0};
2309 
2310  VacuumFailsafeActive = true;
2311 
2312  /*
2313  * Abandon use of a buffer access strategy to allow use of all of
2314  * shared buffers. We assume the caller who allocated the memory for
2315  * the BufferAccessStrategy will free it.
2316  */
2317  vacrel->bstrategy = NULL;
2318 
2319  /* Disable index vacuuming, index cleanup, and heap rel truncation */
2320  vacrel->do_index_vacuuming = false;
2321  vacrel->do_index_cleanup = false;
2322  vacrel->do_rel_truncate = false;
2323 
2324  /* Reset the progress counters */
2325  pgstat_progress_update_multi_param(2, progress_index, progress_val);
2326 
2327  ereport(WARNING,
2328  (errmsg("bypassing nonessential maintenance of table \"%s.%s.%s\" as a failsafe after %d index scans",
2329  vacrel->dbname, vacrel->relnamespace, vacrel->relname,
2330  vacrel->num_index_scans),
2331  errdetail("The table's relfrozenxid or relminmxid is too far in the past."),
2332  errhint("Consider increasing configuration parameter maintenance_work_mem or autovacuum_work_mem.\n"
2333  "You might also need to consider other ways for VACUUM to keep up with the allocation of transaction IDs.")));
2334 
2335  /* Stop applying cost limits from this point on */
2336  VacuumCostActive = false;
2337  VacuumCostBalance = 0;
2338 
2339  return true;
2340  }
2341 
2342  return false;
2343 }
2344 
2345 /*
2346  * lazy_cleanup_all_indexes() -- cleanup all indexes of relation.
2347  */
2348 static void
2350 {
2351  double reltuples = vacrel->new_rel_tuples;
2352  bool estimated_count = vacrel->scanned_pages < vacrel->rel_pages;
2353  const int progress_start_index[] = {
2356  };
2357  const int progress_end_index[] = {
2360  };
2361  int64 progress_start_val[2];
2362  int64 progress_end_val[2] = {0, 0};
2363 
2364  Assert(vacrel->do_index_cleanup);
2365  Assert(vacrel->nindexes > 0);
2366 
2367  /*
2368  * Report that we are now cleaning up indexes and the number of indexes to
2369  * cleanup.
2370  */
2371  progress_start_val[0] = PROGRESS_VACUUM_PHASE_INDEX_CLEANUP;
2372  progress_start_val[1] = vacrel->nindexes;
2373  pgstat_progress_update_multi_param(2, progress_start_index, progress_start_val);
2374 
2375  if (!ParallelVacuumIsActive(vacrel))
2376  {
2377  for (int idx = 0; idx < vacrel->nindexes; idx++)
2378  {
2379  Relation indrel = vacrel->indrels[idx];
2380  IndexBulkDeleteResult *istat = vacrel->indstats[idx];
2381 
2382  vacrel->indstats[idx] =
2383  lazy_cleanup_one_index(indrel, istat, reltuples,
2384  estimated_count, vacrel);
2385 
2386  /* Report the number of indexes cleaned up */
2388  idx + 1);
2389  }
2390  }
2391  else
2392  {
2393  /* Outsource everything to parallel variant */
2394  parallel_vacuum_cleanup_all_indexes(vacrel->pvs, reltuples,
2395  vacrel->num_index_scans,
2396  estimated_count);
2397  }
2398 
2399  /* Reset the progress counters */
2400  pgstat_progress_update_multi_param(2, progress_end_index, progress_end_val);
2401 }
2402 
2403 /*
2404  * lazy_vacuum_one_index() -- vacuum index relation.
2405  *
2406  * Delete all the index tuples containing a TID collected in
2407  * vacrel->dead_items. Also update running statistics. Exact
2408  * details depend on index AM's ambulkdelete routine.
2409  *
2410  * reltuples is the number of heap tuples to be passed to the
2411  * bulkdelete callback. It's always assumed to be estimated.
2412  * See indexam.sgml for more info.
2413  *
2414  * Returns bulk delete stats derived from input stats
2415  */
2416 static IndexBulkDeleteResult *
2418  double reltuples, LVRelState *vacrel)
2419 {
2420  IndexVacuumInfo ivinfo;
2421  LVSavedErrInfo saved_err_info;
2422 
2423  ivinfo.index = indrel;
2424  ivinfo.heaprel = vacrel->rel;
2425  ivinfo.analyze_only = false;
2426  ivinfo.report_progress = false;
2427  ivinfo.estimated_count = true;
2428  ivinfo.message_level = DEBUG2;
2429  ivinfo.num_heap_tuples = reltuples;
2430  ivinfo.strategy = vacrel->bstrategy;
2431 
2432  /*
2433  * Update error traceback information.
2434  *
2435  * The index name is saved during this phase and restored immediately
2436  * after this phase. See vacuum_error_callback.
2437  */
2438  Assert(vacrel->indname == NULL);
2439  vacrel->indname = pstrdup(RelationGetRelationName(indrel));
2440  update_vacuum_error_info(vacrel, &saved_err_info,
2443 
2444  /* Do bulk deletion */
2445  istat = vac_bulkdel_one_index(&ivinfo, istat, (void *) vacrel->dead_items,
2446  vacrel->dead_items_info);
2447 
2448  /* Revert to the previous phase information for error traceback */
2449  restore_vacuum_error_info(vacrel, &saved_err_info);
2450  pfree(vacrel->indname);
2451  vacrel->indname = NULL;
2452 
2453  return istat;
2454 }
2455 
2456 /*
2457  * lazy_cleanup_one_index() -- do post-vacuum cleanup for index relation.
2458  *
2459  * Calls index AM's amvacuumcleanup routine. reltuples is the number
2460  * of heap tuples and estimated_count is true if reltuples is an
2461  * estimated value. See indexam.sgml for more info.
2462  *
2463  * Returns bulk delete stats derived from input stats
2464  */
2465 static IndexBulkDeleteResult *
2467  double reltuples, bool estimated_count,
2468  LVRelState *vacrel)
2469 {
2470  IndexVacuumInfo ivinfo;
2471  LVSavedErrInfo saved_err_info;
2472 
2473  ivinfo.index = indrel;
2474  ivinfo.heaprel = vacrel->rel;
2475  ivinfo.analyze_only = false;
2476  ivinfo.report_progress = false;
2477  ivinfo.estimated_count = estimated_count;
2478  ivinfo.message_level = DEBUG2;
2479 
2480  ivinfo.num_heap_tuples = reltuples;
2481  ivinfo.strategy = vacrel->bstrategy;
2482 
2483  /*
2484  * Update error traceback information.
2485  *
2486  * The index name is saved during this phase and restored immediately
2487  * after this phase. See vacuum_error_callback.
2488  */
2489  Assert(vacrel->indname == NULL);
2490  vacrel->indname = pstrdup(RelationGetRelationName(indrel));
2491  update_vacuum_error_info(vacrel, &saved_err_info,
2494 
2495  istat = vac_cleanup_one_index(&ivinfo, istat);
2496 
2497  /* Revert to the previous phase information for error traceback */
2498  restore_vacuum_error_info(vacrel, &saved_err_info);
2499  pfree(vacrel->indname);
2500  vacrel->indname = NULL;
2501 
2502  return istat;
2503 }
2504 
2505 /*
2506  * should_attempt_truncation - should we attempt to truncate the heap?
2507  *
2508  * Don't even think about it unless we have a shot at releasing a goodly
2509  * number of pages. Otherwise, the time taken isn't worth it, mainly because
2510  * an AccessExclusive lock must be replayed on any hot standby, where it can
2511  * be particularly disruptive.
2512  *
2513  * Also don't attempt it if wraparound failsafe is in effect. The entire
2514  * system might be refusing to allocate new XIDs at this point. The system
2515  * definitely won't return to normal unless and until VACUUM actually advances
2516  * the oldest relfrozenxid -- which hasn't happened for target rel just yet.
2517  * If lazy_truncate_heap attempted to acquire an AccessExclusiveLock to
2518  * truncate the table under these circumstances, an XID exhaustion error might
2519  * make it impossible for VACUUM to fix the underlying XID exhaustion problem.
2520  * There is very little chance of truncation working out when the failsafe is
2521  * in effect in any case. lazy_scan_prune makes the optimistic assumption
2522  * that any LP_DEAD items it encounters will always be LP_UNUSED by the time
2523  * we're called.
2524  */
2525 static bool
2527 {
2528  BlockNumber possibly_freeable;
2529 
2530  if (!vacrel->do_rel_truncate || VacuumFailsafeActive)
2531  return false;
2532 
2533  possibly_freeable = vacrel->rel_pages - vacrel->nonempty_pages;
2534  if (possibly_freeable > 0 &&
2535  (possibly_freeable >= REL_TRUNCATE_MINIMUM ||
2536  possibly_freeable >= vacrel->rel_pages / REL_TRUNCATE_FRACTION))
2537  return true;
2538 
2539  return false;
2540 }
2541 
2542 /*
2543  * lazy_truncate_heap - try to truncate off any empty pages at the end
2544  */
2545 static void
2547 {
2548  BlockNumber orig_rel_pages = vacrel->rel_pages;
2549  BlockNumber new_rel_pages;
2550  bool lock_waiter_detected;
2551  int lock_retry;
2552 
2553  /* Report that we are now truncating */
2556 
2557  /* Update error traceback information one last time */
2560 
2561  /*
2562  * Loop until no more truncating can be done.
2563  */
2564  do
2565  {
2566  /*
2567  * We need full exclusive lock on the relation in order to do
2568  * truncation. If we can't get it, give up rather than waiting --- we
2569  * don't want to block other backends, and we don't want to deadlock
2570  * (which is quite possible considering we already hold a lower-grade
2571  * lock).
2572  */
2573  lock_waiter_detected = false;
2574  lock_retry = 0;
2575  while (true)
2576  {
2578  break;
2579 
2580  /*
2581  * Check for interrupts while trying to (re-)acquire the exclusive
2582  * lock.
2583  */
2585 
2586  if (++lock_retry > (VACUUM_TRUNCATE_LOCK_TIMEOUT /
2588  {
2589  /*
2590  * We failed to establish the lock in the specified number of
2591  * retries. This means we give up truncating.
2592  */
2593  ereport(vacrel->verbose ? INFO : DEBUG2,
2594  (errmsg("\"%s\": stopping truncate due to conflicting lock request",
2595  vacrel->relname)));
2596  return;
2597  }
2598 
2599  (void) WaitLatch(MyLatch,
2602  WAIT_EVENT_VACUUM_TRUNCATE);
2604  }
2605 
2606  /*
2607  * Now that we have exclusive lock, look to see if the rel has grown
2608  * whilst we were vacuuming with non-exclusive lock. If so, give up;
2609  * the newly added pages presumably contain non-deletable tuples.
2610  */
2611  new_rel_pages = RelationGetNumberOfBlocks(vacrel->rel);
2612  if (new_rel_pages != orig_rel_pages)
2613  {
2614  /*
2615  * Note: we intentionally don't update vacrel->rel_pages with the
2616  * new rel size here. If we did, it would amount to assuming that
2617  * the new pages are empty, which is unlikely. Leaving the numbers
2618  * alone amounts to assuming that the new pages have the same
2619  * tuple density as existing ones, which is less unlikely.
2620  */
2622  return;
2623  }
2624 
2625  /*
2626  * Scan backwards from the end to verify that the end pages actually
2627  * contain no tuples. This is *necessary*, not optional, because
2628  * other backends could have added tuples to these pages whilst we
2629  * were vacuuming.
2630  */
2631  new_rel_pages = count_nondeletable_pages(vacrel, &lock_waiter_detected);
2632  vacrel->blkno = new_rel_pages;
2633 
2634  if (new_rel_pages >= orig_rel_pages)
2635  {
2636  /* can't do anything after all */
2638  return;
2639  }
2640 
2641  /*
2642  * Okay to truncate.
2643  */
2644  RelationTruncate(vacrel->rel, new_rel_pages);
2645 
2646  /*
2647  * We can release the exclusive lock as soon as we have truncated.
2648  * Other backends can't safely access the relation until they have
2649  * processed the smgr invalidation that smgrtruncate sent out ... but
2650  * that should happen as part of standard invalidation processing once
2651  * they acquire lock on the relation.
2652  */
2654 
2655  /*
2656  * Update statistics. Here, it *is* correct to adjust rel_pages
2657  * without also touching reltuples, since the tuple count wasn't
2658  * changed by the truncation.
2659  */
2660  vacrel->removed_pages += orig_rel_pages - new_rel_pages;
2661  vacrel->rel_pages = new_rel_pages;
2662 
2663  ereport(vacrel->verbose ? INFO : DEBUG2,
2664  (errmsg("table \"%s\": truncated %u to %u pages",
2665  vacrel->relname,
2666  orig_rel_pages, new_rel_pages)));
2667  orig_rel_pages = new_rel_pages;
2668  } while (new_rel_pages > vacrel->nonempty_pages && lock_waiter_detected);
2669 }
2670 
2671 /*
2672  * Rescan end pages to verify that they are (still) empty of tuples.
2673  *
2674  * Returns number of nondeletable pages (last nonempty page + 1).
2675  */
2676 static BlockNumber
2677 count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected)
2678 {
2679  BlockNumber blkno;
2680  BlockNumber prefetchedUntil;
2681  instr_time starttime;
2682 
2683  /* Initialize the starttime if we check for conflicting lock requests */
2684  INSTR_TIME_SET_CURRENT(starttime);
2685 
2686  /*
2687  * Start checking blocks at what we believe relation end to be and move
2688  * backwards. (Strange coding of loop control is needed because blkno is
2689  * unsigned.) To make the scan faster, we prefetch a few blocks at a time
2690  * in forward direction, so that OS-level readahead can kick in.
2691  */
2692  blkno = vacrel->rel_pages;
2694  "prefetch size must be power of 2");
2695  prefetchedUntil = InvalidBlockNumber;
2696  while (blkno > vacrel->nonempty_pages)
2697  {
2698  Buffer buf;
2699  Page page;
2700  OffsetNumber offnum,
2701  maxoff;
2702  bool hastup;
2703 
2704  /*
2705  * Check if another process requests a lock on our relation. We are
2706  * holding an AccessExclusiveLock here, so they will be waiting. We
2707  * only do this once per VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL, and we
2708  * only check if that interval has elapsed once every 32 blocks to
2709  * keep the number of system calls and actual shared lock table
2710  * lookups to a minimum.
2711  */
2712  if ((blkno % 32) == 0)
2713  {
2714  instr_time currenttime;
2715  instr_time elapsed;
2716 
2717  INSTR_TIME_SET_CURRENT(currenttime);
2718  elapsed = currenttime;
2719  INSTR_TIME_SUBTRACT(elapsed, starttime);
2720  if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000)
2722  {
2724  {
2725  ereport(vacrel->verbose ? INFO : DEBUG2,
2726  (errmsg("table \"%s\": suspending truncate due to conflicting lock request",
2727  vacrel->relname)));
2728 
2729  *lock_waiter_detected = true;
2730  return blkno;
2731  }
2732  starttime = currenttime;
2733  }
2734  }
2735 
2736  /*
2737  * We don't insert a vacuum delay point here, because we have an
2738  * exclusive lock on the table which we want to hold for as short a
2739  * time as possible. We still need to check for interrupts however.
2740  */
2742 
2743  blkno--;
2744 
2745  /* If we haven't prefetched this lot yet, do so now. */
2746  if (prefetchedUntil > blkno)
2747  {
2748  BlockNumber prefetchStart;
2749  BlockNumber pblkno;
2750 
2751  prefetchStart = blkno & ~(PREFETCH_SIZE - 1);
2752  for (pblkno = prefetchStart; pblkno <= blkno; pblkno++)
2753  {
2754  PrefetchBuffer(vacrel->rel, MAIN_FORKNUM, pblkno);
2756  }
2757  prefetchedUntil = prefetchStart;
2758  }
2759 
2760  buf = ReadBufferExtended(vacrel->rel, MAIN_FORKNUM, blkno, RBM_NORMAL,
2761  vacrel->bstrategy);
2762 
2763  /* In this phase we only need shared access to the buffer */
2765 
2766  page = BufferGetPage(buf);
2767 
2768  if (PageIsNew(page) || PageIsEmpty(page))
2769  {
2771  continue;
2772  }
2773 
2774  hastup = false;
2775  maxoff = PageGetMaxOffsetNumber(page);
2776  for (offnum = FirstOffsetNumber;
2777  offnum <= maxoff;
2778  offnum = OffsetNumberNext(offnum))
2779  {
2780  ItemId itemid;
2781 
2782  itemid = PageGetItemId(page, offnum);
2783 
2784  /*
2785  * Note: any non-unused item should be taken as a reason to keep
2786  * this page. Even an LP_DEAD item makes truncation unsafe, since
2787  * we must not have cleaned out its index entries.
2788  */
2789  if (ItemIdIsUsed(itemid))
2790  {
2791  hastup = true;
2792  break; /* can stop scanning */
2793  }
2794  } /* scan along page */
2795 
2797 
2798  /* Done scanning if we found a tuple here */
2799  if (hastup)
2800  return blkno + 1;
2801  }
2802 
2803  /*
2804  * If we fall out of the loop, all the previously-thought-to-be-empty
2805  * pages still are; we need not bother to look at the last known-nonempty
2806  * page.
2807  */
2808  return vacrel->nonempty_pages;
2809 }
2810 
2811 /*
2812  * Allocate dead_items and dead_items_info (either using palloc, or in dynamic
2813  * shared memory). Sets both in vacrel for caller.
2814  *
2815  * Also handles parallel initialization as part of allocating dead_items in
2816  * DSM when required.
2817  */
2818 static void
2819 dead_items_alloc(LVRelState *vacrel, int nworkers)
2820 {
2821  VacDeadItemsInfo *dead_items_info;
2822  int vac_work_mem = AmAutoVacuumWorkerProcess() &&
2823  autovacuum_work_mem != -1 ?
2825 
2826  /*
2827  * Initialize state for a parallel vacuum. As of now, only one worker can
2828  * be used for an index, so we invoke parallelism only if there are at
2829  * least two indexes on a table.
2830  */
2831  if (nworkers >= 0 && vacrel->nindexes > 1 && vacrel->do_index_vacuuming)
2832  {
2833  /*
2834  * Since parallel workers cannot access data in temporary tables, we
2835  * can't perform parallel vacuum on them.
2836  */
2837  if (RelationUsesLocalBuffers(vacrel->rel))
2838  {
2839  /*
2840  * Give warning only if the user explicitly tries to perform a
2841  * parallel vacuum on the temporary table.
2842  */
2843  if (nworkers > 0)
2844  ereport(WARNING,
2845  (errmsg("disabling parallel option of vacuum on \"%s\" --- cannot vacuum temporary tables in parallel",
2846  vacrel->relname)));
2847  }
2848  else
2849  vacrel->pvs = parallel_vacuum_init(vacrel->rel, vacrel->indrels,
2850  vacrel->nindexes, nworkers,
2851  vac_work_mem,
2852  vacrel->verbose ? INFO : DEBUG2,
2853  vacrel->bstrategy);
2854 
2855  /*
2856  * If parallel mode started, dead_items and dead_items_info spaces are
2857  * allocated in DSM.
2858  */
2859  if (ParallelVacuumIsActive(vacrel))
2860  {
2861  vacrel->dead_items = parallel_vacuum_get_dead_items(vacrel->pvs,
2862  &vacrel->dead_items_info);
2863  return;
2864  }
2865  }
2866 
2867  /*
2868  * Serial VACUUM case. Allocate both dead_items and dead_items_info
2869  * locally.
2870  */
2871 
2872  dead_items_info = (VacDeadItemsInfo *) palloc(sizeof(VacDeadItemsInfo));
2873  dead_items_info->max_bytes = vac_work_mem * 1024L;
2874  dead_items_info->num_items = 0;
2875  vacrel->dead_items_info = dead_items_info;
2876 
2877  vacrel->dead_items = TidStoreCreateLocal(dead_items_info->max_bytes, true);
2878 }
2879 
2880 /*
2881  * Add the given block number and offset numbers to dead_items.
2882  */
2883 static void
2885  int num_offsets)
2886 {
2887  TidStore *dead_items = vacrel->dead_items;
2888 
2889  TidStoreSetBlockOffsets(dead_items, blkno, offsets, num_offsets);
2890  vacrel->dead_items_info->num_items += num_offsets;
2891 
2892  /* update the memory usage report */
2894  TidStoreMemoryUsage(dead_items));
2895 }
2896 
2897 /*
2898  * Forget all collected dead items.
2899  */
2900 static void
2902 {
2903  TidStore *dead_items = vacrel->dead_items;
2904 
2905  if (ParallelVacuumIsActive(vacrel))
2906  {
2908  return;
2909  }
2910 
2911  /* Recreate the tidstore with the same max_bytes limitation */
2912  TidStoreDestroy(dead_items);
2913  vacrel->dead_items = TidStoreCreateLocal(vacrel->dead_items_info->max_bytes, true);
2914 
2915  /* Reset the counter */
2916  vacrel->dead_items_info->num_items = 0;
2917 }
2918 
2919 /*
2920  * Perform cleanup for resources allocated in dead_items_alloc
2921  */
2922 static void
2924 {
2925  if (!ParallelVacuumIsActive(vacrel))
2926  {
2927  /* Don't bother with pfree here */
2928  return;
2929  }
2930 
2931  /* End parallel mode */
2932  parallel_vacuum_end(vacrel->pvs, vacrel->indstats);
2933  vacrel->pvs = NULL;
2934 }
2935 
2936 /*
2937  * Check if every tuple in the given page is visible to all current and future
2938  * transactions. Also return the visibility_cutoff_xid which is the highest
2939  * xmin amongst the visible tuples. Set *all_frozen to true if every tuple
2940  * on this page is frozen.
2941  *
2942  * This is a stripped down version of lazy_scan_prune(). If you change
2943  * anything here, make sure that everything stays in sync. Note that an
2944  * assertion calls us to verify that everybody still agrees. Be sure to avoid
2945  * introducing new side-effects here.
2946  */
2947 static bool
2949  TransactionId *visibility_cutoff_xid,
2950  bool *all_frozen)
2951 {
2952  Page page = BufferGetPage(buf);
2954  OffsetNumber offnum,
2955  maxoff;
2956  bool all_visible = true;
2957 
2958  *visibility_cutoff_xid = InvalidTransactionId;
2959  *all_frozen = true;
2960 
2961  maxoff = PageGetMaxOffsetNumber(page);
2962  for (offnum = FirstOffsetNumber;
2963  offnum <= maxoff && all_visible;
2964  offnum = OffsetNumberNext(offnum))
2965  {
2966  ItemId itemid;
2967  HeapTupleData tuple;
2968 
2969  /*
2970  * Set the offset number so that we can display it along with any
2971  * error that occurred while processing this tuple.
2972  */
2973  vacrel->offnum = offnum;
2974  itemid = PageGetItemId(page, offnum);
2975 
2976  /* Unused or redirect line pointers are of no interest */
2977  if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid))
2978  continue;
2979 
2980  ItemPointerSet(&(tuple.t_self), blockno, offnum);
2981 
2982  /*
2983  * Dead line pointers can have index pointers pointing to them. So
2984  * they can't be treated as visible
2985  */
2986  if (ItemIdIsDead(itemid))
2987  {
2988  all_visible = false;
2989  *all_frozen = false;
2990  break;
2991  }
2992 
2993  Assert(ItemIdIsNormal(itemid));
2994 
2995  tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
2996  tuple.t_len = ItemIdGetLength(itemid);
2997  tuple.t_tableOid = RelationGetRelid(vacrel->rel);
2998 
2999  switch (HeapTupleSatisfiesVacuum(&tuple, vacrel->cutoffs.OldestXmin,
3000  buf))
3001  {
3002  case HEAPTUPLE_LIVE:
3003  {
3004  TransactionId xmin;
3005 
3006  /* Check comments in lazy_scan_prune. */
3008  {
3009  all_visible = false;
3010  *all_frozen = false;
3011  break;
3012  }
3013 
3014  /*
3015  * The inserter definitely committed. But is it old enough
3016  * that everyone sees it as committed?
3017  */
3018  xmin = HeapTupleHeaderGetXmin(tuple.t_data);
3019  if (!TransactionIdPrecedes(xmin,
3020  vacrel->cutoffs.OldestXmin))
3021  {
3022  all_visible = false;
3023  *all_frozen = false;
3024  break;
3025  }
3026 
3027  /* Track newest xmin on page. */
3028  if (TransactionIdFollows(xmin, *visibility_cutoff_xid) &&
3029  TransactionIdIsNormal(xmin))
3030  *visibility_cutoff_xid = xmin;
3031 
3032  /* Check whether this tuple is already frozen or not */
3033  if (all_visible && *all_frozen &&
3035  *all_frozen = false;
3036  }
3037  break;
3038 
3039  case HEAPTUPLE_DEAD:
3043  {
3044  all_visible = false;
3045  *all_frozen = false;
3046  break;
3047  }
3048  default:
3049  elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
3050  break;
3051  }
3052  } /* scan along page */
3053 
3054  /* Clear the offset information once we have processed the given page. */
3055  vacrel->offnum = InvalidOffsetNumber;
3056 
3057  return all_visible;
3058 }
3059 
3060 /*
3061  * Update index statistics in pg_class if the statistics are accurate.
3062  */
3063 static void
3065 {
3066  Relation *indrels = vacrel->indrels;
3067  int nindexes = vacrel->nindexes;
3068  IndexBulkDeleteResult **indstats = vacrel->indstats;
3069 
3070  Assert(vacrel->do_index_cleanup);
3071 
3072  for (int idx = 0; idx < nindexes; idx++)
3073  {
3074  Relation indrel = indrels[idx];
3075  IndexBulkDeleteResult *istat = indstats[idx];
3076 
3077  if (istat == NULL || istat->estimated_count)
3078  continue;
3079 
3080  /* Update index statistics */
3081  vac_update_relstats(indrel,
3082  istat->num_pages,
3083  istat->num_index_tuples,
3084  0,
3085  false,
3088  NULL, NULL, false);
3089  }
3090 }
3091 
3092 /*
3093  * Error context callback for errors occurring during vacuum. The error
3094  * context messages for index phases should match the messages set in parallel
3095  * vacuum. If you change this function for those phases, change
3096  * parallel_vacuum_error_callback() as well.
3097  */
3098 static void
3100 {
3101  LVRelState *errinfo = arg;
3102 
3103  switch (errinfo->phase)
3104  {
3106  if (BlockNumberIsValid(errinfo->blkno))
3107  {
3108  if (OffsetNumberIsValid(errinfo->offnum))
3109  errcontext("while scanning block %u offset %u of relation \"%s.%s\"",
3110  errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname);
3111  else
3112  errcontext("while scanning block %u of relation \"%s.%s\"",
3113  errinfo->blkno, errinfo->relnamespace, errinfo->relname);
3114  }
3115  else
3116  errcontext("while scanning relation \"%s.%s\"",
3117  errinfo->relnamespace, errinfo->relname);
3118  break;
3119 
3121  if (BlockNumberIsValid(errinfo->blkno))
3122  {
3123  if (OffsetNumberIsValid(errinfo->offnum))
3124  errcontext("while vacuuming block %u offset %u of relation \"%s.%s\"",
3125  errinfo->blkno, errinfo->offnum, errinfo->relnamespace, errinfo->relname);
3126  else
3127  errcontext("while vacuuming block %u of relation \"%s.%s\"",
3128  errinfo->blkno, errinfo->relnamespace, errinfo->relname);
3129  }
3130  else
3131  errcontext("while vacuuming relation \"%s.%s\"",
3132  errinfo->relnamespace, errinfo->relname);
3133  break;
3134 
3136  errcontext("while vacuuming index \"%s\" of relation \"%s.%s\"",
3137  errinfo->indname, errinfo->relnamespace, errinfo->relname);
3138  break;
3139 
3141  errcontext("while cleaning up index \"%s\" of relation \"%s.%s\"",
3142  errinfo->indname, errinfo->relnamespace, errinfo->relname);
3143  break;
3144 
3146  if (BlockNumberIsValid(errinfo->blkno))
3147  errcontext("while truncating relation \"%s.%s\" to %u blocks",
3148  errinfo->relnamespace, errinfo->relname, errinfo->blkno);
3149  break;
3150 
3152  default:
3153  return; /* do nothing; the errinfo may not be
3154  * initialized */
3155  }
3156 }
3157 
3158 /*
3159  * Updates the information required for vacuum error callback. This also saves
3160  * the current information which can be later restored via restore_vacuum_error_info.
3161  */
3162 static void
3164  int phase, BlockNumber blkno, OffsetNumber offnum)
3165 {
3166  if (saved_vacrel)
3167  {
3168  saved_vacrel->offnum = vacrel->offnum;
3169  saved_vacrel->blkno = vacrel->blkno;
3170  saved_vacrel->phase = vacrel->phase;
3171  }
3172 
3173  vacrel->blkno = blkno;
3174  vacrel->offnum = offnum;
3175  vacrel->phase = phase;
3176 }
3177 
3178 /*
3179  * Restores the vacuum information saved via a prior call to update_vacuum_error_info.
3180  */
3181 static void
3183  const LVSavedErrInfo *saved_vacrel)
3184 {
3185  vacrel->blkno = saved_vacrel->blkno;
3186  vacrel->offnum = saved_vacrel->offnum;
3187  vacrel->phase = saved_vacrel->phase;
3188 }
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
int autovacuum_work_mem
Definition: autovacuum.c:118
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1730
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1790
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
void pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
void pgstat_progress_update_param(int index, int64 val)
void pgstat_progress_update_multi_param(int nparam, const int *index, const int64 *val)
void pgstat_progress_end_command(void)
@ PROGRESS_COMMAND_VACUUM
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
bool track_io_timing
Definition: bufmgr.c:142
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:3667
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition: bufmgr.c:638
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4850
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4867
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2474
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:5165
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5085
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:792
bool ConditionalLockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:5326
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:197
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:198
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:281
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:408
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:199
@ RBM_NORMAL
Definition: bufmgr.h:45
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:359
Size PageGetHeapFreeSpace(Page page)
Definition: bufpage.c:991
void PageTruncateLinePointerArray(Page page)
Definition: bufpage.c:835
static bool PageIsEmpty(Page page)
Definition: bufpage.h:220
Pointer Page
Definition: bufpage.h:78
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:351
static void PageClearAllVisible(Page page)
Definition: bufpage.h:436
#define SizeOfPageHeaderData
Definition: bufpage.h:213
static void PageSetAllVisible(Page page)
Definition: bufpage.h:431
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:240
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static bool PageIsAllVisible(Page page)
Definition: bufpage.h:426
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:369
signed int int32
Definition: c.h:494
#define Max(x, y)
Definition: c.h:998
#define Assert(condition)
Definition: c.h:858
TransactionId MultiXactId
Definition: c.h:662
#define unlikely(x)
Definition: c.h:311
unsigned char uint8
Definition: c.h:504
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:938
uint32 TransactionId
Definition: c.h:652
size_t Size
Definition: c.h:605
int64 TimestampTz
Definition: timestamp.h:39
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3153
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errdetail(const char *fmt,...)
Definition: elog.c:1205
ErrorContextCallback * error_context_stack
Definition: elog.c:94
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define _(x)
Definition: elog.c:90
#define LOG
Definition: elog.h:31
#define errcontext
Definition: elog.h:196
#define WARNING
Definition: elog.h:36
#define DEBUG2
Definition: elog.h:29
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define INFO
Definition: elog.h:34
#define ereport(elevel,...)
Definition: elog.h:149
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:377
Size GetRecordedFreeSpace(Relation rel, BlockNumber heapBlk)
Definition: freespace.c:244
void RecordPageWithFreeSpace(Relation rel, BlockNumber heapBlk, Size spaceAvail)
Definition: freespace.c:194
int64 VacuumPageHit
Definition: globals.c:154
int64 VacuumPageMiss
Definition: globals.c:155
bool VacuumCostActive
Definition: globals.c:159
int64 VacuumPageDirty
Definition: globals.c:156
int VacuumCostBalance
Definition: globals.c:158
int maintenance_work_mem
Definition: globals.c:130
struct Latch * MyLatch
Definition: globals.c:60
Oid MyDatabaseId
Definition: globals.c:91
bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple)
Definition: heapam.c:7319
bool heap_tuple_should_freeze(HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, TransactionId *NoFreezePageRelfrozenXid, MultiXactId *NoFreezePageRelminMxid)
Definition: heapam.c:7374
#define HEAP_PAGE_PRUNE_FREEZE
Definition: heapam.h:42
@ HEAPTUPLE_RECENTLY_DEAD
Definition: heapam.h:127
@ HEAPTUPLE_INSERT_IN_PROGRESS
Definition: heapam.h:128
@ HEAPTUPLE_LIVE
Definition: heapam.h:126
@ HEAPTUPLE_DELETE_IN_PROGRESS
Definition: heapam.h:129
@ HEAPTUPLE_DEAD
Definition: heapam.h:125
@ PRUNE_VACUUM_CLEANUP
Definition: heapam.h:271
@ PRUNE_VACUUM_SCAN
Definition: heapam.h:270
#define HEAP_PAGE_PRUNE_MARK_UNUSED_NOW
Definition: heapam.h:41
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:309
#define HeapTupleHeaderXminCommitted(tup)
Definition: htup_details.h:320
#define MaxHeapTuplesPerPage
Definition: htup_details.h:572
int verbose
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MICROSEC(t)
Definition: instr_time.h:194
WalUsage pgWalUsage
Definition: instrument.c:22
void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
Definition: instrument.c:286
static int pg_cmp_u16(uint16 a, uint16 b)
Definition: int.h:477
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
#define ItemIdIsDead(itemId)
Definition: itemid.h:113
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
#define ItemIdSetUnused(itemId)
Definition: itemid.h:128
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:106
#define ItemIdHasStorage(itemId)
Definition: itemid.h:120
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
void UnlockRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:310
bool ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:275
bool LockHasWaitersRelation(Relation relation, LOCKMODE lockmode)
Definition: lmgr.c:373
#define NoLock
Definition: lockdefs.h:34
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc0(Size size)
Definition: mcxt.c:1346
void * palloc(Size size)
Definition: mcxt.c:1316
#define AmAutoVacuumWorkerProcess()
Definition: miscadmin.h:375
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3274
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define InvalidMultiXactId
Definition: multixact.h:24
#define InvalidOffsetNumber
Definition: off.h:26
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
void * arg
const char * pg_rusage_show(const PGRUsage *ru0)
Definition: pg_rusage.c:40
void pg_rusage_init(PGRUsage *ru0)
Definition: pg_rusage.c:27
static char * buf
Definition: pg_test_fsync.c:73
int64 PgStat_Counter
Definition: pgstat.h:89
PgStat_Counter pgStatBlockReadTime
PgStat_Counter pgStatBlockWriteTime
void pgstat_report_vacuum(Oid tableoid, bool shared, PgStat_Counter livetuples, PgStat_Counter deadtuples)
#define qsort(a, b, c, d)
Definition: port.h:449
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition: procarray.c:4091
#define PROGRESS_VACUUM_PHASE_FINAL_CLEANUP
Definition: progress.h:37
#define PROGRESS_VACUUM_DEAD_TUPLE_BYTES
Definition: progress.h:27
#define PROGRESS_VACUUM_PHASE_SCAN_HEAP
Definition: progress.h:32
#define PROGRESS_VACUUM_TOTAL_HEAP_BLKS
Definition: progress.h:22
#define PROGRESS_VACUUM_PHASE
Definition: progress.h:21
#define PROGRESS_VACUUM_NUM_INDEX_VACUUMS
Definition: progress.h:25
#define PROGRESS_VACUUM_PHASE_VACUUM_HEAP
Definition: progress.h:34
#define PROGRESS_VACUUM_MAX_DEAD_TUPLE_BYTES
Definition: progress.h:26
#define PROGRESS_VACUUM_HEAP_BLKS_SCANNED
Definition: progress.h:23
#define PROGRESS_VACUUM_PHASE_INDEX_CLEANUP
Definition: progress.h:35
#define PROGRESS_VACUUM_PHASE_VACUUM_INDEX
Definition: progress.h:33
#define PROGRESS_VACUUM_INDEXES_PROCESSED
Definition: progress.h:29
#define PROGRESS_VACUUM_INDEXES_TOTAL
Definition: progress.h:28
#define PROGRESS_VACUUM_HEAP_BLKS_VACUUMED
Definition: progress.h:24
#define PROGRESS_VACUUM_PHASE_TRUNCATE
Definition: progress.h:36
void heap_page_prune_and_freeze(Relation relation, Buffer buffer, GlobalVisState *vistest, int options, struct VacuumCutoffs *cutoffs, PruneFreezeResult *presult, PruneReason reason, OffsetNumber *off_loc, TransactionId *new_relfrozen_xid, MultiXactId *new_relmin_mxid)
Definition: pruneheap.c:348
void log_heap_prune_and_freeze(Relation relation, Buffer buffer, TransactionId conflict_xid, bool cleanup_lock, PruneReason reason, HeapTupleFreeze *frozen, int nfrozen, OffsetNumber *redirected, int nredirected, OffsetNumber *dead, int ndead, OffsetNumber *unused, int nunused)
Definition: pruneheap.c:2032
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RelationNeedsWAL(relation)
Definition: rel.h:628
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:637
#define RelationGetNamespace(relation)
Definition: rel.h:546
@ MAIN_FORKNUM
Definition: relpath.h:50
void RelationTruncate(Relation rel, BlockNumber nblocks)
Definition: storage.c:288
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
bool estimated_count
Definition: genam.h:78
BlockNumber pages_deleted
Definition: genam.h:82
BlockNumber pages_newly_deleted
Definition: genam.h:81
BlockNumber pages_free
Definition: genam.h:83
BlockNumber num_pages
Definition: genam.h:77
double num_index_tuples
Definition: genam.h:79
Relation index
Definition: genam.h:46
double num_heap_tuples
Definition: genam.h:52
bool analyze_only
Definition: genam.h:48
BufferAccessStrategy strategy
Definition: genam.h:53
Relation heaprel
Definition: genam.h:47
bool report_progress
Definition: genam.h:49
int message_level
Definition: genam.h:51
bool estimated_count
Definition: genam.h:50
ParallelVacuumState * pvs
Definition: vacuumlazy.c:145
bool verbose
Definition: vacuumlazy.c:175
VacDeadItemsInfo * dead_items_info
Definition: vacuumlazy.c:188
int nindexes
Definition: vacuumlazy.c:141
Buffer next_unskippable_vmbuffer
Definition: vacuumlazy.c:218
OffsetNumber offnum
Definition: vacuumlazy.c:173
TidStore * dead_items
Definition: vacuumlazy.c:187
int64 tuples_deleted
Definition: vacuumlazy.c:207
BlockNumber nonempty_pages
Definition: vacuumlazy.c:196
bool do_rel_truncate
Definition: vacuumlazy.c:157
BlockNumber scanned_pages
Definition: vacuumlazy.c:191
bool aggressive
Definition: vacuumlazy.c:148
GlobalVisState * vistest
Definition: vacuumlazy.c:161
BlockNumber removed_pages
Definition: vacuumlazy.c:192
int num_index_scans
Definition: vacuumlazy.c:205
IndexBulkDeleteResult ** indstats
Definition: vacuumlazy.c:202
double new_live_tuples
Definition: vacuumlazy.c:200
double new_rel_tuples
Definition: vacuumlazy.c:199
TransactionId NewRelfrozenXid
Definition: vacuumlazy.c:163
Relation rel
Definition: vacuumlazy.c:139
bool consider_bypass_optimization
Definition: vacuumlazy.c:152
BlockNumber rel_pages
Definition: vacuumlazy.c:190
BlockNumber next_unskippable_block
Definition: vacuumlazy.c:216
int64 recently_dead_tuples
Definition: vacuumlazy.c:211
int64 tuples_frozen
Definition: vacuumlazy.c:208
BlockNumber frozen_pages
Definition: vacuumlazy.c:193
char * dbname
Definition: vacuumlazy.c:168
BlockNumber missed_dead_pages
Definition: vacuumlazy.c:195
BlockNumber current_block
Definition: vacuumlazy.c:215
char * relnamespace
Definition: vacuumlazy.c:169
int64 live_tuples
Definition: vacuumlazy.c:210
int64 lpdead_items
Definition: vacuumlazy.c:209
BufferAccessStrategy bstrategy
Definition: vacuumlazy.c:144
bool skippedallvis
Definition: vacuumlazy.c:165
BlockNumber lpdead_item_pages
Definition: vacuumlazy.c:194
Relation * indrels
Definition: vacuumlazy.c:140
bool skipwithvm
Definition: vacuumlazy.c:150
bool do_index_cleanup
Definition: vacuumlazy.c:156
MultiXactId NewRelminMxid
Definition: vacuumlazy.c:164
int64 missed_dead_tuples
Definition: vacuumlazy.c:212
BlockNumber blkno
Definition: vacuumlazy.c:172
struct VacuumCutoffs cutoffs
Definition: vacuumlazy.c:160
bool next_unskippable_allvis
Definition: vacuumlazy.c:217
char * relname
Definition: vacuumlazy.c:170
VacErrPhase phase
Definition: vacuumlazy.c:174
char * indname
Definition: vacuumlazy.c:171
bool do_index_vacuuming
Definition: vacuumlazy.c:155
BlockNumber blkno
Definition: vacuumlazy.c:224
VacErrPhase phase
Definition: vacuumlazy.c:226
OffsetNumber offnum
Definition: vacuumlazy.c:225
int recently_dead_tuples
Definition: heapam.h:234
TransactionId vm_conflict_horizon
Definition: heapam.h:249
OffsetNumber deadoffsets[MaxHeapTuplesPerPage]
Definition: heapam.h:263
bool all_visible
Definition: heapam.h:247
Form_pg_class rd_rel
Definition: rel.h:111
BlockNumber blkno
Definition: tidstore.h:26
OffsetNumber * offsets
Definition: tidstore.h:29
size_t max_bytes
Definition: vacuum.h:287
int64 num_items
Definition: vacuum.h:288
TransactionId FreezeLimit
Definition: vacuum.h:277
TransactionId OldestXmin
Definition: vacuum.h:267
TransactionId relfrozenxid
Definition: vacuum.h:251
MultiXactId relminmxid
Definition: vacuum.h:252
MultiXactId MultiXactCutoff
Definition: vacuum.h:278
MultiXactId OldestMxact
Definition: vacuum.h:268
int nworkers
Definition: vacuum.h:239
VacOptValue truncate
Definition: vacuum.h:231
bits32 options
Definition: vacuum.h:219
bool is_wraparound
Definition: vacuum.h:226
int log_min_duration
Definition: vacuum.h:227
VacOptValue index_cleanup
Definition: vacuum.h:230
uint64 wal_bytes
Definition: instrument.h:55
int64 wal_fpi
Definition: instrument.h:54
int64 wal_records
Definition: instrument.h:53
void TidStoreEndIterate(TidStoreIter *iter)
Definition: tidstore.c:536
TidStoreIterResult * TidStoreIterateNext(TidStoreIter *iter)
Definition: tidstore.c:511
void TidStoreDestroy(TidStore *ts)
Definition: tidstore.c:328
TidStore * TidStoreCreateLocal(size_t max_bytes, bool insert_only)
Definition: tidstore.c:165
TidStoreIter * TidStoreBeginIterate(TidStore *ts)
Definition: tidstore.c:482
void TidStoreSetBlockOffsets(TidStore *ts, BlockNumber blkno, OffsetNumber *offsets, int num_offsets)
Definition: tidstore.c:356
size_t TidStoreMemoryUsage(TidStore *ts)
Definition: tidstore.c:551
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:314
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:315
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
IndexBulkDeleteResult * vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat, TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
Definition: vacuum.c:2490
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:2272
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool *frozenxid_updated, bool *minmulti_updated, bool in_outer_xact)
Definition: vacuum.c:1398
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:2315
void vacuum_delay_point(void)
Definition: vacuum.c:2336
bool vacuum_get_cutoffs(Relation rel, const VacuumParams *params, struct VacuumCutoffs *cutoffs)
Definition: vacuum.c:1072
bool vacuum_xid_failsafe_check(const struct VacuumCutoffs *cutoffs)
Definition: vacuum.c:1240
bool VacuumFailsafeActive
Definition: vacuum.c:96
double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:1302
IndexBulkDeleteResult * vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
Definition: vacuum.c:2511
#define VACOPT_VERBOSE
Definition: vacuum.h:182
@ VACOPTVALUE_AUTO
Definition: vacuum.h:203
@ VACOPTVALUE_ENABLED
Definition: vacuum.h:205
@ VACOPTVALUE_UNSPECIFIED
Definition: vacuum.h:202
@ VACOPTVALUE_DISABLED
Definition: vacuum.h:204
#define VACOPT_DISABLE_PAGE_SKIPPING
Definition: vacuum.h:188
static void dead_items_cleanup(LVRelState *vacrel)
Definition: vacuumlazy.c:2923
static bool heap_page_is_all_visible(LVRelState *vacrel, Buffer buf, TransactionId *visibility_cutoff_xid, bool *all_frozen)
Definition: vacuumlazy.c:2948
static void update_relstats_all_indexes(LVRelState *vacrel)
Definition: vacuumlazy.c:3064
static void dead_items_add(LVRelState *vacrel, BlockNumber blkno, OffsetNumber *offsets, int num_offsets)
Definition: vacuumlazy.c:2884
#define VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL
Definition: vacuumlazy.c:82
static void vacuum_error_callback(void *arg)
Definition: vacuumlazy.c:3099
static void lazy_vacuum_heap_page(LVRelState *vacrel, BlockNumber blkno, Buffer buffer, OffsetNumber *offsets, int num_offsets, Buffer vmbuffer)
Definition: vacuumlazy.c:2191
static void lazy_truncate_heap(LVRelState *vacrel)
Definition: vacuumlazy.c:2546
static void lazy_vacuum(LVRelState *vacrel)
Definition: vacuumlazy.c:1861
static void lazy_cleanup_all_indexes(LVRelState *vacrel)
Definition: vacuumlazy.c:2349
static bool lazy_scan_noprune(LVRelState *vacrel, Buffer buf, BlockNumber blkno, Page page, bool *has_lpdead_items)
Definition: vacuumlazy.c:1650
#define REL_TRUNCATE_MINIMUM
Definition: vacuumlazy.c:71
static bool should_attempt_truncation(LVRelState *vacrel)
Definition: vacuumlazy.c:2526
static bool lazy_scan_new_or_empty(LVRelState *vacrel, Buffer buf, BlockNumber blkno, Page page, bool sharelock, Buffer vmbuffer)
Definition: vacuumlazy.c:1285
VacErrPhase
Definition: vacuumlazy.c:127
@ VACUUM_ERRCB_PHASE_SCAN_HEAP
Definition: vacuumlazy.c:129
@ VACUUM_ERRCB_PHASE_VACUUM_INDEX
Definition: vacuumlazy.c:130
@ VACUUM_ERRCB_PHASE_TRUNCATE
Definition: vacuumlazy.c:133
@ VACUUM_ERRCB_PHASE_INDEX_CLEANUP
Definition: vacuumlazy.c:132
@ VACUUM_ERRCB_PHASE_VACUUM_HEAP
Definition: vacuumlazy.c:131
@ VACUUM_ERRCB_PHASE_UNKNOWN
Definition: vacuumlazy.c:128
static void lazy_scan_heap(LVRelState *vacrel)
Definition: vacuumlazy.c:818
#define ParallelVacuumIsActive(vacrel)
Definition: vacuumlazy.c:123
static void restore_vacuum_error_info(LVRelState *vacrel, const LVSavedErrInfo *saved_vacrel)
Definition: vacuumlazy.c:3182
static bool heap_vac_scan_next_block(LVRelState *vacrel, BlockNumber *blkno, bool *all_visible_according_to_vm)
Definition: vacuumlazy.c:1088
static void lazy_scan_prune(LVRelState *vacrel, Buffer buf, BlockNumber blkno, Page page, Buffer vmbuffer, bool all_visible_according_to_vm, bool *has_lpdead_items)
Definition: vacuumlazy.c:1408
void heap_vacuum_rel(Relation rel, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuumlazy.c:295
static IndexBulkDeleteResult * lazy_vacuum_one_index(Relation indrel, IndexBulkDeleteResult *istat, double reltuples, LVRelState *vacrel)
Definition: vacuumlazy.c:2417
static void find_next_unskippable_block(LVRelState *vacrel, bool *skipsallvis)
Definition: vacuumlazy.c:1186
static void dead_items_reset(LVRelState *vacrel)
Definition: vacuumlazy.c:2901
#define REL_TRUNCATE_FRACTION
Definition: vacuumlazy.c:72
static bool lazy_check_wraparound_failsafe(LVRelState *vacrel)
Definition: vacuumlazy.c:2296
struct LVSavedErrInfo LVSavedErrInfo
static IndexBulkDeleteResult * lazy_cleanup_one_index(Relation indrel, IndexBulkDeleteResult *istat, double reltuples, bool estimated_count, LVRelState *vacrel)
Definition: vacuumlazy.c:2466
#define PREFETCH_SIZE
Definition: vacuumlazy.c:117
struct LVRelState LVRelState
#define BYPASS_THRESHOLD_PAGES
Definition: vacuumlazy.c:89
static void dead_items_alloc(LVRelState *vacrel, int nworkers)
Definition: vacuumlazy.c:2819
#define VACUUM_TRUNCATE_LOCK_TIMEOUT
Definition: vacuumlazy.c:83
static bool lazy_vacuum_all_indexes(LVRelState *vacrel)
Definition: vacuumlazy.c:1986
static void update_vacuum_error_info(LVRelState *vacrel, LVSavedErrInfo *saved_vacrel, int phase, BlockNumber blkno, OffsetNumber offnum)
Definition: vacuumlazy.c:3163
static BlockNumber count_nondeletable_pages(LVRelState *vacrel, bool *lock_waiter_detected)
Definition: vacuumlazy.c:2677
#define SKIP_PAGES_THRESHOLD
Definition: vacuumlazy.c:111
#define FAILSAFE_EVERY_PAGES
Definition: vacuumlazy.c:95
#define VACUUM_TRUNCATE_LOCK_CHECK_INTERVAL
Definition: vacuumlazy.c:81
static int cmpOffsetNumbers(const void *a, const void *b)
Definition: vacuumlazy.c:1389
static void lazy_vacuum_heap_rel(LVRelState *vacrel)
Definition: vacuumlazy.c:2103
#define VACUUM_FSM_EVERY_PAGES
Definition: vacuumlazy.c:104
ParallelVacuumState * parallel_vacuum_init(Relation rel, Relation *indrels, int nindexes, int nrequested_workers, int vac_work_mem, int elevel, BufferAccessStrategy bstrategy)
void parallel_vacuum_bulkdel_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans)
void parallel_vacuum_reset_dead_items(ParallelVacuumState *pvs)
void parallel_vacuum_cleanup_all_indexes(ParallelVacuumState *pvs, long num_table_tuples, int num_index_scans, bool estimated_count)
TidStore * parallel_vacuum_get_dead_items(ParallelVacuumState *pvs, VacDeadItemsInfo **dead_items_info_p)
void parallel_vacuum_end(ParallelVacuumState *pvs, IndexBulkDeleteResult **istats)
void visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
bool visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
uint8 visibilitymap_get_status(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
void visibilitymap_count(Relation rel, BlockNumber *all_visible, BlockNumber *all_frozen)
#define VM_ALL_FROZEN(r, b, v)
Definition: visibilitymap.h:26
#define VISIBILITYMAP_VALID_BITS
#define VISIBILITYMAP_ALL_FROZEN
#define VISIBILITYMAP_ALL_VISIBLE
bool IsInParallelMode(void)
Definition: xact.c:1086
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1237