PostgreSQL Source Code  git master
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 apply */
66 
67 /* Define the default setting for synchronous_commit */
68 #define SYNCHRONOUS_COMMIT_ON SYNCHRONOUS_COMMIT_REMOTE_FLUSH
69 
70 /* Synchronous commit level */
71 extern int synchronous_commit;
72 
73 /*
74  * Miscellaneous flag bits to record events which occur on the top level
75  * transaction. These flags are only persisted in MyXactFlags and are intended
76  * so we remember to do certain things later in the transaction. This is
77  * globally accessible, so can be set from anywhere in the code which requires
78  * recording flags.
79  */
80 extern int MyXactFlags;
81 
82 /*
83  * XACT_FLAGS_ACCESSEDTEMPREL - set when a temporary relation is accessed. We
84  * don't allow PREPARE TRANSACTION in that case.
85  */
86 #define XACT_FLAGS_ACCESSEDTEMPREL (1U << 0)
87 
88 /*
89  * XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK - records whether the top level xact
90  * logged any Access Exclusive Locks.
91  */
92 #define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1)
93 
94 
95 /*
96  * start- and end-of-transaction callbacks for dynamically loaded modules
97  */
98 typedef enum
99 {
108 } XactEvent;
109 
110 typedef void (*XactCallback) (XactEvent event, void *arg);
111 
112 typedef enum
113 {
118 } SubXactEvent;
119 
120 typedef void (*SubXactCallback) (SubXactEvent event, SubTransactionId mySubid,
121  SubTransactionId parentSubid, void *arg);
122 
123 
124 /* ----------------
125  * transaction-related XLOG entries
126  * ----------------
127  */
128 
129 /*
130  * XLOG allows to store some information in high 4 bits of log record xl_info
131  * field. We use 3 for the opcode, and one about an optional flag variable.
132  */
133 #define XLOG_XACT_COMMIT 0x00
134 #define XLOG_XACT_PREPARE 0x10
135 #define XLOG_XACT_ABORT 0x20
136 #define XLOG_XACT_COMMIT_PREPARED 0x30
137 #define XLOG_XACT_ABORT_PREPARED 0x40
138 #define XLOG_XACT_ASSIGNMENT 0x50
139 /* free opcode 0x60 */
140 /* free opcode 0x70 */
141 
142 /* mask for filtering opcodes out of xl_info */
143 #define XLOG_XACT_OPMASK 0x70
144 
145 /* does this record have a 'xinfo' field or not */
146 #define XLOG_XACT_HAS_INFO 0x80
147 
148 /*
149  * The following flags, stored in xinfo, determine which information is
150  * contained in commit/abort records.
151  */
152 #define XACT_XINFO_HAS_DBINFO (1U << 0)
153 #define XACT_XINFO_HAS_SUBXACTS (1U << 1)
154 #define XACT_XINFO_HAS_RELFILENODES (1U << 2)
155 #define XACT_XINFO_HAS_INVALS (1U << 3)
156 #define XACT_XINFO_HAS_TWOPHASE (1U << 4)
157 #define XACT_XINFO_HAS_ORIGIN (1U << 5)
158 #define XACT_XINFO_HAS_AE_LOCKS (1U << 6)
159 
160 /*
161  * Also stored in xinfo, these indicating a variety of additional actions that
162  * need to occur when emulating transaction effects during recovery.
163  *
164  * They are named XactCompletion... to differentiate them from
165  * EOXact... routines which run at the end of the original transaction
166  * completion.
167  */
168 #define XACT_COMPLETION_APPLY_FEEDBACK (1U << 29)
169 #define XACT_COMPLETION_UPDATE_RELCACHE_FILE (1U << 30)
170 #define XACT_COMPLETION_FORCE_SYNC_COMMIT (1U << 31)
171 
172 /* Access macros for above flags */
173 #define XactCompletionApplyFeedback(xinfo) \
174  ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)
175 #define XactCompletionRelcacheInitFileInval(xinfo) \
176  ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)
177 #define XactCompletionForceSyncCommit(xinfo) \
178  ((xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT) != 0)
179 
180 typedef struct xl_xact_assignment
181 {
182  TransactionId xtop; /* assigned XID's top-level XID */
183  int nsubxacts; /* number of subtransaction XIDs */
184  TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]; /* assigned subxids */
186 
187 #define MinSizeOfXactAssignment offsetof(xl_xact_assignment, xsub)
188 
189 /*
190  * Commit and abort records can contain a lot of information. But a large
191  * portion of the records won't need all possible pieces of information. So we
192  * only include what's needed.
193  *
194  * A minimal commit/abort record only consists of a xl_xact_commit/abort
195  * struct. The presence of additional information is indicated by bits set in
196  * 'xl_xact_xinfo->xinfo'. The presence of the xinfo field itself is signalled
197  * by a set XLOG_XACT_HAS_INFO bit in the xl_info field.
198  *
199  * NB: All the individual data chunks should be sized to multiples of
200  * sizeof(int) and only require int32 alignment. If they require bigger
201  * alignment, they need to be copied upon reading.
202  */
203 
204 /* sub-records for commit/abort */
205 
206 typedef struct xl_xact_xinfo
207 {
208  /*
209  * Even though we right now only require 1 byte of space in xinfo we use
210  * four so following records don't have to care about alignment. Commit
211  * records can be large, so copying large portions isn't attractive.
212  */
214 } xl_xact_xinfo;
215 
216 typedef struct xl_xact_dbinfo
217 {
218  Oid dbId; /* MyDatabaseId */
219  Oid tsId; /* MyDatabaseTableSpace */
221 
222 typedef struct xl_xact_subxacts
223 {
224  int nsubxacts; /* number of subtransaction XIDs */
225  TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER];
227 #define MinSizeOfXactSubxacts offsetof(xl_xact_subxacts, subxacts)
228 
229 typedef struct xl_xact_relfilenodes
230 {
231  int nrels; /* number of subtransaction XIDs */
232  RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER];
234 #define MinSizeOfXactRelfilenodes offsetof(xl_xact_relfilenodes, xnodes)
235 
236 typedef struct xl_xact_invals
237 {
238  int nmsgs; /* number of shared inval msgs */
239  SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER];
241 #define MinSizeOfXactInvals offsetof(xl_xact_invals, msgs)
242 
243 typedef struct xl_xact_twophase
244 {
247 
248 typedef struct xl_xact_origin
249 {
253 
254 typedef struct xl_xact_commit
255 {
256  TimestampTz xact_time; /* time of commit */
257 
258  /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
259  /* xl_xact_dbinfo follows if XINFO_HAS_DBINFO */
260  /* xl_xact_subxacts follows if XINFO_HAS_SUBXACT */
261  /* xl_xact_relfilenodes follows if XINFO_HAS_RELFILENODES */
262  /* xl_xact_invals follows if XINFO_HAS_INVALS */
263  /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
264  /* xl_xact_origin follows if XINFO_HAS_ORIGIN, stored unaligned! */
266 #define MinSizeOfXactCommit (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz))
267 
268 typedef struct xl_xact_abort
269 {
270  TimestampTz xact_time; /* time of abort */
271 
272  /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */
273  /* No db_info required */
274  /* xl_xact_subxacts follows if HAS_SUBXACT */
275  /* xl_xact_relfilenodes follows if HAS_RELFILENODES */
276  /* No invalidation messages needed. */
277  /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */
278 } xl_xact_abort;
279 #define MinSizeOfXactAbort sizeof(xl_xact_abort)
280 
281 /*
282  * Commit/Abort records in the above form are a bit verbose to parse, so
283  * there's a deconstructed versions generated by ParseCommit/AbortRecord() for
284  * easier consumption.
285  */
286 typedef struct xl_xact_parsed_commit
287 {
289 
291 
292  Oid dbId; /* MyDatabaseId */
293  Oid tsId; /* MyDatabaseTableSpace */
294 
297 
298  int nrels;
300 
301  int nmsgs;
303 
304  TransactionId twophase_xid; /* only for 2PC */
305 
309 
310 typedef struct xl_xact_parsed_abort
311 {
314 
317 
318  int nrels;
320 
321  TransactionId twophase_xid; /* only for 2PC */
323 
324 
325 /* ----------------
326  * extern definitions
327  * ----------------
328  */
329 extern bool IsTransactionState(void);
330 extern bool IsAbortedTransactionBlockState(void);
337 extern void MarkCurrentTransactionIdLoggedIfAny(void);
338 extern bool SubTransactionIsActive(SubTransactionId subxid);
339 extern CommandId GetCurrentCommandId(bool used);
343 extern void SetCurrentStatementStartTimestamp(void);
344 extern int GetCurrentTransactionNestLevel(void);
346 extern void CommandCounterIncrement(void);
347 extern void ForceSyncCommit(void);
348 extern void StartTransactionCommand(void);
349 extern void CommitTransactionCommand(void);
350 extern void AbortCurrentTransaction(void);
351 extern void BeginTransactionBlock(void);
352 extern bool EndTransactionBlock(void);
353 extern bool PrepareTransactionBlock(const char *gid);
354 extern void UserAbortTransactionBlock(void);
355 extern void BeginImplicitTransactionBlock(void);
356 extern void EndImplicitTransactionBlock(void);
357 extern void ReleaseSavepoint(List *options);
358 extern void DefineSavepoint(const char *name);
359 extern void RollbackToSavepoint(List *options);
360 extern void BeginInternalSubTransaction(const char *name);
361 extern void ReleaseCurrentSubTransaction(void);
363 extern bool IsSubTransaction(void);
365 extern void SerializeTransactionState(Size maxsize, char *start_address);
366 extern void StartParallelWorkerTransaction(char *tstatespace);
367 extern void EndParallelWorkerTransaction(void);
368 extern bool IsTransactionBlock(void);
369 extern bool IsTransactionOrTransactionBlock(void);
370 extern char TransactionBlockStatusCode(void);
371 extern void AbortOutOfAnyTransaction(void);
372 extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
373 extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
374 extern void WarnNoTransactionChain(bool isTopLevel, const char *stmtType);
375 extern bool IsInTransactionChain(bool isTopLevel);
376 extern void RegisterXactCallback(XactCallback callback, void *arg);
377 extern void UnregisterXactCallback(XactCallback callback, void *arg);
380 
381 extern int xactGetCommittedChildren(TransactionId **ptr);
382 
383 extern XLogRecPtr XactLogCommitRecord(TimestampTz commit_time,
384  int nsubxacts, TransactionId *subxacts,
385  int nrels, RelFileNode *rels,
386  int nmsgs, SharedInvalidationMessage *msgs,
387  bool relcacheInval, bool forceSync,
388  int xactflags,
389  TransactionId twophase_xid);
390 
391 extern XLogRecPtr XactLogAbortRecord(TimestampTz abort_time,
392  int nsubxacts, TransactionId *subxacts,
393  int nrels, RelFileNode *rels,
394  int xactflags, TransactionId twophase_xid);
395 extern void xact_redo(XLogReaderState *record);
396 
397 /* xactdesc.c */
398 extern void xact_desc(StringInfo buf, XLogReaderState *record);
399 extern const char *xact_identify(uint8 info);
400 
401 /* also in xactdesc.c, so they can be shared between front/backend code */
402 extern void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed);
403 extern void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed);
404 
405 extern void EnterParallelMode(void);
406 extern void ExitParallelMode(void);
407 extern bool IsInParallelMode(void);
408 
409 #endif /* XACT_H */
bool IsTransactionOrTransactionBlock(void)
Definition: xact.c:4466
void EnterParallelMode(void)
Definition: xact.c:873
RelFileNode * xnodes
Definition: xact.h:299
void UnregisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3372
struct xl_xact_parsed_commit xl_xact_parsed_commit
uint32 CommandId
Definition: c.h:469
void UserAbortTransactionBlock(void)
Definition: xact.c:3670
void EndImplicitTransactionBlock(void)
Definition: xact.c:3805
XactEvent
Definition: xact.h:98
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:4344
struct xl_xact_xinfo xl_xact_xinfo
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:754
uint32 TransactionId
Definition: c.h:455
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:405
TimestampTz origin_timestamp
Definition: xact.h:307
bool IsTransactionBlock(void)
Definition: xact.c:4448
TimestampTz GetCurrentTransactionStopTimestamp(void)
Definition: xact.c:722
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition: xactdesc.c:35
void StartTransactionCommand(void)
Definition: xact.c:2674
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4172
int64 TimestampTz
Definition: timestamp.h:39
void(* SubXactCallback)(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: xact.h:120
TransactionId xid
Definition: xact.h:245
TransactionId twophase_xid
Definition: xact.h:304
PGDLLIMPORT int XactIsoLevel
Definition: xact.c:74
unsigned char uint8
Definition: c.h:304
TransactionId * subxacts
Definition: xact.h:296
XLogRecPtr XactLogCommitRecord(TimestampTz commit_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInval, bool forceSync, int xactflags, TransactionId twophase_xid)
Definition: xact.c:5255
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3154
SharedInvalidationMessage * msgs
Definition: xact.h:302
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:418
TransactionId xtop
Definition: xact.h:182
XLogRecPtr origin_lsn
Definition: xact.h:306
TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:184
void BeginImplicitTransactionBlock(void)
Definition: xact.c:3780
uint32 SubTransactionId
Definition: c.h:459
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:3269
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:270
Oid tsId
Definition: xact.h:219
struct xl_xact_origin xl_xact_origin
void StartParallelWorkerTransaction(char *tstatespace)
Definition: xact.c:5062
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:642
#define PGDLLIMPORT
Definition: c.h:1060
int synchronous_commit
Definition: xact.c:82
void(* XactCallback)(XactEvent event, void *arg)
Definition: xact.h:110
int MyXactFlags
Definition: xact.c:118
void BeginTransactionBlock(void)
Definition: xact.c:3414
void WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3201
void CommandCounterIncrement(void)
Definition: xact.c:915
void MarkCurrentTransactionIdLoggedIfAny(void)
Definition: xact.c:446
bool IsSubTransaction(void)
Definition: xact.c:4521
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:371
bool SubTransactionIsActive(SubTransactionId subxid)
Definition: xact.c:656
void SetCurrentStatementStartTimestamp(void)
Definition: xact.c:733
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
void AbortCurrentTransaction(void)
Definition: xact.c:2985
void RequireTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3207
bool DefaultXactReadOnly
Definition: xact.c:76
static char * buf
Definition: pg_test_fsync.c:67
TimestampTz xact_time
Definition: xact.h:256
struct xl_xact_commit xl_xact_commit
Size EstimateTransactionStateSpace(void)
Definition: xact.c:4961
void RegisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3304
struct xl_xact_twophase xl_xact_twophase
unsigned int uint32
Definition: c.h:306
void ReleaseSavepoint(List *options)
Definition: xact.c:3912
void RollbackToSavepoint(List *options)
Definition: xact.c:4033
XLogRecPtr origin_lsn
Definition: xact.h:250
TransactionId * subxacts
Definition: xact.h:316
struct xl_xact_invals xl_xact_invals
bool DefaultXactDeferrable
Definition: xact.c:79
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4243
struct xl_xact_abort xl_xact_abort
void UnregisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3317
void DefineSavepoint(const char *name)
Definition: xact.c:3827
SubXactEvent
Definition: xact.h:112
void xact_desc(StringInfo buf, XLogReaderState *record)
Definition: xactdesc.c:270
int xactGetCommittedChildren(TransactionId **ptr)
Definition: xact.c:5231
void ForceSyncCommit(void)
Definition: xact.c:963
uint32 xinfo
Definition: xact.h:213
bool PrepareTransactionBlock(const char *gid)
Definition: xact.c:3482
RelFileNode * xnodes
Definition: xact.h:319
int nsubxacts
Definition: xact.h:224
int DefaultXactIsoLevel
Definition: xact.c:73
void ExitParallelMode(void)
Definition: xact.c:886
SyncCommitLevel
Definition: xact.h:57
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:680
void SerializeTransactionState(Size maxsize, char *start_address)
Definition: xact.c:4994
Oid dbId
Definition: xact.h:218
TransactionId twophase_xid
Definition: xact.h:321
struct xl_xact_assignment xl_xact_assignment
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4277
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:766
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:435
struct xl_xact_subxacts xl_xact_subxacts
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition: xact.c:710
size_t Size
Definition: c.h:414
void CommitTransactionCommand(void)
Definition: xact.c:2745
TimestampTz xact_time
Definition: xact.h:312
const char * name
Definition: encode.c:521
TimestampTz origin_timestamp
Definition: xact.h:251
struct xl_xact_relfilenodes xl_xact_relfilenodes
TransactionId GetTopTransactionId(void)
Definition: xact.c:390
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3359
bool EndTransactionBlock(void)
Definition: xact.c:3534
char TransactionBlockStatusCode(void)
Definition: xact.c:4480
void * arg
XLogRecPtr XactLogAbortRecord(TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int xactflags, TransactionId twophase_xid)
Definition: xact.c:5402
Definition: pg_list.h:45
void EndParallelWorkerTransaction(void)
Definition: xact.c:5085
int nmsgs
Definition: xact.h:238
bool IsInParallelMode(void)
Definition: xact.c:906
void xact_redo(XLogReaderState *record)
Definition: xact.c:5743
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:701
TimestampTz xact_time
Definition: xact.h:288
TransactionId GetStableLatestTransactionId(void)
Definition: xact.c:462
bool IsTransactionState(void)
Definition: xact.c:351