LCOV - code coverage report
Current view: top level - src/include/access - transam.h (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 12 13 92.3 %
Date: 2020-06-03 09:06:53 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * transam.h
       4             :  *    postgres transaction access method support code
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * src/include/access/transam.h
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #ifndef TRANSAM_H
      15             : #define TRANSAM_H
      16             : 
      17             : #include "access/xlogdefs.h"
      18             : 
      19             : 
      20             : /* ----------------
      21             :  *      Special transaction ID values
      22             :  *
      23             :  * BootstrapTransactionId is the XID for "bootstrap" operations, and
      24             :  * FrozenTransactionId is used for very old tuples.  Both should
      25             :  * always be considered valid.
      26             :  *
      27             :  * FirstNormalTransactionId is the first "normal" transaction id.
      28             :  * Note: if you need to change it, you must change pg_class.h as well.
      29             :  * ----------------
      30             :  */
      31             : #define InvalidTransactionId        ((TransactionId) 0)
      32             : #define BootstrapTransactionId      ((TransactionId) 1)
      33             : #define FrozenTransactionId         ((TransactionId) 2)
      34             : #define FirstNormalTransactionId    ((TransactionId) 3)
      35             : #define MaxTransactionId            ((TransactionId) 0xFFFFFFFF)
      36             : 
      37             : /* ----------------
      38             :  *      transaction ID manipulation macros
      39             :  * ----------------
      40             :  */
      41             : #define TransactionIdIsValid(xid)       ((xid) != InvalidTransactionId)
      42             : #define TransactionIdIsNormal(xid)      ((xid) >= FirstNormalTransactionId)
      43             : #define TransactionIdEquals(id1, id2)   ((id1) == (id2))
      44             : #define TransactionIdStore(xid, dest)   (*(dest) = (xid))
      45             : #define StoreInvalidTransactionId(dest) (*(dest) = InvalidTransactionId)
      46             : 
      47             : #define EpochFromFullTransactionId(x)   ((uint32) ((x).value >> 32))
      48             : #define XidFromFullTransactionId(x)     ((uint32) (x).value)
      49             : #define U64FromFullTransactionId(x)     ((x).value)
      50             : #define FullTransactionIdEquals(a, b)   ((a).value == (b).value)
      51             : #define FullTransactionIdPrecedes(a, b) ((a).value < (b).value)
      52             : #define FullTransactionIdPrecedesOrEquals(a, b) ((a).value <= (b).value)
      53             : #define FullTransactionIdFollows(a, b) ((a).value > (b).value)
      54             : #define FullTransactionIdFollowsOrEquals(a, b) ((a).value >= (b).value)
      55             : #define FullTransactionIdIsValid(x)     TransactionIdIsValid(XidFromFullTransactionId(x))
      56             : #define InvalidFullTransactionId        FullTransactionIdFromEpochAndXid(0, InvalidTransactionId)
      57             : 
      58             : /*
      59             :  * A 64 bit value that contains an epoch and a TransactionId.  This is
      60             :  * wrapped in a struct to prevent implicit conversion to/from TransactionId.
      61             :  * Not all values represent valid normal XIDs.
      62             :  */
      63             : typedef struct FullTransactionId
      64             : {
      65             :     uint64      value;
      66             : } FullTransactionId;
      67             : 
      68             : static inline FullTransactionId
      69     1501102 : FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
      70             : {
      71             :     FullTransactionId result;
      72             : 
      73     1501102 :     result.value = ((uint64) epoch) << 32 | xid;
      74             : 
      75     1501102 :     return result;
      76             : }
      77             : 
      78             : static inline FullTransactionId
      79        3750 : FullTransactionIdFromU64(uint64 value)
      80             : {
      81             :     FullTransactionId result;
      82             : 
      83        3750 :     result.value = value;
      84             : 
      85        3750 :     return result;
      86             : }
      87             : 
      88             : /* advance a transaction ID variable, handling wraparound correctly */
      89             : #define TransactionIdAdvance(dest)  \
      90             :     do { \
      91             :         (dest)++; \
      92             :         if ((dest) < FirstNormalTransactionId) \
      93             :             (dest) = FirstNormalTransactionId; \
      94             :     } while(0)
      95             : 
      96             : /* advance a FullTransactionId variable, stepping over special XIDs */
      97             : static inline void
      98      277444 : FullTransactionIdAdvance(FullTransactionId *dest)
      99             : {
     100      277444 :     dest->value++;
     101      277444 :     while (XidFromFullTransactionId(*dest) < FirstNormalTransactionId)
     102           0 :         dest->value++;
     103      277444 : }
     104             : 
     105             : /* back up a transaction ID variable, handling wraparound correctly */
     106             : #define TransactionIdRetreat(dest)  \
     107             :     do { \
     108             :         (dest)--; \
     109             :     } while ((dest) < FirstNormalTransactionId)
     110             : 
     111             : /* compare two XIDs already known to be normal; this is a macro for speed */
     112             : #define NormalTransactionIdPrecedes(id1, id2) \
     113             :     (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
     114             :     (int32) ((id1) - (id2)) < 0)
     115             : 
     116             : /* compare two XIDs already known to be normal; this is a macro for speed */
     117             : #define NormalTransactionIdFollows(id1, id2) \
     118             :     (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
     119             :     (int32) ((id1) - (id2)) > 0)
     120             : 
     121             : /* ----------
     122             :  *      Object ID (OID) zero is InvalidOid.
     123             :  *
     124             :  *      OIDs 1-9999 are reserved for manual assignment (see .dat files in
     125             :  *      src/include/catalog/).  Of these, 8000-9999 are reserved for
     126             :  *      development purposes (such as in-progress patches and forks);
     127             :  *      they should not appear in released versions.
     128             :  *
     129             :  *      OIDs 10000-11999 are reserved for assignment by genbki.pl, for use
     130             :  *      when the .dat files in src/include/catalog/ do not specify an OID
     131             :  *      for a catalog entry that requires one.
     132             :  *
     133             :  *      OIDS 12000-16383 are reserved for assignment during initdb
     134             :  *      using the OID generator.  (We start the generator at 12000.)
     135             :  *
     136             :  *      OIDs beginning at 16384 are assigned from the OID generator
     137             :  *      during normal multiuser operation.  (We force the generator up to
     138             :  *      16384 as soon as we are in normal operation.)
     139             :  *
     140             :  * The choices of 8000, 10000 and 12000 are completely arbitrary, and can be
     141             :  * moved if we run low on OIDs in any category.  Changing the macros below,
     142             :  * and updating relevant documentation (see bki.sgml and RELEASE_CHANGES),
     143             :  * should be sufficient to do this.  Moving the 16384 boundary between
     144             :  * initdb-assigned OIDs and user-defined objects would be substantially
     145             :  * more painful, however, since some user-defined OIDs will appear in
     146             :  * on-disk data; such a change would probably break pg_upgrade.
     147             :  *
     148             :  * NOTE: if the OID generator wraps around, we skip over OIDs 0-16383
     149             :  * and resume with 16384.  This minimizes the odds of OID conflict, by not
     150             :  * reassigning OIDs that might have been assigned during initdb.
     151             :  * ----------
     152             :  */
     153             : #define FirstGenbkiObjectId     10000
     154             : #define FirstBootstrapObjectId  12000
     155             : #define FirstNormalObjectId     16384
     156             : 
     157             : /*
     158             :  * VariableCache is a data structure in shared memory that is used to track
     159             :  * OID and XID assignment state.  For largely historical reasons, there is
     160             :  * just one struct with different fields that are protected by different
     161             :  * LWLocks.
     162             :  *
     163             :  * Note: xidWrapLimit and oldestXidDB are not "active" values, but are
     164             :  * used just to generate useful messages when xidWarnLimit or xidStopLimit
     165             :  * are exceeded.
     166             :  */
     167             : typedef struct VariableCacheData
     168             : {
     169             :     /*
     170             :      * These fields are protected by OidGenLock.
     171             :      */
     172             :     Oid         nextOid;        /* next OID to assign */
     173             :     uint32      oidCount;       /* OIDs available before must do XLOG work */
     174             : 
     175             :     /*
     176             :      * These fields are protected by XidGenLock.
     177             :      */
     178             :     FullTransactionId nextFullXid;  /* next full XID to assign */
     179             : 
     180             :     TransactionId oldestXid;    /* cluster-wide minimum datfrozenxid */
     181             :     TransactionId xidVacLimit;  /* start forcing autovacuums here */
     182             :     TransactionId xidWarnLimit; /* start complaining here */
     183             :     TransactionId xidStopLimit; /* refuse to advance nextFullXid beyond here */
     184             :     TransactionId xidWrapLimit; /* where the world ends */
     185             :     Oid         oldestXidDB;    /* database with minimum datfrozenxid */
     186             : 
     187             :     /*
     188             :      * These fields are protected by CommitTsLock
     189             :      */
     190             :     TransactionId oldestCommitTsXid;
     191             :     TransactionId newestCommitTsXid;
     192             : 
     193             :     /*
     194             :      * These fields are protected by ProcArrayLock.
     195             :      */
     196             :     TransactionId latestCompletedXid;   /* newest XID that has committed or
     197             :                                          * aborted */
     198             : 
     199             :     /*
     200             :      * These fields are protected by XactTruncationLock
     201             :      */
     202             :     TransactionId oldestClogXid;    /* oldest it's safe to look up in clog */
     203             : 
     204             : } VariableCacheData;
     205             : 
     206             : typedef VariableCacheData *VariableCache;
     207             : 
     208             : 
     209             : /* ----------------
     210             :  *      extern declarations
     211             :  * ----------------
     212             :  */
     213             : 
     214             : /* in transam/xact.c */
     215             : extern bool TransactionStartedDuringRecovery(void);
     216             : 
     217             : /* in transam/varsup.c */
     218             : extern PGDLLIMPORT VariableCache ShmemVariableCache;
     219             : 
     220             : /*
     221             :  * prototypes for functions in transam/transam.c
     222             :  */
     223             : extern bool TransactionIdDidCommit(TransactionId transactionId);
     224             : extern bool TransactionIdDidAbort(TransactionId transactionId);
     225             : extern bool TransactionIdIsKnownCompleted(TransactionId transactionId);
     226             : extern void TransactionIdCommitTree(TransactionId xid, int nxids, TransactionId *xids);
     227             : extern void TransactionIdAsyncCommitTree(TransactionId xid, int nxids, TransactionId *xids, XLogRecPtr lsn);
     228             : extern void TransactionIdAbortTree(TransactionId xid, int nxids, TransactionId *xids);
     229             : extern bool TransactionIdPrecedes(TransactionId id1, TransactionId id2);
     230             : extern bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2);
     231             : extern bool TransactionIdFollows(TransactionId id1, TransactionId id2);
     232             : extern bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2);
     233             : extern TransactionId TransactionIdLatest(TransactionId mainxid,
     234             :                                          int nxids, const TransactionId *xids);
     235             : extern XLogRecPtr TransactionIdGetCommitLSN(TransactionId xid);
     236             : 
     237             : /* in transam/varsup.c */
     238             : extern FullTransactionId GetNewTransactionId(bool isSubXact);
     239             : extern void AdvanceNextFullTransactionIdPastXid(TransactionId xid);
     240             : extern FullTransactionId ReadNextFullTransactionId(void);
     241             : extern void SetTransactionIdLimit(TransactionId oldest_datfrozenxid,
     242             :                                   Oid oldest_datoid);
     243             : extern void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid);
     244             : extern bool ForceTransactionIdLimitUpdate(void);
     245             : extern Oid  GetNewObjectId(void);
     246             : 
     247             : /*
     248             :  * Some frontend programs include this header.  For compilers that emit static
     249             :  * inline functions even when they're unused, that leads to unsatisfied
     250             :  * external references; hence hide them with #ifndef FRONTEND.
     251             :  */
     252             : #ifndef FRONTEND
     253             : 
     254             : /*
     255             :  * For callers that just need the XID part of the next transaction ID.
     256             :  */
     257             : static inline TransactionId
     258      102160 : ReadNewTransactionId(void)
     259             : {
     260      102160 :     return XidFromFullTransactionId(ReadNextFullTransactionId());
     261             : }
     262             : 
     263             : #endif                          /* FRONTEND */
     264             : 
     265             : #endif                          /* TRANSAM_H */

Generated by: LCOV version 1.13