LCOV - code coverage report
Current view: top level - src/backend/access/heap - heapam_handler.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 699 761 91.9 %
Date: 2020-05-31 23:07:13 Functions: 34 34 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * heapam_handler.c
       4             :  *    heap table access method code
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/access/heap/heapam_handler.c
      12             :  *
      13             :  *
      14             :  * NOTES
      15             :  *    This files wires up the lower level heapam.c et al routines with the
      16             :  *    tableam abstraction.
      17             :  *
      18             :  *-------------------------------------------------------------------------
      19             :  */
      20             : #include "postgres.h"
      21             : 
      22             : #include "access/genam.h"
      23             : #include "access/heapam.h"
      24             : #include "access/heaptoast.h"
      25             : #include "access/multixact.h"
      26             : #include "access/rewriteheap.h"
      27             : #include "access/tableam.h"
      28             : #include "access/tsmapi.h"
      29             : #include "access/xact.h"
      30             : #include "catalog/catalog.h"
      31             : #include "catalog/index.h"
      32             : #include "catalog/storage.h"
      33             : #include "catalog/storage_xlog.h"
      34             : #include "commands/progress.h"
      35             : #include "executor/executor.h"
      36             : #include "miscadmin.h"
      37             : #include "pgstat.h"
      38             : #include "storage/bufmgr.h"
      39             : #include "storage/bufpage.h"
      40             : #include "storage/lmgr.h"
      41             : #include "storage/predicate.h"
      42             : #include "storage/procarray.h"
      43             : #include "storage/smgr.h"
      44             : #include "utils/builtins.h"
      45             : #include "utils/rel.h"
      46             : 
      47             : static void reform_and_rewrite_tuple(HeapTuple tuple,
      48             :                                      Relation OldHeap, Relation NewHeap,
      49             :                                      Datum *values, bool *isnull, RewriteState rwstate);
      50             : 
      51             : static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer,
      52             :                                    HeapTuple tuple,
      53             :                                    OffsetNumber tupoffset);
      54             : 
      55             : static BlockNumber heapam_scan_get_blocks_done(HeapScanDesc hscan);
      56             : 
      57             : static const TableAmRoutine heapam_methods;
      58             : 
      59             : 
      60             : /* ------------------------------------------------------------------------
      61             :  * Slot related callbacks for heap AM
      62             :  * ------------------------------------------------------------------------
      63             :  */
      64             : 
      65             : static const TupleTableSlotOps *
      66    19897460 : heapam_slot_callbacks(Relation relation)
      67             : {
      68    19897460 :     return &TTSOpsBufferHeapTuple;
      69             : }
      70             : 
      71             : 
      72             : /* ------------------------------------------------------------------------
      73             :  * Index Scan Callbacks for heap AM
      74             :  * ------------------------------------------------------------------------
      75             :  */
      76             : 
      77             : static IndexFetchTableData *
      78    18528532 : heapam_index_fetch_begin(Relation rel)
      79             : {
      80    18528532 :     IndexFetchHeapData *hscan = palloc0(sizeof(IndexFetchHeapData));
      81             : 
      82    18528532 :     hscan->xs_base.rel = rel;
      83    18528532 :     hscan->xs_cbuf = InvalidBuffer;
      84             : 
      85    18528532 :     return &hscan->xs_base;
      86             : }
      87             : 
      88             : static void
      89    33935534 : heapam_index_fetch_reset(IndexFetchTableData *scan)
      90             : {
      91    33935534 :     IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
      92             : 
      93    33935534 :     if (BufferIsValid(hscan->xs_cbuf))
      94             :     {
      95    15698880 :         ReleaseBuffer(hscan->xs_cbuf);
      96    15698880 :         hscan->xs_cbuf = InvalidBuffer;
      97             :     }
      98    33935534 : }
      99             : 
     100             : static void
     101    18527786 : heapam_index_fetch_end(IndexFetchTableData *scan)
     102             : {
     103    18527786 :     IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
     104             : 
     105    18527786 :     heapam_index_fetch_reset(scan);
     106             : 
     107    18527786 :     pfree(hscan);
     108    18527786 : }
     109             : 
     110             : static bool
     111    23806456 : heapam_index_fetch_tuple(struct IndexFetchTableData *scan,
     112             :                          ItemPointer tid,
     113             :                          Snapshot snapshot,
     114             :                          TupleTableSlot *slot,
     115             :                          bool *call_again, bool *all_dead)
     116             : {
     117    23806456 :     IndexFetchHeapData *hscan = (IndexFetchHeapData *) scan;
     118    23806456 :     BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
     119             :     bool        got_heap_tuple;
     120             : 
     121             :     Assert(TTS_IS_BUFFERTUPLE(slot));
     122             : 
     123             :     /* We can skip the buffer-switching logic if we're in mid-HOT chain. */
     124    23806456 :     if (!*call_again)
     125             :     {
     126             :         /* Switch to correct buffer if we don't have it already */
     127    23663520 :         Buffer      prev_buf = hscan->xs_cbuf;
     128             : 
     129    47327040 :         hscan->xs_cbuf = ReleaseAndReadBuffer(hscan->xs_cbuf,
     130             :                                               hscan->xs_base.rel,
     131    23663520 :                                               ItemPointerGetBlockNumber(tid));
     132             : 
     133             :         /*
     134             :          * Prune page, but only if we weren't already on this page
     135             :          */
     136    23663520 :         if (prev_buf != hscan->xs_cbuf)
     137    17171960 :             heap_page_prune_opt(hscan->xs_base.rel, hscan->xs_cbuf);
     138             :     }
     139             : 
     140             :     /* Obtain share-lock on the buffer so we can examine visibility */
     141    23806456 :     LockBuffer(hscan->xs_cbuf, BUFFER_LOCK_SHARE);
     142    23806456 :     got_heap_tuple = heap_hot_search_buffer(tid,
     143             :                                             hscan->xs_base.rel,
     144             :                                             hscan->xs_cbuf,
     145             :                                             snapshot,
     146             :                                             &bslot->base.tupdata,
     147             :                                             all_dead,
     148    23806456 :                                             !*call_again);
     149    23806456 :     bslot->base.tupdata.t_self = *tid;
     150    23806456 :     LockBuffer(hscan->xs_cbuf, BUFFER_LOCK_UNLOCK);
     151             : 
     152    23806456 :     if (got_heap_tuple)
     153             :     {
     154             :         /*
     155             :          * Only in a non-MVCC snapshot can more than one member of the HOT
     156             :          * chain be visible.
     157             :          */
     158    15738506 :         *call_again = !IsMVCCSnapshot(snapshot);
     159             : 
     160    15738506 :         slot->tts_tableOid = RelationGetRelid(scan->rel);
     161    15738506 :         ExecStoreBufferHeapTuple(&bslot->base.tupdata, slot, hscan->xs_cbuf);
     162             :     }
     163             :     else
     164             :     {
     165             :         /* We've reached the end of the HOT chain. */
     166     8067950 :         *call_again = false;
     167             :     }
     168             : 
     169    23806456 :     return got_heap_tuple;
     170             : }
     171             : 
     172             : 
     173             : /* ------------------------------------------------------------------------
     174             :  * Callbacks for non-modifying operations on individual tuples for heap AM
     175             :  * ------------------------------------------------------------------------
     176             :  */
     177             : 
     178             : static bool
     179       14682 : heapam_fetch_row_version(Relation relation,
     180             :                          ItemPointer tid,
     181             :                          Snapshot snapshot,
     182             :                          TupleTableSlot *slot)
     183             : {
     184       14682 :     BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
     185             :     Buffer      buffer;
     186             : 
     187             :     Assert(TTS_IS_BUFFERTUPLE(slot));
     188             : 
     189       14682 :     bslot->base.tupdata.t_self = *tid;
     190       14682 :     if (heap_fetch(relation, snapshot, &bslot->base.tupdata, &buffer))
     191             :     {
     192             :         /* store in slot, transferring existing pin */
     193       14664 :         ExecStorePinnedBufferHeapTuple(&bslot->base.tupdata, slot, buffer);
     194       14664 :         slot->tts_tableOid = RelationGetRelid(relation);
     195             : 
     196       14664 :         return true;
     197             :     }
     198             : 
     199          18 :     return false;
     200             : }
     201             : 
     202             : static bool
     203         362 : heapam_tuple_tid_valid(TableScanDesc scan, ItemPointer tid)
     204             : {
     205         362 :     HeapScanDesc hscan = (HeapScanDesc) scan;
     206             : 
     207         724 :     return ItemPointerIsValid(tid) &&
     208         362 :         ItemPointerGetBlockNumber(tid) < hscan->rs_nblocks;
     209             : }
     210             : 
     211             : static bool
     212      112714 : heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
     213             :                                 Snapshot snapshot)
     214             : {
     215      112714 :     BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
     216             :     bool        res;
     217             : 
     218             :     Assert(TTS_IS_BUFFERTUPLE(slot));
     219             :     Assert(BufferIsValid(bslot->buffer));
     220             : 
     221             :     /*
     222             :      * We need buffer pin and lock to call HeapTupleSatisfiesVisibility.
     223             :      * Caller should be holding pin, but not lock.
     224             :      */
     225      112714 :     LockBuffer(bslot->buffer, BUFFER_LOCK_SHARE);
     226      112714 :     res = HeapTupleSatisfiesVisibility(bslot->base.tuple, snapshot,
     227             :                                        bslot->buffer);
     228      112714 :     LockBuffer(bslot->buffer, BUFFER_LOCK_UNLOCK);
     229             : 
     230      112714 :     return res;
     231             : }
     232             : 
     233             : 
     234             : /* ----------------------------------------------------------------------------
     235             :  *  Functions for manipulations of physical tuples for heap AM.
     236             :  * ----------------------------------------------------------------------------
     237             :  */
     238             : 
     239             : static void
     240    12214024 : heapam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
     241             :                     int options, BulkInsertState bistate)
     242             : {
     243    12214024 :     bool        shouldFree = true;
     244    12214024 :     HeapTuple   tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
     245             : 
     246             :     /* Update the tuple with table oid */
     247    12214024 :     slot->tts_tableOid = RelationGetRelid(relation);
     248    12214024 :     tuple->t_tableOid = slot->tts_tableOid;
     249             : 
     250             :     /* Perform the insertion, and copy the resulting ItemPointer */
     251    12214024 :     heap_insert(relation, tuple, cid, options, bistate);
     252    12214002 :     ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
     253             : 
     254    12214002 :     if (shouldFree)
     255     1694274 :         pfree(tuple);
     256    12214002 : }
     257             : 
     258             : static void
     259        3898 : heapam_tuple_insert_speculative(Relation relation, TupleTableSlot *slot,
     260             :                                 CommandId cid, int options,
     261             :                                 BulkInsertState bistate, uint32 specToken)
     262             : {
     263        3898 :     bool        shouldFree = true;
     264        3898 :     HeapTuple   tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
     265             : 
     266             :     /* Update the tuple with table oid */
     267        3898 :     slot->tts_tableOid = RelationGetRelid(relation);
     268        3898 :     tuple->t_tableOid = slot->tts_tableOid;
     269             : 
     270        3898 :     HeapTupleHeaderSetSpeculativeToken(tuple->t_data, specToken);
     271        3898 :     options |= HEAP_INSERT_SPECULATIVE;
     272             : 
     273             :     /* Perform the insertion, and copy the resulting ItemPointer */
     274        3898 :     heap_insert(relation, tuple, cid, options, bistate);
     275        3898 :     ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
     276             : 
     277        3898 :     if (shouldFree)
     278          46 :         pfree(tuple);
     279        3898 : }
     280             : 
     281             : static void
     282        3894 : heapam_tuple_complete_speculative(Relation relation, TupleTableSlot *slot,
     283             :                                   uint32 specToken, bool succeeded)
     284             : {
     285        3894 :     bool        shouldFree = true;
     286        3894 :     HeapTuple   tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
     287             : 
     288             :     /* adjust the tuple's state accordingly */
     289        3894 :     if (succeeded)
     290        3884 :         heap_finish_speculative(relation, &slot->tts_tid);
     291             :     else
     292          10 :         heap_abort_speculative(relation, &slot->tts_tid);
     293             : 
     294        3894 :     if (shouldFree)
     295          46 :         pfree(tuple);
     296        3894 : }
     297             : 
     298             : static TM_Result
     299      877238 : heapam_tuple_delete(Relation relation, ItemPointer tid, CommandId cid,
     300             :                     Snapshot snapshot, Snapshot crosscheck, bool wait,
     301             :                     TM_FailureData *tmfd, bool changingPart)
     302             : {
     303             :     /*
     304             :      * Currently Deleting of index tuples are handled at vacuum, in case if
     305             :      * the storage itself is cleaning the dead tuples by itself, it is the
     306             :      * time to call the index tuple deletion also.
     307             :      */
     308      877238 :     return heap_delete(relation, tid, cid, crosscheck, wait, tmfd, changingPart);
     309             : }
     310             : 
     311             : 
     312             : static TM_Result
     313      132228 : heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
     314             :                     CommandId cid, Snapshot snapshot, Snapshot crosscheck,
     315             :                     bool wait, TM_FailureData *tmfd,
     316             :                     LockTupleMode *lockmode, bool *update_indexes)
     317             : {
     318      132228 :     bool        shouldFree = true;
     319      132228 :     HeapTuple   tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
     320             :     TM_Result   result;
     321             : 
     322             :     /* Update the tuple with table oid */
     323      132228 :     slot->tts_tableOid = RelationGetRelid(relation);
     324      132228 :     tuple->t_tableOid = slot->tts_tableOid;
     325             : 
     326      132228 :     result = heap_update(relation, otid, tuple, cid, crosscheck, wait,
     327             :                          tmfd, lockmode);
     328      132204 :     ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
     329             : 
     330             :     /*
     331             :      * Decide whether new index entries are needed for the tuple
     332             :      *
     333             :      * Note: heap_update returns the tid (location) of the new tuple in the
     334             :      * t_self field.
     335             :      *
     336             :      * If it's a HOT update, we mustn't insert new index entries.
     337             :      */
     338      132204 :     *update_indexes = result == TM_Ok && !HeapTupleIsHeapOnly(tuple);
     339             : 
     340      132204 :     if (shouldFree)
     341         300 :         pfree(tuple);
     342             : 
     343      132204 :     return result;
     344             : }
     345             : 
     346             : static TM_Result
     347       18442 : heapam_tuple_lock(Relation relation, ItemPointer tid, Snapshot snapshot,
     348             :                   TupleTableSlot *slot, CommandId cid, LockTupleMode mode,
     349             :                   LockWaitPolicy wait_policy, uint8 flags,
     350             :                   TM_FailureData *tmfd)
     351             : {
     352       18442 :     BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
     353             :     TM_Result   result;
     354             :     Buffer      buffer;
     355       18442 :     HeapTuple   tuple = &bslot->base.tupdata;
     356             :     bool        follow_updates;
     357             : 
     358       18442 :     follow_updates = (flags & TUPLE_LOCK_FLAG_LOCK_UPDATE_IN_PROGRESS) != 0;
     359       18442 :     tmfd->traversed = false;
     360             : 
     361             :     Assert(TTS_IS_BUFFERTUPLE(slot));
     362             : 
     363       18620 : tuple_lock_retry:
     364       18620 :     tuple->t_self = *tid;
     365       18620 :     result = heap_lock_tuple(relation, tuple, cid, mode, wait_policy,
     366             :                              follow_updates, &buffer, tmfd);
     367             : 
     368       18602 :     if (result == TM_Updated &&
     369         240 :         (flags & TUPLE_LOCK_FLAG_FIND_LAST_VERSION))
     370             :     {
     371         214 :         ReleaseBuffer(buffer);
     372             :         /* Should not encounter speculative tuple on recheck */
     373             :         Assert(!HeapTupleHeaderIsSpeculative(tuple->t_data));
     374             : 
     375         214 :         if (!ItemPointerEquals(&tmfd->ctid, &tuple->t_self))
     376             :         {
     377             :             SnapshotData SnapshotDirty;
     378             :             TransactionId priorXmax;
     379             : 
     380             :             /* it was updated, so look at the updated version */
     381         214 :             *tid = tmfd->ctid;
     382             :             /* updated row should have xmin matching this xmax */
     383         214 :             priorXmax = tmfd->xmax;
     384             : 
     385             :             /* signal that a tuple later in the chain is getting locked */
     386         214 :             tmfd->traversed = true;
     387             : 
     388             :             /*
     389             :              * fetch target tuple
     390             :              *
     391             :              * Loop here to deal with updated or busy tuples
     392             :              */
     393         214 :             InitDirtySnapshot(SnapshotDirty);
     394             :             for (;;)
     395             :             {
     396         262 :                 if (ItemPointerIndicatesMovedPartitions(tid))
     397          16 :                     ereport(ERROR,
     398             :                             (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
     399             :                              errmsg("tuple to be locked was already moved to another partition due to concurrent update")));
     400             : 
     401         246 :                 tuple->t_self = *tid;
     402         246 :                 if (heap_fetch(relation, &SnapshotDirty, tuple, &buffer))
     403             :                 {
     404             :                     /*
     405             :                      * If xmin isn't what we're expecting, the slot must have
     406             :                      * been recycled and reused for an unrelated tuple.  This
     407             :                      * implies that the latest version of the row was deleted,
     408             :                      * so we need do nothing.  (Should be safe to examine xmin
     409             :                      * without getting buffer's content lock.  We assume
     410             :                      * reading a TransactionId to be atomic, and Xmin never
     411             :                      * changes in an existing tuple, except to invalid or
     412             :                      * frozen, and neither of those can match priorXmax.)
     413             :                      */
     414         192 :                     if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple->t_data),
     415             :                                              priorXmax))
     416             :                     {
     417           0 :                         ReleaseBuffer(buffer);
     418          18 :                         return TM_Deleted;
     419             :                     }
     420             : 
     421             :                     /* otherwise xmin should not be dirty... */
     422         192 :                     if (TransactionIdIsValid(SnapshotDirty.xmin))
     423           0 :                         ereport(ERROR,
     424             :                                 (errcode(ERRCODE_DATA_CORRUPTED),
     425             :                                  errmsg_internal("t_xmin is uncommitted in tuple to be updated")));
     426             : 
     427             :                     /*
     428             :                      * If tuple is being updated by other transaction then we
     429             :                      * have to wait for its commit/abort, or die trying.
     430             :                      */
     431         192 :                     if (TransactionIdIsValid(SnapshotDirty.xmax))
     432             :                     {
     433           4 :                         ReleaseBuffer(buffer);
     434           4 :                         switch (wait_policy)
     435             :                         {
     436           0 :                             case LockWaitBlock:
     437           0 :                                 XactLockTableWait(SnapshotDirty.xmax,
     438             :                                                   relation, &tuple->t_self,
     439             :                                                   XLTW_FetchUpdated);
     440           0 :                                 break;
     441           2 :                             case LockWaitSkip:
     442           2 :                                 if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
     443             :                                     /* skip instead of waiting */
     444           2 :                                     return TM_WouldBlock;
     445           0 :                                 break;
     446           2 :                             case LockWaitError:
     447           2 :                                 if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
     448           2 :                                     ereport(ERROR,
     449             :                                             (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
     450             :                                              errmsg("could not obtain lock on row in relation \"%s\"",
     451             :                                                     RelationGetRelationName(relation))));
     452           0 :                                 break;
     453             :                         }
     454           0 :                         continue;   /* loop back to repeat heap_fetch */
     455             :                     }
     456             : 
     457             :                     /*
     458             :                      * If tuple was inserted by our own transaction, we have
     459             :                      * to check cmin against cid: cmin >= current CID means
     460             :                      * our command cannot see the tuple, so we should ignore
     461             :                      * it. Otherwise heap_lock_tuple() will throw an error,
     462             :                      * and so would any later attempt to update or delete the
     463             :                      * tuple.  (We need not check cmax because
     464             :                      * HeapTupleSatisfiesDirty will consider a tuple deleted
     465             :                      * by our transaction dead, regardless of cmax.)  We just
     466             :                      * checked that priorXmax == xmin, so we can test that
     467             :                      * variable instead of doing HeapTupleHeaderGetXmin again.
     468             :                      */
     469         198 :                     if (TransactionIdIsCurrentTransactionId(priorXmax) &&
     470          10 :                         HeapTupleHeaderGetCmin(tuple->t_data) >= cid)
     471             :                     {
     472          10 :                         tmfd->xmax = priorXmax;
     473             : 
     474             :                         /*
     475             :                          * Cmin is the problematic value, so store that. See
     476             :                          * above.
     477             :                          */
     478          10 :                         tmfd->cmax = HeapTupleHeaderGetCmin(tuple->t_data);
     479          10 :                         ReleaseBuffer(buffer);
     480          10 :                         return TM_SelfModified;
     481             :                     }
     482             : 
     483             :                     /*
     484             :                      * This is a live tuple, so try to lock it again.
     485             :                      */
     486         178 :                     ReleaseBuffer(buffer);
     487         178 :                     goto tuple_lock_retry;
     488             :                 }
     489             : 
     490             :                 /*
     491             :                  * If the referenced slot was actually empty, the latest
     492             :                  * version of the row must have been deleted, so we need do
     493             :                  * nothing.
     494             :                  */
     495          54 :                 if (tuple->t_data == NULL)
     496             :                 {
     497           0 :                     return TM_Deleted;
     498             :                 }
     499             : 
     500             :                 /*
     501             :                  * As above, if xmin isn't what we're expecting, do nothing.
     502             :                  */
     503          54 :                 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple->t_data),
     504             :                                          priorXmax))
     505             :                 {
     506           0 :                     if (BufferIsValid(buffer))
     507           0 :                         ReleaseBuffer(buffer);
     508           0 :                     return TM_Deleted;
     509             :                 }
     510             : 
     511             :                 /*
     512             :                  * If we get here, the tuple was found but failed
     513             :                  * SnapshotDirty. Assuming the xmin is either a committed xact
     514             :                  * or our own xact (as it certainly should be if we're trying
     515             :                  * to modify the tuple), this must mean that the row was
     516             :                  * updated or deleted by either a committed xact or our own
     517             :                  * xact.  If it was deleted, we can ignore it; if it was
     518             :                  * updated then chain up to the next version and repeat the
     519             :                  * whole process.
     520             :                  *
     521             :                  * As above, it should be safe to examine xmax and t_ctid
     522             :                  * without the buffer content lock, because they can't be
     523             :                  * changing.
     524             :                  */
     525          54 :                 if (ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
     526             :                 {
     527             :                     /* deleted, so forget about it */
     528           6 :                     if (BufferIsValid(buffer))
     529           0 :                         ReleaseBuffer(buffer);
     530           6 :                     return TM_Deleted;
     531             :                 }
     532             : 
     533             :                 /* updated, so look at the updated row */
     534          48 :                 *tid = tuple->t_data->t_ctid;
     535             :                 /* updated row should have xmin matching this xmax */
     536          48 :                 priorXmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
     537          48 :                 if (BufferIsValid(buffer))
     538           0 :                     ReleaseBuffer(buffer);
     539             :                 /* loop back to fetch next in chain */
     540             :             }
     541             :         }
     542             :         else
     543             :         {
     544             :             /* tuple was deleted, so give up */
     545           0 :             return TM_Deleted;
     546             :         }
     547             :     }
     548             : 
     549       18388 :     slot->tts_tableOid = RelationGetRelid(relation);
     550       18388 :     tuple->t_tableOid = slot->tts_tableOid;
     551             : 
     552             :     /* store in slot, transferring existing pin */
     553       18388 :     ExecStorePinnedBufferHeapTuple(tuple, slot, buffer);
     554             : 
     555       18388 :     return result;
     556             : }
     557             : 
     558             : 
     559             : /* ------------------------------------------------------------------------
     560             :  * DDL related callbacks for heap AM.
     561             :  * ------------------------------------------------------------------------
     562             :  */
     563             : 
     564             : static void
     565       65154 : heapam_relation_set_new_filenode(Relation rel,
     566             :                                  const RelFileNode *newrnode,
     567             :                                  char persistence,
     568             :                                  TransactionId *freezeXid,
     569             :                                  MultiXactId *minmulti)
     570             : {
     571             :     SMgrRelation srel;
     572             : 
     573             :     /*
     574             :      * Initialize to the minimum XID that could put tuples in the table. We
     575             :      * know that no xacts older than RecentXmin are still running, so that
     576             :      * will do.
     577             :      */
     578       65154 :     *freezeXid = RecentXmin;
     579             : 
     580             :     /*
     581             :      * Similarly, initialize the minimum Multixact to the first value that
     582             :      * could possibly be stored in tuples in the table.  Running transactions
     583             :      * could reuse values from their local cache, so we are careful to
     584             :      * consider all currently running multis.
     585             :      *
     586             :      * XXX this could be refined further, but is it worth the hassle?
     587             :      */
     588       65154 :     *minmulti = GetOldestMultiXactId();
     589             : 
     590       65154 :     srel = RelationCreateStorage(*newrnode, persistence);
     591             : 
     592             :     /*
     593             :      * If required, set up an init fork for an unlogged table so that it can
     594             :      * be correctly reinitialized on restart.  An immediate sync is required
     595             :      * even if the page has been logged, because the write did not go through
     596             :      * shared_buffers and therefore a concurrent checkpoint may have moved the
     597             :      * redo pointer past our xlog record.  Recovery may as well remove it
     598             :      * while replaying, for example, XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE
     599             :      * record. Therefore, logging is necessary even if wal_level=minimal.
     600             :      */
     601       65154 :     if (persistence == RELPERSISTENCE_UNLOGGED)
     602             :     {
     603             :         Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
     604             :                rel->rd_rel->relkind == RELKIND_MATVIEW ||
     605             :                rel->rd_rel->relkind == RELKIND_TOASTVALUE);
     606         126 :         smgrcreate(srel, INIT_FORKNUM, false);
     607         126 :         log_smgrcreate(newrnode, INIT_FORKNUM);
     608         126 :         smgrimmedsync(srel, INIT_FORKNUM);
     609             :     }
     610             : 
     611       65154 :     smgrclose(srel);
     612       65154 : }
     613             : 
     614             : static void
     615         396 : heapam_relation_nontransactional_truncate(Relation rel)
     616             : {
     617         396 :     RelationTruncate(rel, 0);
     618         396 : }
     619             : 
     620             : static void
     621          22 : heapam_relation_copy_data(Relation rel, const RelFileNode *newrnode)
     622             : {
     623             :     SMgrRelation dstrel;
     624             : 
     625          22 :     dstrel = smgropen(*newrnode, rel->rd_backend);
     626          22 :     RelationOpenSmgr(rel);
     627             : 
     628             :     /*
     629             :      * Since we copy the file directly without looking at the shared buffers,
     630             :      * we'd better first flush out any pages of the source relation that are
     631             :      * in shared buffers.  We assume no new changes will be made while we are
     632             :      * holding exclusive lock on the rel.
     633             :      */
     634          22 :     FlushRelationBuffers(rel);
     635             : 
     636             :     /*
     637             :      * Create and copy all forks of the relation, and schedule unlinking of
     638             :      * old physical files.
     639             :      *
     640             :      * NOTE: any conflict in relfilenode value will be caught in
     641             :      * RelationCreateStorage().
     642             :      */
     643          22 :     RelationCreateStorage(*newrnode, rel->rd_rel->relpersistence);
     644             : 
     645             :     /* copy main fork */
     646          22 :     RelationCopyStorage(rel->rd_smgr, dstrel, MAIN_FORKNUM,
     647          22 :                         rel->rd_rel->relpersistence);
     648             : 
     649             :     /* copy those extra forks that exist */
     650          88 :     for (ForkNumber forkNum = MAIN_FORKNUM + 1;
     651          66 :          forkNum <= MAX_FORKNUM; forkNum++)
     652             :     {
     653          66 :         if (smgrexists(rel->rd_smgr, forkNum))
     654             :         {
     655           0 :             smgrcreate(dstrel, forkNum, false);
     656             : 
     657             :             /*
     658             :              * WAL log creation if the relation is persistent, or this is the
     659             :              * init fork of an unlogged relation.
     660             :              */
     661           0 :             if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT ||
     662           0 :                 (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
     663             :                  forkNum == INIT_FORKNUM))
     664           0 :                 log_smgrcreate(newrnode, forkNum);
     665           0 :             RelationCopyStorage(rel->rd_smgr, dstrel, forkNum,
     666           0 :                                 rel->rd_rel->relpersistence);
     667             :         }
     668             :     }
     669             : 
     670             : 
     671             :     /* drop old relation, and close new one */
     672          22 :     RelationDropStorage(rel);
     673          22 :     smgrclose(dstrel);
     674          22 : }
     675             : 
     676             : static void
     677         326 : heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap,
     678             :                                  Relation OldIndex, bool use_sort,
     679             :                                  TransactionId OldestXmin,
     680             :                                  TransactionId *xid_cutoff,
     681             :                                  MultiXactId *multi_cutoff,
     682             :                                  double *num_tuples,
     683             :                                  double *tups_vacuumed,
     684             :                                  double *tups_recently_dead)
     685             : {
     686             :     RewriteState rwstate;
     687             :     IndexScanDesc indexScan;
     688             :     TableScanDesc tableScan;
     689             :     HeapScanDesc heapScan;
     690             :     bool        is_system_catalog;
     691             :     Tuplesortstate *tuplesort;
     692         326 :     TupleDesc   oldTupDesc = RelationGetDescr(OldHeap);
     693         326 :     TupleDesc   newTupDesc = RelationGetDescr(NewHeap);
     694             :     TupleTableSlot *slot;
     695             :     int         natts;
     696             :     Datum      *values;
     697             :     bool       *isnull;
     698             :     BufferHeapTupleTableSlot *hslot;
     699             : 
     700             :     /* Remember if it's a system catalog */
     701         326 :     is_system_catalog = IsSystemRelation(OldHeap);
     702             : 
     703             :     /*
     704             :      * Valid smgr_targblock implies something already wrote to the relation.
     705             :      * This may be harmless, but this function hasn't planned for it.
     706             :      */
     707             :     Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber);
     708             : 
     709             :     /* Preallocate values/isnull arrays */
     710         326 :     natts = newTupDesc->natts;
     711         326 :     values = (Datum *) palloc(natts * sizeof(Datum));
     712         326 :     isnull = (bool *) palloc(natts * sizeof(bool));
     713             : 
     714             :     /* Initialize the rewrite operation */
     715         326 :     rwstate = begin_heap_rewrite(OldHeap, NewHeap, OldestXmin, *xid_cutoff,
     716             :                                  *multi_cutoff);
     717             : 
     718             : 
     719             :     /* Set up sorting if wanted */
     720         326 :     if (use_sort)
     721          50 :         tuplesort = tuplesort_begin_cluster(oldTupDesc, OldIndex,
     722             :                                             maintenance_work_mem,
     723             :                                             NULL, false);
     724             :     else
     725         276 :         tuplesort = NULL;
     726             : 
     727             :     /*
     728             :      * Prepare to scan the OldHeap.  To ensure we see recently-dead tuples
     729             :      * that still need to be copied, we scan with SnapshotAny and use
     730             :      * HeapTupleSatisfiesVacuum for the visibility test.
     731             :      */
     732         326 :     if (OldIndex != NULL && !use_sort)
     733          24 :     {
     734          24 :         const int   ci_index[] = {
     735             :             PROGRESS_CLUSTER_PHASE,
     736             :             PROGRESS_CLUSTER_INDEX_RELID
     737             :         };
     738             :         int64       ci_val[2];
     739             : 
     740             :         /* Set phase and OIDOldIndex to columns */
     741          24 :         ci_val[0] = PROGRESS_CLUSTER_PHASE_INDEX_SCAN_HEAP;
     742          24 :         ci_val[1] = RelationGetRelid(OldIndex);
     743          24 :         pgstat_progress_update_multi_param(2, ci_index, ci_val);
     744             : 
     745          24 :         tableScan = NULL;
     746          24 :         heapScan = NULL;
     747          24 :         indexScan = index_beginscan(OldHeap, OldIndex, SnapshotAny, 0, 0);
     748          24 :         index_rescan(indexScan, NULL, 0, NULL, 0);
     749             :     }
     750             :     else
     751             :     {
     752             :         /* In scan-and-sort mode and also VACUUM FULL, set phase */
     753         302 :         pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
     754             :                                      PROGRESS_CLUSTER_PHASE_SEQ_SCAN_HEAP);
     755             : 
     756         302 :         tableScan = table_beginscan(OldHeap, SnapshotAny, 0, (ScanKey) NULL);
     757         302 :         heapScan = (HeapScanDesc) tableScan;
     758         302 :         indexScan = NULL;
     759             : 
     760             :         /* Set total heap blocks */
     761         302 :         pgstat_progress_update_param(PROGRESS_CLUSTER_TOTAL_HEAP_BLKS,
     762         302 :                                      heapScan->rs_nblocks);
     763             :     }
     764             : 
     765         326 :     slot = table_slot_create(OldHeap, NULL);
     766         326 :     hslot = (BufferHeapTupleTableSlot *) slot;
     767             : 
     768             :     /*
     769             :      * Scan through the OldHeap, either in OldIndex order or sequentially;
     770             :      * copy each tuple into the NewHeap, or transiently to the tuplesort
     771             :      * module.  Note that we don't bother sorting dead tuples (they won't get
     772             :      * to the new table anyway).
     773             :      */
     774             :     for (;;)
     775      480512 :     {
     776             :         HeapTuple   tuple;
     777             :         Buffer      buf;
     778             :         bool        isdead;
     779             : 
     780      480838 :         CHECK_FOR_INTERRUPTS();
     781             : 
     782      480838 :         if (indexScan != NULL)
     783             :         {
     784          88 :             if (!index_getnext_slot(indexScan, ForwardScanDirection, slot))
     785          24 :                 break;
     786             : 
     787             :             /* Since we used no scan keys, should never need to recheck */
     788          64 :             if (indexScan->xs_recheck)
     789           0 :                 elog(ERROR, "CLUSTER does not support lossy index conditions");
     790             :         }
     791             :         else
     792             :         {
     793      480750 :             if (!table_scan_getnextslot(tableScan, ForwardScanDirection, slot))
     794         302 :                 break;
     795             : 
     796             :             /*
     797             :              * In scan-and-sort mode and also VACUUM FULL, set heap blocks
     798             :              * scanned
     799             :              */
     800      480448 :             pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_BLKS_SCANNED,
     801      480448 :                                          heapScan->rs_cblock + 1);
     802             :         }
     803             : 
     804      480512 :         tuple = ExecFetchSlotHeapTuple(slot, false, NULL);
     805      480512 :         buf = hslot->buffer;
     806             : 
     807      480512 :         LockBuffer(buf, BUFFER_LOCK_SHARE);
     808             : 
     809      480512 :         switch (HeapTupleSatisfiesVacuum(tuple, OldestXmin, buf))
     810             :         {
     811       14822 :             case HEAPTUPLE_DEAD:
     812             :                 /* Definitely dead */
     813       14822 :                 isdead = true;
     814       14822 :                 break;
     815        5026 :             case HEAPTUPLE_RECENTLY_DEAD:
     816        5026 :                 *tups_recently_dead += 1;
     817             :                 /* fall through */
     818      465522 :             case HEAPTUPLE_LIVE:
     819             :                 /* Live or recently dead, must copy it */
     820      465522 :                 isdead = false;
     821      465522 :                 break;
     822         128 :             case HEAPTUPLE_INSERT_IN_PROGRESS:
     823             : 
     824             :                 /*
     825             :                  * Since we hold exclusive lock on the relation, normally the
     826             :                  * only way to see this is if it was inserted earlier in our
     827             :                  * own transaction.  However, it can happen in system
     828             :                  * catalogs, since we tend to release write lock before commit
     829             :                  * there.  Give a warning if neither case applies; but in any
     830             :                  * case we had better copy it.
     831             :                  */
     832         128 :                 if (!is_system_catalog &&
     833          12 :                     !TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(tuple->t_data)))
     834           0 :                     elog(WARNING, "concurrent insert in progress within table \"%s\"",
     835             :                          RelationGetRelationName(OldHeap));
     836             :                 /* treat as live */
     837         128 :                 isdead = false;
     838         128 :                 break;
     839          40 :             case HEAPTUPLE_DELETE_IN_PROGRESS:
     840             : 
     841             :                 /*
     842             :                  * Similar situation to INSERT_IN_PROGRESS case.
     843             :                  */
     844          40 :                 if (!is_system_catalog &&
     845          20 :                     !TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetUpdateXid(tuple->t_data)))
     846           0 :                     elog(WARNING, "concurrent delete in progress within table \"%s\"",
     847             :                          RelationGetRelationName(OldHeap));
     848             :                 /* treat as recently dead */
     849          40 :                 *tups_recently_dead += 1;
     850          40 :                 isdead = false;
     851          40 :                 break;
     852           0 :             default:
     853           0 :                 elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
     854             :                 isdead = false; /* keep compiler quiet */
     855             :                 break;
     856             :         }
     857             : 
     858      480512 :         LockBuffer(buf, BUFFER_LOCK_UNLOCK);
     859             : 
     860      480512 :         if (isdead)
     861             :         {
     862       14822 :             *tups_vacuumed += 1;
     863             :             /* heap rewrite module still needs to see it... */
     864       14822 :             if (rewrite_heap_dead_tuple(rwstate, tuple))
     865             :             {
     866             :                 /* A previous recently-dead tuple is now known dead */
     867           0 :                 *tups_vacuumed += 1;
     868           0 :                 *tups_recently_dead -= 1;
     869             :             }
     870       14822 :             continue;
     871             :         }
     872             : 
     873      465690 :         *num_tuples += 1;
     874      465690 :         if (tuplesort != NULL)
     875             :         {
     876      366294 :             tuplesort_putheaptuple(tuplesort, tuple);
     877             : 
     878             :             /*
     879             :              * In scan-and-sort mode, report increase in number of tuples
     880             :              * scanned
     881             :              */
     882      366294 :             pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED,
     883      366294 :                                          *num_tuples);
     884             :         }
     885             :         else
     886             :         {
     887       99396 :             const int   ct_index[] = {
     888             :                 PROGRESS_CLUSTER_HEAP_TUPLES_SCANNED,
     889             :                 PROGRESS_CLUSTER_HEAP_TUPLES_WRITTEN
     890             :             };
     891             :             int64       ct_val[2];
     892             : 
     893       99396 :             reform_and_rewrite_tuple(tuple, OldHeap, NewHeap,
     894             :                                      values, isnull, rwstate);
     895             : 
     896             :             /*
     897             :              * In indexscan mode and also VACUUM FULL, report increase in
     898             :              * number of tuples scanned and written
     899             :              */
     900       99396 :             ct_val[0] = *num_tuples;
     901       99396 :             ct_val[1] = *num_tuples;
     902       99396 :             pgstat_progress_update_multi_param(2, ct_index, ct_val);
     903             :         }
     904             :     }
     905             : 
     906         326 :     if (indexScan != NULL)
     907          24 :         index_endscan(indexScan);
     908         326 :     if (tableScan != NULL)
     909         302 :         table_endscan(tableScan);
     910         326 :     if (slot)
     911         326 :         ExecDropSingleTupleTableSlot(slot);
     912             : 
     913             :     /*
     914             :      * In scan-and-sort mode, complete the sort, then read out all live tuples
     915             :      * from the tuplestore and write them to the new relation.
     916             :      */
     917         326 :     if (tuplesort != NULL)
     918             :     {
     919          50 :         double      n_tuples = 0;
     920             : 
     921             :         /* Report that we are now sorting tuples */
     922          50 :         pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
     923             :                                      PROGRESS_CLUSTER_PHASE_SORT_TUPLES);
     924             : 
     925          50 :         tuplesort_performsort(tuplesort);
     926             : 
     927             :         /* Report that we are now writing new heap */
     928          50 :         pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
     929             :                                      PROGRESS_CLUSTER_PHASE_WRITE_NEW_HEAP);
     930             : 
     931             :         for (;;)
     932      366294 :         {
     933             :             HeapTuple   tuple;
     934             : 
     935      366344 :             CHECK_FOR_INTERRUPTS();
     936             : 
     937      366344 :             tuple = tuplesort_getheaptuple(tuplesort, true);
     938      366344 :             if (tuple == NULL)
     939          50 :                 break;
     940             : 
     941      366294 :             n_tuples += 1;
     942      366294 :             reform_and_rewrite_tuple(tuple,
     943             :                                      OldHeap, NewHeap,
     944             :                                      values, isnull,
     945             :                                      rwstate);
     946             :             /* Report n_tuples */
     947      366294 :             pgstat_progress_update_param(PROGRESS_CLUSTER_HEAP_TUPLES_WRITTEN,
     948             :                                          n_tuples);
     949             :         }
     950             : 
     951          50 :         tuplesort_end(tuplesort);
     952             :     }
     953             : 
     954             :     /* Write out any remaining tuples, and fsync if needed */
     955         326 :     end_heap_rewrite(rwstate);
     956             : 
     957             :     /* Clean up */
     958         326 :     pfree(values);
     959         326 :     pfree(isnull);
     960         326 : }
     961             : 
     962             : static bool
     963      222888 : heapam_scan_analyze_next_block(TableScanDesc scan, BlockNumber blockno,
     964             :                                BufferAccessStrategy bstrategy)
     965             : {
     966      222888 :     HeapScanDesc hscan = (HeapScanDesc) scan;
     967             : 
     968             :     /*
     969             :      * We must maintain a pin on the target page's buffer to ensure that
     970             :      * concurrent activity - e.g. HOT pruning - doesn't delete tuples out from
     971             :      * under us.  Hence, pin the page until we are done looking at it.  We
     972             :      * also choose to hold sharelock on the buffer throughout --- we could
     973             :      * release and re-acquire sharelock for each tuple, but since we aren't
     974             :      * doing much work per tuple, the extra lock traffic is probably better
     975             :      * avoided.
     976             :      */
     977      222888 :     hscan->rs_cblock = blockno;
     978      222888 :     hscan->rs_cindex = FirstOffsetNumber;
     979      222888 :     hscan->rs_cbuf = ReadBufferExtended(scan->rs_rd, MAIN_FORKNUM,
     980             :                                         blockno, RBM_NORMAL, bstrategy);
     981      222888 :     LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
     982             : 
     983             :     /* in heap all blocks can contain tuples, so always return true */
     984      222888 :     return true;
     985             : }
     986             : 
     987             : static bool
     988    16648846 : heapam_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin,
     989             :                                double *liverows, double *deadrows,
     990             :                                TupleTableSlot *slot)
     991             : {
     992    16648846 :     HeapScanDesc hscan = (HeapScanDesc) scan;
     993             :     Page        targpage;
     994             :     OffsetNumber maxoffset;
     995             :     BufferHeapTupleTableSlot *hslot;
     996             : 
     997             :     Assert(TTS_IS_BUFFERTUPLE(slot));
     998             : 
     999    16648846 :     hslot = (BufferHeapTupleTableSlot *) slot;
    1000    16648846 :     targpage = BufferGetPage(hscan->rs_cbuf);
    1001    16648846 :     maxoffset = PageGetMaxOffsetNumber(targpage);
    1002             : 
    1003             :     /* Inner loop over all tuples on the selected page */
    1004    18426912 :     for (; hscan->rs_cindex <= maxoffset; hscan->rs_cindex++)
    1005             :     {
    1006             :         ItemId      itemid;
    1007    18204024 :         HeapTuple   targtuple = &hslot->base.tupdata;
    1008    18204024 :         bool        sample_it = false;
    1009             : 
    1010    18204024 :         itemid = PageGetItemId(targpage, hscan->rs_cindex);
    1011             : 
    1012             :         /*
    1013             :          * We ignore unused and redirect line pointers.  DEAD line pointers
    1014             :          * should be counted as dead, because we need vacuum to run to get rid
    1015             :          * of them.  Note that this rule agrees with the way that
    1016             :          * heap_page_prune() counts things.
    1017             :          */
    1018    18204024 :         if (!ItemIdIsNormal(itemid))
    1019             :         {
    1020     1608110 :             if (ItemIdIsDead(itemid))
    1021      234754 :                 *deadrows += 1;
    1022     1608110 :             continue;
    1023             :         }
    1024             : 
    1025    16595914 :         ItemPointerSet(&targtuple->t_self, hscan->rs_cblock, hscan->rs_cindex);
    1026             : 
    1027    16595914 :         targtuple->t_tableOid = RelationGetRelid(scan->rs_rd);
    1028    16595914 :         targtuple->t_data = (HeapTupleHeader) PageGetItem(targpage, itemid);
    1029    16595914 :         targtuple->t_len = ItemIdGetLength(itemid);
    1030             : 
    1031    16595914 :         switch (HeapTupleSatisfiesVacuum(targtuple, OldestXmin,
    1032             :                                          hscan->rs_cbuf))
    1033             :         {
    1034    16237836 :             case HEAPTUPLE_LIVE:
    1035    16237836 :                 sample_it = true;
    1036    16237836 :                 *liverows += 1;
    1037    16237836 :                 break;
    1038             : 
    1039      168476 :             case HEAPTUPLE_DEAD:
    1040             :             case HEAPTUPLE_RECENTLY_DEAD:
    1041             :                 /* Count dead and recently-dead rows */
    1042      168476 :                 *deadrows += 1;
    1043      168476 :                 break;
    1044             : 
    1045      188444 :             case HEAPTUPLE_INSERT_IN_PROGRESS:
    1046             : 
    1047             :                 /*
    1048             :                  * Insert-in-progress rows are not counted.  We assume that
    1049             :                  * when the inserting transaction commits or aborts, it will
    1050             :                  * send a stats message to increment the proper count.  This
    1051             :                  * works right only if that transaction ends after we finish
    1052             :                  * analyzing the table; if things happen in the other order,
    1053             :                  * its stats update will be overwritten by ours.  However, the
    1054             :                  * error will be large only if the other transaction runs long
    1055             :                  * enough to insert many tuples, so assuming it will finish
    1056             :                  * after us is the safer option.
    1057             :                  *
    1058             :                  * A special case is that the inserting transaction might be
    1059             :                  * our own.  In this case we should count and sample the row,
    1060             :                  * to accommodate users who load a table and analyze it in one
    1061             :                  * transaction.  (pgstat_report_analyze has to adjust the
    1062             :                  * numbers we send to the stats collector to make this come
    1063             :                  * out right.)
    1064             :                  */
    1065      188444 :                 if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(targtuple->t_data)))
    1066             :                 {
    1067      188108 :                     sample_it = true;
    1068      188108 :                     *liverows += 1;
    1069             :                 }
    1070      188444 :                 break;
    1071             : 
    1072        1158 :             case HEAPTUPLE_DELETE_IN_PROGRESS:
    1073             : 
    1074             :                 /*
    1075             :                  * We count and sample delete-in-progress rows the same as
    1076             :                  * live ones, so that the stats counters come out right if the
    1077             :                  * deleting transaction commits after us, per the same
    1078             :                  * reasoning given above.
    1079             :                  *
    1080             :                  * If the delete was done by our own transaction, however, we
    1081             :                  * must count the row as dead to make pgstat_report_analyze's
    1082             :                  * stats adjustments come out right.  (Note: this works out
    1083             :                  * properly when the row was both inserted and deleted in our
    1084             :                  * xact.)
    1085             :                  *
    1086             :                  * The net effect of these choices is that we act as though an
    1087             :                  * IN_PROGRESS transaction hasn't happened yet, except if it
    1088             :                  * is our own transaction, which we assume has happened.
    1089             :                  *
    1090             :                  * This approach ensures that we behave sanely if we see both
    1091             :                  * the pre-image and post-image rows for a row being updated
    1092             :                  * by a concurrent transaction: we will sample the pre-image
    1093             :                  * but not the post-image.  We also get sane results if the
    1094             :                  * concurrent transaction never commits.
    1095             :                  */
    1096        1158 :                 if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetUpdateXid(targtuple->t_data)))
    1097        1144 :                     *deadrows += 1;
    1098             :                 else
    1099             :                 {
    1100          14 :                     sample_it = true;
    1101          14 :                     *liverows += 1;
    1102             :                 }
    1103        1158 :                 break;
    1104             : 
    1105           0 :             default:
    1106           0 :                 elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
    1107             :                 break;
    1108             :         }
    1109             : 
    1110    16595914 :         if (sample_it)
    1111             :         {
    1112    16425958 :             ExecStoreBufferHeapTuple(targtuple, slot, hscan->rs_cbuf);
    1113    16425958 :             hscan->rs_cindex++;
    1114             : 
    1115             :             /* note that we leave the buffer locked here! */
    1116    16425958 :             return true;
    1117             :         }
    1118             :     }
    1119             : 
    1120             :     /* Now release the lock and pin on the page */
    1121      222888 :     UnlockReleaseBuffer(hscan->rs_cbuf);
    1122      222888 :     hscan->rs_cbuf = InvalidBuffer;
    1123             : 
    1124             :     /* also prevent old slot contents from having pin on page */
    1125      222888 :     ExecClearTuple(slot);
    1126             : 
    1127      222888 :     return false;
    1128             : }
    1129             : 
    1130             : static double
    1131       77558 : heapam_index_build_range_scan(Relation heapRelation,
    1132             :                               Relation indexRelation,
    1133             :                               IndexInfo *indexInfo,
    1134             :                               bool allow_sync,
    1135             :                               bool anyvisible,
    1136             :                               bool progress,
    1137             :                               BlockNumber start_blockno,
    1138             :                               BlockNumber numblocks,
    1139             :                               IndexBuildCallback callback,
    1140             :                               void *callback_state,
    1141             :                               TableScanDesc scan)
    1142             : {
    1143             :     HeapScanDesc hscan;
    1144             :     bool        is_system_catalog;
    1145             :     bool        checking_uniqueness;
    1146             :     HeapTuple   heapTuple;
    1147             :     Datum       values[INDEX_MAX_KEYS];
    1148             :     bool        isnull[INDEX_MAX_KEYS];
    1149             :     double      reltuples;
    1150             :     ExprState  *predicate;
    1151             :     TupleTableSlot *slot;
    1152             :     EState     *estate;
    1153             :     ExprContext *econtext;
    1154             :     Snapshot    snapshot;
    1155       77558 :     bool        need_unregister_snapshot = false;
    1156             :     TransactionId OldestXmin;
    1157       77558 :     BlockNumber previous_blkno = InvalidBlockNumber;
    1158       77558 :     BlockNumber root_blkno = InvalidBlockNumber;
    1159             :     OffsetNumber root_offsets[MaxHeapTuplesPerPage];
    1160             : 
    1161             :     /*
    1162             :      * sanity checks
    1163             :      */
    1164             :     Assert(OidIsValid(indexRelation->rd_rel->relam));
    1165             : 
    1166             :     /* Remember if it's a system catalog */
    1167       77558 :     is_system_catalog = IsSystemRelation(heapRelation);
    1168             : 
    1169             :     /* See whether we're verifying uniqueness/exclusion properties */
    1170       86994 :     checking_uniqueness = (indexInfo->ii_Unique ||
    1171        9436 :                            indexInfo->ii_ExclusionOps != NULL);
    1172             : 
    1173             :     /*
    1174             :      * "Any visible" mode is not compatible with uniqueness checks; make sure
    1175             :      * only one of those is requested.
    1176             :      */
    1177             :     Assert(!(anyvisible && checking_uniqueness));
    1178             : 
    1179             :     /*
    1180             :      * Need an EState for evaluation of index expressions and partial-index
    1181             :      * predicates.  Also a slot to hold the current tuple.
    1182             :      */
    1183       77558 :     estate = CreateExecutorState();
    1184       77558 :     econtext = GetPerTupleExprContext(estate);
    1185       77558 :     slot = table_slot_create(heapRelation, NULL);
    1186             : 
    1187             :     /* Arrange for econtext's scan tuple to be the tuple under test */
    1188       77558 :     econtext->ecxt_scantuple = slot;
    1189             : 
    1190             :     /* Set up execution state for predicate, if any. */
    1191       77558 :     predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
    1192             : 
    1193             :     /*
    1194             :      * Prepare for scan of the base relation.  In a normal index build, we use
    1195             :      * SnapshotAny because we must retrieve all tuples and do our own time
    1196             :      * qual checks (because we have to index RECENTLY_DEAD tuples). In a
    1197             :      * concurrent build, or during bootstrap, we take a regular MVCC snapshot
    1198             :      * and index whatever's live according to that.
    1199             :      */
    1200       77558 :     OldestXmin = InvalidTransactionId;
    1201             : 
    1202             :     /* okay to ignore lazy VACUUMs here */
    1203       77558 :     if (!IsBootstrapProcessingMode() && !indexInfo->ii_Concurrent)
    1204       23676 :         OldestXmin = GetOldestXmin(heapRelation, PROCARRAY_FLAGS_VACUUM);
    1205             : 
    1206       77558 :     if (!scan)
    1207             :     {
    1208             :         /*
    1209             :          * Serial index build.
    1210             :          *
    1211             :          * Must begin our own heap scan in this case.  We may also need to
    1212             :          * register a snapshot whose lifetime is under our direct control.
    1213             :          */
    1214       77368 :         if (!TransactionIdIsValid(OldestXmin))
    1215             :         {
    1216       53876 :             snapshot = RegisterSnapshot(GetTransactionSnapshot());
    1217       53876 :             need_unregister_snapshot = true;
    1218             :         }
    1219             :         else
    1220       23492 :             snapshot = SnapshotAny;
    1221             : 
    1222       77368 :         scan = table_beginscan_strat(heapRelation,  /* relation */
    1223             :                                      snapshot,  /* snapshot */
    1224             :                                      0, /* number of keys */
    1225             :                                      NULL,  /* scan key */
    1226             :                                      true,  /* buffer access strategy OK */
    1227             :                                      allow_sync);   /* syncscan OK? */
    1228             :     }
    1229             :     else
    1230             :     {
    1231             :         /*
    1232             :          * Parallel index build.
    1233             :          *
    1234             :          * Parallel case never registers/unregisters own snapshot.  Snapshot
    1235             :          * is taken from parallel heap scan, and is SnapshotAny or an MVCC
    1236             :          * snapshot, based on same criteria as serial case.
    1237             :          */
    1238             :         Assert(!IsBootstrapProcessingMode());
    1239             :         Assert(allow_sync);
    1240         190 :         snapshot = scan->rs_snapshot;
    1241             :     }
    1242             : 
    1243       77558 :     hscan = (HeapScanDesc) scan;
    1244             : 
    1245             :     /* Publish number of blocks to scan */
    1246       77558 :     if (progress)
    1247             :     {
    1248             :         BlockNumber nblocks;
    1249             : 
    1250       77414 :         if (hscan->rs_base.rs_parallel != NULL)
    1251             :         {
    1252             :             ParallelBlockTableScanDesc pbscan;
    1253             : 
    1254          88 :             pbscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
    1255          88 :             nblocks = pbscan->phs_nblocks;
    1256             :         }
    1257             :         else
    1258       77326 :             nblocks = hscan->rs_nblocks;
    1259             : 
    1260       77414 :         pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_TOTAL,
    1261             :                                      nblocks);
    1262             :     }
    1263             : 
    1264             :     /*
    1265             :      * Must call GetOldestXmin() with SnapshotAny.  Should never call
    1266             :      * GetOldestXmin() with MVCC snapshot. (It's especially worth checking
    1267             :      * this for parallel builds, since ambuild routines that support parallel
    1268             :      * builds must work these details out for themselves.)
    1269             :      */
    1270             :     Assert(snapshot == SnapshotAny || IsMVCCSnapshot(snapshot));
    1271             :     Assert(snapshot == SnapshotAny ? TransactionIdIsValid(OldestXmin) :
    1272             :            !TransactionIdIsValid(OldestXmin));
    1273             :     Assert(snapshot == SnapshotAny || !anyvisible);
    1274             : 
    1275             :     /* set our scan endpoints */
    1276       77558 :     if (!allow_sync)
    1277         292 :         heap_setscanlimits(scan, start_blockno, numblocks);
    1278             :     else
    1279             :     {
    1280             :         /* syncscan can only be requested on whole relation */
    1281             :         Assert(start_blockno == 0);
    1282             :         Assert(numblocks == InvalidBlockNumber);
    1283             :     }
    1284             : 
    1285       77558 :     reltuples = 0;
    1286             : 
    1287             :     /*
    1288             :      * Scan all tuples in the base relation.
    1289             :      */
    1290    18953908 :     while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
    1291             :     {
    1292             :         bool        tupleIsAlive;
    1293             : 
    1294    18876354 :         CHECK_FOR_INTERRUPTS();
    1295             : 
    1296             :         /* Report scan progress, if asked to. */
    1297    18876354 :         if (progress)
    1298             :         {
    1299    16816388 :             BlockNumber blocks_done = heapam_scan_get_blocks_done(hscan);
    1300             : 
    1301    16816388 :             if (blocks_done != previous_blkno)
    1302             :             {
    1303      250208 :                 pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
    1304             :                                              blocks_done);
    1305      250208 :                 previous_blkno = blocks_done;
    1306             :             }
    1307             :         }
    1308             : 
    1309             :         /*
    1310             :          * When dealing with a HOT-chain of updated tuples, we want to index
    1311             :          * the values of the live tuple (if any), but index it under the TID
    1312             :          * of the chain's root tuple.  This approach is necessary to preserve
    1313             :          * the HOT-chain structure in the heap. So we need to be able to find
    1314             :          * the root item offset for every tuple that's in a HOT-chain.  When
    1315             :          * first reaching a new page of the relation, call
    1316             :          * heap_get_root_tuples() to build a map of root item offsets on the
    1317             :          * page.
    1318             :          *
    1319             :          * It might look unsafe to use this information across buffer
    1320             :          * lock/unlock.  However, we hold ShareLock on the table so no
    1321             :          * ordinary insert/update/delete should occur; and we hold pin on the
    1322             :          * buffer continuously while visiting the page, so no pruning
    1323             :          * operation can occur either.
    1324             :          *
    1325             :          * Also, although our opinions about tuple liveness could change while
    1326             :          * we scan the page (due to concurrent transaction commits/aborts),
    1327             :          * the chain root locations won't, so this info doesn't need to be
    1328             :          * rebuilt after waiting for another transaction.
    1329             :          *
    1330             :          * Note the implied assumption that there is no more than one live
    1331             :          * tuple per HOT-chain --- else we could create more than one index
    1332             :          * entry pointing to the same root tuple.
    1333             :          */
    1334    18876354 :         if (hscan->rs_cblock != root_blkno)
    1335             :         {
    1336      266544 :             Page        page = BufferGetPage(hscan->rs_cbuf);
    1337             : 
    1338      266544 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
    1339      266544 :             heap_get_root_tuples(page, root_offsets);
    1340      266544 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    1341             : 
    1342      266544 :             root_blkno = hscan->rs_cblock;
    1343             :         }
    1344             : 
    1345    18876354 :         if (snapshot == SnapshotAny)
    1346             :         {
    1347             :             /* do our own time qual check */
    1348             :             bool        indexIt;
    1349             :             TransactionId xwait;
    1350             : 
    1351    11449810 :     recheck:
    1352             : 
    1353             :             /*
    1354             :              * We could possibly get away with not locking the buffer here,
    1355             :              * since caller should hold ShareLock on the relation, but let's
    1356             :              * be conservative about it.  (This remark is still correct even
    1357             :              * with HOT-pruning: our pin on the buffer prevents pruning.)
    1358             :              */
    1359    11449810 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
    1360             : 
    1361             :             /*
    1362             :              * The criteria for counting a tuple as live in this block need to
    1363             :              * match what analyze.c's heapam_scan_analyze_next_tuple() does,
    1364             :              * otherwise CREATE INDEX and ANALYZE may produce wildly different
    1365             :              * reltuples values, e.g. when there are many recently-dead
    1366             :              * tuples.
    1367             :              */
    1368    11449810 :             switch (HeapTupleSatisfiesVacuum(heapTuple, OldestXmin,
    1369             :                                              hscan->rs_cbuf))
    1370             :             {
    1371        3240 :                 case HEAPTUPLE_DEAD:
    1372             :                     /* Definitely dead, we can ignore it */
    1373        3240 :                     indexIt = false;
    1374        3240 :                     tupleIsAlive = false;
    1375        3240 :                     break;
    1376     8067526 :                 case HEAPTUPLE_LIVE:
    1377             :                     /* Normal case, index and unique-check it */
    1378     8067526 :                     indexIt = true;
    1379     8067526 :                     tupleIsAlive = true;
    1380             :                     /* Count it as live, too */
    1381     8067526 :                     reltuples += 1;
    1382     8067526 :                     break;
    1383      133558 :                 case HEAPTUPLE_RECENTLY_DEAD:
    1384             : 
    1385             :                     /*
    1386             :                      * If tuple is recently deleted then we must index it
    1387             :                      * anyway to preserve MVCC semantics.  (Pre-existing
    1388             :                      * transactions could try to use the index after we finish
    1389             :                      * building it, and may need to see such tuples.)
    1390             :                      *
    1391             :                      * However, if it was HOT-updated then we must only index
    1392             :                      * the live tuple at the end of the HOT-chain.  Since this
    1393             :                      * breaks semantics for pre-existing snapshots, mark the
    1394             :                      * index as unusable for them.
    1395             :                      *
    1396             :                      * We don't count recently-dead tuples in reltuples, even
    1397             :                      * if we index them; see heapam_scan_analyze_next_tuple().
    1398             :                      */
    1399      133558 :                     if (HeapTupleIsHotUpdated(heapTuple))
    1400             :                     {
    1401         100 :                         indexIt = false;
    1402             :                         /* mark the index as unsafe for old snapshots */
    1403         100 :                         indexInfo->ii_BrokenHotChain = true;
    1404             :                     }
    1405             :                     else
    1406      133458 :                         indexIt = true;
    1407             :                     /* In any case, exclude the tuple from unique-checking */
    1408      133558 :                     tupleIsAlive = false;
    1409      133558 :                     break;
    1410     3245406 :                 case HEAPTUPLE_INSERT_IN_PROGRESS:
    1411             : 
    1412             :                     /*
    1413             :                      * In "anyvisible" mode, this tuple is visible and we
    1414             :                      * don't need any further checks.
    1415             :                      */
    1416     3245406 :                     if (anyvisible)
    1417             :                     {
    1418           4 :                         indexIt = true;
    1419           4 :                         tupleIsAlive = true;
    1420           4 :                         reltuples += 1;
    1421           4 :                         break;
    1422             :                     }
    1423             : 
    1424             :                     /*
    1425             :                      * Since caller should hold ShareLock or better, normally
    1426             :                      * the only way to see this is if it was inserted earlier
    1427             :                      * in our own transaction.  However, it can happen in
    1428             :                      * system catalogs, since we tend to release write lock
    1429             :                      * before commit there.  Give a warning if neither case
    1430             :                      * applies.
    1431             :                      */
    1432     3245402 :                     xwait = HeapTupleHeaderGetXmin(heapTuple->t_data);
    1433     3245402 :                     if (!TransactionIdIsCurrentTransactionId(xwait))
    1434             :                     {
    1435          36 :                         if (!is_system_catalog)
    1436           0 :                             elog(WARNING, "concurrent insert in progress within table \"%s\"",
    1437             :                                  RelationGetRelationName(heapRelation));
    1438             : 
    1439             :                         /*
    1440             :                          * If we are performing uniqueness checks, indexing
    1441             :                          * such a tuple could lead to a bogus uniqueness
    1442             :                          * failure.  In that case we wait for the inserting
    1443             :                          * transaction to finish and check again.
    1444             :                          */
    1445          36 :                         if (checking_uniqueness)
    1446             :                         {
    1447             :                             /*
    1448             :                              * Must drop the lock on the buffer before we wait
    1449             :                              */
    1450           0 :                             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    1451           0 :                             XactLockTableWait(xwait, heapRelation,
    1452             :                                               &heapTuple->t_self,
    1453             :                                               XLTW_InsertIndexUnique);
    1454           0 :                             CHECK_FOR_INTERRUPTS();
    1455           0 :                             goto recheck;
    1456             :                         }
    1457             :                     }
    1458             :                     else
    1459             :                     {
    1460             :                         /*
    1461             :                          * For consistency with
    1462             :                          * heapam_scan_analyze_next_tuple(), count
    1463             :                          * HEAPTUPLE_INSERT_IN_PROGRESS tuples as live only
    1464             :                          * when inserted by our own transaction.
    1465             :                          */
    1466     3245366 :                         reltuples += 1;
    1467             :                     }
    1468             : 
    1469             :                     /*
    1470             :                      * We must index such tuples, since if the index build
    1471             :                      * commits then they're good.
    1472             :                      */
    1473     3245402 :                     indexIt = true;
    1474     3245402 :                     tupleIsAlive = true;
    1475     3245402 :                     break;
    1476          80 :                 case HEAPTUPLE_DELETE_IN_PROGRESS:
    1477             : 
    1478             :                     /*
    1479             :                      * As with INSERT_IN_PROGRESS case, this is unexpected
    1480             :                      * unless it's our own deletion or a system catalog; but
    1481             :                      * in anyvisible mode, this tuple is visible.
    1482             :                      */
    1483          80 :                     if (anyvisible)
    1484             :                     {
    1485           0 :                         indexIt = true;
    1486           0 :                         tupleIsAlive = false;
    1487           0 :                         reltuples += 1;
    1488           0 :                         break;
    1489             :                     }
    1490             : 
    1491          80 :                     xwait = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
    1492          80 :                     if (!TransactionIdIsCurrentTransactionId(xwait))
    1493             :                     {
    1494          12 :                         if (!is_system_catalog)
    1495           0 :                             elog(WARNING, "concurrent delete in progress within table \"%s\"",
    1496             :                                  RelationGetRelationName(heapRelation));
    1497             : 
    1498             :                         /*
    1499             :                          * If we are performing uniqueness checks, assuming
    1500             :                          * the tuple is dead could lead to missing a
    1501             :                          * uniqueness violation.  In that case we wait for the
    1502             :                          * deleting transaction to finish and check again.
    1503             :                          *
    1504             :                          * Also, if it's a HOT-updated tuple, we should not
    1505             :                          * index it but rather the live tuple at the end of
    1506             :                          * the HOT-chain.  However, the deleting transaction
    1507             :                          * could abort, possibly leaving this tuple as live
    1508             :                          * after all, in which case it has to be indexed. The
    1509             :                          * only way to know what to do is to wait for the
    1510             :                          * deleting transaction to finish and check again.
    1511             :                          */
    1512          12 :                         if (checking_uniqueness ||
    1513          12 :                             HeapTupleIsHotUpdated(heapTuple))
    1514             :                         {
    1515             :                             /*
    1516             :                              * Must drop the lock on the buffer before we wait
    1517             :                              */
    1518           0 :                             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    1519           0 :                             XactLockTableWait(xwait, heapRelation,
    1520             :                                               &heapTuple->t_self,
    1521             :                                               XLTW_InsertIndexUnique);
    1522           0 :                             CHECK_FOR_INTERRUPTS();
    1523           0 :                             goto recheck;
    1524             :                         }
    1525             : 
    1526             :                         /*
    1527             :                          * Otherwise index it but don't check for uniqueness,
    1528             :                          * the same as a RECENTLY_DEAD tuple.
    1529             :                          */
    1530          12 :                         indexIt = true;
    1531             : 
    1532             :                         /*
    1533             :                          * Count HEAPTUPLE_DELETE_IN_PROGRESS tuples as live,
    1534             :                          * if they were not deleted by the current
    1535             :                          * transaction.  That's what
    1536             :                          * heapam_scan_analyze_next_tuple() does, and we want
    1537             :                          * the behavior to be consistent.
    1538             :                          */
    1539          12 :                         reltuples += 1;
    1540             :                     }
    1541          68 :                     else if (HeapTupleIsHotUpdated(heapTuple))
    1542             :                     {
    1543             :                         /*
    1544             :                          * It's a HOT-updated tuple deleted by our own xact.
    1545             :                          * We can assume the deletion will commit (else the
    1546             :                          * index contents don't matter), so treat the same as
    1547             :                          * RECENTLY_DEAD HOT-updated tuples.
    1548             :                          */
    1549           0 :                         indexIt = false;
    1550             :                         /* mark the index as unsafe for old snapshots */
    1551           0 :                         indexInfo->ii_BrokenHotChain = true;
    1552             :                     }
    1553             :                     else
    1554             :                     {
    1555             :                         /*
    1556             :                          * It's a regular tuple deleted by our own xact. Index
    1557             :                          * it, but don't check for uniqueness nor count in
    1558             :                          * reltuples, the same as a RECENTLY_DEAD tuple.
    1559             :                          */
    1560          68 :                         indexIt = true;
    1561             :                     }
    1562             :                     /* In any case, exclude the tuple from unique-checking */
    1563          80 :                     tupleIsAlive = false;
    1564          80 :                     break;
    1565           0 :                 default:
    1566           0 :                     elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
    1567             :                     indexIt = tupleIsAlive = false; /* keep compiler quiet */
    1568             :                     break;
    1569             :             }
    1570             : 
    1571    11449810 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    1572             : 
    1573    11449810 :             if (!indexIt)
    1574        3340 :                 continue;
    1575             :         }
    1576             :         else
    1577             :         {
    1578             :             /* heap_getnext did the time qual check */
    1579     7426544 :             tupleIsAlive = true;
    1580     7426544 :             reltuples += 1;
    1581             :         }
    1582             : 
    1583    18873014 :         MemoryContextReset(econtext->ecxt_per_tuple_memory);
    1584             : 
    1585             :         /* Set up for predicate or expression evaluation */
    1586    18873014 :         ExecStoreBufferHeapTuple(heapTuple, slot, hscan->rs_cbuf);
    1587             : 
    1588             :         /*
    1589             :          * In a partial index, discard tuples that don't satisfy the
    1590             :          * predicate.
    1591             :          */
    1592    18873014 :         if (predicate != NULL)
    1593             :         {
    1594       52248 :             if (!ExecQual(predicate, econtext))
    1595       15712 :                 continue;
    1596             :         }
    1597             : 
    1598             :         /*
    1599             :          * For the current heap tuple, extract all the attributes we use in
    1600             :          * this index, and note which are null.  This also performs evaluation
    1601             :          * of any expressions needed.
    1602             :          */
    1603    18857302 :         FormIndexDatum(indexInfo,
    1604             :                        slot,
    1605             :                        estate,
    1606             :                        values,
    1607             :                        isnull);
    1608             : 
    1609             :         /*
    1610             :          * You'd think we should go ahead and build the index tuple here, but
    1611             :          * some index AMs want to do further processing on the data first.  So
    1612             :          * pass the values[] and isnull[] arrays, instead.
    1613             :          */
    1614             : 
    1615    18857298 :         if (HeapTupleIsHeapOnly(heapTuple))
    1616             :         {
    1617             :             /*
    1618             :              * For a heap-only tuple, pretend its TID is that of the root. See
    1619             :              * src/backend/access/heap/README.HOT for discussion.
    1620             :              */
    1621             :             ItemPointerData tid;
    1622             :             OffsetNumber offnum;
    1623             : 
    1624       12228 :             offnum = ItemPointerGetOffsetNumber(&heapTuple->t_self);
    1625             : 
    1626       12228 :             if (!OffsetNumberIsValid(root_offsets[offnum - 1]))
    1627           0 :                 ereport(ERROR,
    1628             :                         (errcode(ERRCODE_DATA_CORRUPTED),
    1629             :                          errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
    1630             :                                          ItemPointerGetBlockNumber(&heapTuple->t_self),
    1631             :                                          offnum,
    1632             :                                          RelationGetRelationName(heapRelation))));
    1633             : 
    1634       12228 :             ItemPointerSet(&tid, ItemPointerGetBlockNumber(&heapTuple->t_self),
    1635             :                            root_offsets[offnum - 1]);
    1636             : 
    1637             :             /* Call the AM's callback routine to process the tuple */
    1638       12228 :             callback(indexRelation, &tid, values, isnull, tupleIsAlive,
    1639             :                      callback_state);
    1640             :         }
    1641             :         else
    1642             :         {
    1643             :             /* Call the AM's callback routine to process the tuple */
    1644    18845070 :             callback(indexRelation, &heapTuple->t_self, values, isnull,
    1645             :                      tupleIsAlive, callback_state);
    1646             :         }
    1647             :     }
    1648             : 
    1649             :     /* Report scan progress one last time. */
    1650       77554 :     if (progress)
    1651             :     {
    1652             :         BlockNumber blks_done;
    1653             : 
    1654       77410 :         if (hscan->rs_base.rs_parallel != NULL)
    1655             :         {
    1656             :             ParallelBlockTableScanDesc pbscan;
    1657             : 
    1658          88 :             pbscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
    1659          88 :             blks_done = pbscan->phs_nblocks;
    1660             :         }
    1661             :         else
    1662       77322 :             blks_done = hscan->rs_nblocks;
    1663             : 
    1664       77410 :         pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
    1665             :                                      blks_done);
    1666             :     }
    1667             : 
    1668       77554 :     table_endscan(scan);
    1669             : 
    1670             :     /* we can now forget our snapshot, if set and registered by us */
    1671       77554 :     if (need_unregister_snapshot)
    1672       53876 :         UnregisterSnapshot(snapshot);
    1673             : 
    1674       77554 :     ExecDropSingleTupleTableSlot(slot);
    1675             : 
    1676       77554 :     FreeExecutorState(estate);
    1677             : 
    1678             :     /* These may have been pointing to the now-gone estate */
    1679       77554 :     indexInfo->ii_ExpressionsState = NIL;
    1680       77554 :     indexInfo->ii_PredicateState = NULL;
    1681             : 
    1682       77554 :     return reltuples;
    1683             : }
    1684             : 
    1685             : static void
    1686         164 : heapam_index_validate_scan(Relation heapRelation,
    1687             :                            Relation indexRelation,
    1688             :                            IndexInfo *indexInfo,
    1689             :                            Snapshot snapshot,
    1690             :                            ValidateIndexState *state)
    1691             : {
    1692             :     TableScanDesc scan;
    1693             :     HeapScanDesc hscan;
    1694             :     HeapTuple   heapTuple;
    1695             :     Datum       values[INDEX_MAX_KEYS];
    1696             :     bool        isnull[INDEX_MAX_KEYS];
    1697             :     ExprState  *predicate;
    1698             :     TupleTableSlot *slot;
    1699             :     EState     *estate;
    1700             :     ExprContext *econtext;
    1701         164 :     BlockNumber root_blkno = InvalidBlockNumber;
    1702             :     OffsetNumber root_offsets[MaxHeapTuplesPerPage];
    1703             :     bool        in_index[MaxHeapTuplesPerPage];
    1704         164 :     BlockNumber previous_blkno = InvalidBlockNumber;
    1705             : 
    1706             :     /* state variables for the merge */
    1707         164 :     ItemPointer indexcursor = NULL;
    1708             :     ItemPointerData decoded;
    1709         164 :     bool        tuplesort_empty = false;
    1710             : 
    1711             :     /*
    1712             :      * sanity checks
    1713             :      */
    1714             :     Assert(OidIsValid(indexRelation->rd_rel->relam));
    1715             : 
    1716             :     /*
    1717             :      * Need an EState for evaluation of index expressions and partial-index
    1718             :      * predicates.  Also a slot to hold the current tuple.
    1719             :      */
    1720         164 :     estate = CreateExecutorState();
    1721         164 :     econtext = GetPerTupleExprContext(estate);
    1722         164 :     slot = MakeSingleTupleTableSlot(RelationGetDescr(heapRelation),
    1723             :                                     &TTSOpsHeapTuple);
    1724             : 
    1725             :     /* Arrange for econtext's scan tuple to be the tuple under test */
    1726         164 :     econtext->ecxt_scantuple = slot;
    1727             : 
    1728             :     /* Set up execution state for predicate, if any. */
    1729         164 :     predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate);
    1730             : 
    1731             :     /*
    1732             :      * Prepare for scan of the base relation.  We need just those tuples
    1733             :      * satisfying the passed-in reference snapshot.  We must disable syncscan
    1734             :      * here, because it's critical that we read from block zero forward to
    1735             :      * match the sorted TIDs.
    1736             :      */
    1737         164 :     scan = table_beginscan_strat(heapRelation,  /* relation */
    1738             :                                  snapshot,  /* snapshot */
    1739             :                                  0, /* number of keys */
    1740             :                                  NULL,  /* scan key */
    1741             :                                  true,  /* buffer access strategy OK */
    1742             :                                  false);    /* syncscan not OK */
    1743         164 :     hscan = (HeapScanDesc) scan;
    1744             : 
    1745         164 :     pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_TOTAL,
    1746         164 :                                  hscan->rs_nblocks);
    1747             : 
    1748             :     /*
    1749             :      * Scan all tuples matching the snapshot.
    1750             :      */
    1751       10764 :     while ((heapTuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
    1752             :     {
    1753       10600 :         ItemPointer heapcursor = &heapTuple->t_self;
    1754             :         ItemPointerData rootTuple;
    1755             :         OffsetNumber root_offnum;
    1756             : 
    1757       10600 :         CHECK_FOR_INTERRUPTS();
    1758             : 
    1759       10600 :         state->htups += 1;
    1760             : 
    1761       10600 :         if ((previous_blkno == InvalidBlockNumber) ||
    1762       10512 :             (hscan->rs_cblock != previous_blkno))
    1763             :         {
    1764         140 :             pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_DONE,
    1765         140 :                                          hscan->rs_cblock);
    1766         140 :             previous_blkno = hscan->rs_cblock;
    1767             :         }
    1768             : 
    1769             :         /*
    1770             :          * As commented in table_index_build_scan, we should index heap-only
    1771             :          * tuples under the TIDs of their root tuples; so when we advance onto
    1772             :          * a new heap page, build a map of root item offsets on the page.
    1773             :          *
    1774             :          * This complicates merging against the tuplesort output: we will
    1775             :          * visit the live tuples in order by their offsets, but the root
    1776             :          * offsets that we need to compare against the index contents might be
    1777             :          * ordered differently.  So we might have to "look back" within the
    1778             :          * tuplesort output, but only within the current page.  We handle that
    1779             :          * by keeping a bool array in_index[] showing all the
    1780             :          * already-passed-over tuplesort output TIDs of the current page. We
    1781             :          * clear that array here, when advancing onto a new heap page.
    1782             :          */
    1783       10600 :         if (hscan->rs_cblock != root_blkno)
    1784             :         {
    1785         140 :             Page        page = BufferGetPage(hscan->rs_cbuf);
    1786             : 
    1787         140 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
    1788         140 :             heap_get_root_tuples(page, root_offsets);
    1789         140 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    1790             : 
    1791         140 :             memset(in_index, 0, sizeof(in_index));
    1792             : 
    1793         140 :             root_blkno = hscan->rs_cblock;
    1794             :         }
    1795             : 
    1796             :         /* Convert actual tuple TID to root TID */
    1797       10600 :         rootTuple = *heapcursor;
    1798       10600 :         root_offnum = ItemPointerGetOffsetNumber(heapcursor);
    1799             : 
    1800       10600 :         if (HeapTupleIsHeapOnly(heapTuple))
    1801             :         {
    1802           8 :             root_offnum = root_offsets[root_offnum - 1];
    1803           8 :             if (!OffsetNumberIsValid(root_offnum))
    1804           0 :                 ereport(ERROR,
    1805             :                         (errcode(ERRCODE_DATA_CORRUPTED),
    1806             :                          errmsg_internal("failed to find parent tuple for heap-only tuple at (%u,%u) in table \"%s\"",
    1807             :                                          ItemPointerGetBlockNumber(heapcursor),
    1808             :                                          ItemPointerGetOffsetNumber(heapcursor),
    1809             :                                          RelationGetRelationName(heapRelation))));
    1810           8 :             ItemPointerSetOffsetNumber(&rootTuple, root_offnum);
    1811             :         }
    1812             : 
    1813             :         /*
    1814             :          * "merge" by skipping through the index tuples until we find or pass
    1815             :          * the current root tuple.
    1816             :          */
    1817       21180 :         while (!tuplesort_empty &&
    1818       21068 :                (!indexcursor ||
    1819       21068 :                 ItemPointerCompare(indexcursor, &rootTuple) < 0))
    1820             :         {
    1821             :             Datum       ts_val;
    1822             :             bool        ts_isnull;
    1823             : 
    1824       10580 :             if (indexcursor)
    1825             :             {
    1826             :                 /*
    1827             :                  * Remember index items seen earlier on the current heap page
    1828             :                  */
    1829       10492 :                 if (ItemPointerGetBlockNumber(indexcursor) == root_blkno)
    1830       10440 :                     in_index[ItemPointerGetOffsetNumber(indexcursor) - 1] = true;
    1831             :             }
    1832             : 
    1833       10580 :             tuplesort_empty = !tuplesort_getdatum(state->tuplesort, true,
    1834       10580 :                                                   &ts_val, &ts_isnull, NULL);
    1835             :             Assert(tuplesort_empty || !ts_isnull);
    1836       10580 :             if (!tuplesort_empty)
    1837             :             {
    1838       10568 :                 itemptr_decode(&decoded, DatumGetInt64(ts_val));
    1839       10568 :                 indexcursor = &decoded;
    1840             : 
    1841             :                 /* If int8 is pass-by-ref, free (encoded) TID Datum memory */
    1842             : #ifndef USE_FLOAT8_BYVAL
    1843             :                 pfree(DatumGetPointer(ts_val));
    1844             : #endif
    1845             :             }
    1846             :             else
    1847             :             {
    1848             :                 /* Be tidy */
    1849          12 :                 indexcursor = NULL;
    1850             :             }
    1851             :         }
    1852             : 
    1853             :         /*
    1854             :          * If the tuplesort has overshot *and* we didn't see a match earlier,
    1855             :          * then this tuple is missing from the index, so insert it.
    1856             :          */
    1857       21176 :         if ((tuplesort_empty ||
    1858       10576 :              ItemPointerCompare(indexcursor, &rootTuple) > 0) &&
    1859          40 :             !in_index[root_offnum - 1])
    1860             :         {
    1861          32 :             MemoryContextReset(econtext->ecxt_per_tuple_memory);
    1862             : 
    1863             :             /* Set up for predicate or expression evaluation */
    1864          32 :             ExecStoreHeapTuple(heapTuple, slot, false);
    1865             : 
    1866             :             /*
    1867             :              * In a partial index, discard tuples that don't satisfy the
    1868             :              * predicate.
    1869             :              */
    1870          32 :             if (predicate != NULL)
    1871             :             {
    1872          32 :                 if (!ExecQual(predicate, econtext))
    1873          32 :                     continue;
    1874             :             }
    1875             : 
    1876             :             /*
    1877             :              * For the current heap tuple, extract all the attributes we use
    1878             :              * in this index, and note which are null.  This also performs
    1879             :              * evaluation of any expressions needed.
    1880             :              */
    1881           0 :             FormIndexDatum(indexInfo,
    1882             :                            slot,
    1883             :                            estate,
    1884             :                            values,
    1885             :                            isnull);
    1886             : 
    1887             :             /*
    1888             :              * You'd think we should go ahead and build the index tuple here,
    1889             :              * but some index AMs want to do further processing on the data
    1890             :              * first. So pass the values[] and isnull[] arrays, instead.
    1891             :              */
    1892             : 
    1893             :             /*
    1894             :              * If the tuple is already committed dead, you might think we
    1895             :              * could suppress uniqueness checking, but this is no longer true
    1896             :              * in the presence of HOT, because the insert is actually a proxy
    1897             :              * for a uniqueness check on the whole HOT-chain.  That is, the
    1898             :              * tuple we have here could be dead because it was already
    1899             :              * HOT-updated, and if so the updating transaction will not have
    1900             :              * thought it should insert index entries.  The index AM will
    1901             :              * check the whole HOT-chain and correctly detect a conflict if
    1902             :              * there is one.
    1903             :              */
    1904             : 
    1905           0 :             index_insert(indexRelation,
    1906             :                          values,
    1907             :                          isnull,
    1908             :                          &rootTuple,
    1909             :                          heapRelation,
    1910           0 :                          indexInfo->ii_Unique ?
    1911             :                          UNIQUE_CHECK_YES : UNIQUE_CHECK_NO,
    1912             :                          indexInfo);
    1913             : 
    1914           0 :             state->tups_inserted += 1;
    1915             :         }
    1916             :     }
    1917             : 
    1918         164 :     table_endscan(scan);
    1919             : 
    1920         164 :     ExecDropSingleTupleTableSlot(slot);
    1921             : 
    1922         164 :     FreeExecutorState(estate);
    1923             : 
    1924             :     /* These may have been pointing to the now-gone estate */
    1925         164 :     indexInfo->ii_ExpressionsState = NIL;
    1926         164 :     indexInfo->ii_PredicateState = NULL;
    1927         164 : }
    1928             : 
    1929             : /*
    1930             :  * Return the number of blocks that have been read by this scan since
    1931             :  * starting.  This is meant for progress reporting rather than be fully
    1932             :  * accurate: in a parallel scan, workers can be concurrently reading blocks
    1933             :  * further ahead than what we report.
    1934             :  */
    1935             : static BlockNumber
    1936    16816388 : heapam_scan_get_blocks_done(HeapScanDesc hscan)
    1937             : {
    1938    16816388 :     ParallelBlockTableScanDesc bpscan = NULL;
    1939             :     BlockNumber startblock;
    1940             :     BlockNumber blocks_done;
    1941             : 
    1942    16816388 :     if (hscan->rs_base.rs_parallel != NULL)
    1943             :     {
    1944     1944286 :         bpscan = (ParallelBlockTableScanDesc) hscan->rs_base.rs_parallel;
    1945     1944286 :         startblock = bpscan->phs_startblock;
    1946             :     }
    1947             :     else
    1948    14872102 :         startblock = hscan->rs_startblock;
    1949             : 
    1950             :     /*
    1951             :      * Might have wrapped around the end of the relation, if startblock was
    1952             :      * not zero.
    1953             :      */
    1954    16816388 :     if (hscan->rs_cblock > startblock)
    1955    15717826 :         blocks_done = hscan->rs_cblock - startblock;
    1956             :     else
    1957             :     {
    1958             :         BlockNumber nblocks;
    1959             : 
    1960     1098562 :         nblocks = bpscan != NULL ? bpscan->phs_nblocks : hscan->rs_nblocks;
    1961     2197124 :         blocks_done = nblocks - startblock +
    1962     1098562 :             hscan->rs_cblock;
    1963             :     }
    1964             : 
    1965    16816388 :     return blocks_done;
    1966             : }
    1967             : 
    1968             : 
    1969             : /* ------------------------------------------------------------------------
    1970             :  * Miscellaneous callbacks for the heap AM
    1971             :  * ------------------------------------------------------------------------
    1972             :  */
    1973             : 
    1974             : /*
    1975             :  * Check to see whether the table needs a TOAST table.  It does only if
    1976             :  * (1) there are any toastable attributes, and (2) the maximum length
    1977             :  * of a tuple could exceed TOAST_TUPLE_THRESHOLD.  (We don't want to
    1978             :  * create a toast table for something like "f1 varchar(20)".)
    1979             :  */
    1980             : static bool
    1981       34488 : heapam_relation_needs_toast_table(Relation rel)
    1982             : {
    1983       34488 :     int32       data_length = 0;
    1984       34488 :     bool        maxlength_unknown = false;
    1985       34488 :     bool        has_toastable_attrs = false;
    1986       34488 :     TupleDesc   tupdesc = rel->rd_att;
    1987             :     int32       tuple_length;
    1988             :     int         i;
    1989             : 
    1990      211164 :     for (i = 0; i < tupdesc->natts; i++)
    1991             :     {
    1992      176676 :         Form_pg_attribute att = TupleDescAttr(tupdesc, i);
    1993             : 
    1994      176676 :         if (att->attisdropped)
    1995         642 :             continue;
    1996      176034 :         data_length = att_align_nominal(data_length, att->attalign);
    1997      176034 :         if (att->attlen > 0)
    1998             :         {
    1999             :             /* Fixed-length types are never toastable */
    2000      129408 :             data_length += att->attlen;
    2001             :         }
    2002             :         else
    2003             :         {
    2004       46626 :             int32       maxlen = type_maximum_size(att->atttypid,
    2005             :                                                    att->atttypmod);
    2006             : 
    2007       46626 :             if (maxlen < 0)
    2008       44866 :                 maxlength_unknown = true;
    2009             :             else
    2010        1760 :                 data_length += maxlen;
    2011       46626 :             if (att->attstorage != TYPSTORAGE_PLAIN)
    2012       44440 :                 has_toastable_attrs = true;
    2013             :         }
    2014             :     }
    2015       34488 :     if (!has_toastable_attrs)
    2016       12968 :         return false;           /* nothing to toast? */
    2017       21520 :     if (maxlength_unknown)
    2018       20174 :         return true;            /* any unlimited-length attrs? */
    2019        2692 :     tuple_length = MAXALIGN(SizeofHeapTupleHeader +
    2020        1346 :                             BITMAPLEN(tupdesc->natts)) +
    2021        1346 :         MAXALIGN(data_length);
    2022        1346 :     return (tuple_length > TOAST_TUPLE_THRESHOLD);
    2023             : }
    2024             : 
    2025             : /*
    2026             :  * TOAST tables for heap relations are just heap relations.
    2027             :  */
    2028             : static Oid
    2029       20622 : heapam_relation_toast_am(Relation rel)
    2030             : {
    2031       20622 :     return rel->rd_rel->relam;
    2032             : }
    2033             : 
    2034             : 
    2035             : /* ------------------------------------------------------------------------
    2036             :  * Planner related callbacks for the heap AM
    2037             :  * ------------------------------------------------------------------------
    2038             :  */
    2039             : 
    2040             : #define HEAP_OVERHEAD_BYTES_PER_TUPLE \
    2041             :     (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))
    2042             : #define HEAP_USABLE_BYTES_PER_PAGE \
    2043             :     (BLCKSZ - SizeOfPageHeaderData)
    2044             : 
    2045             : static void
    2046      269194 : heapam_estimate_rel_size(Relation rel, int32 *attr_widths,
    2047             :                          BlockNumber *pages, double *tuples,
    2048             :                          double *allvisfrac)
    2049             : {
    2050      269194 :     table_block_relation_estimate_size(rel, attr_widths, pages,
    2051             :                                        tuples, allvisfrac,
    2052             :                                        HEAP_OVERHEAD_BYTES_PER_TUPLE,
    2053             :                                        HEAP_USABLE_BYTES_PER_PAGE);
    2054      269194 : }
    2055             : 
    2056             : 
    2057             : /* ------------------------------------------------------------------------
    2058             :  * Executor related callbacks for the heap AM
    2059             :  * ------------------------------------------------------------------------
    2060             :  */
    2061             : 
    2062             : static bool
    2063      135106 : heapam_scan_bitmap_next_block(TableScanDesc scan,
    2064             :                               TBMIterateResult *tbmres)
    2065             : {
    2066      135106 :     HeapScanDesc hscan = (HeapScanDesc) scan;
    2067      135106 :     BlockNumber page = tbmres->blockno;
    2068             :     Buffer      buffer;
    2069             :     Snapshot    snapshot;
    2070             :     int         ntup;
    2071             : 
    2072      135106 :     hscan->rs_cindex = 0;
    2073      135106 :     hscan->rs_ntuples = 0;
    2074             : 
    2075             :     /*
    2076             :      * Ignore any claimed entries past what we think is the end of the
    2077             :      * relation. It may have been extended after the start of our scan (we
    2078             :      * only hold an AccessShareLock, and it could be inserts from this
    2079             :      * backend).
    2080             :      */
    2081      135106 :     if (page >= hscan->rs_nblocks)
    2082          12 :         return false;
    2083             : 
    2084             :     /*
    2085             :      * Acquire pin on the target heap page, trading in any pin we held before.
    2086             :      */
    2087      135094 :     hscan->rs_cbuf = ReleaseAndReadBuffer(hscan->rs_cbuf,
    2088             :                                           scan->rs_rd,
    2089             :                                           page);
    2090      135094 :     hscan->rs_cblock = page;
    2091      135094 :     buffer = hscan->rs_cbuf;
    2092      135094 :     snapshot = scan->rs_snapshot;
    2093             : 
    2094      135094 :     ntup = 0;
    2095             : 
    2096             :     /*
    2097             :      * Prune and repair fragmentation for the whole page, if possible.
    2098             :      */
    2099      135094 :     heap_page_prune_opt(scan->rs_rd, buffer);
    2100             : 
    2101             :     /*
    2102             :      * We must hold share lock on the buffer content while examining tuple
    2103             :      * visibility.  Afterwards, however, the tuples we have found to be
    2104             :      * visible are guaranteed good as long as we hold the buffer pin.
    2105             :      */
    2106      135094 :     LockBuffer(buffer, BUFFER_LOCK_SHARE);
    2107             : 
    2108             :     /*
    2109             :      * We need two separate strategies for lossy and non-lossy cases.
    2110             :      */
    2111      135094 :     if (tbmres->ntuples >= 0)
    2112             :     {
    2113             :         /*
    2114             :          * Bitmap is non-lossy, so we just look through the offsets listed in
    2115             :          * tbmres; but we have to follow any HOT chain starting at each such
    2116             :          * offset.
    2117             :          */
    2118             :         int         curslot;
    2119             : 
    2120     1830704 :         for (curslot = 0; curslot < tbmres->ntuples; curslot++)
    2121             :         {
    2122     1775732 :             OffsetNumber offnum = tbmres->offsets[curslot];
    2123             :             ItemPointerData tid;
    2124             :             HeapTupleData heapTuple;
    2125             : 
    2126     1775732 :             ItemPointerSet(&tid, page, offnum);
    2127     1775732 :             if (heap_hot_search_buffer(&tid, scan->rs_rd, buffer, snapshot,
    2128             :                                        &heapTuple, NULL, true))
    2129     1670836 :                 hscan->rs_vistuples[ntup++] = ItemPointerGetOffsetNumber(&tid);
    2130             :         }
    2131             :     }
    2132             :     else
    2133             :     {
    2134             :         /*
    2135             :          * Bitmap is lossy, so we must examine each line pointer on the page.
    2136             :          * But we can ignore HOT chains, since we'll check each tuple anyway.
    2137             :          */
    2138       80116 :         Page        dp = (Page) BufferGetPage(buffer);
    2139       80116 :         OffsetNumber maxoff = PageGetMaxOffsetNumber(dp);
    2140             :         OffsetNumber offnum;
    2141             : 
    2142      570716 :         for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
    2143             :         {
    2144             :             ItemId      lp;
    2145             :             HeapTupleData loctup;
    2146             :             bool        valid;
    2147             : 
    2148      490600 :             lp = PageGetItemId(dp, offnum);
    2149      490600 :             if (!ItemIdIsNormal(lp))
    2150           0 :                 continue;
    2151      490600 :             loctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
    2152      490600 :             loctup.t_len = ItemIdGetLength(lp);
    2153      490600 :             loctup.t_tableOid = scan->rs_rd->rd_id;
    2154      490600 :             ItemPointerSet(&loctup.t_self, page, offnum);
    2155      490600 :             valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
    2156      490600 :             if (valid)
    2157             :             {
    2158      490600 :                 hscan->rs_vistuples[ntup++] = offnum;
    2159      490600 :                 PredicateLockTID(scan->rs_rd, &loctup.t_self, snapshot,
    2160      490600 :                                  HeapTupleHeaderGetXmin(loctup.t_data));
    2161             :             }
    2162      490600 :             HeapCheckForSerializableConflictOut(valid, scan->rs_rd, &loctup,
    2163             :                                                 buffer, snapshot);
    2164             :         }
    2165             :     }
    2166             : 
    2167      135088 :     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    2168             : 
    2169             :     Assert(ntup <= MaxHeapTuplesPerPage);
    2170      135088 :     hscan->rs_ntuples = ntup;
    2171             : 
    2172      135088 :     return ntup > 0;
    2173             : }
    2174             : 
    2175             : static bool
    2176     2293226 : heapam_scan_bitmap_next_tuple(TableScanDesc scan,
    2177             :                               TBMIterateResult *tbmres,
    2178             :                               TupleTableSlot *slot)
    2179             : {
    2180     2293226 :     HeapScanDesc hscan = (HeapScanDesc) scan;
    2181             :     OffsetNumber targoffset;
    2182             :     Page        dp;
    2183             :     ItemId      lp;
    2184             : 
    2185             :     /*
    2186             :      * Out of range?  If so, nothing more to look at on this page
    2187             :      */
    2188     2293226 :     if (hscan->rs_cindex < 0 || hscan->rs_cindex >= hscan->rs_ntuples)
    2189      134922 :         return false;
    2190             : 
    2191     2158304 :     targoffset = hscan->rs_vistuples[hscan->rs_cindex];
    2192     2158304 :     dp = (Page) BufferGetPage(hscan->rs_cbuf);
    2193     2158304 :     lp = PageGetItemId(dp, targoffset);
    2194             :     Assert(ItemIdIsNormal(lp));
    2195             : 
    2196     2158304 :     hscan->rs_ctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lp);
    2197     2158304 :     hscan->rs_ctup.t_len = ItemIdGetLength(lp);
    2198     2158304 :     hscan->rs_ctup.t_tableOid = scan->rs_rd->rd_id;
    2199     2158304 :     ItemPointerSet(&hscan->rs_ctup.t_self, hscan->rs_cblock, targoffset);
    2200             : 
    2201     2158304 :     pgstat_count_heap_fetch(scan->rs_rd);
    2202             : 
    2203             :     /*
    2204             :      * Set up the result slot to point to this tuple.  Note that the slot
    2205             :      * acquires a pin on the buffer.
    2206             :      */
    2207     2158304 :     ExecStoreBufferHeapTuple(&hscan->rs_ctup,
    2208             :                              slot,
    2209             :                              hscan->rs_cbuf);
    2210             : 
    2211     2158304 :     hscan->rs_cindex++;
    2212             : 
    2213     2158304 :     return true;
    2214             : }
    2215             : 
    2216             : static bool
    2217        8654 : heapam_scan_sample_next_block(TableScanDesc scan, SampleScanState *scanstate)
    2218             : {
    2219        8654 :     HeapScanDesc hscan = (HeapScanDesc) scan;
    2220        8654 :     TsmRoutine *tsm = scanstate->tsmroutine;
    2221             :     BlockNumber blockno;
    2222             : 
    2223             :     /* return false immediately if relation is empty */
    2224        8654 :     if (hscan->rs_nblocks == 0)
    2225           0 :         return false;
    2226             : 
    2227        8654 :     if (tsm->NextSampleBlock)
    2228             :     {
    2229        3010 :         blockno = tsm->NextSampleBlock(scanstate, hscan->rs_nblocks);
    2230        3010 :         hscan->rs_cblock = blockno;
    2231             :     }
    2232             :     else
    2233             :     {
    2234             :         /* scanning table sequentially */
    2235             : 
    2236        5644 :         if (hscan->rs_cblock == InvalidBlockNumber)
    2237             :         {
    2238             :             Assert(!hscan->rs_inited);
    2239          52 :             blockno = hscan->rs_startblock;
    2240             :         }
    2241             :         else
    2242             :         {
    2243             :             Assert(hscan->rs_inited);
    2244             : 
    2245        5592 :             blockno = hscan->rs_cblock + 1;
    2246             : 
    2247        5592 :             if (blockno >= hscan->rs_nblocks)
    2248             :             {
    2249             :                 /* wrap to beginning of rel, might not have started at 0 */
    2250          52 :                 blockno = 0;
    2251             :             }
    2252             : 
    2253             :             /*
    2254             :              * Report our new scan position for synchronization purposes.
    2255             :              *
    2256             :              * Note: we do this before checking for end of scan so that the
    2257             :              * final state of the position hint is back at the start of the
    2258             :              * rel.  That's not strictly necessary, but otherwise when you run
    2259             :              * the same query multiple times the starting position would shift
    2260             :              * a little bit backwards on every invocation, which is confusing.
    2261             :              * We don't guarantee any specific ordering in general, though.
    2262             :              */
    2263        5592 :             if (scan->rs_flags & SO_ALLOW_SYNC)
    2264           0 :                 ss_report_location(scan->rs_rd, blockno);
    2265             : 
    2266        5592 :             if (blockno == hscan->rs_startblock)
    2267             :             {
    2268          52 :                 blockno = InvalidBlockNumber;
    2269             :             }
    2270             :         }
    2271             :     }
    2272             : 
    2273        8654 :     if (!BlockNumberIsValid(blockno))
    2274             :     {
    2275         122 :         if (BufferIsValid(hscan->rs_cbuf))
    2276         102 :             ReleaseBuffer(hscan->rs_cbuf);
    2277         122 :         hscan->rs_cbuf = InvalidBuffer;
    2278         122 :         hscan->rs_cblock = InvalidBlockNumber;
    2279         122 :         hscan->rs_inited = false;
    2280             : 
    2281         122 :         return false;
    2282             :     }
    2283             : 
    2284        8532 :     heapgetpage(scan, blockno);
    2285        8532 :     hscan->rs_inited = true;
    2286             : 
    2287        8532 :     return true;
    2288             : }
    2289             : 
    2290             : static bool
    2291      169452 : heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate,
    2292             :                               TupleTableSlot *slot)
    2293             : {
    2294      169452 :     HeapScanDesc hscan = (HeapScanDesc) scan;
    2295      169452 :     TsmRoutine *tsm = scanstate->tsmroutine;
    2296      169452 :     BlockNumber blockno = hscan->rs_cblock;
    2297      169452 :     bool        pagemode = (scan->rs_flags & SO_ALLOW_PAGEMODE) != 0;
    2298             : 
    2299             :     Page        page;
    2300             :     bool        all_visible;
    2301             :     OffsetNumber maxoffset;
    2302             : 
    2303             :     /*
    2304             :      * When not using pagemode, we must lock the buffer during tuple
    2305             :      * visibility checks.
    2306             :      */
    2307      169452 :     if (!pagemode)
    2308        2796 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
    2309             : 
    2310      169452 :     page = (Page) BufferGetPage(hscan->rs_cbuf);
    2311      337980 :     all_visible = PageIsAllVisible(page) &&
    2312      168528 :         !scan->rs_snapshot->takenDuringRecovery;
    2313      169452 :     maxoffset = PageGetMaxOffsetNumber(page);
    2314             : 
    2315             :     for (;;)
    2316           0 :     {
    2317             :         OffsetNumber tupoffset;
    2318             : 
    2319      169452 :         CHECK_FOR_INTERRUPTS();
    2320             : 
    2321             :         /* Ask the tablesample method which tuples to check on this page. */
    2322      169452 :         tupoffset = tsm->NextSampleTuple(scanstate,
    2323             :                                          blockno,
    2324             :                                          maxoffset);
    2325             : 
    2326      169452 :         if (OffsetNumberIsValid(tupoffset))
    2327             :         {
    2328             :             ItemId      itemid;
    2329             :             bool        visible;
    2330      160928 :             HeapTuple   tuple = &(hscan->rs_ctup);
    2331             : 
    2332             :             /* Skip invalid tuple pointers. */
    2333      160928 :             itemid = PageGetItemId(page, tupoffset);
    2334      160928 :             if (!ItemIdIsNormal(itemid))
    2335           0 :                 continue;
    2336             : 
    2337      160928 :             tuple->t_data = (HeapTupleHeader) PageGetItem(page, itemid);
    2338      160928 :             tuple->t_len = ItemIdGetLength(itemid);
    2339      160928 :             ItemPointerSet(&(tuple->t_self), blockno, tupoffset);
    2340             : 
    2341             : 
    2342      160928 :             if (all_visible)
    2343      160232 :                 visible = true;
    2344             :             else
    2345         696 :                 visible = SampleHeapTupleVisible(scan, hscan->rs_cbuf,
    2346             :                                                  tuple, tupoffset);
    2347             : 
    2348             :             /* in pagemode, heapgetpage did this for us */
    2349      160928 :             if (!pagemode)
    2350           4 :                 HeapCheckForSerializableConflictOut(visible, scan->rs_rd, tuple,
    2351             :                                                     hscan->rs_cbuf, scan->rs_snapshot);
    2352             : 
    2353             :             /* Try next tuple from same page. */
    2354      160928 :             if (!visible)
    2355           0 :                 continue;
    2356             : 
    2357             :             /* Found visible tuple, return it. */
    2358      160928 :             if (!pagemode)
    2359           4 :                 LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    2360             : 
    2361      160928 :             ExecStoreBufferHeapTuple(tuple, slot, hscan->rs_cbuf);
    2362             : 
    2363             :             /* Count successfully-fetched tuples as heap fetches */
    2364      160928 :             pgstat_count_heap_getnext(scan->rs_rd);
    2365             : 
    2366      160928 :             return true;
    2367             :         }
    2368             :         else
    2369             :         {
    2370             :             /*
    2371             :              * If we get here, it means we've exhausted the items on this page
    2372             :              * and it's time to move to the next.
    2373             :              */
    2374        8524 :             if (!pagemode)
    2375        2792 :                 LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
    2376             : 
    2377        8524 :             ExecClearTuple(slot);
    2378        8524 :             return false;
    2379             :         }
    2380             :     }
    2381             : 
    2382             :     Assert(0);
    2383             : }
    2384             : 
    2385             : 
    2386             : /* ----------------------------------------------------------------------------
    2387             :  *  Helper functions for the above.
    2388             :  * ----------------------------------------------------------------------------
    2389             :  */
    2390             : 
    2391             : /*
    2392             :  * Reconstruct and rewrite the given tuple
    2393             :  *
    2394             :  * We cannot simply copy the tuple as-is, for several reasons:
    2395             :  *
    2396             :  * 1. We'd like to squeeze out the values of any dropped columns, both
    2397             :  * to save space and to ensure we have no corner-case failures. (It's
    2398             :  * possible for example that the new table hasn't got a TOAST table
    2399             :  * and so is unable to store any large values of dropped cols.)
    2400             :  *
    2401             :  * 2. The tuple might not even be legal for the new table; this is
    2402             :  * currently only known to happen as an after-effect of ALTER TABLE
    2403             :  * SET WITHOUT OIDS.
    2404             :  *
    2405             :  * So, we must reconstruct the tuple from component Datums.
    2406             :  */
    2407             : static void
    2408      465690 : reform_and_rewrite_tuple(HeapTuple tuple,
    2409             :                          Relation OldHeap, Relation NewHeap,
    2410             :                          Datum *values, bool *isnull, RewriteState rwstate)
    2411             : {
    2412      465690 :     TupleDesc   oldTupDesc = RelationGetDescr(OldHeap);
    2413      465690 :     TupleDesc   newTupDesc = RelationGetDescr(NewHeap);
    2414             :     HeapTuple   copiedTuple;
    2415             :     int         i;
    2416             : 
    2417      465690 :     heap_deform_tuple(tuple, oldTupDesc, values, isnull);
    2418             : 
    2419             :     /* Be sure to null out any dropped columns */
    2420     4309696 :     for (i = 0; i < newTupDesc->natts; i++)
    2421             :     {
    2422     3844006 :         if (TupleDescAttr(newTupDesc, i)->attisdropped)
    2423           0 :             isnull[i] = true;
    2424             :     }
    2425             : 
    2426      465690 :     copiedTuple = heap_form_tuple(newTupDesc, values, isnull);
    2427             : 
    2428             :     /* The heap rewrite module does the rest */
    2429      465690 :     rewrite_heap_tuple(rwstate, tuple, copiedTuple);
    2430             : 
    2431      465690 :     heap_freetuple(copiedTuple);
    2432      465690 : }
    2433             : 
    2434             : /*
    2435             :  * Check visibility of the tuple.
    2436             :  */
    2437             : static bool
    2438         696 : SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer,
    2439             :                        HeapTuple tuple,
    2440             :                        OffsetNumber tupoffset)
    2441             : {
    2442         696 :     HeapScanDesc hscan = (HeapScanDesc) scan;
    2443             : 
    2444         696 :     if (scan->rs_flags & SO_ALLOW_PAGEMODE)
    2445             :     {
    2446             :         /*
    2447             :          * In pageatatime mode, heapgetpage() already did visibility checks,
    2448             :          * so just look at the info it left in rs_vistuples[].
    2449             :          *
    2450             :          * We use a binary search over the known-sorted array.  Note: we could
    2451             :          * save some effort if we insisted that NextSampleTuple select tuples
    2452             :          * in increasing order, but it's not clear that there would be enough
    2453             :          * gain to justify the restriction.
    2454             :          */
    2455         692 :         int         start = 0,
    2456         692 :                     end = hscan->rs_ntuples - 1;
    2457             : 
    2458        1356 :         while (start <= end)
    2459             :         {
    2460        1356 :             int         mid = (start + end) / 2;
    2461        1356 :             OffsetNumber curoffset = hscan->rs_vistuples[mid];
    2462             : 
    2463        1356 :             if (tupoffset == curoffset)
    2464         692 :                 return true;
    2465         664 :             else if (tupoffset < curoffset)
    2466         260 :                 end = mid - 1;
    2467             :             else
    2468         404 :                 start = mid + 1;
    2469             :         }
    2470             : 
    2471           0 :         return false;
    2472             :     }
    2473             :     else
    2474             :     {
    2475             :         /* Otherwise, we have to check the tuple individually. */
    2476           4 :         return HeapTupleSatisfiesVisibility(tuple, scan->rs_snapshot,
    2477             :                                             buffer);
    2478             :     }
    2479             : }
    2480             : 
    2481             : 
    2482             : /* ------------------------------------------------------------------------
    2483             :  * Definition of the heap table access method.
    2484             :  * ------------------------------------------------------------------------
    2485             :  */
    2486             : 
    2487             : static const TableAmRoutine heapam_methods = {
    2488             :     .type = T_TableAmRoutine,
    2489             : 
    2490             :     .slot_callbacks = heapam_slot_callbacks,
    2491             : 
    2492             :     .scan_begin = heap_beginscan,
    2493             :     .scan_end = heap_endscan,
    2494             :     .scan_rescan = heap_rescan,
    2495             :     .scan_getnextslot = heap_getnextslot,
    2496             : 
    2497             :     .parallelscan_estimate = table_block_parallelscan_estimate,
    2498             :     .parallelscan_initialize = table_block_parallelscan_initialize,
    2499             :     .parallelscan_reinitialize = table_block_parallelscan_reinitialize,
    2500             : 
    2501             :     .index_fetch_begin = heapam_index_fetch_begin,
    2502             :     .index_fetch_reset = heapam_index_fetch_reset,
    2503             :     .index_fetch_end = heapam_index_fetch_end,
    2504             :     .index_fetch_tuple = heapam_index_fetch_tuple,
    2505             : 
    2506             :     .tuple_insert = heapam_tuple_insert,
    2507             :     .tuple_insert_speculative = heapam_tuple_insert_speculative,
    2508             :     .tuple_complete_speculative = heapam_tuple_complete_speculative,
    2509             :     .multi_insert = heap_multi_insert,
    2510             :     .tuple_delete = heapam_tuple_delete,
    2511             :     .tuple_update = heapam_tuple_update,
    2512             :     .tuple_lock = heapam_tuple_lock,
    2513             : 
    2514             :     .tuple_fetch_row_version = heapam_fetch_row_version,
    2515             :     .tuple_get_latest_tid = heap_get_latest_tid,
    2516             :     .tuple_tid_valid = heapam_tuple_tid_valid,
    2517             :     .tuple_satisfies_snapshot = heapam_tuple_satisfies_snapshot,
    2518             :     .compute_xid_horizon_for_tuples = heap_compute_xid_horizon_for_tuples,
    2519             : 
    2520             :     .relation_set_new_filenode = heapam_relation_set_new_filenode,
    2521             :     .relation_nontransactional_truncate = heapam_relation_nontransactional_truncate,
    2522             :     .relation_copy_data = heapam_relation_copy_data,
    2523             :     .relation_copy_for_cluster = heapam_relation_copy_for_cluster,
    2524             :     .relation_vacuum = heap_vacuum_rel,
    2525             :     .scan_analyze_next_block = heapam_scan_analyze_next_block,
    2526             :     .scan_analyze_next_tuple = heapam_scan_analyze_next_tuple,
    2527             :     .index_build_range_scan = heapam_index_build_range_scan,
    2528             :     .index_validate_scan = heapam_index_validate_scan,
    2529             : 
    2530             :     .relation_size = table_block_relation_size,
    2531             :     .relation_needs_toast_table = heapam_relation_needs_toast_table,
    2532             :     .relation_toast_am = heapam_relation_toast_am,
    2533             :     .relation_fetch_toast_slice = heap_fetch_toast_slice,
    2534             : 
    2535             :     .relation_estimate_size = heapam_estimate_rel_size,
    2536             : 
    2537             :     .scan_bitmap_next_block = heapam_scan_bitmap_next_block,
    2538             :     .scan_bitmap_next_tuple = heapam_scan_bitmap_next_tuple,
    2539             :     .scan_sample_next_block = heapam_scan_sample_next_block,
    2540             :     .scan_sample_next_tuple = heapam_scan_sample_next_tuple
    2541             : };
    2542             : 
    2543             : 
    2544             : const TableAmRoutine *
    2545    20608722 : GetHeapamTableAmRoutine(void)
    2546             : {
    2547    20608722 :     return &heapam_methods;
    2548             : }
    2549             : 
    2550             : Datum
    2551      857080 : heap_tableam_handler(PG_FUNCTION_ARGS)
    2552             : {
    2553      857080 :     PG_RETURN_POINTER(&heapam_methods);
    2554             : }

Generated by: LCOV version 1.13