gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 line
8.8KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * transam.h
  4. * postgres transaction access method support code
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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. #define EpochFromFullTransactionId(x) ((uint32) ((x).value >> 32))
  43. #define XidFromFullTransactionId(x) ((uint32) (x).value)
  44. #define U64FromFullTransactionId(x) ((x).value)
  45. #define FullTransactionIdPrecedes(a, b) ((a).value < (b).value)
  46. #define FullTransactionIdIsValid(x) TransactionIdIsValid(XidFromFullTransactionId(x))
  47. #define InvalidFullTransactionId FullTransactionIdFromEpochAndXid(0, InvalidTransactionId)
  48. /*
  49. * A 64 bit value that contains an epoch and a TransactionId. This is
  50. * wrapped in a struct to prevent implicit conversion to/from TransactionId.
  51. * Not all values represent valid normal XIDs.
  52. */
  53. typedef struct FullTransactionId
  54. {
  55. uint64 value;
  56. } FullTransactionId;
  57. static inline FullTransactionId
  58. FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
  59. {
  60. FullTransactionId result;
  61. result.value = ((uint64) epoch) << 32 | xid;
  62. return result;
  63. }
  64. /* advance a transaction ID variable, handling wraparound correctly */
  65. #define TransactionIdAdvance(dest) \
  66. do { \
  67. (dest)++; \
  68. if ((dest) < FirstNormalTransactionId) \
  69. (dest) = FirstNormalTransactionId; \
  70. } while(0)
  71. /* advance a FullTransactionId variable, stepping over special XIDs */
  72. static inline void
  73. FullTransactionIdAdvance(FullTransactionId *dest)
  74. {
  75. dest->value++;
  76. while (XidFromFullTransactionId(*dest) < FirstNormalTransactionId)
  77. dest->value++;
  78. }
  79. /* back up a transaction ID variable, handling wraparound correctly */
  80. #define TransactionIdRetreat(dest) \
  81. do { \
  82. (dest)--; \
  83. } while ((dest) < FirstNormalTransactionId)
  84. /* compare two XIDs already known to be normal; this is a macro for speed */
  85. #define NormalTransactionIdPrecedes(id1, id2) \
  86. (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
  87. (int32) ((id1) - (id2)) < 0)
  88. /* compare two XIDs already known to be normal; this is a macro for speed */
  89. #define NormalTransactionIdFollows(id1, id2) \
  90. (AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
  91. (int32) ((id1) - (id2)) > 0)
  92. /* ----------
  93. * Object ID (OID) zero is InvalidOid.
  94. *
  95. * OIDs 1-9999 are reserved for manual assignment (see .dat files in
  96. * src/include/catalog/). Of these, 8000-9999 are reserved for
  97. * development purposes (such as in-progress patches and forks);
  98. * they should not appear in released versions.
  99. *
  100. * OIDs 10000-11999 are reserved for assignment by genbki.pl, for use
  101. * when the .dat files in src/include/catalog/ do not specify an OID
  102. * for a catalog entry that requires one.
  103. *
  104. * OIDS 12000-16383 are reserved for assignment during initdb
  105. * using the OID generator. (We start the generator at 12000.)
  106. *
  107. * OIDs beginning at 16384 are assigned from the OID generator
  108. * during normal multiuser operation. (We force the generator up to
  109. * 16384 as soon as we are in normal operation.)
  110. *
  111. * The choices of 8000, 10000 and 12000 are completely arbitrary, and can be
  112. * moved if we run low on OIDs in any category. Changing the macros below,
  113. * and updating relevant documentation (see bki.sgml and RELEASE_CHANGES),
  114. * should be sufficient to do this. Moving the 16384 boundary between
  115. * initdb-assigned OIDs and user-defined objects would be substantially
  116. * more painful, however, since some user-defined OIDs will appear in
  117. * on-disk data; such a change would probably break pg_upgrade.
  118. *
  119. * NOTE: if the OID generator wraps around, we skip over OIDs 0-16383
  120. * and resume with 16384. This minimizes the odds of OID conflict, by not
  121. * reassigning OIDs that might have been assigned during initdb.
  122. * ----------
  123. */
  124. #define FirstGenbkiObjectId 10000
  125. #define FirstBootstrapObjectId 12000
  126. #define FirstNormalObjectId 16384
  127. /*
  128. * VariableCache is a data structure in shared memory that is used to track
  129. * OID and XID assignment state. For largely historical reasons, there is
  130. * just one struct with different fields that are protected by different
  131. * LWLocks.
  132. *
  133. * Note: xidWrapLimit and oldestXidDB are not "active" values, but are
  134. * used just to generate useful messages when xidWarnLimit or xidStopLimit
  135. * are exceeded.
  136. */
  137. typedef struct VariableCacheData
  138. {
  139. /*
  140. * These fields are protected by OidGenLock.
  141. */
  142. Oid nextOid; /* next OID to assign */
  143. uint32 oidCount; /* OIDs available before must do XLOG work */
  144. /*
  145. * These fields are protected by XidGenLock.
  146. */
  147. FullTransactionId nextFullXid; /* next full XID to assign */
  148. TransactionId oldestXid; /* cluster-wide minimum datfrozenxid */
  149. TransactionId xidVacLimit; /* start forcing autovacuums here */
  150. TransactionId xidWarnLimit; /* start complaining here */
  151. TransactionId xidStopLimit; /* refuse to advance nextFullXid beyond here */
  152. TransactionId xidWrapLimit; /* where the world ends */
  153. Oid oldestXidDB; /* database with minimum datfrozenxid */
  154. /*
  155. * These fields are protected by CommitTsLock
  156. */
  157. TransactionId oldestCommitTsXid;
  158. TransactionId newestCommitTsXid;
  159. /*
  160. * These fields are protected by ProcArrayLock.
  161. */
  162. TransactionId latestCompletedXid; /* newest XID that has committed or
  163. * aborted */
  164. /*
  165. * These fields are protected by CLogTruncationLock
  166. */
  167. TransactionId oldestClogXid; /* oldest it's safe to look up in clog */
  168. } VariableCacheData;
  169. typedef VariableCacheData *VariableCache;
  170. /* ----------------
  171. * extern declarations
  172. * ----------------
  173. */
  174. /* in transam/xact.c */
  175. extern bool TransactionStartedDuringRecovery(void);
  176. /* in transam/varsup.c */
  177. extern PGDLLIMPORT VariableCache ShmemVariableCache;
  178. /*
  179. * prototypes for functions in transam/transam.c
  180. */
  181. extern bool TransactionIdDidCommit(TransactionId transactionId);
  182. extern bool TransactionIdDidAbort(TransactionId transactionId);
  183. extern bool TransactionIdIsKnownCompleted(TransactionId transactionId);
  184. extern void TransactionIdAbort(TransactionId transactionId);
  185. extern void TransactionIdCommitTree(TransactionId xid, int nxids, TransactionId *xids);
  186. extern void TransactionIdAsyncCommitTree(TransactionId xid, int nxids, TransactionId *xids, XLogRecPtr lsn);
  187. extern void TransactionIdAbortTree(TransactionId xid, int nxids, TransactionId *xids);
  188. extern bool TransactionIdPrecedes(TransactionId id1, TransactionId id2);
  189. extern bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2);
  190. extern bool TransactionIdFollows(TransactionId id1, TransactionId id2);
  191. extern bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2);
  192. extern TransactionId TransactionIdLatest(TransactionId mainxid,
  193. int nxids, const TransactionId *xids);
  194. extern XLogRecPtr TransactionIdGetCommitLSN(TransactionId xid);
  195. /* in transam/varsup.c */
  196. extern FullTransactionId GetNewTransactionId(bool isSubXact);
  197. extern void AdvanceNextFullTransactionIdPastXid(TransactionId xid);
  198. extern FullTransactionId ReadNextFullTransactionId(void);
  199. extern void SetTransactionIdLimit(TransactionId oldest_datfrozenxid,
  200. Oid oldest_datoid);
  201. extern void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid);
  202. extern bool ForceTransactionIdLimitUpdate(void);
  203. extern Oid GetNewObjectId(void);
  204. /*
  205. * Some frontend programs include this header. For compilers that emit static
  206. * inline functions even when they're unused, that leads to unsatisfied
  207. * external references; hence hide them with #ifndef FRONTEND.
  208. */
  209. #ifndef FRONTEND
  210. /*
  211. * For callers that just need the XID part of the next transaction ID.
  212. */
  213. static inline TransactionId
  214. ReadNewTransactionId(void)
  215. {
  216. return XidFromFullTransactionId(ReadNextFullTransactionId());
  217. }
  218. #endif /* FRONTEND */
  219. #endif /* TRANSAM_H */
上海开阖软件有限公司 沪ICP备12045867号-1