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