xact.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*-------------------------------------------------------------------------
  2. *
  3. * xact.h
  4. * postgres transaction system definitions
  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/xact.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef XACT_H
  15. #define XACT_H
  16. #include "access/xlogreader.h"
  17. #include "lib/stringinfo.h"
  18. #include "nodes/pg_list.h"
  19. #include "storage/relfilenode.h"
  20. #include "storage/sinval.h"
  21. #include "utils/datetime.h"
  22. /*
  23. * Xact isolation levels
  24. */
  25. #define XACT_READ_UNCOMMITTED 0
  26. #define XACT_READ_COMMITTED 1
  27. #define XACT_REPEATABLE_READ 2
  28. #define XACT_SERIALIZABLE 3
  29. extern int DefaultXactIsoLevel;
  30. extern PGDLLIMPORT int XactIsoLevel;
  31. /*
  32. * We implement three isolation levels internally.
  33. * The two stronger ones use one snapshot per database transaction;
  34. * the others use one snapshot per statement.
  35. * Serializable uses predicate locks in addition to snapshots.
  36. * These macros should be used to check which isolation level is selected.
  37. */
  38. #define IsolationUsesXactSnapshot() (XactIsoLevel >= XACT_REPEATABLE_READ)
  39. #define IsolationIsSerializable() (XactIsoLevel == XACT_SERIALIZABLE)
  40. /* Xact read-only state */
  41. extern bool DefaultXactReadOnly;
  42. extern bool XactReadOnly;
  43. /*
  44. * Xact is deferrable -- only meaningful (currently) for read only
  45. * SERIALIZABLE transactions
  46. */
  47. extern bool DefaultXactDeferrable;
  48. extern bool XactDeferrable;
  49. typedef enum
  50. {
  51. SYNCHRONOUS_COMMIT_OFF, /* asynchronous commit */
  52. SYNCHRONOUS_COMMIT_LOCAL_FLUSH, /* wait for local flush only */
  53. SYNCHRONOUS_COMMIT_REMOTE_WRITE, /* wait for local flush and remote
  54. * write */
  55. SYNCHRONOUS_COMMIT_REMOTE_FLUSH, /* wait for local and remote flush */
  56. SYNCHRONOUS_COMMIT_REMOTE_APPLY /* wait for local flush and remote
  57. * apply */
  58. } SyncCommitLevel;
  59. /* Define the default setting for synchronous_commit */
  60. #define SYNCHRONOUS_COMMIT_ON SYNCHRONOUS_COMMIT_REMOTE_FLUSH
  61. /* Synchronous commit level */
  62. extern int synchronous_commit;
  63. /* Kluge for 2PC support */
  64. extern bool MyXactAccessedTempRel;
  65. /*
  66. * start- and end-of-transaction callbacks for dynamically loaded modules
  67. */
  68. typedef enum
  69. {
  70. XACT_EVENT_COMMIT,
  71. XACT_EVENT_PARALLEL_COMMIT,
  72. XACT_EVENT_ABORT,
  73. XACT_EVENT_PARALLEL_ABORT,
  74. XACT_EVENT_PREPARE,
  75. XACT_EVENT_PRE_COMMIT,
  76. XACT_EVENT_PARALLEL_PRE_COMMIT,
  77. XACT_EVENT_PRE_PREPARE
  78. } XactEvent;
  79. typedef void (*XactCallback) (XactEvent event, void *arg);
  80. typedef enum
  81. {
  82. SUBXACT_EVENT_START_SUB,
  83. SUBXACT_EVENT_COMMIT_SUB,
  84. SUBXACT_EVENT_ABORT_SUB,
  85. SUBXACT_EVENT_PRE_COMMIT_SUB
  86. } SubXactEvent;
  87. typedef void (*SubXactCallback) (SubXactEvent event, SubTransactionId mySubid,
  88. SubTransactionId parentSubid, void *arg);
  89. /* ----------------
  90. * transaction-related XLOG entries
  91. * ----------------
  92. */
  93. /*
  94. * XLOG allows to store some information in high 4 bits of log record xl_info
  95. * field. We use 3 for the opcode, and one about an optional flag variable.
  96. */
  97. #define XLOG_XACT_COMMIT 0x00
  98. #define XLOG_XACT_PREPARE 0x10
  99. #define XLOG_XACT_ABORT 0x20
  100. #define XLOG_XACT_COMMIT_PREPARED 0x30
  101. #define XLOG_XACT_ABORT_PREPARED 0x40
  102. #define XLOG_XACT_ASSIGNMENT 0x50
  103. /* free opcode 0x60 */
  104. /* free opcode 0x70 */
  105. /* mask for filtering opcodes out of xl_info */
  106. #define XLOG_XACT_OPMASK 0x70
  107. /* does this record have a 'xinfo' field or not */
  108. #define XLOG_XACT_HAS_INFO 0x80
  109. /*
  110. * The following flags, stored in xinfo, determine which information is
  111. * contained in commit/abort records.
  112. */
  113. #define XACT_XINFO_HAS_DBINFO (1U << 0)
  114. #define XACT_XINFO_HAS_SUBXACTS (1U << 1)
  115. #define XACT_XINFO_HAS_RELFILENODES (1U << 2)
  116. #define XACT_XINFO_HAS_INVALS (1U << 3)
  117. #define XACT_XINFO_HAS_TWOPHASE (1U << 4)
  118. #define XACT_XINFO_HAS_ORIGIN (1U << 5)
  119. /*
  120. * Also stored in xinfo, these indicating a variety of additional actions that
  121. * need to occur when emulating transaction effects during recovery.
  122. *
  123. * They are named XactCompletion... to differentiate them from
  124. * EOXact... routines which run at the end of the original transaction
  125. * completion.
  126. */
  127. #define XACT_COMPLETION_APPLY_FEEDBACK (1U << 29)
  128. #define XACT_COMPLETION_UPDATE_RELCACHE_FILE (1U << 30)
  129. #define XACT_COMPLETION_FORCE_SYNC_COMMIT (1U << 31)
  130. /* Access macros for above flags */
  131. #define XactCompletionApplyFeedback(xinfo) \
  132. ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)
  133. #define XactCompletionRelcacheInitFileInval(xinfo) \
  134. ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)
  135. #define XactCompletionForceSyncCommit(xinfo) \
  136. ((xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT) != 0)
  137. typedef struct xl_xact_assignment
  138. {
  139. TransactionId xtop; /* assigned XID's top-level XID */
  140. int nsubxacts; /* number of subtransaction XIDs */
  141. TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]; /* assigned subxids */
  142. } xl_xact_assignment;
  143. #define MinSizeOfXactAssignment offsetof(xl_xact_assignment, xsub)
  144. /*
  145. * Commit and abort records can contain a lot of information. But a large
  146. * portion of the records won't need all possible pieces of information. So we
  147. * only include what's needed.
  148. *
  149. * A minimal commit/abort record only consists of a xl_xact_commit/abort
  150. * struct. The presence of additional information is indicated by bits set in
  151. * 'xl_xact_xinfo->xinfo'. The presence of the xinfo field itself is signalled
  152. * by a set XLOG_XACT_HAS_INFO bit in the xl_info field.
  153. *
  154. * NB: All the individual data chunks should be sized to multiples of
  155. * sizeof(int) and only require int32 alignment. If they require bigger
  156. * alignment, they need to be copied upon reading.
  157. */
  158. /* sub-records for commit/abort */
  159. typedef struct xl_xact_xinfo
  160. {
  161. /*
  162. * Even though we right now only require 1 byte of space in xinfo we use
  163. * four so following records don't have to care about alignment. Commit
  164. * records can be large, so copying large portions isn't attractive.
  165. */
  166. uint32 xinfo;
  167. } xl_xact_xinfo;
  168. typedef struct xl_xact_dbinfo
  169. {
  170. Oid dbId; /* MyDatabaseId */
  171. Oid tsId; /* MyDatabaseTableSpace */
  172. } xl_xact_dbinfo;
  173. typedef struct xl_xact_subxacts
  174. {
  175. int nsubxacts; /* number of subtransaction XIDs */
  176. TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER];
  177. } xl_xact_subxacts;
  178. #define MinSizeOfXactSubxacts offsetof(xl_xact_subxacts, subxacts)
  179. typedef struct xl_xact_relfilenodes
  180. {
  181. int nrels; /* number of subtransaction XIDs */
  182. RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER];
  183. } xl_xact_relfilenodes;
  184. #define MinSizeOfXactRelfilenodes offsetof(xl_xact_relfilenodes, xnodes)
  185. typedef struct xl_xact_invals
  186. {
  187. int nmsgs; /* number of shared inval msgs */
  188. SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER];
  189. } xl_xact_invals;
  190. #define MinSizeOfXactInvals offsetof(xl_xact_invals, msgs)
  191. typedef struct xl_xact_twophase
  192. {
  193. TransactionId xid;
  194. } xl_xact_twophase;
  195. typedef struct xl_xact_origin
  196. {
  197. XLogRecPtr origin_lsn;
  198. TimestampTz origin_timestamp;
  199. } xl_xact_origin;
  200. typedef struct xl_xact_commit
  201. {
  202. TimestampTz xact_time; /* time of commit */
  203. /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
  204. /* xl_xact_dbinfo follows if XINFO_HAS_DBINFO */
  205. /* xl_xact_subxacts follows if XINFO_HAS_SUBXACT */
  206. /* xl_xact_relfilenodes follows if XINFO_HAS_RELFILENODES */
  207. /* xl_xact_invals follows if XINFO_HAS_INVALS */
  208. /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
  209. /* xl_xact_origin follows if XINFO_HAS_ORIGIN, stored unaligned! */
  210. } xl_xact_commit;
  211. #define MinSizeOfXactCommit (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz))
  212. typedef struct xl_xact_abort
  213. {
  214. TimestampTz xact_time; /* time of abort */
  215. /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
  216. /* No db_info required */
  217. /* xl_xact_subxacts follows if HAS_SUBXACT */
  218. /* xl_xact_relfilenodes follows if HAS_RELFILENODES */
  219. /* No invalidation messages needed. */
  220. /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
  221. } xl_xact_abort;
  222. #define MinSizeOfXactAbort sizeof(xl_xact_abort)
  223. /*
  224. * Commit/Abort records in the above form are a bit verbose to parse, so
  225. * there's a deconstructed versions generated by ParseCommit/AbortRecord() for
  226. * easier consumption.
  227. */
  228. typedef struct xl_xact_parsed_commit
  229. {
  230. TimestampTz xact_time;
  231. uint32 xinfo;
  232. Oid dbId; /* MyDatabaseId */
  233. Oid tsId; /* MyDatabaseTableSpace */
  234. int nsubxacts;
  235. TransactionId *subxacts;
  236. int nrels;
  237. RelFileNode *xnodes;
  238. int nmsgs;
  239. SharedInvalidationMessage *msgs;
  240. TransactionId twophase_xid; /* only for 2PC */
  241. XLogRecPtr origin_lsn;
  242. TimestampTz origin_timestamp;
  243. } xl_xact_parsed_commit;
  244. typedef struct xl_xact_parsed_abort
  245. {
  246. TimestampTz xact_time;
  247. uint32 xinfo;
  248. int nsubxacts;
  249. TransactionId *subxacts;
  250. int nrels;
  251. RelFileNode *xnodes;
  252. TransactionId twophase_xid; /* only for 2PC */
  253. } xl_xact_parsed_abort;
  254. /* ----------------
  255. * extern definitions
  256. * ----------------
  257. */
  258. extern bool IsTransactionState(void);
  259. extern bool IsAbortedTransactionBlockState(void);
  260. extern TransactionId GetTopTransactionId(void);
  261. extern TransactionId GetTopTransactionIdIfAny(void);
  262. extern TransactionId GetCurrentTransactionId(void);
  263. extern TransactionId GetCurrentTransactionIdIfAny(void);
  264. extern TransactionId GetStableLatestTransactionId(void);
  265. extern SubTransactionId GetCurrentSubTransactionId(void);
  266. extern void MarkCurrentTransactionIdLoggedIfAny(void);
  267. extern bool SubTransactionIsActive(SubTransactionId subxid);
  268. extern CommandId GetCurrentCommandId(bool used);
  269. extern TimestampTz GetCurrentTransactionStartTimestamp(void);
  270. extern TimestampTz GetCurrentStatementStartTimestamp(void);
  271. extern TimestampTz GetCurrentTransactionStopTimestamp(void);
  272. extern void SetCurrentStatementStartTimestamp(void);
  273. extern int GetCurrentTransactionNestLevel(void);
  274. extern bool TransactionIdIsCurrentTransactionId(TransactionId xid);
  275. extern void CommandCounterIncrement(void);
  276. extern void ForceSyncCommit(void);
  277. extern void StartTransactionCommand(void);
  278. extern void CommitTransactionCommand(void);
  279. extern void AbortCurrentTransaction(void);
  280. extern void BeginTransactionBlock(void);
  281. extern bool EndTransactionBlock(void);
  282. extern bool PrepareTransactionBlock(char *gid);
  283. extern void UserAbortTransactionBlock(void);
  284. extern void ReleaseSavepoint(List *options);
  285. extern void DefineSavepoint(char *name);
  286. extern void RollbackToSavepoint(List *options);
  287. extern void BeginInternalSubTransaction(char *name);
  288. extern void ReleaseCurrentSubTransaction(void);
  289. extern void RollbackAndReleaseCurrentSubTransaction(void);
  290. extern bool IsSubTransaction(void);
  291. extern Size EstimateTransactionStateSpace(void);
  292. extern void SerializeTransactionState(Size maxsize, char *start_address);
  293. extern void StartParallelWorkerTransaction(char *tstatespace);
  294. extern void EndParallelWorkerTransaction(void);
  295. extern bool IsTransactionBlock(void);
  296. extern bool IsTransactionOrTransactionBlock(void);
  297. extern char TransactionBlockStatusCode(void);
  298. extern void AbortOutOfAnyTransaction(void);
  299. extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
  300. extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
  301. extern void WarnNoTransactionChain(bool isTopLevel, const char *stmtType);
  302. extern bool IsInTransactionChain(bool isTopLevel);
  303. extern void RegisterXactCallback(XactCallback callback, void *arg);
  304. extern void UnregisterXactCallback(XactCallback callback, void *arg);
  305. extern void RegisterSubXactCallback(SubXactCallback callback, void *arg);
  306. extern void UnregisterSubXactCallback(SubXactCallback callback, void *arg);
  307. extern int xactGetCommittedChildren(TransactionId **ptr);
  308. extern XLogRecPtr XactLogCommitRecord(TimestampTz commit_time,
  309. int nsubxacts, TransactionId *subxacts,
  310. int nrels, RelFileNode *rels,
  311. int nmsgs, SharedInvalidationMessage *msgs,
  312. bool relcacheInval, bool forceSync,
  313. TransactionId twophase_xid);
  314. extern XLogRecPtr XactLogAbortRecord(TimestampTz abort_time,
  315. int nsubxacts, TransactionId *subxacts,
  316. int nrels, RelFileNode *rels,
  317. TransactionId twophase_xid);
  318. extern void xact_redo(XLogReaderState *record);
  319. /* xactdesc.c */
  320. extern void xact_desc(StringInfo buf, XLogReaderState *record);
  321. extern const char *xact_identify(uint8 info);
  322. /* also in xactdesc.c, so they can be shared between front/backend code */
  323. extern void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed);
  324. extern void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed);
  325. extern void EnterParallelMode(void);
  326. extern void ExitParallelMode(void);
  327. extern bool IsInParallelMode(void);
  328. #endif /* XACT_H */