Header And Logo

PostgreSQL
| The world's most advanced open source database.

heapam.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * heapam.c
00004  *    heap access method code
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/backend/access/heap/heapam.c
00012  *
00013  *
00014  * INTERFACE ROUTINES
00015  *      relation_open   - open any relation by relation OID
00016  *      relation_openrv - open any relation specified by a RangeVar
00017  *      relation_close  - close any relation
00018  *      heap_open       - open a heap relation by relation OID
00019  *      heap_openrv     - open a heap relation specified by a RangeVar
00020  *      heap_close      - (now just a macro for relation_close)
00021  *      heap_beginscan  - begin relation scan
00022  *      heap_rescan     - restart a relation scan
00023  *      heap_endscan    - end relation scan
00024  *      heap_getnext    - retrieve next tuple in scan
00025  *      heap_fetch      - retrieve tuple with given tid
00026  *      heap_insert     - insert tuple into a relation
00027  *      heap_multi_insert - insert multiple tuples into a relation
00028  *      heap_delete     - delete a tuple from a relation
00029  *      heap_update     - replace a tuple in a relation with another tuple
00030  *      heap_markpos    - mark scan position
00031  *      heap_restrpos   - restore position to marked location
00032  *      heap_sync       - sync heap, for when no WAL has been written
00033  *
00034  * NOTES
00035  *    This file contains the heap_ routines which implement
00036  *    the POSTGRES heap access method used for all POSTGRES
00037  *    relations.
00038  *
00039  *-------------------------------------------------------------------------
00040  */
00041 #include "postgres.h"
00042 
00043 #include "access/heapam.h"
00044 #include "access/heapam_xlog.h"
00045 #include "access/hio.h"
00046 #include "access/multixact.h"
00047 #include "access/relscan.h"
00048 #include "access/sysattr.h"
00049 #include "access/transam.h"
00050 #include "access/tuptoaster.h"
00051 #include "access/valid.h"
00052 #include "access/visibilitymap.h"
00053 #include "access/xact.h"
00054 #include "access/xlogutils.h"
00055 #include "catalog/catalog.h"
00056 #include "catalog/namespace.h"
00057 #include "miscadmin.h"
00058 #include "pgstat.h"
00059 #include "storage/bufmgr.h"
00060 #include "storage/freespace.h"
00061 #include "storage/lmgr.h"
00062 #include "storage/predicate.h"
00063 #include "storage/procarray.h"
00064 #include "storage/smgr.h"
00065 #include "storage/standby.h"
00066 #include "utils/datum.h"
00067 #include "utils/inval.h"
00068 #include "utils/lsyscache.h"
00069 #include "utils/relcache.h"
00070 #include "utils/snapmgr.h"
00071 #include "utils/syscache.h"
00072 #include "utils/tqual.h"
00073 
00074 
00075 /* GUC variable */
00076 bool        synchronize_seqscans = true;
00077 
00078 
00079 static HeapScanDesc heap_beginscan_internal(Relation relation,
00080                         Snapshot snapshot,
00081                         int nkeys, ScanKey key,
00082                         bool allow_strat, bool allow_sync,
00083                         bool is_bitmapscan);
00084 static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup,
00085                     TransactionId xid, CommandId cid, int options);
00086 static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
00087                 Buffer newbuf, HeapTuple oldtup,
00088                 HeapTuple newtup, bool all_visible_cleared,
00089                 bool new_all_visible_cleared);
00090 static void HeapSatisfiesHOTandKeyUpdate(Relation relation,
00091                              Bitmapset *hot_attrs, Bitmapset *key_attrs,
00092                              bool *satisfies_hot, bool *satisfies_key,
00093                              HeapTuple oldtup, HeapTuple newtup);
00094 static void compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask,
00095                           uint16 old_infomask2, TransactionId add_to_xmax,
00096                           LockTupleMode mode, bool is_update,
00097                           TransactionId *result_xmax, uint16 *result_infomask,
00098                           uint16 *result_infomask2);
00099 static HTSU_Result heap_lock_updated_tuple(Relation rel, HeapTuple tuple,
00100                         ItemPointer ctid, TransactionId xid,
00101                         LockTupleMode mode);
00102 static void GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask,
00103                        uint16 *new_infomask2);
00104 static TransactionId MultiXactIdGetUpdateXid(TransactionId xmax,
00105                         uint16 t_infomask);
00106 static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
00107                 int *remaining, uint16 infomask);
00108 static bool ConditionalMultiXactIdWait(MultiXactId multi,
00109                            MultiXactStatus status, int *remaining,
00110                            uint16 infomask);
00111 
00112 
00113 /*
00114  * Each tuple lock mode has a corresponding heavyweight lock, and one or two
00115  * corresponding MultiXactStatuses (one to merely lock tuples, another one to
00116  * update them).  This table (and the macros below) helps us determine the
00117  * heavyweight lock mode and MultiXactStatus values to use for any particular
00118  * tuple lock strength.
00119  */
00120 static const struct
00121 {
00122     LOCKMODE    hwlock;
00123     MultiXactStatus lockstatus;
00124     MultiXactStatus updstatus;
00125 }
00126 tupleLockExtraInfo[MaxLockTupleMode + 1] =
00127 {
00128     {   /* LockTupleKeyShare */
00129         AccessShareLock,
00130         MultiXactStatusForKeyShare,
00131         -1  /* KeyShare does not allow updating tuples */
00132     },
00133     {   /* LockTupleShare */
00134         RowShareLock,
00135         MultiXactStatusForShare,
00136         -1  /* Share does not allow updating tuples */
00137     },
00138     {   /* LockTupleNoKeyExclusive */
00139         ExclusiveLock,
00140         MultiXactStatusForNoKeyUpdate,
00141         MultiXactStatusNoKeyUpdate
00142     },
00143     {   /* LockTupleExclusive */
00144         AccessExclusiveLock,
00145         MultiXactStatusForUpdate,
00146         MultiXactStatusUpdate
00147     }
00148 };
00149 /* Get the LOCKMODE for a given MultiXactStatus */
00150 #define LOCKMODE_from_mxstatus(status) \
00151             (tupleLockExtraInfo[TUPLOCK_from_mxstatus((status))].hwlock)
00152 
00153 /*
00154  * Acquire heavyweight locks on tuples, using a LockTupleMode strength value.
00155  * This is more readable than having every caller translate it to lock.h's
00156  * LOCKMODE.
00157  */
00158 #define LockTupleTuplock(rel, tup, mode) \
00159     LockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
00160 #define UnlockTupleTuplock(rel, tup, mode) \
00161     UnlockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
00162 #define ConditionalLockTupleTuplock(rel, tup, mode) \
00163     ConditionalLockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
00164 
00165 /*
00166  * This table maps tuple lock strength values for each particular
00167  * MultiXactStatus value.
00168  */
00169 static const int MultiXactStatusLock[MaxMultiXactStatus + 1] =
00170 {
00171     LockTupleKeyShare,      /* ForKeyShare */
00172     LockTupleShare,         /* ForShare */
00173     LockTupleNoKeyExclusive,        /* ForNoKeyUpdate */
00174     LockTupleExclusive,     /* ForUpdate */
00175     LockTupleNoKeyExclusive,        /* NoKeyUpdate */
00176     LockTupleExclusive      /* Update */
00177 };
00178 
00179 /* Get the LockTupleMode for a given MultiXactStatus */
00180 #define TUPLOCK_from_mxstatus(status) \
00181             (MultiXactStatusLock[(status)])
00182 /* Get the is_update bit for a given MultiXactStatus */
00183 #define ISUPDATE_from_mxstatus(status) \
00184             ((status) > MultiXactStatusForUpdate)
00185 
00186 /* ----------------------------------------------------------------
00187  *                       heap support routines
00188  * ----------------------------------------------------------------
00189  */
00190 
00191 /* ----------------
00192  *      initscan - scan code common to heap_beginscan and heap_rescan
00193  * ----------------
00194  */
00195 static void
00196 initscan(HeapScanDesc scan, ScanKey key, bool is_rescan)
00197 {
00198     bool        allow_strat;
00199     bool        allow_sync;
00200 
00201     /*
00202      * Determine the number of blocks we have to scan.
00203      *
00204      * It is sufficient to do this once at scan start, since any tuples added
00205      * while the scan is in progress will be invisible to my snapshot anyway.
00206      * (That is not true when using a non-MVCC snapshot.  However, we couldn't
00207      * guarantee to return tuples added after scan start anyway, since they
00208      * might go into pages we already scanned.  To guarantee consistent
00209      * results for a non-MVCC snapshot, the caller must hold some higher-level
00210      * lock that ensures the interesting tuple(s) won't change.)
00211      */
00212     scan->rs_nblocks = RelationGetNumberOfBlocks(scan->rs_rd);
00213 
00214     /*
00215      * If the table is large relative to NBuffers, use a bulk-read access
00216      * strategy and enable synchronized scanning (see syncscan.c).  Although
00217      * the thresholds for these features could be different, we make them the
00218      * same so that there are only two behaviors to tune rather than four.
00219      * (However, some callers need to be able to disable one or both of these
00220      * behaviors, independently of the size of the table; also there is a GUC
00221      * variable that can disable synchronized scanning.)
00222      *
00223      * During a rescan, don't make a new strategy object if we don't have to.
00224      */
00225     if (!RelationUsesLocalBuffers(scan->rs_rd) &&
00226         scan->rs_nblocks > NBuffers / 4)
00227     {
00228         allow_strat = scan->rs_allow_strat;
00229         allow_sync = scan->rs_allow_sync;
00230     }
00231     else
00232         allow_strat = allow_sync = false;
00233 
00234     if (allow_strat)
00235     {
00236         if (scan->rs_strategy == NULL)
00237             scan->rs_strategy = GetAccessStrategy(BAS_BULKREAD);
00238     }
00239     else
00240     {
00241         if (scan->rs_strategy != NULL)
00242             FreeAccessStrategy(scan->rs_strategy);
00243         scan->rs_strategy = NULL;
00244     }
00245 
00246     if (is_rescan)
00247     {
00248         /*
00249          * If rescan, keep the previous startblock setting so that rewinding a
00250          * cursor doesn't generate surprising results.  Reset the syncscan
00251          * setting, though.
00252          */
00253         scan->rs_syncscan = (allow_sync && synchronize_seqscans);
00254     }
00255     else if (allow_sync && synchronize_seqscans)
00256     {
00257         scan->rs_syncscan = true;
00258         scan->rs_startblock = ss_get_location(scan->rs_rd, scan->rs_nblocks);
00259     }
00260     else
00261     {
00262         scan->rs_syncscan = false;
00263         scan->rs_startblock = 0;
00264     }
00265 
00266     scan->rs_inited = false;
00267     scan->rs_ctup.t_data = NULL;
00268     ItemPointerSetInvalid(&scan->rs_ctup.t_self);
00269     scan->rs_cbuf = InvalidBuffer;
00270     scan->rs_cblock = InvalidBlockNumber;
00271 
00272     /* we don't have a marked position... */
00273     ItemPointerSetInvalid(&(scan->rs_mctid));
00274 
00275     /* page-at-a-time fields are always invalid when not rs_inited */
00276 
00277     /*
00278      * copy the scan key, if appropriate
00279      */
00280     if (key != NULL)
00281         memcpy(scan->rs_key, key, scan->rs_nkeys * sizeof(ScanKeyData));
00282 
00283     /*
00284      * Currently, we don't have a stats counter for bitmap heap scans (but the
00285      * underlying bitmap index scans will be counted).
00286      */
00287     if (!scan->rs_bitmapscan)
00288         pgstat_count_heap_scan(scan->rs_rd);
00289 }
00290 
00291 /*
00292  * heapgetpage - subroutine for heapgettup()
00293  *
00294  * This routine reads and pins the specified page of the relation.
00295  * In page-at-a-time mode it performs additional work, namely determining
00296  * which tuples on the page are visible.
00297  */
00298 static void
00299 heapgetpage(HeapScanDesc scan, BlockNumber page)
00300 {
00301     Buffer      buffer;
00302     Snapshot    snapshot;
00303     Page        dp;
00304     int         lines;
00305     int         ntup;
00306     OffsetNumber lineoff;
00307     ItemId      lpp;
00308     bool        all_visible;
00309 
00310     Assert(page < scan->rs_nblocks);
00311 
00312     /* release previous scan buffer, if any */
00313     if (BufferIsValid(scan->rs_cbuf))
00314     {
00315         ReleaseBuffer(scan->rs_cbuf);
00316         scan->rs_cbuf = InvalidBuffer;
00317     }
00318 
00319     /*
00320      * Be sure to check for interrupts at least once per page.  Checks at
00321      * higher code levels won't be able to stop a seqscan that encounters many
00322      * pages' worth of consecutive dead tuples.
00323      */
00324     CHECK_FOR_INTERRUPTS();
00325 
00326     /* read page using selected strategy */
00327     scan->rs_cbuf = ReadBufferExtended(scan->rs_rd, MAIN_FORKNUM, page,
00328                                        RBM_NORMAL, scan->rs_strategy);
00329     scan->rs_cblock = page;
00330 
00331     if (!scan->rs_pageatatime)
00332         return;
00333 
00334     buffer = scan->rs_cbuf;
00335     snapshot = scan->rs_snapshot;
00336 
00337     /*
00338      * Prune and repair fragmentation for the whole page, if possible.
00339      */
00340     Assert(TransactionIdIsValid(RecentGlobalXmin));
00341     heap_page_prune_opt(scan->rs_rd, buffer, RecentGlobalXmin);
00342 
00343     /*
00344      * We must hold share lock on the buffer content while examining tuple
00345      * visibility.  Afterwards, however, the tuples we have found to be
00346      * visible are guaranteed good as long as we hold the buffer pin.
00347      */
00348     LockBuffer(buffer, BUFFER_LOCK_SHARE);
00349 
00350     dp = (Page) BufferGetPage(buffer);
00351     lines = PageGetMaxOffsetNumber(dp);
00352     ntup = 0;
00353 
00354     /*
00355      * If the all-visible flag indicates that all tuples on the page are
00356      * visible to everyone, we can skip the per-tuple visibility tests.
00357      *
00358      * Note: In hot standby, a tuple that's already visible to all
00359      * transactions in the master might still be invisible to a read-only
00360      * transaction in the standby. We partly handle this problem by tracking
00361      * the minimum xmin of visible tuples as the cut-off XID while marking a
00362      * page all-visible on master and WAL log that along with the visibility
00363      * map SET operation. In hot standby, we wait for (or abort) all
00364      * transactions that can potentially may not see one or more tuples on the
00365      * page. That's how index-only scans work fine in hot standby. A crucial
00366      * difference between index-only scans and heap scans is that the
00367      * index-only scan completely relies on the visibility map where as heap
00368      * scan looks at the page-level PD_ALL_VISIBLE flag. We are not sure if the
00369      * page-level flag can be trusted in the same way, because it might get
00370      * propagated somehow without being explicitly WAL-logged, e.g. via a full
00371      * page write. Until we can prove that beyond doubt, let's check each
00372      * tuple for visibility the hard way.
00373      */
00374     all_visible = PageIsAllVisible(dp) && !snapshot->takenDuringRecovery;
00375 
00376     for (lineoff = FirstOffsetNumber, lpp = PageGetItemId(dp, lineoff);
00377          lineoff <= lines;
00378          lineoff++, lpp++)
00379     {
00380         if (ItemIdIsNormal(lpp))
00381         {
00382             HeapTupleData loctup;
00383             bool        valid;
00384 
00385             loctup.t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
00386             loctup.t_len = ItemIdGetLength(lpp);
00387             ItemPointerSet(&(loctup.t_self), page, lineoff);
00388 
00389             if (all_visible)
00390                 valid = true;
00391             else
00392                 valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
00393 
00394             CheckForSerializableConflictOut(valid, scan->rs_rd, &loctup,
00395                                             buffer, snapshot);
00396 
00397             if (valid)
00398                 scan->rs_vistuples[ntup++] = lineoff;
00399         }
00400     }
00401 
00402     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
00403 
00404     Assert(ntup <= MaxHeapTuplesPerPage);
00405     scan->rs_ntuples = ntup;
00406 }
00407 
00408 /* ----------------
00409  *      heapgettup - fetch next heap tuple
00410  *
00411  *      Initialize the scan if not already done; then advance to the next
00412  *      tuple as indicated by "dir"; return the next tuple in scan->rs_ctup,
00413  *      or set scan->rs_ctup.t_data = NULL if no more tuples.
00414  *
00415  * dir == NoMovementScanDirection means "re-fetch the tuple indicated
00416  * by scan->rs_ctup".
00417  *
00418  * Note: the reason nkeys/key are passed separately, even though they are
00419  * kept in the scan descriptor, is that the caller may not want us to check
00420  * the scankeys.
00421  *
00422  * Note: when we fall off the end of the scan in either direction, we
00423  * reset rs_inited.  This means that a further request with the same
00424  * scan direction will restart the scan, which is a bit odd, but a
00425  * request with the opposite scan direction will start a fresh scan
00426  * in the proper direction.  The latter is required behavior for cursors,
00427  * while the former case is generally undefined behavior in Postgres
00428  * so we don't care too much.
00429  * ----------------
00430  */
00431 static void
00432 heapgettup(HeapScanDesc scan,
00433            ScanDirection dir,
00434            int nkeys,
00435            ScanKey key)
00436 {
00437     HeapTuple   tuple = &(scan->rs_ctup);
00438     Snapshot    snapshot = scan->rs_snapshot;
00439     bool        backward = ScanDirectionIsBackward(dir);
00440     BlockNumber page;
00441     bool        finished;
00442     Page        dp;
00443     int         lines;
00444     OffsetNumber lineoff;
00445     int         linesleft;
00446     ItemId      lpp;
00447 
00448     /*
00449      * calculate next starting lineoff, given scan direction
00450      */
00451     if (ScanDirectionIsForward(dir))
00452     {
00453         if (!scan->rs_inited)
00454         {
00455             /*
00456              * return null immediately if relation is empty
00457              */
00458             if (scan->rs_nblocks == 0)
00459             {
00460                 Assert(!BufferIsValid(scan->rs_cbuf));
00461                 tuple->t_data = NULL;
00462                 return;
00463             }
00464             page = scan->rs_startblock; /* first page */
00465             heapgetpage(scan, page);
00466             lineoff = FirstOffsetNumber;        /* first offnum */
00467             scan->rs_inited = true;
00468         }
00469         else
00470         {
00471             /* continue from previously returned page/tuple */
00472             page = scan->rs_cblock;     /* current page */
00473             lineoff =           /* next offnum */
00474                 OffsetNumberNext(ItemPointerGetOffsetNumber(&(tuple->t_self)));
00475         }
00476 
00477         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
00478 
00479         dp = (Page) BufferGetPage(scan->rs_cbuf);
00480         lines = PageGetMaxOffsetNumber(dp);
00481         /* page and lineoff now reference the physically next tid */
00482 
00483         linesleft = lines - lineoff + 1;
00484     }
00485     else if (backward)
00486     {
00487         if (!scan->rs_inited)
00488         {
00489             /*
00490              * return null immediately if relation is empty
00491              */
00492             if (scan->rs_nblocks == 0)
00493             {
00494                 Assert(!BufferIsValid(scan->rs_cbuf));
00495                 tuple->t_data = NULL;
00496                 return;
00497             }
00498 
00499             /*
00500              * Disable reporting to syncscan logic in a backwards scan; it's
00501              * not very likely anyone else is doing the same thing at the same
00502              * time, and much more likely that we'll just bollix things for
00503              * forward scanners.
00504              */
00505             scan->rs_syncscan = false;
00506             /* start from last page of the scan */
00507             if (scan->rs_startblock > 0)
00508                 page = scan->rs_startblock - 1;
00509             else
00510                 page = scan->rs_nblocks - 1;
00511             heapgetpage(scan, page);
00512         }
00513         else
00514         {
00515             /* continue from previously returned page/tuple */
00516             page = scan->rs_cblock;     /* current page */
00517         }
00518 
00519         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
00520 
00521         dp = (Page) BufferGetPage(scan->rs_cbuf);
00522         lines = PageGetMaxOffsetNumber(dp);
00523 
00524         if (!scan->rs_inited)
00525         {
00526             lineoff = lines;    /* final offnum */
00527             scan->rs_inited = true;
00528         }
00529         else
00530         {
00531             lineoff =           /* previous offnum */
00532                 OffsetNumberPrev(ItemPointerGetOffsetNumber(&(tuple->t_self)));
00533         }
00534         /* page and lineoff now reference the physically previous tid */
00535 
00536         linesleft = lineoff;
00537     }
00538     else
00539     {
00540         /*
00541          * ``no movement'' scan direction: refetch prior tuple
00542          */
00543         if (!scan->rs_inited)
00544         {
00545             Assert(!BufferIsValid(scan->rs_cbuf));
00546             tuple->t_data = NULL;
00547             return;
00548         }
00549 
00550         page = ItemPointerGetBlockNumber(&(tuple->t_self));
00551         if (page != scan->rs_cblock)
00552             heapgetpage(scan, page);
00553 
00554         /* Since the tuple was previously fetched, needn't lock page here */
00555         dp = (Page) BufferGetPage(scan->rs_cbuf);
00556         lineoff = ItemPointerGetOffsetNumber(&(tuple->t_self));
00557         lpp = PageGetItemId(dp, lineoff);
00558         Assert(ItemIdIsNormal(lpp));
00559 
00560         tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
00561         tuple->t_len = ItemIdGetLength(lpp);
00562 
00563         return;
00564     }
00565 
00566     /*
00567      * advance the scan until we find a qualifying tuple or run out of stuff
00568      * to scan
00569      */
00570     lpp = PageGetItemId(dp, lineoff);
00571     for (;;)
00572     {
00573         while (linesleft > 0)
00574         {
00575             if (ItemIdIsNormal(lpp))
00576             {
00577                 bool        valid;
00578 
00579                 tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
00580                 tuple->t_len = ItemIdGetLength(lpp);
00581                 ItemPointerSet(&(tuple->t_self), page, lineoff);
00582 
00583                 /*
00584                  * if current tuple qualifies, return it.
00585                  */
00586                 valid = HeapTupleSatisfiesVisibility(tuple,
00587                                                      snapshot,
00588                                                      scan->rs_cbuf);
00589 
00590                 CheckForSerializableConflictOut(valid, scan->rs_rd, tuple,
00591                                                 scan->rs_cbuf, snapshot);
00592 
00593                 if (valid && key != NULL)
00594                     HeapKeyTest(tuple, RelationGetDescr(scan->rs_rd),
00595                                 nkeys, key, valid);
00596 
00597                 if (valid)
00598                 {
00599                     LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
00600                     return;
00601                 }
00602             }
00603 
00604             /*
00605              * otherwise move to the next item on the page
00606              */
00607             --linesleft;
00608             if (backward)
00609             {
00610                 --lpp;          /* move back in this page's ItemId array */
00611                 --lineoff;
00612             }
00613             else
00614             {
00615                 ++lpp;          /* move forward in this page's ItemId array */
00616                 ++lineoff;
00617             }
00618         }
00619 
00620         /*
00621          * if we get here, it means we've exhausted the items on this page and
00622          * it's time to move to the next.
00623          */
00624         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK);
00625 
00626         /*
00627          * advance to next/prior page and detect end of scan
00628          */
00629         if (backward)
00630         {
00631             finished = (page == scan->rs_startblock);
00632             if (page == 0)
00633                 page = scan->rs_nblocks;
00634             page--;
00635         }
00636         else
00637         {
00638             page++;
00639             if (page >= scan->rs_nblocks)
00640                 page = 0;
00641             finished = (page == scan->rs_startblock);
00642 
00643             /*
00644              * Report our new scan position for synchronization purposes. We
00645              * don't do that when moving backwards, however. That would just
00646              * mess up any other forward-moving scanners.
00647              *
00648              * Note: we do this before checking for end of scan so that the
00649              * final state of the position hint is back at the start of the
00650              * rel.  That's not strictly necessary, but otherwise when you run
00651              * the same query multiple times the starting position would shift
00652              * a little bit backwards on every invocation, which is confusing.
00653              * We don't guarantee any specific ordering in general, though.
00654              */
00655             if (scan->rs_syncscan)
00656                 ss_report_location(scan->rs_rd, page);
00657         }
00658 
00659         /*
00660          * return NULL if we've exhausted all the pages
00661          */
00662         if (finished)
00663         {
00664             if (BufferIsValid(scan->rs_cbuf))
00665                 ReleaseBuffer(scan->rs_cbuf);
00666             scan->rs_cbuf = InvalidBuffer;
00667             scan->rs_cblock = InvalidBlockNumber;
00668             tuple->t_data = NULL;
00669             scan->rs_inited = false;
00670             return;
00671         }
00672 
00673         heapgetpage(scan, page);
00674 
00675         LockBuffer(scan->rs_cbuf, BUFFER_LOCK_SHARE);
00676 
00677         dp = (Page) BufferGetPage(scan->rs_cbuf);
00678         lines = PageGetMaxOffsetNumber((Page) dp);
00679         linesleft = lines;
00680         if (backward)
00681         {
00682             lineoff = lines;
00683             lpp = PageGetItemId(dp, lines);
00684         }
00685         else
00686         {
00687             lineoff = FirstOffsetNumber;
00688             lpp = PageGetItemId(dp, FirstOffsetNumber);
00689         }
00690     }
00691 }
00692 
00693 /* ----------------
00694  *      heapgettup_pagemode - fetch next heap tuple in page-at-a-time mode
00695  *
00696  *      Same API as heapgettup, but used in page-at-a-time mode
00697  *
00698  * The internal logic is much the same as heapgettup's too, but there are some
00699  * differences: we do not take the buffer content lock (that only needs to
00700  * happen inside heapgetpage), and we iterate through just the tuples listed
00701  * in rs_vistuples[] rather than all tuples on the page.  Notice that
00702  * lineindex is 0-based, where the corresponding loop variable lineoff in
00703  * heapgettup is 1-based.
00704  * ----------------
00705  */
00706 static void
00707 heapgettup_pagemode(HeapScanDesc scan,
00708                     ScanDirection dir,
00709                     int nkeys,
00710                     ScanKey key)
00711 {
00712     HeapTuple   tuple = &(scan->rs_ctup);
00713     bool        backward = ScanDirectionIsBackward(dir);
00714     BlockNumber page;
00715     bool        finished;
00716     Page        dp;
00717     int         lines;
00718     int         lineindex;
00719     OffsetNumber lineoff;
00720     int         linesleft;
00721     ItemId      lpp;
00722 
00723     /*
00724      * calculate next starting lineindex, given scan direction
00725      */
00726     if (ScanDirectionIsForward(dir))
00727     {
00728         if (!scan->rs_inited)
00729         {
00730             /*
00731              * return null immediately if relation is empty
00732              */
00733             if (scan->rs_nblocks == 0)
00734             {
00735                 Assert(!BufferIsValid(scan->rs_cbuf));
00736                 tuple->t_data = NULL;
00737                 return;
00738             }
00739             page = scan->rs_startblock; /* first page */
00740             heapgetpage(scan, page);
00741             lineindex = 0;
00742             scan->rs_inited = true;
00743         }
00744         else
00745         {
00746             /* continue from previously returned page/tuple */
00747             page = scan->rs_cblock;     /* current page */
00748             lineindex = scan->rs_cindex + 1;
00749         }
00750 
00751         dp = (Page) BufferGetPage(scan->rs_cbuf);
00752         lines = scan->rs_ntuples;
00753         /* page and lineindex now reference the next visible tid */
00754 
00755         linesleft = lines - lineindex;
00756     }
00757     else if (backward)
00758     {
00759         if (!scan->rs_inited)
00760         {
00761             /*
00762              * return null immediately if relation is empty
00763              */
00764             if (scan->rs_nblocks == 0)
00765             {
00766                 Assert(!BufferIsValid(scan->rs_cbuf));
00767                 tuple->t_data = NULL;
00768                 return;
00769             }
00770 
00771             /*
00772              * Disable reporting to syncscan logic in a backwards scan; it's
00773              * not very likely anyone else is doing the same thing at the same
00774              * time, and much more likely that we'll just bollix things for
00775              * forward scanners.
00776              */
00777             scan->rs_syncscan = false;
00778             /* start from last page of the scan */
00779             if (scan->rs_startblock > 0)
00780                 page = scan->rs_startblock - 1;
00781             else
00782                 page = scan->rs_nblocks - 1;
00783             heapgetpage(scan, page);
00784         }
00785         else
00786         {
00787             /* continue from previously returned page/tuple */
00788             page = scan->rs_cblock;     /* current page */
00789         }
00790 
00791         dp = (Page) BufferGetPage(scan->rs_cbuf);
00792         lines = scan->rs_ntuples;
00793 
00794         if (!scan->rs_inited)
00795         {
00796             lineindex = lines - 1;
00797             scan->rs_inited = true;
00798         }
00799         else
00800         {
00801             lineindex = scan->rs_cindex - 1;
00802         }
00803         /* page and lineindex now reference the previous visible tid */
00804 
00805         linesleft = lineindex + 1;
00806     }
00807     else
00808     {
00809         /*
00810          * ``no movement'' scan direction: refetch prior tuple
00811          */
00812         if (!scan->rs_inited)
00813         {
00814             Assert(!BufferIsValid(scan->rs_cbuf));
00815             tuple->t_data = NULL;
00816             return;
00817         }
00818 
00819         page = ItemPointerGetBlockNumber(&(tuple->t_self));
00820         if (page != scan->rs_cblock)
00821             heapgetpage(scan, page);
00822 
00823         /* Since the tuple was previously fetched, needn't lock page here */
00824         dp = (Page) BufferGetPage(scan->rs_cbuf);
00825         lineoff = ItemPointerGetOffsetNumber(&(tuple->t_self));
00826         lpp = PageGetItemId(dp, lineoff);
00827         Assert(ItemIdIsNormal(lpp));
00828 
00829         tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
00830         tuple->t_len = ItemIdGetLength(lpp);
00831 
00832         /* check that rs_cindex is in sync */
00833         Assert(scan->rs_cindex < scan->rs_ntuples);
00834         Assert(lineoff == scan->rs_vistuples[scan->rs_cindex]);
00835 
00836         return;
00837     }
00838 
00839     /*
00840      * advance the scan until we find a qualifying tuple or run out of stuff
00841      * to scan
00842      */
00843     for (;;)
00844     {
00845         while (linesleft > 0)
00846         {
00847             lineoff = scan->rs_vistuples[lineindex];
00848             lpp = PageGetItemId(dp, lineoff);
00849             Assert(ItemIdIsNormal(lpp));
00850 
00851             tuple->t_data = (HeapTupleHeader) PageGetItem((Page) dp, lpp);
00852             tuple->t_len = ItemIdGetLength(lpp);
00853             ItemPointerSet(&(tuple->t_self), page, lineoff);
00854 
00855             /*
00856              * if current tuple qualifies, return it.
00857              */
00858             if (key != NULL)
00859             {
00860                 bool        valid;
00861 
00862                 HeapKeyTest(tuple, RelationGetDescr(scan->rs_rd),
00863                             nkeys, key, valid);
00864                 if (valid)
00865                 {
00866                     scan->rs_cindex = lineindex;
00867                     return;
00868                 }
00869             }
00870             else
00871             {
00872                 scan->rs_cindex = lineindex;
00873                 return;
00874             }
00875 
00876             /*
00877              * otherwise move to the next item on the page
00878              */
00879             --linesleft;
00880             if (backward)
00881                 --lineindex;
00882             else
00883                 ++lineindex;
00884         }
00885 
00886         /*
00887          * if we get here, it means we've exhausted the items on this page and
00888          * it's time to move to the next.
00889          */
00890         if (backward)
00891         {
00892             finished = (page == scan->rs_startblock);
00893             if (page == 0)
00894                 page = scan->rs_nblocks;
00895             page--;
00896         }
00897         else
00898         {
00899             page++;
00900             if (page >= scan->rs_nblocks)
00901                 page = 0;
00902             finished = (page == scan->rs_startblock);
00903 
00904             /*
00905              * Report our new scan position for synchronization purposes. We
00906              * don't do that when moving backwards, however. That would just
00907              * mess up any other forward-moving scanners.
00908              *
00909              * Note: we do this before checking for end of scan so that the
00910              * final state of the position hint is back at the start of the
00911              * rel.  That's not strictly necessary, but otherwise when you run
00912              * the same query multiple times the starting position would shift
00913              * a little bit backwards on every invocation, which is confusing.
00914              * We don't guarantee any specific ordering in general, though.
00915              */
00916             if (scan->rs_syncscan)
00917                 ss_report_location(scan->rs_rd, page);
00918         }
00919 
00920         /*
00921          * return NULL if we've exhausted all the pages
00922          */
00923         if (finished)
00924         {
00925             if (BufferIsValid(scan->rs_cbuf))
00926                 ReleaseBuffer(scan->rs_cbuf);
00927             scan->rs_cbuf = InvalidBuffer;
00928             scan->rs_cblock = InvalidBlockNumber;
00929             tuple->t_data = NULL;
00930             scan->rs_inited = false;
00931             return;
00932         }
00933 
00934         heapgetpage(scan, page);
00935 
00936         dp = (Page) BufferGetPage(scan->rs_cbuf);
00937         lines = scan->rs_ntuples;
00938         linesleft = lines;
00939         if (backward)
00940             lineindex = lines - 1;
00941         else
00942             lineindex = 0;
00943     }
00944 }
00945 
00946 
00947 #if defined(DISABLE_COMPLEX_MACRO)
00948 /*
00949  * This is formatted so oddly so that the correspondence to the macro
00950  * definition in access/htup.h is maintained.
00951  */
00952 Datum
00953 fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
00954             bool *isnull)
00955 {
00956     return (
00957             (attnum) > 0 ?
00958             (
00959              (*(isnull) = false),
00960              HeapTupleNoNulls(tup) ?
00961              (
00962               (tupleDesc)->attrs[(attnum) - 1]->attcacheoff >= 0 ?
00963               (
00964                fetchatt((tupleDesc)->attrs[(attnum) - 1],
00965                         (char *) (tup)->t_data + (tup)->t_data->t_hoff +
00966                         (tupleDesc)->attrs[(attnum) - 1]->attcacheoff)
00967                )
00968               :
00969               nocachegetattr((tup), (attnum), (tupleDesc))
00970               )
00971              :
00972              (
00973               att_isnull((attnum) - 1, (tup)->t_data->t_bits) ?
00974               (
00975                (*(isnull) = true),
00976                (Datum) NULL
00977                )
00978               :
00979               (
00980                nocachegetattr((tup), (attnum), (tupleDesc))
00981                )
00982               )
00983              )
00984             :
00985             (
00986              (Datum) NULL
00987              )
00988         );
00989 }
00990 #endif   /* defined(DISABLE_COMPLEX_MACRO) */
00991 
00992 
00993 /* ----------------------------------------------------------------
00994  *                   heap access method interface
00995  * ----------------------------------------------------------------
00996  */
00997 
00998 /* ----------------
00999  *      relation_open - open any relation by relation OID
01000  *
01001  *      If lockmode is not "NoLock", the specified kind of lock is
01002  *      obtained on the relation.  (Generally, NoLock should only be
01003  *      used if the caller knows it has some appropriate lock on the
01004  *      relation already.)
01005  *
01006  *      An error is raised if the relation does not exist.
01007  *
01008  *      NB: a "relation" is anything with a pg_class entry.  The caller is
01009  *      expected to check whether the relkind is something it can handle.
01010  * ----------------
01011  */
01012 Relation
01013 relation_open(Oid relationId, LOCKMODE lockmode)
01014 {
01015     Relation    r;
01016 
01017     Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
01018 
01019     /* Get the lock before trying to open the relcache entry */
01020     if (lockmode != NoLock)
01021         LockRelationOid(relationId, lockmode);
01022 
01023     /* The relcache does all the real work... */
01024     r = RelationIdGetRelation(relationId);
01025 
01026     if (!RelationIsValid(r))
01027         elog(ERROR, "could not open relation with OID %u", relationId);
01028 
01029     /* Make note that we've accessed a temporary relation */
01030     if (RelationUsesLocalBuffers(r))
01031         MyXactAccessedTempRel = true;
01032 
01033     pgstat_initstats(r);
01034 
01035     return r;
01036 }
01037 
01038 /* ----------------
01039  *      try_relation_open - open any relation by relation OID
01040  *
01041  *      Same as relation_open, except return NULL instead of failing
01042  *      if the relation does not exist.
01043  * ----------------
01044  */
01045 Relation
01046 try_relation_open(Oid relationId, LOCKMODE lockmode)
01047 {
01048     Relation    r;
01049 
01050     Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
01051 
01052     /* Get the lock first */
01053     if (lockmode != NoLock)
01054         LockRelationOid(relationId, lockmode);
01055 
01056     /*
01057      * Now that we have the lock, probe to see if the relation really exists
01058      * or not.
01059      */
01060     if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relationId)))
01061     {
01062         /* Release useless lock */
01063         if (lockmode != NoLock)
01064             UnlockRelationOid(relationId, lockmode);
01065 
01066         return NULL;
01067     }
01068 
01069     /* Should be safe to do a relcache load */
01070     r = RelationIdGetRelation(relationId);
01071 
01072     if (!RelationIsValid(r))
01073         elog(ERROR, "could not open relation with OID %u", relationId);
01074 
01075     /* Make note that we've accessed a temporary relation */
01076     if (RelationUsesLocalBuffers(r))
01077         MyXactAccessedTempRel = true;
01078 
01079     pgstat_initstats(r);
01080 
01081     return r;
01082 }
01083 
01084 /* ----------------
01085  *      relation_openrv - open any relation specified by a RangeVar
01086  *
01087  *      Same as relation_open, but the relation is specified by a RangeVar.
01088  * ----------------
01089  */
01090 Relation
01091 relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
01092 {
01093     Oid         relOid;
01094 
01095     /*
01096      * Check for shared-cache-inval messages before trying to open the
01097      * relation.  This is needed even if we already hold a lock on the
01098      * relation, because GRANT/REVOKE are executed without taking any lock on
01099      * the target relation, and we want to be sure we see current ACL
01100      * information.  We can skip this if asked for NoLock, on the assumption
01101      * that such a call is not the first one in the current command, and so we
01102      * should be reasonably up-to-date already.  (XXX this all could stand to
01103      * be redesigned, but for the moment we'll keep doing this like it's been
01104      * done historically.)
01105      */
01106     if (lockmode != NoLock)
01107         AcceptInvalidationMessages();
01108 
01109     /* Look up and lock the appropriate relation using namespace search */
01110     relOid = RangeVarGetRelid(relation, lockmode, false);
01111 
01112     /* Let relation_open do the rest */
01113     return relation_open(relOid, NoLock);
01114 }
01115 
01116 /* ----------------
01117  *      relation_openrv_extended - open any relation specified by a RangeVar
01118  *
01119  *      Same as relation_openrv, but with an additional missing_ok argument
01120  *      allowing a NULL return rather than an error if the relation is not
01121  *      found.  (Note that some other causes, such as permissions problems,
01122  *      will still result in an ereport.)
01123  * ----------------
01124  */
01125 Relation
01126 relation_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
01127                          bool missing_ok)
01128 {
01129     Oid         relOid;
01130 
01131     /*
01132      * Check for shared-cache-inval messages before trying to open the
01133      * relation.  See comments in relation_openrv().
01134      */
01135     if (lockmode != NoLock)
01136         AcceptInvalidationMessages();
01137 
01138     /* Look up and lock the appropriate relation using namespace search */
01139     relOid = RangeVarGetRelid(relation, lockmode, missing_ok);
01140 
01141     /* Return NULL on not-found */
01142     if (!OidIsValid(relOid))
01143         return NULL;
01144 
01145     /* Let relation_open do the rest */
01146     return relation_open(relOid, NoLock);
01147 }
01148 
01149 /* ----------------
01150  *      relation_close - close any relation
01151  *
01152  *      If lockmode is not "NoLock", we then release the specified lock.
01153  *
01154  *      Note that it is often sensible to hold a lock beyond relation_close;
01155  *      in that case, the lock is released automatically at xact end.
01156  * ----------------
01157  */
01158 void
01159 relation_close(Relation relation, LOCKMODE lockmode)
01160 {
01161     LockRelId   relid = relation->rd_lockInfo.lockRelId;
01162 
01163     Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
01164 
01165     /* The relcache does the real work... */
01166     RelationClose(relation);
01167 
01168     if (lockmode != NoLock)
01169         UnlockRelationId(&relid, lockmode);
01170 }
01171 
01172 
01173 /* ----------------
01174  *      heap_open - open a heap relation by relation OID
01175  *
01176  *      This is essentially relation_open plus check that the relation
01177  *      is not an index nor a composite type.  (The caller should also
01178  *      check that it's not a view or foreign table before assuming it has
01179  *      storage.)
01180  * ----------------
01181  */
01182 Relation
01183 heap_open(Oid relationId, LOCKMODE lockmode)
01184 {
01185     Relation    r;
01186 
01187     r = relation_open(relationId, lockmode);
01188 
01189     if (r->rd_rel->relkind == RELKIND_INDEX)
01190         ereport(ERROR,
01191                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01192                  errmsg("\"%s\" is an index",
01193                         RelationGetRelationName(r))));
01194     else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
01195         ereport(ERROR,
01196                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01197                  errmsg("\"%s\" is a composite type",
01198                         RelationGetRelationName(r))));
01199 
01200     return r;
01201 }
01202 
01203 /* ----------------
01204  *      heap_openrv - open a heap relation specified
01205  *      by a RangeVar node
01206  *
01207  *      As above, but relation is specified by a RangeVar.
01208  * ----------------
01209  */
01210 Relation
01211 heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
01212 {
01213     Relation    r;
01214 
01215     r = relation_openrv(relation, lockmode);
01216 
01217     if (r->rd_rel->relkind == RELKIND_INDEX)
01218         ereport(ERROR,
01219                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01220                  errmsg("\"%s\" is an index",
01221                         RelationGetRelationName(r))));
01222     else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
01223         ereport(ERROR,
01224                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01225                  errmsg("\"%s\" is a composite type",
01226                         RelationGetRelationName(r))));
01227 
01228     return r;
01229 }
01230 
01231 /* ----------------
01232  *      heap_openrv_extended - open a heap relation specified
01233  *      by a RangeVar node
01234  *
01235  *      As above, but optionally return NULL instead of failing for
01236  *      relation-not-found.
01237  * ----------------
01238  */
01239 Relation
01240 heap_openrv_extended(const RangeVar *relation, LOCKMODE lockmode,
01241                      bool missing_ok)
01242 {
01243     Relation    r;
01244 
01245     r = relation_openrv_extended(relation, lockmode, missing_ok);
01246 
01247     if (r)
01248     {
01249         if (r->rd_rel->relkind == RELKIND_INDEX)
01250             ereport(ERROR,
01251                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01252                      errmsg("\"%s\" is an index",
01253                             RelationGetRelationName(r))));
01254         else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
01255             ereport(ERROR,
01256                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01257                      errmsg("\"%s\" is a composite type",
01258                             RelationGetRelationName(r))));
01259     }
01260 
01261     return r;
01262 }
01263 
01264 
01265 /* ----------------
01266  *      heap_beginscan  - begin relation scan
01267  *
01268  * heap_beginscan_strat offers an extended API that lets the caller control
01269  * whether a nondefault buffer access strategy can be used, and whether
01270  * syncscan can be chosen (possibly resulting in the scan not starting from
01271  * block zero).  Both of these default to TRUE with plain heap_beginscan.
01272  *
01273  * heap_beginscan_bm is an alternative entry point for setting up a
01274  * HeapScanDesc for a bitmap heap scan.  Although that scan technology is
01275  * really quite unlike a standard seqscan, there is just enough commonality
01276  * to make it worth using the same data structure.
01277  * ----------------
01278  */
01279 HeapScanDesc
01280 heap_beginscan(Relation relation, Snapshot snapshot,
01281                int nkeys, ScanKey key)
01282 {
01283     return heap_beginscan_internal(relation, snapshot, nkeys, key,
01284                                    true, true, false);
01285 }
01286 
01287 HeapScanDesc
01288 heap_beginscan_strat(Relation relation, Snapshot snapshot,
01289                      int nkeys, ScanKey key,
01290                      bool allow_strat, bool allow_sync)
01291 {
01292     return heap_beginscan_internal(relation, snapshot, nkeys, key,
01293                                    allow_strat, allow_sync, false);
01294 }
01295 
01296 HeapScanDesc
01297 heap_beginscan_bm(Relation relation, Snapshot snapshot,
01298                   int nkeys, ScanKey key)
01299 {
01300     return heap_beginscan_internal(relation, snapshot, nkeys, key,
01301                                    false, false, true);
01302 }
01303 
01304 static HeapScanDesc
01305 heap_beginscan_internal(Relation relation, Snapshot snapshot,
01306                         int nkeys, ScanKey key,
01307                         bool allow_strat, bool allow_sync,
01308                         bool is_bitmapscan)
01309 {
01310     HeapScanDesc scan;
01311 
01312     /*
01313      * increment relation ref count while scanning relation
01314      *
01315      * This is just to make really sure the relcache entry won't go away while
01316      * the scan has a pointer to it.  Caller should be holding the rel open
01317      * anyway, so this is redundant in all normal scenarios...
01318      */
01319     RelationIncrementReferenceCount(relation);
01320 
01321     /*
01322      * allocate and initialize scan descriptor
01323      */
01324     scan = (HeapScanDesc) palloc(sizeof(HeapScanDescData));
01325 
01326     scan->rs_rd = relation;
01327     scan->rs_snapshot = snapshot;
01328     scan->rs_nkeys = nkeys;
01329     scan->rs_bitmapscan = is_bitmapscan;
01330     scan->rs_strategy = NULL;   /* set in initscan */
01331     scan->rs_allow_strat = allow_strat;
01332     scan->rs_allow_sync = allow_sync;
01333 
01334     /*
01335      * we can use page-at-a-time mode if it's an MVCC-safe snapshot
01336      */
01337     scan->rs_pageatatime = IsMVCCSnapshot(snapshot);
01338 
01339     /*
01340      * For a seqscan in a serializable transaction, acquire a predicate lock
01341      * on the entire relation. This is required not only to lock all the
01342      * matching tuples, but also to conflict with new insertions into the
01343      * table. In an indexscan, we take page locks on the index pages covering
01344      * the range specified in the scan qual, but in a heap scan there is
01345      * nothing more fine-grained to lock. A bitmap scan is a different story,
01346      * there we have already scanned the index and locked the index pages
01347      * covering the predicate. But in that case we still have to lock any
01348      * matching heap tuples.
01349      */
01350     if (!is_bitmapscan)
01351         PredicateLockRelation(relation, snapshot);
01352 
01353     /* we only need to set this up once */
01354     scan->rs_ctup.t_tableOid = RelationGetRelid(relation);
01355 
01356     /*
01357      * we do this here instead of in initscan() because heap_rescan also calls
01358      * initscan() and we don't want to allocate memory again
01359      */
01360     if (nkeys > 0)
01361         scan->rs_key = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
01362     else
01363         scan->rs_key = NULL;
01364 
01365     initscan(scan, key, false);
01366 
01367     return scan;
01368 }
01369 
01370 /* ----------------
01371  *      heap_rescan     - restart a relation scan
01372  * ----------------
01373  */
01374 void
01375 heap_rescan(HeapScanDesc scan,
01376             ScanKey key)
01377 {
01378     /*
01379      * unpin scan buffers
01380      */
01381     if (BufferIsValid(scan->rs_cbuf))
01382         ReleaseBuffer(scan->rs_cbuf);
01383 
01384     /*
01385      * reinitialize scan descriptor
01386      */
01387     initscan(scan, key, true);
01388 }
01389 
01390 /* ----------------
01391  *      heap_endscan    - end relation scan
01392  *
01393  *      See how to integrate with index scans.
01394  *      Check handling if reldesc caching.
01395  * ----------------
01396  */
01397 void
01398 heap_endscan(HeapScanDesc scan)
01399 {
01400     /* Note: no locking manipulations needed */
01401 
01402     /*
01403      * unpin scan buffers
01404      */
01405     if (BufferIsValid(scan->rs_cbuf))
01406         ReleaseBuffer(scan->rs_cbuf);
01407 
01408     /*
01409      * decrement relation reference count and free scan descriptor storage
01410      */
01411     RelationDecrementReferenceCount(scan->rs_rd);
01412 
01413     if (scan->rs_key)
01414         pfree(scan->rs_key);
01415 
01416     if (scan->rs_strategy != NULL)
01417         FreeAccessStrategy(scan->rs_strategy);
01418 
01419     pfree(scan);
01420 }
01421 
01422 /* ----------------
01423  *      heap_getnext    - retrieve next tuple in scan
01424  *
01425  *      Fix to work with index relations.
01426  *      We don't return the buffer anymore, but you can get it from the
01427  *      returned HeapTuple.
01428  * ----------------
01429  */
01430 
01431 #ifdef HEAPDEBUGALL
01432 #define HEAPDEBUG_1 \
01433     elog(DEBUG2, "heap_getnext([%s,nkeys=%d],dir=%d) called", \
01434          RelationGetRelationName(scan->rs_rd), scan->rs_nkeys, (int) direction)
01435 #define HEAPDEBUG_2 \
01436     elog(DEBUG2, "heap_getnext returning EOS")
01437 #define HEAPDEBUG_3 \
01438     elog(DEBUG2, "heap_getnext returning tuple")
01439 #else
01440 #define HEAPDEBUG_1
01441 #define HEAPDEBUG_2
01442 #define HEAPDEBUG_3
01443 #endif   /* !defined(HEAPDEBUGALL) */
01444 
01445 
01446 HeapTuple
01447 heap_getnext(HeapScanDesc scan, ScanDirection direction)
01448 {
01449     /* Note: no locking manipulations needed */
01450 
01451     HEAPDEBUG_1;                /* heap_getnext( info ) */
01452 
01453     if (scan->rs_pageatatime)
01454         heapgettup_pagemode(scan, direction,
01455                             scan->rs_nkeys, scan->rs_key);
01456     else
01457         heapgettup(scan, direction, scan->rs_nkeys, scan->rs_key);
01458 
01459     if (scan->rs_ctup.t_data == NULL)
01460     {
01461         HEAPDEBUG_2;            /* heap_getnext returning EOS */
01462         return NULL;
01463     }
01464 
01465     /*
01466      * if we get here it means we have a new current scan tuple, so point to
01467      * the proper return buffer and return the tuple.
01468      */
01469     HEAPDEBUG_3;                /* heap_getnext returning tuple */
01470 
01471     pgstat_count_heap_getnext(scan->rs_rd);
01472 
01473     return &(scan->rs_ctup);
01474 }
01475 
01476 /*
01477  *  heap_fetch      - retrieve tuple with given tid
01478  *
01479  * On entry, tuple->t_self is the TID to fetch.  We pin the buffer holding
01480  * the tuple, fill in the remaining fields of *tuple, and check the tuple
01481  * against the specified snapshot.
01482  *
01483  * If successful (tuple found and passes snapshot time qual), then *userbuf
01484  * is set to the buffer holding the tuple and TRUE is returned.  The caller
01485  * must unpin the buffer when done with the tuple.
01486  *
01487  * If the tuple is not found (ie, item number references a deleted slot),
01488  * then tuple->t_data is set to NULL and FALSE is returned.
01489  *
01490  * If the tuple is found but fails the time qual check, then FALSE is returned
01491  * but tuple->t_data is left pointing to the tuple.
01492  *
01493  * keep_buf determines what is done with the buffer in the FALSE-result cases.
01494  * When the caller specifies keep_buf = true, we retain the pin on the buffer
01495  * and return it in *userbuf (so the caller must eventually unpin it); when
01496  * keep_buf = false, the pin is released and *userbuf is set to InvalidBuffer.
01497  *
01498  * stats_relation is the relation to charge the heap_fetch operation against
01499  * for statistical purposes.  (This could be the heap rel itself, an
01500  * associated index, or NULL to not count the fetch at all.)
01501  *
01502  * heap_fetch does not follow HOT chains: only the exact TID requested will
01503  * be fetched.
01504  *
01505  * It is somewhat inconsistent that we ereport() on invalid block number but
01506  * return false on invalid item number.  There are a couple of reasons though.
01507  * One is that the caller can relatively easily check the block number for
01508  * validity, but cannot check the item number without reading the page
01509  * himself.  Another is that when we are following a t_ctid link, we can be
01510  * reasonably confident that the page number is valid (since VACUUM shouldn't
01511  * truncate off the destination page without having killed the referencing
01512  * tuple first), but the item number might well not be good.
01513  */
01514 bool
01515 heap_fetch(Relation relation,
01516            Snapshot snapshot,
01517            HeapTuple tuple,
01518            Buffer *userbuf,
01519            bool keep_buf,
01520            Relation stats_relation)
01521 {
01522     ItemPointer tid = &(tuple->t_self);
01523     ItemId      lp;
01524     Buffer      buffer;
01525     Page        page;
01526     OffsetNumber offnum;
01527     bool        valid;
01528 
01529     /*
01530      * Fetch and pin the appropriate page of the relation.
01531      */
01532     buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
01533 
01534     /*
01535      * Need share lock on buffer to examine tuple commit status.
01536      */
01537     LockBuffer(buffer, BUFFER_LOCK_SHARE);
01538     page = BufferGetPage(buffer);
01539 
01540     /*
01541      * We'd better check for out-of-range offnum in case of VACUUM since the
01542      * TID was obtained.
01543      */
01544     offnum = ItemPointerGetOffsetNumber(tid);
01545     if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
01546     {
01547         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
01548         if (keep_buf)
01549             *userbuf = buffer;
01550         else
01551         {
01552             ReleaseBuffer(buffer);
01553             *userbuf = InvalidBuffer;
01554         }
01555         tuple->t_data = NULL;
01556         return false;
01557     }
01558 
01559     /*
01560      * get the item line pointer corresponding to the requested tid
01561      */
01562     lp = PageGetItemId(page, offnum);
01563 
01564     /*
01565      * Must check for deleted tuple.
01566      */
01567     if (!ItemIdIsNormal(lp))
01568     {
01569         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
01570         if (keep_buf)
01571             *userbuf = buffer;
01572         else
01573         {
01574             ReleaseBuffer(buffer);
01575             *userbuf = InvalidBuffer;
01576         }
01577         tuple->t_data = NULL;
01578         return false;
01579     }
01580 
01581     /*
01582      * fill in *tuple fields
01583      */
01584     tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
01585     tuple->t_len = ItemIdGetLength(lp);
01586     tuple->t_tableOid = RelationGetRelid(relation);
01587 
01588     /*
01589      * check time qualification of tuple, then release lock
01590      */
01591     valid = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer);
01592 
01593     if (valid)
01594         PredicateLockTuple(relation, tuple, snapshot);
01595 
01596     CheckForSerializableConflictOut(valid, relation, tuple, buffer, snapshot);
01597 
01598     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
01599 
01600     if (valid)
01601     {
01602         /*
01603          * All checks passed, so return the tuple as valid. Caller is now
01604          * responsible for releasing the buffer.
01605          */
01606         *userbuf = buffer;
01607 
01608         /* Count the successful fetch against appropriate rel, if any */
01609         if (stats_relation != NULL)
01610             pgstat_count_heap_fetch(stats_relation);
01611 
01612         return true;
01613     }
01614 
01615     /* Tuple failed time qual, but maybe caller wants to see it anyway. */
01616     if (keep_buf)
01617         *userbuf = buffer;
01618     else
01619     {
01620         ReleaseBuffer(buffer);
01621         *userbuf = InvalidBuffer;
01622     }
01623 
01624     return false;
01625 }
01626 
01627 /*
01628  *  heap_hot_search_buffer  - search HOT chain for tuple satisfying snapshot
01629  *
01630  * On entry, *tid is the TID of a tuple (either a simple tuple, or the root
01631  * of a HOT chain), and buffer is the buffer holding this tuple.  We search
01632  * for the first chain member satisfying the given snapshot.  If one is
01633  * found, we update *tid to reference that tuple's offset number, and
01634  * return TRUE.  If no match, return FALSE without modifying *tid.
01635  *
01636  * heapTuple is a caller-supplied buffer.  When a match is found, we return
01637  * the tuple here, in addition to updating *tid.  If no match is found, the
01638  * contents of this buffer on return are undefined.
01639  *
01640  * If all_dead is not NULL, we check non-visible tuples to see if they are
01641  * globally dead; *all_dead is set TRUE if all members of the HOT chain
01642  * are vacuumable, FALSE if not.
01643  *
01644  * Unlike heap_fetch, the caller must already have pin and (at least) share
01645  * lock on the buffer; it is still pinned/locked at exit.  Also unlike
01646  * heap_fetch, we do not report any pgstats count; caller may do so if wanted.
01647  */
01648 bool
01649 heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer,
01650                        Snapshot snapshot, HeapTuple heapTuple,
01651                        bool *all_dead, bool first_call)
01652 {
01653     Page        dp = (Page) BufferGetPage(buffer);
01654     TransactionId prev_xmax = InvalidTransactionId;
01655     OffsetNumber offnum;
01656     bool        at_chain_start;
01657     bool        valid;
01658     bool        skip;
01659 
01660     /* If this is not the first call, previous call returned a (live!) tuple */
01661     if (all_dead)
01662         *all_dead = first_call;
01663 
01664     Assert(TransactionIdIsValid(RecentGlobalXmin));
01665 
01666     Assert(ItemPointerGetBlockNumber(tid) == BufferGetBlockNumber(buffer));
01667     offnum = ItemPointerGetOffsetNumber(tid);
01668     at_chain_start = first_call;
01669     skip = !first_call;
01670 
01671     /* Scan through possible multiple members of HOT-chain */
01672     for (;;)
01673     {
01674         ItemId      lp;
01675 
01676         /* check for bogus TID */
01677         if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(dp))
01678             break;
01679 
01680         lp = PageGetItemId(dp, offnum);
01681 
01682         /* check for unused, dead, or redirected items */
01683         if (!ItemIdIsNormal(lp))
01684         {
01685             /* We should only see a redirect at start of chain */
01686             if (ItemIdIsRedirected(lp) && at_chain_start)
01687             {
01688                 /* Follow the redirect */
01689                 offnum = ItemIdGetRedirect(lp);
01690                 at_chain_start = false;
01691                 continue;
01692             }
01693             /* else must be end of chain */
01694             break;
01695         }
01696 
01697         heapTuple->t_data = (HeapTupleHeader) PageGetItem(dp, lp);
01698         heapTuple->t_len = ItemIdGetLength(lp);
01699         heapTuple->t_tableOid = relation->rd_id;
01700         heapTuple->t_self = *tid;
01701 
01702         /*
01703          * Shouldn't see a HEAP_ONLY tuple at chain start.
01704          */
01705         if (at_chain_start && HeapTupleIsHeapOnly(heapTuple))
01706             break;
01707 
01708         /*
01709          * The xmin should match the previous xmax value, else chain is
01710          * broken.
01711          */
01712         if (TransactionIdIsValid(prev_xmax) &&
01713             !TransactionIdEquals(prev_xmax,
01714                                  HeapTupleHeaderGetXmin(heapTuple->t_data)))
01715             break;
01716 
01717         /*
01718          * When first_call is true (and thus, skip is initially false) we'll
01719          * return the first tuple we find.  But on later passes, heapTuple
01720          * will initially be pointing to the tuple we returned last time.
01721          * Returning it again would be incorrect (and would loop forever), so
01722          * we skip it and return the next match we find.
01723          */
01724         if (!skip)
01725         {
01726             /* If it's visible per the snapshot, we must return it */
01727             valid = HeapTupleSatisfiesVisibility(heapTuple, snapshot, buffer);
01728             CheckForSerializableConflictOut(valid, relation, heapTuple,
01729                                             buffer, snapshot);
01730             if (valid)
01731             {
01732                 ItemPointerSetOffsetNumber(tid, offnum);
01733                 PredicateLockTuple(relation, heapTuple, snapshot);
01734                 if (all_dead)
01735                     *all_dead = false;
01736                 return true;
01737             }
01738         }
01739         skip = false;
01740 
01741         /*
01742          * If we can't see it, maybe no one else can either.  At caller
01743          * request, check whether all chain members are dead to all
01744          * transactions.
01745          */
01746         if (all_dead && *all_dead &&
01747             !HeapTupleIsSurelyDead(heapTuple->t_data, RecentGlobalXmin))
01748             *all_dead = false;
01749 
01750         /*
01751          * Check to see if HOT chain continues past this tuple; if so fetch
01752          * the next offnum and loop around.
01753          */
01754         if (HeapTupleIsHotUpdated(heapTuple))
01755         {
01756             Assert(ItemPointerGetBlockNumber(&heapTuple->t_data->t_ctid) ==
01757                    ItemPointerGetBlockNumber(tid));
01758             offnum = ItemPointerGetOffsetNumber(&heapTuple->t_data->t_ctid);
01759             at_chain_start = false;
01760             prev_xmax = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
01761         }
01762         else
01763             break;              /* end of chain */
01764     }
01765 
01766     return false;
01767 }
01768 
01769 /*
01770  *  heap_hot_search     - search HOT chain for tuple satisfying snapshot
01771  *
01772  * This has the same API as heap_hot_search_buffer, except that the caller
01773  * does not provide the buffer containing the page, rather we access it
01774  * locally.
01775  */
01776 bool
01777 heap_hot_search(ItemPointer tid, Relation relation, Snapshot snapshot,
01778                 bool *all_dead)
01779 {
01780     bool        result;
01781     Buffer      buffer;
01782     HeapTupleData heapTuple;
01783 
01784     buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
01785     LockBuffer(buffer, BUFFER_LOCK_SHARE);
01786     result = heap_hot_search_buffer(tid, relation, buffer, snapshot,
01787                                     &heapTuple, all_dead, true);
01788     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
01789     ReleaseBuffer(buffer);
01790     return result;
01791 }
01792 
01793 /*
01794  *  heap_get_latest_tid -  get the latest tid of a specified tuple
01795  *
01796  * Actually, this gets the latest version that is visible according to
01797  * the passed snapshot.  You can pass SnapshotDirty to get the very latest,
01798  * possibly uncommitted version.
01799  *
01800  * *tid is both an input and an output parameter: it is updated to
01801  * show the latest version of the row.  Note that it will not be changed
01802  * if no version of the row passes the snapshot test.
01803  */
01804 void
01805 heap_get_latest_tid(Relation relation,
01806                     Snapshot snapshot,
01807                     ItemPointer tid)
01808 {
01809     BlockNumber blk;
01810     ItemPointerData ctid;
01811     TransactionId priorXmax;
01812 
01813     /* this is to avoid Assert failures on bad input */
01814     if (!ItemPointerIsValid(tid))
01815         return;
01816 
01817     /*
01818      * Since this can be called with user-supplied TID, don't trust the input
01819      * too much.  (RelationGetNumberOfBlocks is an expensive check, so we
01820      * don't check t_ctid links again this way.  Note that it would not do to
01821      * call it just once and save the result, either.)
01822      */
01823     blk = ItemPointerGetBlockNumber(tid);
01824     if (blk >= RelationGetNumberOfBlocks(relation))
01825         elog(ERROR, "block number %u is out of range for relation \"%s\"",
01826              blk, RelationGetRelationName(relation));
01827 
01828     /*
01829      * Loop to chase down t_ctid links.  At top of loop, ctid is the tuple we
01830      * need to examine, and *tid is the TID we will return if ctid turns out
01831      * to be bogus.
01832      *
01833      * Note that we will loop until we reach the end of the t_ctid chain.
01834      * Depending on the snapshot passed, there might be at most one visible
01835      * version of the row, but we don't try to optimize for that.
01836      */
01837     ctid = *tid;
01838     priorXmax = InvalidTransactionId;   /* cannot check first XMIN */
01839     for (;;)
01840     {
01841         Buffer      buffer;
01842         Page        page;
01843         OffsetNumber offnum;
01844         ItemId      lp;
01845         HeapTupleData tp;
01846         bool        valid;
01847 
01848         /*
01849          * Read, pin, and lock the page.
01850          */
01851         buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&ctid));
01852         LockBuffer(buffer, BUFFER_LOCK_SHARE);
01853         page = BufferGetPage(buffer);
01854 
01855         /*
01856          * Check for bogus item number.  This is not treated as an error
01857          * condition because it can happen while following a t_ctid link. We
01858          * just assume that the prior tid is OK and return it unchanged.
01859          */
01860         offnum = ItemPointerGetOffsetNumber(&ctid);
01861         if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
01862         {
01863             UnlockReleaseBuffer(buffer);
01864             break;
01865         }
01866         lp = PageGetItemId(page, offnum);
01867         if (!ItemIdIsNormal(lp))
01868         {
01869             UnlockReleaseBuffer(buffer);
01870             break;
01871         }
01872 
01873         /* OK to access the tuple */
01874         tp.t_self = ctid;
01875         tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
01876         tp.t_len = ItemIdGetLength(lp);
01877 
01878         /*
01879          * After following a t_ctid link, we might arrive at an unrelated
01880          * tuple.  Check for XMIN match.
01881          */
01882         if (TransactionIdIsValid(priorXmax) &&
01883             !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(tp.t_data)))
01884         {
01885             UnlockReleaseBuffer(buffer);
01886             break;
01887         }
01888 
01889         /*
01890          * Check time qualification of tuple; if visible, set it as the new
01891          * result candidate.
01892          */
01893         valid = HeapTupleSatisfiesVisibility(&tp, snapshot, buffer);
01894         CheckForSerializableConflictOut(valid, relation, &tp, buffer, snapshot);
01895         if (valid)
01896             *tid = ctid;
01897 
01898         /*
01899          * If there's a valid t_ctid link, follow it, else we're done.
01900          */
01901         if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
01902             HeapTupleHeaderIsOnlyLocked(tp.t_data) ||
01903             ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid))
01904         {
01905             UnlockReleaseBuffer(buffer);
01906             break;
01907         }
01908 
01909         ctid = tp.t_data->t_ctid;
01910         priorXmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
01911         UnlockReleaseBuffer(buffer);
01912     }                           /* end of loop */
01913 }
01914 
01915 
01916 /*
01917  * UpdateXmaxHintBits - update tuple hint bits after xmax transaction ends
01918  *
01919  * This is called after we have waited for the XMAX transaction to terminate.
01920  * If the transaction aborted, we guarantee the XMAX_INVALID hint bit will
01921  * be set on exit.  If the transaction committed, we set the XMAX_COMMITTED
01922  * hint bit if possible --- but beware that that may not yet be possible,
01923  * if the transaction committed asynchronously.
01924  *
01925  * Note that if the transaction was a locker only, we set HEAP_XMAX_INVALID
01926  * even if it commits.
01927  *
01928  * Hence callers should look only at XMAX_INVALID.
01929  *
01930  * Note this is not allowed for tuples whose xmax is a multixact.
01931  */
01932 static void
01933 UpdateXmaxHintBits(HeapTupleHeader tuple, Buffer buffer, TransactionId xid)
01934 {
01935     Assert(TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple), xid));
01936     Assert(!(tuple->t_infomask & HEAP_XMAX_IS_MULTI));
01937 
01938     if (!(tuple->t_infomask & (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID)))
01939     {
01940         if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask) &&
01941             TransactionIdDidCommit(xid))
01942             HeapTupleSetHintBits(tuple, buffer, HEAP_XMAX_COMMITTED,
01943                                  xid);
01944         else
01945             HeapTupleSetHintBits(tuple, buffer, HEAP_XMAX_INVALID,
01946                                  InvalidTransactionId);
01947     }
01948 }
01949 
01950 
01951 /*
01952  * GetBulkInsertState - prepare status object for a bulk insert
01953  */
01954 BulkInsertState
01955 GetBulkInsertState(void)
01956 {
01957     BulkInsertState bistate;
01958 
01959     bistate = (BulkInsertState) palloc(sizeof(BulkInsertStateData));
01960     bistate->strategy = GetAccessStrategy(BAS_BULKWRITE);
01961     bistate->current_buf = InvalidBuffer;
01962     return bistate;
01963 }
01964 
01965 /*
01966  * FreeBulkInsertState - clean up after finishing a bulk insert
01967  */
01968 void
01969 FreeBulkInsertState(BulkInsertState bistate)
01970 {
01971     if (bistate->current_buf != InvalidBuffer)
01972         ReleaseBuffer(bistate->current_buf);
01973     FreeAccessStrategy(bistate->strategy);
01974     pfree(bistate);
01975 }
01976 
01977 
01978 /*
01979  *  heap_insert     - insert tuple into a heap
01980  *
01981  * The new tuple is stamped with current transaction ID and the specified
01982  * command ID.
01983  *
01984  * If the HEAP_INSERT_SKIP_WAL option is specified, the new tuple is not
01985  * logged in WAL, even for a non-temp relation.  Safe usage of this behavior
01986  * requires that we arrange that all new tuples go into new pages not
01987  * containing any tuples from other transactions, and that the relation gets
01988  * fsync'd before commit.  (See also heap_sync() comments)
01989  *
01990  * The HEAP_INSERT_SKIP_FSM option is passed directly to
01991  * RelationGetBufferForTuple, which see for more info.
01992  *
01993  * HEAP_INSERT_FROZEN should only be specified for inserts into
01994  * relfilenodes created during the current subtransaction and when
01995  * there are no prior snapshots or pre-existing portals open.
01996  * This causes rows to be frozen, which is an MVCC violation and
01997  * requires explicit options chosen by user.
01998  *
01999  * Note that these options will be applied when inserting into the heap's
02000  * TOAST table, too, if the tuple requires any out-of-line data.
02001  *
02002  * The BulkInsertState object (if any; bistate can be NULL for default
02003  * behavior) is also just passed through to RelationGetBufferForTuple.
02004  *
02005  * The return value is the OID assigned to the tuple (either here or by the
02006  * caller), or InvalidOid if no OID.  The header fields of *tup are updated
02007  * to match the stored tuple; in particular tup->t_self receives the actual
02008  * TID where the tuple was stored.  But note that any toasting of fields
02009  * within the tuple data is NOT reflected into *tup.
02010  */
02011 Oid
02012 heap_insert(Relation relation, HeapTuple tup, CommandId cid,
02013             int options, BulkInsertState bistate)
02014 {
02015     TransactionId xid = GetCurrentTransactionId();
02016     HeapTuple   heaptup;
02017     Buffer      buffer;
02018     Buffer      vmbuffer = InvalidBuffer;
02019     bool        all_visible_cleared = false;
02020 
02021     /*
02022      * Fill in tuple header fields, assign an OID, and toast the tuple if
02023      * necessary.
02024      *
02025      * Note: below this point, heaptup is the data we actually intend to store
02026      * into the relation; tup is the caller's original untoasted data.
02027      */
02028     heaptup = heap_prepare_insert(relation, tup, xid, cid, options);
02029 
02030     /*
02031      * We're about to do the actual insert -- but check for conflict first, to
02032      * avoid possibly having to roll back work we've just done.
02033      *
02034      * For a heap insert, we only need to check for table-level SSI locks. Our
02035      * new tuple can't possibly conflict with existing tuple locks, and heap
02036      * page locks are only consolidated versions of tuple locks; they do not
02037      * lock "gaps" as index page locks do.  So we don't need to identify a
02038      * buffer before making the call.
02039      */
02040     CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
02041 
02042     /*
02043      * Find buffer to insert this tuple into.  If the page is all visible,
02044      * this will also pin the requisite visibility map page.
02045      */
02046     buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
02047                                        InvalidBuffer, options, bistate,
02048                                        &vmbuffer, NULL);
02049 
02050     /* NO EREPORT(ERROR) from here till changes are logged */
02051     START_CRIT_SECTION();
02052 
02053     RelationPutHeapTuple(relation, buffer, heaptup);
02054 
02055     if (PageIsAllVisible(BufferGetPage(buffer)))
02056     {
02057         all_visible_cleared = true;
02058         PageClearAllVisible(BufferGetPage(buffer));
02059         visibilitymap_clear(relation,
02060                             ItemPointerGetBlockNumber(&(heaptup->t_self)),
02061                             vmbuffer);
02062     }
02063 
02064     /*
02065      * XXX Should we set PageSetPrunable on this page ?
02066      *
02067      * The inserting transaction may eventually abort thus making this tuple
02068      * DEAD and hence available for pruning. Though we don't want to optimize
02069      * for aborts, if no other tuple in this page is UPDATEd/DELETEd, the
02070      * aborted tuple will never be pruned until next vacuum is triggered.
02071      *
02072      * If you do add PageSetPrunable here, add it in heap_xlog_insert too.
02073      */
02074 
02075     MarkBufferDirty(buffer);
02076 
02077     /* XLOG stuff */
02078     if (!(options & HEAP_INSERT_SKIP_WAL) && RelationNeedsWAL(relation))
02079     {
02080         xl_heap_insert xlrec;
02081         xl_heap_header xlhdr;
02082         XLogRecPtr  recptr;
02083         XLogRecData rdata[3];
02084         Page        page = BufferGetPage(buffer);
02085         uint8       info = XLOG_HEAP_INSERT;
02086 
02087         xlrec.all_visible_cleared = all_visible_cleared;
02088         xlrec.target.node = relation->rd_node;
02089         xlrec.target.tid = heaptup->t_self;
02090         rdata[0].data = (char *) &xlrec;
02091         rdata[0].len = SizeOfHeapInsert;
02092         rdata[0].buffer = InvalidBuffer;
02093         rdata[0].next = &(rdata[1]);
02094 
02095         xlhdr.t_infomask2 = heaptup->t_data->t_infomask2;
02096         xlhdr.t_infomask = heaptup->t_data->t_infomask;
02097         xlhdr.t_hoff = heaptup->t_data->t_hoff;
02098 
02099         /*
02100          * note we mark rdata[1] as belonging to buffer; if XLogInsert decides
02101          * to write the whole page to the xlog, we don't need to store
02102          * xl_heap_header in the xlog.
02103          */
02104         rdata[1].data = (char *) &xlhdr;
02105         rdata[1].len = SizeOfHeapHeader;
02106         rdata[1].buffer = buffer;
02107         rdata[1].buffer_std = true;
02108         rdata[1].next = &(rdata[2]);
02109 
02110         /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
02111         rdata[2].data = (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits);
02112         rdata[2].len = heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits);
02113         rdata[2].buffer = buffer;
02114         rdata[2].buffer_std = true;
02115         rdata[2].next = NULL;
02116 
02117         /*
02118          * If this is the single and first tuple on page, we can reinit the
02119          * page instead of restoring the whole thing.  Set flag, and hide
02120          * buffer references from XLogInsert.
02121          */
02122         if (ItemPointerGetOffsetNumber(&(heaptup->t_self)) == FirstOffsetNumber &&
02123             PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
02124         {
02125             info |= XLOG_HEAP_INIT_PAGE;
02126             rdata[1].buffer = rdata[2].buffer = InvalidBuffer;
02127         }
02128 
02129         recptr = XLogInsert(RM_HEAP_ID, info, rdata);
02130 
02131         PageSetLSN(page, recptr);
02132     }
02133 
02134     END_CRIT_SECTION();
02135 
02136     UnlockReleaseBuffer(buffer);
02137     if (vmbuffer != InvalidBuffer)
02138         ReleaseBuffer(vmbuffer);
02139 
02140     /*
02141      * If tuple is cachable, mark it for invalidation from the caches in case
02142      * we abort.  Note it is OK to do this after releasing the buffer, because
02143      * the heaptup data structure is all in local memory, not in the shared
02144      * buffer.
02145      */
02146     CacheInvalidateHeapTuple(relation, heaptup, NULL);
02147 
02148     pgstat_count_heap_insert(relation, 1);
02149 
02150     /*
02151      * If heaptup is a private copy, release it.  Don't forget to copy t_self
02152      * back to the caller's image, too.
02153      */
02154     if (heaptup != tup)
02155     {
02156         tup->t_self = heaptup->t_self;
02157         heap_freetuple(heaptup);
02158     }
02159 
02160     return HeapTupleGetOid(tup);
02161 }
02162 
02163 /*
02164  * Subroutine for heap_insert(). Prepares a tuple for insertion. This sets the
02165  * tuple header fields, assigns an OID, and toasts the tuple if necessary.
02166  * Returns a toasted version of the tuple if it was toasted, or the original
02167  * tuple if not. Note that in any case, the header fields are also set in
02168  * the original tuple.
02169  */
02170 static HeapTuple
02171 heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid,
02172                     CommandId cid, int options)
02173 {
02174     if (relation->rd_rel->relhasoids)
02175     {
02176 #ifdef NOT_USED
02177         /* this is redundant with an Assert in HeapTupleSetOid */
02178         Assert(tup->t_data->t_infomask & HEAP_HASOID);
02179 #endif
02180 
02181         /*
02182          * If the object id of this tuple has already been assigned, trust the
02183          * caller.  There are a couple of ways this can happen.  At initial db
02184          * creation, the backend program sets oids for tuples. When we define
02185          * an index, we set the oid.  Finally, in the future, we may allow
02186          * users to set their own object ids in order to support a persistent
02187          * object store (objects need to contain pointers to one another).
02188          */
02189         if (!OidIsValid(HeapTupleGetOid(tup)))
02190             HeapTupleSetOid(tup, GetNewOid(relation));
02191     }
02192     else
02193     {
02194         /* check there is not space for an OID */
02195         Assert(!(tup->t_data->t_infomask & HEAP_HASOID));
02196     }
02197 
02198     tup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
02199     tup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
02200     tup->t_data->t_infomask |= HEAP_XMAX_INVALID;
02201     if (options & HEAP_INSERT_FROZEN)
02202     {
02203         tup->t_data->t_infomask |= HEAP_XMIN_COMMITTED;
02204         HeapTupleHeaderSetXmin(tup->t_data, FrozenTransactionId);
02205     }
02206     else
02207         HeapTupleHeaderSetXmin(tup->t_data, xid);
02208     HeapTupleHeaderSetCmin(tup->t_data, cid);
02209     HeapTupleHeaderSetXmax(tup->t_data, 0);     /* for cleanliness */
02210     tup->t_tableOid = RelationGetRelid(relation);
02211 
02212     /*
02213      * If the new tuple is too big for storage or contains already toasted
02214      * out-of-line attributes from some other relation, invoke the toaster.
02215      */
02216     if (relation->rd_rel->relkind != RELKIND_RELATION &&
02217         relation->rd_rel->relkind != RELKIND_MATVIEW)
02218     {
02219         /* toast table entries should never be recursively toasted */
02220         Assert(!HeapTupleHasExternal(tup));
02221         return tup;
02222     }
02223     else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
02224         return toast_insert_or_update(relation, tup, NULL, options);
02225     else
02226         return tup;
02227 }
02228 
02229 /*
02230  *  heap_multi_insert   - insert multiple tuple into a heap
02231  *
02232  * This is like heap_insert(), but inserts multiple tuples in one operation.
02233  * That's faster than calling heap_insert() in a loop, because when multiple
02234  * tuples can be inserted on a single page, we can write just a single WAL
02235  * record covering all of them, and only need to lock/unlock the page once.
02236  *
02237  * Note: this leaks memory into the current memory context. You can create a
02238  * temporary context before calling this, if that's a problem.
02239  */
02240 void
02241 heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
02242                   CommandId cid, int options, BulkInsertState bistate)
02243 {
02244     TransactionId xid = GetCurrentTransactionId();
02245     HeapTuple  *heaptuples;
02246     int         i;
02247     int         ndone;
02248     char       *scratch = NULL;
02249     Page        page;
02250     bool        needwal;
02251     Size        saveFreeSpace;
02252 
02253     needwal = !(options & HEAP_INSERT_SKIP_WAL) && RelationNeedsWAL(relation);
02254     saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
02255                                                    HEAP_DEFAULT_FILLFACTOR);
02256 
02257     /* Toast and set header data in all the tuples */
02258     heaptuples = palloc(ntuples * sizeof(HeapTuple));
02259     for (i = 0; i < ntuples; i++)
02260         heaptuples[i] = heap_prepare_insert(relation, tuples[i],
02261                                             xid, cid, options);
02262 
02263     /*
02264      * Allocate some memory to use for constructing the WAL record. Using
02265      * palloc() within a critical section is not safe, so we allocate this
02266      * beforehand.
02267      */
02268     if (needwal)
02269         scratch = palloc(BLCKSZ);
02270 
02271     /*
02272      * We're about to do the actual inserts -- but check for conflict first,
02273      * to avoid possibly having to roll back work we've just done.
02274      *
02275      * For a heap insert, we only need to check for table-level SSI locks. Our
02276      * new tuple can't possibly conflict with existing tuple locks, and heap
02277      * page locks are only consolidated versions of tuple locks; they do not
02278      * lock "gaps" as index page locks do.  So we don't need to identify a
02279      * buffer before making the call.
02280      */
02281     CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
02282 
02283     ndone = 0;
02284     while (ndone < ntuples)
02285     {
02286         Buffer      buffer;
02287         Buffer      vmbuffer = InvalidBuffer;
02288         bool        all_visible_cleared = false;
02289         int         nthispage;
02290 
02291         /*
02292          * Find buffer where at least the next tuple will fit.  If the page is
02293          * all-visible, this will also pin the requisite visibility map page.
02294          */
02295         buffer = RelationGetBufferForTuple(relation, heaptuples[ndone]->t_len,
02296                                            InvalidBuffer, options, bistate,
02297                                            &vmbuffer, NULL);
02298         page = BufferGetPage(buffer);
02299 
02300         /* NO EREPORT(ERROR) from here till changes are logged */
02301         START_CRIT_SECTION();
02302 
02303         /*
02304          * RelationGetBufferForTuple has ensured that the first tuple fits.
02305          * Put that on the page, and then as many other tuples as fit.
02306          */
02307         RelationPutHeapTuple(relation, buffer, heaptuples[ndone]);
02308         for (nthispage = 1; ndone + nthispage < ntuples; nthispage++)
02309         {
02310             HeapTuple   heaptup = heaptuples[ndone + nthispage];
02311 
02312             if (PageGetHeapFreeSpace(page) < MAXALIGN(heaptup->t_len) + saveFreeSpace)
02313                 break;
02314 
02315             RelationPutHeapTuple(relation, buffer, heaptup);
02316         }
02317 
02318         if (PageIsAllVisible(page))
02319         {
02320             all_visible_cleared = true;
02321             PageClearAllVisible(page);
02322             visibilitymap_clear(relation,
02323                                 BufferGetBlockNumber(buffer),
02324                                 vmbuffer);
02325         }
02326 
02327         /*
02328          * XXX Should we set PageSetPrunable on this page ? See heap_insert()
02329          */
02330 
02331         MarkBufferDirty(buffer);
02332 
02333         /* XLOG stuff */
02334         if (needwal)
02335         {
02336             XLogRecPtr  recptr;
02337             xl_heap_multi_insert *xlrec;
02338             XLogRecData rdata[2];
02339             uint8       info = XLOG_HEAP2_MULTI_INSERT;
02340             char       *tupledata;
02341             int         totaldatalen;
02342             char       *scratchptr = scratch;
02343             bool        init;
02344 
02345             /*
02346              * If the page was previously empty, we can reinit the page
02347              * instead of restoring the whole thing.
02348              */
02349             init = (ItemPointerGetOffsetNumber(&(heaptuples[ndone]->t_self)) == FirstOffsetNumber &&
02350                     PageGetMaxOffsetNumber(page) == FirstOffsetNumber + nthispage - 1);
02351 
02352             /* allocate xl_heap_multi_insert struct from the scratch area */
02353             xlrec = (xl_heap_multi_insert *) scratchptr;
02354             scratchptr += SizeOfHeapMultiInsert;
02355 
02356             /*
02357              * Allocate offsets array. Unless we're reinitializing the page,
02358              * in that case the tuples are stored in order starting at
02359              * FirstOffsetNumber and we don't need to store the offsets
02360              * explicitly.
02361              */
02362             if (!init)
02363                 scratchptr += nthispage * sizeof(OffsetNumber);
02364 
02365             /* the rest of the scratch space is used for tuple data */
02366             tupledata = scratchptr;
02367 
02368             xlrec->all_visible_cleared = all_visible_cleared;
02369             xlrec->node = relation->rd_node;
02370             xlrec->blkno = BufferGetBlockNumber(buffer);
02371             xlrec->ntuples = nthispage;
02372 
02373             /*
02374              * Write out an xl_multi_insert_tuple and the tuple data itself
02375              * for each tuple.
02376              */
02377             for (i = 0; i < nthispage; i++)
02378             {
02379                 HeapTuple   heaptup = heaptuples[ndone + i];
02380                 xl_multi_insert_tuple *tuphdr;
02381                 int         datalen;
02382 
02383                 if (!init)
02384                     xlrec->offsets[i] = ItemPointerGetOffsetNumber(&heaptup->t_self);
02385                 /* xl_multi_insert_tuple needs two-byte alignment. */
02386                 tuphdr = (xl_multi_insert_tuple *) SHORTALIGN(scratchptr);
02387                 scratchptr = ((char *) tuphdr) + SizeOfMultiInsertTuple;
02388 
02389                 tuphdr->t_infomask2 = heaptup->t_data->t_infomask2;
02390                 tuphdr->t_infomask = heaptup->t_data->t_infomask;
02391                 tuphdr->t_hoff = heaptup->t_data->t_hoff;
02392 
02393                 /* write bitmap [+ padding] [+ oid] + data */
02394                 datalen = heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits);
02395                 memcpy(scratchptr,
02396                        (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits),
02397                        datalen);
02398                 tuphdr->datalen = datalen;
02399                 scratchptr += datalen;
02400             }
02401             totaldatalen = scratchptr - tupledata;
02402             Assert((scratchptr - scratch) < BLCKSZ);
02403 
02404             rdata[0].data = (char *) xlrec;
02405             rdata[0].len = tupledata - scratch;
02406             rdata[0].buffer = InvalidBuffer;
02407             rdata[0].next = &rdata[1];
02408 
02409             rdata[1].data = tupledata;
02410             rdata[1].len = totaldatalen;
02411             rdata[1].buffer = buffer;
02412             rdata[1].buffer_std = true;
02413             rdata[1].next = NULL;
02414 
02415             /*
02416              * If we're going to reinitialize the whole page using the WAL
02417              * record, hide buffer reference from XLogInsert.
02418              */
02419             if (init)
02420             {
02421                 rdata[1].buffer = InvalidBuffer;
02422                 info |= XLOG_HEAP_INIT_PAGE;
02423             }
02424 
02425             recptr = XLogInsert(RM_HEAP2_ID, info, rdata);
02426 
02427             PageSetLSN(page, recptr);
02428         }
02429 
02430         END_CRIT_SECTION();
02431 
02432         UnlockReleaseBuffer(buffer);
02433         if (vmbuffer != InvalidBuffer)
02434             ReleaseBuffer(vmbuffer);
02435 
02436         ndone += nthispage;
02437     }
02438 
02439     /*
02440      * If tuples are cachable, mark them for invalidation from the caches in
02441      * case we abort.  Note it is OK to do this after releasing the buffer,
02442      * because the heaptuples data structure is all in local memory, not in
02443      * the shared buffer.
02444      */
02445     if (IsSystemRelation(relation))
02446     {
02447         for (i = 0; i < ntuples; i++)
02448             CacheInvalidateHeapTuple(relation, heaptuples[i], NULL);
02449     }
02450 
02451     /*
02452      * Copy t_self fields back to the caller's original tuples. This does
02453      * nothing for untoasted tuples (tuples[i] == heaptuples[i)], but it's
02454      * probably faster to always copy than check.
02455      */
02456     for (i = 0; i < ntuples; i++)
02457         tuples[i]->t_self = heaptuples[i]->t_self;
02458 
02459     pgstat_count_heap_insert(relation, ntuples);
02460 }
02461 
02462 /*
02463  *  simple_heap_insert - insert a tuple
02464  *
02465  * Currently, this routine differs from heap_insert only in supplying
02466  * a default command ID and not allowing access to the speedup options.
02467  *
02468  * This should be used rather than using heap_insert directly in most places
02469  * where we are modifying system catalogs.
02470  */
02471 Oid
02472 simple_heap_insert(Relation relation, HeapTuple tup)
02473 {
02474     return heap_insert(relation, tup, GetCurrentCommandId(true), 0, NULL);
02475 }
02476 
02477 /*
02478  * Given infomask/infomask2, compute the bits that must be saved in the
02479  * "infobits" field of xl_heap_delete, xl_heap_update, xl_heap_lock,
02480  * xl_heap_lock_updated WAL records.
02481  *
02482  * See fix_infomask_from_infobits.
02483  */
02484 static uint8
02485 compute_infobits(uint16 infomask, uint16 infomask2)
02486 {
02487     return
02488         ((infomask & HEAP_XMAX_IS_MULTI) != 0 ? XLHL_XMAX_IS_MULTI : 0) |
02489         ((infomask & HEAP_XMAX_LOCK_ONLY) != 0 ? XLHL_XMAX_LOCK_ONLY : 0) |
02490         ((infomask & HEAP_XMAX_EXCL_LOCK) != 0 ? XLHL_XMAX_EXCL_LOCK : 0) |
02491         /* note we ignore HEAP_XMAX_SHR_LOCK here */
02492         ((infomask & HEAP_XMAX_KEYSHR_LOCK) != 0 ? XLHL_XMAX_KEYSHR_LOCK : 0) |
02493         ((infomask2 & HEAP_KEYS_UPDATED) != 0 ?
02494          XLHL_KEYS_UPDATED : 0);
02495 }
02496 
02497 /*
02498  *  heap_delete - delete a tuple
02499  *
02500  * NB: do not call this directly unless you are prepared to deal with
02501  * concurrent-update conditions.  Use simple_heap_delete instead.
02502  *
02503  *  relation - table to be modified (caller must hold suitable lock)
02504  *  tid - TID of tuple to be deleted
02505  *  cid - delete command ID (used for visibility test, and stored into
02506  *      cmax if successful)
02507  *  crosscheck - if not InvalidSnapshot, also check tuple against this
02508  *  wait - true if should wait for any conflicting update to commit/abort
02509  *  hufd - output parameter, filled in failure cases (see below)
02510  *
02511  * Normal, successful return value is HeapTupleMayBeUpdated, which
02512  * actually means we did delete it.  Failure return codes are
02513  * HeapTupleSelfUpdated, HeapTupleUpdated, or HeapTupleBeingUpdated
02514  * (the last only possible if wait == false).
02515  *
02516  * In the failure cases, the routine fills *hufd with the tuple's t_ctid,
02517  * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax
02518  * (the last only for HeapTupleSelfUpdated, since we
02519  * cannot obtain cmax from a combocid generated by another transaction).
02520  * See comments for struct HeapUpdateFailureData for additional info.
02521  */
02522 HTSU_Result
02523 heap_delete(Relation relation, ItemPointer tid,
02524             CommandId cid, Snapshot crosscheck, bool wait,
02525             HeapUpdateFailureData *hufd)
02526 {
02527     HTSU_Result result;
02528     TransactionId xid = GetCurrentTransactionId();
02529     ItemId      lp;
02530     HeapTupleData tp;
02531     Page        page;
02532     BlockNumber block;
02533     Buffer      buffer;
02534     Buffer      vmbuffer = InvalidBuffer;
02535     TransactionId new_xmax;
02536     uint16      new_infomask,
02537                 new_infomask2;
02538     bool        have_tuple_lock = false;
02539     bool        iscombo;
02540     bool        all_visible_cleared = false;
02541 
02542     Assert(ItemPointerIsValid(tid));
02543 
02544     block = ItemPointerGetBlockNumber(tid);
02545     buffer = ReadBuffer(relation, block);
02546     page = BufferGetPage(buffer);
02547 
02548     /*
02549      * Before locking the buffer, pin the visibility map page if it appears to
02550      * be necessary.  Since we haven't got the lock yet, someone else might be
02551      * in the middle of changing this, so we'll need to recheck after we have
02552      * the lock.
02553      */
02554     if (PageIsAllVisible(page))
02555         visibilitymap_pin(relation, block, &vmbuffer);
02556 
02557     LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
02558 
02559     /*
02560      * If we didn't pin the visibility map page and the page has become all
02561      * visible while we were busy locking the buffer, we'll have to unlock and
02562      * re-lock, to avoid holding the buffer lock across an I/O.  That's a bit
02563      * unfortunate, but hopefully shouldn't happen often.
02564      */
02565     if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
02566     {
02567         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
02568         visibilitymap_pin(relation, block, &vmbuffer);
02569         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
02570     }
02571 
02572     lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid));
02573     Assert(ItemIdIsNormal(lp));
02574 
02575     tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
02576     tp.t_len = ItemIdGetLength(lp);
02577     tp.t_self = *tid;
02578 
02579 l1:
02580     result = HeapTupleSatisfiesUpdate(tp.t_data, cid, buffer);
02581 
02582     if (result == HeapTupleInvisible)
02583     {
02584         UnlockReleaseBuffer(buffer);
02585         elog(ERROR, "attempted to delete invisible tuple");
02586     }
02587     else if (result == HeapTupleBeingUpdated && wait)
02588     {
02589         TransactionId xwait;
02590         uint16      infomask;
02591 
02592         /* must copy state data before unlocking buffer */
02593         xwait = HeapTupleHeaderGetRawXmax(tp.t_data);
02594         infomask = tp.t_data->t_infomask;
02595 
02596         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
02597 
02598         /*
02599          * Acquire tuple lock to establish our priority for the tuple (see
02600          * heap_lock_tuple).  LockTuple will release us when we are
02601          * next-in-line for the tuple.
02602          *
02603          * If we are forced to "start over" below, we keep the tuple lock;
02604          * this arranges that we stay at the head of the line while rechecking
02605          * tuple state.
02606          */
02607         if (!have_tuple_lock)
02608         {
02609             LockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
02610             have_tuple_lock = true;
02611         }
02612 
02613         /*
02614          * Sleep until concurrent transaction ends.  Note that we don't care
02615          * which lock mode the locker has, because we need the strongest one.
02616          */
02617 
02618         if (infomask & HEAP_XMAX_IS_MULTI)
02619         {
02620             /* wait for multixact */
02621             MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate,
02622                             NULL, infomask);
02623             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
02624 
02625             /*
02626              * If xwait had just locked the tuple then some other xact could
02627              * update this tuple before we get to this point.  Check for xmax
02628              * change, and start over if so.
02629              */
02630             if (!(tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
02631                 !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data),
02632                                      xwait))
02633                 goto l1;
02634 
02635             /*
02636              * You might think the multixact is necessarily done here, but not
02637              * so: it could have surviving members, namely our own xact or
02638              * other subxacts of this backend.  It is legal for us to delete
02639              * the tuple in either case, however (the latter case is
02640              * essentially a situation of upgrading our former shared lock to
02641              * exclusive).  We don't bother changing the on-disk hint bits
02642              * since we are about to overwrite the xmax altogether.
02643              */
02644         }
02645         else
02646         {
02647             /* wait for regular transaction to end */
02648             XactLockTableWait(xwait);
02649             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
02650 
02651             /*
02652              * xwait is done, but if xwait had just locked the tuple then some
02653              * other xact could update this tuple before we get to this point.
02654              * Check for xmax change, and start over if so.
02655              */
02656             if ((tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
02657                 !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data),
02658                                      xwait))
02659                 goto l1;
02660 
02661             /* Otherwise check if it committed or aborted */
02662             UpdateXmaxHintBits(tp.t_data, buffer, xwait);
02663         }
02664 
02665         /*
02666          * We may overwrite if previous xmax aborted, or if it committed but
02667          * only locked the tuple without updating it.
02668          */
02669         if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
02670             HEAP_XMAX_IS_LOCKED_ONLY(tp.t_data->t_infomask) ||
02671             HeapTupleHeaderIsOnlyLocked(tp.t_data))
02672             result = HeapTupleMayBeUpdated;
02673         else
02674             result = HeapTupleUpdated;
02675     }
02676 
02677     if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
02678     {
02679         /* Perform additional check for transaction-snapshot mode RI updates */
02680         if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
02681             result = HeapTupleUpdated;
02682     }
02683 
02684     if (result != HeapTupleMayBeUpdated)
02685     {
02686         Assert(result == HeapTupleSelfUpdated ||
02687                result == HeapTupleUpdated ||
02688                result == HeapTupleBeingUpdated);
02689         Assert(!(tp.t_data->t_infomask & HEAP_XMAX_INVALID));
02690         hufd->ctid = tp.t_data->t_ctid;
02691         hufd->xmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
02692         if (result == HeapTupleSelfUpdated)
02693             hufd->cmax = HeapTupleHeaderGetCmax(tp.t_data);
02694         else
02695             hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
02696         UnlockReleaseBuffer(buffer);
02697         if (have_tuple_lock)
02698             UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
02699         if (vmbuffer != InvalidBuffer)
02700             ReleaseBuffer(vmbuffer);
02701         return result;
02702     }
02703 
02704     /*
02705      * We're about to do the actual delete -- check for conflict first, to
02706      * avoid possibly having to roll back work we've just done.
02707      */
02708     CheckForSerializableConflictIn(relation, &tp, buffer);
02709 
02710     /* replace cid with a combo cid if necessary */
02711     HeapTupleHeaderAdjustCmax(tp.t_data, &cid, &iscombo);
02712 
02713     START_CRIT_SECTION();
02714 
02715     /*
02716      * If this transaction commits, the tuple will become DEAD sooner or
02717      * later.  Set flag that this page is a candidate for pruning once our xid
02718      * falls below the OldestXmin horizon.  If the transaction finally aborts,
02719      * the subsequent page pruning will be a no-op and the hint will be
02720      * cleared.
02721      */
02722     PageSetPrunable(page, xid);
02723 
02724     if (PageIsAllVisible(page))
02725     {
02726         all_visible_cleared = true;
02727         PageClearAllVisible(page);
02728         visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
02729                             vmbuffer);
02730     }
02731 
02732     /*
02733      * If this is the first possibly-multixact-able operation in the
02734      * current transaction, set my per-backend OldestMemberMXactId setting.
02735      * We can be certain that the transaction will never become a member of
02736      * any older MultiXactIds than that.  (We have to do this even if we
02737      * end up just using our own TransactionId below, since some other
02738      * backend could incorporate our XID into a MultiXact immediately
02739      * afterwards.)
02740      */
02741     MultiXactIdSetOldestMember();
02742 
02743     compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(tp.t_data),
02744                               tp.t_data->t_infomask, tp.t_data->t_infomask2,
02745                               xid, LockTupleExclusive, true,
02746                               &new_xmax, &new_infomask, &new_infomask2);
02747 
02748     /* store transaction information of xact deleting the tuple */
02749     tp.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
02750     tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
02751     tp.t_data->t_infomask |= new_infomask;
02752     tp.t_data->t_infomask2 |= new_infomask2;
02753     HeapTupleHeaderClearHotUpdated(tp.t_data);
02754     HeapTupleHeaderSetXmax(tp.t_data, new_xmax);
02755     HeapTupleHeaderSetCmax(tp.t_data, cid, iscombo);
02756     /* Make sure there is no forward chain link in t_ctid */
02757     tp.t_data->t_ctid = tp.t_self;
02758 
02759     MarkBufferDirty(buffer);
02760 
02761     /* XLOG stuff */
02762     if (RelationNeedsWAL(relation))
02763     {
02764         xl_heap_delete xlrec;
02765         XLogRecPtr  recptr;
02766         XLogRecData rdata[2];
02767 
02768         xlrec.all_visible_cleared = all_visible_cleared;
02769         xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask,
02770                                               tp.t_data->t_infomask2);
02771         xlrec.target.node = relation->rd_node;
02772         xlrec.target.tid = tp.t_self;
02773         xlrec.xmax = new_xmax;
02774         rdata[0].data = (char *) &xlrec;
02775         rdata[0].len = SizeOfHeapDelete;
02776         rdata[0].buffer = InvalidBuffer;
02777         rdata[0].next = &(rdata[1]);
02778 
02779         rdata[1].data = NULL;
02780         rdata[1].len = 0;
02781         rdata[1].buffer = buffer;
02782         rdata[1].buffer_std = true;
02783         rdata[1].next = NULL;
02784 
02785         recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE, rdata);
02786 
02787         PageSetLSN(page, recptr);
02788     }
02789 
02790     END_CRIT_SECTION();
02791 
02792     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
02793 
02794     if (vmbuffer != InvalidBuffer)
02795         ReleaseBuffer(vmbuffer);
02796 
02797     /*
02798      * If the tuple has toasted out-of-line attributes, we need to delete
02799      * those items too.  We have to do this before releasing the buffer
02800      * because we need to look at the contents of the tuple, but it's OK to
02801      * release the content lock on the buffer first.
02802      */
02803     if (relation->rd_rel->relkind != RELKIND_RELATION &&
02804         relation->rd_rel->relkind != RELKIND_MATVIEW)
02805     {
02806         /* toast table entries should never be recursively toasted */
02807         Assert(!HeapTupleHasExternal(&tp));
02808     }
02809     else if (HeapTupleHasExternal(&tp))
02810         toast_delete(relation, &tp);
02811 
02812     /*
02813      * Mark tuple for invalidation from system caches at next command
02814      * boundary. We have to do this before releasing the buffer because we
02815      * need to look at the contents of the tuple.
02816      */
02817     CacheInvalidateHeapTuple(relation, &tp, NULL);
02818 
02819     /* Now we can release the buffer */
02820     ReleaseBuffer(buffer);
02821 
02822     /*
02823      * Release the lmgr tuple lock, if we had it.
02824      */
02825     if (have_tuple_lock)
02826         UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
02827 
02828     pgstat_count_heap_delete(relation);
02829 
02830     return HeapTupleMayBeUpdated;
02831 }
02832 
02833 /*
02834  *  simple_heap_delete - delete a tuple
02835  *
02836  * This routine may be used to delete a tuple when concurrent updates of
02837  * the target tuple are not expected (for example, because we have a lock
02838  * on the relation associated with the tuple).  Any failure is reported
02839  * via ereport().
02840  */
02841 void
02842 simple_heap_delete(Relation relation, ItemPointer tid)
02843 {
02844     HTSU_Result result;
02845     HeapUpdateFailureData hufd;
02846 
02847     result = heap_delete(relation, tid,
02848                          GetCurrentCommandId(true), InvalidSnapshot,
02849                          true /* wait for commit */,
02850                          &hufd);
02851     switch (result)
02852     {
02853         case HeapTupleSelfUpdated:
02854             /* Tuple was already updated in current command? */
02855             elog(ERROR, "tuple already updated by self");
02856             break;
02857 
02858         case HeapTupleMayBeUpdated:
02859             /* done successfully */
02860             break;
02861 
02862         case HeapTupleUpdated:
02863             elog(ERROR, "tuple concurrently updated");
02864             break;
02865 
02866         default:
02867             elog(ERROR, "unrecognized heap_delete status: %u", result);
02868             break;
02869     }
02870 }
02871 
02872 /*
02873  *  heap_update - replace a tuple
02874  *
02875  * NB: do not call this directly unless you are prepared to deal with
02876  * concurrent-update conditions.  Use simple_heap_update instead.
02877  *
02878  *  relation - table to be modified (caller must hold suitable lock)
02879  *  otid - TID of old tuple to be replaced
02880  *  newtup - newly constructed tuple data to store
02881  *  cid - update command ID (used for visibility test, and stored into
02882  *      cmax/cmin if successful)
02883  *  crosscheck - if not InvalidSnapshot, also check old tuple against this
02884  *  wait - true if should wait for any conflicting update to commit/abort
02885  *  hufd - output parameter, filled in failure cases (see below)
02886  *  lockmode - output parameter, filled with lock mode acquired on tuple
02887  *
02888  * Normal, successful return value is HeapTupleMayBeUpdated, which
02889  * actually means we *did* update it.  Failure return codes are
02890  * HeapTupleSelfUpdated, HeapTupleUpdated, or HeapTupleBeingUpdated
02891  * (the last only possible if wait == false).
02892  *
02893  * On success, the header fields of *newtup are updated to match the new
02894  * stored tuple; in particular, newtup->t_self is set to the TID where the
02895  * new tuple was inserted, and its HEAP_ONLY_TUPLE flag is set iff a HOT
02896  * update was done.  However, any TOAST changes in the new tuple's
02897  * data are not reflected into *newtup.
02898  *
02899  * In the failure cases, the routine fills *hufd with the tuple's t_ctid,
02900  * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax
02901  * (the last only for HeapTupleSelfUpdated, since we
02902  * cannot obtain cmax from a combocid generated by another transaction).
02903  * See comments for struct HeapUpdateFailureData for additional info.
02904  */
02905 HTSU_Result
02906 heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
02907             CommandId cid, Snapshot crosscheck, bool wait,
02908             HeapUpdateFailureData *hufd, LockTupleMode *lockmode)
02909 {
02910     HTSU_Result result;
02911     TransactionId xid = GetCurrentTransactionId();
02912     Bitmapset  *hot_attrs;
02913     Bitmapset  *key_attrs;
02914     ItemId      lp;
02915     HeapTupleData oldtup;
02916     HeapTuple   heaptup;
02917     Page        page;
02918     BlockNumber block;
02919     MultiXactStatus mxact_status;
02920     Buffer      buffer,
02921                 newbuf,
02922                 vmbuffer = InvalidBuffer,
02923                 vmbuffer_new = InvalidBuffer;
02924     bool        need_toast,
02925                 already_marked;
02926     Size        newtupsize,
02927                 pagefree;
02928     bool        have_tuple_lock = false;
02929     bool        iscombo;
02930     bool        satisfies_hot;
02931     bool        satisfies_key;
02932     bool        use_hot_update = false;
02933     bool        key_intact;
02934     bool        all_visible_cleared = false;
02935     bool        all_visible_cleared_new = false;
02936     bool        checked_lockers;
02937     bool        locker_remains;
02938     TransactionId xmax_new_tuple,
02939                   xmax_old_tuple;
02940     uint16      infomask_old_tuple,
02941                 infomask2_old_tuple,
02942                 infomask_new_tuple,
02943                 infomask2_new_tuple;
02944 
02945     Assert(ItemPointerIsValid(otid));
02946 
02947     /*
02948      * Fetch the list of attributes to be checked for HOT update.  This is
02949      * wasted effort if we fail to update or have to put the new tuple on a
02950      * different page.  But we must compute the list before obtaining buffer
02951      * lock --- in the worst case, if we are doing an update on one of the
02952      * relevant system catalogs, we could deadlock if we try to fetch the list
02953      * later.  In any case, the relcache caches the data so this is usually
02954      * pretty cheap.
02955      *
02956      * Note that we get a copy here, so we need not worry about relcache flush
02957      * happening midway through.
02958      */
02959     hot_attrs = RelationGetIndexAttrBitmap(relation, false);
02960     key_attrs = RelationGetIndexAttrBitmap(relation, true);
02961 
02962     block = ItemPointerGetBlockNumber(otid);
02963     buffer = ReadBuffer(relation, block);
02964     page = BufferGetPage(buffer);
02965 
02966     /*
02967      * Before locking the buffer, pin the visibility map page if it appears to
02968      * be necessary.  Since we haven't got the lock yet, someone else might be
02969      * in the middle of changing this, so we'll need to recheck after we have
02970      * the lock.
02971      */
02972     if (PageIsAllVisible(page))
02973         visibilitymap_pin(relation, block, &vmbuffer);
02974 
02975     LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
02976 
02977     lp = PageGetItemId(page, ItemPointerGetOffsetNumber(otid));
02978     Assert(ItemIdIsNormal(lp));
02979 
02980     /*
02981      * Fill in enough data in oldtup for HeapSatisfiesHOTandKeyUpdate to work
02982      * properly.
02983      */
02984     oldtup.t_tableOid = RelationGetRelid(relation);
02985     oldtup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
02986     oldtup.t_len = ItemIdGetLength(lp);
02987     oldtup.t_self = *otid;
02988 
02989     /* the new tuple is ready, except for this: */
02990     newtup->t_tableOid = RelationGetRelid(relation);
02991 
02992     /* Fill in OID for newtup */
02993     if (relation->rd_rel->relhasoids)
02994     {
02995 #ifdef NOT_USED
02996         /* this is redundant with an Assert in HeapTupleSetOid */
02997         Assert(newtup->t_data->t_infomask & HEAP_HASOID);
02998 #endif
02999         HeapTupleSetOid(newtup, HeapTupleGetOid(&oldtup));
03000     }
03001     else
03002     {
03003         /* check there is not space for an OID */
03004         Assert(!(newtup->t_data->t_infomask & HEAP_HASOID));
03005     }
03006 
03007     /*
03008      * If we're not updating any "key" column, we can grab a weaker lock type.
03009      * This allows for more concurrency when we are running simultaneously with
03010      * foreign key checks.
03011      *
03012      * Note that if a column gets detoasted while executing the update, but the
03013      * value ends up being the same, this test will fail and we will use the
03014      * stronger lock.  This is acceptable; the important case to optimize is
03015      * updates that don't manipulate key columns, not those that
03016      * serendipitiously arrive at the same key values.
03017      */
03018     HeapSatisfiesHOTandKeyUpdate(relation, hot_attrs, key_attrs,
03019                                  &satisfies_hot, &satisfies_key,
03020                                  &oldtup, newtup);
03021     if (satisfies_key)
03022     {
03023         *lockmode = LockTupleNoKeyExclusive;
03024         mxact_status = MultiXactStatusNoKeyUpdate;
03025         key_intact = true;
03026 
03027         /*
03028          * If this is the first possibly-multixact-able operation in the
03029          * current transaction, set my per-backend OldestMemberMXactId setting.
03030          * We can be certain that the transaction will never become a member of
03031          * any older MultiXactIds than that.  (We have to do this even if we
03032          * end up just using our own TransactionId below, since some other
03033          * backend could incorporate our XID into a MultiXact immediately
03034          * afterwards.)
03035          */
03036         MultiXactIdSetOldestMember();
03037     }
03038     else
03039     {
03040         *lockmode = LockTupleExclusive;
03041         mxact_status = MultiXactStatusUpdate;
03042         key_intact = false;
03043     }
03044 
03045     /*
03046      * Note: beyond this point, use oldtup not otid to refer to old tuple.
03047      * otid may very well point at newtup->t_self, which we will overwrite
03048      * with the new tuple's location, so there's great risk of confusion if we
03049      * use otid anymore.
03050      */
03051 
03052 l2:
03053     checked_lockers = false;
03054     locker_remains = false;
03055     result = HeapTupleSatisfiesUpdate(oldtup.t_data, cid, buffer);
03056 
03057     /* see below about the "no wait" case */
03058     Assert(result != HeapTupleBeingUpdated || wait);
03059 
03060     if (result == HeapTupleInvisible)
03061     {
03062         UnlockReleaseBuffer(buffer);
03063         elog(ERROR, "attempted to update invisible tuple");
03064     }
03065     else if (result == HeapTupleBeingUpdated && wait)
03066     {
03067         TransactionId   xwait;
03068         uint16      infomask;
03069         bool        can_continue = false;
03070 
03071         checked_lockers = true;
03072 
03073         /*
03074          * XXX note that we don't consider the "no wait" case here.  This
03075          * isn't a problem currently because no caller uses that case, but it
03076          * should be fixed if such a caller is introduced.  It wasn't a problem
03077          * previously because this code would always wait, but now that some
03078          * tuple locks do not conflict with one of the lock modes we use, it is
03079          * possible that this case is interesting to handle specially.
03080          *
03081          * This may cause failures with third-party code that calls heap_update
03082          * directly.
03083          */
03084 
03085         /* must copy state data before unlocking buffer */
03086         xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data);
03087         infomask = oldtup.t_data->t_infomask;
03088 
03089         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
03090 
03091         /*
03092          * Acquire tuple lock to establish our priority for the tuple (see
03093          * heap_lock_tuple).  LockTuple will release us when we are
03094          * next-in-line for the tuple.
03095          *
03096          * If we are forced to "start over" below, we keep the tuple lock;
03097          * this arranges that we stay at the head of the line while rechecking
03098          * tuple state.
03099          */
03100         if (!have_tuple_lock)
03101         {
03102             LockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
03103             have_tuple_lock = true;
03104         }
03105 
03106         /*
03107          * Now we have to do something about the existing locker.  If it's a
03108          * multi, sleep on it; we might be awakened before it is completely
03109          * gone (or even not sleep at all in some cases); we need to preserve
03110          * it as locker, unless it is gone completely.
03111          *
03112          * If it's not a multi, we need to check for sleeping conditions before
03113          * actually going to sleep.  If the update doesn't conflict with the
03114          * locks, we just continue without sleeping (but making sure it is
03115          * preserved).
03116          */
03117         if (infomask & HEAP_XMAX_IS_MULTI)
03118         {
03119             TransactionId   update_xact;
03120             int             remain;
03121 
03122             /* wait for multixact */
03123             MultiXactIdWait((MultiXactId) xwait, mxact_status, &remain,
03124                             infomask);
03125             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
03126 
03127             /*
03128              * If xwait had just locked the tuple then some other xact could
03129              * update this tuple before we get to this point.  Check for xmax
03130              * change, and start over if so.
03131              */
03132             if (!(oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
03133                 !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
03134                                      xwait))
03135                 goto l2;
03136 
03137             /*
03138              * Note that the multixact may not be done by now.  It could have
03139              * surviving members; our own xact or other subxacts of this
03140              * backend, and also any other concurrent transaction that locked
03141              * the tuple with KeyShare if we only got TupleLockUpdate.  If this
03142              * is the case, we have to be careful to mark the updated tuple
03143              * with the surviving members in Xmax.
03144              *
03145              * Note that there could have been another update in the MultiXact.
03146              * In that case, we need to check whether it committed or aborted.
03147              * If it aborted we are safe to update it again; otherwise there is
03148              * an update conflict, and we have to return HeapTupleUpdated
03149              * below.
03150              *
03151              * In the LockTupleExclusive case, we still need to preserve the
03152              * surviving members: those would include the tuple locks we had
03153              * before this one, which are important to keep in case this
03154              * subxact aborts.
03155              */
03156             update_xact = InvalidTransactionId;
03157             if (!HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask))
03158                 update_xact = HeapTupleGetUpdateXid(oldtup.t_data);
03159 
03160             /* there was no UPDATE in the MultiXact; or it aborted. */
03161             if (!TransactionIdIsValid(update_xact) ||
03162                 TransactionIdDidAbort(update_xact))
03163                 can_continue = true;
03164 
03165             locker_remains = remain != 0;
03166         }
03167         else
03168         {
03169             /*
03170              * If it's just a key-share locker, and we're not changing the
03171              * key columns, we don't need to wait for it to end; but we
03172              * need to preserve it as locker.
03173              */
03174             if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact)
03175             {
03176                 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
03177 
03178                 /*
03179                  * recheck the locker; if someone else changed the tuple while we
03180                  * weren't looking, start over.
03181                  */
03182                 if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
03183                     !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
03184                                          xwait))
03185                     goto l2;
03186 
03187                 can_continue = true;
03188                 locker_remains = true;
03189             }
03190             else
03191             {
03192                 /* wait for regular transaction to end */
03193                 XactLockTableWait(xwait);
03194                 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
03195 
03196                 /*
03197                  * xwait is done, but if xwait had just locked the tuple then some
03198                  * other xact could update this tuple before we get to this point.
03199                  * Check for xmax change, and start over if so.
03200                  */
03201                 if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
03202                     !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
03203                                          xwait))
03204                     goto l2;
03205 
03206                 /* Otherwise check if it committed or aborted */
03207                 UpdateXmaxHintBits(oldtup.t_data, buffer, xwait);
03208                 if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID)
03209                     can_continue = true;
03210             }
03211         }
03212 
03213         result = can_continue ? HeapTupleMayBeUpdated : HeapTupleUpdated;
03214     }
03215 
03216     if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
03217     {
03218         /* Perform additional check for transaction-snapshot mode RI updates */
03219         if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer))
03220             result = HeapTupleUpdated;
03221     }
03222 
03223     if (result != HeapTupleMayBeUpdated)
03224     {
03225         Assert(result == HeapTupleSelfUpdated ||
03226                result == HeapTupleUpdated ||
03227                result == HeapTupleBeingUpdated);
03228         Assert(!(oldtup.t_data->t_infomask & HEAP_XMAX_INVALID));
03229         hufd->ctid = oldtup.t_data->t_ctid;
03230         hufd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data);
03231         if (result == HeapTupleSelfUpdated)
03232             hufd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data);
03233         else
03234             hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
03235         UnlockReleaseBuffer(buffer);
03236         if (have_tuple_lock)
03237             UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
03238         if (vmbuffer != InvalidBuffer)
03239             ReleaseBuffer(vmbuffer);
03240         bms_free(hot_attrs);
03241         bms_free(key_attrs);
03242         return result;
03243     }
03244 
03245     /*
03246      * If we didn't pin the visibility map page and the page has become all
03247      * visible while we were busy locking the buffer, or during some
03248      * subsequent window during which we had it unlocked, we'll have to unlock
03249      * and re-lock, to avoid holding the buffer lock across an I/O.  That's a
03250      * bit unfortunate, especially since we'll now have to recheck whether
03251      * the tuple has been locked or updated under us, but hopefully it won't
03252      * happen very often.
03253      */
03254     if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
03255     {
03256         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
03257         visibilitymap_pin(relation, block, &vmbuffer);
03258         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
03259         goto l2;
03260     }
03261 
03262     /*
03263      * We're about to do the actual update -- check for conflict first, to
03264      * avoid possibly having to roll back work we've just done.
03265      */
03266     CheckForSerializableConflictIn(relation, &oldtup, buffer);
03267 
03268     /* Fill in transaction status data */
03269 
03270     /*
03271      * If the tuple we're updating is locked, we need to preserve the locking
03272      * info in the old tuple's Xmax.  Prepare a new Xmax value for this.
03273      */
03274     compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data),
03275                               oldtup.t_data->t_infomask,
03276                               oldtup.t_data->t_infomask2,
03277                               xid, *lockmode, true,
03278                               &xmax_old_tuple, &infomask_old_tuple,
03279                               &infomask2_old_tuple);
03280 
03281     /*
03282      * And also prepare an Xmax value for the new copy of the tuple.  If there
03283      * was no xmax previously, or there was one but all lockers are now gone,
03284      * then use InvalidXid; otherwise, get the xmax from the old tuple.  (In
03285      * rare cases that might also be InvalidXid and yet not have the
03286      * HEAP_XMAX_INVALID bit set; that's fine.)
03287      */
03288     if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) ||
03289         (checked_lockers && !locker_remains))
03290         xmax_new_tuple = InvalidTransactionId;
03291     else
03292         xmax_new_tuple = HeapTupleHeaderGetRawXmax(oldtup.t_data);
03293 
03294     if (!TransactionIdIsValid(xmax_new_tuple))
03295     {
03296         infomask_new_tuple = HEAP_XMAX_INVALID;
03297         infomask2_new_tuple = 0;
03298     }
03299     else
03300     {
03301         /*
03302          * If we found a valid Xmax for the new tuple, then the infomask bits
03303          * to use on the new tuple depend on what was there on the old one.
03304          * Note that since we're doing an update, the only possibility is that
03305          * the lockers had FOR KEY SHARE lock.
03306          */
03307         if (oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI)
03308         {
03309             GetMultiXactIdHintBits(xmax_new_tuple, &infomask_new_tuple,
03310                                    &infomask2_new_tuple);
03311         }
03312         else
03313         {
03314             infomask_new_tuple = HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY;
03315             infomask2_new_tuple = 0;
03316         }
03317     }
03318 
03319     /*
03320      * Prepare the new tuple with the appropriate initial values of Xmin and
03321      * Xmax, as well as initial infomask bits as computed above.
03322      */
03323     newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
03324     newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
03325     HeapTupleHeaderSetXmin(newtup->t_data, xid);
03326     HeapTupleHeaderSetCmin(newtup->t_data, cid);
03327     newtup->t_data->t_infomask |= HEAP_UPDATED | infomask_new_tuple;
03328     newtup->t_data->t_infomask2 |= infomask2_new_tuple;
03329     HeapTupleHeaderSetXmax(newtup->t_data, xmax_new_tuple);
03330 
03331     /*
03332      * Replace cid with a combo cid if necessary.  Note that we already put
03333      * the plain cid into the new tuple.
03334      */
03335     HeapTupleHeaderAdjustCmax(oldtup.t_data, &cid, &iscombo);
03336 
03337     /*
03338      * If the toaster needs to be activated, OR if the new tuple will not fit
03339      * on the same page as the old, then we need to release the content lock
03340      * (but not the pin!) on the old tuple's buffer while we are off doing
03341      * TOAST and/or table-file-extension work.  We must mark the old tuple to
03342      * show that it's already being updated, else other processes may try to
03343      * update it themselves.
03344      *
03345      * We need to invoke the toaster if there are already any out-of-line
03346      * toasted values present, or if the new tuple is over-threshold.
03347      */
03348     if (relation->rd_rel->relkind != RELKIND_RELATION &&
03349         relation->rd_rel->relkind != RELKIND_MATVIEW)
03350     {
03351         /* toast table entries should never be recursively toasted */
03352         Assert(!HeapTupleHasExternal(&oldtup));
03353         Assert(!HeapTupleHasExternal(newtup));
03354         need_toast = false;
03355     }
03356     else
03357         need_toast = (HeapTupleHasExternal(&oldtup) ||
03358                       HeapTupleHasExternal(newtup) ||
03359                       newtup->t_len > TOAST_TUPLE_THRESHOLD);
03360 
03361     pagefree = PageGetHeapFreeSpace(page);
03362 
03363     newtupsize = MAXALIGN(newtup->t_len);
03364 
03365     if (need_toast || newtupsize > pagefree)
03366     {
03367         /* Clear obsolete visibility flags ... */
03368         oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
03369         oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
03370         HeapTupleClearHotUpdated(&oldtup);
03371         /* ... and store info about transaction updating this tuple */
03372         Assert(TransactionIdIsValid(xmax_old_tuple));
03373         HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
03374         oldtup.t_data->t_infomask |= infomask_old_tuple;
03375         oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
03376         HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
03377         /* temporarily make it look not-updated */
03378         oldtup.t_data->t_ctid = oldtup.t_self;
03379         already_marked = true;
03380         LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
03381 
03382         /*
03383          * Let the toaster do its thing, if needed.
03384          *
03385          * Note: below this point, heaptup is the data we actually intend to
03386          * store into the relation; newtup is the caller's original untoasted
03387          * data.
03388          */
03389         if (need_toast)
03390         {
03391             /* Note we always use WAL and FSM during updates */
03392             heaptup = toast_insert_or_update(relation, newtup, &oldtup, 0);
03393             newtupsize = MAXALIGN(heaptup->t_len);
03394         }
03395         else
03396             heaptup = newtup;
03397 
03398         /*
03399          * Now, do we need a new page for the tuple, or not?  This is a bit
03400          * tricky since someone else could have added tuples to the page while
03401          * we weren't looking.  We have to recheck the available space after
03402          * reacquiring the buffer lock.  But don't bother to do that if the
03403          * former amount of free space is still not enough; it's unlikely
03404          * there's more free now than before.
03405          *
03406          * What's more, if we need to get a new page, we will need to acquire
03407          * buffer locks on both old and new pages.  To avoid deadlock against
03408          * some other backend trying to get the same two locks in the other
03409          * order, we must be consistent about the order we get the locks in.
03410          * We use the rule "lock the lower-numbered page of the relation
03411          * first".  To implement this, we must do RelationGetBufferForTuple
03412          * while not holding the lock on the old page, and we must rely on it
03413          * to get the locks on both pages in the correct order.
03414          */
03415         if (newtupsize > pagefree)
03416         {
03417             /* Assume there's no chance to put heaptup on same page. */
03418             newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
03419                                                buffer, 0, NULL,
03420                                                &vmbuffer_new, &vmbuffer);
03421         }
03422         else
03423         {
03424             /* Re-acquire the lock on the old tuple's page. */
03425             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
03426             /* Re-check using the up-to-date free space */
03427             pagefree = PageGetHeapFreeSpace(page);
03428             if (newtupsize > pagefree)
03429             {
03430                 /*
03431                  * Rats, it doesn't fit anymore.  We must now unlock and
03432                  * relock to avoid deadlock.  Fortunately, this path should
03433                  * seldom be taken.
03434                  */
03435                 LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
03436                 newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
03437                                                    buffer, 0, NULL,
03438                                                    &vmbuffer_new, &vmbuffer);
03439             }
03440             else
03441             {
03442                 /* OK, it fits here, so we're done. */
03443                 newbuf = buffer;
03444             }
03445         }
03446     }
03447     else
03448     {
03449         /* No TOAST work needed, and it'll fit on same page */
03450         already_marked = false;
03451         newbuf = buffer;
03452         heaptup = newtup;
03453     }
03454 
03455     /*
03456      * We're about to create the new tuple -- check for conflict first, to
03457      * avoid possibly having to roll back work we've just done.
03458      *
03459      * NOTE: For a tuple insert, we only need to check for table locks, since
03460      * predicate locking at the index level will cover ranges for anything
03461      * except a table scan.  Therefore, only provide the relation.
03462      */
03463     CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);
03464 
03465     /*
03466      * At this point newbuf and buffer are both pinned and locked, and newbuf
03467      * has enough space for the new tuple.  If they are the same buffer, only
03468      * one pin is held.
03469      */
03470 
03471     if (newbuf == buffer)
03472     {
03473         /*
03474          * Since the new tuple is going into the same page, we might be able
03475          * to do a HOT update.  Check if any of the index columns have been
03476          * changed.  If not, then HOT update is possible.
03477          */
03478         if (satisfies_hot)
03479             use_hot_update = true;
03480     }
03481     else
03482     {
03483         /* Set a hint that the old page could use prune/defrag */
03484         PageSetFull(page);
03485     }
03486 
03487     /* NO EREPORT(ERROR) from here till changes are logged */
03488     START_CRIT_SECTION();
03489 
03490     /*
03491      * If this transaction commits, the old tuple will become DEAD sooner or
03492      * later.  Set flag that this page is a candidate for pruning once our xid
03493      * falls below the OldestXmin horizon.  If the transaction finally aborts,
03494      * the subsequent page pruning will be a no-op and the hint will be
03495      * cleared.
03496      *
03497      * XXX Should we set hint on newbuf as well?  If the transaction aborts,
03498      * there would be a prunable tuple in the newbuf; but for now we choose
03499      * not to optimize for aborts.  Note that heap_xlog_update must be kept in
03500      * sync if this decision changes.
03501      */
03502     PageSetPrunable(page, xid);
03503 
03504     if (use_hot_update)
03505     {
03506         /* Mark the old tuple as HOT-updated */
03507         HeapTupleSetHotUpdated(&oldtup);
03508         /* And mark the new tuple as heap-only */
03509         HeapTupleSetHeapOnly(heaptup);
03510         /* Mark the caller's copy too, in case different from heaptup */
03511         HeapTupleSetHeapOnly(newtup);
03512     }
03513     else
03514     {
03515         /* Make sure tuples are correctly marked as not-HOT */
03516         HeapTupleClearHotUpdated(&oldtup);
03517         HeapTupleClearHeapOnly(heaptup);
03518         HeapTupleClearHeapOnly(newtup);
03519     }
03520 
03521     RelationPutHeapTuple(relation, newbuf, heaptup);    /* insert new tuple */
03522 
03523     if (!already_marked)
03524     {
03525         /* Clear obsolete visibility flags ... */
03526         oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
03527         oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
03528         /* ... and store info about transaction updating this tuple */
03529         Assert(TransactionIdIsValid(xmax_old_tuple));
03530         HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
03531         oldtup.t_data->t_infomask |= infomask_old_tuple;
03532         oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
03533         HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
03534     }
03535 
03536     /* record address of new tuple in t_ctid of old one */
03537     oldtup.t_data->t_ctid = heaptup->t_self;
03538 
03539     /* clear PD_ALL_VISIBLE flags */
03540     if (PageIsAllVisible(BufferGetPage(buffer)))
03541     {
03542         all_visible_cleared = true;
03543         PageClearAllVisible(BufferGetPage(buffer));
03544         visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
03545                             vmbuffer);
03546     }
03547     if (newbuf != buffer && PageIsAllVisible(BufferGetPage(newbuf)))
03548     {
03549         all_visible_cleared_new = true;
03550         PageClearAllVisible(BufferGetPage(newbuf));
03551         visibilitymap_clear(relation, BufferGetBlockNumber(newbuf),
03552                             vmbuffer_new);
03553     }
03554 
03555     if (newbuf != buffer)
03556         MarkBufferDirty(newbuf);
03557     MarkBufferDirty(buffer);
03558 
03559     /* XLOG stuff */
03560     if (RelationNeedsWAL(relation))
03561     {
03562         XLogRecPtr  recptr = log_heap_update(relation, buffer,
03563                                              newbuf, &oldtup, heaptup,
03564                                              all_visible_cleared,
03565                                              all_visible_cleared_new);
03566 
03567         if (newbuf != buffer)
03568         {
03569             PageSetLSN(BufferGetPage(newbuf), recptr);
03570         }
03571         PageSetLSN(BufferGetPage(buffer), recptr);
03572     }
03573 
03574     END_CRIT_SECTION();
03575 
03576     if (newbuf != buffer)
03577         LockBuffer(newbuf, BUFFER_LOCK_UNLOCK);
03578     LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
03579 
03580     /*
03581      * Mark old tuple for invalidation from system caches at next command
03582      * boundary, and mark the new tuple for invalidation in case we abort. We
03583      * have to do this before releasing the buffer because oldtup is in the
03584      * buffer.  (heaptup is all in local memory, but it's necessary to process
03585      * both tuple versions in one call to inval.c so we can avoid redundant
03586      * sinval messages.)
03587      */
03588     CacheInvalidateHeapTuple(relation, &oldtup, heaptup);
03589 
03590     /* Now we can release the buffer(s) */
03591     if (newbuf != buffer)
03592         ReleaseBuffer(newbuf);
03593     ReleaseBuffer(buffer);
03594     if (BufferIsValid(vmbuffer_new))
03595         ReleaseBuffer(vmbuffer_new);
03596     if (BufferIsValid(vmbuffer))
03597         ReleaseBuffer(vmbuffer);
03598 
03599     /*
03600      * Release the lmgr tuple lock, if we had it.
03601      */
03602     if (have_tuple_lock)
03603         UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
03604 
03605     pgstat_count_heap_update(relation, use_hot_update);
03606 
03607     /*
03608      * If heaptup is a private copy, release it.  Don't forget to copy t_self
03609      * back to the caller's image, too.
03610      */
03611     if (heaptup != newtup)
03612     {
03613         newtup->t_self = heaptup->t_self;
03614         heap_freetuple(heaptup);
03615     }
03616 
03617     bms_free(hot_attrs);
03618     bms_free(key_attrs);
03619 
03620     return HeapTupleMayBeUpdated;
03621 }
03622 
03623 /*
03624  * Check if the specified attribute's value is same in both given tuples.
03625  * Subroutine for HeapSatisfiesHOTandKeyUpdate.
03626  */
03627 static bool
03628 heap_tuple_attr_equals(TupleDesc tupdesc, int attrnum,
03629                        HeapTuple tup1, HeapTuple tup2)
03630 {
03631     Datum       value1,
03632                 value2;
03633     bool        isnull1,
03634                 isnull2;
03635     Form_pg_attribute att;
03636 
03637     /*
03638      * If it's a whole-tuple reference, say "not equal".  It's not really
03639      * worth supporting this case, since it could only succeed after a no-op
03640      * update, which is hardly a case worth optimizing for.
03641      */
03642     if (attrnum == 0)
03643         return false;
03644 
03645     /*
03646      * Likewise, automatically say "not equal" for any system attribute other
03647      * than OID and tableOID; we cannot expect these to be consistent in a HOT
03648      * chain, or even to be set correctly yet in the new tuple.
03649      */
03650     if (attrnum < 0)
03651     {
03652         if (attrnum != ObjectIdAttributeNumber &&
03653             attrnum != TableOidAttributeNumber)
03654             return false;
03655     }
03656 
03657     /*
03658      * Extract the corresponding values.  XXX this is pretty inefficient if
03659      * there are many indexed columns.  Should HeapSatisfiesHOTandKeyUpdate do a
03660      * single heap_deform_tuple call on each tuple, instead?  But that doesn't
03661      * work for system columns ...
03662      */
03663     value1 = heap_getattr(tup1, attrnum, tupdesc, &isnull1);
03664     value2 = heap_getattr(tup2, attrnum, tupdesc, &isnull2);
03665 
03666     /*
03667      * If one value is NULL and other is not, then they are certainly not
03668      * equal
03669      */
03670     if (isnull1 != isnull2)
03671         return false;
03672 
03673     /*
03674      * If both are NULL, they can be considered equal.
03675      */
03676     if (isnull1)
03677         return true;
03678 
03679     /*
03680      * We do simple binary comparison of the two datums.  This may be overly
03681      * strict because there can be multiple binary representations for the
03682      * same logical value.  But we should be OK as long as there are no false
03683      * positives.  Using a type-specific equality operator is messy because
03684      * there could be multiple notions of equality in different operator
03685      * classes; furthermore, we cannot safely invoke user-defined functions
03686      * while holding exclusive buffer lock.
03687      */
03688     if (attrnum <= 0)
03689     {
03690         /* The only allowed system columns are OIDs, so do this */
03691         return (DatumGetObjectId(value1) == DatumGetObjectId(value2));
03692     }
03693     else
03694     {
03695         Assert(attrnum <= tupdesc->natts);
03696         att = tupdesc->attrs[attrnum - 1];
03697         return datumIsEqual(value1, value2, att->attbyval, att->attlen);
03698     }
03699 }
03700 
03701 /*
03702  * Check which columns are being updated.
03703  *
03704  * This simultaneously checks conditions for HOT updates and for FOR KEY
03705  * SHARE updates.  Since much of the time they will be checking very similar
03706  * sets of columns, and doing the same tests on them, it makes sense to
03707  * optimize and do them together.
03708  *
03709  * We receive two bitmapsets comprising the two sets of columns we're
03710  * interested in.  Note these are destructively modified; that is OK since
03711  * this is invoked at most once in heap_update.
03712  *
03713  * hot_result is set to TRUE if it's okay to do a HOT update (i.e. it does not
03714  * modified indexed columns); key_result is set to TRUE if the update does not
03715  * modify columns used in the key.
03716  */
03717 static void
03718 HeapSatisfiesHOTandKeyUpdate(Relation relation,
03719                              Bitmapset *hot_attrs, Bitmapset *key_attrs,
03720                              bool *satisfies_hot, bool *satisfies_key,
03721                              HeapTuple oldtup, HeapTuple newtup)
03722 {
03723     int     next_hot_attnum;
03724     int     next_key_attnum;
03725     bool    hot_result = true;
03726     bool    key_result = true;
03727     bool    key_done = false;
03728     bool    hot_done = false;
03729 
03730     next_hot_attnum = bms_first_member(hot_attrs);
03731     if (next_hot_attnum == -1)
03732         hot_done = true;
03733     else
03734         /* Adjust for system attributes */
03735         next_hot_attnum += FirstLowInvalidHeapAttributeNumber;
03736 
03737     next_key_attnum = bms_first_member(key_attrs);
03738     if (next_key_attnum == -1)
03739         key_done = true;
03740     else
03741         /* Adjust for system attributes */
03742         next_key_attnum += FirstLowInvalidHeapAttributeNumber;
03743 
03744     for (;;)
03745     {
03746         int     check_now;
03747         bool    changed;
03748 
03749         /* both bitmapsets are now empty */
03750         if (key_done && hot_done)
03751             break;
03752 
03753         /* XXX there's probably an easier way ... */
03754         if (hot_done)
03755             check_now = next_key_attnum;
03756         if (key_done)
03757             check_now = next_hot_attnum;
03758         else
03759             check_now = Min(next_hot_attnum, next_key_attnum);
03760 
03761         changed = !heap_tuple_attr_equals(RelationGetDescr(relation),
03762                                           check_now, oldtup, newtup);
03763         if (changed)
03764         {
03765             if (check_now == next_hot_attnum)
03766                 hot_result = false;
03767             if (check_now == next_key_attnum)
03768                 key_result = false;
03769         }
03770 
03771         /* if both are false now, we can stop checking */
03772         if (!hot_result && !key_result)
03773             break;
03774 
03775         if (check_now == next_hot_attnum)
03776         {
03777             next_hot_attnum = bms_first_member(hot_attrs);
03778             if (next_hot_attnum == -1)
03779                 hot_done = true;
03780             else
03781                 /* Adjust for system attributes */
03782                 next_hot_attnum += FirstLowInvalidHeapAttributeNumber;
03783         }
03784         if (check_now == next_key_attnum)
03785         {
03786             next_key_attnum = bms_first_member(key_attrs);
03787             if (next_key_attnum == -1)
03788                 key_done = true;
03789             else
03790                 /* Adjust for system attributes */
03791                 next_key_attnum += FirstLowInvalidHeapAttributeNumber;
03792         }
03793     }
03794 
03795     *satisfies_hot = hot_result;
03796     *satisfies_key = key_result;
03797 }
03798 
03799 /*
03800  *  simple_heap_update - replace a tuple
03801  *
03802  * This routine may be used to update a tuple when concurrent updates of
03803  * the target tuple are not expected (for example, because we have a lock
03804  * on the relation associated with the tuple).  Any failure is reported
03805  * via ereport().
03806  */
03807 void
03808 simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
03809 {
03810     HTSU_Result result;
03811     HeapUpdateFailureData hufd;
03812     LockTupleMode lockmode;
03813 
03814     result = heap_update(relation, otid, tup,
03815                          GetCurrentCommandId(true), InvalidSnapshot,
03816                          true /* wait for commit */,
03817                          &hufd, &lockmode);
03818     switch (result)
03819     {
03820         case HeapTupleSelfUpdated:
03821             /* Tuple was already updated in current command? */
03822             elog(ERROR, "tuple already updated by self");
03823             break;
03824 
03825         case HeapTupleMayBeUpdated:
03826             /* done successfully */
03827             break;
03828 
03829         case HeapTupleUpdated:
03830             elog(ERROR, "tuple concurrently updated");
03831             break;
03832 
03833         default:
03834             elog(ERROR, "unrecognized heap_update status: %u", result);
03835             break;
03836     }
03837 }
03838 
03839 
03840 /*
03841  * Return the MultiXactStatus corresponding to the given tuple lock mode.
03842  */
03843 static MultiXactStatus
03844 get_mxact_status_for_lock(LockTupleMode mode, bool is_update)
03845 {
03846     MultiXactStatus     retval;
03847 
03848     if (is_update)
03849         retval = tupleLockExtraInfo[mode].updstatus;
03850     else
03851         retval = tupleLockExtraInfo[mode].lockstatus;
03852 
03853     if (retval == -1)
03854         elog(ERROR, "invalid lock tuple mode %d/%s", mode,
03855              is_update ? "true" : "false");
03856 
03857     return retval;
03858 }
03859 
03860 
03861 /*
03862  *  heap_lock_tuple - lock a tuple in shared or exclusive mode
03863  *
03864  * Note that this acquires a buffer pin, which the caller must release.
03865  *
03866  * Input parameters:
03867  *  relation: relation containing tuple (caller must hold suitable lock)
03868  *  tuple->t_self: TID of tuple to lock (rest of struct need not be valid)
03869  *  cid: current command ID (used for visibility test, and stored into
03870  *      tuple's cmax if lock is successful)
03871  *  mode: indicates if shared or exclusive tuple lock is desired
03872  *  nowait: if true, ereport rather than blocking if lock not available
03873  *  follow_updates: if true, follow the update chain to also lock descendant
03874  *      tuples.
03875  *
03876  * Output parameters:
03877  *  *tuple: all fields filled in
03878  *  *buffer: set to buffer holding tuple (pinned but not locked at exit)
03879  *  *hufd: filled in failure cases (see below)
03880  *
03881  * Function result may be:
03882  *  HeapTupleMayBeUpdated: lock was successfully acquired
03883  *  HeapTupleSelfUpdated: lock failed because tuple updated by self
03884  *  HeapTupleUpdated: lock failed because tuple updated by other xact
03885  *
03886  * In the failure cases, the routine fills *hufd with the tuple's t_ctid,
03887  * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax
03888  * (the last only for HeapTupleSelfUpdated, since we
03889  * cannot obtain cmax from a combocid generated by another transaction).
03890  * See comments for struct HeapUpdateFailureData for additional info.
03891  *
03892  * See README.tuplock for a thorough explanation of this mechanism.
03893  */
03894 HTSU_Result
03895 heap_lock_tuple(Relation relation, HeapTuple tuple,
03896                 CommandId cid, LockTupleMode mode, bool nowait,
03897                 bool follow_updates,
03898                 Buffer *buffer, HeapUpdateFailureData *hufd)
03899 {
03900     HTSU_Result result;
03901     ItemPointer tid = &(tuple->t_self);
03902     ItemId      lp;
03903     Page        page;
03904     TransactionId xid,
03905                 xmax;
03906     uint16      old_infomask,
03907                 new_infomask,
03908                 new_infomask2;
03909     bool        have_tuple_lock = false;
03910 
03911     *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
03912     LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
03913 
03914     page = BufferGetPage(*buffer);
03915     lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid));
03916     Assert(ItemIdIsNormal(lp));
03917 
03918     tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
03919     tuple->t_len = ItemIdGetLength(lp);
03920     tuple->t_tableOid = RelationGetRelid(relation);
03921 
03922 l3:
03923     result = HeapTupleSatisfiesUpdate(tuple->t_data, cid, *buffer);
03924 
03925     if (result == HeapTupleInvisible)
03926     {
03927         UnlockReleaseBuffer(*buffer);
03928         elog(ERROR, "attempted to lock invisible tuple");
03929     }
03930     else if (result == HeapTupleBeingUpdated)
03931     {
03932         TransactionId xwait;
03933         uint16      infomask;
03934         uint16      infomask2;
03935         bool        require_sleep;
03936         ItemPointerData t_ctid;
03937 
03938         /* must copy state data before unlocking buffer */
03939         xwait = HeapTupleHeaderGetRawXmax(tuple->t_data);
03940         infomask = tuple->t_data->t_infomask;
03941         infomask2 = tuple->t_data->t_infomask2;
03942         ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid);
03943 
03944         LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
03945 
03946         /*
03947          * If any subtransaction of the current top transaction already holds a
03948          * lock as strong or stronger than what we're requesting, we
03949          * effectively hold the desired lock already.  We *must* succeed
03950          * without trying to take the tuple lock, else we will deadlock against
03951          * anyone wanting to acquire a stronger lock.
03952          */
03953         if (infomask & HEAP_XMAX_IS_MULTI)
03954         {
03955             int     i;
03956             int     nmembers;
03957             MultiXactMember *members;
03958 
03959             /*
03960              * We don't need to allow old multixacts here; if that had been the
03961              * case, HeapTupleSatisfiesUpdate would have returned MayBeUpdated
03962              * and we wouldn't be here.
03963              */
03964             nmembers = GetMultiXactIdMembers(xwait, &members, false);
03965 
03966             for (i = 0; i < nmembers; i++)
03967             {
03968                 if (TransactionIdIsCurrentTransactionId(members[i].xid))
03969                 {
03970                     LockTupleMode   membermode;
03971 
03972                     membermode = TUPLOCK_from_mxstatus(members[i].status);
03973 
03974                     if (membermode >= mode)
03975                     {
03976                         if (have_tuple_lock)
03977                             UnlockTupleTuplock(relation, tid, mode);
03978 
03979                         pfree(members);
03980                         return HeapTupleMayBeUpdated;
03981                     }
03982                 }
03983             }
03984 
03985             pfree(members);
03986         }
03987 
03988         /*
03989          * Acquire tuple lock to establish our priority for the tuple.
03990          * LockTuple will release us when we are next-in-line for the tuple.
03991          * We must do this even if we are share-locking.
03992          *
03993          * If we are forced to "start over" below, we keep the tuple lock;
03994          * this arranges that we stay at the head of the line while rechecking
03995          * tuple state.
03996          */
03997         if (!have_tuple_lock)
03998         {
03999             if (nowait)
04000             {
04001                 if (!ConditionalLockTupleTuplock(relation, tid, mode))
04002                     ereport(ERROR,
04003                             (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
04004                              errmsg("could not obtain lock on row in relation \"%s\"",
04005                                     RelationGetRelationName(relation))));
04006             }
04007             else
04008                 LockTupleTuplock(relation, tid, mode);
04009             have_tuple_lock = true;
04010         }
04011 
04012         /*
04013          * Initially assume that we will have to wait for the locking
04014          * transaction(s) to finish.  We check various cases below in which
04015          * this can be turned off.
04016          */
04017         require_sleep = true;
04018         if (mode == LockTupleKeyShare)
04019         {
04020             /*
04021              * If we're requesting KeyShare, and there's no update present, we
04022              * don't need to wait.  Even if there is an update, we can still
04023              * continue if the key hasn't been modified.
04024              *
04025              * However, if there are updates, we need to walk the update chain
04026              * to mark future versions of the row as locked, too.  That way, if
04027              * somebody deletes that future version, we're protected against
04028              * the key going away.  This locking of future versions could block
04029              * momentarily, if a concurrent transaction is deleting a key; or
04030              * it could return a value to the effect that the transaction
04031              * deleting the key has already committed.  So we do this before
04032              * re-locking the buffer; otherwise this would be prone to
04033              * deadlocks.
04034              *
04035              * Note that the TID we're locking was grabbed before we unlocked
04036              * the buffer.  For it to change while we're not looking, the other
04037              * properties we're testing for below after re-locking the buffer
04038              * would also change, in which case we would restart this loop
04039              * above.
04040              */
04041             if (!(infomask2 & HEAP_KEYS_UPDATED))
04042             {
04043                 bool    updated;
04044 
04045                 updated = !HEAP_XMAX_IS_LOCKED_ONLY(infomask);
04046 
04047                 /*
04048                  * If there are updates, follow the update chain; bail out
04049                  * if that cannot be done.
04050                  */
04051                 if (follow_updates && updated)
04052                 {
04053                     HTSU_Result     res;
04054 
04055                     res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
04056                                                   GetCurrentTransactionId(),
04057                                                   mode);
04058                     if (res != HeapTupleMayBeUpdated)
04059                     {
04060                         result = res;
04061                         /* recovery code expects to have buffer lock held */
04062                         LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04063                         goto failed;
04064                     }
04065                 }
04066 
04067                 LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04068 
04069                 /*
04070                  * Make sure it's still an appropriate lock, else start over.
04071                  * Also, if it wasn't updated before we released the lock, but
04072                  * is updated now, we start over too; the reason is that we now
04073                  * need to follow the update chain to lock the new versions.
04074                  */
04075                 if (!HeapTupleHeaderIsOnlyLocked(tuple->t_data) &&
04076                     ((tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED) ||
04077                      !updated))
04078                     goto l3;
04079 
04080                 /* Things look okay, so we can skip sleeping */
04081                 require_sleep = false;
04082 
04083                 /*
04084                  * Note we allow Xmax to change here; other updaters/lockers
04085                  * could have modified it before we grabbed the buffer lock.
04086                  * However, this is not a problem, because with the recheck we
04087                  * just did we ensure that they still don't conflict with the
04088                  * lock we want.
04089                  */
04090             }
04091         }
04092         else if (mode == LockTupleShare)
04093         {
04094             /*
04095              * If we're requesting Share, we can similarly avoid sleeping if
04096              * there's no update and no exclusive lock present.
04097              */
04098             if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) &&
04099                 !HEAP_XMAX_IS_EXCL_LOCKED(infomask))
04100             {
04101                 LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04102 
04103                 /*
04104                  * Make sure it's still an appropriate lock, else start over.
04105                  * See above about allowing xmax to change.
04106                  */
04107                 if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) ||
04108                     HEAP_XMAX_IS_EXCL_LOCKED(tuple->t_data->t_infomask))
04109                     goto l3;
04110                 require_sleep = false;
04111             }
04112         }
04113         else if (mode == LockTupleNoKeyExclusive)
04114         {
04115             /*
04116              * If we're requesting NoKeyExclusive, we might also be able to
04117              * avoid sleeping; just ensure that there's no other lock type than
04118              * KeyShare.  Note that this is a bit more involved than just
04119              * checking hint bits -- we need to expand the multixact to figure
04120              * out lock modes for each one (unless there was only one such
04121              * locker).
04122              */
04123             if (infomask & HEAP_XMAX_IS_MULTI)
04124             {
04125                 int     nmembers;
04126                 MultiXactMember *members;
04127 
04128                 /*
04129                  * We don't need to allow old multixacts here; if that had been
04130                  * the case, HeapTupleSatisfiesUpdate would have returned
04131                  * MayBeUpdated and we wouldn't be here.
04132                  */
04133                 nmembers = GetMultiXactIdMembers(xwait, &members, false);
04134 
04135                 if (nmembers <= 0)
04136                 {
04137                     /*
04138                      * No need to keep the previous xmax here. This is unlikely
04139                      * to happen.
04140                      */
04141                     require_sleep = false;
04142                 }
04143                 else
04144                 {
04145                     int     i;
04146                     bool    allowed = true;
04147 
04148                     for (i = 0; i < nmembers; i++)
04149                     {
04150                         if (members[i].status != MultiXactStatusForKeyShare)
04151                         {
04152                             allowed = false;
04153                             break;
04154                         }
04155                     }
04156                     if (allowed)
04157                     {
04158                         /*
04159                          * if the xmax changed under us in the meantime, start
04160                          * over.
04161                          */
04162                         LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04163                         if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
04164                             !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
04165                                                  xwait))
04166                         {
04167                             pfree(members);
04168                             goto l3;
04169                         }
04170                         /* otherwise, we're good */
04171                         require_sleep = false;
04172                     }
04173 
04174                     pfree(members);
04175                 }
04176             }
04177             else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
04178             {
04179                 LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04180 
04181                 /* if the xmax changed in the meantime, start over */
04182                 if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
04183                     !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
04184                                          xwait))
04185                     goto l3;
04186                 /* otherwise, we're good */
04187                 require_sleep = false;
04188             }
04189         }
04190 
04191         /*
04192          * By here, we either have already acquired the buffer exclusive lock,
04193          * or we must wait for the locking transaction or multixact; so below
04194          * we ensure that we grab buffer lock after the sleep.
04195          */
04196 
04197         if (require_sleep)
04198         {
04199             if (infomask & HEAP_XMAX_IS_MULTI)
04200             {
04201                 MultiXactStatus status = get_mxact_status_for_lock(mode, false);
04202 
04203                 /* We only ever lock tuples, never update them */
04204                 if (status >= MultiXactStatusNoKeyUpdate)
04205                     elog(ERROR, "invalid lock mode in heap_lock_tuple");
04206 
04207                 /* wait for multixact to end */
04208                 if (nowait)
04209                 {
04210                     if (!ConditionalMultiXactIdWait((MultiXactId) xwait,
04211                                                     status, NULL, infomask))
04212                         ereport(ERROR,
04213                                 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
04214                                  errmsg("could not obtain lock on row in relation \"%s\"",
04215                                         RelationGetRelationName(relation))));
04216                 }
04217                 else
04218                     MultiXactIdWait((MultiXactId) xwait, status, NULL, infomask);
04219 
04220                 /* if there are updates, follow the update chain */
04221                 if (follow_updates &&
04222                     !HEAP_XMAX_IS_LOCKED_ONLY(infomask))
04223                 {
04224                     HTSU_Result     res;
04225 
04226                     res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
04227                                                   GetCurrentTransactionId(),
04228                                                   mode);
04229                     if (res != HeapTupleMayBeUpdated)
04230                     {
04231                         result = res;
04232                         /* recovery code expects to have buffer lock held */
04233                         LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04234                         goto failed;
04235                     }
04236                 }
04237 
04238                 LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04239 
04240                 /*
04241                  * If xwait had just locked the tuple then some other xact
04242                  * could update this tuple before we get to this point. Check
04243                  * for xmax change, and start over if so.
04244                  */
04245                 if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
04246                     !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
04247                                          xwait))
04248                     goto l3;
04249 
04250                 /*
04251                  * Of course, the multixact might not be done here: if we're
04252                  * requesting a light lock mode, other transactions with light
04253                  * locks could still be alive, as well as locks owned by our
04254                  * own xact or other subxacts of this backend.  We need to
04255                  * preserve the surviving MultiXact members.  Note that it
04256                  * isn't absolutely necessary in the latter case, but doing so
04257                  * is simpler.
04258                  */
04259             }
04260             else
04261             {
04262                 /* wait for regular transaction to end */
04263                 if (nowait)
04264                 {
04265                     if (!ConditionalXactLockTableWait(xwait))
04266                         ereport(ERROR,
04267                                 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
04268                                  errmsg("could not obtain lock on row in relation \"%s\"",
04269                                         RelationGetRelationName(relation))));
04270                 }
04271                 else
04272                     XactLockTableWait(xwait);
04273 
04274                 /* if there are updates, follow the update chain */
04275                 if (follow_updates &&
04276                     !HEAP_XMAX_IS_LOCKED_ONLY(infomask))
04277                 {
04278                     HTSU_Result     res;
04279 
04280                     res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
04281                                                   GetCurrentTransactionId(),
04282                                                   mode);
04283                     if (res != HeapTupleMayBeUpdated)
04284                     {
04285                         result = res;
04286                         /* recovery code expects to have buffer lock held */
04287                         LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04288                         goto failed;
04289                     }
04290                 }
04291 
04292                 LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
04293 
04294                 /*
04295                  * xwait is done, but if xwait had just locked the tuple then
04296                  * some other xact could update this tuple before we get to
04297                  * this point.  Check for xmax change, and start over if so.
04298                  */
04299                 if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
04300                     !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
04301                                          xwait))
04302                     goto l3;
04303 
04304                 /*
04305                  * Otherwise check if it committed or aborted.  Note we cannot
04306                  * be here if the tuple was only locked by somebody who didn't
04307                  * conflict with us; that should have been handled above.  So
04308                  * that transaction must necessarily be gone by now.
04309                  */
04310                 UpdateXmaxHintBits(tuple->t_data, *buffer, xwait);
04311             }
04312         }
04313 
04314         /* By here, we're certain that we hold buffer exclusive lock again */
04315 
04316         /*
04317          * We may lock if previous xmax aborted, or if it committed but only
04318          * locked the tuple without updating it; or if we didn't have to wait
04319          * at all for whatever reason.
04320          */
04321         if (!require_sleep ||
04322             (tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
04323             HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) ||
04324             HeapTupleHeaderIsOnlyLocked(tuple->t_data))
04325             result = HeapTupleMayBeUpdated;
04326         else
04327             result = HeapTupleUpdated;
04328     }
04329 
04330 failed:
04331     if (result != HeapTupleMayBeUpdated)
04332     {
04333         Assert(result == HeapTupleSelfUpdated || result == HeapTupleUpdated);
04334         Assert(!(tuple->t_data->t_infomask & HEAP_XMAX_INVALID));
04335         hufd->ctid = tuple->t_data->t_ctid;
04336         hufd->xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
04337         if (result == HeapTupleSelfUpdated)
04338             hufd->cmax = HeapTupleHeaderGetCmax(tuple->t_data);
04339         else
04340             hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
04341         LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
04342         if (have_tuple_lock)
04343             UnlockTupleTuplock(relation, tid, mode);
04344         return result;
04345     }
04346 
04347     xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
04348     old_infomask = tuple->t_data->t_infomask;
04349 
04350     /*
04351      * We might already hold the desired lock (or stronger), possibly under a
04352      * different subtransaction of the current top transaction.  If so, there
04353      * is no need to change state or issue a WAL record.  We already handled
04354      * the case where this is true for xmax being a MultiXactId, so now check
04355      * for cases where it is a plain TransactionId.
04356      *
04357      * Note in particular that this covers the case where we already hold
04358      * exclusive lock on the tuple and the caller only wants key share or share
04359      * lock. It would certainly not do to give up the exclusive lock.
04360      */
04361     if (!(old_infomask & (HEAP_XMAX_INVALID |
04362                           HEAP_XMAX_COMMITTED |
04363                           HEAP_XMAX_IS_MULTI)) &&
04364         (mode == LockTupleKeyShare ?
04365          (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask) ||
04366           HEAP_XMAX_IS_SHR_LOCKED(old_infomask) ||
04367           HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) :
04368          mode == LockTupleShare ?
04369          (HEAP_XMAX_IS_SHR_LOCKED(old_infomask) ||
04370           HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) :
04371          (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))) &&
04372         TransactionIdIsCurrentTransactionId(xmax))
04373     {
04374         LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
04375         /* Probably can't hold tuple lock here, but may as well check */
04376         if (have_tuple_lock)
04377             UnlockTupleTuplock(relation, tid, mode);
04378         return HeapTupleMayBeUpdated;
04379     }
04380 
04381     /*
04382      * If this is the first possibly-multixact-able operation in the
04383      * current transaction, set my per-backend OldestMemberMXactId setting.
04384      * We can be certain that the transaction will never become a member of
04385      * any older MultiXactIds than that.  (We have to do this even if we
04386      * end up just using our own TransactionId below, since some other
04387      * backend could incorporate our XID into a MultiXact immediately
04388      * afterwards.)
04389      */
04390     MultiXactIdSetOldestMember();
04391 
04392     /*
04393      * Compute the new xmax and infomask to store into the tuple.  Note we do
04394      * not modify the tuple just yet, because that would leave it in the wrong
04395      * state if multixact.c elogs.
04396      */
04397     compute_new_xmax_infomask(xmax, old_infomask, tuple->t_data->t_infomask2,
04398                               GetCurrentTransactionId(), mode, false,
04399                               &xid, &new_infomask, &new_infomask2);
04400 
04401     START_CRIT_SECTION();
04402 
04403     /*
04404      * Store transaction information of xact locking the tuple.
04405      *
04406      * Note: Cmax is meaningless in this context, so don't set it; this avoids
04407      * possibly generating a useless combo CID.  Moreover, if we're locking a
04408      * previously updated tuple, it's important to preserve the Cmax.
04409      *
04410      * Also reset the HOT UPDATE bit, but only if there's no update; otherwise
04411      * we would break the HOT chain.
04412      */
04413     tuple->t_data->t_infomask &= ~HEAP_XMAX_BITS;
04414     tuple->t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
04415     tuple->t_data->t_infomask |= new_infomask;
04416     tuple->t_data->t_infomask2 |= new_infomask2;
04417     if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
04418         HeapTupleHeaderClearHotUpdated(tuple->t_data);
04419     HeapTupleHeaderSetXmax(tuple->t_data, xid);
04420 
04421     /*
04422      * Make sure there is no forward chain link in t_ctid.  Note that in the
04423      * cases where the tuple has been updated, we must not overwrite t_ctid,
04424      * because it was set by the updater.  Moreover, if the tuple has been
04425      * updated, we need to follow the update chain to lock the new versions
04426      * of the tuple as well.
04427      */
04428     if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
04429         tuple->t_data->t_ctid = *tid;
04430 
04431     MarkBufferDirty(*buffer);
04432 
04433     /*
04434      * XLOG stuff.  You might think that we don't need an XLOG record because
04435      * there is no state change worth restoring after a crash.  You would be
04436      * wrong however: we have just written either a TransactionId or a
04437      * MultiXactId that may never have been seen on disk before, and we need
04438      * to make sure that there are XLOG entries covering those ID numbers.
04439      * Else the same IDs might be re-used after a crash, which would be
04440      * disastrous if this page made it to disk before the crash.  Essentially
04441      * we have to enforce the WAL log-before-data rule even in this case.
04442      * (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG
04443      * entries for everything anyway.)
04444      */
04445     if (RelationNeedsWAL(relation))
04446     {
04447         xl_heap_lock xlrec;
04448         XLogRecPtr  recptr;
04449         XLogRecData rdata[2];
04450 
04451         xlrec.target.node = relation->rd_node;
04452         xlrec.target.tid = tuple->t_self;
04453         xlrec.locking_xid = xid;
04454         xlrec.infobits_set = compute_infobits(new_infomask,
04455                                               tuple->t_data->t_infomask2);
04456         rdata[0].data = (char *) &xlrec;
04457         rdata[0].len = SizeOfHeapLock;
04458         rdata[0].buffer = InvalidBuffer;
04459         rdata[0].next = &(rdata[1]);
04460 
04461         rdata[1].data = NULL;
04462         rdata[1].len = 0;
04463         rdata[1].buffer = *buffer;
04464         rdata[1].buffer_std = true;
04465         rdata[1].next = NULL;
04466 
04467         recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK, rdata);
04468 
04469         PageSetLSN(page, recptr);
04470     }
04471 
04472     END_CRIT_SECTION();
04473 
04474     LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
04475 
04476     /*
04477      * Don't update the visibility map here. Locking a tuple doesn't change
04478      * visibility info.
04479      */
04480 
04481     /*
04482      * Now that we have successfully marked the tuple as locked, we can
04483      * release the lmgr tuple lock, if we had it.
04484      */
04485     if (have_tuple_lock)
04486         UnlockTupleTuplock(relation, tid, mode);
04487 
04488     return HeapTupleMayBeUpdated;
04489 }
04490 
04491 
04492 /*
04493  * Given an original set of Xmax and infomask, and a transaction (identified by
04494  * add_to_xmax) acquiring a new lock of some mode, compute the new Xmax and
04495  * corresponding infomasks to use on the tuple.
04496  *
04497  * Note that this might have side effects such as creating a new MultiXactId.
04498  *
04499  * Most callers will have called HeapTupleSatisfiesUpdate before this function;
04500  * that will have set the HEAP_XMAX_INVALID bit if the xmax was a MultiXactId
04501  * but it was not running anymore. There is a race condition, which is that the
04502  * MultiXactId may have finished since then, but that uncommon case is handled
04503  * either here, or within MultiXactIdExpand.
04504  *
04505  * There is a similar race condition possible when the old xmax was a regular
04506  * TransactionId.  We test TransactionIdIsInProgress again just to narrow the
04507  * window, but it's still possible to end up creating an unnecessary
04508  * MultiXactId.  Fortunately this is harmless.
04509  */
04510 static void
04511 compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask,
04512                           uint16 old_infomask2, TransactionId add_to_xmax,
04513                           LockTupleMode mode, bool is_update,
04514                           TransactionId *result_xmax, uint16 *result_infomask,
04515                           uint16 *result_infomask2)
04516 {
04517     TransactionId   new_xmax;
04518     uint16          new_infomask,
04519                     new_infomask2;
04520 
04521 l5:
04522     new_infomask = 0;
04523     new_infomask2 = 0;
04524     if (old_infomask & HEAP_XMAX_INVALID)
04525     {
04526         /*
04527          * No previous locker; we just insert our own TransactionId.
04528          */
04529         if (is_update)
04530         {
04531             new_xmax = add_to_xmax;
04532             if (mode == LockTupleExclusive)
04533                 new_infomask2 |= HEAP_KEYS_UPDATED;
04534         }
04535         else
04536         {
04537             new_infomask |= HEAP_XMAX_LOCK_ONLY;
04538             switch (mode)
04539             {
04540                 case LockTupleKeyShare:
04541                     new_xmax = add_to_xmax;
04542                     new_infomask |= HEAP_XMAX_KEYSHR_LOCK;
04543                     break;
04544                 case LockTupleShare:
04545                     new_xmax = add_to_xmax;
04546                     new_infomask |= HEAP_XMAX_SHR_LOCK;
04547                     break;
04548                 case LockTupleNoKeyExclusive:
04549                     new_xmax = add_to_xmax;
04550                     new_infomask |= HEAP_XMAX_EXCL_LOCK;
04551                     break;
04552                 case LockTupleExclusive:
04553                     new_xmax = add_to_xmax;
04554                     new_infomask |= HEAP_XMAX_EXCL_LOCK;
04555                     new_infomask2 |= HEAP_KEYS_UPDATED;
04556                     break;
04557                 default:
04558                     new_xmax = InvalidTransactionId;    /* silence compiler */
04559                     elog(ERROR, "invalid lock mode");
04560             }
04561         }
04562     }
04563     else if (old_infomask & HEAP_XMAX_IS_MULTI)
04564     {
04565         MultiXactStatus     new_status;
04566 
04567         /*
04568          * Currently we don't allow XMAX_COMMITTED to be set for multis,
04569          * so cross-check.
04570          */
04571         Assert(!(old_infomask & HEAP_XMAX_COMMITTED));
04572 
04573         /*
04574          * A multixact together with LOCK_ONLY set but neither lock bit set
04575          * (i.e. a pg_upgraded share locked tuple) cannot possibly be running
04576          * anymore.  This check is critical for databases upgraded by
04577          * pg_upgrade; both MultiXactIdIsRunning and MultiXactIdExpand assume
04578          * that such multis are never passed.
04579          */
04580         if (!(old_infomask & HEAP_LOCK_MASK) &&
04581             HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
04582         {
04583             old_infomask &= ~HEAP_XMAX_IS_MULTI;
04584             old_infomask |= HEAP_XMAX_INVALID;
04585             goto l5;
04586         }
04587 
04588         /*
04589          * If the XMAX is already a MultiXactId, then we need to expand it to
04590          * include add_to_xmax; but if all the members were lockers and are all
04591          * gone, we can do away with the IS_MULTI bit and just set add_to_xmax
04592          * as the only locker/updater.  If all lockers are gone and we have an
04593          * updater that aborted, we can also do without a multi.
04594          *
04595          * The cost of doing GetMultiXactIdMembers would be paid by
04596          * MultiXactIdExpand if we weren't to do this, so this check is not
04597          * incurring extra work anyhow.
04598          */
04599         if (!MultiXactIdIsRunning(xmax))
04600         {
04601             if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) ||
04602                 TransactionIdDidAbort(MultiXactIdGetUpdateXid(xmax,
04603                                                               old_infomask)))
04604             {
04605                 /*
04606                  * Reset these bits and restart; otherwise fall through to
04607                  * create a new multi below.
04608                  */
04609                 old_infomask &= ~HEAP_XMAX_IS_MULTI;
04610                 old_infomask |= HEAP_XMAX_INVALID;
04611                 goto l5;
04612             }
04613         }
04614 
04615         new_status = get_mxact_status_for_lock(mode, is_update);
04616 
04617         new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax,
04618                                      new_status);
04619         GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
04620     }
04621     else if (old_infomask & HEAP_XMAX_COMMITTED)
04622     {
04623         /*
04624          * It's a committed update, so we need to preserve him as updater of
04625          * the tuple.
04626          */
04627         MultiXactStatus     status;
04628         MultiXactStatus     new_status;
04629 
04630         if (old_infomask2 & HEAP_KEYS_UPDATED)
04631             status = MultiXactStatusUpdate;
04632         else
04633             status = MultiXactStatusNoKeyUpdate;
04634 
04635         new_status = get_mxact_status_for_lock(mode, is_update);
04636         /*
04637          * since it's not running, it's obviously impossible for the old
04638          * updater to be identical to the current one, so we need not check
04639          * for that case as we do in the block above.
04640          */
04641         new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
04642         GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
04643     }
04644     else if (TransactionIdIsInProgress(xmax))
04645     {
04646         /*
04647          * If the XMAX is a valid, in-progress TransactionId, then we need to
04648          * create a new MultiXactId that includes both the old locker or
04649          * updater and our own TransactionId.
04650          */
04651         MultiXactStatus     status;
04652         MultiXactStatus     new_status;
04653 
04654         if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
04655         {
04656             if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask))
04657                 status = MultiXactStatusForKeyShare;
04658             else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask))
04659                 status = MultiXactStatusForShare;
04660             else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))
04661             {
04662                 if (old_infomask2 & HEAP_KEYS_UPDATED)
04663                     status = MultiXactStatusForUpdate;
04664                 else
04665                     status = MultiXactStatusForNoKeyUpdate;
04666             }
04667             else
04668             {
04669                 /*
04670                  * LOCK_ONLY can be present alone only when a page has been
04671                  * upgraded by pg_upgrade.  But in that case,
04672                  * TransactionIdIsInProgress() should have returned false.  We
04673                  * assume it's no longer locked in this case.
04674                  */
04675                 elog(WARNING, "LOCK_ONLY found for Xid in progress %u", xmax);
04676                 old_infomask |= HEAP_XMAX_INVALID;
04677                 old_infomask &= ~HEAP_XMAX_LOCK_ONLY;
04678                 goto l5;
04679             }
04680         }
04681         else
04682         {
04683             /* it's an update, but which kind? */
04684             if (old_infomask2 & HEAP_KEYS_UPDATED)
04685                 status = MultiXactStatusUpdate;
04686             else
04687                 status = MultiXactStatusNoKeyUpdate;
04688         }
04689 
04690         new_status = get_mxact_status_for_lock(mode, is_update);
04691 
04692         /*
04693          * If the existing lock mode is identical to or weaker than the new
04694          * one, we can act as though there is no existing lock, so set
04695          * XMAX_INVALID and restart.
04696          */
04697         if (xmax == add_to_xmax)
04698         {
04699             LockTupleMode   old_mode = TUPLOCK_from_mxstatus(status);
04700             bool            old_isupd = ISUPDATE_from_mxstatus(status);
04701 
04702             /*
04703              * We can do this if the new LockTupleMode is higher or equal than
04704              * the old one; and if there was previously an update, we need an
04705              * update, but if there wasn't, then we can accept there not being
04706              * one.
04707              */
04708             if ((mode >= old_mode) && (is_update || !old_isupd))
04709             {
04710                 /*
04711                  * Note that the infomask might contain some other dirty bits.
04712                  * However, since the new infomask is reset to zero, we only
04713                  * set what's minimally necessary, and that the case that
04714                  * checks HEAP_XMAX_INVALID is the very first above, there is
04715                  * no need for extra cleanup of the infomask here.
04716                  */
04717                 old_infomask |= HEAP_XMAX_INVALID;
04718                 goto l5;
04719             }
04720         }
04721         new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
04722         GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
04723     }
04724     else if (!HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) &&
04725              TransactionIdDidCommit(xmax))
04726     {
04727         /*
04728          * It's a committed update, so we gotta preserve him as updater of the
04729          * tuple.
04730          */
04731         MultiXactStatus     status;
04732         MultiXactStatus     new_status;
04733 
04734         if (old_infomask2 & HEAP_KEYS_UPDATED)
04735             status = MultiXactStatusUpdate;
04736         else
04737             status = MultiXactStatusNoKeyUpdate;
04738 
04739         new_status = get_mxact_status_for_lock(mode, is_update);
04740         /*
04741          * since it's not running, it's obviously impossible for the old
04742          * updater to be identical to the current one, so we need not check
04743          * for that case as we do in the block above.
04744          */
04745         new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
04746         GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
04747     }
04748     else
04749     {
04750         /*
04751          * Can get here iff the locking/updating transaction was running when
04752          * the infomask was extracted from the tuple, but finished before
04753          * TransactionIdIsInProgress got to run.  Deal with it as if there was
04754          * no locker at all in the first place.
04755          */
04756         old_infomask |= HEAP_XMAX_INVALID;
04757         goto l5;
04758     }
04759 
04760     *result_infomask = new_infomask;
04761     *result_infomask2 = new_infomask2;
04762     *result_xmax = new_xmax;
04763 }
04764 
04765 
04766 /*
04767  * Recursive part of heap_lock_updated_tuple
04768  *
04769  * Fetch the tuple pointed to by tid in rel, and mark it as locked by the given
04770  * xid with the given mode; if this tuple is updated, recurse to lock the new
04771  * version as well.
04772  */
04773 static HTSU_Result
04774 heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
04775                             LockTupleMode mode)
04776 {
04777     ItemPointerData tupid;
04778     HeapTupleData   mytup;
04779     Buffer          buf;
04780     uint16          new_infomask,
04781                     new_infomask2,
04782                     old_infomask;
04783     TransactionId   xmax,
04784                     new_xmax;
04785 
04786     ItemPointerCopy(tid, &tupid);
04787 
04788     for (;;)
04789     {
04790         new_infomask = 0;
04791         new_xmax = InvalidTransactionId;
04792         ItemPointerCopy(&tupid, &(mytup.t_self));
04793 
04794         if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false, NULL))
04795             elog(ERROR, "unable to fetch updated version of tuple");
04796 
04797 l4:
04798         CHECK_FOR_INTERRUPTS();
04799         LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
04800 
04801         old_infomask = mytup.t_data->t_infomask;
04802         xmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
04803 
04804         /*
04805          * If this tuple is updated and the key has been modified (or deleted),
04806          * what we do depends on the status of the updating transaction: if
04807          * it's live, we sleep until it finishes; if it has committed, we have
04808          * to fail (i.e. return HeapTupleUpdated); if it aborted, we ignore it.
04809          * For updates that didn't touch the key, we can just plough ahead.
04810          */
04811         if (!(old_infomask & HEAP_XMAX_INVALID) &&
04812             (mytup.t_data->t_infomask2 & HEAP_KEYS_UPDATED))
04813         {
04814             TransactionId   update_xid;
04815 
04816             /*
04817              * Note: we *must* check TransactionIdIsInProgress before
04818              * TransactionIdDidAbort/Commit; see comment at top of tqual.c for
04819              * an explanation.
04820              */
04821             update_xid = HeapTupleHeaderGetUpdateXid(mytup.t_data);
04822             if (TransactionIdIsCurrentTransactionId(update_xid))
04823             {
04824                 UnlockReleaseBuffer(buf);
04825                 return HeapTupleSelfUpdated;
04826             }
04827             else if (TransactionIdIsInProgress(update_xid))
04828             {
04829                 LockBuffer(buf, BUFFER_LOCK_UNLOCK);
04830                 /* No LockTupleTuplock here -- see heap_lock_updated_tuple */
04831                 XactLockTableWait(update_xid);
04832                 goto l4;
04833             }
04834             else if (TransactionIdDidAbort(update_xid))
04835                 ;   /* okay to proceed */
04836             else if (TransactionIdDidCommit(update_xid))
04837             {
04838                 UnlockReleaseBuffer(buf);
04839                 return HeapTupleUpdated;
04840             }
04841         }
04842 
04843         /* compute the new Xmax and infomask values for the tuple ... */
04844         compute_new_xmax_infomask(xmax, old_infomask, mytup.t_data->t_infomask2,
04845                                   xid, mode, false,
04846                                   &new_xmax, &new_infomask, &new_infomask2);
04847 
04848         START_CRIT_SECTION();
04849 
04850         /* ... and set them */
04851         HeapTupleHeaderSetXmax(mytup.t_data, new_xmax);
04852         mytup.t_data->t_infomask &= ~HEAP_XMAX_BITS;
04853         mytup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
04854         mytup.t_data->t_infomask |= new_infomask;
04855         mytup.t_data->t_infomask2 |= new_infomask2;
04856 
04857         MarkBufferDirty(buf);
04858 
04859         /* XLOG stuff */
04860         if (RelationNeedsWAL(rel))
04861         {
04862             xl_heap_lock_updated xlrec;
04863             XLogRecPtr  recptr;
04864             XLogRecData rdata[2];
04865             Page        page = BufferGetPage(buf);
04866 
04867             xlrec.target.node = rel->rd_node;
04868             xlrec.target.tid = mytup.t_self;
04869             xlrec.xmax = new_xmax;
04870             xlrec.infobits_set = compute_infobits(new_infomask, new_infomask2);
04871 
04872             rdata[0].data = (char *) &xlrec;
04873             rdata[0].len = SizeOfHeapLockUpdated;
04874             rdata[0].buffer = InvalidBuffer;
04875             rdata[0].next = &(rdata[1]);
04876 
04877             rdata[1].data = NULL;
04878             rdata[1].len = 0;
04879             rdata[1].buffer = buf;
04880             rdata[1].buffer_std = true;
04881             rdata[1].next = NULL;
04882 
04883             recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_LOCK_UPDATED, rdata);
04884 
04885             PageSetLSN(page, recptr);
04886         }
04887 
04888         END_CRIT_SECTION();
04889 
04890         /* if we find the end of update chain, we're done. */
04891         if (mytup.t_data->t_infomask & HEAP_XMAX_INVALID ||
04892             ItemPointerEquals(&mytup.t_self, &mytup.t_data->t_ctid)  ||
04893             HeapTupleHeaderIsOnlyLocked(mytup.t_data))
04894         {
04895             UnlockReleaseBuffer(buf);
04896             return HeapTupleMayBeUpdated;
04897         }
04898 
04899         /* tail recursion */
04900         ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid);
04901         UnlockReleaseBuffer(buf);
04902     }
04903 }
04904 
04905 /*
04906  * heap_lock_updated_tuple
04907  *      Follow update chain when locking an updated tuple, acquiring locks (row
04908  *      marks) on the updated versions.
04909  *
04910  * The initial tuple is assumed to be already locked.
04911  *
04912  * This function doesn't check visibility, it just inconditionally marks the
04913  * tuple(s) as locked.  If any tuple in the updated chain is being deleted
04914  * concurrently (or updated with the key being modified), sleep until the
04915  * transaction doing it is finished.
04916  *
04917  * Note that we don't acquire heavyweight tuple locks on the tuples we walk
04918  * when we have to wait for other transactions to release them, as opposed to
04919  * what heap_lock_tuple does.  The reason is that having more than one
04920  * transaction walking the chain is probably uncommon enough that risk of
04921  * starvation is not likely: one of the preconditions for being here is that
04922  * the snapshot in use predates the update that created this tuple (because we
04923  * started at an earlier version of the tuple), but at the same time such a
04924  * transaction cannot be using repeatable read or serializable isolation
04925  * levels, because that would lead to a serializability failure.
04926  */
04927 static HTSU_Result
04928 heap_lock_updated_tuple(Relation rel, HeapTuple tuple, ItemPointer ctid,
04929                         TransactionId xid, LockTupleMode mode)
04930 {
04931     if (!ItemPointerEquals(&tuple->t_self, ctid))
04932     {
04933         /*
04934          * If this is the first possibly-multixact-able operation in the
04935          * current transaction, set my per-backend OldestMemberMXactId setting.
04936          * We can be certain that the transaction will never become a member of
04937          * any older MultiXactIds than that.  (We have to do this even if we
04938          * end up just using our own TransactionId below, since some other
04939          * backend could incorporate our XID into a MultiXact immediately
04940          * afterwards.)
04941          */
04942         MultiXactIdSetOldestMember();
04943 
04944         return heap_lock_updated_tuple_rec(rel, ctid, xid, mode);
04945     }
04946 
04947     /* nothing to lock */
04948     return HeapTupleMayBeUpdated;
04949 }
04950 
04951 
04952 /*
04953  * heap_inplace_update - update a tuple "in place" (ie, overwrite it)
04954  *
04955  * Overwriting violates both MVCC and transactional safety, so the uses
04956  * of this function in Postgres are extremely limited.  Nonetheless we
04957  * find some places to use it.
04958  *
04959  * The tuple cannot change size, and therefore it's reasonable to assume
04960  * that its null bitmap (if any) doesn't change either.  So we just
04961  * overwrite the data portion of the tuple without touching the null
04962  * bitmap or any of the header fields.
04963  *
04964  * tuple is an in-memory tuple structure containing the data to be written
04965  * over the target tuple.  Also, tuple->t_self identifies the target tuple.
04966  */
04967 void
04968 heap_inplace_update(Relation relation, HeapTuple tuple)
04969 {
04970     Buffer      buffer;
04971     Page        page;
04972     OffsetNumber offnum;
04973     ItemId      lp = NULL;
04974     HeapTupleHeader htup;
04975     uint32      oldlen;
04976     uint32      newlen;
04977 
04978     buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&(tuple->t_self)));
04979     LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
04980     page = (Page) BufferGetPage(buffer);
04981 
04982     offnum = ItemPointerGetOffsetNumber(&(tuple->t_self));
04983     if (PageGetMaxOffsetNumber(page) >= offnum)
04984         lp = PageGetItemId(page, offnum);
04985 
04986     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
04987         elog(ERROR, "heap_inplace_update: invalid lp");
04988 
04989     htup = (HeapTupleHeader) PageGetItem(page, lp);
04990 
04991     oldlen = ItemIdGetLength(lp) - htup->t_hoff;
04992     newlen = tuple->t_len - tuple->t_data->t_hoff;
04993     if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
04994         elog(ERROR, "heap_inplace_update: wrong tuple length");
04995 
04996     /* NO EREPORT(ERROR) from here till changes are logged */
04997     START_CRIT_SECTION();
04998 
04999     memcpy((char *) htup + htup->t_hoff,
05000            (char *) tuple->t_data + tuple->t_data->t_hoff,
05001            newlen);
05002 
05003     MarkBufferDirty(buffer);
05004 
05005     /* XLOG stuff */
05006     if (RelationNeedsWAL(relation))
05007     {
05008         xl_heap_inplace xlrec;
05009         XLogRecPtr  recptr;
05010         XLogRecData rdata[2];
05011 
05012         xlrec.target.node = relation->rd_node;
05013         xlrec.target.tid = tuple->t_self;
05014 
05015         rdata[0].data = (char *) &xlrec;
05016         rdata[0].len = SizeOfHeapInplace;
05017         rdata[0].buffer = InvalidBuffer;
05018         rdata[0].next = &(rdata[1]);
05019 
05020         rdata[1].data = (char *) htup + htup->t_hoff;
05021         rdata[1].len = newlen;
05022         rdata[1].buffer = buffer;
05023         rdata[1].buffer_std = true;
05024         rdata[1].next = NULL;
05025 
05026         recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE, rdata);
05027 
05028         PageSetLSN(page, recptr);
05029     }
05030 
05031     END_CRIT_SECTION();
05032 
05033     UnlockReleaseBuffer(buffer);
05034 
05035     /*
05036      * Send out shared cache inval if necessary.  Note that because we only
05037      * pass the new version of the tuple, this mustn't be used for any
05038      * operations that could change catcache lookup keys.  But we aren't
05039      * bothering with index updates either, so that's true a fortiori.
05040      */
05041     if (!IsBootstrapProcessingMode())
05042         CacheInvalidateHeapTuple(relation, tuple, NULL);
05043 }
05044 
05045 
05046 /*
05047  * heap_freeze_tuple
05048  *
05049  * Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac)
05050  * are older than the specified cutoff XID.  If so, replace them with
05051  * FrozenTransactionId or InvalidTransactionId as appropriate, and return
05052  * TRUE.  Return FALSE if nothing was changed.
05053  *
05054  * It is assumed that the caller has checked the tuple with
05055  * HeapTupleSatisfiesVacuum() and determined that it is not HEAPTUPLE_DEAD
05056  * (else we should be removing the tuple, not freezing it).
05057  *
05058  * NB: cutoff_xid *must* be <= the current global xmin, to ensure that any
05059  * XID older than it could neither be running nor seen as running by any
05060  * open transaction.  This ensures that the replacement will not change
05061  * anyone's idea of the tuple state.  Also, since we assume the tuple is
05062  * not HEAPTUPLE_DEAD, the fact that an XID is not still running allows us
05063  * to assume that it is either committed good or aborted, as appropriate;
05064  * so we need no external state checks to decide what to do.  (This is good
05065  * because this function is applied during WAL recovery, when we don't have
05066  * access to any such state, and can't depend on the hint bits to be set.)
05067  *
05068  * Similarly, cutoff_multi must be less than or equal to the smallest
05069  * MultiXactId used by any transaction currently open.
05070  *
05071  * If the tuple is in a shared buffer, caller must hold an exclusive lock on
05072  * that buffer.
05073  *
05074  * Note: it might seem we could make the changes without exclusive lock, since
05075  * TransactionId read/write is assumed atomic anyway.  However there is a race
05076  * condition: someone who just fetched an old XID that we overwrite here could
05077  * conceivably not finish checking the XID against pg_clog before we finish
05078  * the VACUUM and perhaps truncate off the part of pg_clog he needs.  Getting
05079  * exclusive lock ensures no other backend is in process of checking the
05080  * tuple status.  Also, getting exclusive lock makes it safe to adjust the
05081  * infomask bits.
05082  */
05083 bool
05084 heap_freeze_tuple(HeapTupleHeader tuple, TransactionId cutoff_xid,
05085                   MultiXactId cutoff_multi)
05086 {
05087     bool        changed = false;
05088     TransactionId xid;
05089 
05090     xid = HeapTupleHeaderGetXmin(tuple);
05091     if (TransactionIdIsNormal(xid) &&
05092         TransactionIdPrecedes(xid, cutoff_xid))
05093     {
05094         HeapTupleHeaderSetXmin(tuple, FrozenTransactionId);
05095 
05096         /*
05097          * Might as well fix the hint bits too; usually XMIN_COMMITTED will
05098          * already be set here, but there's a small chance not.
05099          */
05100         Assert(!(tuple->t_infomask & HEAP_XMIN_INVALID));
05101         tuple->t_infomask |= HEAP_XMIN_COMMITTED;
05102         changed = true;
05103     }
05104 
05105     /*
05106      * Note that this code handles IS_MULTI Xmax values, too, but only to mark
05107      * the tuple frozen if the updating Xid in the mxact is below the freeze
05108      * cutoff; it doesn't remove dead members of a very old multixact.
05109      */
05110     xid = HeapTupleHeaderGetRawXmax(tuple);
05111     if ((tuple->t_infomask & HEAP_XMAX_IS_MULTI) ?
05112         (MultiXactIdIsValid(xid) &&
05113          MultiXactIdPrecedes(xid, cutoff_multi)) :
05114         (TransactionIdIsNormal(xid) &&
05115          TransactionIdPrecedes(xid, cutoff_xid)))
05116     {
05117         HeapTupleHeaderSetXmax(tuple, InvalidTransactionId);
05118 
05119         /*
05120          * The tuple might be marked either XMAX_INVALID or XMAX_COMMITTED
05121          * + LOCKED.  Normalize to INVALID just to be sure no one gets
05122          * confused.  Also get rid of the HEAP_KEYS_UPDATED bit.
05123          */
05124         tuple->t_infomask &= ~HEAP_XMAX_BITS;
05125         tuple->t_infomask |= HEAP_XMAX_INVALID;
05126         HeapTupleHeaderClearHotUpdated(tuple);
05127         tuple->t_infomask2 &= ~HEAP_KEYS_UPDATED;
05128         changed = true;
05129     }
05130 
05131     /*
05132      * Old-style VACUUM FULL is gone, but we have to keep this code as long as
05133      * we support having MOVED_OFF/MOVED_IN tuples in the database.
05134      */
05135     if (tuple->t_infomask & HEAP_MOVED)
05136     {
05137         xid = HeapTupleHeaderGetXvac(tuple);
05138         if (TransactionIdIsNormal(xid) &&
05139             TransactionIdPrecedes(xid, cutoff_xid))
05140         {
05141             /*
05142              * If a MOVED_OFF tuple is not dead, the xvac transaction must
05143              * have failed; whereas a non-dead MOVED_IN tuple must mean the
05144              * xvac transaction succeeded.
05145              */
05146             if (tuple->t_infomask & HEAP_MOVED_OFF)
05147                 HeapTupleHeaderSetXvac(tuple, InvalidTransactionId);
05148             else
05149                 HeapTupleHeaderSetXvac(tuple, FrozenTransactionId);
05150 
05151             /*
05152              * Might as well fix the hint bits too; usually XMIN_COMMITTED
05153              * will already be set here, but there's a small chance not.
05154              */
05155             Assert(!(tuple->t_infomask & HEAP_XMIN_INVALID));
05156             tuple->t_infomask |= HEAP_XMIN_COMMITTED;
05157             changed = true;
05158         }
05159     }
05160 
05161     return changed;
05162 }
05163 
05164 /*
05165  * For a given MultiXactId, return the hint bits that should be set in the
05166  * tuple's infomask.
05167  *
05168  * Normally this should be called for a multixact that was just created, and
05169  * so is on our local cache, so the GetMembers call is fast.
05170  */
05171 static void
05172 GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask,
05173                        uint16 *new_infomask2)
05174 {
05175     int     nmembers;
05176     MultiXactMember *members;
05177     int     i;
05178     uint16  bits = HEAP_XMAX_IS_MULTI;
05179     uint16  bits2 = 0;
05180     bool    has_update = false;
05181     LockTupleMode   strongest = LockTupleKeyShare;
05182 
05183     /*
05184      * We only use this in multis we just created, so they cannot be values
05185      * pre-pg_upgrade.
05186      */
05187     nmembers = GetMultiXactIdMembers(multi, &members, false);
05188 
05189     for (i = 0; i < nmembers; i++)
05190     {
05191         LockTupleMode   mode;
05192 
05193         /*
05194          * Remember the strongest lock mode held by any member of the
05195          * multixact.
05196          */
05197         mode = TUPLOCK_from_mxstatus(members[i].status);
05198         if (mode > strongest)
05199             strongest = mode;
05200 
05201         /* See what other bits we need */
05202         switch (members[i].status)
05203         {
05204             case MultiXactStatusForKeyShare:
05205             case MultiXactStatusForShare:
05206             case MultiXactStatusForNoKeyUpdate:
05207                 break;
05208 
05209             case MultiXactStatusForUpdate:
05210                 bits2 |= HEAP_KEYS_UPDATED;
05211                 break;
05212 
05213             case MultiXactStatusNoKeyUpdate:
05214                 has_update = true;
05215                 break;
05216 
05217             case MultiXactStatusUpdate:
05218                 bits2 |= HEAP_KEYS_UPDATED;
05219                 has_update = true;
05220                 break;
05221         }
05222     }
05223 
05224     if (strongest == LockTupleExclusive ||
05225         strongest == LockTupleNoKeyExclusive)
05226         bits |= HEAP_XMAX_EXCL_LOCK;
05227     else if (strongest == LockTupleShare)
05228         bits |= HEAP_XMAX_SHR_LOCK;
05229     else if (strongest == LockTupleKeyShare)
05230         bits |= HEAP_XMAX_KEYSHR_LOCK;
05231 
05232     if (!has_update)
05233         bits |= HEAP_XMAX_LOCK_ONLY;
05234 
05235     if (nmembers > 0)
05236         pfree(members);
05237 
05238     *new_infomask = bits;
05239     *new_infomask2 = bits2;
05240 }
05241 
05242 /*
05243  * MultiXactIdGetUpdateXid
05244  *
05245  * Given a multixact Xmax and corresponding infomask, which does not have the
05246  * HEAP_XMAX_LOCK_ONLY bit set, obtain and return the Xid of the updating
05247  * transaction.
05248  */
05249 static TransactionId
05250 MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask)
05251 {
05252     TransactionId   update_xact = InvalidTransactionId;
05253     MultiXactMember *members;
05254     int             nmembers;
05255 
05256     Assert(!(t_infomask & HEAP_XMAX_LOCK_ONLY));
05257     Assert(t_infomask & HEAP_XMAX_IS_MULTI);
05258 
05259     /*
05260      * Since we know the LOCK_ONLY bit is not set, this cannot be a
05261      * multi from pre-pg_upgrade.
05262      */
05263     nmembers = GetMultiXactIdMembers(xmax, &members, false);
05264 
05265     if (nmembers > 0)
05266     {
05267         int     i;
05268 
05269         for (i = 0; i < nmembers; i++)
05270         {
05271             /* Ignore lockers */
05272             if (members[i].status == MultiXactStatusForKeyShare ||
05273                 members[i].status == MultiXactStatusForShare ||
05274                 members[i].status == MultiXactStatusForNoKeyUpdate ||
05275                 members[i].status == MultiXactStatusForUpdate)
05276                 continue;
05277 
05278             /* ignore aborted transactions */
05279             if (TransactionIdDidAbort(members[i].xid))
05280                 continue;
05281             /* there should be at most one non-aborted updater */
05282             Assert(update_xact == InvalidTransactionId);
05283             Assert(members[i].status == MultiXactStatusNoKeyUpdate ||
05284                    members[i].status == MultiXactStatusUpdate);
05285             update_xact = members[i].xid;
05286 #ifndef USE_ASSERT_CHECKING
05287             /*
05288              * in an assert-enabled build, walk the whole array to ensure
05289              * there's no other updater.
05290              */
05291             break;
05292 #endif
05293         }
05294 
05295         pfree(members);
05296     }
05297 
05298     return update_xact;
05299 }
05300 
05301 /*
05302  * HeapTupleGetUpdateXid
05303  *      As above, but use a HeapTupleHeader
05304  *
05305  * See also HeapTupleHeaderGetUpdateXid, which can be used without previously
05306  * checking the hint bits.
05307  */
05308 TransactionId
05309 HeapTupleGetUpdateXid(HeapTupleHeader tuple)
05310 {
05311     return MultiXactIdGetUpdateXid(HeapTupleHeaderGetRawXmax(tuple),
05312                                    tuple->t_infomask);
05313 }
05314 
05315 /*
05316  * Do_MultiXactIdWait
05317  *      Actual implementation for the two functions below.
05318  *
05319  * We do this by sleeping on each member using XactLockTableWait.  Any
05320  * members that belong to the current backend are *not* waited for, however;
05321  * this would not merely be useless but would lead to Assert failure inside
05322  * XactLockTableWait.  By the time this returns, it is certain that all
05323  * transactions *of other backends* that were members of the MultiXactId
05324  * that conflict with the requested status are dead (and no new ones can have
05325  * been added, since it is not legal to add members to an existing
05326  * MultiXactId).
05327  *
05328  * But by the time we finish sleeping, someone else may have changed the Xmax
05329  * of the containing tuple, so the caller needs to iterate on us somehow.
05330  *
05331  * Note that in case we return false, the number of remaining members is
05332  * not to be trusted.
05333  */
05334 static bool
05335 Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
05336                    int *remaining, uint16 infomask, bool nowait)
05337 {
05338     bool        allow_old;
05339     bool        result = true;
05340     MultiXactMember *members;
05341     int         nmembers;
05342     int         remain = 0;
05343 
05344     allow_old = !(infomask & HEAP_LOCK_MASK) && HEAP_XMAX_IS_LOCKED_ONLY(infomask);
05345     nmembers = GetMultiXactIdMembers(multi, &members, allow_old);
05346 
05347     if (nmembers >= 0)
05348     {
05349         int         i;
05350 
05351         for (i = 0; i < nmembers; i++)
05352         {
05353             TransactionId memxid = members[i].xid;
05354             MultiXactStatus memstatus = members[i].status;
05355 
05356             if (TransactionIdIsCurrentTransactionId(memxid))
05357             {
05358                 remain++;
05359                 continue;
05360             }
05361 
05362             if (!DoLockModesConflict(LOCKMODE_from_mxstatus(memstatus),
05363                                      LOCKMODE_from_mxstatus(status)))
05364             {
05365                 if (remaining && TransactionIdIsInProgress(memxid))
05366                     remain++;
05367                 continue;
05368             }
05369 
05370             /*
05371              * This member conflicts with our multi, so we have to sleep (or
05372              * return failure, if asked to avoid waiting.)
05373              */
05374             if (nowait)
05375             {
05376                 result = ConditionalXactLockTableWait(memxid);
05377                 if (!result)
05378                     break;
05379             }
05380             else
05381                 XactLockTableWait(memxid);
05382         }
05383 
05384         pfree(members);
05385     }
05386 
05387     if (remaining)
05388         *remaining = remain;
05389 
05390     return result;
05391 }
05392 
05393 /*
05394  * MultiXactIdWait
05395  *      Sleep on a MultiXactId.
05396  *
05397  * By the time we finish sleeping, someone else may have changed the Xmax
05398  * of the containing tuple, so the caller needs to iterate on us somehow.
05399  *
05400  * We return (in *remaining, if not NULL) the number of members that are still
05401  * running, including any (non-aborted) subtransactions of our own transaction.
05402  *
05403  */
05404 static void
05405 MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
05406                 int *remaining, uint16 infomask)
05407 {
05408     Do_MultiXactIdWait(multi, status, remaining, infomask, false);
05409 }
05410 
05411 /*
05412  * ConditionalMultiXactIdWait
05413  *      As above, but only lock if we can get the lock without blocking.
05414  *
05415  * By the time we finish sleeping, someone else may have changed the Xmax
05416  * of the containing tuple, so the caller needs to iterate on us somehow.
05417  *
05418  * If the multixact is now all gone, return true.  Returns false if some
05419  * transactions might still be running.
05420  *
05421  * We return (in *remaining, if not NULL) the number of members that are still
05422  * running, including any (non-aborted) subtransactions of our own transaction.
05423  */
05424 static bool
05425 ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status,
05426                            int *remaining, uint16 infomask)
05427 {
05428     return Do_MultiXactIdWait(multi, status, remaining, infomask, true);
05429 }
05430 
05431 /*
05432  * heap_tuple_needs_freeze
05433  *
05434  * Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac)
05435  * are older than the specified cutoff XID or MultiXactId.  If so, return TRUE.
05436  *
05437  * It doesn't matter whether the tuple is alive or dead, we are checking
05438  * to see if a tuple needs to be removed or frozen to avoid wraparound.
05439  */
05440 bool
05441 heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid,
05442                         MultiXactId cutoff_multi, Buffer buf)
05443 {
05444     TransactionId xid;
05445 
05446     xid = HeapTupleHeaderGetXmin(tuple);
05447     if (TransactionIdIsNormal(xid) &&
05448         TransactionIdPrecedes(xid, cutoff_xid))
05449         return true;
05450 
05451     if (!(tuple->t_infomask & HEAP_XMAX_INVALID))
05452     {
05453         if (!(tuple->t_infomask & HEAP_XMAX_IS_MULTI))
05454         {
05455             xid = HeapTupleHeaderGetRawXmax(tuple);
05456             if (TransactionIdIsNormal(xid) &&
05457                 TransactionIdPrecedes(xid, cutoff_xid))
05458                 return true;
05459         }
05460         else
05461         {
05462             MultiXactId multi;
05463 
05464             multi = HeapTupleHeaderGetRawXmax(tuple);
05465             if (MultiXactIdPrecedes(multi, cutoff_multi))
05466                 return true;
05467         }
05468     }
05469 
05470     if (tuple->t_infomask & HEAP_MOVED)
05471     {
05472         xid = HeapTupleHeaderGetXvac(tuple);
05473         if (TransactionIdIsNormal(xid) &&
05474             TransactionIdPrecedes(xid, cutoff_xid))
05475             return true;
05476     }
05477 
05478     return false;
05479 }
05480 
05481 /* ----------------
05482  *      heap_markpos    - mark scan position
05483  * ----------------
05484  */
05485 void
05486 heap_markpos(HeapScanDesc scan)
05487 {
05488     /* Note: no locking manipulations needed */
05489 
05490     if (scan->rs_ctup.t_data != NULL)
05491     {
05492         scan->rs_mctid = scan->rs_ctup.t_self;
05493         if (scan->rs_pageatatime)
05494             scan->rs_mindex = scan->rs_cindex;
05495     }
05496     else
05497         ItemPointerSetInvalid(&scan->rs_mctid);
05498 }
05499 
05500 /* ----------------
05501  *      heap_restrpos   - restore position to marked location
05502  * ----------------
05503  */
05504 void
05505 heap_restrpos(HeapScanDesc scan)
05506 {
05507     /* XXX no amrestrpos checking that ammarkpos called */
05508 
05509     if (!ItemPointerIsValid(&scan->rs_mctid))
05510     {
05511         scan->rs_ctup.t_data = NULL;
05512 
05513         /*
05514          * unpin scan buffers
05515          */
05516         if (BufferIsValid(scan->rs_cbuf))
05517             ReleaseBuffer(scan->rs_cbuf);
05518         scan->rs_cbuf = InvalidBuffer;
05519         scan->rs_cblock = InvalidBlockNumber;
05520         scan->rs_inited = false;
05521     }
05522     else
05523     {
05524         /*
05525          * If we reached end of scan, rs_inited will now be false.  We must
05526          * reset it to true to keep heapgettup from doing the wrong thing.
05527          */
05528         scan->rs_inited = true;
05529         scan->rs_ctup.t_self = scan->rs_mctid;
05530         if (scan->rs_pageatatime)
05531         {
05532             scan->rs_cindex = scan->rs_mindex;
05533             heapgettup_pagemode(scan,
05534                                 NoMovementScanDirection,
05535                                 0,      /* needn't recheck scan keys */
05536                                 NULL);
05537         }
05538         else
05539             heapgettup(scan,
05540                        NoMovementScanDirection,
05541                        0,       /* needn't recheck scan keys */
05542                        NULL);
05543     }
05544 }
05545 
05546 /*
05547  * If 'tuple' contains any visible XID greater than latestRemovedXid,
05548  * ratchet forwards latestRemovedXid to the greatest one found.
05549  * This is used as the basis for generating Hot Standby conflicts, so
05550  * if a tuple was never visible then removing it should not conflict
05551  * with queries.
05552  */
05553 void
05554 HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple,
05555                                        TransactionId *latestRemovedXid)
05556 {
05557     TransactionId xmin = HeapTupleHeaderGetXmin(tuple);
05558     TransactionId xmax = HeapTupleHeaderGetUpdateXid(tuple);
05559     TransactionId xvac = HeapTupleHeaderGetXvac(tuple);
05560 
05561     if (tuple->t_infomask & HEAP_MOVED)
05562     {
05563         if (TransactionIdPrecedes(*latestRemovedXid, xvac))
05564             *latestRemovedXid = xvac;
05565     }
05566 
05567     /*
05568      * Ignore tuples inserted by an aborted transaction or if the tuple was
05569      * updated/deleted by the inserting transaction.
05570      *
05571      * Look for a committed hint bit, or if no xmin bit is set, check clog.
05572      * This needs to work on both master and standby, where it is used to
05573      * assess btree delete records.
05574      */
05575     if ((tuple->t_infomask & HEAP_XMIN_COMMITTED) ||
05576         (!(tuple->t_infomask & HEAP_XMIN_COMMITTED) &&
05577          !(tuple->t_infomask & HEAP_XMIN_INVALID) &&
05578          TransactionIdDidCommit(xmin)))
05579     {
05580         if (xmax != xmin &&
05581             TransactionIdFollows(xmax, *latestRemovedXid))
05582             *latestRemovedXid = xmax;
05583     }
05584 
05585     /* *latestRemovedXid may still be invalid at end */
05586 }
05587 
05588 /*
05589  * Perform XLogInsert to register a heap cleanup info message. These
05590  * messages are sent once per VACUUM and are required because
05591  * of the phasing of removal operations during a lazy VACUUM.
05592  * see comments for vacuum_log_cleanup_info().
05593  */
05594 XLogRecPtr
05595 log_heap_cleanup_info(RelFileNode rnode, TransactionId latestRemovedXid)
05596 {
05597     xl_heap_cleanup_info xlrec;
05598     XLogRecPtr  recptr;
05599     XLogRecData rdata;
05600 
05601     xlrec.node = rnode;
05602     xlrec.latestRemovedXid = latestRemovedXid;
05603 
05604     rdata.data = (char *) &xlrec;
05605     rdata.len = SizeOfHeapCleanupInfo;
05606     rdata.buffer = InvalidBuffer;
05607     rdata.next = NULL;
05608 
05609     recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_CLEANUP_INFO, &rdata);
05610 
05611     return recptr;
05612 }
05613 
05614 /*
05615  * Perform XLogInsert for a heap-clean operation.  Caller must already
05616  * have modified the buffer and marked it dirty.
05617  *
05618  * Note: prior to Postgres 8.3, the entries in the nowunused[] array were
05619  * zero-based tuple indexes.  Now they are one-based like other uses
05620  * of OffsetNumber.
05621  *
05622  * We also include latestRemovedXid, which is the greatest XID present in
05623  * the removed tuples. That allows recovery processing to cancel or wait
05624  * for long standby queries that can still see these tuples.
05625  */
05626 XLogRecPtr
05627 log_heap_clean(Relation reln, Buffer buffer,
05628                OffsetNumber *redirected, int nredirected,
05629                OffsetNumber *nowdead, int ndead,
05630                OffsetNumber *nowunused, int nunused,
05631                TransactionId latestRemovedXid)
05632 {
05633     xl_heap_clean xlrec;
05634     uint8       info;
05635     XLogRecPtr  recptr;
05636     XLogRecData rdata[4];
05637 
05638     /* Caller should not call me on a non-WAL-logged relation */
05639     Assert(RelationNeedsWAL(reln));
05640 
05641     xlrec.node = reln->rd_node;
05642     xlrec.block = BufferGetBlockNumber(buffer);
05643     xlrec.latestRemovedXid = latestRemovedXid;
05644     xlrec.nredirected = nredirected;
05645     xlrec.ndead = ndead;
05646 
05647     rdata[0].data = (char *) &xlrec;
05648     rdata[0].len = SizeOfHeapClean;
05649     rdata[0].buffer = InvalidBuffer;
05650     rdata[0].next = &(rdata[1]);
05651 
05652     /*
05653      * The OffsetNumber arrays are not actually in the buffer, but we pretend
05654      * that they are.  When XLogInsert stores the whole buffer, the offset
05655      * arrays need not be stored too.  Note that even if all three arrays are
05656      * empty, we want to expose the buffer as a candidate for whole-page
05657      * storage, since this record type implies a defragmentation operation
05658      * even if no item pointers changed state.
05659      */
05660     if (nredirected > 0)
05661     {
05662         rdata[1].data = (char *) redirected;
05663         rdata[1].len = nredirected * sizeof(OffsetNumber) * 2;
05664     }
05665     else
05666     {
05667         rdata[1].data = NULL;
05668         rdata[1].len = 0;
05669     }
05670     rdata[1].buffer = buffer;
05671     rdata[1].buffer_std = true;
05672     rdata[1].next = &(rdata[2]);
05673 
05674     if (ndead > 0)
05675     {
05676         rdata[2].data = (char *) nowdead;
05677         rdata[2].len = ndead * sizeof(OffsetNumber);
05678     }
05679     else
05680     {
05681         rdata[2].data = NULL;
05682         rdata[2].len = 0;
05683     }
05684     rdata[2].buffer = buffer;
05685     rdata[2].buffer_std = true;
05686     rdata[2].next = &(rdata[3]);
05687 
05688     if (nunused > 0)
05689     {
05690         rdata[3].data = (char *) nowunused;
05691         rdata[3].len = nunused * sizeof(OffsetNumber);
05692     }
05693     else
05694     {
05695         rdata[3].data = NULL;
05696         rdata[3].len = 0;
05697     }
05698     rdata[3].buffer = buffer;
05699     rdata[3].buffer_std = true;
05700     rdata[3].next = NULL;
05701 
05702     info = XLOG_HEAP2_CLEAN;
05703     recptr = XLogInsert(RM_HEAP2_ID, info, rdata);
05704 
05705     return recptr;
05706 }
05707 
05708 /*
05709  * Perform XLogInsert for a heap-freeze operation.  Caller must already
05710  * have modified the buffer and marked it dirty.
05711  */
05712 XLogRecPtr
05713 log_heap_freeze(Relation reln, Buffer buffer,
05714                 TransactionId cutoff_xid, MultiXactId cutoff_multi,
05715                 OffsetNumber *offsets, int offcnt)
05716 {
05717     xl_heap_freeze xlrec;
05718     XLogRecPtr  recptr;
05719     XLogRecData rdata[2];
05720 
05721     /* Caller should not call me on a non-WAL-logged relation */
05722     Assert(RelationNeedsWAL(reln));
05723     /* nor when there are no tuples to freeze */
05724     Assert(offcnt > 0);
05725 
05726     xlrec.node = reln->rd_node;
05727     xlrec.block = BufferGetBlockNumber(buffer);
05728     xlrec.cutoff_xid = cutoff_xid;
05729     xlrec.cutoff_multi = cutoff_multi;
05730 
05731     rdata[0].data = (char *) &xlrec;
05732     rdata[0].len = SizeOfHeapFreeze;
05733     rdata[0].buffer = InvalidBuffer;
05734     rdata[0].next = &(rdata[1]);
05735 
05736     /*
05737      * The tuple-offsets array is not actually in the buffer, but pretend that
05738      * it is.  When XLogInsert stores the whole buffer, the offsets array need
05739      * not be stored too.
05740      */
05741     rdata[1].data = (char *) offsets;
05742     rdata[1].len = offcnt * sizeof(OffsetNumber);
05743     rdata[1].buffer = buffer;
05744     rdata[1].buffer_std = true;
05745     rdata[1].next = NULL;
05746 
05747     recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_FREEZE, rdata);
05748 
05749     return recptr;
05750 }
05751 
05752 /*
05753  * Perform XLogInsert for a heap-visible operation.  'block' is the block
05754  * being marked all-visible, and vm_buffer is the buffer containing the
05755  * corresponding visibility map block.  Both should have already been modified
05756  * and dirtied.
05757  *
05758  * If checksums are enabled, we also add the heap_buffer to the chain to
05759  * protect it from being torn.
05760  */
05761 XLogRecPtr
05762 log_heap_visible(RelFileNode rnode, Buffer heap_buffer, Buffer vm_buffer,
05763                  TransactionId cutoff_xid)
05764 {
05765     xl_heap_visible xlrec;
05766     XLogRecPtr  recptr;
05767     XLogRecData rdata[3];
05768 
05769     Assert(BufferIsValid(heap_buffer));
05770     Assert(BufferIsValid(vm_buffer));
05771 
05772     xlrec.node = rnode;
05773     xlrec.block = BufferGetBlockNumber(heap_buffer);
05774     xlrec.cutoff_xid = cutoff_xid;
05775 
05776     rdata[0].data = (char *) &xlrec;
05777     rdata[0].len = SizeOfHeapVisible;
05778     rdata[0].buffer = InvalidBuffer;
05779     rdata[0].next = &(rdata[1]);
05780 
05781     rdata[1].data = NULL;
05782     rdata[1].len = 0;
05783     rdata[1].buffer = vm_buffer;
05784     rdata[1].buffer_std = false;
05785     rdata[1].next = NULL;
05786 
05787     if (DataChecksumsEnabled())
05788     {
05789         rdata[1].next = &(rdata[2]);
05790 
05791         rdata[2].data = NULL;
05792         rdata[2].len = 0;
05793         rdata[2].buffer = heap_buffer;
05794         rdata[2].buffer_std = true;
05795         rdata[2].next = NULL;
05796     }
05797 
05798     recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_VISIBLE, rdata);
05799 
05800     return recptr;
05801 }
05802 
05803 /*
05804  * Perform XLogInsert for a heap-update operation.  Caller must already
05805  * have modified the buffer(s) and marked them dirty.
05806  */
05807 static XLogRecPtr
05808 log_heap_update(Relation reln, Buffer oldbuf,
05809                 Buffer newbuf, HeapTuple oldtup, HeapTuple newtup,
05810                 bool all_visible_cleared, bool new_all_visible_cleared)
05811 {
05812     xl_heap_update xlrec;
05813     xl_heap_header xlhdr;
05814     uint8       info;
05815     XLogRecPtr  recptr;
05816     XLogRecData rdata[4];
05817     Page        page = BufferGetPage(newbuf);
05818 
05819     /* Caller should not call me on a non-WAL-logged relation */
05820     Assert(RelationNeedsWAL(reln));
05821 
05822     if (HeapTupleIsHeapOnly(newtup))
05823         info = XLOG_HEAP_HOT_UPDATE;
05824     else
05825         info = XLOG_HEAP_UPDATE;
05826 
05827     xlrec.target.node = reln->rd_node;
05828     xlrec.target.tid = oldtup->t_self;
05829     xlrec.old_xmax = HeapTupleHeaderGetRawXmax(oldtup->t_data);
05830     xlrec.old_infobits_set = compute_infobits(oldtup->t_data->t_infomask,
05831                                               oldtup->t_data->t_infomask2);
05832     xlrec.new_xmax = HeapTupleHeaderGetRawXmax(newtup->t_data);
05833     xlrec.all_visible_cleared = all_visible_cleared;
05834     xlrec.newtid = newtup->t_self;
05835     xlrec.new_all_visible_cleared = new_all_visible_cleared;
05836 
05837     rdata[0].data = (char *) &xlrec;
05838     rdata[0].len = SizeOfHeapUpdate;
05839     rdata[0].buffer = InvalidBuffer;
05840     rdata[0].next = &(rdata[1]);
05841 
05842     rdata[1].data = NULL;
05843     rdata[1].len = 0;
05844     rdata[1].buffer = oldbuf;
05845     rdata[1].buffer_std = true;
05846     rdata[1].next = &(rdata[2]);
05847 
05848     xlhdr.t_infomask2 = newtup->t_data->t_infomask2;
05849     xlhdr.t_infomask = newtup->t_data->t_infomask;
05850     xlhdr.t_hoff = newtup->t_data->t_hoff;
05851 
05852     /*
05853      * As with insert records, we need not store the rdata[2] segment if we
05854      * decide to store the whole buffer instead.
05855      */
05856     rdata[2].data = (char *) &xlhdr;
05857     rdata[2].len = SizeOfHeapHeader;
05858     rdata[2].buffer = newbuf;
05859     rdata[2].buffer_std = true;
05860     rdata[2].next = &(rdata[3]);
05861 
05862     /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
05863     rdata[3].data = (char *) newtup->t_data + offsetof(HeapTupleHeaderData, t_bits);
05864     rdata[3].len = newtup->t_len - offsetof(HeapTupleHeaderData, t_bits);
05865     rdata[3].buffer = newbuf;
05866     rdata[3].buffer_std = true;
05867     rdata[3].next = NULL;
05868 
05869     /* If new tuple is the single and first tuple on page... */
05870     if (ItemPointerGetOffsetNumber(&(newtup->t_self)) == FirstOffsetNumber &&
05871         PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
05872     {
05873         info |= XLOG_HEAP_INIT_PAGE;
05874         rdata[2].buffer = rdata[3].buffer = InvalidBuffer;
05875     }
05876 
05877     recptr = XLogInsert(RM_HEAP_ID, info, rdata);
05878 
05879     return recptr;
05880 }
05881 
05882 /*
05883  * Perform XLogInsert of a HEAP_NEWPAGE record to WAL. Caller is responsible
05884  * for writing the page to disk after calling this routine.
05885  *
05886  * Note: If you're using this function, you should be building pages in private
05887  * memory and writing them directly to smgr.  If you're using buffers, call
05888  * log_newpage_buffer instead.
05889  *
05890  * Note: the NEWPAGE log record is used for both heaps and indexes, so do
05891  * not do anything that assumes we are touching a heap.
05892  */
05893 XLogRecPtr
05894 log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno,
05895             Page page)
05896 {
05897     xl_heap_newpage xlrec;
05898     XLogRecPtr  recptr;
05899     XLogRecData rdata[2];
05900 
05901     /* NO ELOG(ERROR) from here till newpage op is logged */
05902     START_CRIT_SECTION();
05903 
05904     xlrec.node = *rnode;
05905     xlrec.forknum = forkNum;
05906     xlrec.blkno = blkno;
05907 
05908     rdata[0].data = (char *) &xlrec;
05909     rdata[0].len = SizeOfHeapNewpage;
05910     rdata[0].buffer = InvalidBuffer;
05911     rdata[0].next = &(rdata[1]);
05912 
05913     rdata[1].data = (char *) page;
05914     rdata[1].len = BLCKSZ;
05915     rdata[1].buffer = InvalidBuffer;
05916     rdata[1].next = NULL;
05917 
05918     recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_NEWPAGE, rdata);
05919 
05920     /*
05921      * The page may be uninitialized. If so, we can't set the LSN and TLI
05922      * because that would corrupt the page.
05923      */
05924     if (!PageIsNew(page))
05925     {
05926         PageSetLSN(page, recptr);
05927     }
05928 
05929     END_CRIT_SECTION();
05930 
05931     return recptr;
05932 }
05933 
05934 /*
05935  * Perform XLogInsert of a HEAP_NEWPAGE record to WAL.
05936  *
05937  * Caller should initialize the buffer and mark it dirty before calling this
05938  * function.  This function will set the page LSN and TLI.
05939  *
05940  * Note: the NEWPAGE log record is used for both heaps and indexes, so do
05941  * not do anything that assumes we are touching a heap.
05942  */
05943 XLogRecPtr
05944 log_newpage_buffer(Buffer buffer)
05945 {
05946     xl_heap_newpage xlrec;
05947     XLogRecPtr  recptr;
05948     XLogRecData rdata[2];
05949     Page        page = BufferGetPage(buffer);
05950 
05951     /* We should be in a critical section. */
05952     Assert(CritSectionCount > 0);
05953 
05954     BufferGetTag(buffer, &xlrec.node, &xlrec.forknum, &xlrec.blkno);
05955 
05956     rdata[0].data = (char *) &xlrec;
05957     rdata[0].len = SizeOfHeapNewpage;
05958     rdata[0].buffer = InvalidBuffer;
05959     rdata[0].next = &(rdata[1]);
05960 
05961     rdata[1].data = page;
05962     rdata[1].len = BLCKSZ;
05963     rdata[1].buffer = InvalidBuffer;
05964     rdata[1].next = NULL;
05965 
05966     recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_NEWPAGE, rdata);
05967 
05968     /*
05969      * The page may be uninitialized. If so, we can't set the LSN and TLI
05970      * because that would corrupt the page.
05971      */
05972     if (!PageIsNew(page))
05973     {
05974         PageSetLSN(page, recptr);
05975     }
05976 
05977     return recptr;
05978 }
05979 
05980 /*
05981  * Handles CLEANUP_INFO
05982  */
05983 static void
05984 heap_xlog_cleanup_info(XLogRecPtr lsn, XLogRecord *record)
05985 {
05986     xl_heap_cleanup_info *xlrec = (xl_heap_cleanup_info *) XLogRecGetData(record);
05987 
05988     if (InHotStandby)
05989         ResolveRecoveryConflictWithSnapshot(xlrec->latestRemovedXid, xlrec->node);
05990 
05991     /*
05992      * Actual operation is a no-op. Record type exists to provide a means for
05993      * conflict processing to occur before we begin index vacuum actions. see
05994      * vacuumlazy.c and also comments in btvacuumpage()
05995      */
05996 
05997     /* Backup blocks are not used in cleanup_info records */
05998     Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
05999 }
06000 
06001 /*
06002  * Handles HEAP2_CLEAN record type
06003  */
06004 static void
06005 heap_xlog_clean(XLogRecPtr lsn, XLogRecord *record)
06006 {
06007     xl_heap_clean *xlrec = (xl_heap_clean *) XLogRecGetData(record);
06008     Buffer      buffer;
06009     Page        page;
06010     OffsetNumber *end;
06011     OffsetNumber *redirected;
06012     OffsetNumber *nowdead;
06013     OffsetNumber *nowunused;
06014     int         nredirected;
06015     int         ndead;
06016     int         nunused;
06017     Size        freespace;
06018 
06019     /*
06020      * We're about to remove tuples. In Hot Standby mode, ensure that there's
06021      * no queries running for which the removed tuples are still visible.
06022      *
06023      * Not all HEAP2_CLEAN records remove tuples with xids, so we only want to
06024      * conflict on the records that cause MVCC failures for user queries. If
06025      * latestRemovedXid is invalid, skip conflict processing.
06026      */
06027     if (InHotStandby && TransactionIdIsValid(xlrec->latestRemovedXid))
06028         ResolveRecoveryConflictWithSnapshot(xlrec->latestRemovedXid,
06029                                             xlrec->node);
06030 
06031     /*
06032      * If we have a full-page image, restore it (using a cleanup lock) and
06033      * we're done.
06034      */
06035     if (record->xl_info & XLR_BKP_BLOCK(0))
06036     {
06037         (void) RestoreBackupBlock(lsn, record, 0, true, false);
06038         return;
06039     }
06040 
06041     buffer = XLogReadBufferExtended(xlrec->node, MAIN_FORKNUM, xlrec->block, RBM_NORMAL);
06042     if (!BufferIsValid(buffer))
06043         return;
06044     LockBufferForCleanup(buffer);
06045     page = (Page) BufferGetPage(buffer);
06046 
06047     if (lsn <= PageGetLSN(page))
06048     {
06049         UnlockReleaseBuffer(buffer);
06050         return;
06051     }
06052 
06053     nredirected = xlrec->nredirected;
06054     ndead = xlrec->ndead;
06055     end = (OffsetNumber *) ((char *) xlrec + record->xl_len);
06056     redirected = (OffsetNumber *) ((char *) xlrec + SizeOfHeapClean);
06057     nowdead = redirected + (nredirected * 2);
06058     nowunused = nowdead + ndead;
06059     nunused = (end - nowunused);
06060     Assert(nunused >= 0);
06061 
06062     /* Update all item pointers per the record, and repair fragmentation */
06063     heap_page_prune_execute(buffer,
06064                             redirected, nredirected,
06065                             nowdead, ndead,
06066                             nowunused, nunused);
06067 
06068     freespace = PageGetHeapFreeSpace(page);     /* needed to update FSM below */
06069 
06070     /*
06071      * Note: we don't worry about updating the page's prunability hints. At
06072      * worst this will cause an extra prune cycle to occur soon.
06073      */
06074 
06075     PageSetLSN(page, lsn);
06076     MarkBufferDirty(buffer);
06077     UnlockReleaseBuffer(buffer);
06078 
06079     /*
06080      * Update the FSM as well.
06081      *
06082      * XXX: We don't get here if the page was restored from full page image.
06083      * We don't bother to update the FSM in that case, it doesn't need to be
06084      * totally accurate anyway.
06085      */
06086     XLogRecordPageWithFreeSpace(xlrec->node, xlrec->block, freespace);
06087 }
06088 
06089 static void
06090 heap_xlog_freeze(XLogRecPtr lsn, XLogRecord *record)
06091 {
06092     xl_heap_freeze *xlrec = (xl_heap_freeze *) XLogRecGetData(record);
06093     TransactionId cutoff_xid = xlrec->cutoff_xid;
06094     MultiXactId cutoff_multi = xlrec->cutoff_multi;
06095     Buffer      buffer;
06096     Page        page;
06097 
06098     /*
06099      * In Hot Standby mode, ensure that there's no queries running which still
06100      * consider the frozen xids as running.
06101      */
06102     if (InHotStandby)
06103         ResolveRecoveryConflictWithSnapshot(cutoff_xid, xlrec->node);
06104 
06105     /* If we have a full-page image, restore it and we're done */
06106     if (record->xl_info & XLR_BKP_BLOCK(0))
06107     {
06108         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06109         return;
06110     }
06111 
06112     buffer = XLogReadBuffer(xlrec->node, xlrec->block, false);
06113     if (!BufferIsValid(buffer))
06114         return;
06115     page = (Page) BufferGetPage(buffer);
06116 
06117     if (lsn <= PageGetLSN(page))
06118     {
06119         UnlockReleaseBuffer(buffer);
06120         return;
06121     }
06122 
06123     if (record->xl_len > SizeOfHeapFreeze)
06124     {
06125         OffsetNumber *offsets;
06126         OffsetNumber *offsets_end;
06127 
06128         offsets = (OffsetNumber *) ((char *) xlrec + SizeOfHeapFreeze);
06129         offsets_end = (OffsetNumber *) ((char *) xlrec + record->xl_len);
06130 
06131         while (offsets < offsets_end)
06132         {
06133             /* offsets[] entries are one-based */
06134             ItemId      lp = PageGetItemId(page, *offsets);
06135             HeapTupleHeader tuple = (HeapTupleHeader) PageGetItem(page, lp);
06136 
06137             (void) heap_freeze_tuple(tuple, cutoff_xid, cutoff_multi);
06138             offsets++;
06139         }
06140     }
06141 
06142     PageSetLSN(page, lsn);
06143     MarkBufferDirty(buffer);
06144     UnlockReleaseBuffer(buffer);
06145 }
06146 
06147 /*
06148  * Replay XLOG_HEAP2_VISIBLE record.
06149  *
06150  * The critical integrity requirement here is that we must never end up with
06151  * a situation where the visibility map bit is set, and the page-level
06152  * PD_ALL_VISIBLE bit is clear.  If that were to occur, then a subsequent
06153  * page modification would fail to clear the visibility map bit.
06154  */
06155 static void
06156 heap_xlog_visible(XLogRecPtr lsn, XLogRecord *record)
06157 {
06158     xl_heap_visible *xlrec = (xl_heap_visible *) XLogRecGetData(record);
06159 
06160     /*
06161      * If there are any Hot Standby transactions running that have an xmin
06162      * horizon old enough that this page isn't all-visible for them, they
06163      * might incorrectly decide that an index-only scan can skip a heap fetch.
06164      *
06165      * NB: It might be better to throw some kind of "soft" conflict here that
06166      * forces any index-only scan that is in flight to perform heap fetches,
06167      * rather than killing the transaction outright.
06168      */
06169     if (InHotStandby)
06170         ResolveRecoveryConflictWithSnapshot(xlrec->cutoff_xid, xlrec->node);
06171 
06172     /*
06173      * If heap block was backed up, restore it. This can only happen with
06174      * checksums enabled.
06175      */
06176     if (record->xl_info & XLR_BKP_BLOCK(1))
06177     {
06178         Assert(DataChecksumsEnabled());
06179         (void) RestoreBackupBlock(lsn, record, 1, false, false);
06180     }
06181     else
06182     {
06183         Buffer      buffer;
06184         Page        page;
06185 
06186         /*
06187          * Read the heap page, if it still exists. If the heap file has been
06188          * dropped or truncated later in recovery, we don't need to update the
06189          * page, but we'd better still update the visibility map.
06190          */
06191         buffer = XLogReadBufferExtended(xlrec->node, MAIN_FORKNUM,
06192                                         xlrec->block, RBM_NORMAL);
06193         if (BufferIsValid(buffer))
06194         {
06195             LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
06196 
06197             page = (Page) BufferGetPage(buffer);
06198 
06199             /*
06200              * We don't bump the LSN of the heap page when setting the
06201              * visibility map bit (unless checksums are enabled, in which case
06202              * we must), because that would generate an unworkable volume of
06203              * full-page writes.  This exposes us to torn page hazards, but
06204              * since we're not inspecting the existing page contents in any
06205              * way, we don't care.
06206              *
06207              * However, all operations that clear the visibility map bit *do*
06208              * bump the LSN, and those operations will only be replayed if the
06209              * XLOG LSN follows the page LSN.  Thus, if the page LSN has
06210              * advanced past our XLOG record's LSN, we mustn't mark the page
06211              * all-visible, because the subsequent update won't be replayed to
06212              * clear the flag.
06213              */
06214             if (lsn > PageGetLSN(page))
06215             {
06216                 PageSetAllVisible(page);
06217                 MarkBufferDirty(buffer);
06218             }
06219 
06220             /* Done with heap page. */
06221             UnlockReleaseBuffer(buffer);
06222         }
06223     }
06224 
06225     /*
06226      * Even if we skipped the heap page update due to the LSN interlock, it's
06227      * still safe to update the visibility map.  Any WAL record that clears
06228      * the visibility map bit does so before checking the page LSN, so any
06229      * bits that need to be cleared will still be cleared.
06230      */
06231     if (record->xl_info & XLR_BKP_BLOCK(0))
06232         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06233     else
06234     {
06235         Relation    reln;
06236         Buffer      vmbuffer = InvalidBuffer;
06237 
06238         reln = CreateFakeRelcacheEntry(xlrec->node);
06239         visibilitymap_pin(reln, xlrec->block, &vmbuffer);
06240 
06241         /*
06242          * Don't set the bit if replay has already passed this point.
06243          *
06244          * It might be safe to do this unconditionally; if replay has passed
06245          * this point, we'll replay at least as far this time as we did
06246          * before, and if this bit needs to be cleared, the record responsible
06247          * for doing so should be again replayed, and clear it.  For right
06248          * now, out of an abundance of conservatism, we use the same test here
06249          * we did for the heap page.  If this results in a dropped bit, no
06250          * real harm is done; and the next VACUUM will fix it.
06251          */
06252         if (lsn > PageGetLSN(BufferGetPage(vmbuffer)))
06253             visibilitymap_set(reln, xlrec->block, InvalidBuffer, lsn, vmbuffer,
06254                               xlrec->cutoff_xid);
06255 
06256         ReleaseBuffer(vmbuffer);
06257         FreeFakeRelcacheEntry(reln);
06258     }
06259 }
06260 
06261 static void
06262 heap_xlog_newpage(XLogRecPtr lsn, XLogRecord *record)
06263 {
06264     xl_heap_newpage *xlrec = (xl_heap_newpage *) XLogRecGetData(record);
06265     Buffer      buffer;
06266     Page        page;
06267 
06268     /* Backup blocks are not used in newpage records */
06269     Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
06270 
06271     /*
06272      * Note: the NEWPAGE log record is used for both heaps and indexes, so do
06273      * not do anything that assumes we are touching a heap.
06274      */
06275     buffer = XLogReadBufferExtended(xlrec->node, xlrec->forknum, xlrec->blkno,
06276                                     RBM_ZERO);
06277     Assert(BufferIsValid(buffer));
06278     LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
06279     page = (Page) BufferGetPage(buffer);
06280 
06281     Assert(record->xl_len == SizeOfHeapNewpage + BLCKSZ);
06282     memcpy(page, (char *) xlrec + SizeOfHeapNewpage, BLCKSZ);
06283 
06284     /*
06285      * The page may be uninitialized. If so, we can't set the LSN because that
06286      * would corrupt the page.
06287      */
06288     if (!PageIsNew(page))
06289     {
06290         PageSetLSN(page, lsn);
06291     }
06292 
06293     MarkBufferDirty(buffer);
06294     UnlockReleaseBuffer(buffer);
06295 }
06296 
06297 /*
06298  * Given an "infobits" field from an XLog record, set the correct bits in the
06299  * given infomask and infomask2 for the tuple touched by the record.
06300  *
06301  * (This is the reverse of compute_infobits).
06302  */
06303 static void
06304 fix_infomask_from_infobits(uint8 infobits, uint16 *infomask, uint16 *infomask2)
06305 {
06306     *infomask &= ~(HEAP_XMAX_IS_MULTI | HEAP_XMAX_LOCK_ONLY |
06307                    HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_EXCL_LOCK);
06308     *infomask2 &= ~HEAP_KEYS_UPDATED;
06309 
06310     if (infobits & XLHL_XMAX_IS_MULTI)
06311         *infomask |= HEAP_XMAX_IS_MULTI;
06312     if (infobits & XLHL_XMAX_LOCK_ONLY)
06313         *infomask |= HEAP_XMAX_LOCK_ONLY;
06314     if (infobits & XLHL_XMAX_EXCL_LOCK)
06315         *infomask |= HEAP_XMAX_EXCL_LOCK;
06316     /* note HEAP_XMAX_SHR_LOCK isn't considered here */
06317     if (infobits & XLHL_XMAX_KEYSHR_LOCK)
06318         *infomask |= HEAP_XMAX_KEYSHR_LOCK;
06319 
06320     if (infobits & XLHL_KEYS_UPDATED)
06321         *infomask2 |= HEAP_KEYS_UPDATED;
06322 }
06323 
06324 static void
06325 heap_xlog_delete(XLogRecPtr lsn, XLogRecord *record)
06326 {
06327     xl_heap_delete *xlrec = (xl_heap_delete *) XLogRecGetData(record);
06328     Buffer      buffer;
06329     Page        page;
06330     OffsetNumber offnum;
06331     ItemId      lp = NULL;
06332     HeapTupleHeader htup;
06333     BlockNumber blkno;
06334 
06335     blkno = ItemPointerGetBlockNumber(&(xlrec->target.tid));
06336 
06337     /*
06338      * The visibility map may need to be fixed even if the heap page is
06339      * already up-to-date.
06340      */
06341     if (xlrec->all_visible_cleared)
06342     {
06343         Relation    reln = CreateFakeRelcacheEntry(xlrec->target.node);
06344         Buffer      vmbuffer = InvalidBuffer;
06345 
06346         visibilitymap_pin(reln, blkno, &vmbuffer);
06347         visibilitymap_clear(reln, blkno, vmbuffer);
06348         ReleaseBuffer(vmbuffer);
06349         FreeFakeRelcacheEntry(reln);
06350     }
06351 
06352     /* If we have a full-page image, restore it and we're done */
06353     if (record->xl_info & XLR_BKP_BLOCK(0))
06354     {
06355         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06356         return;
06357     }
06358 
06359     buffer = XLogReadBuffer(xlrec->target.node, blkno, false);
06360     if (!BufferIsValid(buffer))
06361         return;
06362     page = (Page) BufferGetPage(buffer);
06363 
06364     if (lsn <= PageGetLSN(page))        /* changes are applied */
06365     {
06366         UnlockReleaseBuffer(buffer);
06367         return;
06368     }
06369 
06370     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
06371     if (PageGetMaxOffsetNumber(page) >= offnum)
06372         lp = PageGetItemId(page, offnum);
06373 
06374     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
06375         elog(PANIC, "heap_delete_redo: invalid lp");
06376 
06377     htup = (HeapTupleHeader) PageGetItem(page, lp);
06378 
06379     htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
06380     htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
06381     HeapTupleHeaderClearHotUpdated(htup);
06382     fix_infomask_from_infobits(xlrec->infobits_set,
06383                                &htup->t_infomask, &htup->t_infomask2);
06384     HeapTupleHeaderSetXmax(htup, xlrec->xmax);
06385     HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
06386 
06387     /* Mark the page as a candidate for pruning */
06388     PageSetPrunable(page, record->xl_xid);
06389 
06390     if (xlrec->all_visible_cleared)
06391         PageClearAllVisible(page);
06392 
06393     /* Make sure there is no forward chain link in t_ctid */
06394     htup->t_ctid = xlrec->target.tid;
06395     PageSetLSN(page, lsn);
06396     MarkBufferDirty(buffer);
06397     UnlockReleaseBuffer(buffer);
06398 }
06399 
06400 static void
06401 heap_xlog_insert(XLogRecPtr lsn, XLogRecord *record)
06402 {
06403     xl_heap_insert *xlrec = (xl_heap_insert *) XLogRecGetData(record);
06404     Buffer      buffer;
06405     Page        page;
06406     OffsetNumber offnum;
06407     struct
06408     {
06409         HeapTupleHeaderData hdr;
06410         char        data[MaxHeapTupleSize];
06411     }           tbuf;
06412     HeapTupleHeader htup;
06413     xl_heap_header xlhdr;
06414     uint32      newlen;
06415     Size        freespace;
06416     BlockNumber blkno;
06417 
06418     blkno = ItemPointerGetBlockNumber(&(xlrec->target.tid));
06419 
06420     /*
06421      * The visibility map may need to be fixed even if the heap page is
06422      * already up-to-date.
06423      */
06424     if (xlrec->all_visible_cleared)
06425     {
06426         Relation    reln = CreateFakeRelcacheEntry(xlrec->target.node);
06427         Buffer      vmbuffer = InvalidBuffer;
06428 
06429         visibilitymap_pin(reln, blkno, &vmbuffer);
06430         visibilitymap_clear(reln, blkno, vmbuffer);
06431         ReleaseBuffer(vmbuffer);
06432         FreeFakeRelcacheEntry(reln);
06433     }
06434 
06435     /* If we have a full-page image, restore it and we're done */
06436     if (record->xl_info & XLR_BKP_BLOCK(0))
06437     {
06438         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06439         return;
06440     }
06441 
06442     if (record->xl_info & XLOG_HEAP_INIT_PAGE)
06443     {
06444         buffer = XLogReadBuffer(xlrec->target.node, blkno, true);
06445         Assert(BufferIsValid(buffer));
06446         page = (Page) BufferGetPage(buffer);
06447 
06448         PageInit(page, BufferGetPageSize(buffer), 0);
06449     }
06450     else
06451     {
06452         buffer = XLogReadBuffer(xlrec->target.node, blkno, false);
06453         if (!BufferIsValid(buffer))
06454             return;
06455         page = (Page) BufferGetPage(buffer);
06456 
06457         if (lsn <= PageGetLSN(page))    /* changes are applied */
06458         {
06459             UnlockReleaseBuffer(buffer);
06460             return;
06461         }
06462     }
06463 
06464     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
06465     if (PageGetMaxOffsetNumber(page) + 1 < offnum)
06466         elog(PANIC, "heap_insert_redo: invalid max offset number");
06467 
06468     newlen = record->xl_len - SizeOfHeapInsert - SizeOfHeapHeader;
06469     Assert(newlen <= MaxHeapTupleSize);
06470     memcpy((char *) &xlhdr,
06471            (char *) xlrec + SizeOfHeapInsert,
06472            SizeOfHeapHeader);
06473     htup = &tbuf.hdr;
06474     MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
06475     /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
06476     memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
06477            (char *) xlrec + SizeOfHeapInsert + SizeOfHeapHeader,
06478            newlen);
06479     newlen += offsetof(HeapTupleHeaderData, t_bits);
06480     htup->t_infomask2 = xlhdr.t_infomask2;
06481     htup->t_infomask = xlhdr.t_infomask;
06482     htup->t_hoff = xlhdr.t_hoff;
06483     HeapTupleHeaderSetXmin(htup, record->xl_xid);
06484     HeapTupleHeaderSetCmin(htup, FirstCommandId);
06485     htup->t_ctid = xlrec->target.tid;
06486 
06487     offnum = PageAddItem(page, (Item) htup, newlen, offnum, true, true);
06488     if (offnum == InvalidOffsetNumber)
06489         elog(PANIC, "heap_insert_redo: failed to add tuple");
06490 
06491     freespace = PageGetHeapFreeSpace(page);     /* needed to update FSM below */
06492 
06493     PageSetLSN(page, lsn);
06494 
06495     if (xlrec->all_visible_cleared)
06496         PageClearAllVisible(page);
06497 
06498     MarkBufferDirty(buffer);
06499     UnlockReleaseBuffer(buffer);
06500 
06501     /*
06502      * If the page is running low on free space, update the FSM as well.
06503      * Arbitrarily, our definition of "low" is less than 20%. We can't do much
06504      * better than that without knowing the fill-factor for the table.
06505      *
06506      * XXX: We don't get here if the page was restored from full page image.
06507      * We don't bother to update the FSM in that case, it doesn't need to be
06508      * totally accurate anyway.
06509      */
06510     if (freespace < BLCKSZ / 5)
06511         XLogRecordPageWithFreeSpace(xlrec->target.node, blkno, freespace);
06512 }
06513 
06514 /*
06515  * Handles MULTI_INSERT record type.
06516  */
06517 static void
06518 heap_xlog_multi_insert(XLogRecPtr lsn, XLogRecord *record)
06519 {
06520     char       *recdata = XLogRecGetData(record);
06521     xl_heap_multi_insert *xlrec;
06522     Buffer      buffer;
06523     Page        page;
06524     struct
06525     {
06526         HeapTupleHeaderData hdr;
06527         char        data[MaxHeapTupleSize];
06528     }           tbuf;
06529     HeapTupleHeader htup;
06530     uint32      newlen;
06531     Size        freespace;
06532     BlockNumber blkno;
06533     int         i;
06534     bool        isinit = (record->xl_info & XLOG_HEAP_INIT_PAGE) != 0;
06535 
06536     /*
06537      * Insertion doesn't overwrite MVCC data, so no conflict processing is
06538      * required.
06539      */
06540 
06541     xlrec = (xl_heap_multi_insert *) recdata;
06542     recdata += SizeOfHeapMultiInsert;
06543 
06544     /*
06545      * If we're reinitializing the page, the tuples are stored in order from
06546      * FirstOffsetNumber. Otherwise there's an array of offsets in the WAL
06547      * record.
06548      */
06549     if (!isinit)
06550         recdata += sizeof(OffsetNumber) * xlrec->ntuples;
06551 
06552     blkno = xlrec->blkno;
06553 
06554     /*
06555      * The visibility map may need to be fixed even if the heap page is
06556      * already up-to-date.
06557      */
06558     if (xlrec->all_visible_cleared)
06559     {
06560         Relation    reln = CreateFakeRelcacheEntry(xlrec->node);
06561         Buffer      vmbuffer = InvalidBuffer;
06562 
06563         visibilitymap_pin(reln, blkno, &vmbuffer);
06564         visibilitymap_clear(reln, blkno, vmbuffer);
06565         ReleaseBuffer(vmbuffer);
06566         FreeFakeRelcacheEntry(reln);
06567     }
06568 
06569     /* If we have a full-page image, restore it and we're done */
06570     if (record->xl_info & XLR_BKP_BLOCK(0))
06571     {
06572         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06573         return;
06574     }
06575 
06576     if (isinit)
06577     {
06578         buffer = XLogReadBuffer(xlrec->node, blkno, true);
06579         Assert(BufferIsValid(buffer));
06580         page = (Page) BufferGetPage(buffer);
06581 
06582         PageInit(page, BufferGetPageSize(buffer), 0);
06583     }
06584     else
06585     {
06586         buffer = XLogReadBuffer(xlrec->node, blkno, false);
06587         if (!BufferIsValid(buffer))
06588             return;
06589         page = (Page) BufferGetPage(buffer);
06590 
06591         if (lsn <= PageGetLSN(page))    /* changes are applied */
06592         {
06593             UnlockReleaseBuffer(buffer);
06594             return;
06595         }
06596     }
06597 
06598     for (i = 0; i < xlrec->ntuples; i++)
06599     {
06600         OffsetNumber offnum;
06601         xl_multi_insert_tuple *xlhdr;
06602 
06603         if (isinit)
06604             offnum = FirstOffsetNumber + i;
06605         else
06606             offnum = xlrec->offsets[i];
06607         if (PageGetMaxOffsetNumber(page) + 1 < offnum)
06608             elog(PANIC, "heap_multi_insert_redo: invalid max offset number");
06609 
06610         xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(recdata);
06611         recdata = ((char *) xlhdr) + SizeOfMultiInsertTuple;
06612 
06613         newlen = xlhdr->datalen;
06614         Assert(newlen <= MaxHeapTupleSize);
06615         htup = &tbuf.hdr;
06616         MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
06617         /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
06618         memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
06619                (char *) recdata,
06620                newlen);
06621         recdata += newlen;
06622 
06623         newlen += offsetof(HeapTupleHeaderData, t_bits);
06624         htup->t_infomask2 = xlhdr->t_infomask2;
06625         htup->t_infomask = xlhdr->t_infomask;
06626         htup->t_hoff = xlhdr->t_hoff;
06627         HeapTupleHeaderSetXmin(htup, record->xl_xid);
06628         HeapTupleHeaderSetCmin(htup, FirstCommandId);
06629         ItemPointerSetBlockNumber(&htup->t_ctid, blkno);
06630         ItemPointerSetOffsetNumber(&htup->t_ctid, offnum);
06631 
06632         offnum = PageAddItem(page, (Item) htup, newlen, offnum, true, true);
06633         if (offnum == InvalidOffsetNumber)
06634             elog(PANIC, "heap_multi_insert_redo: failed to add tuple");
06635     }
06636 
06637     freespace = PageGetHeapFreeSpace(page);     /* needed to update FSM below */
06638 
06639     PageSetLSN(page, lsn);
06640 
06641     if (xlrec->all_visible_cleared)
06642         PageClearAllVisible(page);
06643 
06644     MarkBufferDirty(buffer);
06645     UnlockReleaseBuffer(buffer);
06646 
06647     /*
06648      * If the page is running low on free space, update the FSM as well.
06649      * Arbitrarily, our definition of "low" is less than 20%. We can't do much
06650      * better than that without knowing the fill-factor for the table.
06651      *
06652      * XXX: We don't get here if the page was restored from full page image.
06653      * We don't bother to update the FSM in that case, it doesn't need to be
06654      * totally accurate anyway.
06655      */
06656     if (freespace < BLCKSZ / 5)
06657         XLogRecordPageWithFreeSpace(xlrec->node, blkno, freespace);
06658 }
06659 
06660 /*
06661  * Handles UPDATE and HOT_UPDATE
06662  */
06663 static void
06664 heap_xlog_update(XLogRecPtr lsn, XLogRecord *record, bool hot_update)
06665 {
06666     xl_heap_update *xlrec = (xl_heap_update *) XLogRecGetData(record);
06667     bool        samepage = (ItemPointerGetBlockNumber(&(xlrec->newtid)) ==
06668                             ItemPointerGetBlockNumber(&(xlrec->target.tid)));
06669     Buffer      obuffer,
06670                 nbuffer;
06671     Page        page;
06672     OffsetNumber offnum;
06673     ItemId      lp = NULL;
06674     HeapTupleHeader htup;
06675     struct
06676     {
06677         HeapTupleHeaderData hdr;
06678         char        data[MaxHeapTupleSize];
06679     }           tbuf;
06680     xl_heap_header xlhdr;
06681     int         hsize;
06682     uint32      newlen;
06683     Size        freespace;
06684 
06685     /*
06686      * The visibility map may need to be fixed even if the heap page is
06687      * already up-to-date.
06688      */
06689     if (xlrec->all_visible_cleared)
06690     {
06691         Relation    reln = CreateFakeRelcacheEntry(xlrec->target.node);
06692         BlockNumber block = ItemPointerGetBlockNumber(&xlrec->target.tid);
06693         Buffer      vmbuffer = InvalidBuffer;
06694 
06695         visibilitymap_pin(reln, block, &vmbuffer);
06696         visibilitymap_clear(reln, block, vmbuffer);
06697         ReleaseBuffer(vmbuffer);
06698         FreeFakeRelcacheEntry(reln);
06699     }
06700 
06701     /*
06702      * In normal operation, it is important to lock the two pages in
06703      * page-number order, to avoid possible deadlocks against other update
06704      * operations going the other way.  However, during WAL replay there can
06705      * be no other update happening, so we don't need to worry about that. But
06706      * we *do* need to worry that we don't expose an inconsistent state to Hot
06707      * Standby queries --- so the original page can't be unlocked before we've
06708      * added the new tuple to the new page.
06709      */
06710 
06711     if (record->xl_info & XLR_BKP_BLOCK(0))
06712     {
06713         obuffer = RestoreBackupBlock(lsn, record, 0, false, true);
06714         if (samepage)
06715         {
06716             /* backup block covered both changes, so we're done */
06717             UnlockReleaseBuffer(obuffer);
06718             return;
06719         }
06720         goto newt;
06721     }
06722 
06723     /* Deal with old tuple version */
06724 
06725     obuffer = XLogReadBuffer(xlrec->target.node,
06726                              ItemPointerGetBlockNumber(&(xlrec->target.tid)),
06727                              false);
06728     if (!BufferIsValid(obuffer))
06729         goto newt;
06730     page = (Page) BufferGetPage(obuffer);
06731 
06732     if (lsn <= PageGetLSN(page))        /* changes are applied */
06733     {
06734         if (samepage)
06735         {
06736             UnlockReleaseBuffer(obuffer);
06737             return;
06738         }
06739         goto newt;
06740     }
06741 
06742     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
06743     if (PageGetMaxOffsetNumber(page) >= offnum)
06744         lp = PageGetItemId(page, offnum);
06745 
06746     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
06747         elog(PANIC, "heap_update_redo: invalid lp");
06748 
06749     htup = (HeapTupleHeader) PageGetItem(page, lp);
06750 
06751     htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
06752     htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
06753     if (hot_update)
06754         HeapTupleHeaderSetHotUpdated(htup);
06755     else
06756         HeapTupleHeaderClearHotUpdated(htup);
06757     fix_infomask_from_infobits(xlrec->old_infobits_set, &htup->t_infomask,
06758                                &htup->t_infomask2);
06759     HeapTupleHeaderSetXmax(htup, xlrec->old_xmax);
06760     HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
06761     /* Set forward chain link in t_ctid */
06762     htup->t_ctid = xlrec->newtid;
06763 
06764     /* Mark the page as a candidate for pruning */
06765     PageSetPrunable(page, record->xl_xid);
06766 
06767     if (xlrec->all_visible_cleared)
06768         PageClearAllVisible(page);
06769 
06770     /*
06771      * this test is ugly, but necessary to avoid thinking that insert change
06772      * is already applied
06773      */
06774     if (samepage)
06775     {
06776         nbuffer = obuffer;
06777         goto newsame;
06778     }
06779 
06780     PageSetLSN(page, lsn);
06781     MarkBufferDirty(obuffer);
06782 
06783     /* Deal with new tuple */
06784 
06785 newt:;
06786 
06787     /*
06788      * The visibility map may need to be fixed even if the heap page is
06789      * already up-to-date.
06790      */
06791     if (xlrec->new_all_visible_cleared)
06792     {
06793         Relation    reln = CreateFakeRelcacheEntry(xlrec->target.node);
06794         BlockNumber block = ItemPointerGetBlockNumber(&xlrec->newtid);
06795         Buffer      vmbuffer = InvalidBuffer;
06796 
06797         visibilitymap_pin(reln, block, &vmbuffer);
06798         visibilitymap_clear(reln, block, vmbuffer);
06799         ReleaseBuffer(vmbuffer);
06800         FreeFakeRelcacheEntry(reln);
06801     }
06802 
06803     if (record->xl_info & XLR_BKP_BLOCK(1))
06804     {
06805         (void) RestoreBackupBlock(lsn, record, 1, false, false);
06806         if (BufferIsValid(obuffer))
06807             UnlockReleaseBuffer(obuffer);
06808         return;
06809     }
06810 
06811     if (record->xl_info & XLOG_HEAP_INIT_PAGE)
06812     {
06813         nbuffer = XLogReadBuffer(xlrec->target.node,
06814                                  ItemPointerGetBlockNumber(&(xlrec->newtid)),
06815                                  true);
06816         Assert(BufferIsValid(nbuffer));
06817         page = (Page) BufferGetPage(nbuffer);
06818 
06819         PageInit(page, BufferGetPageSize(nbuffer), 0);
06820     }
06821     else
06822     {
06823         nbuffer = XLogReadBuffer(xlrec->target.node,
06824                                  ItemPointerGetBlockNumber(&(xlrec->newtid)),
06825                                  false);
06826         if (!BufferIsValid(nbuffer))
06827         {
06828             if (BufferIsValid(obuffer))
06829                 UnlockReleaseBuffer(obuffer);
06830             return;
06831         }
06832         page = (Page) BufferGetPage(nbuffer);
06833 
06834         if (lsn <= PageGetLSN(page))    /* changes are applied */
06835         {
06836             UnlockReleaseBuffer(nbuffer);
06837             if (BufferIsValid(obuffer))
06838                 UnlockReleaseBuffer(obuffer);
06839             return;
06840         }
06841     }
06842 
06843 newsame:;
06844 
06845     offnum = ItemPointerGetOffsetNumber(&(xlrec->newtid));
06846     if (PageGetMaxOffsetNumber(page) + 1 < offnum)
06847         elog(PANIC, "heap_update_redo: invalid max offset number");
06848 
06849     hsize = SizeOfHeapUpdate + SizeOfHeapHeader;
06850 
06851     newlen = record->xl_len - hsize;
06852     Assert(newlen <= MaxHeapTupleSize);
06853     memcpy((char *) &xlhdr,
06854            (char *) xlrec + SizeOfHeapUpdate,
06855            SizeOfHeapHeader);
06856     htup = &tbuf.hdr;
06857     MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
06858     /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
06859     memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
06860            (char *) xlrec + hsize,
06861            newlen);
06862     newlen += offsetof(HeapTupleHeaderData, t_bits);
06863     htup->t_infomask2 = xlhdr.t_infomask2;
06864     htup->t_infomask = xlhdr.t_infomask;
06865     htup->t_hoff = xlhdr.t_hoff;
06866 
06867     HeapTupleHeaderSetXmin(htup, record->xl_xid);
06868     HeapTupleHeaderSetCmin(htup, FirstCommandId);
06869     HeapTupleHeaderSetXmax(htup, xlrec->new_xmax);
06870     /* Make sure there is no forward chain link in t_ctid */
06871     htup->t_ctid = xlrec->newtid;
06872 
06873     offnum = PageAddItem(page, (Item) htup, newlen, offnum, true, true);
06874     if (offnum == InvalidOffsetNumber)
06875         elog(PANIC, "heap_update_redo: failed to add tuple");
06876 
06877     if (xlrec->new_all_visible_cleared)
06878         PageClearAllVisible(page);
06879 
06880     freespace = PageGetHeapFreeSpace(page);     /* needed to update FSM below */
06881 
06882     PageSetLSN(page, lsn);
06883     MarkBufferDirty(nbuffer);
06884     UnlockReleaseBuffer(nbuffer);
06885 
06886     if (BufferIsValid(obuffer) && obuffer != nbuffer)
06887         UnlockReleaseBuffer(obuffer);
06888 
06889     /*
06890      * If the new page is running low on free space, update the FSM as well.
06891      * Arbitrarily, our definition of "low" is less than 20%. We can't do much
06892      * better than that without knowing the fill-factor for the table.
06893      *
06894      * However, don't update the FSM on HOT updates, because after crash
06895      * recovery, either the old or the new tuple will certainly be dead and
06896      * prunable. After pruning, the page will have roughly as much free space
06897      * as it did before the update, assuming the new tuple is about the same
06898      * size as the old one.
06899      *
06900      * XXX: We don't get here if the page was restored from full page image.
06901      * We don't bother to update the FSM in that case, it doesn't need to be
06902      * totally accurate anyway.
06903      */
06904     if (!hot_update && freespace < BLCKSZ / 5)
06905         XLogRecordPageWithFreeSpace(xlrec->target.node,
06906                                  ItemPointerGetBlockNumber(&(xlrec->newtid)),
06907                                     freespace);
06908 }
06909 
06910 static void
06911 heap_xlog_lock(XLogRecPtr lsn, XLogRecord *record)
06912 {
06913     xl_heap_lock *xlrec = (xl_heap_lock *) XLogRecGetData(record);
06914     Buffer      buffer;
06915     Page        page;
06916     OffsetNumber offnum;
06917     ItemId      lp = NULL;
06918     HeapTupleHeader htup;
06919 
06920     /* If we have a full-page image, restore it and we're done */
06921     if (record->xl_info & XLR_BKP_BLOCK(0))
06922     {
06923         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06924         return;
06925     }
06926 
06927     buffer = XLogReadBuffer(xlrec->target.node,
06928                             ItemPointerGetBlockNumber(&(xlrec->target.tid)),
06929                             false);
06930     if (!BufferIsValid(buffer))
06931         return;
06932     page = (Page) BufferGetPage(buffer);
06933 
06934     if (lsn <= PageGetLSN(page))        /* changes are applied */
06935     {
06936         UnlockReleaseBuffer(buffer);
06937         return;
06938     }
06939 
06940     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
06941     if (PageGetMaxOffsetNumber(page) >= offnum)
06942         lp = PageGetItemId(page, offnum);
06943 
06944     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
06945         elog(PANIC, "heap_lock_redo: invalid lp");
06946 
06947     htup = (HeapTupleHeader) PageGetItem(page, lp);
06948 
06949     fix_infomask_from_infobits(xlrec->infobits_set, &htup->t_infomask,
06950                                &htup->t_infomask2);
06951     HeapTupleHeaderClearHotUpdated(htup);
06952     HeapTupleHeaderSetXmax(htup, xlrec->locking_xid);
06953     HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
06954     /* Make sure there is no forward chain link in t_ctid */
06955     htup->t_ctid = xlrec->target.tid;
06956     PageSetLSN(page, lsn);
06957     MarkBufferDirty(buffer);
06958     UnlockReleaseBuffer(buffer);
06959 }
06960 
06961 static void
06962 heap_xlog_lock_updated(XLogRecPtr lsn, XLogRecord *record)
06963 {
06964     xl_heap_lock_updated *xlrec =
06965         (xl_heap_lock_updated *) XLogRecGetData(record);
06966     Buffer      buffer;
06967     Page        page;
06968     OffsetNumber offnum;
06969     ItemId      lp = NULL;
06970     HeapTupleHeader htup;
06971 
06972     /* If we have a full-page image, restore it and we're done */
06973     if (record->xl_info & XLR_BKP_BLOCK(0))
06974     {
06975         (void) RestoreBackupBlock(lsn, record, 0, false, false);
06976         return;
06977     }
06978 
06979     buffer = XLogReadBuffer(xlrec->target.node,
06980                             ItemPointerGetBlockNumber(&(xlrec->target.tid)),
06981                             false);
06982     if (!BufferIsValid(buffer))
06983         return;
06984     page = (Page) BufferGetPage(buffer);
06985 
06986     if (lsn <= PageGetLSN(page))        /* changes are applied */
06987     {
06988         UnlockReleaseBuffer(buffer);
06989         return;
06990     }
06991 
06992     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
06993     if (PageGetMaxOffsetNumber(page) >= offnum)
06994         lp = PageGetItemId(page, offnum);
06995 
06996     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
06997         elog(PANIC, "heap_xlog_lock_updated: invalid lp");
06998 
06999     htup = (HeapTupleHeader) PageGetItem(page, lp);
07000 
07001     fix_infomask_from_infobits(xlrec->infobits_set, &htup->t_infomask,
07002                                &htup->t_infomask2);
07003     HeapTupleHeaderSetXmax(htup, xlrec->xmax);
07004 
07005     PageSetLSN(page, lsn);
07006     MarkBufferDirty(buffer);
07007     UnlockReleaseBuffer(buffer);
07008 }
07009 
07010 static void
07011 heap_xlog_inplace(XLogRecPtr lsn, XLogRecord *record)
07012 {
07013     xl_heap_inplace *xlrec = (xl_heap_inplace *) XLogRecGetData(record);
07014     Buffer      buffer;
07015     Page        page;
07016     OffsetNumber offnum;
07017     ItemId      lp = NULL;
07018     HeapTupleHeader htup;
07019     uint32      oldlen;
07020     uint32      newlen;
07021 
07022     /* If we have a full-page image, restore it and we're done */
07023     if (record->xl_info & XLR_BKP_BLOCK(0))
07024     {
07025         (void) RestoreBackupBlock(lsn, record, 0, false, false);
07026         return;
07027     }
07028 
07029     buffer = XLogReadBuffer(xlrec->target.node,
07030                             ItemPointerGetBlockNumber(&(xlrec->target.tid)),
07031                             false);
07032     if (!BufferIsValid(buffer))
07033         return;
07034     page = (Page) BufferGetPage(buffer);
07035 
07036     if (lsn <= PageGetLSN(page))        /* changes are applied */
07037     {
07038         UnlockReleaseBuffer(buffer);
07039         return;
07040     }
07041 
07042     offnum = ItemPointerGetOffsetNumber(&(xlrec->target.tid));
07043     if (PageGetMaxOffsetNumber(page) >= offnum)
07044         lp = PageGetItemId(page, offnum);
07045 
07046     if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
07047         elog(PANIC, "heap_inplace_redo: invalid lp");
07048 
07049     htup = (HeapTupleHeader) PageGetItem(page, lp);
07050 
07051     oldlen = ItemIdGetLength(lp) - htup->t_hoff;
07052     newlen = record->xl_len - SizeOfHeapInplace;
07053     if (oldlen != newlen)
07054         elog(PANIC, "heap_inplace_redo: wrong tuple length");
07055 
07056     memcpy((char *) htup + htup->t_hoff,
07057            (char *) xlrec + SizeOfHeapInplace,
07058            newlen);
07059 
07060     PageSetLSN(page, lsn);
07061     MarkBufferDirty(buffer);
07062     UnlockReleaseBuffer(buffer);
07063 }
07064 
07065 void
07066 heap_redo(XLogRecPtr lsn, XLogRecord *record)
07067 {
07068     uint8       info = record->xl_info & ~XLR_INFO_MASK;
07069 
07070     /*
07071      * These operations don't overwrite MVCC data so no conflict processing is
07072      * required. The ones in heap2 rmgr do.
07073      */
07074 
07075     switch (info & XLOG_HEAP_OPMASK)
07076     {
07077         case XLOG_HEAP_INSERT:
07078             heap_xlog_insert(lsn, record);
07079             break;
07080         case XLOG_HEAP_DELETE:
07081             heap_xlog_delete(lsn, record);
07082             break;
07083         case XLOG_HEAP_UPDATE:
07084             heap_xlog_update(lsn, record, false);
07085             break;
07086         case XLOG_HEAP_HOT_UPDATE:
07087             heap_xlog_update(lsn, record, true);
07088             break;
07089         case XLOG_HEAP_NEWPAGE:
07090             heap_xlog_newpage(lsn, record);
07091             break;
07092         case XLOG_HEAP_LOCK:
07093             heap_xlog_lock(lsn, record);
07094             break;
07095         case XLOG_HEAP_INPLACE:
07096             heap_xlog_inplace(lsn, record);
07097             break;
07098         default:
07099             elog(PANIC, "heap_redo: unknown op code %u", info);
07100     }
07101 }
07102 
07103 void
07104 heap2_redo(XLogRecPtr lsn, XLogRecord *record)
07105 {
07106     uint8       info = record->xl_info & ~XLR_INFO_MASK;
07107 
07108     switch (info & XLOG_HEAP_OPMASK)
07109     {
07110         case XLOG_HEAP2_FREEZE:
07111             heap_xlog_freeze(lsn, record);
07112             break;
07113         case XLOG_HEAP2_CLEAN:
07114             heap_xlog_clean(lsn, record);
07115             break;
07116         case XLOG_HEAP2_CLEANUP_INFO:
07117             heap_xlog_cleanup_info(lsn, record);
07118             break;
07119         case XLOG_HEAP2_VISIBLE:
07120             heap_xlog_visible(lsn, record);
07121             break;
07122         case XLOG_HEAP2_MULTI_INSERT:
07123             heap_xlog_multi_insert(lsn, record);
07124             break;
07125         case XLOG_HEAP2_LOCK_UPDATED:
07126             heap_xlog_lock_updated(lsn, record);
07127             break;
07128         default:
07129             elog(PANIC, "heap2_redo: unknown op code %u", info);
07130     }
07131 }
07132 
07133 /*
07134  *  heap_sync       - sync a heap, for use when no WAL has been written
07135  *
07136  * This forces the heap contents (including TOAST heap if any) down to disk.
07137  * If we skipped using WAL, and WAL is otherwise needed, we must force the
07138  * relation down to disk before it's safe to commit the transaction.  This
07139  * requires writing out any dirty buffers and then doing a forced fsync.
07140  *
07141  * Indexes are not touched.  (Currently, index operations associated with
07142  * the commands that use this are WAL-logged and so do not need fsync.
07143  * That behavior might change someday, but in any case it's likely that
07144  * any fsync decisions required would be per-index and hence not appropriate
07145  * to be done here.)
07146  */
07147 void
07148 heap_sync(Relation rel)
07149 {
07150     /* non-WAL-logged tables never need fsync */
07151     if (!RelationNeedsWAL(rel))
07152         return;
07153 
07154     /* main heap */
07155     FlushRelationBuffers(rel);
07156     /* FlushRelationBuffers will have opened rd_smgr */
07157     smgrimmedsync(rel->rd_smgr, MAIN_FORKNUM);
07158 
07159     /* FSM is not critical, don't bother syncing it */
07160 
07161     /* toast heap, if any */
07162     if (OidIsValid(rel->rd_rel->reltoastrelid))
07163     {
07164         Relation    toastrel;
07165 
07166         toastrel = heap_open(rel->rd_rel->reltoastrelid, AccessShareLock);
07167         FlushRelationBuffers(toastrel);
07168         smgrimmedsync(toastrel->rd_smgr, MAIN_FORKNUM);
07169         heap_close(toastrel, AccessShareLock);
07170     }
07171 }