PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xact.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * xact.h
4  * postgres transaction system definitions
5  *
6  *
7  * Portions Copyright (c) 1996-2017, 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 
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 
25 /*
26  * Xact isolation levels
27  */
28 #define XACT_READ_UNCOMMITTED 0
29 #define XACT_READ_COMMITTED 1
30 #define XACT_REPEATABLE_READ 2
31 #define XACT_SERIALIZABLE 3
32 
33 extern int DefaultXactIsoLevel;
34 extern PGDLLIMPORT int XactIsoLevel;
35 
36 /*
37  * We implement three isolation levels internally.
38  * The two stronger ones use one snapshot per database transaction;
39  * the others use one snapshot per statement.
40  * Serializable uses predicate locks in addition to snapshots.
41  * These macros should be used to check which isolation level is selected.
42  */
43 #define IsolationUsesXactSnapshot() (XactIsoLevel >= XACT_REPEATABLE_READ)
44 #define IsolationIsSerializable() (XactIsoLevel == XACT_SERIALIZABLE)
45 
46 /* Xact read-only state */
47 extern bool DefaultXactReadOnly;
48 extern bool XactReadOnly;
49 
50 /*
51  * Xact is deferrable -- only meaningful (currently) for read only
52  * SERIALIZABLE transactions
53  */
54 extern bool DefaultXactDeferrable;
55 extern bool XactDeferrable;
56 
57 typedef enum
58 {
59  SYNCHRONOUS_COMMIT_OFF, /* asynchronous commit */
60  SYNCHRONOUS_COMMIT_LOCAL_FLUSH, /* wait for local flush only */
61  SYNCHRONOUS_COMMIT_REMOTE_WRITE, /* wait for local flush and remote
62  * write */
63  SYNCHRONOUS_COMMIT_REMOTE_FLUSH, /* wait for local and remote flush */
64  SYNCHRONOUS_COMMIT_REMOTE_APPLY /* wait for local flush and remote
65  * apply */
67 
68 /* Define the default setting for synchronous_commit */
69 #define SYNCHRONOUS_COMMIT_ON SYNCHRONOUS_COMMIT_REMOTE_FLUSH
70 
71 /* Synchronous commit level */
72 extern int synchronous_commit;
73 
74 /* Kluge for 2PC support */
75 extern bool MyXactAccessedTempRel;
76 
77 /*
78  * start- and end-of-transaction callbacks for dynamically loaded modules
79  */
80 typedef enum
81 {
90 } XactEvent;
91 
92 typedef void (*XactCallback) (XactEvent event, void *arg);
93 
94 typedef enum
95 {
100 } SubXactEvent;
101 
102 typedef void (*SubXactCallback) (SubXactEvent event, SubTransactionId mySubid,
103  SubTransactionId parentSubid, void *arg);
104 
105 
106 /* ----------------
107  * transaction-related XLOG entries
108  * ----------------
109  */
110 
111 /*
112  * XLOG allows to store some information in high 4 bits of log record xl_info
113  * field. We use 3 for the opcode, and one about an optional flag variable.
114  */
115 #define XLOG_XACT_COMMIT 0x00
116 #define XLOG_XACT_PREPARE 0x10
117 #define XLOG_XACT_ABORT 0x20
118 #define XLOG_XACT_COMMIT_PREPARED 0x30
119 #define XLOG_XACT_ABORT_PREPARED 0x40
120 #define XLOG_XACT_ASSIGNMENT 0x50
121 /* free opcode 0x60 */
122 /* free opcode 0x70 */
123 
124 /* mask for filtering opcodes out of xl_info */
125 #define XLOG_XACT_OPMASK 0x70
126 
127 /* does this record have a 'xinfo' field or not */
128 #define XLOG_XACT_HAS_INFO 0x80
129 
130 /*
131  * The following flags, stored in xinfo, determine which information is
132  * contained in commit/abort records.
133  */
134 #define XACT_XINFO_HAS_DBINFO (1U << 0)
135 #define XACT_XINFO_HAS_SUBXACTS (1U << 1)
136 #define XACT_XINFO_HAS_RELFILENODES (1U << 2)
137 #define XACT_XINFO_HAS_INVALS (1U << 3)
138 #define XACT_XINFO_HAS_TWOPHASE (1U << 4)
139 #define XACT_XINFO_HAS_ORIGIN (1U << 5)
140 
141 /*
142  * Also stored in xinfo, these indicating a variety of additional actions that
143  * need to occur when emulating transaction effects during recovery.
144  *
145  * They are named XactCompletion... to differentiate them from
146  * EOXact... routines which run at the end of the original transaction
147  * completion.
148  */
149 #define XACT_COMPLETION_APPLY_FEEDBACK (1U << 29)
150 #define XACT_COMPLETION_UPDATE_RELCACHE_FILE (1U << 30)
151 #define XACT_COMPLETION_FORCE_SYNC_COMMIT (1U << 31)
152 
153 /* Access macros for above flags */
154 #define XactCompletionApplyFeedback(xinfo) \
155  ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)
156 #define XactCompletionRelcacheInitFileInval(xinfo) \
157  ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)
158 #define XactCompletionForceSyncCommit(xinfo) \
159  ((xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT) != 0)
160 
161 typedef struct xl_xact_assignment
162 {
163  TransactionId xtop; /* assigned XID's top-level XID */
164  int nsubxacts; /* number of subtransaction XIDs */
165  TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]; /* assigned subxids */
167 
168 #define MinSizeOfXactAssignment offsetof(xl_xact_assignment, xsub)
169 
170 /*
171  * Commit and abort records can contain a lot of information. But a large
172  * portion of the records won't need all possible pieces of information. So we
173  * only include what's needed.
174  *
175  * A minimal commit/abort record only consists of a xl_xact_commit/abort
176  * struct. The presence of additional information is indicated by bits set in
177  * 'xl_xact_xinfo->xinfo'. The presence of the xinfo field itself is signalled
178  * by a set XLOG_XACT_HAS_INFO bit in the xl_info field.
179  *
180  * NB: All the individual data chunks should be sized to multiples of
181  * sizeof(int) and only require int32 alignment. If they require bigger
182  * alignment, they need to be copied upon reading.
183  */
184 
185 /* sub-records for commit/abort */
186 
187 typedef struct xl_xact_xinfo
188 {
189  /*
190  * Even though we right now only require 1 byte of space in xinfo we use
191  * four so following records don't have to care about alignment. Commit
192  * records can be large, so copying large portions isn't attractive.
193  */
195 } xl_xact_xinfo;
196 
197 typedef struct xl_xact_dbinfo
198 {
199  Oid dbId; /* MyDatabaseId */
200  Oid tsId; /* MyDatabaseTableSpace */
202 
203 typedef struct xl_xact_subxacts
204 {
205  int nsubxacts; /* number of subtransaction XIDs */
206  TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER];
208 #define MinSizeOfXactSubxacts offsetof(xl_xact_subxacts, subxacts)
209 
210 typedef struct xl_xact_relfilenodes
211 {
212  int nrels; /* number of subtransaction XIDs */
213  RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER];
215 #define MinSizeOfXactRelfilenodes offsetof(xl_xact_relfilenodes, xnodes)
216 
217 typedef struct xl_xact_invals
218 {
219  int nmsgs; /* number of shared inval msgs */
220  SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER];
222 #define MinSizeOfXactInvals offsetof(xl_xact_invals, msgs)
223 
224 typedef struct xl_xact_twophase
225 {
228 
229 typedef struct xl_xact_origin
230 {
234 
235 typedef struct xl_xact_commit
236 {
237  TimestampTz xact_time; /* time of commit */
238 
239  /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
240  /* xl_xact_dbinfo follows if XINFO_HAS_DBINFO */
241  /* xl_xact_subxacts follows if XINFO_HAS_SUBXACT */
242  /* xl_xact_relfilenodes follows if XINFO_HAS_RELFILENODES */
243  /* xl_xact_invals follows if XINFO_HAS_INVALS */
244  /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
245  /* xl_xact_origin follows if XINFO_HAS_ORIGIN, stored unaligned! */
247 #define MinSizeOfXactCommit (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz))
248 
249 typedef struct xl_xact_abort
250 {
251  TimestampTz xact_time; /* time of abort */
252 
253  /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
254  /* No db_info required */
255  /* xl_xact_subxacts follows if HAS_SUBXACT */
256  /* xl_xact_relfilenodes follows if HAS_RELFILENODES */
257  /* No invalidation messages needed. */
258  /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
259 } xl_xact_abort;
260 #define MinSizeOfXactAbort sizeof(xl_xact_abort)
261 
262 /*
263  * Commit/Abort records in the above form are a bit verbose to parse, so
264  * there's a deconstructed versions generated by ParseCommit/AbortRecord() for
265  * easier consumption.
266  */
267 typedef struct xl_xact_parsed_commit
268 {
270 
272 
273  Oid dbId; /* MyDatabaseId */
274  Oid tsId; /* MyDatabaseTableSpace */
275 
278 
279  int nrels;
281 
282  int nmsgs;
284 
285  TransactionId twophase_xid; /* only for 2PC */
286 
290 
291 typedef struct xl_xact_parsed_abort
292 {
295 
298 
299  int nrels;
301 
302  TransactionId twophase_xid; /* only for 2PC */
304 
305 
306 /* ----------------
307  * extern definitions
308  * ----------------
309  */
310 extern bool IsTransactionState(void);
311 extern bool IsAbortedTransactionBlockState(void);
318 extern void MarkCurrentTransactionIdLoggedIfAny(void);
319 extern bool SubTransactionIsActive(SubTransactionId subxid);
320 extern CommandId GetCurrentCommandId(bool used);
324 extern void SetCurrentStatementStartTimestamp(void);
325 extern int GetCurrentTransactionNestLevel(void);
327 extern void CommandCounterIncrement(void);
328 extern void ForceSyncCommit(void);
329 extern void StartTransactionCommand(void);
330 extern void CommitTransactionCommand(void);
331 extern void AbortCurrentTransaction(void);
332 extern void BeginTransactionBlock(void);
333 extern bool EndTransactionBlock(void);
334 extern bool PrepareTransactionBlock(char *gid);
335 extern void UserAbortTransactionBlock(void);
336 extern void ReleaseSavepoint(List *options);
337 extern void DefineSavepoint(char *name);
338 extern void RollbackToSavepoint(List *options);
339 extern void BeginInternalSubTransaction(char *name);
340 extern void ReleaseCurrentSubTransaction(void);
342 extern bool IsSubTransaction(void);
344 extern void SerializeTransactionState(Size maxsize, char *start_address);
345 extern void StartParallelWorkerTransaction(char *tstatespace);
346 extern void EndParallelWorkerTransaction(void);
347 extern bool IsTransactionBlock(void);
348 extern bool IsTransactionOrTransactionBlock(void);
349 extern char TransactionBlockStatusCode(void);
350 extern void AbortOutOfAnyTransaction(void);
351 extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
352 extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
353 extern void WarnNoTransactionChain(bool isTopLevel, const char *stmtType);
354 extern bool IsInTransactionChain(bool isTopLevel);
355 extern void RegisterXactCallback(XactCallback callback, void *arg);
356 extern void UnregisterXactCallback(XactCallback callback, void *arg);
359 
360 extern int xactGetCommittedChildren(TransactionId **ptr);
361 
362 extern XLogRecPtr XactLogCommitRecord(TimestampTz commit_time,
363  int nsubxacts, TransactionId *subxacts,
364  int nrels, RelFileNode *rels,
365  int nmsgs, SharedInvalidationMessage *msgs,
366  bool relcacheInval, bool forceSync,
367  TransactionId twophase_xid);
368 
369 extern XLogRecPtr XactLogAbortRecord(TimestampTz abort_time,
370  int nsubxacts, TransactionId *subxacts,
371  int nrels, RelFileNode *rels,
372  TransactionId twophase_xid);
373 extern void xact_redo(XLogReaderState *record);
374 
375 /* xactdesc.c */
376 extern void xact_desc(StringInfo buf, XLogReaderState *record);
377 extern const char *xact_identify(uint8 info);
378 
379 /* also in xactdesc.c, so they can be shared between front/backend code */
380 extern void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed);
381 extern void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed);
382 
383 extern void EnterParallelMode(void);
384 extern void ExitParallelMode(void);
385 extern bool IsInParallelMode(void);
386 
387 #endif /* XACT_H */
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4320
void EnterParallelMode(void)
Definition: xact.c:879
RelFileNode * xnodes
Definition: xact.h:280
void UnregisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3371
struct xl_xact_parsed_commit xl_xact_parsed_commit
uint32 CommandId
Definition: c.h:407
void UserAbortTransactionBlock(void)
Definition: xact.c:3647
XactEvent
Definition: xact.h:80
const char * xact_identify(uint8 info)
Definition: xactdesc.c:303
struct xl_xact_parsed_abort xl_xact_parsed_abort
void AbortOutOfAnyTransaction(void)
Definition: xact.c:4220
struct xl_xact_xinfo xl_xact_xinfo
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:760
uint32 TransactionId
Definition: c.h:393
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:403
TimestampTz origin_timestamp
Definition: xact.h:288
bool IsTransactionBlock(void)
Definition: xact.c:4302
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:728
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition: xactdesc.c:35
void StartTransactionCommand(void)
Definition: xact.c:2675
TransactionId xid
Definition: xact.h:226
TransactionId twophase_xid
Definition: xact.h:285
PGDLLIMPORT int XactIsoLevel
Definition: xact.c:74
unsigned char uint8
Definition: c.h:263
TransactionId * subxacts
Definition: xact.h:277
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3152
SharedInvalidationMessage * msgs
Definition: xact.h:283
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:416
TransactionId xtop
Definition: xact.h:163
XLogRecPtr XactLogCommitRecord(TimestampTz commit_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInval, bool forceSync, TransactionId twophase_xid)
Definition: xact.c:5106
XLogRecPtr origin_lsn
Definition: xact.h:287
TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:165
uint32 SubTransactionId
Definition: c.h:397
struct xl_xact_dbinfo xl_xact_dbinfo
void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
Definition: xactdesc.c:122
bool IsInTransactionChain(bool isTopLevel)
Definition: xact.c:3268
unsigned int Oid
Definition: postgres_ext.h:31
bool XactReadOnly
Definition: xact.c:77
bool XactDeferrable
Definition: xact.c:80
TimestampTz xact_time
Definition: xact.h:251
Oid tsId
Definition: xact.h:200
struct xl_xact_origin xl_xact_origin
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:4915
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:648
#define PGDLLIMPORT
Definition: c.h:1062
int synchronous_commit
Definition: xact.c:82
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:213
void(* SubXactCallback)(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: xact.h:102
void BeginTransactionBlock(void)
Definition: xact.c:3413
void WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3200
double TimestampTz
Definition: timestamp.h:51
void DefineSavepoint(char *name)
Definition: xact.c:3746
void CommandCounterIncrement(void)
Definition: xact.c:921
void MarkCurrentTransactionIdLoggedIfAny(void)
Definition: xact.c:444
bool IsSubTransaction(void)
Definition: xact.c:4374
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:369
bool SubTransactionIsActive(SubTransactionId subxid)
Definition: xact.c:662
void SetCurrentStatementStartTimestamp(void)
Definition: xact.c:739
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:49
void AbortCurrentTransaction(void)
Definition: xact.c:2984
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:220
void RequireTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3206
bool DefaultXactReadOnly
Definition: xact.c:76
static char * buf
Definition: pg_test_fsync.c:65
TimestampTz xact_time
Definition: xact.h:237
struct xl_xact_commit xl_xact_commit
Size EstimateTransactionStateSpace(void)
Definition: xact.c:4814
void RegisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3303
struct xl_xact_twophase xl_xact_twophase
unsigned int uint32
Definition: c.h:265
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
void ReleaseSavepoint(List *options)
Definition: xact.c:3809
void RollbackToSavepoint(List *options)
Definition: xact.c:3922
XLogRecPtr origin_lsn
Definition: xact.h:231
TransactionId * subxacts
Definition: xact.h:297
struct xl_xact_invals xl_xact_invals
bool DefaultXactDeferrable
Definition: xact.c:79
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
struct xl_xact_abort xl_xact_abort
void UnregisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3316
SubXactEvent
Definition: xact.h:94
void xact_desc(StringInfo buf, XLogReaderState *record)
Definition: xactdesc.c:270
int xactGetCommittedChildren(TransactionId **ptr)
Definition: xact.c:5082
void ForceSyncCommit(void)
Definition: xact.c:969
uint32 xinfo
Definition: xact.h:194
RelFileNode * xnodes
Definition: xact.h:300
int nsubxacts
Definition: xact.h:205
int DefaultXactIsoLevel
Definition: xact.c:73
void ExitParallelMode(void)
Definition: xact.c:892
SyncCommitLevel
Definition: xact.h:57
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:686
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:4847
Oid dbId
Definition: xact.h:199
TransactionId twophase_xid
Definition: xact.h:302
struct xl_xact_assignment xl_xact_assignment
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
XLogRecPtr XactLogAbortRecord(TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, TransactionId twophase_xid)
Definition: xact.c:5251
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:772
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:433
struct xl_xact_subxacts xl_xact_subxacts
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:716
size_t Size
Definition: c.h:352
void CommitTransactionCommand(void)
Definition: xact.c:2745
TimestampTz xact_time
Definition: xact.h:293
void(* XactCallback)(XactEvent event, void *arg)
Definition: xact.h:92
const char * name
Definition: encode.c:521
TimestampTz origin_timestamp
Definition: xact.h:232
bool PrepareTransactionBlock(char *gid)
Definition: xact.c:3472
struct xl_xact_relfilenodes xl_xact_relfilenodes
TransactionId GetTopTransactionId(void)
Definition: xact.c:388
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3358
bool EndTransactionBlock(void)
Definition: xact.c:3523
char TransactionBlockStatusCode(void)
Definition: xact.c:4334
void * arg
Definition: pg_list.h:45
void EndParallelWorkerTransaction(void)
Definition: xact.c:4938
int nmsgs
Definition: xact.h:219
bool IsInParallelMode(void)
Definition: xact.c:912
void xact_redo(XLogReaderState *record)
Definition: xact.c:5583
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:206
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:707
TimestampTz xact_time
Definition: xact.h:269
bool MyXactAccessedTempRel
Definition: xact.c:116
TransactionId GetStableLatestTransactionId(void)
Definition: xact.c:460
bool IsTransactionState(void)
Definition: xact.c:349