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

Generated by: LCOV version 2.0-1