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: f76c13edadc2b319036e0d238703ed56bb23f934 vs 9e17d25e79d4756be08b4a5521b4b58450217137 Lines: 91.3 % 743 678 1 64 678 1
Current Date: 2026-09-20 14:13:17 +0900 Functions: 96.4 % 28 27 1 27
Baseline: lcov-20260920-baseline Branches: 73.8 % 663 489 174 489
Baseline Date: 2026-09-20 14:13:13 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 3 3 3
(7,30] days: 100.0 % 1 1 1
(30,360] days: 87.3 % 322 281 1 40 281
(360..) days: 94.2 % 417 393 24 393
Function coverage date bins:
(30,360] days: 92.9 % 14 13 1 13
(360..) days: 100.0 % 14 14 14
Branch coverage date bins:
(30,360] days: 75.5 % 310 234 76 234
(360..) days: 72.2 % 353 255 98 255

 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
  174 melanieplageman@gmai      272                 :CBC    11187125 : heap_page_prune_opt(Relation relation, Buffer buffer, Buffer *vmbuffer,
                                273                 :                :                     bool rel_read_only)
                                274                 :                : {
 3805 kgrittn@postgresql.o      275                 :       11187125 :     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                 :                :      */
 4584 rhaas@postgresql.org      285         [ +  + ]:       11187125 :     if (RecoveryInProgress())
                                286                 :         266272 :         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                 :                :      */
  199 melanieplageman@gmai      294                 :       10920853 :     prune_xid = PageGetPruneXid(page);
 2230 andres@anarazel.de        295         [ +  + ]:       10920853 :     if (!TransactionIdIsValid(prune_xid))
                                296                 :        7185943 :         return;
                                297                 :                : 
                                298                 :                :     /*
                                299                 :                :      * Check whether prune_xid indicates that there may be dead rows that can
                                300                 :                :      * be cleaned up.
                                301                 :                :      */
                                302                 :        3734910 :     vistest = GlobalVisTestFor(relation);
                                303                 :                : 
  180 melanieplageman@gmai      304         [ +  + ]:        3734910 :     if (!GlobalVisTestIsRemovableXid(vistest, prune_xid, true))
 1111 tmunro@postgresql.or      305                 :        1596749 :         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                 :                :      */
  892 akorotkov@postgresql      319         [ +  + ]:        2138161 :     minfree = RelationGetTargetPageFreeSpace(relation,
                                320                 :                :                                              HEAP_DEFAULT_FILLFACTOR);
 6940 tgl@sss.pgh.pa.us         321                 :        2138161 :     minfree = Max(minfree, BLCKSZ / 10);
                                322                 :                : 
 6643                           323   [ +  +  +  + ]:        2138161 :     if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
                                324                 :                :     {
   72 melanieplageman@gmai      325                 :         133236 :         bool        record_free_space = false;
                                326                 :         133236 :         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                 :                :          */
    9                           334                 :         133236 :         visibilitymap_pin(relation, BufferGetBlockNumber(buffer), vmbuffer);
                                335                 :                : 
                                336                 :                :         /* OK, try to get exclusive buffer lock */
 6940 tgl@sss.pgh.pa.us         337         [ +  + ]:         133236 :         if (!ConditionalLockBufferForCleanup(buffer))
                                338                 :           1938 :             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                 :                :          */
 6643                           345   [ +  +  +  - ]:         131298 :         if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
                                346                 :                :         {
                                347                 :                :             OffsetNumber dummy_off_loc;
                                348                 :                :             PruneFreezeResult presult;
                                349                 :                :             PruneFreezeParams params;
                                350                 :                : 
  182 melanieplageman@gmai      351                 :         131298 :             params.relation = relation;
                                352                 :         131298 :             params.buffer = buffer;
                                353                 :         131298 :             params.vmbuffer = *vmbuffer;
                                354                 :         131298 :             params.reason = PRUNE_ON_ACCESS;
                                355                 :         131298 :             params.vistest = vistest;
                                356                 :         131298 :             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                 :         131298 :             params.options = HEAP_PAGE_PRUNE_ALLOW_FAST_PATH;
  174                           365         [ +  + ]:         131298 :             if (rel_read_only)
                                366                 :          36536 :                 params.options |= HEAP_PAGE_PRUNE_SET_VM;
                                367                 :                : 
  304                           368                 :         131298 :             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                 :                :              */
 1088 rhaas@postgresql.org      385         [ +  + ]:         131298 :             if (presult.ndeleted > presult.nnewlpdead)
 1773 pg@bowt.ie                386                 :          20554 :                 pgstat_update_heap_dead_tuples(relation,
 1088 rhaas@postgresql.org      387                 :          20554 :                                                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                 :                :              */
   72 melanieplageman@gmai      396         [ +  + ]:         131298 :             if (presult.newly_all_visible)
                                397                 :                :             {
                                398                 :          13715 :                 record_free_space = true;
                                399                 :          13715 :                 freespace = PageGetHeapFreeSpace(page);
                                400                 :                :             }
                                401                 :                :         }
                                402                 :                : 
                                403                 :                :         /* And release buffer lock */
 6940 tgl@sss.pgh.pa.us         404                 :         131298 :         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                 :                :          */
   72 melanieplageman@gmai      412         [ +  + ]:         131298 :         if (record_free_space)
                                413                 :          13715 :             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
  298                           425                 :         226610 : 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                 :         226610 :     prstate->vistest = params->vistest;
                                433                 :         226610 :     prstate->mark_unused_now =
  304                           434                 :         226610 :         (params->options & HEAP_PAGE_PRUNE_MARK_UNUSED_NOW) != 0;
                                435                 :                : 
                                436                 :                :     /* cutoffs must be provided if we will attempt freezing */
  299                           437   [ +  +  -  + ]:         226610 :     Assert(!(params->options & HEAP_PAGE_PRUNE_FREEZE) || params->cutoffs);
  298                           438                 :         226610 :     prstate->attempt_freeze = (params->options & HEAP_PAGE_PRUNE_FREEZE) != 0;
  174                           439                 :         226610 :     prstate->attempt_set_vm = (params->options & HEAP_PAGE_PRUNE_SET_VM) != 0;
  298                           440                 :         226610 :     prstate->cutoffs = params->cutoffs;
  199                           441                 :         226610 :     prstate->relation = params->relation;
                                442                 :         226610 :     prstate->block = BufferGetBlockNumber(params->buffer);
                                443                 :         226610 :     prstate->buffer = params->buffer;
                                444                 :         226610 :     prstate->page = BufferGetPage(params->buffer);
                                445                 :                : 
  182                           446         [ -  + ]:         226610 :     Assert(BufferIsValid(params->vmbuffer));
                                447                 :         226610 :     prstate->vmbuffer = params->vmbuffer;
  180                           448                 :         226610 :     prstate->new_vmbits = 0;
  182                           449                 :         226610 :     prstate->old_vmbits = visibilitymap_get_status(prstate->relation,
                                450                 :                :                                                    prstate->block,
                                451                 :                :                                                    &prstate->vmbuffer);
                                452                 :                : 
                                453                 :                :     /*
                                454                 :                :      * Our strategy is to scan the page and make lists of items to change,
                                455                 :                :      * then apply the changes within a critical section.  This keeps as much
                                456                 :                :      * logic as possible out of the critical section, and also ensures that
                                457                 :                :      * WAL replay will work the same as the normal case.
                                458                 :                :      *
                                459                 :                :      * First, initialize the new pd_prune_xid value to zero (indicating no
                                460                 :                :      * prunable tuples).  If we find any tuples which may soon become
                                461                 :                :      * prunable, we will save the lowest relevant XID in new_prune_xid. Also
                                462                 :                :      * initialize the rest of our working state.
                                463                 :                :      */
  298                           464                 :         226610 :     prstate->new_prune_xid = InvalidTransactionId;
                                465                 :         226610 :     prstate->latest_xid_removed = InvalidTransactionId;
                                466                 :         226610 :     prstate->nredirected = prstate->ndead = prstate->nunused = 0;
                                467                 :         226610 :     prstate->nfrozen = 0;
                                468                 :         226610 :     prstate->nroot_items = 0;
                                469                 :         226610 :     prstate->nheaponly_items = 0;
                                470                 :                : 
                                471                 :                :     /* initialize page freezing working state */
                                472                 :         226610 :     prstate->pagefrz.freeze_required = false;
  194                           473                 :         226610 :     prstate->pagefrz.FreezePageConflictXid = InvalidTransactionId;
  298                           474         [ +  + ]:         226610 :     if (prstate->attempt_freeze)
                                475                 :                :     {
  290                           476   [ +  -  -  + ]:          95312 :         Assert(new_relfrozen_xid && new_relmin_mxid);
                                477                 :          95312 :         prstate->pagefrz.FreezePageRelfrozenXid = *new_relfrozen_xid;
                                478                 :          95312 :         prstate->pagefrz.NoFreezePageRelfrozenXid = *new_relfrozen_xid;
                                479                 :          95312 :         prstate->pagefrz.FreezePageRelminMxid = *new_relmin_mxid;
                                480                 :          95312 :         prstate->pagefrz.NoFreezePageRelminMxid = *new_relmin_mxid;
                                481                 :                :     }
                                482                 :                :     else
                                483                 :                :     {
                                484   [ +  -  -  + ]:         131298 :         Assert(!new_relfrozen_xid && !new_relmin_mxid);
  298                           485                 :         131298 :         prstate->pagefrz.FreezePageRelminMxid = InvalidMultiXactId;
                                486                 :         131298 :         prstate->pagefrz.NoFreezePageRelminMxid = InvalidMultiXactId;
                                487                 :         131298 :         prstate->pagefrz.FreezePageRelfrozenXid = InvalidTransactionId;
                                488                 :         131298 :         prstate->pagefrz.NoFreezePageRelfrozenXid = InvalidTransactionId;
                                489                 :                :     }
                                490                 :                : 
                                491                 :         226610 :     prstate->ndeleted = 0;
                                492                 :         226610 :     prstate->live_tuples = 0;
                                493                 :         226610 :     prstate->recently_dead_tuples = 0;
                                494                 :         226610 :     prstate->hastup = false;
                                495                 :         226610 :     prstate->lpdead_items = 0;
                                496                 :                : 
                                497                 :                :     /*
                                498                 :                :      * deadoffsets are filled in during pruning but are only used to populate
                                499                 :                :      * PruneFreezeResult->deadoffsets. To avoid needing two copies of the
                                500                 :                :      * array, just save a pointer to the result offsets array in the
                                501                 :                :      * PruneState.
                                502                 :                :      */
  278                           503                 :         226610 :     prstate->deadoffsets = presult->deadoffsets;
                                504                 :                : 
                                505                 :                :     /*
                                506                 :                :      * We track whether the page will be all-visible/all-frozen at the end of
                                507                 :                :      * pruning and freezing. While examining tuple visibility, we'll set
                                508                 :                :      * set_all_visible to false if there are tuples on the page not visible to
                                509                 :                :      * all running and future transactions. If setting the VM is enabled for
                                510                 :                :      * this scan, we will do so if the page ends up being all-visible.
                                511                 :                :      *
                                512                 :                :      * We also keep track of the newest live XID, which is used to calculate
                                513                 :                :      * the snapshot conflict horizon for a WAL record setting the VM.
                                514                 :                :      */
  174                           515                 :         226610 :     prstate->set_all_visible = prstate->attempt_set_vm;
  180                           516                 :         226610 :     prstate->newest_live_xid = InvalidTransactionId;
                                517                 :                : 
                                518                 :                :     /*
                                519                 :                :      * Currently, only VACUUM performs freezing, but other callers may in the
                                520                 :                :      * future. We must initialize set_all_frozen based on whether or not the
                                521                 :                :      * caller passed HEAP_PAGE_PRUNE_FREEZE, because if they did not, we won't
                                522                 :                :      * call heap_prepare_freeze_tuple() for each tuple, and set_all_frozen
                                523                 :                :      * will never be cleared for tuples that need freezing. This would lead to
                                524                 :                :      * incorrectly setting the visibility map all-frozen for this page. We
                                525                 :                :      * can't set the page all-frozen in the VM if the caller didn't pass
                                526                 :                :      * HEAP_PAGE_PRUNE_SET_VM.
                                527                 :                :      *
                                528                 :                :      * When freezing is not required (no XIDs/MXIDs older than the freeze
                                529                 :                :      * cutoff), we may still choose to "opportunistically" freeze if doing so
                                530                 :                :      * would make the page all-frozen.
                                531                 :                :      *
                                532                 :                :      * We will not be able to freeze the whole page at the end of vacuum if
                                533                 :                :      * there are tuples present that are not visible to everyone or if there
                                534                 :                :      * are dead tuples which will not be removable. However, dead tuples that
                                535                 :                :      * will be removed by the end of vacuum should not prevent this
                                536                 :                :      * opportunistic freezing.
                                537                 :                :      *
                                538                 :                :      * Therefore, we do not clear set_all_visible and set_all_frozen when we
                                539                 :                :      * encounter LP_DEAD items. Instead, we correct them after deciding
                                540                 :                :      * whether to freeze, but before updating the VM, to avoid setting the VM
                                541                 :                :      * bits incorrectly.
                                542                 :                :      */
  174                           543   [ +  +  +  - ]:         226610 :     prstate->set_all_frozen = prstate->attempt_freeze && prstate->attempt_set_vm;
  298                           544                 :         226610 : }
                                545                 :                : 
                                546                 :                : /*
                                547                 :                :  * Helper for heap_page_prune_and_freeze(). Iterates over every tuple on the
                                548                 :                :  * page, examines its visibility information, and determines the appropriate
                                549                 :                :  * action for each tuple. All tuples are processed and classified during this
                                550                 :                :  * phase, but no modifications are made to the page until the later execution
                                551                 :                :  * stage.
                                552                 :                :  *
                                553                 :                :  * *off_loc is used for error callback and cleared before returning.
                                554                 :                :  */
                                555                 :                : static void
  199                           556                 :         213574 : prune_freeze_plan(PruneState *prstate, OffsetNumber *off_loc)
                                557                 :                : {
                                558                 :         213574 :     Page        page = prstate->page;
                                559                 :         213574 :     BlockNumber blockno = prstate->block;
                                560                 :         213574 :     OffsetNumber maxoff = PageGetMaxOffsetNumber(prstate->page);
                                561                 :                :     OffsetNumber offnum;
                                562                 :                :     HeapTupleData tup;
                                563                 :                : 
                                564                 :         213574 :     tup.t_tableOid = RelationGetRelid(prstate->relation);
                                565                 :                : 
                                566                 :                :     /*
                                567                 :                :      * Determine HTSV for all tuples, and queue them up for processing as HOT
                                568                 :                :      * chain roots or as heap-only items.
                                569                 :                :      *
                                570                 :                :      * Determining HTSV only once for each tuple is required for correctness,
                                571                 :                :      * to deal with cases where running HTSV twice could result in different
                                572                 :                :      * results.  For example, RECENTLY_DEAD can turn to DEAD if another
                                573                 :                :      * checked item causes GlobalVisTestIsRemovableFullXid() to update the
                                574                 :                :      * horizon, or INSERT_IN_PROGRESS can change to DEAD if the inserting
                                575                 :                :      * transaction aborts.
                                576                 :                :      *
                                577                 :                :      * It's also good for performance. Most commonly tuples within a page are
                                578                 :                :      * stored at decreasing offsets (while the items are stored at increasing
                                579                 :                :      * offsets). When processing all tuples on a page this leads to reading
                                580                 :                :      * memory at decreasing offsets within a page, with a variable stride.
                                581                 :                :      * That's hard for CPU prefetchers to deal with. Processing the items in
                                582                 :                :      * reverse order (and thus the tuples in increasing order) increases
                                583                 :                :      * prefetching efficiency significantly / decreases the number of cache
                                584                 :                :      * misses.
                                585                 :                :      */
 1745 andres@anarazel.de        586                 :         213574 :     for (offnum = maxoff;
                                587         [ +  + ]:       21675977 :          offnum >= FirstOffsetNumber;
                                588                 :       21462403 :          offnum = OffsetNumberPrev(offnum))
                                589                 :                :     {
                                590                 :       21462403 :         ItemId      itemid = PageGetItemId(page, offnum);
                                591                 :                :         HeapTupleHeader htup;
                                592                 :                : 
                                593                 :                :         /*
                                594                 :                :          * Set the offset number so that we can display it along with any
                                595                 :                :          * error that occurred while processing this tuple.
                                596                 :                :          */
  902 heikki.linnakangas@i      597                 :       21462403 :         *off_loc = offnum;
                                598                 :                : 
  298 melanieplageman@gmai      599                 :       21462403 :         prstate->processed[offnum] = false;
                                600                 :       21462403 :         prstate->htsv[offnum] = -1;
                                601                 :                : 
                                602                 :                :         /* Nothing to do if slot doesn't contain a tuple */
  902 heikki.linnakangas@i      603         [ +  + ]:       21462403 :         if (!ItemIdIsUsed(itemid))
                                604                 :                :         {
  199 melanieplageman@gmai      605                 :         382118 :             heap_prune_record_unchanged_lp_unused(prstate, offnum);
 1745 andres@anarazel.de        606                 :         382118 :             continue;
                                607                 :                :         }
                                608                 :                : 
  902 heikki.linnakangas@i      609         [ +  + ]:       21080285 :         if (ItemIdIsDead(itemid))
                                610                 :                :         {
                                611                 :                :             /*
                                612                 :                :              * If the caller set mark_unused_now true, we can set dead line
                                613                 :                :              * pointers LP_UNUSED now.
                                614                 :                :              */
  298 melanieplageman@gmai      615         [ +  + ]:        1580703 :             if (unlikely(prstate->mark_unused_now))
                                616                 :           2140 :                 heap_prune_record_unused(prstate, offnum, false);
                                617                 :                :             else
  199                           618                 :        1578563 :                 heap_prune_record_unchanged_lp_dead(prstate, offnum);
  902 heikki.linnakangas@i      619                 :        1580703 :             continue;
                                620                 :                :         }
                                621                 :                : 
                                622         [ +  + ]:       19499582 :         if (ItemIdIsRedirected(itemid))
                                623                 :                :         {
                                624                 :                :             /* This is the start of a HOT chain */
  298 melanieplageman@gmai      625                 :         213596 :             prstate->root_items[prstate->nroot_items++] = offnum;
  902 heikki.linnakangas@i      626                 :         213596 :             continue;
                                627                 :                :         }
                                628                 :                : 
                                629         [ -  + ]:       19285986 :         Assert(ItemIdIsNormal(itemid));
                                630                 :                : 
                                631                 :                :         /*
                                632                 :                :          * Get the tuple's visibility status and queue it up for processing.
                                633                 :                :          */
                                634                 :       19285986 :         htup = (HeapTupleHeader) PageGetItem(page, itemid);
                                635                 :       19285986 :         tup.t_data = htup;
                                636                 :       19285986 :         tup.t_len = ItemIdGetLength(itemid);
                                637                 :       19285986 :         ItemPointerSet(&tup.t_self, blockno, offnum);
                                638                 :                : 
  199 melanieplageman@gmai      639                 :       19285986 :         prstate->htsv[offnum] = heap_prune_satisfies_vacuum(prstate, &tup);
                                640                 :                : 
  902 heikki.linnakangas@i      641         [ +  + ]:       19285986 :         if (!HeapTupleHeaderIsHeapOnly(htup))
  298 melanieplageman@gmai      642                 :       18927613 :             prstate->root_items[prstate->nroot_items++] = offnum;
                                643                 :                :         else
                                644                 :         358373 :             prstate->heaponly_items[prstate->nheaponly_items++] = offnum;
                                645                 :                :     }
                                646                 :                : 
                                647                 :                :     /*
                                648                 :                :      * Process HOT chains.
                                649                 :                :      *
                                650                 :                :      * We added the items to the array starting from 'maxoff', so by
                                651                 :                :      * processing the array in reverse order, we process the items in
                                652                 :                :      * ascending offset number order.  The order doesn't matter for
                                653                 :                :      * correctness, but some quick micro-benchmarking suggests that this is
                                654                 :                :      * faster.  (Earlier PostgreSQL versions, which scanned all the items on
                                655                 :                :      * the page instead of using the root_items array, also did it in
                                656                 :                :      * ascending offset number order.)
                                657                 :                :      */
                                658         [ +  + ]:       19354783 :     for (int i = prstate->nroot_items - 1; i >= 0; i--)
                                659                 :                :     {
                                660                 :       19141209 :         offnum = prstate->root_items[i];
                                661                 :                : 
                                662                 :                :         /* Ignore items already processed as part of an earlier chain */
                                663         [ -  + ]:       19141209 :         if (prstate->processed[offnum])
 6770 tgl@sss.pgh.pa.us         664                 :UBC           0 :             continue;
                                665                 :                : 
                                666                 :                :         /* see preceding loop */
  902 heikki.linnakangas@i      667                 :CBC    19141209 :         *off_loc = offnum;
                                668                 :                : 
                                669                 :                :         /* Process this item or chain of items */
  199 melanieplageman@gmai      670                 :       19141209 :         heap_prune_chain(maxoff, offnum, prstate);
                                671                 :                :     }
                                672                 :                : 
                                673                 :                :     /*
                                674                 :                :      * Process any heap-only tuples that were not already processed as part of
                                675                 :                :      * a HOT chain.
                                676                 :                :      */
  298                           677         [ +  + ]:         571947 :     for (int i = prstate->nheaponly_items - 1; i >= 0; i--)
                                678                 :                :     {
                                679                 :         358373 :         offnum = prstate->heaponly_items[i];
                                680                 :                : 
                                681         [ +  + ]:         358373 :         if (prstate->processed[offnum])
  902 heikki.linnakangas@i      682                 :         342622 :             continue;
                                683                 :                : 
                                684                 :                :         /* see preceding loop */
                                685                 :          15751 :         *off_loc = offnum;
                                686                 :                : 
                                687                 :                :         /*
                                688                 :                :          * If the tuple is DEAD and doesn't chain to anything else, mark it
                                689                 :                :          * unused.  (If it does chain, we can only remove it as part of
                                690                 :                :          * pruning its chain.)
                                691                 :                :          *
                                692                 :                :          * We need this primarily to handle aborted HOT updates, that is,
                                693                 :                :          * XMIN_INVALID heap-only tuples.  Those might not be linked to by any
                                694                 :                :          * chain, since the parent tuple might be re-updated before any
                                695                 :                :          * pruning occurs.  So we have to be able to reap them separately from
                                696                 :                :          * chain-pruning.  (Note that HeapTupleHeaderIsHotUpdated will never
                                697                 :                :          * return true for an XMIN_INVALID tuple, so this code will work even
                                698                 :                :          * when there were sequential updates within the aborted transaction.)
                                699                 :                :          */
  298 melanieplageman@gmai      700         [ +  + ]:          15751 :         if (prstate->htsv[offnum] == HEAPTUPLE_DEAD)
                                701                 :                :         {
  902 heikki.linnakangas@i      702                 :           3291 :             ItemId      itemid = PageGetItemId(page, offnum);
                                703                 :           3291 :             HeapTupleHeader htup = (HeapTupleHeader) PageGetItem(page, itemid);
                                704                 :                : 
                                705         [ +  - ]:           3291 :             if (likely(!HeapTupleHeaderIsHotUpdated(htup)))
                                706                 :                :             {
                                707                 :           3291 :                 HeapTupleHeaderAdvanceConflictHorizon(htup,
                                708                 :                :                                                       &prstate->latest_xid_removed);
  298 melanieplageman@gmai      709                 :           3291 :                 heap_prune_record_unused(prstate, offnum, true);
                                710                 :                :             }
                                711                 :                :             else
                                712                 :                :             {
                                713                 :                :                 /*
                                714                 :                :                  * This tuple should've been processed and removed as part of
                                715                 :                :                  * a HOT chain, so something's wrong.  To preserve evidence,
                                716                 :                :                  * we don't dare to remove it.  We cannot leave behind a DEAD
                                717                 :                :                  * tuple either, because that will cause VACUUM to error out.
                                718                 :                :                  * Throwing an error with a distinct error message seems like
                                719                 :                :                  * the least bad option.
                                720                 :                :                  */
  902 heikki.linnakangas@i      721         [ #  # ]:UBC           0 :                 elog(ERROR, "dead heap-only tuple (%u, %d) is not linked to from any HOT chain",
                                722                 :                :                      blockno, offnum);
                                723                 :                :             }
                                724                 :                :         }
                                725                 :                :         else
  199 melanieplageman@gmai      726                 :CBC       12460 :             heap_prune_record_unchanged_lp_normal(prstate, offnum);
                                727                 :                :     }
                                728                 :                : 
                                729                 :                :     /* We should now have processed every tuple exactly once  */
                                730                 :                : #ifdef USE_ASSERT_CHECKING
  902 heikki.linnakangas@i      731                 :         213574 :     for (offnum = FirstOffsetNumber;
                                732         [ +  + ]:       21675977 :          offnum <= maxoff;
                                733                 :       21462403 :          offnum = OffsetNumberNext(offnum))
                                734                 :                :     {
                                735                 :       21462403 :         *off_loc = offnum;
                                736                 :                : 
  298 melanieplageman@gmai      737         [ -  + ]:       21462403 :         Assert(prstate->processed[offnum]);
                                738                 :                :     }
                                739                 :                : #endif
                                740                 :                : 
                                741                 :                :     /* Clear the offset information once we have processed the given page. */
  902 heikki.linnakangas@i      742                 :         213574 :     *off_loc = InvalidOffsetNumber;
  298 melanieplageman@gmai      743                 :         213574 : }
                                744                 :                : 
                                745                 :                : /*
                                746                 :                :  * Decide whether to proceed with freezing according to the freeze plans
                                747                 :                :  * prepared for the current heap buffer. If freezing is chosen, this function
                                748                 :                :  * performs several pre-freeze checks.
                                749                 :                :  *
                                750                 :                :  * The values of do_prune, do_hint_prune, and did_tuple_hint_fpi must be
                                751                 :                :  * determined before calling this function.
                                752                 :                :  *
                                753                 :                :  * prstate is both an input and output parameter.
                                754                 :                :  *
                                755                 :                :  * Returns true if we should apply the freeze plans and freeze tuples on the
                                756                 :                :  * page, and false otherwise.
                                757                 :                :  */
                                758                 :                : static bool
  199                           759                 :         213574 : heap_page_will_freeze(bool did_tuple_hint_fpi,
                                760                 :                :                       bool do_prune,
                                761                 :                :                       bool do_hint_prune,
                                762                 :                :                       PruneState *prstate)
                                763                 :                : {
  298                           764                 :         213574 :     bool        do_freeze = false;
                                765                 :                : 
                                766                 :                :     /*
                                767                 :                :      * If the caller specified we should not attempt to freeze any tuples,
                                768                 :                :      * validate that everything is in the right state and return.
                                769                 :                :      */
                                770         [ +  + ]:         213574 :     if (!prstate->attempt_freeze)
                                771                 :                :     {
  199                           772   [ +  -  -  + ]:         131298 :         Assert(!prstate->set_all_frozen && prstate->nfrozen == 0);
  298                           773                 :         131298 :         return false;
                                774                 :                :     }
                                775                 :                : 
                                776         [ +  + ]:          82276 :     if (prstate->pagefrz.freeze_required)
                                777                 :                :     {
                                778                 :                :         /*
                                779                 :                :          * heap_prepare_freeze_tuple indicated that at least one XID/MXID from
                                780                 :                :          * before FreezeLimit/MultiXactCutoff is present.  Must freeze to
                                781                 :                :          * advance relfrozenxid/relminmxid.
                                782                 :                :          */
                                783                 :          20555 :         do_freeze = true;
                                784                 :                :     }
                                785                 :                :     else
                                786                 :                :     {
                                787                 :                :         /*
                                788                 :                :          * Opportunistically freeze the page if we are generating an FPI
                                789                 :                :          * anyway and if doing so means that we can set the page all-frozen
                                790                 :                :          * afterwards (might not happen until VACUUM's final heap pass).
                                791                 :                :          *
                                792                 :                :          * XXX: Previously, we knew if pruning emitted an FPI by checking
                                793                 :                :          * pgWalUsage.wal_fpi before and after pruning.  Once the freeze and
                                794                 :                :          * prune records were combined, this heuristic couldn't be used
                                795                 :                :          * anymore.  The opportunistic freeze heuristic must be improved;
                                796                 :                :          * however, for now, try to approximate the old logic.
                                797                 :                :          */
  199                           798   [ +  +  +  + ]:          61721 :         if (prstate->set_all_frozen && prstate->nfrozen > 0)
                                799                 :                :         {
                                800         [ -  + ]:          28804 :             Assert(prstate->set_all_visible);
                                801                 :                : 
                                802                 :                :             /*
                                803                 :                :              * Freezing would make the page all-frozen.  Have already emitted
                                804                 :                :              * an FPI or will do so anyway?
                                805                 :                :              */
                                806   [ +  +  +  +  :          28804 :             if (RelationNeedsWAL(prstate->relation))
                                        +  -  +  - ]
                                807                 :                :             {
  298                           808         [ +  + ]:          26763 :                 if (did_tuple_hint_fpi)
                                809                 :           1363 :                     do_freeze = true;
                                810         [ +  + ]:          25400 :                 else if (do_prune)
                                811                 :                :                 {
  199                           812         [ +  + ]:           4491 :                     if (XLogCheckBufferNeedsBackup(prstate->buffer))
  298                           813                 :           1063 :                         do_freeze = true;
                                814                 :                :                 }
                                815         [ +  + ]:          20909 :                 else if (do_hint_prune)
                                816                 :                :                 {
  199                           817   [ +  +  +  -  :          22920 :                     if (XLogHintBitIsNeeded() &&
                                              +  + ]
                                818                 :          11460 :                         XLogCheckBufferNeedsBackup(prstate->buffer))
  298                           819                 :           2168 :                         do_freeze = true;
                                820                 :                :                 }
                                821                 :                :             }
                                822                 :                :         }
                                823                 :                :     }
                                824                 :                : 
                                825         [ +  + ]:          82276 :     if (do_freeze)
                                826                 :                :     {
                                827                 :                :         /*
                                828                 :                :          * Validate the tuples we will be freezing before entering the
                                829                 :                :          * critical section.
                                830                 :                :          */
  199                           831                 :          25149 :         heap_pre_freeze_checks(prstate->buffer, prstate->frozen, prstate->nfrozen);
  194                           832         [ -  + ]:          25149 :         Assert(TransactionIdPrecedes(prstate->pagefrz.FreezePageConflictXid,
                                833                 :                :                                      prstate->cutoffs->OldestXmin));
                                834                 :                :     }
  298                           835         [ +  + ]:          57127 :     else if (prstate->nfrozen > 0)
                                836                 :                :     {
                                837                 :                :         /*
                                838                 :                :          * The page contained some tuples that were not already frozen, and we
                                839                 :                :          * chose not to freeze them now.  The page won't be all-frozen then.
                                840                 :                :          */
                                841         [ -  + ]:          26215 :         Assert(!prstate->pagefrz.freeze_required);
                                842                 :                : 
  199                           843                 :          26215 :         prstate->set_all_frozen = false;
  298                           844                 :          26215 :         prstate->nfrozen = 0;    /* avoid miscounts in instrumentation */
                                845                 :                :     }
                                846                 :                :     else
                                847                 :                :     {
                                848                 :                :         /*
                                849                 :                :          * We have no freeze plans to execute.  The page might already be
                                850                 :                :          * all-frozen (perhaps only following pruning), though.  Such pages
                                851                 :                :          * can be marked all-frozen in the VM by our caller, even though none
                                852                 :                :          * of its tuples were newly frozen here.
                                853                 :                :          */
                                854                 :                :     }
                                855                 :                : 
                                856                 :          82276 :     return do_freeze;
                                857                 :                : }
                                858                 :                : 
                                859                 :                : /*
                                860                 :                :  * Emit a warning about and fix visibility map corruption on the given page.
                                861                 :                :  *
                                862                 :                :  * The caller specifies the type of corruption it has already detected via
                                863                 :                :  * corruption_type, so that we can emit the appropriate warning. All cases
                                864                 :                :  * result in the VM bits being cleared; corruption types where PD_ALL_VISIBLE
                                865                 :                :  * is incorrectly set also clear PD_ALL_VISIBLE.
                                866                 :                :  *
                                867                 :                :  * Must be called while holding an exclusive lock on the heap buffer. Dead
                                868                 :                :  * items and not all-visible tuples must have been discovered under that same
                                869                 :                :  * lock. Although we do not hold a lock on the VM buffer, it is pinned, and
                                870                 :                :  * the heap buffer is exclusively locked, ensuring that no other backend can
                                871                 :                :  * update the VM bits corresponding to this heap page.
                                872                 :                :  *
                                873                 :                :  * This function makes changes to the VM and, potentially, the heap page, but
                                874                 :                :  * it does not need to be done in a critical section.
                                875                 :                :  */
                                876                 :                : static void
  182 melanieplageman@gmai      877                 :UBC           0 : heap_page_fix_vm_corruption(PruneState *prstate, OffsetNumber offnum,
                                878                 :                :                             VMCorruptionType corruption_type)
                                879                 :                : {
                                880                 :              0 :     const char *relname = RelationGetRelationName(prstate->relation);
                                881                 :              0 :     bool        do_clear_vm = false;
                                882                 :              0 :     bool        do_clear_heap = false;
                                883                 :                : 
                                884         [ #  # ]:              0 :     Assert(BufferIsLockedByMeInMode(prstate->buffer, BUFFER_LOCK_EXCLUSIVE));
                                885                 :                : 
                                886   [ #  #  #  # ]:              0 :     switch (corruption_type)
                                887                 :                :     {
                                888                 :              0 :         case VM_CORRUPT_LPDEAD:
                                889         [ #  # ]:              0 :             ereport(WARNING,
                                890                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                891                 :                :                      errmsg("dead line pointer found on page marked all-visible"),
                                892                 :                :                      errcontext("relation \"%s\", page %u, tuple %u",
                                893                 :                :                                 relname, prstate->block, offnum)));
                                894                 :              0 :             do_clear_vm = true;
                                895                 :              0 :             do_clear_heap = true;
                                896                 :              0 :             break;
                                897                 :                : 
                                898                 :              0 :         case VM_CORRUPT_TUPLE_VISIBILITY:
                                899                 :                : 
                                900                 :                :             /*
                                901                 :                :              * A HEAPTUPLE_LIVE tuple on an all-visible page can appear to not
                                902                 :                :              * be visible to everyone when
                                903                 :                :              * GetOldestNonRemovableTransactionId() returns a conservative
                                904                 :                :              * value that's older than the real safe xmin. That is not
                                905                 :                :              * corruption -- the PD_ALL_VISIBLE flag is still correct.
                                906                 :                :              *
                                907                 :                :              * However, dead tuple versions, in-progress inserts, and
                                908                 :                :              * in-progress deletes should never appear on a page marked
                                909                 :                :              * all-visible. That indicates real corruption. PD_ALL_VISIBLE
                                910                 :                :              * should have been cleared by the DML operation that deleted or
                                911                 :                :              * inserted the tuple.
                                912                 :                :              */
                                913         [ #  # ]:              0 :             ereport(WARNING,
                                914                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                915                 :                :                      errmsg("tuple not visible to all transactions found on page marked all-visible"),
                                916                 :                :                      errcontext("relation \"%s\", page %u, tuple %u",
                                917                 :                :                                 relname, prstate->block, offnum)));
                                918                 :              0 :             do_clear_vm = true;
                                919                 :              0 :             do_clear_heap = true;
                                920                 :              0 :             break;
                                921                 :                : 
                                922                 :              0 :         case VM_CORRUPT_MISSING_PAGE_HINT:
                                923                 :                : 
                                924                 :                :             /*
                                925                 :                :              * As of PostgreSQL 9.2, the visibility map bit should never be
                                926                 :                :              * set if the page-level bit is clear. However, for vacuum, it's
                                927                 :                :              * possible that the bit got cleared after
                                928                 :                :              * heap_vac_scan_next_block() was called, so we must recheck now
                                929                 :                :              * that we have the buffer lock before concluding that the VM is
                                930                 :                :              * corrupt.
                                931                 :                :              */
                                932         [ #  # ]:              0 :             Assert(!PageIsAllVisible(prstate->page));
                                933         [ #  # ]:              0 :             Assert(prstate->old_vmbits & VISIBILITYMAP_VALID_BITS);
                                934         [ #  # ]:              0 :             ereport(WARNING,
                                935                 :                :                     (errcode(ERRCODE_DATA_CORRUPTED),
                                936                 :                :                      errmsg("page is not marked all-visible but visibility map bit is set"),
                                937                 :                :                      errcontext("relation \"%s\", page %u",
                                938                 :                :                                 relname, prstate->block)));
                                939                 :              0 :             do_clear_vm = true;
                                940                 :              0 :             break;
                                941                 :                :     }
                                942                 :                : 
                                943   [ #  #  #  # ]:              0 :     Assert(do_clear_heap || do_clear_vm);
                                944                 :                : 
                                945                 :                :     /* Avoid marking the buffer dirty if PD_ALL_VISIBLE is already clear */
                                946         [ #  # ]:              0 :     if (do_clear_heap)
                                947                 :                :     {
                                948         [ #  # ]:              0 :         Assert(PageIsAllVisible(prstate->page));
                                949                 :              0 :         PageClearAllVisible(prstate->page);
                                950                 :              0 :         MarkBufferDirtyHint(prstate->buffer, true);
                                951                 :                :     }
                                952                 :                : 
                                953         [ #  # ]:              0 :     if (do_clear_vm)
                                954                 :                :     {
   67                           955                 :              0 :         LockBuffer(prstate->vmbuffer, BUFFER_LOCK_EXCLUSIVE);
                                956                 :                :         /* This VM clear is not WAL-logged, so its return value is not needed. */
   47 melanieplageman@gmai      957                 :UNC           0 :         (void) visibilitymap_clear(prstate->relation->rd_locator,
                                958                 :                :                                    prstate->block, prstate->vmbuffer,
                                959                 :                :                                    VISIBILITYMAP_VALID_BITS);
   67 melanieplageman@gmai      960                 :UBC           0 :         LockBuffer(prstate->vmbuffer, BUFFER_LOCK_UNLOCK);
  182                           961                 :              0 :         prstate->old_vmbits = 0;
                                962                 :                :     }
                                963                 :              0 : }
                                964                 :                : 
                                965                 :                : /*
                                966                 :                :  * Decide whether to set the visibility map bits (all-visible and all-frozen)
                                967                 :                :  * for the current page using information from the PruneState and VM.
                                968                 :                :  *
                                969                 :                :  * This function does not actually set the VM bits or page-level visibility
                                970                 :                :  * hint, PD_ALL_VISIBLE.
                                971                 :                :  *
                                972                 :                :  * This should be called only after do_freeze has been decided (and do_prune
                                973                 :                :  * has been set), as these factor into our heuristic-based decision.
                                974                 :                :  *
                                975                 :                :  * Returns true if one or both VM bits should be set and false otherwise.
                                976                 :                :  */
                                977                 :                : static bool
  174 melanieplageman@gmai      978                 :CBC      213574 : heap_page_will_set_vm(PruneState *prstate, PruneReason reason,
                                979                 :                :                       bool do_prune, bool do_freeze)
                                980                 :                : {
                                981         [ +  + ]:         213574 :     if (!prstate->attempt_set_vm)
  180                           982                 :          94762 :         return false;
                                983                 :                : 
                                984         [ +  + ]:         118812 :     if (!prstate->set_all_visible)
                                985                 :          38329 :         return false;
                                986                 :                : 
                                987                 :                :     /*
                                988                 :                :      * If this is an on-access call and we're not actually pruning, avoid
                                989                 :                :      * setting the visibility map if it would newly dirty the heap page or, if
                                990                 :                :      * the page is already dirty, if doing so would require including a
                                991                 :                :      * full-page image (FPI) of the heap page in the WAL.
                                992                 :                :      */
  174                           993   [ +  +  +  +  :          80483 :     if (reason == PRUNE_ON_ACCESS && !do_prune && !do_freeze &&
                                              +  - ]
                                994   [ +  +  +  + ]:          29025 :         (!BufferIsDirty(prstate->buffer) || XLogCheckBufferNeedsBackup(prstate->buffer)))
                                995                 :                :     {
                                996                 :          15428 :         prstate->set_all_visible = prstate->set_all_frozen = false;
                                997                 :          15428 :         return false;
                                998                 :                :     }
                                999                 :                : 
  180                          1000                 :          65055 :     prstate->new_vmbits = VISIBILITYMAP_ALL_VISIBLE;
                               1001                 :                : 
                               1002         [ +  + ]:          65055 :     if (prstate->set_all_frozen)
                               1003                 :          34129 :         prstate->new_vmbits |= VISIBILITYMAP_ALL_FROZEN;
                               1004                 :                : 
                               1005         [ +  + ]:          65055 :     if (prstate->new_vmbits == prstate->old_vmbits)
                               1006                 :                :     {
                               1007                 :           2691 :         prstate->new_vmbits = 0;
                               1008                 :           2691 :         return false;
                               1009                 :                :     }
                               1010                 :                : 
                               1011                 :          62364 :     return true;
                               1012                 :                : }
                               1013                 :                : 
                               1014                 :                : /*
                               1015                 :                :  * If the page is already all-frozen, or already all-visible and freezing
                               1016                 :                :  * won't be attempted, there is no remaining work and we can use the fast path
                               1017                 :                :  * to avoid the expensive overhead of heap_page_prune_and_freeze().
                               1018                 :                :  *
                               1019                 :                :  * This can happen when the page has a stale prune hint, or if VACUUM is
                               1020                 :                :  * scanning an already all-frozen page due to SKIP_PAGES_THRESHOLD.
                               1021                 :                :  *
                               1022                 :                :  * The caller must already have examined the visibility map and saved the
                               1023                 :                :  * status of the page's VM bits in prstate->old_vmbits. Caller must hold a
                               1024                 :                :  * content lock on the heap page since it will examine line pointers.
                               1025                 :                :  *
                               1026                 :                :  * Before calling prune_freeze_fast_path(), the caller should first
                               1027                 :                :  * check for and fix any discrepancy between the page-level visibility hint
                               1028                 :                :  * and the visibility map. Otherwise, the fast path will always prevent us
                               1029                 :                :  * from getting them in sync. Note that if there are tuples on the page that
                               1030                 :                :  * are not visible to all but the VM is incorrectly marked
                               1031                 :                :  * all-visible/all-frozen, we will not get the chance to fix that corruption
                               1032                 :                :  * when using the fast path.
                               1033                 :                :  */
                               1034                 :                : static void
  182                          1035                 :          13036 : prune_freeze_fast_path(PruneState *prstate, PruneFreezeResult *presult)
                               1036                 :                : {
                               1037                 :          13036 :     OffsetNumber maxoff = PageGetMaxOffsetNumber(prstate->page);
                               1038                 :          13036 :     Page        page = prstate->page;
                               1039                 :                : 
                               1040   [ -  +  -  -  :          13036 :     Assert((prstate->old_vmbits & VISIBILITYMAP_ALL_FROZEN) ||
                                              -  - ]
                               1041                 :                :            ((prstate->old_vmbits & VISIBILITYMAP_ALL_VISIBLE) &&
                               1042                 :                :             !prstate->attempt_freeze));
                               1043                 :                : 
                               1044                 :                :     /* We'll fill in presult for the caller */
                               1045                 :          13036 :     memset(presult, 0, sizeof(PruneFreezeResult));
                               1046                 :                : 
                               1047                 :                :     /* Clear any stale prune hint */
                               1048         [ -  + ]:          13036 :     if (TransactionIdIsValid(PageGetPruneXid(page)))
                               1049                 :                :     {
  182 melanieplageman@gmai     1050                 :UBC           0 :         PageClearPrunable(page);
                               1051                 :              0 :         MarkBufferDirtyHint(prstate->buffer, true);
                               1052                 :                :     }
                               1053                 :                : 
  182 melanieplageman@gmai     1054         [ -  + ]:CBC       13036 :     if (PageIsEmpty(page))
  182 melanieplageman@gmai     1055                 :UBC           0 :         return;
                               1056                 :                : 
                               1057                 :                :     /*
                               1058                 :                :      * Since the page is all-visible, a count of the normal ItemIds on the
                               1059                 :                :      * page should be sufficient for vacuum's live tuple count.
                               1060                 :                :      */
  182 melanieplageman@gmai     1061                 :CBC       13036 :     for (OffsetNumber off = FirstOffsetNumber;
                               1062         [ +  + ]:         770245 :          off <= maxoff;
                               1063                 :         757209 :          off = OffsetNumberNext(off))
                               1064                 :                :     {
                               1065                 :         757209 :         ItemId      lp = PageGetItemId(page, off);
                               1066                 :                : 
                               1067         [ +  + ]:         757209 :         if (!ItemIdIsUsed(lp))
                               1068                 :          21173 :             continue;
                               1069                 :                : 
                               1070                 :         736036 :         presult->hastup = true;
                               1071                 :                : 
                               1072         [ +  + ]:         736036 :         if (ItemIdIsNormal(lp))
                               1073                 :         727822 :             prstate->live_tuples++;
                               1074                 :                :     }
                               1075                 :                : 
                               1076                 :          13036 :     presult->live_tuples = prstate->live_tuples;
                               1077                 :                : }
                               1078                 :                : 
                               1079                 :                : /*
                               1080                 :                :  * Prune and repair fragmentation and potentially freeze tuples on the
                               1081                 :                :  * specified page. If the page's visibility status has changed, update it in
                               1082                 :                :  * the VM.
                               1083                 :                :  *
                               1084                 :                :  * Caller must have pin and buffer cleanup lock on the page.  Note that we
                               1085                 :                :  * don't update the FSM information for page on caller's behalf.  Caller might
                               1086                 :                :  * also need to account for a reduction in the length of the line pointer
                               1087                 :                :  * array following array truncation by us.
                               1088                 :                :  *
                               1089                 :                :  * params contains the input parameters used to control freezing and pruning
                               1090                 :                :  * behavior. See the definition of PruneFreezeParams for more on what each
                               1091                 :                :  * parameter does.
                               1092                 :                :  *
                               1093                 :                :  * If the HEAP_PAGE_PRUNE_FREEZE option is set in params, we will freeze
                               1094                 :                :  * tuples if it's required in order to advance relfrozenxid / relminmxid, or
                               1095                 :                :  * if it's considered advantageous for overall system performance to do so
                               1096                 :                :  * now.  The 'params.cutoffs', 'presult', 'new_relfrozen_xid' and
                               1097                 :                :  * 'new_relmin_mxid' arguments are required when freezing.
                               1098                 :                :  *
                               1099                 :                :  * A vmbuffer corresponding to the heap page is also passed and if the page is
                               1100                 :                :  * found to be all-visible/all-frozen, we will set it in the VM.
                               1101                 :                :  *
                               1102                 :                :  * presult contains output parameters needed by callers, such as the number of
                               1103                 :                :  * tuples removed and the offsets of dead items on the page after pruning.
                               1104                 :                :  * heap_page_prune_and_freeze() is responsible for initializing it.  Required
                               1105                 :                :  * by all callers.
                               1106                 :                :  *
                               1107                 :                :  * off_loc is the offset location required by the caller to use in error
                               1108                 :                :  * callback.
                               1109                 :                :  *
                               1110                 :                :  * new_relfrozen_xid and new_relmin_mxid must be provided by the caller if the
                               1111                 :                :  * HEAP_PAGE_PRUNE_FREEZE option is set in params.  On entry, they contain the
                               1112                 :                :  * oldest XID and multi-XID seen on the relation so far.  They will be updated
                               1113                 :                :  * with the oldest values present on the page after pruning.  After processing
                               1114                 :                :  * the whole relation, VACUUM can use these values as the new
                               1115                 :                :  * relfrozenxid/relminmxid for the relation.
                               1116                 :                :  */
                               1117                 :                : void
  298                          1118                 :         226610 : heap_page_prune_and_freeze(PruneFreezeParams *params,
                               1119                 :                :                            PruneFreezeResult *presult,
                               1120                 :                :                            OffsetNumber *off_loc,
                               1121                 :                :                            TransactionId *new_relfrozen_xid,
                               1122                 :                :                            MultiXactId *new_relmin_mxid)
                               1123                 :                : {
                               1124                 :                :     PruneState  prstate;
                               1125                 :                :     bool        do_freeze;
                               1126                 :                :     bool        do_prune;
                               1127                 :                :     bool        do_hint_prune;
                               1128                 :                :     bool        do_set_vm;
                               1129                 :                :     bool        did_tuple_hint_fpi;
                               1130                 :         226610 :     int64       fpi_before = pgWalUsage.wal_fpi;
                               1131                 :                :     TransactionId conflict_xid;
                               1132                 :                : 
                               1133                 :                :     /* Initialize prstate */
                               1134                 :         226610 :     prune_freeze_setup(params,
                               1135                 :                :                        new_relfrozen_xid, new_relmin_mxid,
                               1136                 :                :                        presult, &prstate);
                               1137                 :                : 
                               1138                 :                :     /*
                               1139                 :                :      * If the VM is set but PD_ALL_VISIBLE is clear, fix that corruption
                               1140                 :                :      * before pruning and freezing so that the page and VM start out in a
                               1141                 :                :      * consistent state.
                               1142                 :                :      */
  182                          1143         [ +  + ]:         226610 :     if ((prstate.old_vmbits & VISIBILITYMAP_VALID_BITS) &&
                               1144         [ -  + ]:          19349 :         !PageIsAllVisible(prstate.page))
  182 melanieplageman@gmai     1145                 :UBC           0 :         heap_page_fix_vm_corruption(&prstate, InvalidOffsetNumber,
                               1146                 :                :                                     VM_CORRUPT_MISSING_PAGE_HINT);
                               1147                 :                : 
                               1148                 :                :     /*
                               1149                 :                :      * If the page is already all-frozen, or already all-visible when freezing
                               1150                 :                :      * is not being attempted, take the fast path, skipping pruning and
                               1151                 :                :      * freezing code entirely. This must be done after fixing any discrepancy
                               1152                 :                :      * between the page-level visibility hint and the VM, since that may have
                               1153                 :                :      * cleared old_vmbits.
                               1154                 :                :      */
  182 melanieplageman@gmai     1155         [ +  + ]:CBC      226610 :     if ((params->options & HEAP_PAGE_PRUNE_ALLOW_FAST_PATH) != 0 &&
                               1156         [ +  + ]:         225314 :         ((prstate.old_vmbits & VISIBILITYMAP_ALL_FROZEN) ||
                               1157         [ +  + ]:         212278 :          ((prstate.old_vmbits & VISIBILITYMAP_ALL_VISIBLE) &&
                               1158         [ -  + ]:           5868 :           !prstate.attempt_freeze)))
                               1159                 :                :     {
                               1160                 :          13036 :         prune_freeze_fast_path(&prstate, presult);
                               1161                 :          13036 :         return;
                               1162                 :                :     }
                               1163                 :                : 
                               1164                 :                :     /*
                               1165                 :                :      * Examine all line pointers and tuple visibility information to determine
                               1166                 :                :      * which line pointers should change state and which tuples may be frozen.
                               1167                 :                :      * Prepare queue of state changes to later be executed in a critical
                               1168                 :                :      * section.
                               1169                 :                :      */
  199                          1170                 :         213574 :     prune_freeze_plan(&prstate, off_loc);
                               1171                 :                : 
                               1172                 :                :     /*
                               1173                 :                :      * After processing all the live tuples on the page, if the newest xmin
                               1174                 :                :      * amongst them may be considered running by any snapshot, the page cannot
                               1175                 :                :      * be all-visible. This should be done before determining whether or not
                               1176                 :                :      * to opportunistically freeze.
                               1177                 :                :      */
  180                          1178         [ +  + ]:         213574 :     if (prstate.set_all_visible &&
                               1179   [ +  +  +  + ]:         199804 :         TransactionIdIsNormal(prstate.newest_live_xid) &&
                               1180                 :          85933 :         GlobalVisTestXidConsideredRunning(prstate.vistest,
                               1181                 :                :                                           prstate.newest_live_xid,
                               1182                 :                :                                           true))
                               1183                 :           3360 :         prstate.set_all_visible = prstate.set_all_frozen = false;
                               1184                 :                : 
                               1185                 :                :     /*
                               1186                 :                :      * If checksums are enabled, calling heap_prune_satisfies_vacuum() while
                               1187                 :                :      * checking tuple visibility information in prune_freeze_plan() may have
                               1188                 :                :      * caused an FPI to be emitted.
                               1189                 :                :      */
  298                          1190                 :         213574 :     did_tuple_hint_fpi = fpi_before != pgWalUsage.wal_fpi;
                               1191                 :                : 
  900 heikki.linnakangas@i     1192                 :         622642 :     do_prune = prstate.nredirected > 0 ||
                               1193   [ +  +  +  + ]:         354110 :         prstate.ndead > 0 ||
                               1194         [ +  + ]:         140536 :         prstate.nunused > 0;
                               1195                 :                : 
                               1196                 :                :     /*
                               1197                 :                :      * Even if we don't prune anything, if we found a new value for the
                               1198                 :                :      * pd_prune_xid field or the page was marked full, we will update the hint
                               1199                 :                :      * bit.
                               1200                 :                :      */
  199 melanieplageman@gmai     1201   [ +  +  +  + ]:         249596 :     do_hint_prune = PageGetPruneXid(prstate.page) != prstate.new_prune_xid ||
                               1202                 :          36022 :         PageIsFull(prstate.page);
                               1203                 :                : 
                               1204                 :                :     /*
                               1205                 :                :      * Decide if we want to go ahead with freezing according to the freeze
                               1206                 :                :      * plans we prepared, or not.
                               1207                 :                :      */
                               1208                 :         213574 :     do_freeze = heap_page_will_freeze(did_tuple_hint_fpi,
                               1209                 :                :                                       do_prune,
                               1210                 :                :                                       do_hint_prune,
                               1211                 :                :                                       &prstate);
                               1212                 :                : 
                               1213                 :                :     /*
                               1214                 :                :      * While scanning the line pointers, we did not clear
                               1215                 :                :      * set_all_visible/set_all_frozen when encountering LP_DEAD items because
                               1216                 :                :      * we wanted the decision whether or not to freeze the page to be
                               1217                 :                :      * unaffected by the short-term presence of LP_DEAD items.  These LP_DEAD
                               1218                 :                :      * items are effectively assumed to be LP_UNUSED items in the making.  It
                               1219                 :                :      * doesn't matter which vacuum heap pass (initial pass or final pass) ends
                               1220                 :                :      * up setting the page all-frozen, as long as the ongoing VACUUM does it.
                               1221                 :                :      *
                               1222                 :                :      * Now that we finished determining whether or not to freeze the page,
                               1223                 :                :      * update set_all_visible and set_all_frozen so that they reflect the true
                               1224                 :                :      * state of the page for setting PD_ALL_VISIBLE and VM bits.
                               1225                 :                :      */
  304                          1226         [ +  + ]:         213574 :     if (prstate.lpdead_items > 0)
  199                          1227                 :          78165 :         prstate.set_all_visible = prstate.set_all_frozen = false;
                               1228                 :                : 
                               1229   [ +  +  -  + ]:         213574 :     Assert(!prstate.set_all_frozen || prstate.set_all_visible);
  174                          1230   [ +  +  -  + ]:         213574 :     Assert(!prstate.set_all_visible || prstate.attempt_set_vm);
  180                          1231   [ +  +  -  + ]:         213574 :     Assert(!prstate.set_all_visible || (prstate.lpdead_items == 0));
                               1232                 :                : 
  174                          1233                 :         213574 :     do_set_vm = heap_page_will_set_vm(&prstate, params->reason, do_prune, do_freeze);
                               1234                 :                : 
                               1235                 :                :     /*
                               1236                 :                :      * new_vmbits should be 0 regardless of whether or not the page is
                               1237                 :                :      * all-visible if we do not intend to set the VM.
                               1238                 :                :      */
  180                          1239   [ +  +  -  + ]:         213574 :     Assert(do_set_vm || prstate.new_vmbits == 0);
                               1240                 :                : 
                               1241                 :                :     /*
                               1242                 :                :      * The snapshot conflict horizon for the whole record is the most
                               1243                 :                :      * conservative (newest) horizon required by any change in the record.
                               1244                 :                :      */
                               1245                 :         213574 :     conflict_xid = InvalidTransactionId;
                               1246         [ +  + ]:         213574 :     if (do_set_vm)
                               1247                 :          62364 :         conflict_xid = prstate.newest_live_xid;
                               1248   [ +  +  +  + ]:         213574 :     if (do_freeze && TransactionIdFollows(prstate.pagefrz.FreezePageConflictXid, conflict_xid))
                               1249                 :           2709 :         conflict_xid = prstate.pagefrz.FreezePageConflictXid;
                               1250   [ +  +  +  + ]:         213574 :     if (do_prune && TransactionIdFollows(prstate.latest_xid_removed, conflict_xid))
                               1251                 :          64589 :         conflict_xid = prstate.latest_xid_removed;
                               1252                 :                : 
                               1253                 :                :     /* Lock vmbuffer before entering a critical section */
                               1254         [ +  + ]:         213574 :     if (do_set_vm)
                               1255                 :          62364 :         LockBuffer(prstate.vmbuffer, BUFFER_LOCK_EXCLUSIVE);
                               1256                 :                : 
                               1257                 :                :     /* Any error while applying the changes is critical */
  900 heikki.linnakangas@i     1258                 :         213574 :     START_CRIT_SECTION();
                               1259                 :                : 
  341 melanieplageman@gmai     1260         [ +  + ]:         213574 :     if (do_hint_prune)
                               1261                 :                :     {
                               1262                 :                :         /*
                               1263                 :                :          * Update the page's pd_prune_xid field to either zero, or the lowest
                               1264                 :                :          * XID of any soon-prunable tuple.
                               1265                 :                :          */
  199                          1266                 :         177778 :         ((PageHeader) prstate.page)->pd_prune_xid = prstate.new_prune_xid;
                               1267                 :                : 
                               1268                 :                :         /*
                               1269                 :                :          * Also clear the "page is full" flag, since there's no point in
                               1270                 :                :          * repeating the prune/defrag process until something else happens to
                               1271                 :                :          * the page.
                               1272                 :                :          */
                               1273                 :         177778 :         PageClearFull(prstate.page);
                               1274                 :                : 
                               1275                 :                :         /*
                               1276                 :                :          * If that's all we had to do to the page, this is a non-WAL-logged
                               1277                 :                :          * hint. If we are going to freeze or prune the page or set
                               1278                 :                :          * PD_ALL_VISIBLE, we will mark the buffer dirty below.
                               1279                 :                :          *
                               1280                 :                :          * Setting PD_ALL_VISIBLE is fully WAL-logged because it is forbidden
                               1281                 :                :          * for the VM to be set and PD_ALL_VISIBLE to be clear.
                               1282                 :                :          */
  180                          1283   [ +  +  +  +  :         177778 :         if (!do_freeze && !do_prune && !do_set_vm)
                                              +  + ]
  199                          1284                 :          66541 :             MarkBufferDirtyHint(prstate.buffer, true);
                               1285                 :                :     }
                               1286                 :                : 
  180                          1287   [ +  +  +  +  :         213574 :     if (do_prune || do_freeze || do_set_vm)
                                              +  + ]
                               1288                 :                :     {
                               1289                 :                :         /* Apply the planned item changes and repair page fragmentation. */
  900 heikki.linnakangas@i     1290         [ +  + ]:         135268 :         if (do_prune)
                               1291                 :                :         {
  199 melanieplageman@gmai     1292                 :          73490 :             heap_page_prune_execute(prstate.buffer, false,
                               1293                 :                :                                     prstate.redirected, prstate.nredirected,
                               1294                 :                :                                     prstate.nowdead, prstate.ndead,
                               1295                 :                :                                     prstate.nowunused, prstate.nunused);
                               1296                 :                :         }
                               1297                 :                : 
  900 heikki.linnakangas@i     1298         [ +  + ]:         135268 :         if (do_freeze)
  199 melanieplageman@gmai     1299                 :          25149 :             heap_freeze_prepared_tuples(prstate.buffer, prstate.frozen, prstate.nfrozen);
                               1300                 :                : 
                               1301                 :                :         /* Set the visibility map and page visibility hint */
  180                          1302         [ +  + ]:         135268 :         if (do_set_vm)
                               1303                 :                :         {
                               1304                 :                :             /*
                               1305                 :                :              * While it is valid for PD_ALL_VISIBLE to be set when the
                               1306                 :                :              * corresponding VM bit is clear, we strongly prefer to keep them
                               1307                 :                :              * in sync.
                               1308                 :                :              *
                               1309                 :                :              * The heap buffer must be marked dirty before adding it to the
                               1310                 :                :              * WAL chain when setting the VM. We don't worry about
                               1311                 :                :              * unnecessarily dirtying the heap buffer if PD_ALL_VISIBLE is
                               1312                 :                :              * already set, though. It is extremely rare to have a clean heap
                               1313                 :                :              * buffer with PD_ALL_VISIBLE already set and the VM bits clear,
                               1314                 :                :              * so there is no point in optimizing it.
                               1315                 :                :              */
                               1316                 :          62364 :             PageSetAllVisible(prstate.page);
                               1317                 :          62364 :             PageClearPrunable(prstate.page);
    6                          1318                 :          62364 :             (void) visibilitymap_set(prstate.block, prstate.vmbuffer,
                               1319                 :          62364 :                                      prstate.new_vmbits,
                               1320                 :          62364 :                                      prstate.relation->rd_locator);
                               1321                 :                :         }
                               1322                 :                : 
  199                          1323                 :         135268 :         MarkBufferDirty(prstate.buffer);
                               1324                 :                : 
                               1325                 :                :         /*
                               1326                 :                :          * Emit a WAL XLOG_HEAP2_PRUNE* record showing what we did
                               1327                 :                :          */
                               1328   [ +  +  +  +  :         135268 :         if (RelationNeedsWAL(prstate.relation))
                                        +  -  +  - ]
                               1329                 :                :         {
                               1330   [ +  +  +  + ]:         192385 :             log_heap_prune_and_freeze(prstate.relation, prstate.buffer,
                               1331                 :                :                                       do_set_vm ? prstate.vmbuffer : InvalidBuffer,
  180                          1332                 :          60389 :                                       do_set_vm ? prstate.new_vmbits : 0,
                               1333                 :                :                                       conflict_xid,
                               1334                 :                :                                       true, /* cleanup lock */
                               1335                 :                :                                       params->reason,
                               1336                 :                :                                       prstate.frozen, prstate.nfrozen,
                               1337                 :                :                                       prstate.redirected, prstate.nredirected,
                               1338                 :                :                                       prstate.nowdead, prstate.ndead,
                               1339                 :                :                                       prstate.nowunused, prstate.nunused);
                               1340                 :                :         }
                               1341                 :                :     }
                               1342                 :                : 
 6940 tgl@sss.pgh.pa.us        1343         [ -  + ]:         213574 :     END_CRIT_SECTION();
                               1344                 :                : 
  180 melanieplageman@gmai     1345         [ +  + ]:         213574 :     if (do_set_vm)
                               1346                 :          62364 :         LockBuffer(prstate.vmbuffer, BUFFER_LOCK_UNLOCK);
                               1347                 :                : 
                               1348                 :                :     /*
                               1349                 :                :      * During its second pass over the heap, VACUUM calls
                               1350                 :                :      * heap_page_would_be_all_visible() to determine whether a page is
                               1351                 :                :      * all-visible and all-frozen. The logic here is similar. After completing
                               1352                 :                :      * pruning and freezing, use an assertion to verify that our results
                               1353                 :                :      * remain consistent with heap_page_would_be_all_visible(). It's also a
                               1354                 :                :      * valuable cross-check of the page state after pruning and freezing.
                               1355                 :                :      */
                               1356                 :                : #ifdef USE_ASSERT_CHECKING
                               1357         [ +  + ]:         213574 :     if (prstate.set_all_visible)
                               1358                 :                :     {
                               1359                 :                :         TransactionId debug_cutoff;
                               1360                 :                :         bool        debug_all_frozen;
                               1361                 :                : 
                               1362         [ -  + ]:          65055 :         Assert(prstate.lpdead_items == 0);
                               1363                 :                : 
                               1364         [ -  + ]:          65055 :         Assert(heap_page_is_all_visible(prstate.relation, prstate.buffer,
                               1365                 :                :                                         prstate.vistest,
                               1366                 :                :                                         &debug_all_frozen,
                               1367                 :                :                                         &debug_cutoff, off_loc));
                               1368                 :                : 
                               1369   [ +  +  -  + ]:          65055 :         Assert(!TransactionIdIsValid(debug_cutoff) ||
                               1370                 :                :                debug_cutoff == prstate.newest_live_xid);
                               1371                 :                : 
                               1372                 :                :         /*
                               1373                 :                :          * It's possible the page is composed entirely of frozen tuples but is
                               1374                 :                :          * not set all-frozen in the VM and did not pass
                               1375                 :                :          * HEAP_PAGE_PRUNE_FREEZE. In this case, it's possible
                               1376                 :                :          * heap_page_is_all_visible() finds the page completely frozen, even
                               1377                 :                :          * though prstate.set_all_frozen is false.
                               1378                 :                :          */
                               1379   [ +  +  -  + ]:          65055 :         Assert(!prstate.set_all_frozen || debug_all_frozen);
                               1380                 :                :     }
                               1381                 :                : #endif
                               1382                 :                : 
                               1383                 :                :     /* Copy information back for caller */
  902 heikki.linnakangas@i     1384                 :         213574 :     presult->ndeleted = prstate.ndeleted;
  900                          1385                 :         213574 :     presult->nnewlpdead = prstate.ndead;
                               1386                 :         213574 :     presult->nfrozen = prstate.nfrozen;
                               1387                 :         213574 :     presult->live_tuples = prstate.live_tuples;
                               1388                 :         213574 :     presult->recently_dead_tuples = prstate.recently_dead_tuples;
                               1389                 :         213574 :     presult->hastup = prstate.hastup;
                               1390                 :                : 
                               1391                 :         213574 :     presult->lpdead_items = prstate.lpdead_items;
                               1392                 :                :     /* the presult->deadoffsets array was already filled in */
                               1393                 :                : 
  180 melanieplageman@gmai     1394                 :         213574 :     presult->newly_all_visible = false;
                               1395                 :         213574 :     presult->newly_all_frozen = false;
                               1396                 :         213574 :     presult->newly_all_visible_frozen = false;
                               1397         [ +  + ]:         213574 :     if (do_set_vm)
                               1398                 :                :     {
                               1399         [ +  + ]:          62364 :         if ((prstate.old_vmbits & VISIBILITYMAP_ALL_VISIBLE) == 0)
                               1400                 :                :         {
                               1401                 :          58742 :             presult->newly_all_visible = true;
                               1402         [ +  + ]:          58742 :             if (prstate.set_all_frozen)
                               1403                 :          30063 :                 presult->newly_all_visible_frozen = true;
                               1404                 :                :         }
                               1405         [ +  - ]:           3622 :         else if ((prstate.old_vmbits & VISIBILITYMAP_ALL_FROZEN) == 0 &&
                               1406         [ +  - ]:           3622 :                  prstate.set_all_frozen)
                               1407                 :           3622 :             presult->newly_all_frozen = true;
                               1408                 :                :     }
                               1409                 :                : 
  341                          1410         [ +  + ]:         213574 :     if (prstate.attempt_freeze)
                               1411                 :                :     {
  900 heikki.linnakangas@i     1412         [ +  + ]:          82276 :         if (presult->nfrozen > 0)
                               1413                 :                :         {
                               1414                 :          25149 :             *new_relfrozen_xid = prstate.pagefrz.FreezePageRelfrozenXid;
                               1415                 :          25149 :             *new_relmin_mxid = prstate.pagefrz.FreezePageRelminMxid;
                               1416                 :                :         }
                               1417                 :                :         else
                               1418                 :                :         {
                               1419                 :          57127 :             *new_relfrozen_xid = prstate.pagefrz.NoFreezePageRelfrozenXid;
                               1420                 :          57127 :             *new_relmin_mxid = prstate.pagefrz.NoFreezePageRelminMxid;
                               1421                 :                :         }
                               1422                 :                :     }
                               1423                 :                : }
                               1424                 :                : 
                               1425                 :                : 
                               1426                 :                : /*
                               1427                 :                :  * Perform visibility checks for heap pruning.
                               1428                 :                :  */
                               1429                 :                : static HTSV_Result
  199 melanieplageman@gmai     1430                 :       19285986 : heap_prune_satisfies_vacuum(PruneState *prstate, HeapTuple tup)
                               1431                 :                : {
                               1432                 :                :     HTSV_Result res;
                               1433                 :                :     TransactionId dead_after;
                               1434                 :                : 
                               1435                 :       19285986 :     res = HeapTupleSatisfiesVacuumHorizon(tup, prstate->buffer, &dead_after);
                               1436                 :                : 
 2230 andres@anarazel.de       1437         [ +  + ]:       19285986 :     if (res != HEAPTUPLE_RECENTLY_DEAD)
                               1438                 :       15048436 :         return res;
                               1439                 :                : 
                               1440                 :                :     /*
                               1441                 :                :      * For VACUUM, we must be sure to prune tuples with xmax older than
                               1442                 :                :      * OldestXmin -- a visibility cutoff determined at the beginning of
                               1443                 :                :      * vacuuming the relation. OldestXmin is used for freezing determination
                               1444                 :                :      * and we cannot freeze dead tuples' xmaxes.
                               1445                 :                :      */
  793 melanieplageman@gmai     1446         [ +  + ]:        4237550 :     if (prstate->cutoffs &&
                               1447         [ +  - ]:        1350093 :         TransactionIdIsValid(prstate->cutoffs->OldestXmin) &&
                               1448   [ +  -  -  +  :        1350093 :         NormalTransactionIdPrecedes(dead_after, prstate->cutoffs->OldestXmin))
                                              +  + ]
                               1449                 :         982261 :         return HEAPTUPLE_DEAD;
                               1450                 :                : 
                               1451                 :                :     /*
                               1452                 :                :      * Determine whether or not the tuple is considered dead when compared
                               1453                 :                :      * with the provided GlobalVisState. On-access pruning does not provide
                               1454                 :                :      * VacuumCutoffs. And for vacuum, even if the tuple's xmax is not older
                               1455                 :                :      * than OldestXmin, GlobalVisTestIsRemovableXid() could find the row dead
                               1456                 :                :      * if the GlobalVisState has been updated since the beginning of vacuuming
                               1457                 :                :      * the relation.
                               1458                 :                :      */
  180                          1459         [ +  + ]:        3255289 :     if (GlobalVisTestIsRemovableXid(prstate->vistest, dead_after, true))
  793                          1460                 :        2833915 :         return HEAPTUPLE_DEAD;
                               1461                 :                : 
 2230 andres@anarazel.de       1462                 :         421374 :     return res;
                               1463                 :                : }
                               1464                 :                : 
                               1465                 :                : 
                               1466                 :                : /*
                               1467                 :                :  * Pruning calculates tuple visibility once and saves the results in an array
                               1468                 :                :  * of int8.  See PruneState.htsv for details.  This helper function is meant
                               1469                 :                :  * to guard against examining visibility status array members which have not
                               1470                 :                :  * yet been computed.
                               1471                 :                :  */
                               1472                 :                : static inline HTSV_Result
  900 heikki.linnakangas@i     1473                 :       19270235 : htsv_get_valid_status(int status)
                               1474                 :                : {
                               1475   [ +  -  -  + ]:       19270235 :     Assert(status >= HEAPTUPLE_DEAD &&
                               1476                 :                :            status <= HEAPTUPLE_DELETE_IN_PROGRESS);
                               1477                 :       19270235 :     return (HTSV_Result) status;
                               1478                 :                : }
                               1479                 :                : 
                               1480                 :                : /*
                               1481                 :                :  * Prune specified line pointer or a HOT chain originating at line pointer.
                               1482                 :                :  *
                               1483                 :                :  * Tuple visibility information is provided in prstate->htsv.
                               1484                 :                :  *
                               1485                 :                :  * If the item is an index-referenced tuple (i.e. not a heap-only tuple),
                               1486                 :                :  * the HOT chain is pruned by removing all DEAD tuples at the start of the HOT
                               1487                 :                :  * chain.  We also prune any RECENTLY_DEAD tuples preceding a DEAD tuple.
                               1488                 :                :  * This is OK because a RECENTLY_DEAD tuple preceding a DEAD tuple is really
                               1489                 :                :  * DEAD, our visibility test is just too coarse to detect it.
                               1490                 :                :  *
                               1491                 :                :  * Pruning must never leave behind a DEAD tuple that still has tuple storage.
                               1492                 :                :  * VACUUM isn't prepared to deal with that case.
                               1493                 :                :  *
                               1494                 :                :  * The root line pointer is redirected to the tuple immediately after the
                               1495                 :                :  * latest DEAD tuple.  If all tuples in the chain are DEAD, the root line
                               1496                 :                :  * pointer is marked LP_DEAD.  (This includes the case of a DEAD simple
                               1497                 :                :  * tuple, which we treat as a chain of length 1.)
                               1498                 :                :  *
                               1499                 :                :  * We don't actually change the page here. We just add entries to the arrays in
                               1500                 :                :  * prstate showing the changes to be made.  Items to be redirected are added
                               1501                 :                :  * to the redirected[] array (two entries per redirection); items to be set to
                               1502                 :                :  * LP_DEAD state are added to nowdead[]; and items to be set to LP_UNUSED
                               1503                 :                :  * state are added to nowunused[].  We perform bookkeeping of live tuples,
                               1504                 :                :  * visibility etc. based on what the page will look like after the changes
                               1505                 :                :  * applied.  All that bookkeeping is performed in the heap_prune_record_*()
                               1506                 :                :  * subroutines.  The division of labor is that heap_prune_chain() decides the
                               1507                 :                :  * fate of each tuple, ie. whether it's going to be removed, redirected or
                               1508                 :                :  * left unchanged, and the heap_prune_record_*() subroutines update PruneState
                               1509                 :                :  * based on that outcome.
                               1510                 :                :  */
                               1511                 :                : static void
  199 melanieplageman@gmai     1512                 :       19141209 : heap_prune_chain(OffsetNumber maxoff, OffsetNumber rootoffnum,
                               1513                 :                :                  PruneState *prstate)
                               1514                 :                : {
 6884 bruce@momjian.us         1515                 :       19141209 :     TransactionId priorXmax = InvalidTransactionId;
                               1516                 :                :     ItemId      rootlp;
                               1517                 :                :     OffsetNumber offnum;
                               1518                 :                :     OffsetNumber chainitems[MaxHeapTuplesPerPage];
  199 melanieplageman@gmai     1519                 :       19141209 :     Page        page = prstate->page;
                               1520                 :                : 
                               1521                 :                :     /*
                               1522                 :                :      * After traversing the HOT chain, ndeadchain is the index in chainitems
                               1523                 :                :      * of the first live successor after the last dead item.
                               1524                 :                :      */
  902 heikki.linnakangas@i     1525                 :       19141209 :     int         ndeadchain = 0,
                               1526                 :       19141209 :                 nchain = 0;
                               1527                 :                : 
                               1528                 :       19141209 :     rootlp = PageGetItemId(page, rootoffnum);
                               1529                 :                : 
                               1530                 :                :     /* Start from the root tuple */
 6940 tgl@sss.pgh.pa.us        1531                 :       19141209 :     offnum = rootoffnum;
                               1532                 :                : 
                               1533                 :                :     /* while not end of the chain */
                               1534                 :                :     for (;;)
                               1535                 :         342622 :     {
                               1536                 :                :         HeapTupleHeader htup;
                               1537                 :                :         ItemId      lp;
                               1538                 :                : 
                               1539                 :                :         /* Sanity check (pure paranoia) */
 1824 pg@bowt.ie               1540         [ -  + ]:       19483831 :         if (offnum < FirstOffsetNumber)
 1824 pg@bowt.ie               1541                 :UBC           0 :             break;
                               1542                 :                : 
                               1543                 :                :         /*
                               1544                 :                :          * An offset past the end of page's line pointer array is possible
                               1545                 :                :          * when the array was truncated (original item must have been unused)
                               1546                 :                :          */
 1824 pg@bowt.ie               1547         [ -  + ]:CBC    19483831 :         if (offnum > maxoff)
 6940 tgl@sss.pgh.pa.us        1548                 :UBC           0 :             break;
                               1549                 :                : 
                               1550                 :                :         /* If item is already processed, stop --- it must not be same chain */
  902 heikki.linnakangas@i     1551         [ -  + ]:CBC    19483831 :         if (prstate->processed[offnum])
 6770 tgl@sss.pgh.pa.us        1552                 :UBC           0 :             break;
                               1553                 :                : 
  902 heikki.linnakangas@i     1554                 :CBC    19483831 :         lp = PageGetItemId(page, offnum);
                               1555                 :                : 
                               1556                 :                :         /*
                               1557                 :                :          * Unused item obviously isn't part of the chain. Likewise, a dead
                               1558                 :                :          * line pointer can't be part of the chain.  Both of those cases were
                               1559                 :                :          * already marked as processed.
                               1560                 :                :          */
                               1561         [ -  + ]:       19483831 :         Assert(ItemIdIsUsed(lp));
                               1562         [ -  + ]:       19483831 :         Assert(!ItemIdIsDead(lp));
                               1563                 :                : 
                               1564                 :                :         /*
                               1565                 :                :          * If we are looking at the redirected root line pointer, jump to the
                               1566                 :                :          * first normal tuple in the chain.  If we find a redirect somewhere
                               1567                 :                :          * else, stop --- it must not be same chain.
                               1568                 :                :          */
 6940 tgl@sss.pgh.pa.us        1569         [ +  + ]:       19483831 :         if (ItemIdIsRedirected(lp))
                               1570                 :                :         {
                               1571         [ -  + ]:         213596 :             if (nchain > 0)
 6940 tgl@sss.pgh.pa.us        1572                 :UBC           0 :                 break;          /* not at start of chain */
 6940 tgl@sss.pgh.pa.us        1573                 :CBC      213596 :             chainitems[nchain++] = offnum;
                               1574                 :         213596 :             offnum = ItemIdGetRedirect(rootlp);
                               1575                 :         213596 :             continue;
                               1576                 :                :         }
                               1577                 :                : 
                               1578         [ -  + ]:       19270235 :         Assert(ItemIdIsNormal(lp));
                               1579                 :                : 
  902 heikki.linnakangas@i     1580                 :       19270235 :         htup = (HeapTupleHeader) PageGetItem(page, lp);
                               1581                 :                : 
                               1582                 :                :         /*
                               1583                 :                :          * Check the tuple XMIN against prior XMAX, if any
                               1584                 :                :          */
 6940 tgl@sss.pgh.pa.us        1585   [ +  +  -  + ]:       19399261 :         if (TransactionIdIsValid(priorXmax) &&
 3244 alvherre@alvh.no-ip.     1586                 :         129026 :             !TransactionIdEquals(HeapTupleHeaderGetXmin(htup), priorXmax))
 6940 tgl@sss.pgh.pa.us        1587                 :UBC           0 :             break;
                               1588                 :                : 
                               1589                 :                :         /*
                               1590                 :                :          * OK, this tuple is indeed a member of the chain.
                               1591                 :                :          */
 6940 tgl@sss.pgh.pa.us        1592                 :CBC    19270235 :         chainitems[nchain++] = offnum;
                               1593                 :                : 
  900 heikki.linnakangas@i     1594   [ +  +  +  - ]:       19270235 :         switch (htsv_get_valid_status(prstate->htsv[offnum]))
                               1595                 :                :         {
 6940 tgl@sss.pgh.pa.us        1596                 :        3888313 :             case HEAPTUPLE_DEAD:
                               1597                 :                : 
                               1598                 :                :                 /* Remember the last DEAD tuple seen */
  902 heikki.linnakangas@i     1599                 :        3888313 :                 ndeadchain = nchain;
                               1600                 :        3888313 :                 HeapTupleHeaderAdvanceConflictHorizon(htup,
                               1601                 :                :                                                       &prstate->latest_xid_removed);
                               1602                 :                :                 /* Advance to next chain member */
 6940 tgl@sss.pgh.pa.us        1603                 :        3888313 :                 break;
                               1604                 :                : 
                               1605                 :         421374 :             case HEAPTUPLE_RECENTLY_DEAD:
                               1606                 :                : 
                               1607                 :                :                 /*
                               1608                 :                :                  * We don't need to advance the conflict horizon for
                               1609                 :                :                  * RECENTLY_DEAD tuples, even if we are removing them.  This
                               1610                 :                :                  * is because we only remove RECENTLY_DEAD tuples if they
                               1611                 :                :                  * precede a DEAD tuple, and the DEAD tuple must have been
                               1612                 :                :                  * inserted by a newer transaction than the RECENTLY_DEAD
                               1613                 :                :                  * tuple by virtue of being later in the chain.  We will have
                               1614                 :                :                  * advanced the conflict horizon for the DEAD tuple.
                               1615                 :                :                  */
                               1616                 :                : 
                               1617                 :                :                 /*
                               1618                 :                :                  * Advance past RECENTLY_DEAD tuples just in case there's a
                               1619                 :                :                  * DEAD one after them.  We have to make sure that we don't
                               1620                 :                :                  * miss any DEAD tuples, since DEAD tuples that still have
                               1621                 :                :                  * tuple storage after pruning will confuse VACUUM.
                               1622                 :                :                  */
                               1623                 :         421374 :                 break;
                               1624                 :                : 
                               1625                 :       14960548 :             case HEAPTUPLE_DELETE_IN_PROGRESS:
                               1626                 :                :             case HEAPTUPLE_LIVE:
                               1627                 :                :             case HEAPTUPLE_INSERT_IN_PROGRESS:
  902 heikki.linnakangas@i     1628                 :       14960548 :                 goto process_chain;
                               1629                 :                : 
 6940 tgl@sss.pgh.pa.us        1630                 :UBC           0 :             default:
                               1631         [ #  # ]:              0 :                 elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
                               1632                 :                :                 goto process_chain;
                               1633                 :                :         }
                               1634                 :                : 
                               1635                 :                :         /*
                               1636                 :                :          * If the tuple is not HOT-updated, then we are at the end of this
                               1637                 :                :          * HOT-update chain.
                               1638                 :                :          */
 6940 tgl@sss.pgh.pa.us        1639         [ +  + ]:CBC     4309687 :         if (!HeapTupleHeaderIsHotUpdated(htup))
  902 heikki.linnakangas@i     1640                 :        4180661 :             goto process_chain;
                               1641                 :                : 
                               1642                 :                :         /* HOT implies it can't have moved to different partition */
 3088 andres@anarazel.de       1643         [ -  + ]:         129026 :         Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
                               1644                 :                : 
                               1645                 :                :         /*
                               1646                 :                :          * Advance to next chain member.
                               1647                 :                :          */
  199 melanieplageman@gmai     1648         [ -  + ]:         129026 :         Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == prstate->block);
 6940 tgl@sss.pgh.pa.us        1649                 :         129026 :         offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 4988 alvherre@alvh.no-ip.     1650                 :         129026 :         priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               1651                 :                :     }
                               1652                 :                : 
  902 heikki.linnakangas@i     1653   [ #  #  #  # ]:UBC           0 :     if (ItemIdIsRedirected(rootlp) && nchain < 2)
                               1654                 :                :     {
                               1655                 :                :         /*
                               1656                 :                :          * We found a redirect item that doesn't point to a valid follow-on
                               1657                 :                :          * item.  This can happen if the loop in heap_page_prune_and_freeze()
                               1658                 :                :          * caused us to visit the dead successor of a redirect item before
                               1659                 :                :          * visiting the redirect item.  We can clean up by setting the
                               1660                 :                :          * redirect item to LP_DEAD state or LP_UNUSED if the caller
                               1661                 :                :          * indicated.
                               1662                 :                :          */
                               1663                 :              0 :         heap_prune_record_dead_or_unused(prstate, rootoffnum, false);
                               1664                 :              0 :         return;
                               1665                 :                :     }
                               1666                 :                : 
                               1667                 :              0 : process_chain:
                               1668                 :                : 
  902 heikki.linnakangas@i     1669         [ +  + ]:CBC    19141209 :     if (ndeadchain == 0)
                               1670                 :                :     {
                               1671                 :                :         /*
                               1672                 :                :          * No DEAD tuple was found, so the chain is entirely composed of
                               1673                 :                :          * normal, unchanged tuples.  Leave it alone.
                               1674                 :                :          */
  900                          1675                 :       15298256 :         int         i = 0;
                               1676                 :                : 
                               1677         [ +  + ]:       15298256 :         if (ItemIdIsRedirected(rootlp))
                               1678                 :                :         {
                               1679                 :         190839 :             heap_prune_record_unchanged_lp_redirect(prstate, rootoffnum);
                               1680                 :         190839 :             i++;
                               1681                 :                :         }
                               1682         [ +  + ]:       30604125 :         for (; i < nchain; i++)
  199 melanieplageman@gmai     1683                 :       15305869 :             heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
                               1684                 :                :     }
  902 heikki.linnakangas@i     1685         [ +  + ]:        3842953 :     else if (ndeadchain == nchain)
                               1686                 :                :     {
                               1687                 :                :         /*
                               1688                 :                :          * The entire chain is dead.  Mark the root line pointer LP_DEAD, and
                               1689                 :                :          * fully remove the other tuples in the chain.
                               1690                 :                :          */
                               1691                 :        3769743 :         heap_prune_record_dead_or_unused(prstate, rootoffnum, ItemIdIsNormal(rootlp));
                               1692         [ +  + ]:        3814707 :         for (int i = 1; i < nchain; i++)
                               1693                 :          44964 :             heap_prune_record_unused(prstate, chainitems[i], true);
                               1694                 :                :     }
                               1695                 :                :     else
                               1696                 :                :     {
                               1697                 :                :         /*
                               1698                 :                :          * We found a DEAD tuple in the chain.  Redirect the root line pointer
                               1699                 :                :          * to the first non-DEAD tuple, and mark as unused each intermediate
                               1700                 :                :          * item that we are able to remove from the chain.
                               1701                 :                :          */
                               1702                 :          73210 :         heap_prune_record_redirect(prstate, rootoffnum, chainitems[ndeadchain],
                               1703                 :          73210 :                                    ItemIdIsNormal(rootlp));
                               1704         [ +  + ]:          96363 :         for (int i = 1; i < ndeadchain; i++)
                               1705                 :          23153 :             heap_prune_record_unused(prstate, chainitems[i], true);
                               1706                 :                : 
                               1707                 :                :         /* the rest of tuples in the chain are normal, unchanged tuples */
                               1708         [ +  + ]:         149263 :         for (int i = ndeadchain; i < nchain; i++)
  199 melanieplageman@gmai     1709                 :          76053 :             heap_prune_record_unchanged_lp_normal(prstate, chainitems[i]);
                               1710                 :                :     }
                               1711                 :                : }
                               1712                 :                : 
                               1713                 :                : /* Record lowest soon-prunable XID */
                               1714                 :                : static void
  182                          1715                 :         597999 : heap_prune_record_prunable(PruneState *prstate, TransactionId xid,
                               1716                 :                :                            OffsetNumber offnum)
                               1717                 :                : {
                               1718                 :                :     /*
                               1719                 :                :      * This should exactly match the PageSetPrunable macro.  We can't store
                               1720                 :                :      * directly into the page header yet, so we update working state.
                               1721                 :                :      */
 6770 tgl@sss.pgh.pa.us        1722         [ -  + ]:         597999 :     Assert(TransactionIdIsNormal(xid));
                               1723   [ +  +  +  + ]:        1162562 :     if (!TransactionIdIsValid(prstate->new_prune_xid) ||
                               1724                 :         564563 :         TransactionIdPrecedes(xid, prstate->new_prune_xid))
                               1725                 :          35195 :         prstate->new_prune_xid = xid;
                               1726                 :                : 
                               1727                 :                :     /*
                               1728                 :                :      * It's incorrect for a page to be marked all-visible if it contains
                               1729                 :                :      * prunable items.
                               1730                 :                :      */
  182 melanieplageman@gmai     1731         [ -  + ]:         597999 :     if (PageIsAllVisible(prstate->page))
  182 melanieplageman@gmai     1732                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum,
                               1733                 :                :                                     VM_CORRUPT_TUPLE_VISIBILITY);
 6770 tgl@sss.pgh.pa.us        1734                 :CBC      597999 : }
                               1735                 :                : 
                               1736                 :                : /* Record line pointer to be redirected */
                               1737                 :                : static void
                               1738                 :          73210 : heap_prune_record_redirect(PruneState *prstate,
                               1739                 :                :                            OffsetNumber offnum, OffsetNumber rdoffnum,
                               1740                 :                :                            bool was_normal)
                               1741                 :                : {
  902 heikki.linnakangas@i     1742         [ -  + ]:          73210 :     Assert(!prstate->processed[offnum]);
                               1743                 :          73210 :     prstate->processed[offnum] = true;
                               1744                 :                : 
                               1745                 :                :     /*
                               1746                 :                :      * Do not mark the redirect target here.  It needs to be counted
                               1747                 :                :      * separately as an unchanged tuple.
                               1748                 :                :      */
                               1749                 :                : 
 6770 tgl@sss.pgh.pa.us        1750         [ -  + ]:          73210 :     Assert(prstate->nredirected < MaxHeapTuplesPerPage);
                               1751                 :          73210 :     prstate->redirected[prstate->nredirected * 2] = offnum;
                               1752                 :          73210 :     prstate->redirected[prstate->nredirected * 2 + 1] = rdoffnum;
                               1753                 :                : 
                               1754                 :          73210 :     prstate->nredirected++;
                               1755                 :                : 
                               1756                 :                :     /*
                               1757                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1758                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1759                 :                :      * count.
                               1760                 :                :      */
  902 heikki.linnakangas@i     1761         [ +  + ]:          73210 :     if (was_normal)
                               1762                 :          63616 :         prstate->ndeleted++;
                               1763                 :                : 
  900                          1764                 :          73210 :     prstate->hastup = true;
 6940 tgl@sss.pgh.pa.us        1765                 :          73210 : }
                               1766                 :                : 
                               1767                 :                : /* Record line pointer to be marked dead */
                               1768                 :                : static void
  902 heikki.linnakangas@i     1769                 :        3734992 : heap_prune_record_dead(PruneState *prstate, OffsetNumber offnum,
                               1770                 :                :                        bool was_normal)
                               1771                 :                : {
                               1772         [ -  + ]:        3734992 :     Assert(!prstate->processed[offnum]);
                               1773                 :        3734992 :     prstate->processed[offnum] = true;
                               1774                 :                : 
 6770 tgl@sss.pgh.pa.us        1775         [ -  + ]:        3734992 :     Assert(prstate->ndead < MaxHeapTuplesPerPage);
                               1776                 :        3734992 :     prstate->nowdead[prstate->ndead] = offnum;
                               1777                 :        3734992 :     prstate->ndead++;
                               1778                 :                : 
                               1779                 :                :     /*
                               1780                 :                :      * Deliberately delay unsetting set_all_visible and set_all_frozen until
                               1781                 :                :      * later during pruning. Removable dead tuples shouldn't preclude freezing
                               1782                 :                :      * the page.
                               1783                 :                :      */
                               1784                 :                : 
                               1785                 :                :     /* Record the dead offset for vacuum */
  900 heikki.linnakangas@i     1786                 :        3734992 :     prstate->deadoffsets[prstate->lpdead_items++] = offnum;
                               1787                 :                : 
                               1788                 :                :     /*
                               1789                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1790                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1791                 :                :      * count.
                               1792                 :                :      */
  902                          1793         [ +  + ]:        3734992 :     if (was_normal)
                               1794                 :        3721829 :         prstate->ndeleted++;
 6940 tgl@sss.pgh.pa.us        1795                 :        3734992 : }
                               1796                 :                : 
                               1797                 :                : /*
                               1798                 :                :  * Depending on whether or not the caller set mark_unused_now to true, record that a
                               1799                 :                :  * line pointer should be marked LP_DEAD or LP_UNUSED. There are other cases in
                               1800                 :                :  * which we will mark line pointers LP_UNUSED, but we will not mark line
                               1801                 :                :  * pointers LP_DEAD if mark_unused_now is true.
                               1802                 :                :  */
                               1803                 :                : static void
  902 heikki.linnakangas@i     1804                 :        3769743 : heap_prune_record_dead_or_unused(PruneState *prstate, OffsetNumber offnum,
                               1805                 :                :                                  bool was_normal)
                               1806                 :                : {
                               1807                 :                :     /*
                               1808                 :                :      * If the caller set mark_unused_now to true, we can remove dead tuples
                               1809                 :                :      * during pruning instead of marking their line pointers dead. Set this
                               1810                 :                :      * tuple's line pointer LP_UNUSED. We hint that this option is less
                               1811                 :                :      * likely.
                               1812                 :                :      */
  976 rhaas@postgresql.org     1813         [ +  + ]:        3769743 :     if (unlikely(prstate->mark_unused_now))
  902 heikki.linnakangas@i     1814                 :          34751 :         heap_prune_record_unused(prstate, offnum, was_normal);
                               1815                 :                :     else
                               1816                 :        3734992 :         heap_prune_record_dead(prstate, offnum, was_normal);
                               1817                 :                : 
                               1818                 :                :     /*
                               1819                 :                :      * It's incorrect for the page to be set all-visible if it contains dead
                               1820                 :                :      * items. Fix that on the heap page and check the VM for corruption as
                               1821                 :                :      * well. Do that here rather than in heap_prune_record_dead() so we also
                               1822                 :                :      * cover tuples that are directly marked LP_UNUSED via mark_unused_now.
                               1823                 :                :      */
  182 melanieplageman@gmai     1824         [ -  + ]:        3769743 :     if (PageIsAllVisible(prstate->page))
  182 melanieplageman@gmai     1825                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum, VM_CORRUPT_LPDEAD);
  976 rhaas@postgresql.org     1826                 :CBC     3769743 : }
                               1827                 :                : 
                               1828                 :                : /* Record line pointer to be marked unused */
                               1829                 :                : static void
  902 heikki.linnakangas@i     1830                 :         108299 : heap_prune_record_unused(PruneState *prstate, OffsetNumber offnum, bool was_normal)
                               1831                 :                : {
                               1832         [ -  + ]:         108299 :     Assert(!prstate->processed[offnum]);
                               1833                 :         108299 :     prstate->processed[offnum] = true;
                               1834                 :                : 
 6770 tgl@sss.pgh.pa.us        1835         [ -  + ]:         108299 :     Assert(prstate->nunused < MaxHeapTuplesPerPage);
                               1836                 :         108299 :     prstate->nowunused[prstate->nunused] = offnum;
                               1837                 :         108299 :     prstate->nunused++;
                               1838                 :                : 
                               1839                 :                :     /*
                               1840                 :                :      * If the root entry had been a normal tuple, we are deleting it, so count
                               1841                 :                :      * it in the result.  But changing a redirect (even to DEAD state) doesn't
                               1842                 :                :      * count.
                               1843                 :                :      */
  902 heikki.linnakangas@i     1844         [ +  + ]:         108299 :     if (was_normal)
                               1845                 :         106159 :         prstate->ndeleted++;
 6770 tgl@sss.pgh.pa.us        1846                 :         108299 : }
                               1847                 :                : 
                               1848                 :                : /*
                               1849                 :                :  * Record an unused line pointer that is left unchanged.
                               1850                 :                :  */
                               1851                 :                : static void
  199 melanieplageman@gmai     1852                 :         382118 : heap_prune_record_unchanged_lp_unused(PruneState *prstate, OffsetNumber offnum)
                               1853                 :                : {
  900 heikki.linnakangas@i     1854         [ -  + ]:         382118 :     Assert(!prstate->processed[offnum]);
                               1855                 :         382118 :     prstate->processed[offnum] = true;
                               1856                 :         382118 : }
                               1857                 :                : 
                               1858                 :                : /*
                               1859                 :                :  * Record line pointer that is left unchanged.  We consider freezing it, and
                               1860                 :                :  * update bookkeeping of tuple counts and page visibility.
                               1861                 :                :  */
                               1862                 :                : static void
  199 melanieplageman@gmai     1863                 :       15394382 : heap_prune_record_unchanged_lp_normal(PruneState *prstate, OffsetNumber offnum)
                               1864                 :                : {
                               1865                 :                :     HeapTupleHeader htup;
                               1866                 :                :     TransactionId xmin;
                               1867                 :       15394382 :     Page        page = prstate->page;
                               1868                 :                : 
  900 heikki.linnakangas@i     1869         [ -  + ]:       15394382 :     Assert(!prstate->processed[offnum]);
                               1870                 :       15394382 :     prstate->processed[offnum] = true;
                               1871                 :                : 
                               1872                 :       15394382 :     prstate->hastup = true;      /* the page is not empty */
                               1873                 :                : 
                               1874                 :                :     /*
                               1875                 :                :      * The criteria for counting a tuple as live in this block need to match
                               1876                 :                :      * what analyze.c's acquire_sample_rows() does, otherwise VACUUM and
                               1877                 :                :      * ANALYZE may produce wildly different reltuples values, e.g. when there
                               1878                 :                :      * are many recently-dead tuples.
                               1879                 :                :      *
                               1880                 :                :      * The logic here is a bit simpler than acquire_sample_rows(), as VACUUM
                               1881                 :                :      * can't run inside a transaction block, which makes some cases impossible
                               1882                 :                :      * (e.g. in-progress insert from the same transaction).
                               1883                 :                :      *
                               1884                 :                :      * HEAPTUPLE_DEAD are handled by the other heap_prune_record_*()
                               1885                 :                :      * subroutines.  They don't count dead items like acquire_sample_rows()
                               1886                 :                :      * does, because we assume that all dead items will become LP_UNUSED
                               1887                 :                :      * before VACUUM finishes.  This difference is only superficial.  VACUUM
                               1888                 :                :      * effectively agrees with ANALYZE about DEAD items, in the end.  VACUUM
                               1889                 :                :      * won't remember LP_DEAD items, but only because they're not supposed to
                               1890                 :                :      * be left behind when it is done. (Cases where we bypass index vacuuming
                               1891                 :                :      * will violate this optimistic assumption, but the overall impact of that
                               1892                 :                :      * should be negligible.)
                               1893                 :                :      */
                               1894                 :       15394382 :     htup = (HeapTupleHeader) PageGetItem(page, PageGetItemId(page, offnum));
                               1895                 :                : 
                               1896   [ +  +  +  +  :       15394382 :     switch (prstate->htsv[offnum])
                                                 - ]
                               1897                 :                :     {
                               1898                 :       14796383 :         case HEAPTUPLE_LIVE:
                               1899                 :                : 
                               1900                 :                :             /*
                               1901                 :                :              * Count it as live.  Not only is this natural, but it's also what
                               1902                 :                :              * acquire_sample_rows() does.
                               1903                 :                :              */
                               1904                 :       14796383 :             prstate->live_tuples++;
                               1905                 :                : 
                               1906                 :                :             /*
                               1907                 :                :              * Is the tuple definitely visible to all transactions?
                               1908                 :                :              *
                               1909                 :                :              * NB: Like with per-tuple hint bits, we can't set the
                               1910                 :                :              * PD_ALL_VISIBLE flag if the inserter committed asynchronously.
                               1911                 :                :              * See SetHintBits for more info.  Check that the tuple is hinted
                               1912                 :                :              * xmin-committed because of that.
                               1913                 :                :              */
  180 melanieplageman@gmai     1914         [ +  + ]:       14796383 :             if (!HeapTupleHeaderXminCommitted(htup))
                               1915                 :                :             {
                               1916                 :          44367 :                 prstate->set_all_visible = false;
                               1917                 :          44367 :                 prstate->set_all_frozen = false;
                               1918                 :          44367 :                 break;
                               1919                 :                :             }
                               1920                 :                : 
                               1921                 :                :             /*
                               1922                 :                :              * The inserter definitely committed. But we don't know if it is
                               1923                 :                :              * old enough that everyone sees it as committed. Later, after
                               1924                 :                :              * processing all the tuples on the page, we'll check if there is
                               1925                 :                :              * any snapshot that still considers the newest xid on the page to
                               1926                 :                :              * be running. If so, we don't consider the page all-visible.
                               1927                 :                :              */
                               1928                 :       14752016 :             xmin = HeapTupleHeaderGetXmin(htup);
                               1929                 :                : 
                               1930                 :                :             /* Track newest xmin on page. */
                               1931   [ +  +  +  + ]:       14752016 :             if (TransactionIdFollows(xmin, prstate->newest_live_xid) &&
                               1932                 :                :                 TransactionIdIsNormal(xmin))
                               1933                 :         634264 :                 prstate->newest_live_xid = xmin;
                               1934                 :                : 
  900 heikki.linnakangas@i     1935                 :       14752016 :             break;
                               1936                 :                : 
                               1937                 :         421374 :         case HEAPTUPLE_RECENTLY_DEAD:
                               1938                 :         421374 :             prstate->recently_dead_tuples++;
  199 melanieplageman@gmai     1939                 :         421374 :             prstate->set_all_visible = false;
                               1940                 :         421374 :             prstate->set_all_frozen = false;
                               1941                 :                : 
                               1942                 :                :             /*
                               1943                 :                :              * This tuple will soon become DEAD.  Update the hint field so
                               1944                 :                :              * that the page is reconsidered for pruning in future.
                               1945                 :                :              */
  900 heikki.linnakangas@i     1946                 :         421374 :             heap_prune_record_prunable(prstate,
                               1947                 :                :                                        HeapTupleHeaderGetUpdateXid(htup),
                               1948                 :                :                                        offnum);
                               1949                 :         421374 :             break;
                               1950                 :                : 
                               1951                 :         158815 :         case HEAPTUPLE_INSERT_IN_PROGRESS:
                               1952                 :                : 
                               1953                 :                :             /*
                               1954                 :                :              * We do not count these rows as live, because we expect the
                               1955                 :                :              * inserting transaction to update the counters at commit, and we
                               1956                 :                :              * assume that will happen only after we report our results.  This
                               1957                 :                :              * assumption is a bit shaky, but it is what acquire_sample_rows()
                               1958                 :                :              * does, so be consistent.
                               1959                 :                :              */
  199 melanieplageman@gmai     1960                 :         158815 :             prstate->set_all_visible = false;
                               1961                 :         158815 :             prstate->set_all_frozen = false;
                               1962                 :                : 
                               1963                 :                :             /*
                               1964                 :                :              * Though there is nothing "prunable" on the page, we maintain
                               1965                 :                :              * pd_prune_xid for inserts so that we have the opportunity to
                               1966                 :                :              * mark them all-visible during the next round of pruning.
                               1967                 :                :              */
  174                          1968                 :         158815 :             heap_prune_record_prunable(prstate,
                               1969                 :                :                                        HeapTupleHeaderGetXmin(htup),
                               1970                 :                :                                        offnum);
  900 heikki.linnakangas@i     1971                 :         158815 :             break;
                               1972                 :                : 
                               1973                 :          17810 :         case HEAPTUPLE_DELETE_IN_PROGRESS:
                               1974                 :                : 
                               1975                 :                :             /*
                               1976                 :                :              * This an expected case during concurrent vacuum.  Count such
                               1977                 :                :              * rows as live.  As above, we assume the deleting transaction
                               1978                 :                :              * will commit and update the counters after we report.
                               1979                 :                :              */
                               1980                 :          17810 :             prstate->live_tuples++;
  199 melanieplageman@gmai     1981                 :          17810 :             prstate->set_all_visible = false;
                               1982                 :          17810 :             prstate->set_all_frozen = false;
                               1983                 :                : 
                               1984                 :                :             /*
                               1985                 :                :              * This tuple may soon become DEAD.  Update the hint field so that
                               1986                 :                :              * the page is reconsidered for pruning in future.
                               1987                 :                :              */
  900 heikki.linnakangas@i     1988                 :          17810 :             heap_prune_record_prunable(prstate,
                               1989                 :                :                                        HeapTupleHeaderGetUpdateXid(htup),
                               1990                 :                :                                        offnum);
                               1991                 :          17810 :             break;
                               1992                 :                : 
  900 heikki.linnakangas@i     1993                 :UBC           0 :         default:
                               1994                 :                : 
                               1995                 :                :             /*
                               1996                 :                :              * DEAD tuples should've been passed to heap_prune_record_dead()
                               1997                 :                :              * or heap_prune_record_unused() instead.
                               1998                 :                :              */
                               1999         [ #  # ]:              0 :             elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result %d",
                               2000                 :                :                  prstate->htsv[offnum]);
                               2001                 :                :             break;
                               2002                 :                :     }
                               2003                 :                : 
                               2004                 :                :     /* Consider freezing any normal tuples which will not be removed */
  341 melanieplageman@gmai     2005         [ +  + ]:CBC    15394382 :     if (prstate->attempt_freeze)
                               2006                 :                :     {
                               2007                 :                :         bool        totally_frozen;
                               2008                 :                : 
  900 heikki.linnakangas@i     2009         [ +  + ]:        5224462 :         if ((heap_prepare_freeze_tuple(htup,
                               2010                 :        5224462 :                                        prstate->cutoffs,
                               2011                 :                :                                        &prstate->pagefrz,
                               2012                 :        5224462 :                                        &prstate->frozen[prstate->nfrozen],
                               2013                 :                :                                        &totally_frozen)))
                               2014                 :                :         {
                               2015                 :                :             /* Save prepared freeze plan for later */
                               2016                 :        3664447 :             prstate->frozen[prstate->nfrozen++].offset = offnum;
                               2017                 :                :         }
                               2018                 :                : 
                               2019                 :                :         /*
                               2020                 :                :          * If any tuple isn't either totally frozen already or eligible to
                               2021                 :                :          * become totally frozen (according to its freeze plan), then the page
                               2022                 :                :          * definitely cannot be set all-frozen in the visibility map later on.
                               2023                 :                :          */
                               2024         [ +  + ]:        5224462 :         if (!totally_frozen)
  199 melanieplageman@gmai     2025                 :         865327 :             prstate->set_all_frozen = false;
                               2026                 :                :     }
  900 heikki.linnakangas@i     2027                 :       15394382 : }
                               2028                 :                : 
                               2029                 :                : 
                               2030                 :                : /*
                               2031                 :                :  * Record line pointer that was already LP_DEAD and is left unchanged.
                               2032                 :                :  */
                               2033                 :                : static void
  199 melanieplageman@gmai     2034                 :        1578563 : heap_prune_record_unchanged_lp_dead(PruneState *prstate, OffsetNumber offnum)
                               2035                 :                : {
  900 heikki.linnakangas@i     2036         [ -  + ]:        1578563 :     Assert(!prstate->processed[offnum]);
                               2037                 :        1578563 :     prstate->processed[offnum] = true;
                               2038                 :                : 
                               2039                 :                :     /*
                               2040                 :                :      * Deliberately don't set hastup for LP_DEAD items.  We make the soft
                               2041                 :                :      * assumption that any LP_DEAD items encountered here will become
                               2042                 :                :      * LP_UNUSED later on, before count_nondeletable_pages is reached.  If we
                               2043                 :                :      * don't make this assumption then rel truncation will only happen every
                               2044                 :                :      * other VACUUM, at most.  Besides, VACUUM must treat
                               2045                 :                :      * hastup/nonempty_pages as provisional no matter how LP_DEAD items are
                               2046                 :                :      * handled (handled here, or handled later on).
                               2047                 :                :      *
                               2048                 :                :      * Similarly, don't unset set_all_visible and set_all_frozen until later,
                               2049                 :                :      * at the end of heap_page_prune_and_freeze().  This will allow us to
                               2050                 :                :      * attempt to freeze the page after pruning.  As long as we unset it
                               2051                 :                :      * before updating the visibility map, this will be correct.
                               2052                 :                :      */
                               2053                 :                : 
                               2054                 :                :     /* Record the dead offset for vacuum */
                               2055                 :        1578563 :     prstate->deadoffsets[prstate->lpdead_items++] = offnum;
                               2056                 :                : 
                               2057                 :                :     /*
                               2058                 :                :      * It's incorrect for a page to be marked all-visible if it contains dead
                               2059                 :                :      * items.
                               2060                 :                :      */
  182 melanieplageman@gmai     2061         [ -  + ]:        1578563 :     if (PageIsAllVisible(prstate->page))
  182 melanieplageman@gmai     2062                 :UBC           0 :         heap_page_fix_vm_corruption(prstate, offnum, VM_CORRUPT_LPDEAD);
  900 heikki.linnakangas@i     2063                 :CBC     1578563 : }
                               2064                 :                : 
                               2065                 :                : /*
                               2066                 :                :  * Record LP_REDIRECT that is left unchanged.
                               2067                 :                :  */
                               2068                 :                : static void
                               2069                 :         190839 : heap_prune_record_unchanged_lp_redirect(PruneState *prstate, OffsetNumber offnum)
                               2070                 :                : {
                               2071                 :                :     /*
                               2072                 :                :      * A redirect line pointer doesn't count as a live tuple.
                               2073                 :                :      *
                               2074                 :                :      * If we leave a redirect line pointer in place, there will be another
                               2075                 :                :      * tuple on the page that it points to.  We will do the bookkeeping for
                               2076                 :                :      * that separately.  So we have nothing to do here, except remember that
                               2077                 :                :      * we processed this item.
                               2078                 :                :      */
  902                          2079         [ -  + ]:         190839 :     Assert(!prstate->processed[offnum]);
                               2080                 :         190839 :     prstate->processed[offnum] = true;
                               2081                 :         190839 : }
                               2082                 :                : 
                               2083                 :                : /*
                               2084                 :                :  * Perform the actual page changes needed by heap_page_prune_and_freeze().
                               2085                 :                :  *
                               2086                 :                :  * If 'lp_truncate_only' is set, we are merely marking LP_DEAD line pointers
                               2087                 :                :  * as unused, not redirecting or removing anything else.  The
                               2088                 :                :  * PageRepairFragmentation() call is skipped in that case.
                               2089                 :                :  *
                               2090                 :                :  * If 'lp_truncate_only' is not set, the caller must hold a cleanup lock on
                               2091                 :                :  * the buffer.  If it is set, an ordinary exclusive lock suffices.
                               2092                 :                :  */
                               2093                 :                : void
  909                          2094                 :          85098 : heap_page_prune_execute(Buffer buffer, bool lp_truncate_only,
                               2095                 :                :                         OffsetNumber *redirected, int nredirected,
                               2096                 :                :                         OffsetNumber *nowdead, int ndead,
                               2097                 :                :                         OffsetNumber *nowunused, int nunused)
                               2098                 :                : {
  387 peter@eisentraut.org     2099                 :          85098 :     Page        page = BufferGetPage(buffer);
                               2100                 :                :     OffsetNumber *offnum;
                               2101                 :                :     HeapTupleHeader htup PG_USED_FOR_ASSERTS_ONLY;
                               2102                 :                : 
                               2103                 :                :     /* Shouldn't be called unless there's something to do */
 1993 pg@bowt.ie               2104   [ +  +  +  +  :          85098 :     Assert(nredirected > 0 || ndead > 0 || nunused > 0);
                                              -  + ]
                               2105                 :                : 
                               2106                 :                :     /* If 'lp_truncate_only', we can only remove already-dead line pointers */
  909 heikki.linnakangas@i     2107   [ +  +  +  -  :          85098 :     Assert(!lp_truncate_only || (nredirected == 0 && ndead == 0));
                                              -  + ]
                               2108                 :                : 
                               2109                 :                :     /* Update all redirected line pointers */
 6770 tgl@sss.pgh.pa.us        2110                 :          85098 :     offnum = redirected;
 1781 pg@bowt.ie               2111         [ +  + ]:         178344 :     for (int i = 0; i < nredirected; i++)
                               2112                 :                :     {
 6770 tgl@sss.pgh.pa.us        2113                 :          93246 :         OffsetNumber fromoff = *offnum++;
                               2114                 :          93246 :         OffsetNumber tooff = *offnum++;
                               2115                 :          93246 :         ItemId      fromlp = PageGetItemId(page, fromoff);
                               2116                 :                :         ItemId      tolp PG_USED_FOR_ASSERTS_ONLY;
                               2117                 :                : 
                               2118                 :                : #ifdef USE_ASSERT_CHECKING
                               2119                 :                : 
                               2120                 :                :         /*
                               2121                 :                :          * Any existing item that we set as an LP_REDIRECT (any 'from' item)
                               2122                 :                :          * must be the first item from a HOT chain.  If the item has tuple
                               2123                 :                :          * storage then it can't be a heap-only tuple.  Otherwise we are just
                               2124                 :                :          * maintaining an existing LP_REDIRECT from an existing HOT chain that
                               2125                 :                :          * has been pruned at least once before now.
                               2126                 :                :          */
 1781 pg@bowt.ie               2127         [ +  + ]:          93246 :         if (!ItemIdIsRedirected(fromlp))
                               2128                 :                :         {
                               2129   [ +  -  -  + ]:          83095 :             Assert(ItemIdHasStorage(fromlp) && ItemIdIsNormal(fromlp));
                               2130                 :                : 
                               2131                 :          83095 :             htup = (HeapTupleHeader) PageGetItem(page, fromlp);
                               2132         [ -  + ]:          83095 :             Assert(!HeapTupleHeaderIsHeapOnly(htup));
                               2133                 :                :         }
                               2134                 :                :         else
                               2135                 :                :         {
                               2136                 :                :             /* We shouldn't need to redundantly set the redirect */
                               2137         [ -  + ]:          10151 :             Assert(ItemIdGetRedirect(fromlp) != tooff);
                               2138                 :                :         }
                               2139                 :                : 
                               2140                 :                :         /*
                               2141                 :                :          * The item that we're about to set as an LP_REDIRECT (the 'from'
                               2142                 :                :          * item) will point to an existing item (the 'to' item) that is
                               2143                 :                :          * already a heap-only tuple.  There can be at most one LP_REDIRECT
                               2144                 :                :          * item per HOT chain.
                               2145                 :                :          *
                               2146                 :                :          * We need to keep around an LP_REDIRECT item (after original
                               2147                 :                :          * non-heap-only root tuple gets pruned away) so that it's always
                               2148                 :                :          * possible for VACUUM to easily figure out what TID to delete from
                               2149                 :                :          * indexes when an entire HOT chain becomes dead.  A heap-only tuple
                               2150                 :                :          * can never become LP_DEAD; an LP_REDIRECT item or a regular heap
                               2151                 :                :          * tuple can.
                               2152                 :                :          *
                               2153                 :                :          * This check may miss problems, e.g. the target of a redirect could
                               2154                 :                :          * be marked as unused subsequently. The page_verify_redirects() check
                               2155                 :                :          * below will catch such problems.
                               2156                 :                :          */
                               2157                 :          93246 :         tolp = PageGetItemId(page, tooff);
                               2158   [ +  -  -  + ]:          93246 :         Assert(ItemIdHasStorage(tolp) && ItemIdIsNormal(tolp));
                               2159                 :          93246 :         htup = (HeapTupleHeader) PageGetItem(page, tolp);
                               2160         [ -  + ]:          93246 :         Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2161                 :                : #endif
                               2162                 :                : 
 6068 tgl@sss.pgh.pa.us        2163                 :          93246 :         ItemIdSetRedirect(fromlp, tooff);
                               2164                 :                :     }
                               2165                 :                : 
                               2166                 :                :     /* Update all now-dead line pointers */
 6770                          2167                 :          85098 :     offnum = nowdead;
 1781 pg@bowt.ie               2168         [ +  + ]:        4101603 :     for (int i = 0; i < ndead; i++)
                               2169                 :                :     {
 6770 tgl@sss.pgh.pa.us        2170                 :        4016505 :         OffsetNumber off = *offnum++;
                               2171                 :        4016505 :         ItemId      lp = PageGetItemId(page, off);
                               2172                 :                : 
                               2173                 :                : #ifdef USE_ASSERT_CHECKING
                               2174                 :                : 
                               2175                 :                :         /*
                               2176                 :                :          * An LP_DEAD line pointer must be left behind when the original item
                               2177                 :                :          * (which is dead to everybody) could still be referenced by a TID in
                               2178                 :                :          * an index.  This should never be necessary with any individual
                               2179                 :                :          * heap-only tuple item, though. (It's not clear how much of a problem
                               2180                 :                :          * that would be, but there is no reason to allow it.)
                               2181                 :                :          */
 1781 pg@bowt.ie               2182         [ +  + ]:        4016505 :         if (ItemIdHasStorage(lp))
                               2183                 :                :         {
                               2184         [ -  + ]:        4002105 :             Assert(ItemIdIsNormal(lp));
                               2185                 :        4002105 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2186         [ -  + ]:        4002105 :             Assert(!HeapTupleHeaderIsHeapOnly(htup));
                               2187                 :                :         }
                               2188                 :                :         else
                               2189                 :                :         {
                               2190                 :                :             /* Whole HOT chain becomes dead */
                               2191         [ -  + ]:          14400 :             Assert(ItemIdIsRedirected(lp));
                               2192                 :                :         }
                               2193                 :                : #endif
                               2194                 :                : 
 6770 tgl@sss.pgh.pa.us        2195                 :        4016505 :         ItemIdSetDead(lp);
                               2196                 :                :     }
                               2197                 :                : 
                               2198                 :                :     /* Update all now-unused line pointers */
                               2199                 :          85098 :     offnum = nowunused;
 1781 pg@bowt.ie               2200         [ +  + ]:         358544 :     for (int i = 0; i < nunused; i++)
                               2201                 :                :     {
 6770 tgl@sss.pgh.pa.us        2202                 :         273446 :         OffsetNumber off = *offnum++;
                               2203                 :         273446 :         ItemId      lp = PageGetItemId(page, off);
                               2204                 :                : 
                               2205                 :                : #ifdef USE_ASSERT_CHECKING
                               2206                 :                : 
  909 heikki.linnakangas@i     2207         [ +  + ]:         273446 :         if (lp_truncate_only)
                               2208                 :                :         {
                               2209                 :                :             /* Setting LP_DEAD to LP_UNUSED in vacuum's second pass */
                               2210   [ +  -  -  + ]:         142668 :             Assert(ItemIdIsDead(lp) && !ItemIdHasStorage(lp));
                               2211                 :                :         }
                               2212                 :                :         else
                               2213                 :                :         {
                               2214                 :                :             /*
                               2215                 :                :              * When heap_page_prune_and_freeze() was called, mark_unused_now
                               2216                 :                :              * may have been passed as true, which allows would-be LP_DEAD
                               2217                 :                :              * items to be made LP_UNUSED instead.  This is only possible if
                               2218                 :                :              * the relation has no indexes.  If there are any dead items, then
                               2219                 :                :              * mark_unused_now was not true and every item being marked
                               2220                 :                :              * LP_UNUSED must refer to a heap-only tuple.
                               2221                 :                :              */
                               2222         [ +  + ]:         130778 :             if (ndead > 0)
                               2223                 :                :             {
                               2224   [ +  -  -  + ]:          61767 :                 Assert(ItemIdHasStorage(lp) && ItemIdIsNormal(lp));
                               2225                 :          61767 :                 htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2226         [ -  + ]:          61767 :                 Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2227                 :                :             }
                               2228                 :                :             else
                               2229         [ -  + ]:          69011 :                 Assert(ItemIdIsUsed(lp));
                               2230                 :                :         }
                               2231                 :                : 
                               2232                 :                : #endif
                               2233                 :                : 
 6770 tgl@sss.pgh.pa.us        2234                 :         273446 :         ItemIdSetUnused(lp);
                               2235                 :                :     }
                               2236                 :                : 
  909 heikki.linnakangas@i     2237         [ +  + ]:          85098 :     if (lp_truncate_only)
                               2238                 :           2762 :         PageTruncateLinePointerArray(page);
                               2239                 :                :     else
                               2240                 :                :     {
                               2241                 :                :         /*
                               2242                 :                :          * Finally, repair any fragmentation, and update the page's hint bit
                               2243                 :                :          * about whether it has free pointers.
                               2244                 :                :          */
                               2245                 :          82336 :         PageRepairFragmentation(page);
                               2246                 :                : 
                               2247                 :                :         /*
                               2248                 :                :          * Now that the page has been modified, assert that redirect items
                               2249                 :                :          * still point to valid targets.
                               2250                 :                :          */
                               2251                 :          82336 :         page_verify_redirects(page);
                               2252                 :                :     }
 1763 andres@anarazel.de       2253                 :          85098 : }
                               2254                 :                : 
                               2255                 :                : 
                               2256                 :                : /*
                               2257                 :                :  * If built with assertions, verify that all LP_REDIRECT items point to a
                               2258                 :                :  * valid item.
                               2259                 :                :  *
                               2260                 :                :  * One way that bugs related to HOT pruning show is redirect items pointing to
                               2261                 :                :  * removed tuples. It's not trivial to reliably check that marking an item
                               2262                 :                :  * unused will not orphan a redirect item during heap_prune_chain() /
                               2263                 :                :  * heap_page_prune_execute(), so we additionally check the whole page after
                               2264                 :                :  * pruning. Without this check such bugs would typically only cause asserts
                               2265                 :                :  * later, potentially well after the corruption has been introduced.
                               2266                 :                :  *
                               2267                 :                :  * Also check comments in heap_page_prune_execute()'s redirection loop.
                               2268                 :                :  */
                               2269                 :                : static void
                               2270                 :          82336 : page_verify_redirects(Page page)
                               2271                 :                : {
                               2272                 :                : #ifdef USE_ASSERT_CHECKING
                               2273                 :                :     OffsetNumber offnum;
                               2274                 :                :     OffsetNumber maxoff;
                               2275                 :                : 
                               2276                 :          82336 :     maxoff = PageGetMaxOffsetNumber(page);
                               2277                 :          82336 :     for (offnum = FirstOffsetNumber;
                               2278         [ +  + ]:        8218779 :          offnum <= maxoff;
                               2279                 :        8136443 :          offnum = OffsetNumberNext(offnum))
                               2280                 :                :     {
                               2281                 :        8136443 :         ItemId      itemid = PageGetItemId(page, offnum);
                               2282                 :                :         OffsetNumber targoff;
                               2283                 :                :         ItemId      targitem;
                               2284                 :                :         HeapTupleHeader htup;
                               2285                 :                : 
                               2286         [ +  + ]:        8136443 :         if (!ItemIdIsRedirected(itemid))
                               2287                 :        7882841 :             continue;
                               2288                 :                : 
                               2289                 :         253602 :         targoff = ItemIdGetRedirect(itemid);
                               2290                 :         253602 :         targitem = PageGetItemId(page, targoff);
                               2291                 :                : 
                               2292         [ -  + ]:         253602 :         Assert(ItemIdIsUsed(targitem));
                               2293         [ -  + ]:         253602 :         Assert(ItemIdIsNormal(targitem));
                               2294         [ -  + ]:         253602 :         Assert(ItemIdHasStorage(targitem));
                               2295                 :         253602 :         htup = (HeapTupleHeader) PageGetItem(page, targitem);
                               2296         [ -  + ]:         253602 :         Assert(HeapTupleHeaderIsHeapOnly(htup));
                               2297                 :                :     }
                               2298                 :                : #endif
 6940 tgl@sss.pgh.pa.us        2299                 :          82336 : }
                               2300                 :                : 
                               2301                 :                : 
                               2302                 :                : /*
                               2303                 :                :  * For all items in this page, find their respective root line pointers.
                               2304                 :                :  * If item k is part of a HOT-chain with root at item j, then we set
                               2305                 :                :  * root_offsets[k - 1] = j.
                               2306                 :                :  *
                               2307                 :                :  * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries.
                               2308                 :                :  * Unused entries are filled with InvalidOffsetNumber (zero).
                               2309                 :                :  *
                               2310                 :                :  * The function must be called with at least share lock on the buffer, to
                               2311                 :                :  * prevent concurrent prune operations.
                               2312                 :                :  *
                               2313                 :                :  * Note: The information collected here is valid only as long as the caller
                               2314                 :                :  * holds a pin on the buffer. Once pin is released, a tuple might be pruned
                               2315                 :                :  * and reused by a completely unrelated tuple.
                               2316                 :                :  */
                               2317                 :                : void
                               2318                 :         139793 : heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
                               2319                 :                : {
                               2320                 :                :     OffsetNumber offnum,
                               2321                 :                :                 maxoff;
                               2322                 :                : 
 2229 alvherre@alvh.no-ip.     2323   [ +  -  -  +  :         139793 :     MemSet(root_offsets, InvalidOffsetNumber,
                                     -  -  -  -  -  
                                                 - ]
                               2324                 :                :            MaxHeapTuplesPerPage * sizeof(OffsetNumber));
                               2325                 :                : 
 6940 tgl@sss.pgh.pa.us        2326                 :         139793 :     maxoff = PageGetMaxOffsetNumber(page);
 6704 bruce@momjian.us         2327         [ +  + ]:       11735129 :     for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
                               2328                 :                :     {
 6884                          2329                 :       11595336 :         ItemId      lp = PageGetItemId(page, offnum);
                               2330                 :                :         HeapTupleHeader htup;
                               2331                 :                :         OffsetNumber nextoffnum;
                               2332                 :                :         TransactionId priorXmax;
                               2333                 :                : 
                               2334                 :                :         /* skip unused and dead items */
 6940 tgl@sss.pgh.pa.us        2335   [ +  +  +  + ]:       11595336 :         if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
                               2336                 :          11626 :             continue;
                               2337                 :                : 
                               2338         [ +  + ]:       11583710 :         if (ItemIdIsNormal(lp))
                               2339                 :                :         {
                               2340                 :       11580459 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2341                 :                : 
                               2342                 :                :             /*
                               2343                 :                :              * Check if this tuple is part of a HOT-chain rooted at some other
                               2344                 :                :              * tuple. If so, skip it for now; we'll process it when we find
                               2345                 :                :              * its root.
                               2346                 :                :              */
                               2347         [ +  + ]:       11580459 :             if (HeapTupleHeaderIsHeapOnly(htup))
                               2348                 :           3627 :                 continue;
                               2349                 :                : 
                               2350                 :                :             /*
                               2351                 :                :              * This is either a plain tuple or the root of a HOT-chain.
                               2352                 :                :              * Remember it in the mapping.
                               2353                 :                :              */
                               2354                 :       11576832 :             root_offsets[offnum - 1] = offnum;
                               2355                 :                : 
                               2356                 :                :             /* If it's not the start of a HOT-chain, we're done with it */
                               2357         [ +  + ]:       11576832 :             if (!HeapTupleHeaderIsHotUpdated(htup))
                               2358                 :       11576550 :                 continue;
                               2359                 :                : 
                               2360                 :                :             /* Set up to scan the HOT-chain */
                               2361                 :            282 :             nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 4988 alvherre@alvh.no-ip.     2362                 :            282 :             priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               2363                 :                :         }
                               2364                 :                :         else
                               2365                 :                :         {
                               2366                 :                :             /* Must be a redirect item. We do not set its root_offsets entry */
 6940 tgl@sss.pgh.pa.us        2367         [ -  + ]:           3251 :             Assert(ItemIdIsRedirected(lp));
                               2368                 :                :             /* Set up to scan the HOT-chain */
                               2369                 :           3251 :             nextoffnum = ItemIdGetRedirect(lp);
                               2370                 :           3251 :             priorXmax = InvalidTransactionId;
                               2371                 :                :         }
                               2372                 :                : 
                               2373                 :                :         /*
                               2374                 :                :          * Now follow the HOT-chain and collect other tuples in the chain.
                               2375                 :                :          *
                               2376                 :                :          * Note: Even though this is a nested loop, the complexity of the
                               2377                 :                :          * function is O(N) because a tuple in the page should be visited not
                               2378                 :                :          * more than twice, once in the outer loop and once in HOT-chain
                               2379                 :                :          * chases.
                               2380                 :                :          */
                               2381                 :                :         for (;;)
                               2382                 :                :         {
                               2383                 :                :             /* Sanity check (pure paranoia) */
   66 pg@bowt.ie               2384         [ -  + ]:           3623 :             if (nextoffnum < FirstOffsetNumber)
 1824 pg@bowt.ie               2385                 :UBC           0 :                 break;
                               2386                 :                : 
                               2387                 :                :             /*
                               2388                 :                :              * An offset past the end of page's line pointer array is possible
                               2389                 :                :              * when the array was truncated
                               2390                 :                :              */
   66 pg@bowt.ie               2391         [ -  + ]:CBC        3623 :             if (nextoffnum > maxoff)
 1992 pg@bowt.ie               2392                 :UBC           0 :                 break;
                               2393                 :                : 
 6940 tgl@sss.pgh.pa.us        2394                 :CBC        3623 :             lp = PageGetItemId(page, nextoffnum);
                               2395                 :                : 
                               2396                 :                :             /* Check for broken chains */
                               2397         [ -  + ]:           3623 :             if (!ItemIdIsNormal(lp))
 6940 tgl@sss.pgh.pa.us        2398                 :UBC           0 :                 break;
                               2399                 :                : 
 6940 tgl@sss.pgh.pa.us        2400                 :CBC        3623 :             htup = (HeapTupleHeader) PageGetItem(page, lp);
                               2401                 :                : 
                               2402   [ +  +  -  + ]:           3995 :             if (TransactionIdIsValid(priorXmax) &&
 3244 alvherre@alvh.no-ip.     2403                 :            372 :                 !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
 6940 tgl@sss.pgh.pa.us        2404                 :UBC           0 :                 break;
                               2405                 :                : 
                               2406                 :                :             /* Remember the root line pointer for this item */
 6940 tgl@sss.pgh.pa.us        2407                 :CBC        3623 :             root_offsets[nextoffnum - 1] = offnum;
                               2408                 :                : 
                               2409                 :                :             /* Advance to next chain member, if any */
                               2410         [ +  + ]:           3623 :             if (!HeapTupleHeaderIsHotUpdated(htup))
                               2411                 :           3533 :                 break;
                               2412                 :                : 
                               2413                 :                :             /* HOT implies it can't have moved to different partition */
 3088 andres@anarazel.de       2414         [ -  + ]:             90 :             Assert(!HeapTupleHeaderIndicatesMovedPartitions(htup));
                               2415                 :                : 
 6940 tgl@sss.pgh.pa.us        2416                 :             90 :             nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
 4988 alvherre@alvh.no-ip.     2417                 :             90 :             priorXmax = HeapTupleHeaderGetUpdateXid(htup);
                               2418                 :                :         }
                               2419                 :                :     }
 6940 tgl@sss.pgh.pa.us        2420                 :         139793 : }
                               2421                 :                : 
                               2422                 :                : 
                               2423                 :                : /*
                               2424                 :                :  * Compare fields that describe actions required to freeze tuple with caller's
                               2425                 :                :  * open plan.  If everything matches then the frz tuple plan is equivalent to
                               2426                 :                :  * caller's plan.
                               2427                 :                :  */
                               2428                 :                : static inline bool
  909 heikki.linnakangas@i     2429                 :        1284790 : heap_log_freeze_eq(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
                               2430                 :                : {
                               2431         [ +  + ]:        1284790 :     if (plan->xmax == frz->xmax &&
                               2432         [ +  + ]:        1284757 :         plan->t_infomask2 == frz->t_infomask2 &&
                               2433         [ +  + ]:        1283599 :         plan->t_infomask == frz->t_infomask &&
                               2434         [ +  - ]:        1280187 :         plan->frzflags == frz->frzflags)
                               2435                 :        1280187 :         return true;
                               2436                 :                : 
                               2437                 :                :     /* Caller must call heap_log_freeze_new_plan again for frz */
                               2438                 :           4603 :     return false;
                               2439                 :                : }
                               2440                 :                : 
                               2441                 :                : /*
                               2442                 :                :  * Comparator used to deduplicate the freeze plans used in WAL records.
                               2443                 :                :  */
                               2444                 :                : static int
                               2445                 :        1598997 : heap_log_freeze_cmp(const void *arg1, const void *arg2)
                               2446                 :                : {
  251 peter@eisentraut.org     2447                 :        1598997 :     const HeapTupleFreeze *frz1 = arg1;
                               2448                 :        1598997 :     const HeapTupleFreeze *frz2 = arg2;
                               2449                 :                : 
  909 heikki.linnakangas@i     2450         [ +  + ]:        1598997 :     if (frz1->xmax < frz2->xmax)
                               2451                 :             66 :         return -1;
                               2452         [ +  + ]:        1598931 :     else if (frz1->xmax > frz2->xmax)
                               2453                 :             58 :         return 1;
                               2454                 :                : 
                               2455         [ +  + ]:        1598873 :     if (frz1->t_infomask2 < frz2->t_infomask2)
                               2456                 :           6284 :         return -1;
                               2457         [ +  + ]:        1592589 :     else if (frz1->t_infomask2 > frz2->t_infomask2)
                               2458                 :           6835 :         return 1;
                               2459                 :                : 
                               2460         [ +  + ]:        1585754 :     if (frz1->t_infomask < frz2->t_infomask)
                               2461                 :          13021 :         return -1;
                               2462         [ +  + ]:        1572733 :     else if (frz1->t_infomask > frz2->t_infomask)
                               2463                 :          23524 :         return 1;
                               2464                 :                : 
                               2465         [ -  + ]:        1549209 :     if (frz1->frzflags < frz2->frzflags)
  909 heikki.linnakangas@i     2466                 :UBC           0 :         return -1;
  909 heikki.linnakangas@i     2467         [ -  + ]:CBC     1549209 :     else if (frz1->frzflags > frz2->frzflags)
  909 heikki.linnakangas@i     2468                 :UBC           0 :         return 1;
                               2469                 :                : 
                               2470                 :                :     /*
                               2471                 :                :      * heap_log_freeze_eq would consider these tuple-wise plans to be equal.
                               2472                 :                :      * (So the tuples will share a single canonical freeze plan.)
                               2473                 :                :      *
                               2474                 :                :      * We tiebreak on page offset number to keep each freeze plan's page
                               2475                 :                :      * offset number array individually sorted. (Unnecessary, but be tidy.)
                               2476                 :                :      */
  909 heikki.linnakangas@i     2477         [ +  + ]:CBC     1549209 :     if (frz1->offset < frz2->offset)
                               2478                 :        1403551 :         return -1;
                               2479         [ +  - ]:         145658 :     else if (frz1->offset > frz2->offset)
                               2480                 :         145658 :         return 1;
                               2481                 :                : 
  909 heikki.linnakangas@i     2482                 :UBC           0 :     Assert(false);
                               2483                 :                :     return 0;
                               2484                 :                : }
                               2485                 :                : 
                               2486                 :                : /*
                               2487                 :                :  * Start new plan initialized using tuple-level actions.  At least one tuple
                               2488                 :                :  * will have steps required to freeze described by caller's plan during REDO.
                               2489                 :                :  */
                               2490                 :                : static inline void
  909 heikki.linnakangas@i     2491                 :CBC       29749 : heap_log_freeze_new_plan(xlhp_freeze_plan *plan, HeapTupleFreeze *frz)
                               2492                 :                : {
                               2493                 :          29749 :     plan->xmax = frz->xmax;
                               2494                 :          29749 :     plan->t_infomask2 = frz->t_infomask2;
                               2495                 :          29749 :     plan->t_infomask = frz->t_infomask;
                               2496                 :          29749 :     plan->frzflags = frz->frzflags;
                               2497                 :          29749 :     plan->ntuples = 1;           /* for now */
                               2498                 :          29749 : }
                               2499                 :                : 
                               2500                 :                : /*
                               2501                 :                :  * Deduplicate tuple-based freeze plans so that each distinct set of
                               2502                 :                :  * processing steps is only stored once in the WAL record.
                               2503                 :                :  * Called during original execution of freezing (for logged relations).
                               2504                 :                :  *
                               2505                 :                :  * Return value is number of plans set in *plans_out for caller.  Also writes
                               2506                 :                :  * an array of offset numbers into *offsets_out output argument for caller
                               2507                 :                :  * (actually there is one array per freeze plan, but that's not of immediate
                               2508                 :                :  * concern to our caller).
                               2509                 :                :  */
                               2510                 :                : static int
                               2511                 :          25146 : heap_log_freeze_plan(HeapTupleFreeze *tuples, int ntuples,
                               2512                 :                :                      xlhp_freeze_plan *plans_out,
                               2513                 :                :                      OffsetNumber *offsets_out)
                               2514                 :                : {
                               2515                 :          25146 :     int         nplans = 0;
                               2516                 :                : 
                               2517                 :                :     /* Sort tuple-based freeze plans in the order required to deduplicate */
                               2518                 :          25146 :     qsort(tuples, ntuples, sizeof(HeapTupleFreeze), heap_log_freeze_cmp);
                               2519                 :                : 
                               2520         [ +  + ]:        1335082 :     for (int i = 0; i < ntuples; i++)
                               2521                 :                :     {
                               2522                 :        1309936 :         HeapTupleFreeze *frz = tuples + i;
                               2523                 :                : 
                               2524         [ +  + ]:        1309936 :         if (i == 0)
                               2525                 :                :         {
                               2526                 :                :             /* New canonical freeze plan starting with first tup */
                               2527                 :          25146 :             heap_log_freeze_new_plan(plans_out, frz);
                               2528                 :          25146 :             nplans++;
                               2529                 :                :         }
                               2530         [ +  + ]:        1284790 :         else if (heap_log_freeze_eq(plans_out, frz))
                               2531                 :                :         {
                               2532                 :                :             /* tup matches open canonical plan -- include tup in it */
                               2533         [ -  + ]:        1280187 :             Assert(offsets_out[i - 1] < frz->offset);
                               2534                 :        1280187 :             plans_out->ntuples++;
                               2535                 :                :         }
                               2536                 :                :         else
                               2537                 :                :         {
                               2538                 :                :             /* Tup doesn't match current plan -- done with it now */
                               2539                 :           4603 :             plans_out++;
                               2540                 :                : 
                               2541                 :                :             /* New canonical freeze plan starting with this tup */
                               2542                 :           4603 :             heap_log_freeze_new_plan(plans_out, frz);
                               2543                 :           4603 :             nplans++;
                               2544                 :                :         }
                               2545                 :                : 
                               2546                 :                :         /*
                               2547                 :                :          * Save page offset number in dedicated buffer in passing.
                               2548                 :                :          *
                               2549                 :                :          * REDO routine relies on the record's offset numbers array grouping
                               2550                 :                :          * offset numbers by freeze plan.  The sort order within each grouping
                               2551                 :                :          * is ascending offset number order, just to keep things tidy.
                               2552                 :                :          */
                               2553                 :        1309936 :         offsets_out[i] = frz->offset;
                               2554                 :                :     }
                               2555                 :                : 
                               2556   [ +  -  -  + ]:          25146 :     Assert(nplans > 0 && nplans <= ntuples);
                               2557                 :                : 
                               2558                 :          25146 :     return nplans;
                               2559                 :                : }
                               2560                 :                : 
                               2561                 :                : /*
                               2562                 :                :  * Write an XLOG_HEAP2_PRUNE* WAL record
                               2563                 :                :  *
                               2564                 :                :  * This is used for several different page maintenance operations:
                               2565                 :                :  *
                               2566                 :                :  * - Page pruning, in VACUUM's 1st pass or on access: Some items are
                               2567                 :                :  *   redirected, some marked dead, and some removed altogether.
                               2568                 :                :  *
                               2569                 :                :  * - Freezing: Items are marked as 'frozen'.
                               2570                 :                :  *
                               2571                 :                :  * - Vacuum, 2nd pass: Items that are already LP_DEAD are marked as unused.
                               2572                 :                :  *
                               2573                 :                :  * They have enough commonalities that we use a single WAL record for them
                               2574                 :                :  * all.
                               2575                 :                :  *
                               2576                 :                :  * If replaying the record requires a cleanup lock, pass cleanup_lock = true.
                               2577                 :                :  * Replaying 'redirected' or 'dead' items always requires a cleanup lock, but
                               2578                 :                :  * replaying 'unused' items depends on whether they were all previously marked
                               2579                 :                :  * as dead.
                               2580                 :                :  *
                               2581                 :                :  * If the VM is being updated, vmflags will contain the bits to set. In this
                               2582                 :                :  * case, vmbuffer should already have been updated and marked dirty and should
                               2583                 :                :  * still be pinned and locked.
                               2584                 :                :  *
                               2585                 :                :  * Note: This function scribbles on the 'frozen' array.
                               2586                 :                :  *
                               2587                 :                :  * Note: This is called in a critical section, so careful what you do here.
                               2588                 :                :  */
                               2589                 :                : void
                               2590                 :         154611 : log_heap_prune_and_freeze(Relation relation, Buffer buffer,
                               2591                 :                :                           Buffer vmbuffer, uint8 vmflags,
                               2592                 :                :                           TransactionId conflict_xid,
                               2593                 :                :                           bool cleanup_lock,
                               2594                 :                :                           PruneReason reason,
                               2595                 :                :                           HeapTupleFreeze *frozen, int nfrozen,
                               2596                 :                :                           OffsetNumber *redirected, int nredirected,
                               2597                 :                :                           OffsetNumber *dead, int ndead,
                               2598                 :                :                           OffsetNumber *unused, int nunused)
                               2599                 :                : {
                               2600                 :                :     xl_heap_prune xlrec;
                               2601                 :                :     XLogRecPtr  recptr;
                               2602                 :                :     uint8       info;
                               2603                 :                :     uint8       regbuf_flags_heap;
                               2604                 :                : 
  180 melanieplageman@gmai     2605                 :         154611 :     Page        heap_page = BufferGetPage(buffer);
                               2606                 :                : 
                               2607                 :                :     /* The following local variables hold data registered in the WAL record: */
                               2608                 :                :     xlhp_freeze_plan plans[MaxHeapTuplesPerPage];
                               2609                 :                :     xlhp_freeze_plans freeze_plans;
                               2610                 :                :     xlhp_prune_items redirect_items;
                               2611                 :                :     xlhp_prune_items dead_items;
                               2612                 :                :     xlhp_prune_items unused_items;
                               2613                 :                :     OffsetNumber frz_offsets[MaxHeapTuplesPerPage];
  342                          2614   [ +  +  +  +  :         154611 :     bool        do_prune = nredirected > 0 || ndead > 0 || nunused > 0;
                                              +  + ]
                               2615                 :         154611 :     bool        do_set_vm = vmflags & VISIBILITYMAP_VALID_BITS;
  180                          2616                 :         154611 :     bool        heap_fpi_allowed = true;
                               2617                 :                : 
  342                          2618         [ -  + ]:         154611 :     Assert((vmflags & VISIBILITYMAP_VALID_BITS) == vmflags);
                               2619                 :                : 
  909 heikki.linnakangas@i     2620                 :         154611 :     xlrec.flags = 0;
  342 melanieplageman@gmai     2621                 :         154611 :     regbuf_flags_heap = REGBUF_STANDARD;
                               2622                 :                : 
                               2623                 :                :     /*
                               2624                 :                :      * We can avoid an FPI of the heap page if the only modification we are
                               2625                 :                :      * making to it is to set PD_ALL_VISIBLE and checksums/wal_log_hints are
                               2626                 :                :      * disabled.
                               2627                 :                :      *
                               2628                 :                :      * However, if the page has never been WAL-logged (LSN is invalid), we
                               2629                 :                :      * must force an FPI regardless.  This can happen when another backend
                               2630                 :                :      * extends the heap, initializes the page, and then fails before WAL-
                               2631                 :                :      * logging it.  Since heap extension is not WAL-logged, recovery might try
                               2632                 :                :      * to replay our record and find that the page isn't initialized, which
                               2633                 :                :      * would cause a PANIC.
                               2634                 :                :      */
  180                          2635         [ -  + ]:         154611 :     if (!XLogRecPtrIsValid(PageGetLSN(heap_page)))
  180 melanieplageman@gmai     2636                 :UBC           0 :         regbuf_flags_heap |= REGBUF_FORCE_IMAGE;
  180 melanieplageman@gmai     2637   [ +  +  +  +  :CBC      154611 :     else if (!do_prune && nfrozen == 0 && (!do_set_vm || !XLogHintBitIsNeeded()))
                                     +  -  +  +  +  
                                                 + ]
                               2638                 :                :     {
  342                          2639                 :           2852 :         regbuf_flags_heap |= REGBUF_NO_IMAGE;
  180                          2640                 :           2852 :         heap_fpi_allowed = false;
                               2641                 :                :     }
                               2642                 :                : 
                               2643                 :                :     /*
                               2644                 :                :      * Prepare data for the buffer.  The arrays are not actually in the
                               2645                 :                :      * buffer, but we pretend that they are.  When XLogInsert stores a full
                               2646                 :                :      * page image, the arrays can be omitted.
                               2647                 :                :      */
  909 heikki.linnakangas@i     2648                 :         154611 :     XLogBeginInsert();
  342 melanieplageman@gmai     2649                 :         154611 :     XLogRegisterBuffer(0, buffer, regbuf_flags_heap);
                               2650                 :                : 
                               2651         [ +  + ]:         154611 :     if (do_set_vm)
                               2652                 :          82348 :         XLogRegisterBuffer(1, vmbuffer, 0);
                               2653                 :                : 
  909 heikki.linnakangas@i     2654         [ +  + ]:         154611 :     if (nfrozen > 0)
                               2655                 :                :     {
                               2656                 :                :         int         nplans;
                               2657                 :                : 
                               2658                 :          25146 :         xlrec.flags |= XLHP_HAS_FREEZE_PLANS;
                               2659                 :                : 
                               2660                 :                :         /*
                               2661                 :                :          * Prepare deduplicated representation for use in the WAL record. This
                               2662                 :                :          * destructively sorts frozen tuples array in-place.
                               2663                 :                :          */
                               2664                 :          25146 :         nplans = heap_log_freeze_plan(frozen, nfrozen, plans, frz_offsets);
                               2665                 :                : 
                               2666                 :          25146 :         freeze_plans.nplans = nplans;
  586 peter@eisentraut.org     2667                 :          25146 :         XLogRegisterBufData(0, &freeze_plans,
                               2668                 :                :                             offsetof(xlhp_freeze_plans, plans));
                               2669                 :          25146 :         XLogRegisterBufData(0, plans,
                               2670                 :                :                             sizeof(xlhp_freeze_plan) * nplans);
                               2671                 :                :     }
  909 heikki.linnakangas@i     2672         [ +  + ]:         154611 :     if (nredirected > 0)
                               2673                 :                :     {
                               2674                 :          18071 :         xlrec.flags |= XLHP_HAS_REDIRECTIONS;
                               2675                 :                : 
                               2676                 :          18071 :         redirect_items.ntargets = nredirected;
  586 peter@eisentraut.org     2677                 :          18071 :         XLogRegisterBufData(0, &redirect_items,
                               2678                 :                :                             offsetof(xlhp_prune_items, data));
                               2679                 :          18071 :         XLogRegisterBufData(0, redirected,
                               2680                 :                :                             sizeof(OffsetNumber[2]) * nredirected);
                               2681                 :                :     }
  909 heikki.linnakangas@i     2682         [ +  + ]:         154611 :     if (ndead > 0)
                               2683                 :                :     {
                               2684                 :          59307 :         xlrec.flags |= XLHP_HAS_DEAD_ITEMS;
                               2685                 :                : 
                               2686                 :          59307 :         dead_items.ntargets = ndead;
  586 peter@eisentraut.org     2687                 :          59307 :         XLogRegisterBufData(0, &dead_items,
                               2688                 :                :                             offsetof(xlhp_prune_items, data));
                               2689                 :          59307 :         XLogRegisterBufData(0, dead,
                               2690                 :                :                             sizeof(OffsetNumber) * ndead);
                               2691                 :                :     }
  909 heikki.linnakangas@i     2692         [ +  + ]:         154611 :     if (nunused > 0)
                               2693                 :                :     {
                               2694                 :          37721 :         xlrec.flags |= XLHP_HAS_NOW_UNUSED_ITEMS;
                               2695                 :                : 
                               2696                 :          37721 :         unused_items.ntargets = nunused;
  586 peter@eisentraut.org     2697                 :          37721 :         XLogRegisterBufData(0, &unused_items,
                               2698                 :                :                             offsetof(xlhp_prune_items, data));
                               2699                 :          37721 :         XLogRegisterBufData(0, unused,
                               2700                 :                :                             sizeof(OffsetNumber) * nunused);
                               2701                 :                :     }
  909 heikki.linnakangas@i     2702         [ +  + ]:         154611 :     if (nfrozen > 0)
  586 peter@eisentraut.org     2703                 :          25146 :         XLogRegisterBufData(0, frz_offsets,
                               2704                 :                :                             sizeof(OffsetNumber) * nfrozen);
                               2705                 :                : 
                               2706                 :                :     /*
                               2707                 :                :      * Prepare the main xl_heap_prune record.  We already set the XLHP_HAS_*
                               2708                 :                :      * flag above.
                               2709                 :                :      */
  342 melanieplageman@gmai     2710         [ +  + ]:         154611 :     if (vmflags & VISIBILITYMAP_ALL_VISIBLE)
                               2711                 :                :     {
                               2712                 :          82348 :         xlrec.flags |= XLHP_VM_ALL_VISIBLE;
                               2713         [ +  + ]:          82348 :         if (vmflags & VISIBILITYMAP_ALL_FROZEN)
                               2714                 :          48740 :             xlrec.flags |= XLHP_VM_ALL_FROZEN;
                               2715                 :                :     }
  909 heikki.linnakangas@i     2716   [ +  +  +  +  :         154611 :     if (RelationIsAccessibleInLogicalDecoding(relation))
                                     +  -  -  +  -  
                                     -  -  -  +  +  
                                     +  +  -  +  -  
                                           -  +  - ]
                               2717                 :            656 :         xlrec.flags |= XLHP_IS_CATALOG_REL;
                               2718         [ +  + ]:         154611 :     if (TransactionIdIsValid(conflict_xid))
                               2719                 :         120687 :         xlrec.flags |= XLHP_HAS_CONFLICT_HORIZON;
                               2720         [ +  + ]:         154611 :     if (cleanup_lock)
                               2721                 :         131996 :         xlrec.flags |= XLHP_CLEANUP_LOCK;
                               2722                 :                :     else
                               2723                 :                :     {
                               2724   [ +  -  -  + ]:          22615 :         Assert(nredirected == 0 && ndead == 0);
                               2725                 :                :         /* also, any items in 'unused' must've been LP_DEAD previously */
                               2726                 :                :     }
  586 peter@eisentraut.org     2727                 :         154611 :     XLogRegisterData(&xlrec, SizeOfHeapPrune);
  909 heikki.linnakangas@i     2728         [ +  + ]:         154611 :     if (TransactionIdIsValid(conflict_xid))
  586 peter@eisentraut.org     2729                 :         120687 :         XLogRegisterData(&conflict_xid, sizeof(TransactionId));
                               2730                 :                : 
  909 heikki.linnakangas@i     2731   [ +  +  +  - ]:         154611 :     switch (reason)
                               2732                 :                :     {
                               2733                 :          69487 :         case PRUNE_ON_ACCESS:
                               2734                 :          69487 :             info = XLOG_HEAP2_PRUNE_ON_ACCESS;
                               2735                 :          69487 :             break;
                               2736                 :          62509 :         case PRUNE_VACUUM_SCAN:
                               2737                 :          62509 :             info = XLOG_HEAP2_PRUNE_VACUUM_SCAN;
                               2738                 :          62509 :             break;
                               2739                 :          22615 :         case PRUNE_VACUUM_CLEANUP:
                               2740                 :          22615 :             info = XLOG_HEAP2_PRUNE_VACUUM_CLEANUP;
                               2741                 :          22615 :             break;
  909 heikki.linnakangas@i     2742                 :UBC           0 :         default:
                               2743         [ #  # ]:              0 :             elog(ERROR, "unrecognized prune reason: %d", (int) reason);
                               2744                 :                :             break;
                               2745                 :                :     }
  909 heikki.linnakangas@i     2746                 :CBC      154611 :     recptr = XLogInsert(RM_HEAP2_ID, info);
                               2747                 :                : 
  342 melanieplageman@gmai     2748         [ +  + ]:         154611 :     if (do_set_vm)
                               2749                 :                :     {
                               2750         [ -  + ]:          82348 :         Assert(BufferIsDirty(vmbuffer));
                               2751                 :          82348 :         PageSetLSN(BufferGetPage(vmbuffer), recptr);
                               2752                 :                :     }
                               2753                 :                : 
                               2754                 :                :     /*
                               2755                 :                :      * If we explicitly skip an FPI, we must not stamp the heap page with this
                               2756                 :                :      * record's LSN. Recovery skips records <= the stamped LSN, so this could
                               2757                 :                :      * lead to skipping an earlier FPI needed to repair a torn page.
                               2758                 :                :      */
  180                          2759         [ +  + ]:         154611 :     if (heap_fpi_allowed)
                               2760                 :                :     {
  342                          2761         [ -  + ]:         151759 :         Assert(BufferIsDirty(buffer));
  180                          2762                 :         151759 :         PageSetLSN(heap_page, recptr);
                               2763                 :                :     }
  909 heikki.linnakangas@i     2764                 :         154611 : }
        

Generated by: LCOV version 2.0-1