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.

444 lines
15KB

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