1 /*-------------------------------------------------------------------------
4 * postgres transaction system definitions
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 *-------------------------------------------------------------------------
17 #include "access/xlog.h"
18 #include "nodes/pg_list.h"
19 #include "storage/relfilenode.h"
20 #include "utils/timestamp.h"
24 * Xact isolation levels
26 #define XACT_READ_UNCOMMITTED 0
27 #define XACT_READ_COMMITTED 1
28 #define XACT_REPEATABLE_READ 2
29 #define XACT_SERIALIZABLE 3
31 extern int DefaultXactIsoLevel
;
32 extern int XactIsoLevel
;
35 * We only implement two isolation levels internally. This macro should
36 * be used to check which one is selected.
38 #define IsXactIsoLevelSerializable (XactIsoLevel >= XACT_REPEATABLE_READ)
40 /* Xact read-only state */
41 extern bool DefaultXactReadOnly
;
42 extern bool XactReadOnly
;
44 /* Asynchronous commits */
45 extern bool XactSyncCommit
;
47 /* Kluge for 2PC support */
48 extern bool MyXactAccessedTempRel
;
51 * start- and end-of-transaction callbacks for dynamically loaded modules
60 typedef void (*XactCallback
) (XactEvent event
, void *arg
);
64 SUBXACT_EVENT_START_SUB
,
65 SUBXACT_EVENT_COMMIT_SUB
,
66 SUBXACT_EVENT_ABORT_SUB
69 typedef void (*SubXactCallback
) (SubXactEvent event
, SubTransactionId mySubid
,
70 SubTransactionId parentSubid
, void *arg
);
74 * transaction-related XLOG entries
79 * XLOG allows to store some information in high 4 bits of log
80 * record xl_info field
82 #define XLOG_XACT_COMMIT 0x00
83 #define XLOG_XACT_PREPARE 0x10
84 #define XLOG_XACT_ABORT 0x20
85 #define XLOG_XACT_COMMIT_PREPARED 0x30
86 #define XLOG_XACT_ABORT_PREPARED 0x40
88 typedef struct xl_xact_commit
90 TimestampTz xact_time
; /* time of commit */
91 int nrels
; /* number of RelFileForks */
92 int nsubxacts
; /* number of subtransaction XIDs */
93 /* Array of RelFileFork(s) to drop at commit */
94 RelFileFork xnodes
[1]; /* VARIABLE LENGTH ARRAY */
95 /* ARRAY OF COMMITTED SUBTRANSACTION XIDs FOLLOWS */
98 #define MinSizeOfXactCommit offsetof(xl_xact_commit, xnodes)
100 typedef struct xl_xact_abort
102 TimestampTz xact_time
; /* time of abort */
103 int nrels
; /* number of RelFileForks */
104 int nsubxacts
; /* number of subtransaction XIDs */
105 /* Array of RelFileFork(s) to drop at abort */
106 RelFileFork xnodes
[1]; /* VARIABLE LENGTH ARRAY */
107 /* ARRAY OF ABORTED SUBTRANSACTION XIDs FOLLOWS */
110 #define MinSizeOfXactAbort offsetof(xl_xact_abort, xnodes)
113 * COMMIT_PREPARED and ABORT_PREPARED are identical to COMMIT/ABORT records
114 * except that we have to store the XID of the prepared transaction explicitly
115 * --- the XID in the record header will be for the transaction doing the
116 * COMMIT PREPARED or ABORT PREPARED command.
119 typedef struct xl_xact_commit_prepared
121 TransactionId xid
; /* XID of prepared xact */
122 xl_xact_commit crec
; /* COMMIT record */
123 /* MORE DATA FOLLOWS AT END OF STRUCT */
124 } xl_xact_commit_prepared
;
126 #define MinSizeOfXactCommitPrepared offsetof(xl_xact_commit_prepared, crec.xnodes)
128 typedef struct xl_xact_abort_prepared
130 TransactionId xid
; /* XID of prepared xact */
131 xl_xact_abort arec
; /* ABORT record */
132 /* MORE DATA FOLLOWS AT END OF STRUCT */
133 } xl_xact_abort_prepared
;
135 #define MinSizeOfXactAbortPrepared offsetof(xl_xact_abort_prepared, arec.xnodes)
142 extern bool IsTransactionState(void);
143 extern bool IsAbortedTransactionBlockState(void);
144 extern TransactionId
GetTopTransactionId(void);
145 extern TransactionId
GetTopTransactionIdIfAny(void);
146 extern TransactionId
GetCurrentTransactionId(void);
147 extern TransactionId
GetCurrentTransactionIdIfAny(void);
148 extern SubTransactionId
GetCurrentSubTransactionId(void);
149 extern CommandId
GetCurrentCommandId(bool used
);
150 extern TimestampTz
GetCurrentTransactionStartTimestamp(void);
151 extern TimestampTz
GetCurrentStatementStartTimestamp(void);
152 extern TimestampTz
GetCurrentTransactionStopTimestamp(void);
153 extern void SetCurrentStatementStartTimestamp(void);
154 extern int GetCurrentTransactionNestLevel(void);
155 extern bool TransactionIdIsCurrentTransactionId(TransactionId xid
);
156 extern void CommandCounterIncrement(void);
157 extern void ForceSyncCommit(void);
158 extern void StartTransactionCommand(void);
159 extern void CommitTransactionCommand(void);
160 extern void AbortCurrentTransaction(void);
161 extern void BeginTransactionBlock(void);
162 extern bool EndTransactionBlock(void);
163 extern bool PrepareTransactionBlock(char *gid
);
164 extern void UserAbortTransactionBlock(void);
165 extern void ReleaseSavepoint(List
*options
);
166 extern void DefineSavepoint(char *name
);
167 extern void RollbackToSavepoint(List
*options
);
168 extern void BeginInternalSubTransaction(char *name
);
169 extern void ReleaseCurrentSubTransaction(void);
170 extern void RollbackAndReleaseCurrentSubTransaction(void);
171 extern bool IsSubTransaction(void);
172 extern bool IsTransactionBlock(void);
173 extern bool IsTransactionOrTransactionBlock(void);
174 extern char TransactionBlockStatusCode(void);
175 extern void AbortOutOfAnyTransaction(void);
176 extern void PreventTransactionChain(bool isTopLevel
, const char *stmtType
);
177 extern void RequireTransactionChain(bool isTopLevel
, const char *stmtType
);
178 extern bool IsInTransactionChain(bool isTopLevel
);
179 extern void RegisterXactCallback(XactCallback callback
, void *arg
);
180 extern void UnregisterXactCallback(XactCallback callback
, void *arg
);
181 extern void RegisterSubXactCallback(SubXactCallback callback
, void *arg
);
182 extern void UnregisterSubXactCallback(SubXactCallback callback
, void *arg
);
184 extern TransactionId
RecordTransactionCommit(void);
186 extern int xactGetCommittedChildren(TransactionId
**ptr
);
188 extern void xact_redo(XLogRecPtr lsn
, XLogRecord
*record
);
189 extern void xact_desc(StringInfo buf
, uint8 xl_info
, char *rec
);