LCOV - differential code coverage report
Current view: top level - src/backend/access/heap - pruneheap.c (source / functions) Coverage Total Hit UNC UBC CBC DUB
Current: 1a1248818fb8b8b0353c292c632d6a6af77e8209 vs 8e82d6610e7280d7870c0c36ff1c797e3cfb3e09 Lines: 91.3 % 745 680 1 64 680 1
Current Date: 2026-10-12 11:56:10 +0200 Functions: 96.4 % 28 27 1 27
Baseline: lcov-20261012-baseline Branches: 73.9 % 675 499 176 499
Baseline Date: 2026-10-12 10:36:06 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 9 9 9
(30,360] days: 86.3 % 300 259 1 40 259
(360..) days: 94.5 % 436 412 24 412
Function coverage date bins:
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 100.0 % 14 14 14
Branch coverage date bins:
(7,30] days: 83.3 % 18 15 3 15
(30,360] days: 74.1 % 278 206 72 206
(360..) days: 73.4 % 379 278 101 278

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pruneheap.c
                                  4                 :                :  *    heap page pruning and HOT-chain management code
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/access/heap/pruneheap.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/heapam.h"
                                 18                 :                : #include "access/heapam_xlog.h"
                                 19                 :                : #include "access/htup_details.h"
                                 20                 :                : #include "access/multixact.h"
                                 21                 :                : #include "access/transam.h"
                                 22                 :                : #include "access/visibilitymap.h"
                                 23                 :                : #include "access/xlog.h"
                                 24                 :                : #include "access/xloginsert.h"
                                 25                 :                : #include "commands/vacuum.h"
                                 26                 :                : #include "executor/instrument.h"
                                 27                 :                : #include "miscadmin.h"
                                 28                 :                : #include "pgstat.h"
                                 29                 :                : #include "storage/bufmgr.h"
                                 30                 :                : #include "storage/freespace.h"
                                 31                 :                : #include "utils/rel.h"
                                 32                 :                : #include "utils/snapmgr.h"
                                 33                 :                : 
                                 34                 :                : /* Working data for heap_page_prune_and_freeze() and subroutines */
                                 35                 :                : typedef struct
                                 36                 :                : {
                                 37                 :                :     /*-------------------------------------------------------
                                 38                 :                :      * Arguments passed to heap_page_prune_and_freeze()
                                 39                 :                :      *-------------------------------------------------------
                                 40                 :                :      */
                                 41                 :                : 
                                 42                 :                :     /* tuple visibility test, initialized for the relation */
                                 43                 :                :     GlobalVisState *vistest;
                                 44                 :                :     /* whether or not dead items can be set LP_UNUSED during pruning */
                                 45                 :                :     bool        mark_unused_now;
                                 46                 :                :     /* whether to attempt freezing tuples */
                                 47                 :                :     bool        attempt_freeze;
                                 48                 :                :     /* whether to attempt setting the VM */
                                 49                 :                :     bool        attempt_set_vm;
                                 50                 :                :     struct VacuumCutoffs *cutoffs;
                                 51                 :                :     Relation    relation;
                                 52                 :                : 
                                 53                 :                :     /*
                                 54                 :                :      * Keep the buffer, block, and page handy so that helpers needing to
                                 55                 :                :      * access them don't need to make repeated calls to BufferGetBlockNumber()
                                 56                 :                :      * and BufferGetPage().
                                 57                 :                :      */
                                 58                 :                :     BlockNumber block;
                                 59                 :                :     Buffer      buffer;
                                 60                 :                :     Page        page;
                                 61                 :                : 
                                 62                 :                :     /*-------------------------------------------------------
                                 63                 :                :      * Fields describing what to do to the page
                                 64                 :                :      *-------------------------------------------------------
                                 65                 :                :      */
                                 66                 :                :     TransactionId new_prune_xid;    /* new prune hint value */
                                 67                 :                :     TransactionId latest_xid_removed;
                                 68                 :                :     int         nredirected;    /* numbers of entries in arrays below */
                                 69                 :                :     int         ndead;
                                 70                 :                :     int         nunused;
                                 71                 :                :     int         nfrozen;
                                 72                 :                :     /* arrays that accumulate indexes of items to be changed */
                                 73                 :                :     OffsetNumber redirected[MaxHeapTuplesPerPage * 2];
                                 74                 :                :     OffsetNumber nowdead[MaxHeapTuplesPerPage];
                                 75                 :                :     OffsetNumber nowunused[MaxHeapTuplesPerPage];
                                 76                 :                :     HeapTupleFreeze frozen[MaxHeapTuplesPerPage];
                                 77                 :                : 
                                 78                 :                :     /*
                                 79                 :                :      * set_all_visible and set_all_frozen indicate if the all-visible and
                                 80                 :                :      * all-frozen bits in the visibility map can be set for this page after
                                 81                 :                :      * pruning. They are only tracked when the caller requests VM updates
                                 82                 :                :      * (attempt_set_vm); otherwise they remain false throughout.
                                 83                 :                :      *
                                 84                 :                :      * NOTE: set_all_visible and set_all_frozen initially don't include
                                 85                 :                :      * LP_DEAD items. That's convenient for heap_page_prune_and_freeze() to
                                 86                 :                :      * use them to decide whether to opportunistically freeze the page or not.
                                 87                 :                :      * The set_all_visible and set_all_frozen values ultimately used to set
                                 88                 :                :      * the VM are adjusted to include LP_DEAD items after we determine whether
                                 89                 :                :      * or not to opportunistically freeze.
                                 90                 :                :      */
                                 91                 :                :     bool        set_all_visible;
                                 92                 :                :     bool        set_all_frozen;
                                 93                 :                : 
                                 94                 :                :     /*-------------------------------------------------------
                                 95                 :                :      * Working state for HOT chain processing
                                 96                 :                :      *-------------------------------------------------------
                                 97                 :                :      */
                                 98                 :                : 
                                 99                 :                :     /*
                                100                 :                :      * 'root_items' contains offsets of all LP_REDIRECT line pointers and
                                101                 :                :      * normal non-HOT tuples.  They can be stand-alone items or the first item
                                102                 :                :      * in a HOT chain.  'heaponly_items' contains heap-only tuples which can
                                103                 :                :      * only be removed as part of a HOT chain.
                                104                 :                :      */
                                105                 :                :     int         nroot_items;
                                106                 :                :     OffsetNumber root_items[MaxHeapTuplesPerPage];
                                107                 :                :     int         nheaponly_items;
                                108                 :                :     OffsetNumber heaponly_items[MaxHeapTuplesPerPage];
                                109                 :                : 
                                110                 :                :     /*
                                111                 :                :      * processed[offnum] is true if item at offnum has been processed.
                                112                 :                :      *
                                113                 :                :      * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
                                114                 :                :      * 1. Otherwise every access would need to subtract 1.
                                115                 :                :      */
                                116                 :                :     bool        processed[MaxHeapTuplesPerPage + 1];
                                117                 :                : 
                                118                 :                :     /*
                                119                 :                :      * Tuple visibility is only computed once for each tuple, for correctness
                                120                 :                :      * and efficiency reasons; see comment in heap_page_prune_and_freeze() for
                                121                 :                :      * details.  This is of type int8[], instead of HTSV_Result[], so we can
                                122                 :                :      * use -1 to indicate no visibility has been computed, e.g. for LP_DEAD
                                123                 :                :      * items.
                                124                 :                :      *
                                125                 :                :      * This needs to be MaxHeapTuplesPerPage + 1 long as FirstOffsetNumber is
                                126                 :                :      * 1. Otherwise every access would need to subtract 1.
                                127                 :                :      */
                                128                 :                :     int8        htsv[MaxHeapTuplesPerPage + 1];
                                129                 :                : 
                                130                 :                :     /*-------------------------------------------------------
                                131                 :                :      * Working state for freezing
                                132                 :                :      *-------------------------------------------------------
                                133                 :                :      */
                                134                 :                :     HeapPageFreeze pagefrz;
                                135                 :                : 
                                136                 :                :     /*-------------------------------------------------------
                                137                 :                :      * Working state for visibility map processing
                                138                 :                :      *-------------------------------------------------------
                                139                 :                :      */
                                140                 :                : 
                                141                 :                :     /*
                                142                 :                :      * Caller must provide a pinned vmbuffer corresponding to the heap block
                                143                 :                :      * passed to heap_page_prune_and_freeze(). We will fix any corruption
                                144                 :                :      * found in the VM and set the VM if the page is all-visible/all-frozen.
                                145                 :                :      */
                                146                 :                :     Buffer      vmbuffer;
                                147                 :                : 
                                148                 :                :     /*
                                149                 :                :      * The state of the VM bits at the beginning of pruning and the state they
                                150                 :                :      * will be in at the end.
                                151                 :                :      */
                                152                 :                :     uint8       old_vmbits;
                                153                 :                :     uint8       new_vmbits;
                                154                 :                : 
                                155                 :                :     /* The newest xmin of live tuples on the page */
                                156                 :                :     TransactionId newest_live_xid;
                                157                 :                : 
                                158                 :                :     /*-------------------------------------------------------
                                159                 :                :      * Information about what was done
                                160                 :                :      *
                                161                 :                :      * These fields are not used by pruning itself for the most part, but are
                                162                 :                :      * used to collect information about what was pruned and what state the
                                163                 :                :      * page is in after pruning, for the benefit of the caller.  They are
                                164                 :                :      * copied to the caller's PruneFreezeResult at the end.
                                165                 :                :      * -------------------------------------------------------
                                166                 :                :      */
                                167                 :                : 
                                168                 :                :     int         ndeleted;       /* Number of tuples deleted from the page */
                                169                 :                : 
                                170                 :                :     /* Number of live and recently dead tuples, after pruning */
                                171                 :                :     int         live_tuples;
                                172                 :                :     int         recently_dead_tuples;
                                173                 :                : 
                                174                 :                :     /* Whether or not the page makes rel truncation unsafe */
                                175                 :                :     bool        hastup;
                                176                 :                : 
                                177                 :                :     /*
                                178                 :                :      * LP_DEAD items on the page after pruning.  Includes existing LP_DEAD
                                179                 :                :      * items
                                180                 :                :      */
                                181                 :                :     int         lpdead_items;   /* number of items in the array */
                                182                 :                :     OffsetNumber *deadoffsets;  /* points directly to presult->deadoffsets */
                                183                 :                : } PruneState;
                                184                 :                : 
                                185                 :                : /*
                                186                 :                :  * Type of visibility map corruption detected on a heap page and its
                                187                 :                :  * associated VM page. Passed to heap_page_fix_vm_corruption() so the caller
                                188                 :                :  * can specify what it found rather than having the function rederive the
                                189                 :                :  * corruption from page state.
                                190                 :                :  */
                                191                 :                : typedef enum VMCorruptionType
                                192                 :                : {
                                193                 :                :     /* VM bits are set but the heap page-level PD_ALL_VISIBLE flag is not */
                                194                 :                :     VM_CORRUPT_MISSING_PAGE_HINT,
                                195                 :                :     /* LP_DEAD line pointers found on a page marked all-visible */
                                196                 :                :     VM_CORRUPT_LPDEAD,
                                197                 :                :     /* Tuple not visible to all transactions on a page marked all-visible */
                                198                 :                :     VM_CORRUPT_TUPLE_VISIBILITY,
                                199                 :                : } VMCorruptionType;
                                200                 :                : 
                                201                 :                : /* Local functions */
                                202                 :                : static void prune_freeze_setup(PruneFreezeParams *params,
                                203                 :                :                                TransactionId *new_relfrozen_xid,
                                204                 :                :                                MultiXactId *new_relmin_mxid,
                                205                 :                :                                PruneFreezeResult *presult,
                                206                 :                :                                PruneState *prstate);
                                207                 :                : static void heap_page_fix_vm_corruption(PruneState *prstate,
                                208                 :                :                                         OffsetNumber offnum,
                                209                 :                :                                         VMCorruptionType corruption_type);
                                210                 :                : static void prune_freeze_fast_path(PruneState *prstate,
                                211                 :                :                                    PruneFreezeResult *presult);
                                212                 :                : static void prune_freeze_plan(PruneState *prstate,
                                213                 :                :                               OffsetNumber *off_loc);
                                214                 :                : static HTSV_Result heap_prune_satisfies_vacuum(PruneState *prstate,
                                215                 :                :                                                HeapTuple tup);
                                216                 :                : static inline HTSV_Result htsv_get_valid_status(int status);
                                217                 :                : static void heap_prune_chain(OffsetNumber maxoff,
                                218                 :                :                              OffsetNumber rootoffnum, PruneState *prstate);
                                219                 :                : static void heap_prune_record_prunable(PruneState *prstate, TransactionId xid,
                                220                 :                :                                        OffsetNumber offnum);
                                221                 :                : static void heap_prune_record_redirect(PruneState *prstate,
                                222                 :                :                                        OffsetNumber offnum, OffsetNumber rdoffnum,
                                223                 :                :                                        bool was_normal);
                                224                 :                : static void heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum,
                                225                 :                :                                    bool was_normal);
                                226                 :                : static void heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum,
                                227                 :                :                                              bool was_normal);
                                228                 :                : static void heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum, bool was_normal);
                                229                 :                : 
                                230                 :                : static void heap_prune_record_unchanged_lp_unused(PruneState *prstate, OffsetNumber offnum);
                                231                 :                : static void heap_prune_record_unchanged_lp_normal(PruneState *prstate, OffsetNumber offnum);
                                232                 :                : static void heap_prune_record_unchanged_lp_dead(PruneState *prstate, OffsetNumber offnum);
                                233                 :                : static void heap_prune_record_unchanged_lp_redirect(PruneState *prstate, OffsetNumber offnum);
                                234                 :                : 
                                235                 :                : static void page_verify_redirects(Page page);
                                236                 :                : 
                                237                 :                : static bool heap_page_will_freeze(bool did_tuple_hint_fpi, bool do_prune, bool do_hint_prune,
                                238                 :                :                                   PruneState *prstate);
                                239                 :                : static bool heap_page_will_set_vm(PruneState *prstate, PruneReason reason,
                                240                 :                :                                   bool do_prune, bool do_freeze);
                                241                 :                : 
                                242                 :                : 
                                243                 :                : /*
                                244                 :                :  * Optionally prune and repair fragmentation in the specified page.
                                245                 :                :  *
                                246                 :                :  * This is an opportunistic function.  It will perform housekeeping
                                247                 :                :  * only if the page heuristically looks like a candidate for pruning and we
                                248                 :                :  * can acquire buffer cleanup lock without blocking.
                                249                 :                :  *
                                250                 :                :  * Note: this is called quite often.  It's important that it fall out quickly
                                251                 :                :  * if there's not any use in pruning.
                                252                 :                :  *
                                253                 :                :  * Caller must have pin on the buffer, and must *not* have a lock on it.
                                254                 :                :  *
                                255                 :                :  * This function may pin *vmbuffer. It's passed by reference so the caller can
                                256                 :                :  * reuse the pin across calls, avoiding repeated pin/unpin cycles. If we find
                                257                 :                :  * VM corruption during pruning, we will fix it. Caller is responsible for
                                258                 :                :  * unpinning *vmbuffer.
                                259                 :                :  *
                                260                 :                :  * rel_read_only is true if we determined at plan time that the query does not
                                261                 :                :  * modify the relation. It is counterproductive to set the VM if the query
                                262                 :                :  * will immediately clear it.
                                263                 :                :  *
                                264                 :                :  * As noted in ScanRelIsReadOnly(), INSERT ... SELECT from the same table will
                                265                 :                :  * report the scan relation as read-only. This is usually harmless in
                                266                 :                :  * practice. It is useful to set scanned pages all-visible that won't be
                                267                 :                :  * inserted into. Pages it does insert to will rarely meet the criteria for
                                268                 :                :  * pruning, and those that do are likely to contain in-progress inserts which
                                269                 :                :  * make the page not fully all-visible.
                                270                 :                :  */
                                271                 :                : void
  196 melanieplageman@gmai      272                 :CBC    11796392 : heap_page_prune_opt(Relation relation, Buffer buffer, Buffer *vmbuffer,
                                273                 :                :                     bool rel_read_only)
                                274                 :                : {
 3827 kgrittn@postgresql.o      275                 :       11796392 :     Page        page = BufferGetPage(buffer);
                                276                 :                :     TransactionId prune_xid;
                                277                 :                :     GlobalVisState *vistest;
                                278                 :                :     Size        minfree;
                                279                 :                : 
                                280                 :                :     /*
                                281                 :                :      * We can't write WAL in recovery mode, so there's no point trying to
                                282                 :                :      * clean the page. The primary will likely issue a cleaning WAL record
                                283                 :                :      * soon anyway, so this is no particular loss.
                                284                 :                :      */
 4606 rhaas@postgresql.org      285         [ +  + ]:       11796392 :     if (RecoveryInProgress())
                                286                 :         274650 :         return;
                                287                 :                : 
                                288                 :                :     /*
                                289                 :                :      * First check whether there's any chance there's something to prune,
                                290                 :                :      * determining the appropriate horizon is a waste if there's no prune_xid
                                291                 :                :      * (i.e. no updates/deletes left potentially dead tuples around and no
                                292                 :                :      * inserts inserted new tuples that may be visible to all).
                                293                 :                :      */
  221 melanieplageman@gmai      294                 :       11521742 :     prune_xid = PageGetPruneXid(page);
 2252 andres@anarazel.de        295         [ +  + ]:       11521742 :     if (!TransactionIdIsValid(prune_xid))
                                296                 :        7772335 :         return;
                                297                 :                : 
                                298                 :                :     /*
                                299                 :                :      * Check whether prune_xid indicates that there may be dead rows that can
                                300                 :                :      * be cleaned up.
                                301                 :                :      */
                                302                 :        3749407 :     vistest = GlobalVisTestFor(relation);
                                303                 :                : 
  202 melanieplageman@gmai      304         [ +  + ]:        3749407 :     if (!GlobalVisTestIsRemovableXid(vistest, prune_xid, true))
 1133 tmunro@postgresql.or      305                 :        1593346 :         return;
                                306                 :                : 
                                307                 :                :     /*
                                308                 :                :      * We prune when a previous UPDATE failed to find enough space on the page
                                309                 :                :      * for a new tuple version, or when free space falls below the relation's
                                310                 :                :      * fill-factor target (but not less than 10%).
                                311                 :                :      *
                                312                 :                :      * Checking free space here is questionable since we aren't holding any
                                313                 :                :      * lock on the buffer; in the worst case we could get a bogus answer. It's
                                314                 :                :      * unlikely to be *seriously* wrong, though, since reading either pd_lower
                                315                 :                :      * or pd_upper is probably atomic.  Avoiding taking a lock seems more
                                316                 :                :      * important than sometimes getting a wrong answer in what is after all
                                317                 :                :      * just a heuristic estimate.
                                318                 :                :      */
  914 akorotkov@postgresql      319         [ +  + ]:        2156061 :     minfree = RelationGetTargetPageFreeSpace(relation,
                                320                 :                :                                              HEAP_DEFAULT_FILLFACTOR);
 6962 tgl@sss.pgh.pa.us         321                 :        2156061 :     minfree = Max(minfree, BLCKSZ / 10);
                                322                 :                : 
 6665                           323   [ +  +  +  + ]:        2156061 :     if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
                                324                 :                :     {
   94 melanieplageman@gmai      325                 :         132697 :         bool        record_free_space = false;
                                326                 :         132697 :         Size        freespace = 0;
                                327                 :                : 
                                328                 :                :         /*
                                329                 :                :          * Pin the VM page before taking the heap cleanup lock. This may
                                330                 :                :          * occasionally lead to an unnecessary pin when the buffer is
                                331                 :                :          * contended, but the same VM page covers many heap pages, so there is
                                332                 :                :          * a good chance for the work to be reusable.
                                333                 :                :          */
   31                           334                 :         132697 :         visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer);
                                335                 :                : 
                                336                 :                :         /* OK, try to get exclusive buffer lock */
 6962 tgl@sss.pgh.pa.us         337         [ +  + ]:         132697 :         if (!ConditionalLockBufferForCleanup(buffer))
                                338                 :           1875 :             return;
                                339                 :                : 
                                340                 :                :         /*
                                341                 :                :          * Now that we have buffer lock, get accurate information about the
                                342                 :                :          * page's free space, and recheck the heuristic about whether to
                                343                 :                :          * prune.
                                344                 :                :          */
 6665                           345   [ +  +  +  - ]:         130822 :         if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
                                346                 :                :         {
                                347                 :                :             OffsetNumber dummy_off_loc;
                                348                 :                :             PruneFreezeResult presult;
                                349                 :                :             PruneFreezeParams params;
                                350                 :                : 
  204 melanieplageman@gmai      351                 :         130822 :             params.relation = relation;
                                352                 :         130822 :             params.buffer = buffer;
                                353                 :         130822 :             params.vmbuffer = *vmbuffer;
                                354                 :         130822 :             params.reason = PRUNE_ON_ACCESS;
                                355                 :         130822 :             params.vistest = vistest;
                                356                 :         130822 :             params.cutoffs = NULL;
                                357                 :                : 
                                358                 :                :             /*
                                359                 :                :              * We don't pass the HEAP_PAGE_PRUNE_MARK_UNUSED_NOW option
                                360                 :                :              * regardless of whether or not the relation has indexes, since we
                                361                 :                :              * cannot safely determine that during on-access pruning with the
                                362                 :                :              * current implementation.
                                363                 :                :              */
                                364                 :         130822 :             params.options = HEAP_PAGE_PRUNE_ALLOW_FAST_PATH;
  196                           365         [ +  + ]:         130822 :             if (rel_read_only)
                                366                 :          36794 :                 params.options |= HEAP_PAGE_PRUNE_SET_VM;
                                367                 :                : 
  326                           368                 :         130822 :             heap_page_prune_and_freeze(&params, &presult, &dummy_off_loc,
                                369                 :                :                                        NULL, NULL);
                                370                 :                : 
                                371                 :                :             /*
                                372                 :                :              * Report the number of tuples reclaimed to pgstats.  This is
                                373                 :                :              * presult.ndeleted minus the number of newly-LP_DEAD-set items.
                                374                 :                :              *
                                375                 :                :              * We derive the number of dead tuples like this to avoid totally
                                376                 :                :              * forgetting about items that were set to LP_DEAD, since they
                                377                 :                :              * still need to be cleaned up by VACUUM.  We only want to count
                                378                 :                :              * heap-only tuples that just became LP_UNUSED in our report,
                                379                 :                :              * which don't.
                                380                 :                :              *
                                381                 :                :              * VACUUM doesn't have to compensate in the same way when it
                                382                 :                :              * tracks ndeleted, since it will set the same LP_DEAD items to
                                383                 :                :              * LP_UNUSED separately.
                                384                 :                :              */
 1110 rhaas@postgresql.org      385         [ +  + ]:         130822 :             if (presult.ndeleted > presult.nnewlpdead)
 1795 pg@bowt.ie                386                 :          20726 :                 pgstat_update_heap_dead_tuples(relation,
 1110 rhaas@postgresql.org      387                 :          20726 :                                                presult.ndeleted - presult.nnewlpdead);
                                388                 :                : 
                                389                 :                :             /*
                                390                 :                :              * If this prune newly set the page all-visible, VACUUM may later
                                391                 :                :              * skip the page and not update the free space map (FSM) for it.
                                392                 :                :              * Keep the FSM from going stale by recording it now. We do not
                                393                 :                :              * want to update the freespace map otherwise, to reserve
                                394                 :                :              * freespace on this page for HOT updates.
                                395                 :                :              */
   94 melanieplageman@gmai      396         [ +  + ]:         130822 :             if (presult.newly_all_visible)
                                397                 :                :             {
                                398                 :          28684 :                 record_free_space = true;
                                399                 :          28684 :                 freespace = PageGetHeapFreeSpace(page);
                                400                 :                :             }
                                401                 :                :         }
                                402                 :                : 
                                403                 :                :         /* And release buffer lock */
 6962 tgl@sss.pgh.pa.us         404                 :         130822 :         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
                                405                 :                : 
                                406                 :                :         /*
                                407                 :                :          * RecordPageWithFreeSpace() only dirties the FSM when the recorded
                                408                 :                :          * free-space category actually changes. Note that vacuum will still
                                409                 :                :          * do FreeSpaceMapVacuum() for ranges of pages that are skipped, so we
                                410                 :                :          * don't have to worry about that here.
                                411                 :                :          */
   94 melanieplageman@gmai      412         [ +  + ]:         130822 :         if (record_free_space)
                                413                 :          28684 :             RecordPageWithFreeSpace(relation, BufferGetBlockNumber(buffer), freespace);
                                414                 :                :     }
                                415                 :                : }
                                416                 :                : 
                                417                 :                : /*
                                418                 :                :  * Helper for heap_page_prune_and_freeze() to initialize the PruneState using
                                419                 :                :  * the provided parameters.
                                420                 :                :  *
                                421                 :                :  * params, new_relfrozen_xid, new_relmin_mxid, and presult are input
                                422                 :                :  * parameters and are not modified by this function. Only prstate is modified.
                                423                 :                :  */
                                424                 :                : static void
  320                           425                 :         223640 : prune_freeze_setup(PruneFreezeParams *params,
                                426                 :                :                    TransactionId *new_relfrozen_xid,
                                427                 :                :                    MultiXactId *new_relmin_mxid,
                                428                 :                :                    PruneFreezeResult *presult,
                                429                 :                :                    PruneState *prstate)
                                430                 :                : {
                                431                 :                :     /* Copy parameters to prstate */
                                432                 :         223640 :     prstate->vistest = params->vistest;
                                433                 :         223640 :     prstate->mark_unused_now =
  326                           434                 :         223640 :         (params->options & HEAP_PAGE_PRUNE_MARK_UNUSED_NOW) != 0;
                                435                 :                : 
                                436                 :                :     /* cutoffs must be provided if we will attempt freezing */
  321                           437   [ +  +  -  + ]:         223640 :     Assert(!(params->options & HEAP_PAGE_PRUNE_FREEZE) || params->cutoffs);
  320                           438                 :         223640 :     prstate->attempt_freeze = (params->options & HEAP_PAGE_PRUNE_FREEZE) != 0;
  196                           439                 :         223640 :     prstate->attempt_set_vm = (params->options & HEAP_PAGE_PRUNE_SET_VM) != 0;
  320                           440                 :         223640 :     prstate->cutoffs = params->cutoffs;
  221                           441                 :         223640 :     prstate->relation = params->relation;
                                442                 :         223640 :     prstate->block = BufferGetBlockNumber(params->buffer);
                                443                 :         223640 :     prstate->buffer = params->buffer;
                                444                 :         223640 :     prstate->page = BufferGetPage(params->buffer);
                                445                 :                : 
                                446                 :                :     /*
                                447                 :                :      * The caller must have pinned the VM page covering this heap block. If it
                                448                 :                :      * doesn't have the correct page pinned, visibilitymap_get_status() will
                                449                 :                :      * silently release the caller's pin and take its own, leaving the caller
                                450                 :                :      * holding a stale buffer and leaking ours.
                                451                 :                :      */
   20                           452         [ -  + ]:         223640 :     Assert(visibilitymap_pin_ok(prstate->block, params->vmbuffer));
  204                           453                 :         223640 :     prstate->vmbuffer = params->vmbuffer;
  202                           454                 :         223640 :     prstate->new_vmbits = 0;
  204                           455                 :         223640 :     prstate->old_vmbits = visibilitymap_get_status(prstate->relation,
                                456                 :                :                                                    prstate->block,
                                457                 :                :                                                    &prstate->vmbuffer);
                                458                 :                : 
                                459                 :                :     /*
                                460                 :                :      * Our strategy is to scan the page and make lists of items to change,
                                461                 :                :      * then apply the changes within a critical section.  This keeps as much
                                462                 :                :      * logic as possible out of the critical section, and also ensures that
                                463                 :                :      * WAL replay will work the same as the normal case.
                                464                 :                :      *
                                465                 :                :      * First, initialize the new pd_prune_xid value to zero (indicating no
                                466                 :                :      * prunable tuples).  If we find any tuples which may soon become
                                467                 :                :      * prunable, we will save the lowest relevant XID in new_prune_xid. Also
                                468                 :                :      * initialize the rest of our working state.
                                469                 :                :      */
  320                           470                 :         223640 :     prstate->new_prune_xid = InvalidTransactionId;
                                471                 :         223640 :     prstate->latest_xid_removed = InvalidTransactionId;
                                472                 :         223640 :     prstate->nredirected = prstate->ndead = prstate->nunused = 0;
                                473                 :         223640 :     prstate->nfrozen = 0;
                                474                 :         223640 :     prstate->nroot_items = 0;
                                475                 :         223640 :     prstate->nheaponly_items = 0;
                                476                 :                : 
                                477                 :                :     /* initialize page freezing working state */
                                478                 :         223640 :     prstate->pagefrz.freeze_required = false;
  216                           479                 :         223640 :     prstate->pagefrz.FreezePageConflictXid = InvalidTransactionId;
  320                           480         [ +  + ]:         223640 :     if (prstate->attempt_freeze)
                                481                 :                :     {
  312                           482   [ +  -  -  + ]:          92818 :         Assert(new_relfrozen_xid && new_relmin_mxid);
                                483                 :          92818 :         prstate->pagefrz.FreezePageRelfrozenXid = *new_relfrozen_xid;
                                484                 :          92818 :         prstate->pagefrz.NoFreezePageRelfrozenXid = *new_relfrozen_xid;
                                485                 :          92818 :         prstate->pagefrz.FreezePageRelminMxid = *new_relmin_mxid;
                                486                 :          92818 :         prstate->pagefrz.NoFreezePageRelminMxid = *new_relmin_mxid;
                                487                 :                :     }
                                488                 :                :     else
                                489                 :                :     {
                                490   [ +  -  -  + ]:         130822 :         Assert(!new_relfrozen_xid && !new_relmin_mxid);
  320                           491                 :         130822 :         prstate->pagefrz.FreezePageRelminMxid = InvalidMultiXactId;
                                492                 :         130822 :         prstate->pagefrz.NoFreezePageRelminMxid = InvalidMultiXactId;
                                493                 :         130822 :         prstate->pagefrz.FreezePageRelfrozenXid = InvalidTransactionId;
                                494                 :         130822 :         prstate->pagefrz.NoFreezePageRelfrozenXid = InvalidTransactionId;
                                495                 :                :     }
                                496                 :                : 
                                497                 :         223640 :     prstate->ndeleted = 0;
                                498                 :         223640 :     prstate->live_tuples = 0;
                                499                 :         223640 :     prstate->recently_dead_tuples = 0;
                                500                 :         223640 :     prstate->hastup = false;
                                501                 :         223640 :     prstate->lpdead_items = 0;
                                502                 :                : 
                                503                 :                :     /*
                                504                 :                :      * deadoffsets are filled in during pruning but are only used to populate
                                505                 :                :      * PruneFreezeResult->deadoffsets. To avoid needing two copies of the
                                506                 :                :      * array, just save a pointer to the result offsets array in the
                                507                 :                :      * PruneState.
                                508                 :                :      */
  300                           509                 :         223640 :     prstate->deadoffsets = presult->deadoffsets;
                                510                 :                : 
                                511                 :                :     /*
                                512                 :                :      * We track whether the page will be all-visible/all-frozen at the end of
                                513                 :                :      * pruning and freezing. While examining tuple visibility, we'll set
                                514                 :                :      * set_all_visible to false if there are tuples on the page not visible to
                                515                 :                :      * all running and future transactions. If setting the VM is enabled for
                                516                 :                :      * this scan, we will do so if the page ends up being all-visible.
                                517                 :                :      *
                                518                 :                :      * We also keep track of the newest live XID, which is used to calculate
                                519                 :                :      * the snapshot conflict horizon for a WAL record setting the VM.
                                520                 :                :      */
  196                           521                 :         223640 :     prstate->set_all_visible = prstate->attempt_set_vm;
  202                           522                 :         223640 :     prstate->newest_live_xid = InvalidTransactionId;
                                523                 :                : 
                                524                 :                :     /*
                                525                 :                :      * Currently, only VACUUM performs freezing, but other callers may in the
                                526                 :                :      * future. We must initialize set_all_frozen based on whether or not the
                                527                 :                :      * caller passed HEAP_PAGE_PRUNE_FREEZE, because if they did not, we won't
                                528                 :                :      * call heap_prepare_freeze_tuple() for each tuple, and set_all_frozen
                                529                 :                :      * will never be cleared for tuples that need freezing. This would lead to
                                530                 :                :      * incorrectly setting the visibility map all-frozen for this page. We
                                531                 :                :      * can't set the page all-frozen in the VM if the caller didn't pass
                                532                 :                :      * HEAP_PAGE_PRUNE_SET_VM.
                                533                 :                :      *
                                534                 :                :      * When freezing is not required (no XIDs/MXIDs older than the freeze
                                535                 :                :      * cutoff), we may still choose to "opportunistically" freeze if doing so
                                536                 :                :      * would make the page all-frozen.
                                537                 :                :      *
                                538                 :                :      * We will not be able to freeze the whole page at the end of vacuum if
                                539                 :                :      * there are tuples present that are not visible to everyone or if there
                                540                 :                :      * are dead tuples which will not be removable. However, dead tuples that
                                541                 :                :      * will be removed by the end of vacuum should not prevent this
                                542                 :                :      * opportunistic freezing.
                                543                 :                :      *
                                544                 :                :      * Therefore, we do not clear set_all_visible and set_all_frozen when we
                                545                 :                :      * encounter LP_DEAD items. Instead, we correct them after deciding
                                546                 :                :      * whether to freeze, but before updating the VM, to avoid setting the VM
                                547                 :                :      * bits incorrectly.
                                548                 :                :      */
  196                           549   [ +  +  +  - ]:         223640 :     prstate->set_all_frozen = prstate->attempt_freeze && prstate->attempt_set_vm;
  320                           550                 :         223640 : }
                                551                 :                : 
                                552                 :                : /*
                                553                 :                :  * Helper for heap_page_prune_and_freeze(). Iterates over every tuple on the
                                554                 :                :  * page, examines its visibility information, and determines the appropriate
                                555                 :                :  * action for each tuple. All tuples are processed and classified during this
                                556                 :                :  * phase, but no modifications are made to the page until the later execution
                                557                 :                :  * stage.
                                558                 :                :  *
                                559                 :                :  * *off_loc is used for error callback and cleared before returning.
                                560                 :                :  */
                                561                 :                : static void
  221                           562                 :         210650 : prune_freeze_plan(PruneState *prstate, OffsetNumber *off_loc)
                                563                 :                : {
                                564                 :         210650 :     Page        page = prstate->page;
                                565                 :         210650 :     BlockNumber blockno = prstate->block;
                                566                 :         210650 :     OffsetNumber maxoff = PageGetMaxOffsetNumber(prstate->page);
                                567                 :                :     OffsetNumber offnum;
                                568                 :                :     HeapTupleData tup;
                                569                 :                : 
                                570                 :         210650 :     tup.t_tableOid = RelationGetRelid(prstate->relation);
                                571                 :                : 
                                572                 :                :     /*
                                573                 :                :      * Determine HTSV for all tuples, and queue them up for processing as HOT
                                574                 :                :      * chain roots or as heap-only items.
                                575                 :                :      *
                                576                 :                :      * Determining HTSV only once for each tuple is required for correctness,
                                577                 :                :      * to deal with cases where running HTSV twice could result in different
                                578                 :                :      * results.  For example, RECENTLY_DEAD can turn to DEAD if another
                                579                 :                :      * checked item causes GlobalVisTestIsRemovableFullXid() to update the
                                580                 :                :      * horizon, or INSERT_IN_PROGRESS can change to DEAD if the inserting
                                581                 :                :      * transaction aborts.
                                582                 :                :      *
                                583                 :                :      * It's also good for performance. Most commonly tuples within a page are
                                584                 :                :      * stored at decreasing offsets (while the items are stored at increasing
                                585                 :                :      * offsets). When processing all tuples on a page this leads to reading
                                586                 :                :      * memory at decreasing offsets within a page, with a variable stride.
                                587                 :                :      * That's hard for CPU prefetchers to deal with. Processing the items in
                                588                 :                :      * reverse order (and thus the tuples in increasing order) increases
                                589                 :                :      * prefetching efficiency significantly / decreases the number of cache
                                590                 :                :      * misses.
                                591                 :                :      */
 1767 andres@anarazel.de        592                 :         210650 :     for (offnum = maxoff;
                                593         [ +  + ]:       21440320 :          offnum >= FirstOffsetNumber;
                                594                 :       21229670 :          offnum = OffsetNumberPrev(offnum))
                                595                 :                :     {
                                596                 :       21229670 :         ItemId      itemid = PageGetItemId(page, offnum);
                                597                 :                :         HeapTupleHeader htup;
                                598                 :                : 
                                599                 :                :         /*
                                600                 :                :          * Set the offset number so that we can display it along with any
                                601                 :                :          * error that occurred while processing this tuple.
                                602                 :                :          */
  924 heikki.linnakangas@i      603                 :       21229670 :         *off_loc = offnum;
                                604                 :                : 
  320 melanieplageman@gmai      605                 :       21229670 :         prstate->processed[offnum] = false;
                                606                 :       21229670 :         prstate->htsv[offnum] = -1;
                                607                 :                : 
                                608                 :                :         /* Nothing to do if slot doesn't contain a tuple */
  924 heikki.linnakangas@i      609         [ +  + ]:       21229670 :         if (!ItemIdIsUsed(itemid))
                                610                 :                :         {
  221 melanieplageman@gmai      611                 :         356899 :             heap_prune_record_unchanged_lp_unused(prstate, offnum);
 1767 andres@anarazel.de        612                 :         356899 :             continue;
                                613                 :                :         }
                                614                 :                : 
  924 heikki.linnakangas@i      615         [ +  + ]:       20872771 :         if (ItemIdIsDead(itemid))
                                616                 :                :         {
                                617                 :                :             /*
                                618                 :                :              * If the caller set mark_unused_now true, we can set dead line
                                619                 :                :              * pointers LP_UNUSED now.
                                620                 :                :              */
  320 melanieplageman@gmai      621         [ +  + ]:        1585561 :             if (unlikely(prstate->mark_unused_now))
                                622                 :           1401 :                 heap_prune_record_unused(prstate, offnum, false);
                                623                 :                :             else
  221                           624                 :        1584160 :                 heap_prune_record_unchanged_lp_dead(prstate, offnum);
  924 heikki.linnakangas@i      625                 :        1585561 :             continue;
                                626                 :                :         }
                                627                 :                : 
                                628         [ +  + ]:       19287210 :         if (ItemIdIsRedirected(itemid))
                                629                 :                :         {
                                630                 :                :             /* This is the start of a HOT chain */
  320 melanieplageman@gmai      631                 :         210154 :             prstate->root_items[prstate->nroot_items++] = offnum;
  924 heikki.linnakangas@i      632                 :         210154 :             continue;
                                633                 :                :         }
                                634                 :                : 
                                635         [ -  + ]:       19077056 :         Assert(ItemIdIsNormal(itemid));
                                636                 :                : 
                                637                 :                :         /*
                                638                 :                :          * Get the tuple's visibility status and queue it up for processing.
                                639                 :                :          */
                                640                 :       19077056 :         htup = (HeapTupleHeader) PageGetItem(page, itemid);
                                641                 :       19077056 :         tup.t_data = htup;
                                642                 :       19077056 :         tup.t_len = ItemIdGetLength(itemid);
                                643                 :       19077056 :         ItemPointerSet(&tup.t_self, blockno, offnum);
                                644                 :                : 
  221 melanieplageman@gmai      645                 :       19077056 :         prstate->htsv[offnum] = heap_prune_satisfies_vacuum(prstate, &tup);
                                646                 :                : 
  924 heikki.linnakangas@i      647         [ +  + ]:       19077056 :         if (!HeapTupleHeaderIsHeapOnly(htup))
  320 melanieplageman@gmai      648                 :       18720953 :             prstate->root_items[prstate->nroot_items++] = offnum;
                                649                 :                :         else
                                650                 :         356103 :             prstate->heaponly_items[prstate->nheaponly_items++] = offnum;
                                651                 :                :     }
                                652                 :                : 
                                653                 :                :     /*
                                654                 :                :      * Process HOT chains.
                                655                 :                :      *
                                656                 :                :      * We added the items to the array starting from 'maxoff', so by
                                657                 :                :      * processing the array in reverse order, we process the items in
                                658                 :                :      * ascending offset number order.  The order doesn't matter for
                                659                 :                :      * correctness, but some quick micro-benchmarking suggests that this is
                                660                 :                :      * faster.  (Earlier PostgreSQL versions, which scanned all the items on
                                661                 :                :      * the page instead of using the root_items array, also did it in
                                662                 :                :      * ascending offset number order.)
                                663                 :                :      */
                                664         [ +  + ]:       19141757 :     for (int i = prstate->nroot_items - 1; i >= 0; i--)
                                665                 :                :     {
                                666                 :       18931107 :         offnum = prstate->root_items[i];
                                667                 :                : 
                                668                 :                :         /* Ignore items already processed as part of an earlier chain */
                                669         [ -  + ]:       18931107 :         if (prstate->processed[offnum])
 6792 tgl@sss.pgh.pa.us         670                 :UBC           0 :             continue;
                                671                 :                : 
                                672                 :                :         /* see preceding loop */
  924 heikki.linnakangas@i      673                 :CBC    18931107 :         *off_loc = offnum;
                                674                 :                : 
                                675                 :                :         /* Process this item or chain of items */
  221 melanieplageman@gmai      676                 :       18931107 :         heap_prune_chain(maxoff, offnum, prstate);
                                677                 :                :     }
                                678                 :                : 
                                679                 :                :     /*
                                680                 :                :      * Process any heap-only tuples that were not already processed as part of
                                681                 :                :      * a HOT chain.
                                682                 :                :      */
  320                           683         [ +  + ]:         566753 :     for (int i = prstate->nheaponly_items - 1; i >= 0; i--)
                                684                 :                :     {
                                685                 :         356103 :         offnum = prstate->heaponly_items[i];
                                686                 :                : 
                                687         [ +  + ]:         356103 :         if (prstate->processed[offnum])
  924 heikki.linnakangas@i      688                 :         339925 :             continue;
                                689                 :                : 
                                690                 :                :         /* see preceding loop */
                                691                 :          16178 :         *off_loc = offnum;
                                692                 :                : 
                                693                 :                :         /*
                                694                 :                :          * If the tuple is DEAD and doesn't chain to anything else, mark it
                                695                 :                :          * unused.  (If it does chain, we can only remove it as part of
                                696                 :                :          * pruning its chain.)
                                697                 :                :          *
                                698                 :                :          * We need this primarily to handle aborted HOT updates, that is,
                                699                 :                :          * XMIN_INVALID heap-only tuples.  Those might not be linked to by any
                                700                 :                :          * chain, since the parent tuple might be re-updated before any
                                701                 :                :          * pruning occurs.  So we have to be able to reap them separately from
                                702                 :                :          * chain-pruning.  (Note that HeapTupleHeaderIsHotUpdated will never
                                703                 :                :          * return true for an XMIN_INVALID tuple, so this code will work even
                                704                 :                :          * when there were sequential updates within the aborted transaction.)
                                705                 :                :          */
  320 melanieplageman@gmai      706         [ +  + ]:          16178 :         if (prstate->htsv[offnum] == HEAPTUPLE_DEAD)
                                707                 :                :         {
  924 heikki.linnakangas@i      708                 :           3488 :             ItemId      itemid = PageGetItemId(page, offnum);
                                709                 :           3488 :             HeapTupleHeader htup = (HeapTupleHeader) PageGetItem(page, itemid);
                                710                 :                : 
                                711         [ +  - ]:           3488 :             if (likely(!HeapTupleHeaderIsHotUpdated(htup)))
                                712                 :                :             {
                                713                 :           3488 :                 HeapTupleHeaderAdvanceConflictHorizon(htup,
                                714                 :                :                                                       &prstate->latest_xid_removed);
  320 melanieplageman@gmai      715                 :           3488 :                 heap_prune_record_unused(prstate, offnum, true);
                                716                 :                :             }
                                717                 :                :             else
                                718                 :                :             {
                                719                 :                :                 /*
                                720                 :                :                  * This tuple should've been processed and removed as part of
                                721                 :                :                  * a HOT chain, so something's wrong.  To preserve evidence,
                                722                 :                :                  * we don't dare to remove it.  We cannot leave behind a DEAD
                                723                 :                :                  * tuple either, because that will cause VACUUM to error out.
                                724                 :                :                  * Throwing an error with a distinct error message seems like
                                725                 :                :                  * the least bad option.
                                726                 :                :                  */
  924 heikki.linnakangas@i      727         [ #  # ]:UBC           0 :                 elog(ERROR, "dead heap-only tuple (%u, %d) is not linked to from any HOT chain",
                                728                 :                :                      blockno, offnum);
                                729                 :                :             }
                                730                 :                :         }
                                731                 :                :         else
  221 melanieplageman@gmai      732                 :CBC       12690 :             heap_prune_record_unchanged_lp_normal(prstate, offnum);
                                733                 :                :     }
                                734                 :                : 
                                735                 :                :     /* We should now have processed every tuple exactly once  */
                                736                 :                : #ifdef USE_ASSERT_CHECKING
  924 heikki.linnakangas@i      737                 :         210650 :     for (offnum = FirstOffsetNumber;
                                738         [ +  + ]:       21440320 :          offnum <= maxoff;
                                739                 :       21229670 :          offnum = OffsetNumberNext(offnum))
                                740                 :                :     {
                                741                 :       21229670 :         *off_loc = offnum;
                                742                 :                : 
  320 melanieplageman@gmai      743         [ -  + ]:       21229670 :         Assert(prstate->processed[offnum]);
                                744                 :                :     }
                                745                 :                : #endif
                                746                 :                : 
                                747                 :                :     /* Clear the offset information once we have processed the given page. */
  924 heikki.linnakangas@i      748                 :         210650 :     *off_loc = InvalidOffsetNumber;
  320 melanieplageman@gmai      749                 :         210650 : }
                                750                 :                : 
                                751                 :                : /*
                                752                 :                :  * Decide whether to proceed with freezing according to the freeze plans
                                753                 :                :  * prepared for the current heap buffer. If freezing is chosen, this function
                                754                 :                :  * performs several pre-freeze checks.
                                755                 :                :  *
                                756                 :                :  * The values of do_prune, do_hint_prune, and did_tuple_hint_fpi must be
                                757                 :                :  * determined before calling this function.
                                758                 :                :  *
                                759                 :                :  * prstate is both an input and output parameter.
                                760                 :                :  *
                                761                 :                :  * Returns true if we should apply the freeze plans and freeze tuples on the
                                762                 :                :  * page, and false otherwise.
                                763                 :                :  */
                                764                 :                : static bool
  221                           765                 :         210650 : heap_page_will_freeze(bool did_tuple_hint_fpi,
                                766                 :                :                       bool do_prune,
                                767                 :                :                       bool do_hint_prune,
                                768                 :                :                       PruneState *prstate)
                                769                 :                : {
  320                           770                 :         210650 :     bool        do_freeze = false;
                                771                 :                : 
                                772                 :                :     /*
                                773                 :                :      * If the caller specified we should not attempt to freeze any tuples,
                                774                 :                :      * validate that everything is in the right state and return.
                                775                 :                :      */
                                776         [ +  + ]:         210650 :     if (!prstate->attempt_freeze)
                                777                 :                :     {
  221                           778   [ +  -  -  + ]:         130822 :         Assert(!prstate->set_all_frozen && prstate->nfrozen == 0);
  320                           779                 :         130822 :         return false;
                                780                 :                :     }
                                781                 :                : 
                                782         [ +  + ]:          79828 :     if (prstate->pagefrz.freeze_required)
                                783                 :                :     {
                                784                 :                :         /*
                                785                 :                :          * heap_prepare_freeze_tuple indicated that at least one XID/MXID from
                                786                 :                :          * before FreezeLimit/MultiXactCutoff is present.  Must freeze to
                                787                 :                :          * advance relfrozenxid/relminmxid.
                                788                 :                :          */
                                789                 :          20612 :         do_freeze = true;
                                790                 :                :     }
                                791                 :                :     else
                                792                 :                :     {
                                793                 :                :         /*
                                794                 :                :          * Opportunistically freeze the page if we are generating an FPI
                                795                 :                :          * anyway and if doing so means that we can set the page all-frozen
                                796                 :                :          * afterwards (might not happen until VACUUM's final heap pass).
                                797                 :                :          *
                                798                 :                :          * XXX: Previously, we knew if pruning emitted an FPI by checking
                                799                 :                :          * pgWalUsage.wal_fpi before and after pruning.  Once the freeze and
                                800                 :                :          * prune records were combined, this heuristic couldn't be used
                                801                 :                :          * anymore.  The opportunistic freeze heuristic must be improved;
                                802                 :                :          * however, for now, try to approximate the old logic.
                                803                 :                :          */
  221                           804   [ +  +  +  + ]:          59216 :         if (prstate->set_all_frozen && prstate->nfrozen > 0)
                                805                 :                :         {
                                806         [ -  + ]:          25863 :             Assert(prstate->set_all_visible);
                                807                 :                : 
                                808                 :                :             /*
                                809                 :                :              * Freezing would make the page all-frozen.  Have already emitted
                                810                 :                :              * an FPI or will do so anyway?
                                811                 :                :              */
                                812   [ +  +  +  +  :          25863 :             if (RelationNeedsWAL(prstate->relation))
                                        +  -  +  - ]
                                813                 :                :             {
  320                           814         [ +  + ]:          24414 :                 if (did_tuple_hint_fpi)
                                815                 :           1526 :                     do_freeze = true;
                                816         [ +  + ]:          22888 :                 else if (do_prune)
                                817                 :                :                 {
  221                           818         [ +  + ]:           3885 :                     if (XLogCheckBufferNeedsBackup(prstate->buffer))
  320                           819                 :           1068 :                         do_freeze = true;
                                820                 :                :                 }
                                821         [ +  + ]:          19003 :                 else if (do_hint_prune)
                                822                 :                :                 {
  221                           823   [ +  +  +  -  :          22540 :                     if (XLogHintBitIsNeeded() &&
                                              +  + ]
                                824                 :          11270 :                         XLogCheckBufferNeedsBackup(prstate->buffer))
  320                           825                 :           2197 :                         do_freeze = true;
                                826                 :                :                 }
                                827                 :                :             }
                                828                 :                :         }
                                829                 :                :     }
                                830                 :                : 
                                831         [ +  + ]:          79828 :     if (do_freeze)
                                832                 :                :     {
                                833                 :                :         /*
                                834                 :                :          * Validate the tuples we will be freezing before entering the
                                835                 :                :          * critical section.
                                836                 :                :          */
  221                           837                 :          25403 :         heap_pre_freeze_checks(prstate->buffer, prstate->frozen, prstate->nfrozen);
  216                           838         [ -  + ]:          25403 :         Assert(TransactionIdPrecedes(prstate->pagefrz.FreezePageConflictXid,
                                839                 :                :                                      prstate->cutoffs->OldestXmin));
                                840                 :                :     }
  320                           841         [ +  + ]:          54425 :     else if (prstate->nfrozen > 0)
                                842                 :                :     {
                                843                 :                :         /*
                                844                 :                :          * The page contained some tuples that were not already frozen, and we
                                845                 :                :          * chose not to freeze them now.  The page won't be all-frozen then.
                                846                 :                :          */
                                847         [ -  + ]:          22621 :         Assert(!prstate->pagefrz.freeze_required);
                                848                 :                : 
  221                           849                 :          22621 :         prstate->set_all_frozen = false;
  320                           850                 :          22621 :         prstate->nfrozen = 0;    /* avoid miscounts in instrumentation */
                                851                 :                :     }
                                852                 :                :     else
                                853                 :                :     {
                                854                 :                :         /*
                                855                 :                :          * We have no freeze plans to execute.  The page might already be
                                856                 :                :          * all-frozen (perhaps only following pruning), though.  Such pages
                                857                 :                :          * can be marked all-frozen in the VM by our caller, even though none
                                858                 :                :          * of its tuples were newly frozen here.
                                859                 :                :          */
                                860                 :                :     }
                                861                 :                : 
                                862                 :          79828 :     return do_freeze;
                                863                 :                : }
                                864                 :                : 
                                865                 :                : /*
                                866                 :                :  * Emit a warning about and fix visibility map corruption on the given page.
                                867                 :                :  *
                                868                 :                :  * The caller specifies the type of corruption it has already detected via
                                869                 :                :  * corruption_type, so that we can emit the appropriate warning. All cases
                                870                 :                :  * result in the VM bits being cleared; corruption types where PD_ALL_VISIBLE
                                871                 :                :  * is incorrectly set also clear PD_ALL_VISIBLE.
                                872                 :                :  *
                                873                 :                :  * Must be called while holding an exclusive lock on the heap buffer. Dead
                                874                 :                :  * items and not all-visible tuples must have been discovered under that same
                                875                 :                :  * lock. Although we do not hold a lock on the VM buffer, it is pinned, and
                                876                 :                :  * the heap buffer is exclusively locked, ensuring that no other backend can
                                877                 :                :  * update the VM bits corresponding to this heap page.
                                878                 :                :  *
                                879                 :                :  * This function makes changes to the VM and, potentially, the heap page, but
                                880                 :                :  * it does not need to be done in a critical section.
                                881                 :                :  */
                                882                 :                : static void
  204 melanieplageman@gmai      883                 :UBC           0 : heap_page_fix_vm_corruption(PruneState *prstate, OffsetNumber offnum,
                                884                 :                :                             VMCorruptionType corruption_type)
                                885                 :                : {
                                886                 :              0 :     const char *relname = RelationGetRelationName(prstate->relation);
                                887                 :              0 :     bool        do_clear_vm = false;
                                888                 :              0 :     bool        do_clear_heap = false;
                                889                 :                : 
                                890         [ #  # ]:              0 :     Assert(BufferIsLockedByMeInMode(prstate->buffer, BUFFER_LOCK_EXCLUSIVE));
                                891                 :                : 
                                892   [ #  #  #  # ]:              0 :     switch (corruption_type)
                                893                 :                :     {
                                894                 :              0 :         case VM_CORRUPT_LPDEAD:
                                895         [ #  # ]:              0 :             ereport(WARNING,
                                896                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                897                 :                :                      errmsg("dead line pointer found on page marked all-visible"),
                                898                 :                :                      errcontext("relation \"%s\", page %u, tuple %u",
                                899                 :                :                                 relname, prstate->block, offnum)));
                                900                 :              0 :             do_clear_vm = true;
                                901                 :              0 :             do_clear_heap = true;
                                902                 :              0 :             break;
                                903                 :                : 
                                904                 :              0 :         case VM_CORRUPT_TUPLE_VISIBILITY:
                                905                 :                : 
                                906                 :                :             /*
                                907                 :                :              * A HEAPTUPLE_LIVE tuple on an all-visible page can appear to not
                                908                 :                :              * be visible to everyone when
                                909                 :                :              * GetOldestNonRemovableTransactionId() returns a conservative
                                910                 :                :              * value that's older than the real safe xmin. That is not
                                911                 :                :              * corruption -- the PD_ALL_VISIBLE flag is still correct.
                                912                 :                :              *
                                913                 :                :              * However, dead tuple versions, in-progress inserts, and
                                914                 :                :              * in-progress deletes should never appear on a page marked
                                915                 :                :              * all-visible. That indicates real corruption. PD_ALL_VISIBLE
                                916                 :                :              * should have been cleared by the DML operation that deleted or
                                917                 :                :              * inserted the tuple.
                                918                 :                :              */
                                919         [ #  # ]:              0 :             ereport(WARNING,
                                920                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                921                 :                :                      errmsg("tuple not visible to all transactions found on page marked all-visible"),
                                922                 :                :                      errcontext("relation \"%s\", page %u, tuple %u",
                                923                 :                :                                 relname, prstate->block, offnum)));
                                924                 :              0 :             do_clear_vm = true;
                                925                 :              0 :             do_clear_heap = true;
                                926                 :              0 :             break;
                                927                 :                : 
                                928                 :              0 :         case VM_CORRUPT_MISSING_PAGE_HINT:
                                929                 :                : 
                                930                 :                :             /*
                                931                 :                :              * As of PostgreSQL 9.2, the visibility map bit should never be
                                932                 :                :              * set if the page-level bit is clear. However, for vacuum, it's
                                933                 :                :              * possible that the bit got cleared after
                                934                 :                :              * heap_vac_scan_next_block() was called, so we must recheck now
                                935                 :                :              * that we have the buffer lock before concluding that the VM is
                                936                 :                :              * corrupt.
                                937                 :                :              */
                                938         [ #  # ]:              0 :             Assert(!PageIsAllVisible(prstate->page));
                                939         [ #  # ]:              0 :             Assert(prstate->old_vmbits & VISIBILITYMAP_VALID_BITS);
                                940         [ #  # ]:              0 :             ereport(WARNING,
                                941                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                942                 :                :                      errmsg("page is not marked all-visible but visibility map bit is set"),
                                943                 :                :                      errcontext("relation \"%s\", page %u",
                                944                 :                :                                 relname, prstate->block)));
                                945                 :              0 :             do_clear_vm = true;
                                946                 :              0 :             break;
                                947                 :                :     }
                                948                 :                : 
                                949   [ #  #  #  # ]:              0 :     Assert(do_clear_heap || do_clear_vm);
                                950                 :                : 
                                951                 :                :     /* Avoid marking the buffer dirty if PD_ALL_VISIBLE is already clear */
                                952         [ #  # ]:              0 :     if (do_clear_heap)
                                953                 :                :     {
                                954         [ #  # ]:              0 :         Assert(PageIsAllVisible(prstate->page));
                                955                 :              0 :         PageClearAllVisible(prstate->page);
                                956                 :              0 :         MarkBufferDirtyHint(prstate->buffer, true);
                                957                 :                :     }
                                958                 :                : 
                                959         [ #  # ]:              0 :     if (do_clear_vm)
                                960                 :                :     {
   89                           961                 :              0 :         LockBuffer(prstate->vmbuffer, BUFFER_LOCK_EXCLUSIVE);
                                962                 :                :         /* This VM clear is not WAL-logged, so its return value is not needed. */
   69 melanieplageman@gmai      963                 :UNC           0 :         (void) visibilitymap_clear(prstate->relation->rd_locator,
                                964                 :                :                                    prstate->block, prstate->vmbuffer,
                                965                 :                :                                    VISIBILITYMAP_VALID_BITS);
   89 melanieplageman@gmai      966                 :UBC           0 :         LockBuffer(prstate->vmbuffer, BUFFER_LOCK_UNLOCK);
  204                           967                 :              0 :         prstate->old_vmbits = 0;
                                968                 :                :     }
                                969                 :              0 : }
                                970                 :                : 
                                971                 :                : /*
                                972                 :                :  * Decide whether to set the visibility map bits (all-visible and all-frozen)
                                973                 :                :  * for the current page using information from the PruneState and VM.
                                974                 :                :  *
                                975                 :                :  * This function does not actually set the VM bits or page-level visibility
                                976                 :                :  * hint, PD_ALL_VISIBLE.
                                977                 :                :  *
                                978                 :                :  * This should be called only after do_freeze has been decided (and do_prune
                                979                 :                :  * has been set), as these factor into our heuristic-based decision.
                                980                 :                :  *
                                981                 :                :  * Returns true if one or both VM bits should be set and false otherwise.
                                982                 :                :  */
                                983                 :                : static bool
  196 melanieplageman@gmai      984                 :CBC      210650 : heap_page_will_set_vm(PruneState *prstate, PruneReason reason,
                                985                 :                :                       bool do_prune, bool do_freeze)
                                986                 :                : {
                                987         [ +  + ]:         210650 :     if (!prstate->attempt_set_vm)
  202                           988                 :          94028 :         return false;
                                989                 :                : 
                                990         [ +  + ]:         116622 :     if (!prstate->set_all_visible)
                                991                 :          38153 :         return false;
                                992                 :                : 
                                993                 :                :     /*
                                994                 :                :      * If this is an on-access call and we're not actually pruning or
                                995                 :                :      * freezing, consider whether setting the VM would cost us an additional
                                996                 :                :      * heap page FPI. If the relation isn't WAL-logged, or if hint bits are
                                997                 :                :      * not WAL-logged, setting the VM won't include a heap page FPI (the
                                998                 :                :      * latter passes REGBUF_NO_IMAGE for the heap page), apart from a page
                                999                 :                :      * that has never been WAL-logged, which we don't bother about here.
                               1000                 :                :      */
  196                          1001   [ +  +  +  +  :          78469 :     if (reason == PRUNE_ON_ACCESS && !do_prune && !do_freeze &&
                                              +  - ]
   19                          1002   [ +  +  +  +  :          29133 :         RelationNeedsWAL(prstate->relation) && XLogHintBitIsNeeded())
                                     +  -  +  -  +  
                                           +  +  + ]
                               1003                 :                :     {
                               1004                 :                :         /*
                               1005                 :                :          * Because the page is known to be all-visible, we will clear
                               1006                 :                :          * pd_prune_xid regardless of whether we actually set the page
                               1007                 :                :          * all-visible in the VM. That clear is a hint update which is not
                               1008                 :                :          * WAL-logged, other than an FPI for torn-page protection, so in some
                               1009                 :                :          * cases we want to avoid setting the VM if doing so would cost us a
                               1010                 :                :          * heap page FPI that clearing pd_prune_xid wouldn't have.
                               1011                 :                :          *
                               1012                 :                :          * Since hint bits are WAL-logged, if the buffer is clean, clearing
                               1013                 :                :          * pd_prune_xid will already emit a heap page FPI if one is needed, so
                               1014                 :                :          * there's no reason to avoid setting the VM.
                               1015                 :                :          *
                               1016                 :                :          * However, if the heap buffer is already dirty, clearing pd_prune_xid
                               1017                 :                :          * will never emit an FPI. So avoid setting the VM if the page hasn't
                               1018                 :                :          * been WAL-logged since the current checkpoint began, as the record
                               1019                 :                :          * setting the VM would then include a heap page FPI.
                               1020                 :                :          */
                               1021   [ +  +  +  + ]:          29539 :         if (BufferIsDirty(prstate->buffer) &&
                               1022                 :          13737 :             XLogCheckBufferNeedsBackup(prstate->buffer))
                               1023                 :                :         {
                               1024                 :            565 :             prstate->set_all_visible = prstate->set_all_frozen = false;
                               1025                 :            565 :             return false;
                               1026                 :                :         }
                               1027                 :                :     }
                               1028                 :                : 
  202                          1029                 :          77904 :     prstate->new_vmbits = VISIBILITYMAP_ALL_VISIBLE;
                               1030                 :                : 
                               1031         [ +  + ]:          77904 :     if (prstate->set_all_frozen)
                               1032                 :          34223 :         prstate->new_vmbits |= VISIBILITYMAP_ALL_FROZEN;
                               1033                 :                : 
                               1034         [ +  + ]:          77904 :     if (prstate->new_vmbits == prstate->old_vmbits)
                               1035                 :                :     {
                               1036                 :           3616 :         prstate->new_vmbits = 0;
                               1037                 :           3616 :         return false;
                               1038                 :                :     }
                               1039                 :                : 
                               1040                 :          74288 :     return true;
                               1041                 :                : }
                               1042                 :                : 
                               1043                 :                : /*
                               1044                 :                :  * If the page is already all-frozen, or already all-visible and freezing
                               1045                 :                :  * won't be attempted, there is no remaining work and we can use the fast path
                               1046                 :                :  * to avoid the expensive overhead of heap_page_prune_and_freeze().
                               1047                 :                :  *
                               1048                 :                :  * This can happen when the page has a stale prune hint, or if VACUUM is
                               1049                 :                :  * scanning an already all-frozen page due to SKIP_PAGES_THRESHOLD.
                               1050                 :                :  *
                               1051                 :                :  * The caller must already have examined the visibility map and saved the
                               1052                 :                :  * status of the page's VM bits in prstate->old_vmbits. Caller must hold a
                               1053                 :                :  * content lock on the heap page since it will examine line pointers.
                               1054                 :                :  *
                               1055                 :                :  * Before calling prune_freeze_fast_path(), the caller should first
                               1056                 :                :  * check for and fix any discrepancy between the page-level visibility hint
                               1057                 :                :  * and the visibility map. Otherwise, the fast path will always prevent us
                               1058                 :                :  * from getting them in sync. Note that if there are tuples on the page that
                               1059                 :                :  * are not visible to all but the VM is incorrectly marked
                               1060                 :                :  * all-visible/all-frozen, we will not get the chance to fix that corruption
                               1061                 :                :  * when using the fast path.
                               1062                 :                :  */
                               1063                 :                : static void
  204                          1064                 :          12990 : prune_freeze_fast_path(PruneState *prstate, PruneFreezeResult *presult)
                               1065                 :                : {
                               1066                 :          12990 :     OffsetNumber maxoff = PageGetMaxOffsetNumber(prstate->page);
                               1067                 :          12990 :     Page        page = prstate->page;
                               1068                 :                : 
                               1069   [ -  +  -  -  :          12990 :     Assert((prstate->old_vmbits & VISIBILITYMAP_ALL_FROZEN) ||
                                              -  - ]
                               1070                 :                :            ((prstate->old_vmbits & VISIBILITYMAP_ALL_VISIBLE) &&
                               1071                 :                :             !prstate->attempt_freeze));
                               1072                 :                : 
                               1073                 :                :     /* We'll fill in presult for the caller */
                               1074                 :          12990 :     memset(presult, 0, sizeof(PruneFreezeResult));
                               1075                 :                : 
                               1076                 :                :     /* Clear any stale prune hint */
                               1077         [ -  + ]:          12990 :     if (TransactionIdIsValid(PageGetPruneXid(page)))
                               1078                 :                :     {
  204 melanieplageman@gmai     1079                 :UBC           0 :         PageClearPrunable(page);
                               1080                 :              0 :         MarkBufferDirtyHint(prstate->buffer, true);
                               1081                 :                :     }
                               1082                 :                : 
  204 melanieplageman@gmai     1083         [ -  + ]:CBC       12990 :     if (PageIsEmpty(page))
  204 melanieplageman@gmai     1084                 :UBC           0 :         return;
                               1085                 :                : 
                               1086                 :                :     /*
                               1087                 :                :      * Since the page is all-visible, a count of the normal ItemIds on the
                               1088                 :                :      * page should be sufficient for vacuum's live tuple count.
                               1089                 :                :      */
  204 melanieplageman@gmai     1090                 :CBC       12990 :     for (OffsetNumber off = FirstOffsetNumber;
                               1091         [ +  + ]:         774917 :          off <= maxoff;
                               1092                 :         761927 :          off = OffsetNumberNext(off))
                               1093                 :                :     {
                               1094                 :         761927 :         ItemId      lp = PageGetItemId(page, off);
                               1095                 :                : 
                               1096         [ +  + ]:         761927 :         if (!ItemIdIsUsed(lp))
                               1097                 :          21962 :             continue;
                               1098                 :                : 
                               1099                 :         739965 :         presult->hastup = true;
                               1100                 :                : 
                               1101         [ +  + ]:         739965 :         if (ItemIdIsNormal(lp))
                               1102                 :         731658 :             prstate->live_tuples++;
                               1103                 :                :     }
                               1104                 :                : 
                               1105                 :          12990 :     presult->live_tuples = prstate->live_tuples;
                               1106                 :                : }
                               1107                 :                : 
                               1108                 :                : /*
                               1109                 :                :  * Prune and repair fragmentation and potentially freeze tuples on the
                               1110                 :                :  * specified page. If the page's visibility status has changed, update it in
                               1111                 :                :  * the VM.
                               1112                 :                :  *
                               1113                 :                :  * Caller must have pin and buffer cleanup lock on the page.  Note that we
                               1114                 :                :  * don't update the FSM information for page on caller's behalf.  Caller might
                               1115                 :                :  * also need to account for a reduction in the length of the line pointer
                               1116                 :                :  * array following array truncation by us.
                               1117                 :                :  *
                               1118                 :                :  * params contains the input parameters used to control freezing and pruning
                               1119                 :                :  * behavior. See the definition of PruneFreezeParams for more on what each
                               1120                 :                :  * parameter does.
                               1121                 :                :  *
                               1122                 :                :  * If the HEAP_PAGE_PRUNE_FREEZE option is set in params, we will freeze
                               1123                 :                :  * tuples if it's required in order to advance relfrozenxid / relminmxid, or
                               1124                 :                :  * if it's considered advantageous for overall system performance to do so
                               1125                 :                :  * now.  The 'params.cutoffs', 'presult', 'new_relfrozen_xid' and
                               1126                 :                :  * 'new_relmin_mxid' arguments are required when freezing.
                               1127                 :                :  *
                               1128                 :                :  * A vmbuffer corresponding to the heap page is also passed and if the page is
                               1129                 :                :  * found to be all-visible/all-frozen, we will set it in the VM.
                               1130                 :                :  *
                               1131                 :                :  * presult contains output parameters needed by callers, such as the number of
                               1132                 :                :  * tuples removed and the offsets of dead items on the page after pruning.
                               1133                 :                :  * heap_page_prune_and_freeze() is responsible for initializing it.  Required
                               1134                 :                :  * by all callers.
                               1135                 :                :  *
                               1136                 :                :  * off_loc is the offset location required by the caller to use in error
                               1137                 :                :  * callback.
                               1138                 :                :  *
                               1139                 :                :  * new_relfrozen_xid and new_relmin_mxid must be provided by the caller if the
                               1140                 :                :  * HEAP_PAGE_PRUNE_FREEZE option is set in params.  On entry, they contain the
                               1141                 :                :  * oldest XID and multi-XID seen on the relation so far.  They will be updated
                               1142                 :                :  * with the oldest values present on the page after pruning.  After processing
                               1143                 :                :  * the whole relation, VACUUM can use these values as the new
                               1144                 :                :  * relfrozenxid/relminmxid for the relation.
                               1145                 :                :  */
                               1146                 :                : void
  320                          1147                 :         223640 : heap_page_prune_and_freeze(PruneFreezeParams *params,
                               1148                 :                :                            PruneFreezeResult *presult,
                               1149                 :                :                            OffsetNumber *off_loc,
                               1150                 :                :                            TransactionId *new_relfrozen_xid,
                               1151                 :                :                            MultiXactId *new_relmin_mxid)
                               1152                 :                : {
                               1153                 :                :     PruneState  prstate;
                               1154                 :                :     bool        do_freeze;
                               1155                 :                :     bool        do_prune;
                               1156                 :                :     bool        do_hint_prune;
                               1157                 :                :     bool        do_set_vm;
                               1158                 :                :     bool        did_tuple_hint_fpi;
                               1159                 :         223640 :     int64       fpi_before = pgWalUsage.wal_fpi;
                               1160                 :                :     TransactionId conflict_xid;
                               1161                 :                : 
                               1162                 :                :     /* Initialize prstate */
                               1163                 :         223640 :     prune_freeze_setup(params,
                               1164                 :                :                        new_relfrozen_xid, new_relmin_mxid,
                               1165                 :                :                        presult, &prstate);
                               1166                 :                : 
                               1167                 :                :     /*
                               1168                 :                :      * If the VM is set but PD_ALL_VISIBLE is clear, fix that corruption
                               1169                 :                :      * before pruning and freezing so that the page and VM start out in a
                               1170                 :                :      * consistent state.
                               1171                 :                :      */
  204                          1172         [ +  + ]:         223640 :     if ((prstate.old_vmbits & VISIBILITYMAP_VALID_BITS) &&
                               1173         [ -  + ]:          20768 :         !PageIsAllVisible(prstate.page))
  204 melanieplageman@gmai     1174                 :UBC           0 :         heap_page_fix_vm_corruption(&prstate, InvalidOffsetNumber,
                               1175                 :                :                                     VM_CORRUPT_MISSING_PAGE_HINT);
                               1176                 :                : 
                               1177                 :                :     /*
                               1178                 :                :      * If the page is already all-frozen, or already all-visible when freezing
                               1179                 :                :      * is not being attempted, take the fast path, skipping pruning and
                               1180                 :                :      * freezing code entirely. This must be done after fixing any discrepancy
                               1181                 :                :      * between the page-level visibility hint and the VM, since that may have
                               1182                 :                :      * cleared old_vmbits.
                               1183                 :                :      */
  204 melanieplageman@gmai     1184         [ +  + ]:CBC      223640 :     if ((params->options & HEAP_PAGE_PRUNE_ALLOW_FAST_PATH) != 0 &&
                               1185         [ +  + ]:         222344 :         ((prstate.old_vmbits & VISIBILITYMAP_ALL_FROZEN) ||
                               1186         [ +  + ]:         209354 :          ((prstate.old_vmbits & VISIBILITYMAP_ALL_VISIBLE) &&
                               1187         [ -  + ]:           7333 :           !prstate.attempt_freeze)))
                               1188                 :                :     {
                               1189                 :          12990 :         prune_freeze_fast_path(&prstate, presult);
                               1190                 :          12990 :         return;
                               1191                 :                :     }
                               1192                 :                : 
                               1193                 :                :     /*
                               1194                 :                :      * Examine all line pointers and tuple visibility information to determine
                               1195                 :                :      * which line pointers should change state and which tuples may be frozen.
                               1196                 :                :      * Prepare queue of state changes to later be executed in a critical
                               1197                 :                :      * section.
                               1198                 :                :      */
  221                          1199                 :         210650 :     prune_freeze_plan(&prstate, off_loc);
                               1200                 :                : 
                               1201                 :                :     /*
                               1202                 :                :      * After processing all the live tuples on the page, if the newest xmin
                               1203                 :                :      * amongst them may be considered running by any snapshot, the page cannot
                               1204                 :                :      * be all-visible. This should be done before determining whether or not
                               1205                 :                :      * to opportunistically freeze.
                               1206                 :                :      */
  202                          1207         [ +  + ]:         210650 :     if (prstate.set_all_visible &&
                               1208   [ +  +  +  + ]:         194528 :         TransactionIdIsNormal(prstate.newest_live_xid) &&
                               1209                 :          83315 :         GlobalVisTestXidConsideredRunning(prstate.vistest,
                               1210                 :                :                                           prstate.newest_live_xid,
                               1211                 :                :                                           true))
                               1212                 :           3653 :         prstate.set_all_visible = prstate.set_all_frozen = false;
                               1213                 :                : 
                               1214                 :                :     /*
                               1215                 :                :      * If checksums are enabled, calling heap_prune_satisfies_vacuum() while
                               1216                 :                :      * checking tuple visibility information in prune_freeze_plan() may have
                               1217                 :                :      * caused an FPI to be emitted.
                               1218                 :                :      */
  320                          1219                 :         210650 :     did_tuple_hint_fpi = fpi_before != pgWalUsage.wal_fpi;
                               1220                 :                : 
  922 heikki.linnakangas@i     1221                 :         613888 :     do_prune = prstate.nredirected > 0 ||
                               1222   [ +  +  +  + ]:         348820 :         prstate.ndead > 0 ||
                               1223         [ +  + ]:         138170 :         prstate.nunused > 0;
                               1224                 :                : 
                               1225                 :                :     /*
                               1226                 :                :      * Even if we don't prune anything, if we found a new value for the
                               1227                 :                :      * pd_prune_xid field or the page was marked full, we will update the hint
                               1228                 :                :      * bit.
                               1229                 :                :      */
  221 melanieplageman@gmai     1230   [ +  +  +  + ]:         244268 :     do_hint_prune = PageGetPruneXid(prstate.page) != prstate.new_prune_xid ||
                               1231                 :          33618 :         PageIsFull(prstate.page);
                               1232                 :                : 
                               1233                 :                :     /*
                               1234                 :                :      * Decide if we want to go ahead with freezing according to the freeze
                               1235                 :                :      * plans we prepared, or not.
                               1236                 :                :      */
                               1237                 :         210650 :     do_freeze = heap_page_will_freeze(did_tuple_hint_fpi,
                               1238                 :                :                                       do_prune,
                               1239                 :                :                                       do_hint_prune,
                               1240                 :                :                                       &prstate);
                               1241                 :                : 
                               1242                 :                :     /*
                               1243                 :                :      * While scanning the line pointers, we did not clear
                               1244                 :                :      * set_all_visible/set_all_frozen when encountering LP_DEAD items because
                               1245                 :                :      * we wanted the decision whether or not to freeze the page to be
                               1246                 :                :      * unaffected by the short-term presence of LP_DEAD items.  These LP_DEAD
                               1247                 :                :      * items are effectively assumed to be LP_UNUSED items in the making.  It
                               1248                 :                :      * doesn't matter which vacuum heap pass (initial pass or final pass) ends
                               1249                 :                :      * up setting the page all-frozen, as long as the ongoing VACUUM does it.
                               1250                 :                :      *
                               1251                 :                :      * Now that we finished determining whether or not to freeze the page,
                               1252                 :                :      * update set_all_visible and set_all_frozen so that they reflect the true
                               1253                 :                :      * state of the page for setting PD_ALL_VISIBLE and VM bits.
                               1254                 :                :      */
  326                          1255         [ +  + ]:         210650 :     if (prstate.lpdead_items > 0)
  221                          1256                 :          77139 :         prstate.set_all_visible = prstate.set_all_frozen = false;
                               1257                 :                : 
                               1258   [ +  +  -  + ]:         210650 :     Assert(!prstate.set_all_frozen || prstate.set_all_visible);
  196                          1259   [ +  +  -  + ]:         210650 :     Assert(!prstate.set_all_visible || prstate.attempt_set_vm);
  202                          1260   [ +  +  -  + ]:         210650 :     Assert(!prstate.set_all_visible || (prstate.lpdead_items == 0));
                               1261                 :                : 
  196                          1262                 :         210650 :     do_set_vm = heap_page_will_set_vm(&prstate, params->reason, do_prune, do_freeze);
                               1263                 :                : 
                               1264                 :                :     /*
                               1265                 :                :      * new_vmbits should be 0 regardless of whether or not the page is
                               1266                 :                :      * all-visible if we do not intend to set the VM.
                               1267                 :                :      */
  202                          1268   [ +  +  -  + ]:         210650 :     Assert(do_set_vm || prstate.new_vmbits == 0);
                               1269                 :                : 
                               1270                 :                :     /*
                               1271                 :                :      * The snapshot conflict horizon for the whole record is the most
                               1272                 :                :      * conservative (newest) horizon required by any change in the record.
                               1273                 :                :      */
                               1274                 :         210650 :     conflict_xid = InvalidTransactionId;
                               1275         [ +  + ]:         210650 :     if (do_set_vm)
                               1276                 :          74288 :         conflict_xid = prstate.newest_live_xid;
                               1277   [ +  +  +  + ]:         210650 :     if (do_freeze && TransactionIdFollows(prstate.pagefrz.FreezePageConflictXid, conflict_xid))
                               1278                 :           2865 :         conflict_xid = prstate.pagefrz.FreezePageConflictXid;
                               1279   [ +  +  +  + ]:         210650 :     if (do_prune && TransactionIdFollows(prstate.latest_xid_removed, conflict_xid))
                               1280                 :          63729 :         conflict_xid = prstate.latest_xid_removed;
                               1281                 :                : 
                               1282                 :                :     /* Lock vmbuffer before entering a critical section */
                               1283         [ +  + ]:         210650 :     if (do_set_vm)
                               1284                 :          74288 :         LockBuffer(prstate.vmbuffer, BUFFER_LOCK_EXCLUSIVE);
                               1285                 :                : 
                               1286                 :                :     /* Any error while applying the changes is critical */
  922 heikki.linnakangas@i     1287                 :         210650 :     START_CRIT_SECTION();
                               1288                 :                : 
  363 melanieplageman@gmai     1289         [ +  + ]:         210650 :     if (do_hint_prune)
                               1290                 :                :     {
                               1291                 :                :         /*
                               1292                 :                :          * Update the page's pd_prune_xid field to either zero, or the lowest
                               1293                 :                :          * XID of any soon-prunable tuple.
                               1294                 :                :          */
  221                          1295                 :         177283 :         ((PageHeader) prstate.page)->pd_prune_xid = prstate.new_prune_xid;
                               1296                 :                : 
                               1297                 :                :         /*
                               1298                 :                :          * Also clear the "page is full" flag, since there's no point in
                               1299                 :                :          * repeating the prune/defrag process until something else happens to
                               1300                 :                :          * the page.
                               1301                 :                :          */
                               1302                 :         177283 :         PageClearFull(prstate.page);
                               1303                 :                : 
                               1304                 :                :         /*
                               1305                 :                :          * If that's all we had to do to the page, this is a non-WAL-logged
                               1306                 :                :          * hint. If we are going to freeze or prune the page or set
                               1307                 :                :          * PD_ALL_VISIBLE, we will mark the buffer dirty below.
                               1308                 :                :          *
                               1309                 :                :          * Setting PD_ALL_VISIBLE is fully WAL-logged because it is forbidden
                               1310                 :                :          * for the VM to be set and PD_ALL_VISIBLE to be clear.
                               1311                 :                :          */
  202                          1312   [ +  +  +  +  :         177283 :         if (!do_freeze && !do_prune && !do_set_vm)
                                              +  + ]
  221                          1313                 :          51610 :             MarkBufferDirtyHint(prstate.buffer, true);
                               1314                 :                :     }
                               1315                 :                : 
  202                          1316   [ +  +  +  +  :         210650 :     if (do_prune || do_freeze || do_set_vm)
                                              +  + ]
                               1317                 :                :     {
                               1318                 :                :         /* Apply the planned item changes and repair page fragmentation. */
  922 heikki.linnakangas@i     1319         [ +  + ]:         146644 :         if (do_prune)
                               1320                 :                :         {
  221 melanieplageman@gmai     1321                 :          72923 :             heap_page_prune_execute(prstate.buffer, false,
                               1322                 :                :                                     prstate.redirected, prstate.nredirected,
                               1323                 :                :                                     prstate.nowdead, prstate.ndead,
                               1324                 :                :                                     prstate.nowunused, prstate.nunused);
                               1325                 :                :         }
                               1326                 :                : 
  922 heikki.linnakangas@i     1327         [ +  + ]:         146644 :         if (do_freeze)
  221 melanieplageman@gmai     1328                 :          25403 :             heap_freeze_prepared_tuples(prstate.buffer, prstate.frozen, prstate.nfrozen);
                               1329                 :                : 
                               1330                 :                :         /* Set the visibility map and page visibility hint */
  202                          1331         [ +  + ]:         146644 :         if (do_set_vm)
                               1332                 :                :         {
                               1333                 :                :             /*
                               1334                 :                :              * While it is valid for PD_ALL_VISIBLE to be set when the
                               1335                 :                :              * corresponding VM bit is clear, we strongly prefer to keep them
                               1336                 :                :              * in sync.
                               1337                 :                :              *
                               1338                 :                :              * The heap buffer must be marked dirty before adding it to the
                               1339                 :                :              * WAL chain when setting the VM. We don't worry about
                               1340                 :                :              * unnecessarily dirtying the heap buffer if PD_ALL_VISIBLE is
                               1341                 :                :              * already set, though. It is extremely rare to have a clean heap
                               1342                 :                :              * buffer with PD_ALL_VISIBLE already set and the VM bits clear,
                               1343                 :                :              * so there is no point in optimizing it.
                               1344                 :                :              */
                               1345                 :          74288 :             PageSetAllVisible(prstate.page);
                               1346                 :          74288 :             PageClearPrunable(prstate.page);
   28                          1347                 :          74288 :             (void) visibilitymap_set(prstate.block, prstate.vmbuffer,
                               1348                 :          74288 :                                      prstate.new_vmbits,
                               1349                 :          74288 :                                      prstate.relation->rd_locator);
                               1350                 :                :         }
                               1351                 :                : 
  221                          1352                 :         146644 :         MarkBufferDirty(prstate.buffer);
                               1353                 :                : 
                               1354                 :                :         /*
                               1355                 :                :          * Emit a WAL XLOG_HEAP2_PRUNE* record showing what we did
                               1356                 :                :          */
                               1357   [ +  +  +  +  :         146644 :         if (RelationNeedsWAL(prstate.relation))
                                        +  -  +  - ]
                               1358                 :                :         {
                               1359   [ +  +  +  + ]:         193042 :             log_heap_prune_and_freeze(prstate.relation, prstate.buffer,
                               1360                 :                :                                       do_set_vm ? prstate.vmbuffer : InvalidBuffer,
  202                          1361                 :          60996 :                                       do_set_vm ? prstate.new_vmbits : 0,
                               1362                 :                :                                       conflict_xid,
                               1363                 :                :                                       do_prune, /* cleanup lock */
                               1364                 :                :                                       params->reason,
                               1365                 :                :                                       prstate.frozen, prstate.nfrozen,
                               1366                 :                :                                       prstate.redirected, prstate.nredirected,
                               1367                 :                :                                       prstate.nowdead, prstate.ndead,
                               1368                 :                :                                       prstate.nowunused, prstate.nunused);
                               1369                 :                :         }
                               1370                 :                :     }
                               1371                 :                : 
 6962 tgl@sss.pgh.pa.us        1372         [ -  + ]:         210650 :     END_CRIT_SECTION();
                               1373                 :                : 
  202 melanieplageman@gmai     1374         [ +  + ]:         210650 :     if (do_set_vm)
                               1375                 :          74288 :         LockBuffer(prstate.vmbuffer, BUFFER_LOCK_UNLOCK);
                               1376                 :                : 
                               1377                 :                :     /*
                               1378                 :                :      * During its second pass over the heap, VACUUM calls
                               1379                 :                :      * heap_page_would_be_all_visible() to determine whether a page is
                               1380                 :                :      * all-visible and all-frozen. The logic here is similar. After completing
                               1381                 :                :      * pruning and freezing, use an assertion to verify that our results
                               1382                 :                :      * remain consistent with heap_page_would_be_all_visible(). It's also a
                               1383                 :                :      * valuable cross-check of the page state after pruning and freezing.
                               1384                 :                :      */
                               1385                 :                : #ifdef USE_ASSERT_CHECKING
                               1386         [ +  + ]:         210650 :     if (prstate.set_all_visible)
                               1387                 :                :     {
                               1388                 :                :         TransactionId debug_cutoff;
                               1389                 :                :         bool        debug_all_frozen;
                               1390                 :                : 
                               1391         [ -  + ]:          77904 :         Assert(prstate.lpdead_items == 0);
                               1392                 :                : 
                               1393         [ -  + ]:          77904 :         Assert(heap_page_is_all_visible(prstate.relation, prstate.buffer,
                               1394                 :                :                                         prstate.vistest,
                               1395                 :                :                                         &debug_all_frozen,
                               1396                 :                :                                         &debug_cutoff, off_loc));
                               1397                 :                : 
                               1398   [ +  +  -  + ]:          77904 :         Assert(!TransactionIdIsValid(debug_cutoff) ||
                               1399                 :                :                debug_cutoff == prstate.newest_live_xid);
                               1400                 :                : 
                               1401                 :                :         /*
                               1402                 :                :          * It's possible the page is composed entirely of frozen tuples but is
                               1403                 :                :          * not set all-frozen in the VM and did not pass
                               1404                 :                :          * HEAP_PAGE_PRUNE_FREEZE. In this case, it's possible
                               1405                 :                :          * heap_page_is_all_visible() finds the page completely frozen, even
                               1406                 :                :          * though prstate.set_all_frozen is false.
                               1407                 :                :          */
                               1408   [ +  +  -  + ]:          77904 :         Assert(!prstate.set_all_frozen || debug_all_frozen);
                               1409                 :                :     }
                               1410                 :                : #endif
                               1411                 :                : 
                               1412                 :                :     /* Copy information back for caller */
  924 heikki.linnakangas@i     1413                 :         210650 :     presult->ndeleted = prstate.ndeleted;
  922                          1414                 :         210650 :     presult->nnewlpdead = prstate.ndead;
                               1415                 :         210650 :     presult->nfrozen = prstate.nfrozen;
                               1416                 :         210650 :     presult->live_tuples = prstate.live_tuples;
                               1417                 :         210650 :     presult->recently_dead_tuples = prstate.recently_dead_tuples;
                               1418                 :         210650 :     presult->hastup = prstate.hastup;
                               1419                 :                : 
                               1420                 :         210650 :     presult->lpdead_items = prstate.lpdead_items;
                               1421                 :                :     /* the presult->deadoffsets array was already filled in */
                               1422                 :                : 
  202 melanieplageman@gmai     1423                 :         210650 :     presult->newly_all_visible = false;
                               1424                 :         210650 :     presult->newly_all_frozen = false;
                               1425                 :         210650 :     presult->newly_all_visible_frozen = false;
                               1426         [ +  + ]:         210650 :     if (do_set_vm)
                               1427                 :                :     {
                               1428         [ +  + ]:          74288 :         if ((prstate.old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0)
                               1429                 :                :         {
                               1430                 :          70126 :             presult->newly_all_visible = true;
                               1431         [ +  + ]:          70126 :             if (prstate.set_all_frozen)
                               1432                 :          29617 :                 presult->newly_all_visible_frozen = true;
                               1433                 :                :         }
                               1434         [ +  - ]:           4162 :         else if ((prstate.old_vmbits & VISIBILITYMAP_ALL_FROZEN) == 0 &&
                               1435         [ +  - ]:           4162 :                  prstate.set_all_frozen)
                               1436                 :           4162 :             presult->newly_all_frozen = true;
                               1437                 :                :     }
                               1438                 :                : 
  363                          1439         [ +  + ]:         210650 :     if (prstate.attempt_freeze)
                               1440                 :                :     {
  922 heikki.linnakangas@i     1441         [ +  + ]:          79828 :         if (presult->nfrozen > 0)
                               1442                 :                :         {
                               1443                 :          25403 :             *new_relfrozen_xid = prstate.pagefrz.FreezePageRelfrozenXid;
                               1444                 :          25403 :             *new_relmin_mxid = prstate.pagefrz.FreezePageRelminMxid;
                               1445                 :                :         }
                               1446                 :                :         else
                               1447                 :                :         {
                               1448                 :          54425 :             *new_relfrozen_xid = prstate.pagefrz.NoFreezePageRelfrozenXid;
                               1449                 :          54425 :             *new_relmin_mxid = prstate.pagefrz.NoFreezePageRelminMxid;
                               1450                 :                :         }
                               1451                 :                :     }
                               1452                 :                : }
                               1453                 :                : 
                               1454                 :                : 
                               1455                 :                : /*
                               1456                 :                :  * Perform visibility checks for heap pruning.
                               1457                 :                :  */
                               1458                 :                : static HTSV_Result
  221 melanieplageman@gmai     1459                 :       19077056 : heap_prune_satisfies_vacuum(PruneState *prstate, HeapTuple tup)
                               1460                 :                : {
                               1461                 :                :     HTSV_Result res;
                               1462                 :                :     TransactionId dead_after;
                               1463                 :                : 
                               1464                 :       19077056 :     res = HeapTupleSatisfiesVacuumHorizon(tup, prstate->buffer, &dead_after);
                               1465                 :                : 
 2252 andres@anarazel.de       1466         [ +  + ]:       19077056 :     if (res != HEAPTUPLE_RECENTLY_DEAD)
                               1467                 :       14776103 :         return res;
                               1468                 :                : 
                               1469                 :                :     /*
                               1470                 :                :      * For VACUUM, we must be sure to prune tuples with xmax older than
                               1471                 :                :      * OldestXmin -- a visibility cutoff determined at the beginning of
                               1472                 :                :      * vacuuming the relation. OldestXmin is used for freezing determination
                               1473                 :                :      * and we cannot freeze dead tuples' xmaxes.
                               1474                 :                :      */
  815 melanieplageman@gmai     1475         [ +  + ]:        4300953 :     if (prstate->cutoffs &&
                               1476         [ +  - ]:        1362107 :         TransactionIdIsValid(prstate->cutoffs->OldestXmin) &&
                               1477   [ +  -  -  +  :        1362107 :         NormalTransactionIdPrecedes(dead_after, prstate->cutoffs->OldestXmin))
                                              +  + ]
                               1478                 :         970224 :         return HEAPTUPLE_DEAD;
                               1479                 :                : 
                               1480                 :                :     /*
                               1481                 :                :      * Determine whether or not the tuple is considered dead when compared
                               1482                 :                :      * with the provided GlobalVisState. On-access pruning does not provide
                               1483                 :                :      * VacuumCutoffs. And for vacuum, even if the tuple's xmax is not older
                               1484                 :                :      * than OldestXmin, GlobalVisTestIsRemovableXid() could find the row dead
                               1485                 :                :      * if the GlobalVisState has been updated since the beginning of vacuuming
                               1486                 :                :      * the relation.
                               1487                 :                :      */
  202                          1488         [ +  + ]:        3330729 :     if (GlobalVisTestIsRemovableXid(prstate->vistest, dead_after, true))
  815                          1489                 :        2886810 :         return HEAPTUPLE_DEAD;
                               1490                 :                : 
 2252 andres@anarazel.de       1491                 :         443919 :     return res;
                               1492                 :                : }
                               1493                 :                : 
                               1494                 :                : 
                               1495                 :                : /*
                               1496                 :                :  * Pruning calculates tuple visibility once and saves the results in an array
                               1497                 :                :  * of int8.  See PruneState.htsv for details.  This helper function is meant
                               1498                 :                :  * to guard against examining visibility status array members which have not
                               1499                 :                :  * yet been computed.
                               1500                 :                :  */
                               1501                 :                : static inline HTSV_Result
  922 heikki.linnakangas@i     1502                 :       19060878 : htsv_get_valid_status(int status)
                               1503                 :                : {
                               1504   [ +  -  -  + ]:       19060878 :     Assert(status >= HEAPTUPLE_DEAD &&
                               1505                 :                :            status <= HEAPTUPLE_DELETE_IN_PROGRESS);
                               1506                 :       19060878 :     return (HTSV_Result) status;
                               1507                 :                : }
                               1508                 :                : 
                               1509                 :                : /*
                               1510                 :                :  * Prune specified line pointer or a HOT chain originating at line pointer.
                               1511                 :                :  *
                               1512                 :                :  * Tuple visibility information is provided in prstate->htsv.
                               1513                 :                :  *
                               1514                 :                :  * If the item is an index-referenced tuple (i.e. not a heap-only tuple),
                               1515                 :                :  * the HOT chain is pruned by removing all DEAD tuples at the start of the HOT
                               1516                 :                :  * chain.  We also prune any RECENTLY_DEAD tuples preceding a DEAD tuple.
                               1517                 :                :  * This is OK because a RECENTLY_DEAD tuple preceding a DEAD tuple is really
                               1518                 :                :  * DEAD, our visibility test is just too coarse to detect it.
                               1519                 :                :  *
                               1520                 :                :  * Pruning must never leave behind a DEAD tuple that still has tuple storage.
                               1521                 :                :  * VACUUM isn't prepared to deal with that case.
                               1522                 :                :  *
                               1523                 :                :  * The root line pointer is redirected to the tuple immediately after the
                               1524                 :                :  * latest DEAD tuple.  If all tuples in the chain are DEAD, the root line
                               1525                 :                :  * pointer is marked LP_DEAD.  (This includes the case of a DEAD simple
                               1526                 :                :  * tuple, which we treat as a chain of length 1.)
                               1527                 :                :  *
                               1528                 :                :  * We don't actually change the page here. We just add entries to the arrays in
                               1529                 :                :  * prstate showing the changes to be made.  Items to be redirected are added
                               1530                 :                :  * to the redirected[] array (two entries per redirection); items to be set to
                               1531                 :                :  * LP_DEAD state are added to nowdead[]; and items to be set to LP_UNUSED
                               1532                 :                :  * state are added to nowunused[].  We perform bookkeeping of live tuples,
                               1533                 :                :  * visibility etc. based on what the page will look like after the changes
                               1534                 :                :  * applied.  All that bookkeeping is performed in the heap_prune_record_*()
                               1535                 :                :  * subroutines.  The division of labor is that heap_prune_chain() decides the
                               1536                 :                :  * fate of each tuple, ie. whether it's going to be removed, redirected or
                               1537                 :                :  * left unchanged, and the heap_prune_record_*() subroutines update PruneState
                               1538                 :                :  * based on that outcome.
                               1539                 :                :  */
                               1540                 :                : static void
  221 melanieplageman@gmai     1541                 :       18931107 : heap_prune_chain(OffsetNumber maxoff, OffsetNumber rootoffnum,
                               1542                 :                :                  PruneState *prstate)
                               1543                 :                : {
 6906 bruce@momjian.us         1544                 :       18931107 :     TransactionId priorXmax = InvalidTransactionId;
                               1545                 :                :     ItemId      rootlp;
                               1546                 :                :     OffsetNumber offnum;
                               1547                 :                :     OffsetNumber chainitems[MaxHeapTuplesPerPage];
  221 melanieplageman@gmai     1548                 :       18931107 :     Page        page = prstate->page;
                               1549                 :                : 
                               1550                 :                :     /*
                               1551                 :                :      * After traversing the HOT chain, ndeadchain is the index in chainitems
                               1552                 :                :      * of the first live successor after the last dead item.
                               1553                 :                :      */
  924 heikki.linnakangas@i     1554                 :       18931107 :     int         ndeadchain = 0,
                               1555                 :       18931107 :                 nchain = 0;
                               1556                 :                : 
                               1557                 :       18931107 :     rootlp = PageGetItemId(page, rootoffnum);
                               1558                 :                : 
                               1559                 :                :     /* Start from the root tuple */
 6962 tgl@sss.pgh.pa.us        1560                 :       18931107 :     offnum = rootoffnum;
                               1561                 :                : 
                               1562                 :                :     /* while not end of the chain */
                               1563                 :                :     for (;;)
                               1564                 :         339925 :     {
                               1565                 :                :         HeapTupleHeader htup;
                               1566                 :                :         ItemId      lp;
                               1567                 :                : 
                               1568                 :                :         /* Sanity check (pure paranoia) */
 1846 pg@bowt.ie               1569         [ -  + ]:       19271032 :         if (offnum < FirstOffsetNumber)
 1846 pg@bowt.ie               1570                 :UBC           0 :             break;
                               1571                 :                : 
                               1572                 :                :         /*
                               1573                 :                :          * An offset past the end of page's line pointer array is possible
                               1574                 :                :          * when the array was truncated (original item must have been unused)
                               1575                 :                :          */
 1846 pg@bowt.ie               1576         [ -  + ]:CBC    19271032 :         if (offnum > maxoff)
 6962 tgl@sss.pgh.pa.us        1577                 :UBC           0 :             break;
                               1578                 :                : 
                               1579                 :                :         /* If item is already processed, stop --- it must not be same chain */
  924 heikki.linnakangas@i     1580         [ -  + ]:CBC    19271032 :         if (prstate->processed[offnum])
 6792 tgl@sss.pgh.pa.us        1581                 :UBC           0 :             break;
                               1582                 :                : 
  924 heikki.linnakangas@i     1583                 :CBC    19271032 :         lp = PageGetItemId(page, offnum);
                               1584                 :                : 
                               1585                 :                :         /*
                               1586                 :                :          * Unused item obviously isn't part of the chain. Likewise, a dead
                               1587                 :                :          * line pointer can't be part of the chain.  Both of those cases were
                               1588                 :                :          * already marked as processed.
                               1589                 :                :          */
                               1590         [ -  + ]:       19271032 :         Assert(ItemIdIsUsed(lp));
                               1591         [ -  + ]:       19271032 :         Assert(!ItemIdIsDead(lp));
                               1592                 :                : 
                               1593                 :                :         /*
                               1594                 :                :          * If we are looking at the redirected root line pointer, jump to the
                               1595                 :                :          * first normal tuple in the chain.  If we find a redirect somewhere
                               1596                 :                :          * else, stop --- it must not be same chain.
                               1597                 :                :          */
 6962 tgl@sss.pgh.pa.us        1598         [ +  + ]:       19271032 :         if (ItemIdIsRedirected(lp))
                               1599                 :                :         {
                               1600         [ -  + ]:         210154 :             if (nchain > 0)
 6962 tgl@sss.pgh.pa.us        1601                 :UBC           0 :                 break;          /* not at start of chain */
 6962 tgl@sss.pgh.pa.us        1602                 :CBC      210154 :             chainitems[nchain++] = offnum;
                               1603                 :         210154 :             offnum = ItemIdGetRedirect(rootlp);
                               1604                 :         210154 :             continue;
                               1605                 :                :         }
                               1606                 :                : 
                               1607         [ -  + ]:       19060878 :         Assert(ItemIdIsNormal(lp));
                               1608                 :                : 
  924 heikki.linnakangas@i     1609                 :       19060878 :         htup = (HeapTupleHeader) PageGetItem(page, lp);
                               1610                 :                : 
                               1611                 :                :         /*
                               1612                 :                :          * Check the tuple XMIN against prior XMAX, if any
                               1613                 :                :          */
 6962 tgl@sss.pgh.pa.us        1614   [ +  +  -  + ]:       19190649 :         if (TransactionIdIsValid(priorXmax) &&
 3266 alvherre@alvh.no-ip.     1615                 :         129771 :             !TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
 6962 tgl@sss.pgh.pa.us        1616                 :UBC           0 :             break;
                               1617                 :                : 
                               1618                 :                :         /*
                               1619                 :                :          * OK, this tuple is indeed a member of the chain.
                               1620                 :                :          */
 6962 tgl@sss.pgh.pa.us        1621                 :CBC    19060878 :         chainitems[nchain++] = offnum;
                               1622                 :                : 
  922 heikki.linnakangas@i     1623   [ +  +  +  - ]:       19060878 :         switch (htsv_get_valid_status(prstate->htsv[offnum]))
                               1624                 :                :         {
 6962 tgl@sss.pgh.pa.us        1625                 :        3930654 :             case HEAPTUPLE_DEAD:
                               1626                 :                : 
                               1627                 :                :                 /* Remember the last DEAD tuple seen */
  924 heikki.linnakangas@i     1628                 :        3930654 :                 ndeadchain = nchain;
                               1629                 :        3930654 :                 HeapTupleHeaderAdvanceConflictHorizon(htup,
                               1630                 :                :                                                       &prstate->latest_xid_removed);
                               1631                 :                :                 /* Advance to next chain member */
 6962 tgl@sss.pgh.pa.us        1632                 :        3930654 :                 break;
                               1633                 :                : 
                               1634                 :         443919 :             case HEAPTUPLE_RECENTLY_DEAD:
                               1635                 :                : 
                               1636                 :                :                 /*
                               1637                 :                :                  * We don't need to advance the conflict horizon for
                               1638                 :                :                  * RECENTLY_DEAD tuples, even if we are removing them.  This
                               1639                 :                :                  * is because we only remove RECENTLY_DEAD tuples if they
                               1640                 :                :                  * precede a DEAD tuple, and the DEAD tuple must have been
                               1641                 :                :                  * inserted by a newer transaction than the RECENTLY_DEAD
                               1642                 :                :                  * tuple by virtue of being later in the chain.  We will have
                               1643                 :                :                  * advanced the conflict horizon for the DEAD tuple.
                               1644                 :                :                  */
                               1645                 :                : 
                               1646                 :                :                 /*
                               1647                 :                :                  * Advance past RECENTLY_DEAD tuples just in case there's a
                               1648                 :                :                  * DEAD one after them.  We have to make sure that we don't
                               1649                 :                :                  * miss any DEAD tuples, since DEAD tuples that still have
                               1650                 :                :                  * tuple storage after pruning will confuse VACUUM.
                               1651                 :                :                  */
                               1652                 :         443919 :                 break;
                               1653                 :                : 
                               1654                 :       14686305 :             case HEAPTUPLE_DELETE_IN_PROGRESS:
                               1655                 :                :             case HEAPTUPLE_LIVE:
                               1656                 :                :             case HEAPTUPLE_INSERT_IN_PROGRESS:
  924 heikki.linnakangas@i     1657                 :       14686305 :                 goto process_chain;
                               1658                 :                : 
 6962 tgl@sss.pgh.pa.us        1659                 :UBC           0 :             default:
                               1660         [ #  # ]:              0 :                 elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
                               1661                 :                :                 goto process_chain;
                               1662                 :                :         }
                               1663                 :                : 
                               1664                 :                :         /*
                               1665                 :                :          * If the tuple is not HOT-updated, then we are at the end of this
                               1666                 :                :          * HOT-update chain.
                               1667                 :                :          */
 6962 tgl@sss.pgh.pa.us        1668         [ +  + ]:CBC     4374573 :         if (!HeapTupleHeaderIsHotUpdated(htup))
  924 heikki.linnakangas@i     1669                 :        4244802 :             goto process_chain;
                               1670                 :                : 
                               1671                 :                :         /* HOT implies it can't have moved to different partition */
 3110 andres@anarazel.de       1672         [ -  + ]:         129771 :         Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
                               1673                 :                : 
                               1674                 :                :         /*
                               1675                 :                :          * Advance to next chain member.
                               1676                 :                :          */
  221 melanieplageman@gmai     1677         [ -  + ]:         129771 :         Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == prstate->block);
 6962 tgl@sss.pgh.pa.us        1678                 :         129771 :         offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 5010 alvherre@alvh.no-ip.     1679                 :         129771 :         priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               1680                 :                :     }
                               1681                 :                : 
  924 heikki.linnakangas@i     1682   [ #  #  #  # ]:UBC           0 :     if (ItemIdIsRedirected(rootlp) && nchain < 2)
                               1683                 :                :     {
                               1684                 :                :         /*
                               1685                 :                :          * We found a redirect item that doesn't point to a valid follow-on
                               1686                 :                :          * item.  This can happen if the loop in heap_page_prune_and_freeze()
                               1687                 :                :          * caused us to visit the dead successor of a redirect item before
                               1688                 :                :          * visiting the redirect item.  We can clean up by setting the
                               1689                 :                :          * redirect item to LP_DEAD state or LP_UNUSED if the caller
                               1690                 :                :          * indicated.
                               1691                 :                :          */
                               1692                 :              0 :         heap_prune_record_dead_or_unused(prstate, rootoffnum, false);
                               1693                 :              0 :         return;
                               1694                 :                :     }
                               1695                 :                : 
                               1696                 :              0 : process_chain:
                               1697                 :                : 
  924 heikki.linnakangas@i     1698         [ +  + ]:CBC    18931107 :     if (ndeadchain == 0)
                               1699                 :                :     {
                               1700                 :                :         /*
                               1701                 :                :          * No DEAD tuple was found, so the chain is entirely composed of
                               1702                 :                :          * normal, unchanged tuples.  Leave it alone.
                               1703                 :                :          */
  922                          1704                 :       15045449 :         int         i = 0;
                               1705                 :                : 
                               1706         [ +  + ]:       15045449 :         if (ItemIdIsRedirected(rootlp))
                               1707                 :                :         {
                               1708                 :         187561 :             heap_prune_record_unchanged_lp_redirect(prstate, rootoffnum);
                               1709                 :         187561 :             i++;
                               1710                 :                :         }
                               1711         [ +  + ]:       30099470 :         for (; i < nchain; i++)
  221 melanieplageman@gmai     1712                 :       15054021 :             heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
                               1713                 :                :     }
  924 heikki.linnakangas@i     1714         [ +  + ]:        3885658 :     else if (ndeadchain == nchain)
                               1715                 :                :     {
                               1716                 :                :         /*
                               1717                 :                :          * The entire chain is dead.  Mark the root line pointer LP_DEAD, and
                               1718                 :                :          * fully remove the other tuples in the chain.
                               1719                 :                :          */
                               1720                 :        3812458 :         heap_prune_record_dead_or_unused(prstate, rootoffnum, ItemIdIsNormal(rootlp));
                               1721         [ +  + ]:        3857207 :         for (int i = 1; i < nchain; i++)
                               1722                 :          44749 :             heap_prune_record_unused(prstate, chainitems[i], true);
                               1723                 :                :     }
                               1724                 :                :     else
                               1725                 :                :     {
                               1726                 :                :         /*
                               1727                 :                :          * We found a DEAD tuple in the chain.  Redirect the root line pointer
                               1728                 :                :          * to the first non-DEAD tuple, and mark as unused each intermediate
                               1729                 :                :          * item that we are able to remove from the chain.
                               1730                 :                :          */
                               1731                 :          73200 :         heap_prune_record_redirect(prstate, rootoffnum, chainitems[ndeadchain],
                               1732                 :          73200 :                                    ItemIdIsNormal(rootlp));
                               1733         [ +  + ]:          96040 :         for (int i = 1; i < ndeadchain; i++)
                               1734                 :          22840 :             heap_prune_record_unused(prstate, chainitems[i], true);
                               1735                 :                : 
                               1736                 :                :         /* the rest of tuples in the chain are normal, unchanged tuples */
                               1737         [ +  + ]:         149403 :         for (int i = ndeadchain; i < nchain; i++)
  221 melanieplageman@gmai     1738                 :          76203 :             heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
                               1739                 :                :     }
                               1740                 :                : }
                               1741                 :                : 
                               1742                 :                : /* Record lowest soon-prunable XID */
                               1743                 :                : static void
  204                          1744                 :         610131 : heap_prune_record_prunable(PruneState *prstate, TransactionId xid,
                               1745                 :                :                            OffsetNumber offnum)
                               1746                 :                : {
                               1747                 :                :     /*
                               1748                 :                :      * This should exactly match the PageSetPrunable macro.  We can't store
                               1749                 :                :      * directly into the page header yet, so we update working state.
                               1750                 :                :      */
 6792 tgl@sss.pgh.pa.us        1751         [ -  + ]:         610131 :     Assert(TransactionIdIsNormal(xid));
                               1752   [ +  +  +  + ]:        1186124 :     if (!TransactionIdIsValid(prstate->new_prune_xid) ||
                               1753                 :         575993 :         TransactionIdPrecedes(xid, prstate->new_prune_xid))
                               1754                 :          36209 :         prstate->new_prune_xid = xid;
                               1755                 :                : 
                               1756                 :                :     /*
                               1757                 :                :      * It's incorrect for a page to be marked all-visible if it contains
                               1758                 :                :      * prunable items.
                               1759                 :                :      */
  204 melanieplageman@gmai     1760         [ -  + ]:         610131 :     if (PageIsAllVisible(prstate->page))
  204 melanieplageman@gmai     1761                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum,
                               1762                 :                :                                     VM_CORRUPT_TUPLE_VISIBILITY);
 6792 tgl@sss.pgh.pa.us        1763                 :CBC      610131 : }
                               1764                 :                : 
                               1765                 :                : /* Record line pointer to be redirected */
                               1766                 :                : static void
                               1767                 :          73200 : heap_prune_record_redirect(PruneState *prstate,
                               1768                 :                :                            OffsetNumber offnum, OffsetNumber rdoffnum,
                               1769                 :                :                            bool was_normal)
                               1770                 :                : {
  924 heikki.linnakangas@i     1771         [ -  + ]:          73200 :     Assert(!prstate->processed[offnum]);
                               1772                 :          73200 :     prstate->processed[offnum] = true;
                               1773                 :                : 
                               1774                 :                :     /*
                               1775                 :                :      * Do not mark the redirect target here.  It needs to be counted
                               1776                 :                :      * separately as an unchanged tuple.
                               1777                 :                :      */
                               1778                 :                : 
 6792 tgl@sss.pgh.pa.us        1779         [ -  + ]:          73200 :     Assert(prstate->nredirected < MaxHeapTuplesPerPage);
                               1780                 :          73200 :     prstate->redirected[prstate->nredirected * 2] = offnum;
                               1781                 :          73200 :     prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
                               1782                 :                : 
                               1783                 :          73200 :     prstate->nredirected++;
                               1784                 :                : 
                               1785                 :                :     /*
                               1786                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1787                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1788                 :                :      * count.
                               1789                 :                :      */
  924 heikki.linnakangas@i     1790         [ +  + ]:          73200 :     if (was_normal)
                               1791                 :          63818 :         prstate->ndeleted++;
                               1792                 :                : 
  922                          1793                 :          73200 :     prstate->hastup = true;
 6962 tgl@sss.pgh.pa.us        1794                 :          73200 : }
                               1795                 :                : 
                               1796                 :                : /* Record line pointer to be marked dead */
                               1797                 :                : static void
  924 heikki.linnakangas@i     1798                 :        3777973 : heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum,
                               1799                 :                :                        bool was_normal)
                               1800                 :                : {
                               1801         [ -  + ]:        3777973 :     Assert(!prstate->processed[offnum]);
                               1802                 :        3777973 :     prstate->processed[offnum] = true;
                               1803                 :                : 
 6792 tgl@sss.pgh.pa.us        1804         [ -  + ]:        3777973 :     Assert(prstate->ndead < MaxHeapTuplesPerPage);
                               1805                 :        3777973 :     prstate->nowdead[prstate->ndead] = offnum;
                               1806                 :        3777973 :     prstate->ndead++;
                               1807                 :                : 
                               1808                 :                :     /*
                               1809                 :                :      * Deliberately delay unsetting set_all_visible and set_all_frozen until
                               1810                 :                :      * later during pruning. Removable dead tuples shouldn't preclude freezing
                               1811                 :                :      * the page.
                               1812                 :                :      */
                               1813                 :                : 
                               1814                 :                :     /* Record the dead offset for vacuum */
  922 heikki.linnakangas@i     1815                 :        3777973 :     prstate->deadoffsets[prstate->lpdead_items++] = offnum;
                               1816                 :                : 
                               1817                 :                :     /*
                               1818                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1819                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1820                 :                :      * count.
                               1821                 :                :      */
  924                          1822         [ +  + ]:        3777973 :     if (was_normal)
                               1823                 :        3764762 :         prstate->ndeleted++;
 6962 tgl@sss.pgh.pa.us        1824                 :        3777973 : }
                               1825                 :                : 
                               1826                 :                : /*
                               1827                 :                :  * Depending on whether or not the caller set mark_unused_now to true, record that a
                               1828                 :                :  * line pointer should be marked LP_DEAD or LP_UNUSED. There are other cases in
                               1829                 :                :  * which we will mark line pointers LP_UNUSED, but we will not mark line
                               1830                 :                :  * pointers LP_DEAD if mark_unused_now is true.
                               1831                 :                :  */
                               1832                 :                : static void
  924 heikki.linnakangas@i     1833                 :        3812458 : heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum,
                               1834                 :                :                                  bool was_normal)
                               1835                 :                : {
                               1836                 :                :     /*
                               1837                 :                :      * If the caller set mark_unused_now to true, we can remove dead tuples
                               1838                 :                :      * during pruning instead of marking their line pointers dead. Set this
                               1839                 :                :      * tuple's line pointer LP_UNUSED. We hint that this option is less
                               1840                 :                :      * likely.
                               1841                 :                :      */
  998 rhaas@postgresql.org     1842         [ +  + ]:        3812458 :     if (unlikely(prstate->mark_unused_now))
  924 heikki.linnakangas@i     1843                 :          34485 :         heap_prune_record_unused(prstate, offnum, was_normal);
                               1844                 :                :     else
                               1845                 :        3777973 :         heap_prune_record_dead(prstate, offnum, was_normal);
                               1846                 :                : 
                               1847                 :                :     /*
                               1848                 :                :      * It's incorrect for the page to be set all-visible if it contains dead
                               1849                 :                :      * items. Fix that on the heap page and check the VM for corruption as
                               1850                 :                :      * well. Do that here rather than in heap_prune_record_dead() so we also
                               1851                 :                :      * cover tuples that are directly marked LP_UNUSED via mark_unused_now.
                               1852                 :                :      */
  204 melanieplageman@gmai     1853         [ -  + ]:        3812458 :     if (PageIsAllVisible(prstate->page))
  204 melanieplageman@gmai     1854                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum, VM_CORRUPT_LPDEAD);
  998 rhaas@postgresql.org     1855                 :CBC     3812458 : }
                               1856                 :                : 
                               1857                 :                : /* Record line pointer to be marked unused */
                               1858                 :                : static void
  924 heikki.linnakangas@i     1859                 :         106963 : heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum, bool was_normal)
                               1860                 :                : {
                               1861         [ -  + ]:         106963 :     Assert(!prstate->processed[offnum]);
                               1862                 :         106963 :     prstate->processed[offnum] = true;
                               1863                 :                : 
 6792 tgl@sss.pgh.pa.us        1864         [ -  + ]:         106963 :     Assert(prstate->nunused < MaxHeapTuplesPerPage);
                               1865                 :         106963 :     prstate->nowunused[prstate->nunused] = offnum;
                               1866                 :         106963 :     prstate->nunused++;
                               1867                 :                : 
                               1868                 :                :     /*
                               1869                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1870                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1871                 :                :      * count.
                               1872                 :                :      */
  924 heikki.linnakangas@i     1873         [ +  + ]:         106963 :     if (was_normal)
                               1874                 :         105562 :         prstate->ndeleted++;
 6792 tgl@sss.pgh.pa.us        1875                 :         106963 : }
                               1876                 :                : 
                               1877                 :                : /*
                               1878                 :                :  * Record an unused line pointer that is left unchanged.
                               1879                 :                :  */
                               1880                 :                : static void
  221 melanieplageman@gmai     1881                 :         356899 : heap_prune_record_unchanged_lp_unused(PruneState *prstate, OffsetNumber offnum)
                               1882                 :                : {
  922 heikki.linnakangas@i     1883         [ -  + ]:         356899 :     Assert(!prstate->processed[offnum]);
                               1884                 :         356899 :     prstate->processed[offnum] = true;
                               1885                 :         356899 : }
                               1886                 :                : 
                               1887                 :                : /*
                               1888                 :                :  * Record line pointer that is left unchanged.  We consider freezing it, and
                               1889                 :                :  * update bookkeeping of tuple counts and page visibility.
                               1890                 :                :  */
                               1891                 :                : static void
  221 melanieplageman@gmai     1892                 :       15142914 : heap_prune_record_unchanged_lp_normal(PruneState *prstate, OffsetNumber offnum)
                               1893                 :                : {
                               1894                 :                :     HeapTupleHeader htup;
                               1895                 :                :     TransactionId xmin;
                               1896                 :       15142914 :     Page        page = prstate->page;
                               1897                 :                : 
  922 heikki.linnakangas@i     1898         [ -  + ]:       15142914 :     Assert(!prstate->processed[offnum]);
                               1899                 :       15142914 :     prstate->processed[offnum] = true;
                               1900                 :                : 
                               1901                 :       15142914 :     prstate->hastup = true;      /* the page is not empty */
                               1902                 :                : 
                               1903                 :                :     /*
                               1904                 :                :      * The criteria for counting a tuple as live in this block need to match
                               1905                 :                :      * what analyze.c's acquire_sample_rows() does, otherwise VACUUM and
                               1906                 :                :      * ANALYZE may produce wildly different reltuples values, e.g. when there
                               1907                 :                :      * are many recently-dead tuples.
                               1908                 :                :      *
                               1909                 :                :      * The logic here is a bit simpler than acquire_sample_rows(), as VACUUM
                               1910                 :                :      * can't run inside a transaction block, which makes some cases impossible
                               1911                 :                :      * (e.g. in-progress insert from the same transaction).
                               1912                 :                :      *
                               1913                 :                :      * HEAPTUPLE_DEAD are handled by the other heap_prune_record_*()
                               1914                 :                :      * subroutines.  They don't count dead items like acquire_sample_rows()
                               1915                 :                :      * does, because we assume that all dead items will become LP_UNUSED
                               1916                 :                :      * before VACUUM finishes.  This difference is only superficial.  VACUUM
                               1917                 :                :      * effectively agrees with ANALYZE about DEAD items, in the end.  VACUUM
                               1918                 :                :      * won't remember LP_DEAD items, but only because they're not supposed to
                               1919                 :                :      * be left behind when it is done. (Cases where we bypass index vacuuming
                               1920                 :                :      * will violate this optimistic assumption, but the overall impact of that
                               1921                 :                :      * should be negligible.)
                               1922                 :                :      */
                               1923                 :       15142914 :     htup = (HeapTupleHeader) PageGetItem(page, PageGetItemId(page, offnum));
                               1924                 :                : 
                               1925   [ +  +  +  +  :       15142914 :     switch (prstate->htsv[offnum])
                                                 - ]
                               1926                 :                :     {
                               1927                 :       14532783 :         case HEAPTUPLE_LIVE:
                               1928                 :                : 
                               1929                 :                :             /*
                               1930                 :                :              * Count it as live.  Not only is this natural, but it's also what
                               1931                 :                :              * acquire_sample_rows() does.
                               1932                 :                :              */
                               1933                 :       14532783 :             prstate->live_tuples++;
                               1934                 :                : 
                               1935                 :                :             /*
                               1936                 :                :              * Is the tuple definitely visible to all transactions?
                               1937                 :                :              *
                               1938                 :                :              * NB: Like with per-tuple hint bits, we can't set the
                               1939                 :                :              * PD_ALL_VISIBLE flag if the inserter committed asynchronously.
                               1940                 :                :              * See SetHintBits for more info.  Check that the tuple is hinted
                               1941                 :                :              * xmin-committed because of that.
                               1942                 :                :              */
  202 melanieplageman@gmai     1943         [ +  + ]:       14532783 :             if (!HeapTupleHeaderXminCommitted(htup))
                               1944                 :                :             {
                               1945                 :          40274 :                 prstate->set_all_visible = false;
                               1946                 :          40274 :                 prstate->set_all_frozen = false;
                               1947                 :          40274 :                 break;
                               1948                 :                :             }
                               1949                 :                : 
                               1950                 :                :             /*
                               1951                 :                :              * The inserter definitely committed. But we don't know if it is
                               1952                 :                :              * old enough that everyone sees it as committed. Later, after
                               1953                 :                :              * processing all the tuples on the page, we'll check if there is
                               1954                 :                :              * any snapshot that still considers the newest xid on the page to
                               1955                 :                :              * be running. If so, we don't consider the page all-visible.
                               1956                 :                :              */
                               1957                 :       14492509 :             xmin = HeapTupleHeaderGetXmin(htup);
                               1958                 :                : 
                               1959                 :                :             /* Track newest xmin on page. */
                               1960   [ +  +  +  + ]:       14492509 :             if (TransactionIdFollows(xmin, prstate->newest_live_xid) &&
                               1961                 :                :                 TransactionIdIsNormal(xmin))
                               1962                 :         623440 :                 prstate->newest_live_xid = xmin;
                               1963                 :                : 
  922 heikki.linnakangas@i     1964                 :       14492509 :             break;
                               1965                 :                : 
                               1966                 :         443919 :         case HEAPTUPLE_RECENTLY_DEAD:
                               1967                 :         443919 :             prstate->recently_dead_tuples++;
  221 melanieplageman@gmai     1968                 :         443919 :             prstate->set_all_visible = false;
                               1969                 :         443919 :             prstate->set_all_frozen = false;
                               1970                 :                : 
                               1971                 :                :             /*
                               1972                 :                :              * This tuple will soon become DEAD.  Update the hint field so
                               1973                 :                :              * that the page is reconsidered for pruning in future.
                               1974                 :                :              */
  922 heikki.linnakangas@i     1975                 :         443919 :             heap_prune_record_prunable(prstate,
                               1976                 :                :                                        HeapTupleHeaderGetUpdateXid(htup),
                               1977                 :                :                                        offnum);
                               1978                 :         443919 :             break;
                               1979                 :                : 
                               1980                 :         148838 :         case HEAPTUPLE_INSERT_IN_PROGRESS:
                               1981                 :                : 
                               1982                 :                :             /*
                               1983                 :                :              * We do not count these rows as live, because we expect the
                               1984                 :                :              * inserting transaction to update the counters at commit, and we
                               1985                 :                :              * assume that will happen only after we report our results.  This
                               1986                 :                :              * assumption is a bit shaky, but it is what acquire_sample_rows()
                               1987                 :                :              * does, so be consistent.
                               1988                 :                :              */
  221 melanieplageman@gmai     1989                 :         148838 :             prstate->set_all_visible = false;
                               1990                 :         148838 :             prstate->set_all_frozen = false;
                               1991                 :                : 
                               1992                 :                :             /*
                               1993                 :                :              * Though there is nothing "prunable" on the page, we maintain
                               1994                 :                :              * pd_prune_xid for inserts so that we have the opportunity to
                               1995                 :                :              * mark them all-visible during the next round of pruning.
                               1996                 :                :              */
  196                          1997                 :         148838 :             heap_prune_record_prunable(prstate,
                               1998                 :                :                                        HeapTupleHeaderGetXmin(htup),
                               1999                 :                :                                        offnum);
  922 heikki.linnakangas@i     2000                 :         148838 :             break;
                               2001                 :                : 
                               2002                 :          17374 :         case HEAPTUPLE_DELETE_IN_PROGRESS:
                               2003                 :                : 
                               2004                 :                :             /*
                               2005                 :                :              * This an expected case during concurrent vacuum.  Count such
                               2006                 :                :              * rows as live.  As above, we assume the deleting transaction
                               2007                 :                :              * will commit and update the counters after we report.
                               2008                 :                :              */
                               2009                 :          17374 :             prstate->live_tuples++;
  221 melanieplageman@gmai     2010                 :          17374 :             prstate->set_all_visible = false;
                               2011                 :          17374 :             prstate->set_all_frozen = false;
                               2012                 :                : 
                               2013                 :                :             /*
                               2014                 :                :              * This tuple may soon become DEAD.  Update the hint field so that
                               2015                 :                :              * the page is reconsidered for pruning in future.
                               2016                 :                :              */
  922 heikki.linnakangas@i     2017                 :          17374 :             heap_prune_record_prunable(prstate,
                               2018                 :                :                                        HeapTupleHeaderGetUpdateXid(htup),
                               2019                 :                :                                        offnum);
                               2020                 :          17374 :             break;
                               2021                 :                : 
  922 heikki.linnakangas@i     2022                 :UBC           0 :         default:
                               2023                 :                : 
                               2024                 :                :             /*
                               2025                 :                :              * DEAD tuples should've been passed to heap_prune_record_dead()
                               2026                 :                :              * or heap_prune_record_unused() instead.
                               2027                 :                :              */
                               2028         [ #  # ]:              0 :             elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result %d",
                               2029                 :                :                  prstate->htsv[offnum]);
                               2030                 :                :             break;
                               2031                 :                :     }
                               2032                 :                : 
                               2033                 :                :     /* Consider freezing any normal tuples which will not be removed */
  363 melanieplageman@gmai     2034         [ +  + ]:CBC    15142914 :     if (prstate->attempt_freeze)
                               2035                 :                :     {
                               2036                 :                :         bool        totally_frozen;
                               2037                 :                : 
  922 heikki.linnakangas@i     2038         [ +  + ]:        5035165 :         if ((heap_prepare_freeze_tuple(htup,
                               2039                 :        5035165 :                                        prstate->cutoffs,
                               2040                 :                :                                        &prstate->pagefrz,
                               2041                 :        5035165 :                                        &prstate->frozen[prstate->nfrozen],
                               2042                 :                :                                        &totally_frozen)))
                               2043                 :                :         {
                               2044                 :                :             /* Save prepared freeze plan for later */
                               2045                 :        3358935 :             prstate->frozen[prstate->nfrozen++].offset = offnum;
                               2046                 :                :         }
                               2047                 :                : 
                               2048                 :                :         /*
                               2049                 :                :          * If any tuple isn't either totally frozen already or eligible to
                               2050                 :                :          * become totally frozen (according to its freeze plan), then the page
                               2051                 :                :          * definitely cannot be set all-frozen in the visibility map later on.
                               2052                 :                :          */
                               2053         [ +  + ]:        5035165 :         if (!totally_frozen)
  221 melanieplageman@gmai     2054                 :         918920 :             prstate->set_all_frozen = false;
                               2055                 :                :     }
  922 heikki.linnakangas@i     2056                 :       15142914 : }
                               2057                 :                : 
                               2058                 :                : 
                               2059                 :                : /*
                               2060                 :                :  * Record line pointer that was already LP_DEAD and is left unchanged.
                               2061                 :                :  */
                               2062                 :                : static void
  221 melanieplageman@gmai     2063                 :        1584160 : heap_prune_record_unchanged_lp_dead(PruneState *prstate, OffsetNumber offnum)
                               2064                 :                : {
  922 heikki.linnakangas@i     2065         [ -  + ]:        1584160 :     Assert(!prstate->processed[offnum]);
                               2066                 :        1584160 :     prstate->processed[offnum] = true;
                               2067                 :                : 
                               2068                 :                :     /*
                               2069                 :                :      * Deliberately don't set hastup for LP_DEAD items.  We make the soft
                               2070                 :                :      * assumption that any LP_DEAD items encountered here will become
                               2071                 :                :      * LP_UNUSED later on, before count_nondeletable_pages is reached.  If we
                               2072                 :                :      * don't make this assumption then rel truncation will only happen every
                               2073                 :                :      * other VACUUM, at most.  Besides, VACUUM must treat
                               2074                 :                :      * hastup/nonempty_pages as provisional no matter how LP_DEAD items are
                               2075                 :                :      * handled (handled here, or handled later on).
                               2076                 :                :      *
                               2077                 :                :      * Similarly, don't unset set_all_visible and set_all_frozen until later,
                               2078                 :                :      * at the end of heap_page_prune_and_freeze().  This will allow us to
                               2079                 :                :      * attempt to freeze the page after pruning.  As long as we unset it
                               2080                 :                :      * before updating the visibility map, this will be correct.
                               2081                 :                :      */
                               2082                 :                : 
                               2083                 :                :     /* Record the dead offset for vacuum */
                               2084                 :        1584160 :     prstate->deadoffsets[prstate->lpdead_items++] = offnum;
                               2085                 :                : 
                               2086                 :                :     /*
                               2087                 :                :      * It's incorrect for a page to be marked all-visible if it contains dead
                               2088                 :                :      * items.
                               2089                 :                :      */
  204 melanieplageman@gmai     2090         [ -  + ]:        1584160 :     if (PageIsAllVisible(prstate->page))
  204 melanieplageman@gmai     2091                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum, VM_CORRUPT_LPDEAD);
  922 heikki.linnakangas@i     2092                 :CBC     1584160 : }
                               2093                 :                : 
                               2094                 :                : /*
                               2095                 :                :  * Record LP_REDIRECT that is left unchanged.
                               2096                 :                :  */
                               2097                 :                : static void
                               2098                 :         187561 : heap_prune_record_unchanged_lp_redirect(PruneState *prstate, OffsetNumber offnum)
                               2099                 :                : {
                               2100                 :                :     /*
                               2101                 :                :      * A redirect line pointer doesn't count as a live tuple.
                               2102                 :                :      *
                               2103                 :                :      * If we leave a redirect line pointer in place, there will be another
                               2104                 :                :      * tuple on the page that it points to.  We will do the bookkeeping for
                               2105                 :                :      * that separately.  So we have nothing to do here, except remember that
                               2106                 :                :      * we processed this item.
                               2107                 :                :      */
  924                          2108         [ -  + ]:         187561 :     Assert(!prstate->processed[offnum]);
                               2109                 :         187561 :     prstate->processed[offnum] = true;
                               2110                 :         187561 : }
                               2111                 :                : 
                               2112                 :                : /*
                               2113                 :                :  * Perform the actual page changes needed by heap_page_prune_and_freeze().
                               2114                 :                :  *
                               2115                 :                :  * If 'lp_truncate_only' is set, we are merely marking LP_DEAD line pointers
                               2116                 :                :  * as unused, not redirecting or removing anything else.  The
                               2117                 :                :  * PageRepairFragmentation() call is skipped in that case.
                               2118                 :                :  *
                               2119                 :                :  * If 'lp_truncate_only' is not set, the caller must hold a cleanup lock on
                               2120                 :                :  * the buffer.  If it is set, an ordinary exclusive lock suffices.
                               2121                 :                :  */
                               2122                 :                : void
  931                          2123                 :          84681 : heap_page_prune_execute(Buffer buffer, bool lp_truncate_only,
                               2124                 :                :                         OffsetNumber *redirected, int nredirected,
                               2125                 :                :                         OffsetNumber *nowdead, int ndead,
                               2126                 :                :                         OffsetNumber *nowunused, int nunused)
                               2127                 :                : {
  409 peter@eisentraut.org     2128                 :          84681 :     Page        page = BufferGetPage(buffer);
                               2129                 :                :     OffsetNumber *offnum;
                               2130                 :                :     HeapTupleHeader htup PG_USED_FOR_ASSERTS_ONLY;
                               2131                 :                : 
                               2132                 :                :     /* Shouldn't be called unless there's something to do */
 2015 pg@bowt.ie               2133   [ +  +  +  +  :          84681 :     Assert(nredirected > 0 || ndead > 0 || nunused > 0);
                                              -  + ]
                               2134                 :                : 
                               2135                 :                :     /* If 'lp_truncate_only', we can only remove already-dead line pointers */
  931 heikki.linnakangas@i     2136   [ +  +  +  -  :          84681 :     Assert(!lp_truncate_only || (nredirected == 0 && ndead == 0));
                                              -  + ]
                               2137                 :                : 
                               2138                 :                :     /* Update all redirected line pointers */
 6792 tgl@sss.pgh.pa.us        2139                 :          84681 :     offnum = redirected;
 1803 pg@bowt.ie               2140         [ +  + ]:         177964 :     for (int i = 0; i < nredirected; i++)
                               2141                 :                :     {
 6792 tgl@sss.pgh.pa.us        2142                 :          93283 :         OffsetNumber fromoff = *offnum++;
                               2143                 :          93283 :         OffsetNumber tooff = *offnum++;
                               2144                 :          93283 :         ItemId      fromlp = PageGetItemId(page, fromoff);
                               2145                 :                :         ItemId      tolp PG_USED_FOR_ASSERTS_ONLY;
                               2146                 :                : 
                               2147                 :                : #ifdef USE_ASSERT_CHECKING
                               2148                 :                : 
                               2149                 :                :         /*
                               2150                 :                :          * Any existing item that we set as an LP_REDIRECT (any 'from' item)
                               2151                 :                :          * must be the first item from a HOT chain.  If the item has tuple
                               2152                 :                :          * storage then it can't be a heap-only tuple.  Otherwise we are just
                               2153                 :                :          * maintaining an existing LP_REDIRECT from an existing HOT chain that
                               2154                 :                :          * has been pruned at least once before now.
                               2155                 :                :          */
 1803 pg@bowt.ie               2156         [ +  + ]:          93283 :         if (!ItemIdIsRedirected(fromlp))
                               2157                 :                :         {
                               2158   [ +  -  -  + ]:          83356 :             Assert(ItemIdHasStorage(fromlp) && ItemIdIsNormal(fromlp));
                               2159                 :                : 
                               2160                 :          83356 :             htup = (HeapTupleHeader) PageGetItem(page, fromlp);
                               2161         [ -  + ]:          83356 :             Assert(!HeapTupleHeaderIsHeapOnly(htup));
                               2162                 :                :         }
                               2163                 :                :         else
                               2164                 :                :         {
                               2165                 :                :             /* We shouldn't need to redundantly set the redirect */
                               2166         [ -  + ]:           9927 :             Assert(ItemIdGetRedirect(fromlp) != tooff);
                               2167                 :                :         }
                               2168                 :                : 
                               2169                 :                :         /*
                               2170                 :                :          * The item that we're about to set as an LP_REDIRECT (the 'from'
                               2171                 :                :          * item) will point to an existing item (the 'to' item) that is
                               2172                 :                :          * already a heap-only tuple.  There can be at most one LP_REDIRECT
                               2173                 :                :          * item per HOT chain.
                               2174                 :                :          *
                               2175                 :                :          * We need to keep around an LP_REDIRECT item (after original
                               2176                 :                :          * non-heap-only root tuple gets pruned away) so that it's always
                               2177                 :                :          * possible for VACUUM to easily figure out what TID to delete from
                               2178                 :                :          * indexes when an entire HOT chain becomes dead.  A heap-only tuple
                               2179                 :                :          * can never become LP_DEAD; an LP_REDIRECT item or a regular heap
                               2180                 :                :          * tuple can.
                               2181                 :                :          *
                               2182                 :                :          * This check may miss problems, e.g. the target of a redirect could
                               2183                 :                :          * be marked as unused subsequently. The page_verify_redirects() check
                               2184                 :                :          * below will catch such problems.
                               2185                 :                :          */
                               2186                 :          93283 :         tolp = PageGetItemId(page, tooff);
                               2187   [ +  -  -  + ]:          93283 :         Assert(ItemIdHasStorage(tolp) && ItemIdIsNormal(tolp));
                               2188                 :          93283 :         htup = (HeapTupleHeader) PageGetItem(page, tolp);
                               2189         [ -  + ]:          93283 :         Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2190                 :                : #endif
                               2191                 :                : 
 6090 tgl@sss.pgh.pa.us        2192                 :          93283 :         ItemIdSetRedirect(fromlp, tooff);
                               2193                 :                :     }
                               2194                 :                : 
                               2195                 :                :     /* Update all now-dead line pointers */
 6792                          2196                 :          84681 :     offnum = nowdead;
 1803 pg@bowt.ie               2197         [ +  + ]:        4148563 :     for (int i = 0; i < ndead; i++)
                               2198                 :                :     {
 6792 tgl@sss.pgh.pa.us        2199                 :        4063882 :         OffsetNumber off = *offnum++;
                               2200                 :        4063882 :         ItemId      lp = PageGetItemId(page, off);
                               2201                 :                : 
                               2202                 :                : #ifdef USE_ASSERT_CHECKING
                               2203                 :                : 
                               2204                 :                :         /*
                               2205                 :                :          * An LP_DEAD line pointer must be left behind when the original item
                               2206                 :                :          * (which is dead to everybody) could still be referenced by a TID in
                               2207                 :                :          * an index.  This should never be necessary with any individual
                               2208                 :                :          * heap-only tuple item, though. (It's not clear how much of a problem
                               2209                 :                :          * that would be, but there is no reason to allow it.)
                               2210                 :                :          */
 1803 pg@bowt.ie               2211         [ +  + ]:        4063882 :         if (ItemIdHasStorage(lp))
                               2212                 :                :         {
                               2213         [ -  + ]:        4049249 :             Assert(ItemIdIsNormal(lp));
                               2214                 :        4049249 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2215         [ -  + ]:        4049249 :             Assert(!HeapTupleHeaderIsHeapOnly(htup));
                               2216                 :                :         }
                               2217                 :                :         else
                               2218                 :                :         {
                               2219                 :                :             /* Whole HOT chain becomes dead */
                               2220         [ -  + ]:          14633 :             Assert(ItemIdIsRedirected(lp));
                               2221                 :                :         }
                               2222                 :                : #endif
                               2223                 :                : 
 6792 tgl@sss.pgh.pa.us        2224                 :        4063882 :         ItemIdSetDead(lp);
                               2225                 :                :     }
                               2226                 :                : 
                               2227                 :                :     /* Update all now-unused line pointers */
                               2228                 :          84681 :     offnum = nowunused;
 1803 pg@bowt.ie               2229         [ +  + ]:         419542 :     for (int i = 0; i < nunused; i++)
                               2230                 :                :     {
 6792 tgl@sss.pgh.pa.us        2231                 :         334861 :         OffsetNumber off = *offnum++;
                               2232                 :         334861 :         ItemId      lp = PageGetItemId(page, off);
                               2233                 :                : 
                               2234                 :                : #ifdef USE_ASSERT_CHECKING
                               2235                 :                : 
  931 heikki.linnakangas@i     2236         [ +  + ]:         334861 :         if (lp_truncate_only)
                               2237                 :                :         {
                               2238                 :                :             /* Setting LP_DEAD to LP_UNUSED in vacuum's second pass */
                               2239   [ +  -  -  + ]:         205405 :             Assert(ItemIdIsDead(lp) && !ItemIdHasStorage(lp));
                               2240                 :                :         }
                               2241                 :                :         else
                               2242                 :                :         {
                               2243                 :                :             /*
                               2244                 :                :              * When heap_page_prune_and_freeze() was called, mark_unused_now
                               2245                 :                :              * may have been passed as true, which allows would-be LP_DEAD
                               2246                 :                :              * items to be made LP_UNUSED instead.  This is only possible if
                               2247                 :                :              * the relation has no indexes.  If there are any dead items, then
                               2248                 :                :              * mark_unused_now was not true and every item being marked
                               2249                 :                :              * LP_UNUSED must refer to a heap-only tuple.
                               2250                 :                :              */
                               2251         [ +  + ]:         129456 :             if (ndead > 0)
                               2252                 :                :             {
                               2253   [ +  -  -  + ]:          61585 :                 Assert(ItemIdHasStorage(lp) && ItemIdIsNormal(lp));
                               2254                 :          61585 :                 htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2255         [ -  + ]:          61585 :                 Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2256                 :                :             }
                               2257                 :                :             else
                               2258         [ -  + ]:          67871 :                 Assert(ItemIdIsUsed(lp));
                               2259                 :                :         }
                               2260                 :                : 
                               2261                 :                : #endif
                               2262                 :                : 
 6792 tgl@sss.pgh.pa.us        2263                 :         334861 :         ItemIdSetUnused(lp);
                               2264                 :                :     }
                               2265                 :                : 
  931 heikki.linnakangas@i     2266         [ +  + ]:          84681 :     if (lp_truncate_only)
                               2267                 :           3112 :         PageTruncateLinePointerArray(page);
                               2268                 :                :     else
                               2269                 :                :     {
                               2270                 :                :         /*
                               2271                 :                :          * Finally, repair any fragmentation, and update the page's hint bit
                               2272                 :                :          * about whether it has free pointers.
                               2273                 :                :          */
                               2274                 :          81569 :         PageRepairFragmentation(page);
                               2275                 :                : 
                               2276                 :                :         /*
                               2277                 :                :          * Now that the page has been modified, assert that redirect items
                               2278                 :                :          * still point to valid targets.
                               2279                 :                :          */
                               2280                 :          81569 :         page_verify_redirects(page);
                               2281                 :                :     }
 1785 andres@anarazel.de       2282                 :          84681 : }
                               2283                 :                : 
                               2284                 :                : 
                               2285                 :                : /*
                               2286                 :                :  * If built with assertions, verify that all LP_REDIRECT items point to a
                               2287                 :                :  * valid item.
                               2288                 :                :  *
                               2289                 :                :  * One way that bugs related to HOT pruning show is redirect items pointing to
                               2290                 :                :  * removed tuples. It's not trivial to reliably check that marking an item
                               2291                 :                :  * unused will not orphan a redirect item during heap_prune_chain() /
                               2292                 :                :  * heap_page_prune_execute(), so we additionally check the whole page after
                               2293                 :                :  * pruning. Without this check such bugs would typically only cause asserts
                               2294                 :                :  * later, potentially well after the corruption has been introduced.
                               2295                 :                :  *
                               2296                 :                :  * Also check comments in heap_page_prune_execute()'s redirection loop.
                               2297                 :                :  */
                               2298                 :                : static void
                               2299                 :          81569 : page_verify_redirects(Page page)
                               2300                 :                : {
                               2301                 :                : #ifdef USE_ASSERT_CHECKING
                               2302                 :                :     OffsetNumber offnum;
                               2303                 :                :     OffsetNumber maxoff;
                               2304                 :                : 
                               2305                 :          81569 :     maxoff = PageGetMaxOffsetNumber(page);
                               2306                 :          81569 :     for (offnum = FirstOffsetNumber;
                               2307         [ +  + ]:        8201996 :          offnum <= maxoff;
                               2308                 :        8120427 :          offnum = OffsetNumberNext(offnum))
                               2309                 :                :     {
                               2310                 :        8120427 :         ItemId      itemid = PageGetItemId(page, offnum);
                               2311                 :                :         OffsetNumber targoff;
                               2312                 :                :         ItemId      targitem;
                               2313                 :                :         HeapTupleHeader htup;
                               2314                 :                : 
                               2315         [ +  + ]:        8120427 :         if (!ItemIdIsRedirected(itemid))
                               2316                 :        7868397 :             continue;
                               2317                 :                : 
                               2318                 :         252030 :         targoff = ItemIdGetRedirect(itemid);
                               2319                 :         252030 :         targitem = PageGetItemId(page, targoff);
                               2320                 :                : 
                               2321         [ -  + ]:         252030 :         Assert(ItemIdIsUsed(targitem));
                               2322         [ -  + ]:         252030 :         Assert(ItemIdIsNormal(targitem));
                               2323         [ -  + ]:         252030 :         Assert(ItemIdHasStorage(targitem));
                               2324                 :         252030 :         htup = (HeapTupleHeader) PageGetItem(page, targitem);
                               2325         [ -  + ]:         252030 :         Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2326                 :                :     }
                               2327                 :                : #endif
 6962 tgl@sss.pgh.pa.us        2328                 :          81569 : }
                               2329                 :                : 
                               2330                 :                : 
                               2331                 :                : /*
                               2332                 :                :  * For all items in this page, find their respective root line pointers.
                               2333                 :                :  * If item k is part of a HOT-chain with root at item j, then we set
                               2334                 :                :  * root_offsets[k - 1] = j.
                               2335                 :                :  *
                               2336                 :                :  * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
                               2337                 :                :  * Unused entries are filled with InvalidOffsetNumber (zero).
                               2338                 :                :  *
                               2339                 :                :  * The function must be called with at least share lock on the buffer, to
                               2340                 :                :  * prevent concurrent prune operations.
                               2341                 :                :  *
                               2342                 :                :  * Note: The information collected here is valid only as long as the caller
                               2343                 :                :  * holds a pin on the buffer. Once pin is released, a tuple might be pruned
                               2344                 :                :  * and reused by a completely unrelated tuple.
                               2345                 :                :  */
                               2346                 :                : void
                               2347                 :         139855 : heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
                               2348                 :                : {
                               2349                 :                :     OffsetNumber offnum,
                               2350                 :                :                 maxoff;
                               2351                 :                : 
 2251 alvherre@alvh.no-ip.     2352   [ +  -  -  +  :         139855 :     MemSet(root_offsets, InvalidOffsetNumber,
                                     -  -  -  -  -  
                                                 - ]
                               2353                 :                :            MaxHeapTuplesPerPage * sizeof(OffsetNumber));
                               2354                 :                : 
 6962 tgl@sss.pgh.pa.us        2355                 :         139855 :     maxoff = PageGetMaxOffsetNumber(page);
 6726 bruce@momjian.us         2356         [ +  + ]:       11728671 :     for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
                               2357                 :                :     {
 6906                          2358                 :       11588816 :         ItemId      lp = PageGetItemId(page, offnum);
                               2359                 :                :         HeapTupleHeader htup;
                               2360                 :                :         OffsetNumber nextoffnum;
                               2361                 :                :         TransactionId priorXmax;
                               2362                 :                : 
                               2363                 :                :         /* skip unused and dead items */
 6962 tgl@sss.pgh.pa.us        2364   [ +  +  +  + ]:       11588816 :         if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
                               2365                 :          11604 :             continue;
                               2366                 :                : 
                               2367         [ +  + ]:       11577212 :         if (ItemIdIsNormal(lp))
                               2368                 :                :         {
                               2369                 :       11573975 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2370                 :                : 
                               2371                 :                :             /*
                               2372                 :                :              * Check if this tuple is part of a HOT-chain rooted at some other
                               2373                 :                :              * tuple. If so, skip it for now; we'll process it when we find
                               2374                 :                :              * its root.
                               2375                 :                :              */
                               2376         [ +  + ]:       11573975 :             if (HeapTupleHeaderIsHeapOnly(htup))
                               2377                 :           3626 :                 continue;
                               2378                 :                : 
                               2379                 :                :             /*
                               2380                 :                :              * This is either a plain tuple or the root of a HOT-chain.
                               2381                 :                :              * Remember it in the mapping.
                               2382                 :                :              */
                               2383                 :       11570349 :             root_offsets[offnum - 1] = offnum;
                               2384                 :                : 
                               2385                 :                :             /* If it's not the start of a HOT-chain, we're done with it */
                               2386         [ +  + ]:       11570349 :             if (!HeapTupleHeaderIsHotUpdated(htup))
                               2387                 :       11570066 :                 continue;
                               2388                 :                : 
                               2389                 :                :             /* Set up to scan the HOT-chain */
                               2390                 :            283 :             nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 5010 alvherre@alvh.no-ip.     2391                 :            283 :             priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               2392                 :                :         }
                               2393                 :                :         else
                               2394                 :                :         {
                               2395                 :                :             /* Must be a redirect item. We do not set its root_offsets entry */
 6962 tgl@sss.pgh.pa.us        2396         [ -  + ]:           3237 :             Assert(ItemIdIsRedirected(lp));
                               2397                 :                :             /* Set up to scan the HOT-chain */
                               2398                 :           3237 :             nextoffnum = ItemIdGetRedirect(lp);
                               2399                 :           3237 :             priorXmax = InvalidTransactionId;
                               2400                 :                :         }
                               2401                 :                : 
                               2402                 :                :         /*
                               2403                 :                :          * Now follow the HOT-chain and collect other tuples in the chain.
                               2404                 :                :          *
                               2405                 :                :          * Note: Even though this is a nested loop, the complexity of the
                               2406                 :                :          * function is O(N) because a tuple in the page should be visited not
                               2407                 :                :          * more than twice, once in the outer loop and once in HOT-chain
                               2408                 :                :          * chases.
                               2409                 :                :          */
                               2410                 :                :         for (;;)
                               2411                 :                :         {
                               2412                 :                :             /* Sanity check (pure paranoia) */
   88 pg@bowt.ie               2413         [ -  + ]:           3622 :             if (nextoffnum < FirstOffsetNumber)
 1846 pg@bowt.ie               2414                 :UBC           0 :                 break;
                               2415                 :                : 
                               2416                 :                :             /*
                               2417                 :                :              * An offset past the end of page's line pointer array is possible
                               2418                 :                :              * when the array was truncated
                               2419                 :                :              */
   88 pg@bowt.ie               2420         [ -  + ]:CBC        3622 :             if (nextoffnum > maxoff)
 2014 pg@bowt.ie               2421                 :UBC           0 :                 break;
                               2422                 :                : 
 6962 tgl@sss.pgh.pa.us        2423                 :CBC        3622 :             lp = PageGetItemId(page, nextoffnum);
                               2424                 :                : 
                               2425                 :                :             /* Check for broken chains */
                               2426         [ -  + ]:           3622 :             if (!ItemIdIsNormal(lp))
 6962 tgl@sss.pgh.pa.us        2427                 :UBC           0 :                 break;
                               2428                 :                : 
 6962 tgl@sss.pgh.pa.us        2429                 :CBC        3622 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2430                 :                : 
                               2431   [ +  +  -  + ]:           4007 :             if (TransactionIdIsValid(priorXmax) &&
 3266 alvherre@alvh.no-ip.     2432                 :            385 :                 !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
 6962 tgl@sss.pgh.pa.us        2433                 :UBC           0 :                 break;
                               2434                 :                : 
                               2435                 :                :             /* Remember the root line pointer for this item */
 6962 tgl@sss.pgh.pa.us        2436                 :CBC        3622 :             root_offsets[nextoffnum - 1] = offnum;
                               2437                 :                : 
                               2438                 :                :             /* Advance to next chain member, if any */
                               2439         [ +  + ]:           3622 :             if (!HeapTupleHeaderIsHotUpdated(htup))
                               2440                 :           3520 :                 break;
                               2441                 :                : 
                               2442                 :                :             /* HOT implies it can't have moved to different partition */
 3110 andres@anarazel.de       2443         [ -  + ]:            102 :             Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
                               2444                 :                : 
 6962 tgl@sss.pgh.pa.us        2445                 :            102 :             nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 5010 alvherre@alvh.no-ip.     2446                 :            102 :             priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               2447                 :                :         }
                               2448                 :                :     }
 6962 tgl@sss.pgh.pa.us        2449                 :         139855 : }
                               2450                 :                : 
                               2451                 :                : 
                               2452                 :                : /*
                               2453                 :                :  * Compare fields that describe actions required to freeze tuple with caller's
                               2454                 :                :  * open plan.  If everything matches then the frz tuple plan is equivalent to
                               2455                 :                :  * caller's plan.
                               2456                 :                :  */
                               2457                 :                : static inline bool
  931 heikki.linnakangas@i     2458                 :        1306224 : heap_log_freeze_eq(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
                               2459                 :                : {
                               2460         [ +  + ]:        1306224 :     if (plan->xmax == frz->xmax &&
                               2461         [ +  + ]:        1306187 :         plan->t_infomask2 == frz->t_infomask2 &&
                               2462         [ +  + ]:        1304974 :         plan->t_infomask == frz->t_infomask &&
                               2463         [ +  - ]:        1301551 :         plan->frzflags == frz->frzflags)
                               2464                 :        1301551 :         return true;
                               2465                 :                : 
                               2466                 :                :     /* Caller must call heap_log_freeze_new_plan again for frz */
                               2467                 :           4673 :     return false;
                               2468                 :                : }
                               2469                 :                : 
                               2470                 :                : /*
                               2471                 :                :  * Comparator used to deduplicate the freeze plans used in WAL records.
                               2472                 :                :  */
                               2473                 :                : static int
                               2474                 :        1630536 : heap_log_freeze_cmp(const void *arg1, const void *arg2)
                               2475                 :                : {
  273 peter@eisentraut.org     2476                 :        1630536 :     const HeapTupleFreeze *frz1 = arg1;
                               2477                 :        1630536 :     const HeapTupleFreeze *frz2 = arg2;
                               2478                 :                : 
  931 heikki.linnakangas@i     2479         [ +  + ]:        1630536 :     if (frz1->xmax < frz2->xmax)
                               2480                 :             55 :         return -1;
                               2481         [ +  + ]:        1630481 :     else if (frz1->xmax > frz2->xmax)
                               2482                 :             64 :         return 1;
                               2483                 :                : 
                               2484         [ +  + ]:        1630417 :     if (frz1->t_infomask2 < frz2->t_infomask2)
                               2485                 :           6722 :         return -1;
                               2486         [ +  + ]:        1623695 :     else if (frz1->t_infomask2 > frz2->t_infomask2)
                               2487                 :           7458 :         return 1;
                               2488                 :                : 
                               2489         [ +  + ]:        1616237 :     if (frz1->t_infomask < frz2->t_infomask)
                               2490                 :          13141 :         return -1;
                               2491         [ +  + ]:        1603096 :     else if (frz1->t_infomask > frz2->t_infomask)
                               2492                 :          24065 :         return 1;
                               2493                 :                : 
                               2494         [ -  + ]:        1579031 :     if (frz1->frzflags < frz2->frzflags)
  931 heikki.linnakangas@i     2495                 :UBC           0 :         return -1;
  931 heikki.linnakangas@i     2496         [ -  + ]:CBC     1579031 :     else if (frz1->frzflags > frz2->frzflags)
  931 heikki.linnakangas@i     2497                 :UBC           0 :         return 1;
                               2498                 :                : 
                               2499                 :                :     /*
                               2500                 :                :      * heap_log_freeze_eq would consider these tuple-wise plans to be equal.
                               2501                 :                :      * (So the tuples will share a single canonical freeze plan.)
                               2502                 :                :      *
                               2503                 :                :      * We tiebreak on page offset number to keep each freeze plan's page
                               2504                 :                :      * offset number array individually sorted. (Unnecessary, but be tidy.)
                               2505                 :                :      */
  931 heikki.linnakangas@i     2506         [ +  + ]:CBC     1579031 :     if (frz1->offset < frz2->offset)
                               2507                 :        1431416 :         return -1;
                               2508         [ +  - ]:         147615 :     else if (frz1->offset > frz2->offset)
                               2509                 :         147615 :         return 1;
                               2510                 :                : 
  931 heikki.linnakangas@i     2511                 :UBC           0 :     Assert(false);
                               2512                 :                :     return 0;
                               2513                 :                : }
                               2514                 :                : 
                               2515                 :                : /*
                               2516                 :                :  * Start new plan initialized using tuple-level actions.  At least one tuple
                               2517                 :                :  * will have steps required to freeze described by caller's plan during REDO.
                               2518                 :                :  */
                               2519                 :                : static inline void
  931 heikki.linnakangas@i     2520                 :CBC       30073 : heap_log_freeze_new_plan(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
                               2521                 :                : {
                               2522                 :          30073 :     plan->xmax = frz->xmax;
                               2523                 :          30073 :     plan->t_infomask2 = frz->t_infomask2;
                               2524                 :          30073 :     plan->t_infomask = frz->t_infomask;
                               2525                 :          30073 :     plan->frzflags = frz->frzflags;
                               2526                 :          30073 :     plan->ntuples = 1;           /* for now */
                               2527                 :          30073 : }
                               2528                 :                : 
                               2529                 :                : /*
                               2530                 :                :  * Deduplicate tuple-based freeze plans so that each distinct set of
                               2531                 :                :  * processing steps is only stored once in the WAL record.
                               2532                 :                :  * Called during original execution of freezing (for logged relations).
                               2533                 :                :  *
                               2534                 :                :  * Return value is number of plans set in *plans_out for caller.  Also writes
                               2535                 :                :  * an array of offset numbers into *offsets_out output argument for caller
                               2536                 :                :  * (actually there is one array per freeze plan, but that's not of immediate
                               2537                 :                :  * concern to our caller).
                               2538                 :                :  */
                               2539                 :                : static int
                               2540                 :          25400 : heap_log_freeze_plan(HeapTupleFreeze *tuples, int ntuples,
                               2541                 :                :                      xlhp_freeze_plan *plans_out,
                               2542                 :                :                      OffsetNumber *offsets_out)
                               2543                 :                : {
                               2544                 :          25400 :     int         nplans = 0;
                               2545                 :                : 
                               2546                 :                :     /* Sort tuple-based freeze plans in the order required to deduplicate */
                               2547                 :          25400 :     qsort(tuples, ntuples, sizeof(HeapTupleFreeze), heap_log_freeze_cmp);
                               2548                 :                : 
                               2549         [ +  + ]:        1357024 :     for (int i = 0; i < ntuples; i++)
                               2550                 :                :     {
                               2551                 :        1331624 :         HeapTupleFreeze *frz = tuples + i;
                               2552                 :                : 
                               2553         [ +  + ]:        1331624 :         if (i == 0)
                               2554                 :                :         {
                               2555                 :                :             /* New canonical freeze plan starting with first tup */
                               2556                 :          25400 :             heap_log_freeze_new_plan(plans_out, frz);
                               2557                 :          25400 :             nplans++;
                               2558                 :                :         }
                               2559         [ +  + ]:        1306224 :         else if (heap_log_freeze_eq(plans_out, frz))
                               2560                 :                :         {
                               2561                 :                :             /* tup matches open canonical plan -- include tup in it */
                               2562         [ -  + ]:        1301551 :             Assert(offsets_out[i - 1] < frz->offset);
                               2563                 :        1301551 :             plans_out->ntuples++;
                               2564                 :                :         }
                               2565                 :                :         else
                               2566                 :                :         {
                               2567                 :                :             /* Tup doesn't match current plan -- done with it now */
                               2568                 :           4673 :             plans_out++;
                               2569                 :                : 
                               2570                 :                :             /* New canonical freeze plan starting with this tup */
                               2571                 :           4673 :             heap_log_freeze_new_plan(plans_out, frz);
                               2572                 :           4673 :             nplans++;
                               2573                 :                :         }
                               2574                 :                : 
                               2575                 :                :         /*
                               2576                 :                :          * Save page offset number in dedicated buffer in passing.
                               2577                 :                :          *
                               2578                 :                :          * REDO routine relies on the record's offset numbers array grouping
                               2579                 :                :          * offset numbers by freeze plan.  The sort order within each grouping
                               2580                 :                :          * is ascending offset number order, just to keep things tidy.
                               2581                 :                :          */
                               2582                 :        1331624 :         offsets_out[i] = frz->offset;
                               2583                 :                :     }
                               2584                 :                : 
                               2585   [ +  -  -  + ]:          25400 :     Assert(nplans > 0 && nplans <= ntuples);
                               2586                 :                : 
                               2587                 :          25400 :     return nplans;
                               2588                 :                : }
                               2589                 :                : 
                               2590                 :                : /*
                               2591                 :                :  * Write an XLOG_HEAP2_PRUNE* WAL record
                               2592                 :                :  *
                               2593                 :                :  * This is used for several different page maintenance operations:
                               2594                 :                :  *
                               2595                 :                :  * - Page pruning, in VACUUM's 1st pass or on access: Some items are
                               2596                 :                :  *   redirected, some marked dead, and some removed altogether.
                               2597                 :                :  *
                               2598                 :                :  * - Freezing: Items are marked as 'frozen'.
                               2599                 :                :  *
                               2600                 :                :  * - Vacuum, 2nd pass: Items that are already LP_DEAD are marked as unused.
                               2601                 :                :  *
                               2602                 :                :  * They have enough commonalities that we use a single WAL record for them
                               2603                 :                :  * all.
                               2604                 :                :  *
                               2605                 :                :  * If replaying the record requires a cleanup lock, pass cleanup_lock = true.
                               2606                 :                :  * Replaying 'redirected' or 'dead' items always requires a cleanup lock, but
                               2607                 :                :  * replaying 'unused' items depends on whether they were all previously marked
                               2608                 :                :  * as dead.
                               2609                 :                :  *
                               2610                 :                :  * If the VM is being updated, vmflags will contain the bits to set. In this
                               2611                 :                :  * case, vmbuffer should already have been updated and marked dirty and should
                               2612                 :                :  * still be pinned and locked.
                               2613                 :                :  *
                               2614                 :                :  * Note: This function scribbles on the 'frozen' array.
                               2615                 :                :  *
                               2616                 :                :  * Note: This is called in a critical section, so careful what you do here.
                               2617                 :                :  */
                               2618                 :                : void
                               2619                 :         153618 : log_heap_prune_and_freeze(Relation relation, Buffer buffer,
                               2620                 :                :                           Buffer vmbuffer, uint8 vmflags,
                               2621                 :                :                           TransactionId conflict_xid,
                               2622                 :                :                           bool cleanup_lock,
                               2623                 :                :                           PruneReason reason,
                               2624                 :                :                           HeapTupleFreeze *frozen, int nfrozen,
                               2625                 :                :                           OffsetNumber *redirected, int nredirected,
                               2626                 :                :                           OffsetNumber *dead, int ndead,
                               2627                 :                :                           OffsetNumber *unused, int nunused)
                               2628                 :                : {
                               2629                 :                :     xl_heap_prune xlrec;
                               2630                 :                :     XLogRecPtr  recptr;
                               2631                 :                :     uint8       info;
                               2632                 :                :     uint8       regbuf_flags_heap;
                               2633                 :                : 
  202 melanieplageman@gmai     2634                 :         153618 :     Page        heap_page = BufferGetPage(buffer);
                               2635                 :                : 
                               2636                 :                :     /* The following local variables hold data registered in the WAL record: */
                               2637                 :                :     xlhp_freeze_plan plans[MaxHeapTuplesPerPage];
                               2638                 :                :     xlhp_freeze_plans freeze_plans;
                               2639                 :                :     xlhp_prune_items redirect_items;
                               2640                 :                :     xlhp_prune_items dead_items;
                               2641                 :                :     xlhp_prune_items unused_items;
                               2642                 :                :     OffsetNumber frz_offsets[MaxHeapTuplesPerPage];
  364                          2643   [ +  +  +  +  :         153618 :     bool        do_prune = nredirected > 0 || ndead > 0 || nunused > 0;
                                              +  + ]
                               2644                 :         153618 :     bool        do_set_vm = vmflags & VISIBILITYMAP_VALID_BITS;
  202                          2645                 :         153618 :     bool        heap_fpi_allowed = true;
                               2646                 :                : 
  364                          2647         [ -  + ]:         153618 :     Assert((vmflags & VISIBILITYMAP_VALID_BITS) == vmflags);
                               2648                 :                : 
  931 heikki.linnakangas@i     2649                 :         153618 :     xlrec.flags = 0;
  364 melanieplageman@gmai     2650                 :         153618 :     regbuf_flags_heap = REGBUF_STANDARD;
                               2651                 :                : 
                               2652                 :                :     /*
                               2653                 :                :      * We can avoid an FPI of the heap page if the only modification we are
                               2654                 :                :      * making to it is to set PD_ALL_VISIBLE and checksums/wal_log_hints are
                               2655                 :                :      * disabled.
                               2656                 :                :      *
                               2657                 :                :      * However, if the page has never been WAL-logged (LSN is invalid), we
                               2658                 :                :      * must force an FPI regardless.  This can happen when another backend
                               2659                 :                :      * extends the heap, initializes the page, and then fails before WAL-
                               2660                 :                :      * logging it.  Since heap extension is not WAL-logged, recovery might try
                               2661                 :                :      * to replay our record and find that the page isn't initialized, which
                               2662                 :                :      * would cause a PANIC.
                               2663                 :                :      */
  202                          2664         [ -  + ]:         153618 :     if (!XLogRecPtrIsValid(PageGetLSN(heap_page)))
  202 melanieplageman@gmai     2665                 :UBC           0 :         regbuf_flags_heap |= REGBUF_FORCE_IMAGE;
  202 melanieplageman@gmai     2666   [ +  +  +  +  :CBC      153618 :     else if (!do_prune && nfrozen == 0 && (!do_set_vm || !XLogHintBitIsNeeded()))
                                     +  -  +  +  +  
                                                 + ]
                               2667                 :                :     {
  364                          2668                 :           2865 :         regbuf_flags_heap |= REGBUF_NO_IMAGE;
  202                          2669                 :           2865 :         heap_fpi_allowed = false;
                               2670                 :                :     }
                               2671                 :                : 
                               2672                 :                :     /*
                               2673                 :                :      * Prepare data for the buffer.  The arrays are not actually in the
                               2674                 :                :      * buffer, but we pretend that they are.  When XLogInsert stores a full
                               2675                 :                :      * page image, the arrays can be omitted.
                               2676                 :                :      */
  931 heikki.linnakangas@i     2677                 :         153618 :     XLogBeginInsert();
  364 melanieplageman@gmai     2678                 :         153618 :     XLogRegisterBuffer(0, buffer, regbuf_flags_heap);
                               2679                 :                : 
                               2680         [ +  + ]:         153618 :     if (do_set_vm)
                               2681                 :          81700 :         XLogRegisterBuffer(1, vmbuffer, 0);
                               2682                 :                : 
  931 heikki.linnakangas@i     2683         [ +  + ]:         153618 :     if (nfrozen > 0)
                               2684                 :                :     {
                               2685                 :                :         int         nplans;
                               2686                 :                : 
                               2687                 :          25400 :         xlrec.flags |= XLHP_HAS_FREEZE_PLANS;
                               2688                 :                : 
                               2689                 :                :         /*
                               2690                 :                :          * Prepare deduplicated representation for use in the WAL record. This
                               2691                 :                :          * destructively sorts frozen tuples array in-place.
                               2692                 :                :          */
                               2693                 :          25400 :         nplans = heap_log_freeze_plan(frozen, nfrozen, plans, frz_offsets);
                               2694                 :                : 
                               2695                 :          25400 :         freeze_plans.nplans = nplans;
  608 peter@eisentraut.org     2696                 :          25400 :         XLogRegisterBufData(0, &freeze_plans,
                               2697                 :                :                             offsetof(xlhp_freeze_plans, plans));
                               2698                 :          25400 :         XLogRegisterBufData(0, plans,
                               2699                 :                :                             sizeof(xlhp_freeze_plan) * nplans);
                               2700                 :                :     }
  931 heikki.linnakangas@i     2701         [ +  + ]:         153618 :     if (nredirected > 0)
                               2702                 :                :     {
                               2703                 :          18039 :         xlrec.flags |= XLHP_HAS_REDIRECTIONS;
                               2704                 :                : 
                               2705                 :          18039 :         redirect_items.ntargets = nredirected;
  608 peter@eisentraut.org     2706                 :          18039 :         XLogRegisterBufData(0, &redirect_items,
                               2707                 :                :                             offsetof(xlhp_prune_items, data));
                               2708                 :          18039 :         XLogRegisterBufData(0, redirected,
                               2709                 :                :                             sizeof(OffsetNumber[2]) * nredirected);
                               2710                 :                :     }
  931 heikki.linnakangas@i     2711         [ +  + ]:         153618 :     if (ndead > 0)
                               2712                 :                :     {
                               2713                 :          58742 :         xlrec.flags |= XLHP_HAS_DEAD_ITEMS;
                               2714                 :                : 
                               2715                 :          58742 :         dead_items.ntargets = ndead;
  608 peter@eisentraut.org     2716                 :          58742 :         XLogRegisterBufData(0, &dead_items,
                               2717                 :                :                             offsetof(xlhp_prune_items, data));
                               2718                 :          58742 :         XLogRegisterBufData(0, dead,
                               2719                 :                :                             sizeof(OffsetNumber) * ndead);
                               2720                 :                :     }
  931 heikki.linnakangas@i     2721         [ +  + ]:         153618 :     if (nunused > 0)
                               2722                 :                :     {
                               2723                 :          36591 :         xlrec.flags |= XLHP_HAS_NOW_UNUSED_ITEMS;
                               2724                 :                : 
                               2725                 :          36591 :         unused_items.ntargets = nunused;
  608 peter@eisentraut.org     2726                 :          36591 :         XLogRegisterBufData(0, &unused_items,
                               2727                 :                :                             offsetof(xlhp_prune_items, data));
                               2728                 :          36591 :         XLogRegisterBufData(0, unused,
                               2729                 :                :                             sizeof(OffsetNumber) * nunused);
                               2730                 :                :     }
  931 heikki.linnakangas@i     2731         [ +  + ]:         153618 :     if (nfrozen > 0)
  608 peter@eisentraut.org     2732                 :          25400 :         XLogRegisterBufData(0, frz_offsets,
                               2733                 :                :                             sizeof(OffsetNumber) * nfrozen);
                               2734                 :                : 
                               2735                 :                :     /*
                               2736                 :                :      * Prepare the main xl_heap_prune record.  We already set the XLHP_HAS_*
                               2737                 :                :      * flag above.
                               2738                 :                :      */
  364 melanieplageman@gmai     2739         [ +  + ]:         153618 :     if (vmflags & VISIBILITYMAP_ALL_VISIBLE)
                               2740                 :                :     {
                               2741                 :          81700 :         xlrec.flags |= XLHP_VM_ALL_VISIBLE;
                               2742         [ +  + ]:          81700 :         if (vmflags & VISIBILITYMAP_ALL_FROZEN)
                               2743                 :          48529 :             xlrec.flags |= XLHP_VM_ALL_FROZEN;
                               2744                 :                :     }
  931 heikki.linnakangas@i     2745   [ +  +  +  +  :         153618 :     if (RelationIsAccessibleInLogicalDecoding(relation))
                                     +  -  -  +  -  
                                     -  -  -  +  +  
                                     +  +  -  +  -  
                                           -  +  - ]
                               2746                 :            664 :         xlrec.flags |= XLHP_IS_CATALOG_REL;
                               2747         [ +  + ]:         153618 :     if (TransactionIdIsValid(conflict_xid))
                               2748                 :         119486 :         xlrec.flags |= XLHP_HAS_CONFLICT_HORIZON;
                               2749         [ +  + ]:         153618 :     if (cleanup_lock)
                               2750                 :          71590 :         xlrec.flags |= XLHP_CLEANUP_LOCK;
                               2751                 :                :     else
                               2752                 :                :     {
                               2753   [ +  -  -  + ]:          82028 :         Assert(nredirected == 0 && ndead == 0);
                               2754                 :                :         /* also, any items in 'unused' must've been LP_DEAD previously */
                               2755                 :                :     }
  608 peter@eisentraut.org     2756                 :         153618 :     XLogRegisterData(&xlrec, SizeOfHeapPrune);
  931 heikki.linnakangas@i     2757         [ +  + ]:         153618 :     if (TransactionIdIsValid(conflict_xid))
  608 peter@eisentraut.org     2758                 :         119486 :         XLogRegisterData(&conflict_xid, sizeof(TransactionId));
                               2759                 :                : 
  931 heikki.linnakangas@i     2760   [ +  +  +  - ]:         153618 :     switch (reason)
                               2761                 :                :     {
                               2762                 :          71667 :         case PRUNE_ON_ACCESS:
                               2763                 :          71667 :             info = XLOG_HEAP2_PRUNE_ON_ACCESS;
                               2764                 :          71667 :             break;
                               2765                 :          60379 :         case PRUNE_VACUUM_SCAN:
                               2766                 :          60379 :             info = XLOG_HEAP2_PRUNE_VACUUM_SCAN;
                               2767                 :          60379 :             break;
                               2768                 :          21572 :         case PRUNE_VACUUM_CLEANUP:
                               2769                 :          21572 :             info = XLOG_HEAP2_PRUNE_VACUUM_CLEANUP;
                               2770                 :          21572 :             break;
  931 heikki.linnakangas@i     2771                 :UBC           0 :         default:
                               2772         [ #  # ]:              0 :             elog(ERROR, "unrecognized prune reason: %d", (int) reason);
                               2773                 :                :             break;
                               2774                 :                :     }
  931 heikki.linnakangas@i     2775                 :CBC      153618 :     recptr = XLogInsert(RM_HEAP2_ID, info);
                               2776                 :                : 
  364 melanieplageman@gmai     2777         [ +  + ]:         153618 :     if (do_set_vm)
                               2778                 :                :     {
                               2779         [ -  + ]:          81700 :         Assert(BufferIsDirty(vmbuffer));
                               2780                 :          81700 :         PageSetLSN(BufferGetPage(vmbuffer), recptr);
                               2781                 :                :     }
                               2782                 :                : 
                               2783                 :                :     /*
                               2784                 :                :      * If we explicitly skip an FPI, we must not stamp the heap page with this
                               2785                 :                :      * record's LSN. Recovery skips records <= the stamped LSN, so this could
                               2786                 :                :      * lead to skipping an earlier FPI needed to repair a torn page.
                               2787                 :                :      */
  202                          2788         [ +  + ]:         153618 :     if (heap_fpi_allowed)
                               2789                 :                :     {
  364                          2790         [ -  + ]:         150753 :         Assert(BufferIsDirty(buffer));
  202                          2791                 :         150753 :         PageSetLSN(heap_page, recptr);
                               2792                 :                :     }
  931 heikki.linnakangas@i     2793                 :         153618 : }
        

Generated by: LCOV version 2.0-1