1 /*-------------------------------------------------------------------------
4 * postgres transaction system definitions
7 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/access/xact.h,v 1.103 2010/02/26 02:01:21 momjian Exp $
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
87 #define XLOG_XACT_ASSIGNMENT 0x50
89 typedef struct xl_xact_assignment
91 TransactionId xtop; /* assigned XID's top-level XID */
92 int nsubxacts; /* number of subtransaction XIDs */
93 TransactionId xsub[1]; /* assigned subxids */
96 #define MinSizeOfXactAssignment offsetof(xl_xact_assignment, xsub)
98 typedef struct xl_xact_commit
100 TimestampTz xact_time; /* time of commit */
101 uint32 xinfo; /* info flags */
102 int nrels; /* number of RelFileNodes */
103 int nsubxacts; /* number of subtransaction XIDs */
104 int nmsgs; /* number of shared inval msgs */
105 Oid dbId; /* MyDatabaseId */
106 Oid tsId; /* MyDatabaseTableSpace */
107 /* Array of RelFileNode(s) to drop at commit */
108 RelFileNode xnodes[1]; /* VARIABLE LENGTH ARRAY */
109 /* ARRAY OF COMMITTED SUBTRANSACTION XIDs FOLLOWS */
110 /* ARRAY OF SHARED INVALIDATION MESSAGES FOLLOWS */
113 #define MinSizeOfXactCommit offsetof(xl_xact_commit, xnodes)
116 * These flags are set in the xinfo fields of WAL commit records,
117 * indicating a variety of additional actions that need to occur
118 * when emulating transaction effects during recovery.
119 * They are named XactCompletion... to differentiate them from
120 * EOXact... routines which run at the end of the original
121 * transaction completion.
123 #define XACT_COMPLETION_UPDATE_RELCACHE_FILE 0x01
124 #define XACT_COMPLETION_FORCE_SYNC_COMMIT 0x02
126 /* Access macros for above flags */
127 #define XactCompletionRelcacheInitFileInval(xlrec) ((xlrec)->xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE)
128 #define XactCompletionForceSyncCommit(xlrec) ((xlrec)->xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT)
130 typedef struct xl_xact_abort
132 TimestampTz xact_time; /* time of abort */
133 int nrels; /* number of RelFileNodes */
134 int nsubxacts; /* number of subtransaction XIDs */
135 /* Array of RelFileNode(s) to drop at abort */
136 RelFileNode xnodes[1]; /* VARIABLE LENGTH ARRAY */
137 /* ARRAY OF ABORTED SUBTRANSACTION XIDs FOLLOWS */
140 /* Note the intentional lack of an invalidation message array c.f. commit */
142 #define MinSizeOfXactAbort offsetof(xl_xact_abort, xnodes)
145 * COMMIT_PREPARED and ABORT_PREPARED are identical to COMMIT/ABORT records
146 * except that we have to store the XID of the prepared transaction explicitly
147 * --- the XID in the record header will be for the transaction doing the
148 * COMMIT PREPARED or ABORT PREPARED command.
151 typedef struct xl_xact_commit_prepared
153 TransactionId xid; /* XID of prepared xact */
154 xl_xact_commit crec; /* COMMIT record */
155 /* MORE DATA FOLLOWS AT END OF STRUCT */
156 } xl_xact_commit_prepared;
158 #define MinSizeOfXactCommitPrepared offsetof(xl_xact_commit_prepared, crec.xnodes)
160 typedef struct xl_xact_abort_prepared
162 TransactionId xid; /* XID of prepared xact */
163 xl_xact_abort arec; /* ABORT record */
164 /* MORE DATA FOLLOWS AT END OF STRUCT */
165 } xl_xact_abort_prepared;
167 #define MinSizeOfXactAbortPrepared offsetof(xl_xact_abort_prepared, arec.xnodes)
174 extern bool IsTransactionState(void);
175 extern bool IsAbortedTransactionBlockState(void);
176 extern TransactionId GetTopTransactionId(void);
177 extern TransactionId GetTopTransactionIdIfAny(void);
178 extern TransactionId GetCurrentTransactionId(void);
179 extern TransactionId GetCurrentTransactionIdIfAny(void);
180 extern SubTransactionId GetCurrentSubTransactionId(void);
181 extern CommandId GetCurrentCommandId(bool used);
182 extern TimestampTz GetCurrentTransactionStartTimestamp(void);
183 extern TimestampTz GetCurrentStatementStartTimestamp(void);
184 extern TimestampTz GetCurrentTransactionStopTimestamp(void);
185 extern void SetCurrentStatementStartTimestamp(void);
186 extern int GetCurrentTransactionNestLevel(void);
187 extern bool TransactionIdIsCurrentTransactionId(TransactionId xid);
188 extern void CommandCounterIncrement(void);
189 extern void ForceSyncCommit(void);
190 extern void StartTransactionCommand(void);
191 extern void CommitTransactionCommand(void);
192 extern void AbortCurrentTransaction(void);
193 extern void BeginTransactionBlock(void);
194 extern bool EndTransactionBlock(void);
195 extern bool PrepareTransactionBlock(char *gid);
196 extern void UserAbortTransactionBlock(void);
197 extern void ReleaseSavepoint(List *options);
198 extern void DefineSavepoint(char *name);
199 extern void RollbackToSavepoint(List *options);
200 extern void BeginInternalSubTransaction(char *name);
201 extern void ReleaseCurrentSubTransaction(void);
202 extern void RollbackAndReleaseCurrentSubTransaction(void);
203 extern bool IsSubTransaction(void);
204 extern bool IsTransactionBlock(void);
205 extern bool IsTransactionOrTransactionBlock(void);
206 extern char TransactionBlockStatusCode(void);
207 extern void AbortOutOfAnyTransaction(void);
208 extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
209 extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
210 extern bool IsInTransactionChain(bool isTopLevel);
211 extern void RegisterXactCallback(XactCallback callback, void *arg);
212 extern void UnregisterXactCallback(XactCallback callback, void *arg);
213 extern void RegisterSubXactCallback(SubXactCallback callback, void *arg);
214 extern void UnregisterSubXactCallback(SubXactCallback callback, void *arg);
216 extern int xactGetCommittedChildren(TransactionId **ptr);
218 extern void xact_redo(XLogRecPtr lsn, XLogRecord *record);
219 extern void xact_desc(StringInfo buf, uint8 xl_info, char *rec);