transam.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*-------------------------------------------------------------------------
  2. *
  3. * transam.h
  4. * postgres transaction access method support code
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, 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. #include "access/xlogdefs.h"
  17. /* ----------------
  18. * Special transaction ID values
  19. *
  20. * BootstrapTransactionId is the XID for "bootstrap" operations, and
  21. * FrozenTransactionId is used for very old tuples. Both should
  22. * always be considered valid.
  23. *
  24. * FirstNormalTransactionId is the first "normal" transaction id.
  25. * Note: if you need to change it, you must change pg_class.h as well.
  26. * ----------------
  27. */
  28. #define InvalidTransactionId ((TransactionId) 0)
  29. #define BootstrapTransactionId ((TransactionId) 1)
  30. #define FrozenTransactionId ((TransactionId) 2)
  31. #define FirstNormalTransactionId ((TransactionId) 3)
  32. #define MaxTransactionId ((TransactionId) 0xFFFFFFFF)
  33. /* ----------------
  34. * transaction ID manipulation macros
  35. * ----------------
  36. */
  37. #define TransactionIdIsValid(xid) ((xid) != InvalidTransactionId)
  38. #define TransactionIdIsNormal(xid) ((xid) >= FirstNormalTransactionId)
  39. #define TransactionIdEquals(id1, id2) ((id1) == (id2))
  40. #define TransactionIdStore(xid, dest) (*(dest) = (xid))
  41. #define StoreInvalidTransactionId(dest) (*(dest) = InvalidTransactionId)
  42. /* advance a transaction ID variable, handling wraparound correctly */
  43. #define TransactionIdAdvance(dest) \
  44. do { \
  45. (dest)++; \
  46. if ((dest) < FirstNormalTransactionId) \
  47. (dest) = FirstNormalTransactionId; \
  48. } while(0)
  49. /* back up a transaction ID variable, handling wraparound correctly */
  50. #define TransactionIdRetreat(dest) \
  51. do { \
  52. (dest)--; \
  53. } while ((dest) < FirstNormalTransactionId)
  54. /* compare two XIDs already known to be normal; this is a macro for speed */
  55. #define NormalTransactionIdPrecedes(id1, id2) \
  56. (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
  57. (int32) ((id1) - (id2)) < 0)
  58. /* compare two XIDs already known to be normal; this is a macro for speed */
  59. #define NormalTransactionIdFollows(id1, id2) \
  60. (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
  61. (int32) ((id1) - (id2)) > 0)
  62. /* ----------
  63. * Object ID (OID) zero is InvalidOid.
  64. *
  65. * OIDs 1-9999 are reserved for manual assignment (see the files
  66. * in src/include/catalog/).
  67. *
  68. * OIDS 10000-16383 are reserved for assignment during initdb
  69. * using the OID generator. (We start the generator at 10000.)
  70. *
  71. * OIDs beginning at 16384 are assigned from the OID generator
  72. * during normal multiuser operation. (We force the generator up to
  73. * 16384 as soon as we are in normal operation.)
  74. *
  75. * The choices of 10000 and 16384 are completely arbitrary, and can be moved
  76. * if we run low on OIDs in either category. Changing the macros below
  77. * should be sufficient to do this.
  78. *
  79. * NOTE: if the OID generator wraps around, we skip over OIDs 0-16383
  80. * and resume with 16384. This minimizes the odds of OID conflict, by not
  81. * reassigning OIDs that might have been assigned during initdb.
  82. * ----------
  83. */
  84. #define FirstBootstrapObjectId 10000
  85. #define FirstNormalObjectId 16384
  86. /*
  87. * VariableCache is a data structure in shared memory that is used to track
  88. * OID and XID assignment state. For largely historical reasons, there is
  89. * just one struct with different fields that are protected by different
  90. * LWLocks.
  91. *
  92. * Note: xidWrapLimit and oldestXidDB are not "active" values, but are
  93. * used just to generate useful messages when xidWarnLimit or xidStopLimit
  94. * are exceeded.
  95. */
  96. typedef struct VariableCacheData
  97. {
  98. /*
  99. * These fields are protected by OidGenLock.
  100. */
  101. Oid nextOid; /* next OID to assign */
  102. uint32 oidCount; /* OIDs available before must do XLOG work */
  103. /*
  104. * These fields are protected by XidGenLock.
  105. */
  106. TransactionId nextXid; /* next XID to assign */
  107. TransactionId oldestXid; /* cluster-wide minimum datfrozenxid */
  108. TransactionId xidVacLimit; /* start forcing autovacuums here */
  109. TransactionId xidWarnLimit; /* start complaining here */
  110. TransactionId xidStopLimit; /* refuse to advance nextXid beyond here */
  111. TransactionId xidWrapLimit; /* where the world ends */
  112. Oid oldestXidDB; /* database with minimum datfrozenxid */
  113. /*
  114. * These fields are protected by CommitTsLock
  115. */
  116. TransactionId oldestCommitTsXid;
  117. TransactionId newestCommitTsXid;
  118. /*
  119. * These fields are protected by ProcArrayLock.
  120. */
  121. TransactionId latestCompletedXid; /* newest XID that has committed or
  122. * aborted */
  123. } VariableCacheData;
  124. typedef VariableCacheData *VariableCache;
  125. /* ----------------
  126. * extern declarations
  127. * ----------------
  128. */
  129. /* in transam/xact.c */
  130. extern bool TransactionStartedDuringRecovery(void);
  131. /* in transam/varsup.c */
  132. extern PGDLLIMPORT VariableCache ShmemVariableCache;
  133. /*
  134. * prototypes for functions in transam/transam.c
  135. */
  136. extern bool TransactionIdDidCommit(TransactionId transactionId);
  137. extern bool TransactionIdDidAbort(TransactionId transactionId);
  138. extern bool TransactionIdIsKnownCompleted(TransactionId transactionId);
  139. extern void TransactionIdAbort(TransactionId transactionId);
  140. extern void TransactionIdCommitTree(TransactionId xid, int nxids, TransactionId *xids);
  141. extern void TransactionIdAsyncCommitTree(TransactionId xid, int nxids, TransactionId *xids, XLogRecPtr lsn);
  142. extern void TransactionIdAbortTree(TransactionId xid, int nxids, TransactionId *xids);
  143. extern bool TransactionIdPrecedes(TransactionId id1, TransactionId id2);
  144. extern bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2);
  145. extern bool TransactionIdFollows(TransactionId id1, TransactionId id2);
  146. extern bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2);
  147. extern TransactionId TransactionIdLatest(TransactionId mainxid,
  148. int nxids, const TransactionId *xids);
  149. extern XLogRecPtr TransactionIdGetCommitLSN(TransactionId xid);
  150. /* in transam/varsup.c */
  151. extern TransactionId GetNewTransactionId(bool isSubXact);
  152. extern TransactionId ReadNewTransactionId(void);
  153. extern void SetTransactionIdLimit(TransactionId oldest_datfrozenxid,
  154. Oid oldest_datoid);
  155. extern bool ForceTransactionIdLimitUpdate(void);
  156. extern Oid GetNewObjectId(void);
  157. #endif /* TRAMSAM_H */