LCOV - code coverage report
Current view: top level - src/backend/access/heap - vacuumlazy.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 853 989 86.2 %
Date: 2025-12-13 07:18:15 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.16