1 /*-------------------------------------------------------------------------
4 * top level transaction system support routines
6 * See src/backend/access/transam/README for more information.
8 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/access/transam/xact.c
15 *-------------------------------------------------------------------------
23 #include "access/multixact.h"
24 #include "access/subtrans.h"
25 #include "access/transam.h"
26 #include "access/twophase.h"
27 #include "access/xact.h"
28 #include "access/xlogutils.h"
29 #include "catalog/catalog.h"
30 #include "catalog/namespace.h"
31 #include "catalog/storage.h"
32 #include "commands/async.h"
33 #include "commands/tablecmds.h"
34 #include "commands/trigger.h"
35 #include "executor/spi.h"
36 #include "libpq/be-fsstubs.h"
37 #include "miscadmin.h"
39 #include "replication/walsender.h"
40 #include "replication/syncrep.h"
41 #include "storage/fd.h"
42 #include "storage/lmgr.h"
43 #include "storage/predicate.h"
44 #include "storage/proc.h"
45 #include "storage/procarray.h"
46 #include "storage/sinvaladt.h"
47 #include "storage/smgr.h"
48 #include "utils/catcache.h"
49 #include "utils/combocid.h"
50 #include "utils/guc.h"
51 #include "utils/inval.h"
52 #include "utils/memutils.h"
53 #include "utils/relmapper.h"
54 #include "utils/snapmgr.h"
55 #include "utils/timestamp.h"
60 * User-tweakable parameters
62 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
65 bool DefaultXactReadOnly = false;
68 bool DefaultXactDeferrable = false;
71 int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
74 * MyXactAccessedTempRel is set when a temporary relation is accessed.
75 * We don't allow PREPARE TRANSACTION in that case. (This is global
76 * so that it can be set from heapam.c.)
78 bool MyXactAccessedTempRel = false;
82 * transaction states - transaction state from server perspective
84 typedef enum TransState
86 TRANS_DEFAULT, /* idle */
87 TRANS_START, /* transaction starting */
88 TRANS_INPROGRESS, /* inside a valid transaction */
89 TRANS_COMMIT, /* commit in progress */
90 TRANS_ABORT, /* abort in progress */
91 TRANS_PREPARE /* prepare in progress */
95 * transaction block states - transaction state of client queries
97 * Note: the subtransaction states are used only for non-topmost
98 * transactions; the others appear only in the topmost transaction.
100 typedef enum TBlockState
102 /* not-in-transaction-block states */
103 TBLOCK_DEFAULT, /* idle */
104 TBLOCK_STARTED, /* running single-query transaction */
106 /* transaction block states */
107 TBLOCK_BEGIN, /* starting transaction block */
108 TBLOCK_INPROGRESS, /* live transaction */
109 TBLOCK_END, /* COMMIT received */
110 TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
111 TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
112 TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
113 TBLOCK_PREPARE, /* live xact, PREPARE received */
115 /* subtransaction states */
116 TBLOCK_SUBBEGIN, /* starting a subtransaction */
117 TBLOCK_SUBINPROGRESS, /* live subtransaction */
118 TBLOCK_SUBRELEASE, /* RELEASE received */
119 TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
120 TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
121 TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
122 TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
123 TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
124 TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
128 * transaction state structure
130 typedef struct TransactionStateData
132 TransactionId transactionId; /* my XID, or Invalid if none */
133 SubTransactionId subTransactionId; /* my subxact ID */
134 char *name; /* savepoint name, if any */
135 int savepointLevel; /* savepoint level */
136 TransState state; /* low-level state */
137 TBlockState blockState; /* high-level state */
138 int nestingLevel; /* transaction nesting depth */
139 int gucNestLevel; /* GUC context nesting depth */
140 MemoryContext curTransactionContext; /* my xact-lifetime context */
141 ResourceOwner curTransactionOwner; /* my query resources */
142 TransactionId *childXids; /* subcommitted child XIDs, in XID order */
143 int nChildXids; /* # of subcommitted child XIDs */
144 int maxChildXids; /* allocated size of childXids[] */
145 Oid prevUser; /* previous CurrentUserId setting */
146 int prevSecContext; /* previous SecurityRestrictionContext */
147 bool prevXactReadOnly; /* entry-time xact r/o state */
148 bool startedInRecovery; /* did we start in recovery? */
149 struct TransactionStateData *parent; /* back link to parent */
150 } TransactionStateData;
152 typedef TransactionStateData *TransactionState;
155 * CurrentTransactionState always points to the current transaction state
156 * block. It will point to TopTransactionStateData when not in a
157 * transaction at all, or when in a top-level transaction.
159 static TransactionStateData TopTransactionStateData = {
160 0, /* transaction id */
161 0, /* subtransaction id */
162 NULL, /* savepoint name */
163 0, /* savepoint level */
164 TRANS_DEFAULT, /* transaction state */
165 TBLOCK_DEFAULT, /* transaction block state from the client
167 0, /* transaction nesting depth */
168 0, /* GUC context nesting depth */
169 NULL, /* cur transaction context */
170 NULL, /* cur transaction resource owner */
171 NULL, /* subcommitted child Xids */
172 0, /* # of subcommitted child Xids */
173 0, /* allocated size of childXids[] */
174 InvalidOid, /* previous CurrentUserId setting */
175 0, /* previous SecurityRestrictionContext */
176 false, /* entry-time xact r/o state */
177 false, /* startedInRecovery */
178 NULL /* link to parent state block */
182 * unreportedXids holds XIDs of all subtransactions that have not yet been
183 * reported in a XLOG_XACT_ASSIGNMENT record.
185 static int nUnreportedXids;
186 static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS];
188 static TransactionState CurrentTransactionState = &TopTransactionStateData;
191 * The subtransaction ID and command ID assignment counters are global
192 * to a whole transaction, so we do not keep them in the state stack.
194 static SubTransactionId currentSubTransactionId;
195 static CommandId currentCommandId;
196 static bool currentCommandIdUsed;
199 * xactStartTimestamp is the value of transaction_timestamp().
200 * stmtStartTimestamp is the value of statement_timestamp().
201 * xactStopTimestamp is the time at which we log a commit or abort WAL record.
202 * These do not change as we enter and exit subtransactions, so we don't
203 * keep them inside the TransactionState stack.
205 static TimestampTz xactStartTimestamp;
206 static TimestampTz stmtStartTimestamp;
207 static TimestampTz xactStopTimestamp;
210 * GID to be used for preparing the current transaction. This is also
211 * global to a whole transaction, so we don't keep it in the state stack.
213 static char *prepareGID;
216 * Some commands want to force synchronous commit.
218 static bool forceSyncCommit = false;
221 * Private context for transaction-abort work --- we reserve space for this
222 * at startup to ensure that AbortTransaction and AbortSubTransaction can work
223 * when we've run out of memory.
225 static MemoryContext TransactionAbortContext = NULL;
228 * List of add-on start- and end-of-xact callbacks
230 typedef struct XactCallbackItem
232 struct XactCallbackItem *next;
233 XactCallback callback;
237 static XactCallbackItem *Xact_callbacks = NULL;
240 * List of add-on start- and end-of-subxact callbacks
242 typedef struct SubXactCallbackItem
244 struct SubXactCallbackItem *next;
245 SubXactCallback callback;
247 } SubXactCallbackItem;
249 static SubXactCallbackItem *SubXact_callbacks = NULL;
252 /* local function prototypes */
253 static void AssignTransactionId(TransactionState s);
254 static void AbortTransaction(void);
255 static void AtAbort_Memory(void);
256 static void AtCleanup_Memory(void);
257 static void AtAbort_ResourceOwner(void);
258 static void AtCCI_LocalCache(void);
259 static void AtCommit_Memory(void);
260 static void AtStart_Cache(void);
261 static void AtStart_Memory(void);
262 static void AtStart_ResourceOwner(void);
263 static void CallXactCallbacks(XactEvent event);
264 static void CallSubXactCallbacks(SubXactEvent event,
265 SubTransactionId mySubid,
266 SubTransactionId parentSubid);
267 static void CleanupTransaction(void);
268 static void CommitTransaction(void);
269 static TransactionId RecordTransactionAbort(bool isSubXact);
270 static void StartTransaction(void);
272 static void StartSubTransaction(void);
273 static void CommitSubTransaction(void);
274 static void AbortSubTransaction(void);
275 static void CleanupSubTransaction(void);
276 static void PushTransaction(void);
277 static void PopTransaction(void);
279 static void AtSubAbort_Memory(void);
280 static void AtSubCleanup_Memory(void);
281 static void AtSubAbort_ResourceOwner(void);
282 static void AtSubCommit_Memory(void);
283 static void AtSubStart_Memory(void);
284 static void AtSubStart_ResourceOwner(void);
286 static void ShowTransactionState(const char *str);
287 static void ShowTransactionStateRec(TransactionState state);
288 static const char *BlockStateAsString(TBlockState blockState);
289 static const char *TransStateAsString(TransState state);
292 /* ----------------------------------------------------------------
293 * transaction state accessors
294 * ----------------------------------------------------------------
300 * This returns true if we are inside a valid transaction; that is,
301 * it is safe to initiate database access, take heavyweight locks, etc.
304 IsTransactionState(void)
306 TransactionState s = CurrentTransactionState;
309 * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
310 * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
311 * TRANS_PREPARE since it might be too soon or too late within those
312 * transition states to do anything interesting. Hence, the only "valid"
313 * state is TRANS_INPROGRESS.
315 return (s->state == TRANS_INPROGRESS);
319 * IsAbortedTransactionBlockState
321 * This returns true if we are within an aborted transaction block.
324 IsAbortedTransactionBlockState(void)
326 TransactionState s = CurrentTransactionState;
328 if (s->blockState == TBLOCK_ABORT ||
329 s->blockState == TBLOCK_SUBABORT)
337 * GetTopTransactionId
339 * This will return the XID of the main transaction, assigning one if
340 * it's not yet set. Be careful to call this only inside a valid xact.
343 GetTopTransactionId(void)
345 if (!TransactionIdIsValid(TopTransactionStateData.transactionId))
346 AssignTransactionId(&TopTransactionStateData);
347 return TopTransactionStateData.transactionId;
351 * GetTopTransactionIdIfAny
353 * This will return the XID of the main transaction, if one is assigned.
354 * It will return InvalidTransactionId if we are not currently inside a
355 * transaction, or inside a transaction that hasn't yet been assigned an XID.
358 GetTopTransactionIdIfAny(void)
360 return TopTransactionStateData.transactionId;
364 * GetCurrentTransactionId
366 * This will return the XID of the current transaction (main or sub
367 * transaction), assigning one if it's not yet set. Be careful to call this
368 * only inside a valid xact.
371 GetCurrentTransactionId(void)
373 TransactionState s = CurrentTransactionState;
375 if (!TransactionIdIsValid(s->transactionId))
376 AssignTransactionId(s);
377 return s->transactionId;
381 * GetCurrentTransactionIdIfAny
383 * This will return the XID of the current sub xact, if one is assigned.
384 * It will return InvalidTransactionId if we are not currently inside a
385 * transaction, or inside a transaction that hasn't been assigned an XID yet.
388 GetCurrentTransactionIdIfAny(void)
390 return CurrentTransactionState->transactionId;
394 * GetStableLatestTransactionId
396 * Get the transaction's XID if it has one, else read the next-to-be-assigned
397 * XID. Once we have a value, return that same value for the remainder of the
398 * current transaction. This is meant to provide the reference point for the
399 * age(xid) function, but might be useful for other maintenance tasks as well.
402 GetStableLatestTransactionId(void)
404 static LocalTransactionId lxid = InvalidLocalTransactionId;
405 static TransactionId stablexid = InvalidTransactionId;
407 if (lxid != MyProc->lxid)
410 stablexid = GetTopTransactionIdIfAny();
411 if (!TransactionIdIsValid(stablexid))
412 stablexid = ReadNewTransactionId();
415 Assert(TransactionIdIsValid(stablexid));
421 * AssignTransactionId
423 * Assigns a new permanent XID to the given TransactionState.
424 * We do not assign XIDs to transactions until/unless this is called.
425 * Also, any parent TransactionStates that don't yet have XIDs are assigned
426 * one; this maintains the invariant that a child transaction has an XID
427 * following its parent's.
430 AssignTransactionId(TransactionState s)
432 bool isSubXact = (s->parent != NULL);
433 ResourceOwner currentOwner;
435 /* Assert that caller didn't screw up */
436 Assert(!TransactionIdIsValid(s->transactionId));
437 Assert(s->state == TRANS_INPROGRESS);
440 * Ensure parent(s) have XIDs, so that a child always has an XID later
441 * than its parent. Musn't recurse here, or we might get a stack overflow
442 * if we're at the bottom of a huge stack of subtransactions none of which
445 if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
447 TransactionState p = s->parent;
448 TransactionState *parents;
449 size_t parentOffset = 0;
451 parents = palloc(sizeof(TransactionState) * s->nestingLevel);
452 while (p != NULL && !TransactionIdIsValid(p->transactionId))
454 parents[parentOffset++] = p;
459 * This is technically a recursive call, but the recursion will never
460 * be more than one layer deep.
462 while (parentOffset != 0)
463 AssignTransactionId(parents[--parentOffset]);
469 * Generate a new Xid and record it in PG_PROC and pg_subtrans.
471 * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
472 * shared storage other than PG_PROC; because if there's no room for it in
473 * PG_PROC, the subtrans entry is needed to ensure that other backends see
474 * the Xid as "running". See GetNewTransactionId.
476 s->transactionId = GetNewTransactionId(isSubXact);
479 SubTransSetParent(s->transactionId, s->parent->transactionId, false);
482 * If it's a top-level transaction, the predicate locking system needs to
483 * be told about it too.
486 RegisterPredicateLockingXid(s->transactionId);
489 * Acquire lock on the transaction XID. (We assume this cannot block.) We
490 * have to ensure that the lock is assigned to the transaction's own
493 currentOwner = CurrentResourceOwner;
496 CurrentResourceOwner = s->curTransactionOwner;
497 XactLockTableInsert(s->transactionId);
501 /* Ensure CurrentResourceOwner is restored on error */
502 CurrentResourceOwner = currentOwner;
506 CurrentResourceOwner = currentOwner;
509 * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
510 * top-level transaction we issue a WAL record for the assignment. We
511 * include the top-level xid and all the subxids that have not yet been
512 * reported using XLOG_XACT_ASSIGNMENT records.
514 * This is required to limit the amount of shared memory required in a hot
515 * standby server to keep track of in-progress XIDs. See notes for
516 * RecordKnownAssignedTransactionIds().
518 * We don't keep track of the immediate parent of each subxid, only the
519 * top-level transaction that each subxact belongs to. This is correct in
520 * recovery only because aborted subtransactions are separately WAL
523 if (isSubXact && XLogStandbyInfoActive())
525 unreportedXids[nUnreportedXids] = s->transactionId;
529 * ensure this test matches similar one in
530 * RecoverPreparedTransactions()
532 if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS)
534 XLogRecData rdata[2];
535 xl_xact_assignment xlrec;
538 * xtop is always set by now because we recurse up transaction
539 * stack to the highest unassigned xid and then come back down
541 xlrec.xtop = GetTopTransactionId();
542 Assert(TransactionIdIsValid(xlrec.xtop));
543 xlrec.nsubxacts = nUnreportedXids;
545 rdata[0].data = (char *) &xlrec;
546 rdata[0].len = MinSizeOfXactAssignment;
547 rdata[0].buffer = InvalidBuffer;
548 rdata[0].next = &rdata[1];
550 rdata[1].data = (char *) unreportedXids;
551 rdata[1].len = PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId);
552 rdata[1].buffer = InvalidBuffer;
553 rdata[1].next = NULL;
555 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT, rdata);
563 * GetCurrentSubTransactionId
566 GetCurrentSubTransactionId(void)
568 TransactionState s = CurrentTransactionState;
570 return s->subTransactionId;
575 * GetCurrentCommandId
577 * "used" must be TRUE if the caller intends to use the command ID to mark
578 * inserted/updated/deleted tuples. FALSE means the ID is being fetched
579 * for read-only purposes (ie, as a snapshot validity cutoff). See
580 * CommandCounterIncrement() for discussion.
583 GetCurrentCommandId(bool used)
585 /* this is global to a transaction, not subtransaction-local */
587 currentCommandIdUsed = true;
588 return currentCommandId;
592 * GetCurrentTransactionStartTimestamp
595 GetCurrentTransactionStartTimestamp(void)
597 return xactStartTimestamp;
601 * GetCurrentStatementStartTimestamp
604 GetCurrentStatementStartTimestamp(void)
606 return stmtStartTimestamp;
610 * GetCurrentTransactionStopTimestamp
612 * We return current time if the transaction stop time hasn't been set
613 * (which can happen if we decide we don't need to log an XLOG record).
616 GetCurrentTransactionStopTimestamp(void)
618 if (xactStopTimestamp != 0)
619 return xactStopTimestamp;
620 return GetCurrentTimestamp();
624 * SetCurrentStatementStartTimestamp
627 SetCurrentStatementStartTimestamp(void)
629 stmtStartTimestamp = GetCurrentTimestamp();
633 * SetCurrentTransactionStopTimestamp
636 SetCurrentTransactionStopTimestamp(void)
638 xactStopTimestamp = GetCurrentTimestamp();
642 * GetCurrentTransactionNestLevel
644 * Note: this will return zero when not inside any transaction, one when
645 * inside a top-level transaction, etc.
648 GetCurrentTransactionNestLevel(void)
650 TransactionState s = CurrentTransactionState;
652 return s->nestingLevel;
657 * TransactionIdIsCurrentTransactionId
660 TransactionIdIsCurrentTransactionId(TransactionId xid)
665 * We always say that BootstrapTransactionId is "not my transaction ID"
666 * even when it is (ie, during bootstrap). Along with the fact that
667 * transam.c always treats BootstrapTransactionId as already committed,
668 * this causes the tqual.c routines to see all tuples as committed, which
669 * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
670 * it never updates or deletes them, so all tuples can be presumed good
673 * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
674 * not my transaction ID, so we can just return "false" immediately for
675 * any non-normal XID.
677 if (!TransactionIdIsNormal(xid))
681 * We will return true for the Xid of the current subtransaction, any of
682 * its subcommitted children, any of its parents, or any of their
683 * previously subcommitted children. However, a transaction being aborted
684 * is no longer "current", even though it may still have an entry on the
687 for (s = CurrentTransactionState; s != NULL; s = s->parent)
692 if (s->state == TRANS_ABORT)
694 if (!TransactionIdIsValid(s->transactionId))
695 continue; /* it can't have any child XIDs either */
696 if (TransactionIdEquals(xid, s->transactionId))
698 /* As the childXids array is ordered, we can use binary search */
700 high = s->nChildXids - 1;
706 middle = low + (high - low) / 2;
707 probe = s->childXids[middle];
708 if (TransactionIdEquals(probe, xid))
710 else if (TransactionIdPrecedes(probe, xid))
721 * TransactionStartedDuringRecovery
723 * Returns true if the current transaction started while recovery was still
724 * in progress. Recovery might have ended since so RecoveryInProgress() might
725 * return false already.
728 TransactionStartedDuringRecovery(void)
730 return CurrentTransactionState->startedInRecovery;
734 * CommandCounterIncrement
737 CommandCounterIncrement(void)
740 * If the current value of the command counter hasn't been "used" to mark
741 * tuples, we need not increment it, since there's no need to distinguish
742 * a read-only command from others. This helps postpone command counter
743 * overflow, and keeps no-op CommandCounterIncrement operations cheap.
745 if (currentCommandIdUsed)
747 currentCommandId += 1;
748 if (currentCommandId == FirstCommandId) /* check for overflow */
750 currentCommandId -= 1;
752 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
753 errmsg("cannot have more than 2^32-1 commands in a transaction")));
755 currentCommandIdUsed = false;
757 /* Propagate new command ID into static snapshots */
758 SnapshotSetCommandId(currentCommandId);
761 * Make any catalog changes done by the just-completed command visible
762 * in the local syscache. We obviously don't need to do this after a
763 * read-only command. (But see hacks in inval.c to make real sure we
764 * don't think a command that queued inval messages was read-only.)
773 * Interface routine to allow commands to force a synchronous commit of the
774 * current top-level transaction
777 ForceSyncCommit(void)
779 forceSyncCommit = true;
783 /* ----------------------------------------------------------------
784 * StartTransaction stuff
785 * ----------------------------------------------------------------
794 AcceptInvalidationMessages();
803 TransactionState s = CurrentTransactionState;
806 * If this is the first time through, create a private context for
807 * AbortTransaction to work in. By reserving some space now, we can
808 * insulate AbortTransaction from out-of-memory scenarios. Like
809 * ErrorContext, we set it up with slow growth rate and a nonzero minimum
810 * size, so that space will be reserved immediately.
812 if (TransactionAbortContext == NULL)
813 TransactionAbortContext =
814 AllocSetContextCreate(TopMemoryContext,
815 "TransactionAbortContext",
821 * We shouldn't have a transaction context already.
823 Assert(TopTransactionContext == NULL);
826 * Create a toplevel context for the transaction.
828 TopTransactionContext =
829 AllocSetContextCreate(TopMemoryContext,
830 "TopTransactionContext",
831 ALLOCSET_DEFAULT_MINSIZE,
832 ALLOCSET_DEFAULT_INITSIZE,
833 ALLOCSET_DEFAULT_MAXSIZE);
836 * In a top-level transaction, CurTransactionContext is the same as
837 * TopTransactionContext.
839 CurTransactionContext = TopTransactionContext;
840 s->curTransactionContext = CurTransactionContext;
842 /* Make the CurTransactionContext active. */
843 MemoryContextSwitchTo(CurTransactionContext);
847 * AtStart_ResourceOwner
850 AtStart_ResourceOwner(void)
852 TransactionState s = CurrentTransactionState;
855 * We shouldn't have a transaction resource owner already.
857 Assert(TopTransactionResourceOwner == NULL);
860 * Create a toplevel resource owner for the transaction.
862 s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
864 TopTransactionResourceOwner = s->curTransactionOwner;
865 CurTransactionResourceOwner = s->curTransactionOwner;
866 CurrentResourceOwner = s->curTransactionOwner;
869 /* ----------------------------------------------------------------
870 * StartSubTransaction stuff
871 * ----------------------------------------------------------------
878 AtSubStart_Memory(void)
880 TransactionState s = CurrentTransactionState;
882 Assert(CurTransactionContext != NULL);
885 * Create a CurTransactionContext, which will be used to hold data that
886 * survives subtransaction commit but disappears on subtransaction abort.
887 * We make it a child of the immediate parent's CurTransactionContext.
889 CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
890 "CurTransactionContext",
891 ALLOCSET_DEFAULT_MINSIZE,
892 ALLOCSET_DEFAULT_INITSIZE,
893 ALLOCSET_DEFAULT_MAXSIZE);
894 s->curTransactionContext = CurTransactionContext;
896 /* Make the CurTransactionContext active. */
897 MemoryContextSwitchTo(CurTransactionContext);
901 * AtSubStart_ResourceOwner
904 AtSubStart_ResourceOwner(void)
906 TransactionState s = CurrentTransactionState;
908 Assert(s->parent != NULL);
911 * Create a resource owner for the subtransaction. We make it a child of
912 * the immediate parent's resource owner.
914 s->curTransactionOwner =
915 ResourceOwnerCreate(s->parent->curTransactionOwner,
918 CurTransactionResourceOwner = s->curTransactionOwner;
919 CurrentResourceOwner = s->curTransactionOwner;
922 /* ----------------------------------------------------------------
923 * CommitTransaction stuff
924 * ----------------------------------------------------------------
928 * RecordTransactionCommit
930 * Returns latest XID among xact and its children, or InvalidTransactionId
931 * if the xact has no XID. (We compute that here just because it's easier.)
934 RecordTransactionCommit(void)
936 TransactionId xid = GetTopTransactionIdIfAny();
937 bool markXidCommitted = TransactionIdIsValid(xid);
938 TransactionId latestXid = InvalidTransactionId;
942 TransactionId *children;
944 SharedInvalidationMessage *invalMessages = NULL;
945 bool RelcacheInitFileInval = false;
948 /* Get data needed for commit record */
949 nrels = smgrGetPendingDeletes(true, &rels);
950 nchildren = xactGetCommittedChildren(&children);
951 if (XLogStandbyInfoActive())
952 nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
953 &RelcacheInitFileInval);
954 wrote_xlog = (XactLastRecEnd != 0);
957 * If we haven't been assigned an XID yet, we neither can, nor do we want
958 * to write a COMMIT record.
960 if (!markXidCommitted)
963 * We expect that every smgrscheduleunlink is followed by a catalog
964 * update, and hence XID assignment, so we shouldn't get here with any
965 * pending deletes. Use a real test not just an Assert to check this,
966 * since it's a bit fragile.
969 elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
971 /* Can't have child XIDs either; AssignTransactionId enforces this */
972 Assert(nchildren == 0);
975 * If we didn't create XLOG entries, we're done here; otherwise we
976 * should flush those entries the same as a commit record. (An
977 * example of a possible record that wouldn't cause an XID to be
978 * assigned is a sequence advance record due to nextval() --- we want
979 * to flush that to disk before reporting commit.)
987 * Begin commit critical section and insert the commit XLOG record.
989 /* Tell bufmgr and smgr to prepare for commit */
993 * Mark ourselves as within our "commit critical section". This
994 * forces any concurrent checkpoint to wait until we've updated
995 * pg_clog. Without this, it is possible for the checkpoint to set
996 * REDO after the XLOG record but fail to flush the pg_clog update to
997 * disk, leading to loss of the transaction commit if the system
998 * crashes a little later.
1000 * Note: we could, but don't bother to, set this flag in
1001 * RecordTransactionAbort. That's because loss of a transaction abort
1002 * is noncritical; the presumption would be that it aborted, anyway.
1004 * It's safe to change the inCommit flag of our own backend without
1005 * holding the ProcArrayLock, since we're the only one modifying it.
1006 * This makes checkpoint's determination of which xacts are inCommit a
1007 * bit fuzzy, but it doesn't matter.
1009 START_CRIT_SECTION();
1010 MyPgXact->inCommit = true;
1012 SetCurrentTransactionStopTimestamp();
1015 * Do we need the long commit record? If not, use the compact format.
1017 if (nrels > 0 || nmsgs > 0 || RelcacheInitFileInval || forceSyncCommit)
1019 XLogRecData rdata[4];
1021 xl_xact_commit xlrec;
1024 * Set flags required for recovery processing of commits.
1027 if (RelcacheInitFileInval)
1028 xlrec.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
1029 if (forceSyncCommit)
1030 xlrec.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
1032 xlrec.dbId = MyDatabaseId;
1033 xlrec.tsId = MyDatabaseTableSpace;
1035 xlrec.xact_time = xactStopTimestamp;
1036 xlrec.nrels = nrels;
1037 xlrec.nsubxacts = nchildren;
1038 xlrec.nmsgs = nmsgs;
1039 rdata[0].data = (char *) (&xlrec);
1040 rdata[0].len = MinSizeOfXactCommit;
1041 rdata[0].buffer = InvalidBuffer;
1042 /* dump rels to delete */
1045 rdata[0].next = &(rdata[1]);
1046 rdata[1].data = (char *) rels;
1047 rdata[1].len = nrels * sizeof(RelFileNode);
1048 rdata[1].buffer = InvalidBuffer;
1051 /* dump committed child Xids */
1054 rdata[lastrdata].next = &(rdata[2]);
1055 rdata[2].data = (char *) children;
1056 rdata[2].len = nchildren * sizeof(TransactionId);
1057 rdata[2].buffer = InvalidBuffer;
1060 /* dump shared cache invalidation messages */
1063 rdata[lastrdata].next = &(rdata[3]);
1064 rdata[3].data = (char *) invalMessages;
1065 rdata[3].len = nmsgs * sizeof(SharedInvalidationMessage);
1066 rdata[3].buffer = InvalidBuffer;
1069 rdata[lastrdata].next = NULL;
1071 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
1075 XLogRecData rdata[2];
1077 xl_xact_commit_compact xlrec;
1079 xlrec.xact_time = xactStopTimestamp;
1080 xlrec.nsubxacts = nchildren;
1081 rdata[0].data = (char *) (&xlrec);
1082 rdata[0].len = MinSizeOfXactCommitCompact;
1083 rdata[0].buffer = InvalidBuffer;
1084 /* dump committed child Xids */
1087 rdata[0].next = &(rdata[1]);
1088 rdata[1].data = (char *) children;
1089 rdata[1].len = nchildren * sizeof(TransactionId);
1090 rdata[1].buffer = InvalidBuffer;
1093 rdata[lastrdata].next = NULL;
1095 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT_COMPACT, rdata);
1100 * Check if we want to commit asynchronously. We can allow the XLOG flush
1101 * to happen asynchronously if synchronous_commit=off, or if the current
1102 * transaction has not performed any WAL-logged operation. The latter
1103 * case can arise if the current transaction wrote only to temporary
1104 * and/or unlogged tables. In case of a crash, the loss of such a
1105 * transaction will be irrelevant since temp tables will be lost anyway,
1106 * and unlogged tables will be truncated. (Given the foregoing, you might
1107 * think that it would be unnecessary to emit the XLOG record at all in
1108 * this case, but we don't currently try to do that. It would certainly
1109 * cause problems at least in Hot Standby mode, where the
1110 * KnownAssignedXids machinery requires tracking every XID assignment. It
1111 * might be OK to skip it only when wal_level < hot_standby, but for now
1114 * However, if we're doing cleanup of any non-temp rels or committing any
1115 * command that wanted to force sync commit, then we must flush XLOG
1116 * immediately. (We must not allow asynchronous commit if there are any
1117 * non-temp tables to be deleted, because we might delete the files before
1118 * the COMMIT record is flushed to disk. We do allow asynchronous commit
1119 * if all to-be-deleted tables are temporary though, since they are lost
1120 * anyway if we crash.)
1122 if ((wrote_xlog && synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
1123 forceSyncCommit || nrels > 0)
1125 XLogFlush(XactLastRecEnd);
1128 * Now we may update the CLOG, if we wrote a COMMIT record above
1130 if (markXidCommitted)
1131 TransactionIdCommitTree(xid, nchildren, children);
1136 * Asynchronous commit case:
1138 * This enables possible committed transaction loss in the case of a
1139 * postmaster crash because WAL buffers are left unwritten. Ideally we
1140 * could issue the WAL write without the fsync, but some
1141 * wal_sync_methods do not allow separate write/fsync.
1143 * Report the latest async commit LSN, so that the WAL writer knows to
1144 * flush this commit.
1146 XLogSetAsyncXactLSN(XactLastRecEnd);
1149 * We must not immediately update the CLOG, since we didn't flush the
1150 * XLOG. Instead, we store the LSN up to which the XLOG must be
1151 * flushed before the CLOG may be updated.
1153 if (markXidCommitted)
1154 TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1158 * If we entered a commit critical section, leave it now, and let
1159 * checkpoints proceed.
1161 if (markXidCommitted)
1163 MyPgXact->inCommit = false;
1167 /* Compute latestXid while we have the child XIDs handy */
1168 latestXid = TransactionIdLatest(xid, nchildren, children);
1171 * Wait for synchronous replication, if required.
1173 * Note that at this stage we have marked clog, but still show as running
1174 * in the procarray and continue to hold locks.
1177 SyncRepWaitForLSN(XactLastRecEnd);
1179 /* Reset XactLastRecEnd until the next transaction writes something */
1183 /* Clean up local data */
1195 AtCCI_LocalCache(void)
1198 * Make any pending relation map changes visible. We must do this before
1199 * processing local sinval messages, so that the map changes will get
1200 * reflected into the relcache when relcache invals are processed.
1202 AtCCI_RelationMap();
1205 * Make catalog changes visible to me for the next command.
1207 CommandEndInvalidationMessages();
1214 AtCommit_Memory(void)
1217 * Now that we're "out" of a transaction, have the system allocate things
1218 * in the top memory context instead of per-transaction contexts.
1220 MemoryContextSwitchTo(TopMemoryContext);
1223 * Release all transaction-local memory.
1225 Assert(TopTransactionContext != NULL);
1226 MemoryContextDelete(TopTransactionContext);
1227 TopTransactionContext = NULL;
1228 CurTransactionContext = NULL;
1229 CurrentTransactionState->curTransactionContext = NULL;
1232 /* ----------------------------------------------------------------
1233 * CommitSubTransaction stuff
1234 * ----------------------------------------------------------------
1238 * AtSubCommit_Memory
1241 AtSubCommit_Memory(void)
1243 TransactionState s = CurrentTransactionState;
1245 Assert(s->parent != NULL);
1247 /* Return to parent transaction level's memory context. */
1248 CurTransactionContext = s->parent->curTransactionContext;
1249 MemoryContextSwitchTo(CurTransactionContext);
1252 * Ordinarily we cannot throw away the child's CurTransactionContext,
1253 * since the data it contains will be needed at upper commit. However, if
1254 * there isn't actually anything in it, we can throw it away. This avoids
1255 * a small memory leak in the common case of "trivial" subxacts.
1257 if (MemoryContextIsEmpty(s->curTransactionContext))
1259 MemoryContextDelete(s->curTransactionContext);
1260 s->curTransactionContext = NULL;
1265 * AtSubCommit_childXids
1267 * Pass my own XID and my child XIDs up to my parent as committed children.
1270 AtSubCommit_childXids(void)
1272 TransactionState s = CurrentTransactionState;
1275 Assert(s->parent != NULL);
1278 * The parent childXids array will need to hold my XID and all my
1279 * childXids, in addition to the XIDs already there.
1281 new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1283 /* Allocate or enlarge the parent array if necessary */
1284 if (s->parent->maxChildXids < new_nChildXids)
1286 int new_maxChildXids;
1287 TransactionId *new_childXids;
1290 * Make it 2x what's needed right now, to avoid having to enlarge it
1291 * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1292 * is what ensures that we don't need to worry about integer overflow
1293 * here or in the calculation of new_nChildXids.)
1295 new_maxChildXids = Min(new_nChildXids * 2,
1296 (int) (MaxAllocSize / sizeof(TransactionId)));
1298 if (new_maxChildXids < new_nChildXids)
1300 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1301 errmsg("maximum number of committed subtransactions (%d) exceeded",
1302 (int) (MaxAllocSize / sizeof(TransactionId)))));
1305 * We keep the child-XID arrays in TopTransactionContext; this avoids
1306 * setting up child-transaction contexts for what might be just a few
1307 * bytes of grandchild XIDs.
1309 if (s->parent->childXids == NULL)
1311 MemoryContextAlloc(TopTransactionContext,
1312 new_maxChildXids * sizeof(TransactionId));
1314 new_childXids = repalloc(s->parent->childXids,
1315 new_maxChildXids * sizeof(TransactionId));
1317 s->parent->childXids = new_childXids;
1318 s->parent->maxChildXids = new_maxChildXids;
1322 * Copy all my XIDs to parent's array.
1324 * Note: We rely on the fact that the XID of a child always follows that
1325 * of its parent. By copying the XID of this subtransaction before the
1326 * XIDs of its children, we ensure that the array stays ordered. Likewise,
1327 * all XIDs already in the array belong to subtransactions started and
1328 * subcommitted before us, so their XIDs must precede ours.
1330 s->parent->childXids[s->parent->nChildXids] = s->transactionId;
1332 if (s->nChildXids > 0)
1333 memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1335 s->nChildXids * sizeof(TransactionId));
1337 s->parent->nChildXids = new_nChildXids;
1339 /* Release child's array to avoid leakage */
1340 if (s->childXids != NULL)
1341 pfree(s->childXids);
1342 /* We must reset these to avoid double-free if fail later in commit */
1343 s->childXids = NULL;
1345 s->maxChildXids = 0;
1348 /* ----------------------------------------------------------------
1349 * AbortTransaction stuff
1350 * ----------------------------------------------------------------
1354 * RecordTransactionAbort
1356 * Returns latest XID among xact and its children, or InvalidTransactionId
1357 * if the xact has no XID. (We compute that here just because it's easier.)
1359 static TransactionId
1360 RecordTransactionAbort(bool isSubXact)
1362 TransactionId xid = GetCurrentTransactionIdIfAny();
1363 TransactionId latestXid;
1367 TransactionId *children;
1368 XLogRecData rdata[3];
1370 xl_xact_abort xlrec;
1373 * If we haven't been assigned an XID, nobody will care whether we aborted
1374 * or not. Hence, we're done in that case. It does not matter if we have
1375 * rels to delete (note that this routine is not responsible for actually
1376 * deleting 'em). We cannot have any child XIDs, either.
1378 if (!TransactionIdIsValid(xid))
1380 /* Reset XactLastRecEnd until the next transaction writes something */
1383 return InvalidTransactionId;
1387 * We have a valid XID, so we should write an ABORT record for it.
1389 * We do not flush XLOG to disk here, since the default assumption after a
1390 * crash would be that we aborted, anyway. For the same reason, we don't
1391 * need to worry about interlocking against checkpoint start.
1395 * Check that we haven't aborted halfway through RecordTransactionCommit.
1397 if (TransactionIdDidCommit(xid))
1398 elog(PANIC, "cannot abort transaction %u, it was already committed",
1401 /* Fetch the data we need for the abort record */
1402 nrels = smgrGetPendingDeletes(false, &rels);
1403 nchildren = xactGetCommittedChildren(&children);
1405 /* XXX do we really need a critical section here? */
1406 START_CRIT_SECTION();
1408 /* Write the ABORT record */
1410 xlrec.xact_time = GetCurrentTimestamp();
1413 SetCurrentTransactionStopTimestamp();
1414 xlrec.xact_time = xactStopTimestamp;
1416 xlrec.nrels = nrels;
1417 xlrec.nsubxacts = nchildren;
1418 rdata[0].data = (char *) (&xlrec);
1419 rdata[0].len = MinSizeOfXactAbort;
1420 rdata[0].buffer = InvalidBuffer;
1421 /* dump rels to delete */
1424 rdata[0].next = &(rdata[1]);
1425 rdata[1].data = (char *) rels;
1426 rdata[1].len = nrels * sizeof(RelFileNode);
1427 rdata[1].buffer = InvalidBuffer;
1430 /* dump committed child Xids */
1433 rdata[lastrdata].next = &(rdata[2]);
1434 rdata[2].data = (char *) children;
1435 rdata[2].len = nchildren * sizeof(TransactionId);
1436 rdata[2].buffer = InvalidBuffer;
1439 rdata[lastrdata].next = NULL;
1441 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);
1444 * Report the latest async abort LSN, so that the WAL writer knows to
1445 * flush this abort. There's nothing to be gained by delaying this, since
1446 * WALWriter may as well do this when it can. This is important with
1447 * streaming replication because if we don't flush WAL regularly we will
1448 * find that large aborts leave us with a long backlog for when commits
1449 * occur after the abort, increasing our window of data loss should
1450 * problems occur at that point.
1453 XLogSetAsyncXactLSN(XactLastRecEnd);
1456 * Mark the transaction aborted in clog. This is not absolutely necessary
1457 * but we may as well do it while we are here; also, in the subxact case
1458 * it is helpful because XactLockTableWait makes use of it to avoid
1459 * waiting for already-aborted subtransactions. It is OK to do it without
1460 * having flushed the ABORT record to disk, because in event of a crash
1461 * we'd be assumed to have aborted anyway.
1463 TransactionIdAbortTree(xid, nchildren, children);
1467 /* Compute latestXid while we have the child XIDs handy */
1468 latestXid = TransactionIdLatest(xid, nchildren, children);
1471 * If we're aborting a subtransaction, we can immediately remove failed
1472 * XIDs from PGPROC's cache of running child XIDs. We do that here for
1473 * subxacts, because we already have the child XID array at hand. For
1474 * main xacts, the equivalent happens just after this function returns.
1477 XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1479 /* Reset XactLastRecEnd until the next transaction writes something */
1483 /* And clean up local data */
1494 AtAbort_Memory(void)
1497 * Switch into TransactionAbortContext, which should have some free space
1498 * even if nothing else does. We'll work in this context until we've
1499 * finished cleaning up.
1501 * It is barely possible to get here when we've not been able to create
1502 * TransactionAbortContext yet; if so use TopMemoryContext.
1504 if (TransactionAbortContext != NULL)
1505 MemoryContextSwitchTo(TransactionAbortContext);
1507 MemoryContextSwitchTo(TopMemoryContext);
1514 AtSubAbort_Memory(void)
1516 Assert(TransactionAbortContext != NULL);
1518 MemoryContextSwitchTo(TransactionAbortContext);
1523 * AtAbort_ResourceOwner
1526 AtAbort_ResourceOwner(void)
1529 * Make sure we have a valid ResourceOwner, if possible (else it will be
1530 * NULL, which is OK)
1532 CurrentResourceOwner = TopTransactionResourceOwner;
1536 * AtSubAbort_ResourceOwner
1539 AtSubAbort_ResourceOwner(void)
1541 TransactionState s = CurrentTransactionState;
1543 /* Make sure we have a valid ResourceOwner */
1544 CurrentResourceOwner = s->curTransactionOwner;
1549 * AtSubAbort_childXids
1552 AtSubAbort_childXids(void)
1554 TransactionState s = CurrentTransactionState;
1557 * We keep the child-XID arrays in TopTransactionContext (see
1558 * AtSubCommit_childXids). This means we'd better free the array
1559 * explicitly at abort to avoid leakage.
1561 if (s->childXids != NULL)
1562 pfree(s->childXids);
1563 s->childXids = NULL;
1565 s->maxChildXids = 0;
1568 * We could prune the unreportedXids array here. But we don't bother. That
1569 * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1570 * would likely introduce more CPU time into the more common paths, so we
1571 * choose not to do that.
1575 /* ----------------------------------------------------------------
1576 * CleanupTransaction stuff
1577 * ----------------------------------------------------------------
1584 AtCleanup_Memory(void)
1586 Assert(CurrentTransactionState->parent == NULL);
1589 * Now that we're "out" of a transaction, have the system allocate things
1590 * in the top memory context instead of per-transaction contexts.
1592 MemoryContextSwitchTo(TopMemoryContext);
1595 * Clear the special abort context for next time.
1597 if (TransactionAbortContext != NULL)
1598 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1601 * Release all transaction-local memory.
1603 if (TopTransactionContext != NULL)
1604 MemoryContextDelete(TopTransactionContext);
1605 TopTransactionContext = NULL;
1606 CurTransactionContext = NULL;
1607 CurrentTransactionState->curTransactionContext = NULL;
1611 /* ----------------------------------------------------------------
1612 * CleanupSubTransaction stuff
1613 * ----------------------------------------------------------------
1617 * AtSubCleanup_Memory
1620 AtSubCleanup_Memory(void)
1622 TransactionState s = CurrentTransactionState;
1624 Assert(s->parent != NULL);
1626 /* Make sure we're not in an about-to-be-deleted context */
1627 MemoryContextSwitchTo(s->parent->curTransactionContext);
1628 CurTransactionContext = s->parent->curTransactionContext;
1631 * Clear the special abort context for next time.
1633 if (TransactionAbortContext != NULL)
1634 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1637 * Delete the subxact local memory contexts. Its CurTransactionContext can
1638 * go too (note this also kills CurTransactionContexts from any children
1641 if (s->curTransactionContext)
1642 MemoryContextDelete(s->curTransactionContext);
1643 s->curTransactionContext = NULL;
1646 /* ----------------------------------------------------------------
1647 * interface routines
1648 * ----------------------------------------------------------------
1655 StartTransaction(void)
1658 VirtualTransactionId vxid;
1661 * Let's just make sure the state stack is empty
1663 s = &TopTransactionStateData;
1664 CurrentTransactionState = s;
1667 * check the current transaction state
1669 if (s->state != TRANS_DEFAULT)
1670 elog(WARNING, "StartTransaction while in %s state",
1671 TransStateAsString(s->state));
1674 * set the current transaction state information appropriately during
1677 s->state = TRANS_START;
1678 s->transactionId = InvalidTransactionId; /* until assigned */
1681 * Make sure we've reset xact state variables
1683 * If recovery is still in progress, mark this transaction as read-only.
1684 * We have lower level defences in XLogInsert and elsewhere to stop us
1685 * from modifying data during recovery, but this gives the normal
1686 * indication to the user that the transaction is read-only.
1688 if (RecoveryInProgress())
1690 s->startedInRecovery = true;
1691 XactReadOnly = true;
1695 s->startedInRecovery = false;
1696 XactReadOnly = DefaultXactReadOnly;
1698 XactDeferrable = DefaultXactDeferrable;
1699 XactIsoLevel = DefaultXactIsoLevel;
1700 forceSyncCommit = false;
1701 MyXactAccessedTempRel = false;
1704 * reinitialize within-transaction counters
1706 s->subTransactionId = TopSubTransactionId;
1707 currentSubTransactionId = TopSubTransactionId;
1708 currentCommandId = FirstCommandId;
1709 currentCommandIdUsed = false;
1712 * initialize reported xid accounting
1714 nUnreportedXids = 0;
1717 * must initialize resource-management stuff first
1720 AtStart_ResourceOwner();
1723 * Assign a new LocalTransactionId, and combine it with the backendId to
1724 * form a virtual transaction id.
1726 vxid.backendId = MyBackendId;
1727 vxid.localTransactionId = GetNextLocalTransactionId();
1730 * Lock the virtual transaction id before we announce it in the proc array
1732 VirtualXactLockTableInsert(vxid);
1735 * Advertise it in the proc array. We assume assignment of
1736 * LocalTransactionID is atomic, and the backendId should be set already.
1738 Assert(MyProc->backendId == vxid.backendId);
1739 MyProc->lxid = vxid.localTransactionId;
1741 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1744 * set transaction_timestamp() (a/k/a now()). We want this to be the same
1745 * as the first command's statement_timestamp(), so don't do a fresh
1746 * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1747 * xactStopTimestamp as unset.
1749 xactStartTimestamp = stmtStartTimestamp;
1750 xactStopTimestamp = 0;
1751 pgstat_report_xact_timestamp(xactStartTimestamp);
1754 * initialize current transaction state fields
1756 * note: prevXactReadOnly is not used at the outermost level
1758 s->nestingLevel = 1;
1759 s->gucNestLevel = 1;
1760 s->childXids = NULL;
1762 s->maxChildXids = 0;
1763 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1764 /* SecurityRestrictionContext should never be set outside a transaction */
1765 Assert(s->prevSecContext == 0);
1768 * initialize other subsystems for new transaction
1773 AfterTriggerBeginXact();
1776 * done with start processing, set current transaction state to "in
1779 s->state = TRANS_INPROGRESS;
1781 ShowTransactionState("StartTransaction");
1788 * NB: if you change this routine, better look at PrepareTransaction too!
1791 CommitTransaction(void)
1793 TransactionState s = CurrentTransactionState;
1794 TransactionId latestXid;
1796 ShowTransactionState("CommitTransaction");
1799 * check the current transaction state
1801 if (s->state != TRANS_INPROGRESS)
1802 elog(WARNING, "CommitTransaction while in %s state",
1803 TransStateAsString(s->state));
1804 Assert(s->parent == NULL);
1807 * Do pre-commit processing that involves calling user-defined code, such
1808 * as triggers. Since closing cursors could queue trigger actions,
1809 * triggers could open cursors, etc, we have to keep looping until there's
1810 * nothing left to do.
1815 * Fire all currently pending deferred triggers.
1817 AfterTriggerFireDeferred();
1820 * Close open portals (converting holdable ones into static portals).
1821 * If there weren't any, we are done ... otherwise loop back to check
1822 * if they queued deferred triggers. Lather, rinse, repeat.
1824 if (!PreCommit_Portals(false))
1829 * The remaining actions cannot call any user-defined code, so it's safe
1830 * to start shutting down within-transaction services. But note that most
1831 * of this stuff could still throw an error, which would switch us into
1832 * the transaction-abort path.
1835 /* Shut down the deferred-trigger manager */
1836 AfterTriggerEndXact(true);
1839 * Let ON COMMIT management do its thing (must happen after closing
1840 * cursors, to avoid dangling-reference problems)
1842 PreCommit_on_commit_actions();
1844 /* close large objects before lower-level cleanup */
1845 AtEOXact_LargeObject(true);
1848 * Mark serializable transaction as complete for predicate locking
1849 * purposes. This should be done as late as we can put it and still allow
1850 * errors to be raised for failure patterns found at commit.
1852 PreCommit_CheckForSerializationFailure();
1855 * Insert notifications sent by NOTIFY commands into the queue. This
1856 * should be late in the pre-commit sequence to minimize time spent
1857 * holding the notify-insertion lock.
1861 /* Prevent cancel/die interrupt while cleaning up */
1864 /* Commit updates to the relation map --- do this as late as possible */
1865 AtEOXact_RelationMap(true);
1868 * set the current transaction state information appropriately during
1871 s->state = TRANS_COMMIT;
1874 * Here is where we really truly commit.
1876 latestXid = RecordTransactionCommit();
1878 TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
1881 * Let others know about no transaction in progress by me. Note that this
1882 * must be done _before_ releasing locks we hold and _after_
1883 * RecordTransactionCommit.
1885 ProcArrayEndTransaction(MyProc, latestXid);
1888 * This is all post-commit cleanup. Note that if an error is raised here,
1889 * it's too late to abort the transaction. This should be just
1890 * noncritical resource releasing.
1892 * The ordering of operations is not entirely random. The idea is:
1893 * release resources visible to other backends (eg, files, buffer pins);
1894 * then release locks; then release backend-local resources. We want to
1895 * release locks at the point where any backend waiting for us will see
1896 * our transaction as being fully cleaned up.
1898 * Resources that can be associated with individual queries are handled by
1899 * the ResourceOwner mechanism. The other calls here are for backend-wide
1903 CallXactCallbacks(XACT_EVENT_COMMIT);
1905 ResourceOwnerRelease(TopTransactionResourceOwner,
1906 RESOURCE_RELEASE_BEFORE_LOCKS,
1909 /* Check we've released all buffer pins */
1910 AtEOXact_Buffers(true);
1912 /* Clean up the relation cache */
1913 AtEOXact_RelationCache(true);
1916 * Make catalog changes visible to all backends. This has to happen after
1917 * relcache references are dropped (see comments for
1918 * AtEOXact_RelationCache), but before locks are released (if anyone is
1919 * waiting for lock on a relation we've modified, we want them to know
1920 * about the catalog change before they start using the relation).
1922 AtEOXact_Inval(true);
1924 AtEOXact_MultiXact();
1926 ResourceOwnerRelease(TopTransactionResourceOwner,
1927 RESOURCE_RELEASE_LOCKS,
1929 ResourceOwnerRelease(TopTransactionResourceOwner,
1930 RESOURCE_RELEASE_AFTER_LOCKS,
1934 * Likewise, dropping of files deleted during the transaction is best done
1935 * after releasing relcache and buffer pins. (This is not strictly
1936 * necessary during commit, since such pins should have been released
1937 * already, but this ordering is definitely critical during abort.) Since
1938 * this may take many seconds, also delay until after releasing locks.
1939 * Other backends will observe the attendant catalog changes and not
1940 * attempt to access affected files.
1942 smgrDoPendingDeletes(true);
1944 /* Check we've released all catcache entries */
1945 AtEOXact_CatCache(true);
1948 AtEOXact_GUC(true, 1);
1950 AtEOXact_on_commit_actions(true);
1951 AtEOXact_Namespace(true);
1954 AtEOXact_ComboCid();
1955 AtEOXact_HashTables(true);
1956 AtEOXact_PgStat(true);
1957 AtEOXact_Snapshot(true);
1958 pgstat_report_xact_timestamp(0);
1960 CurrentResourceOwner = NULL;
1961 ResourceOwnerDelete(TopTransactionResourceOwner);
1962 s->curTransactionOwner = NULL;
1963 CurTransactionResourceOwner = NULL;
1964 TopTransactionResourceOwner = NULL;
1968 s->transactionId = InvalidTransactionId;
1969 s->subTransactionId = InvalidSubTransactionId;
1970 s->nestingLevel = 0;
1971 s->gucNestLevel = 0;
1972 s->childXids = NULL;
1974 s->maxChildXids = 0;
1977 * done with commit processing, set current transaction state back to
1980 s->state = TRANS_DEFAULT;
1982 RESUME_INTERRUPTS();
1987 * PrepareTransaction
1989 * NB: if you change this routine, better look at CommitTransaction too!
1992 PrepareTransaction(void)
1994 TransactionState s = CurrentTransactionState;
1995 TransactionId xid = GetCurrentTransactionId();
1996 GlobalTransaction gxact;
1997 TimestampTz prepared_at;
1999 ShowTransactionState("PrepareTransaction");
2002 * check the current transaction state
2004 if (s->state != TRANS_INPROGRESS)
2005 elog(WARNING, "PrepareTransaction while in %s state",
2006 TransStateAsString(s->state));
2007 Assert(s->parent == NULL);
2010 * Do pre-commit processing that involves calling user-defined code, such
2011 * as triggers. Since closing cursors could queue trigger actions,
2012 * triggers could open cursors, etc, we have to keep looping until there's
2013 * nothing left to do.
2018 * Fire all currently pending deferred triggers.
2020 AfterTriggerFireDeferred();
2023 * Close open portals (converting holdable ones into static portals).
2024 * If there weren't any, we are done ... otherwise loop back to check
2025 * if they queued deferred triggers. Lather, rinse, repeat.
2027 if (!PreCommit_Portals(true))
2032 * The remaining actions cannot call any user-defined code, so it's safe
2033 * to start shutting down within-transaction services. But note that most
2034 * of this stuff could still throw an error, which would switch us into
2035 * the transaction-abort path.
2038 /* Shut down the deferred-trigger manager */
2039 AfterTriggerEndXact(true);
2042 * Let ON COMMIT management do its thing (must happen after closing
2043 * cursors, to avoid dangling-reference problems)
2045 PreCommit_on_commit_actions();
2047 /* close large objects before lower-level cleanup */
2048 AtEOXact_LargeObject(true);
2051 * Mark serializable transaction as complete for predicate locking
2052 * purposes. This should be done as late as we can put it and still allow
2053 * errors to be raised for failure patterns found at commit.
2055 PreCommit_CheckForSerializationFailure();
2057 /* NOTIFY will be handled below */
2060 * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2061 * this transaction. Having the prepared xact hold locks on another
2062 * backend's temp table seems a bad idea --- for instance it would prevent
2063 * the backend from exiting. There are other problems too, such as how to
2064 * clean up the source backend's local buffers and ON COMMIT state if the
2065 * prepared xact includes a DROP of a temp table.
2067 * We must check this after executing any ON COMMIT actions, because they
2068 * might still access a temp relation.
2070 * XXX In principle this could be relaxed to allow some useful special
2071 * cases, such as a temp table created and dropped all within the
2072 * transaction. That seems to require much more bookkeeping though.
2074 if (MyXactAccessedTempRel)
2076 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2077 errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2080 * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2081 * supported if we added cleanup logic to twophase.c, but for now it
2082 * doesn't seem worth the trouble.
2084 if (XactHasExportedSnapshots())
2086 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2087 errmsg("cannot PREPARE a transaction that has exported snapshots")));
2089 /* Prevent cancel/die interrupt while cleaning up */
2093 * set the current transaction state information appropriately during
2094 * prepare processing
2096 s->state = TRANS_PREPARE;
2098 prepared_at = GetCurrentTimestamp();
2100 /* Tell bufmgr and smgr to prepare for commit */
2104 * Reserve the GID for this transaction. This could fail if the requested
2105 * GID is invalid or already in use.
2107 gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2108 GetUserId(), MyDatabaseId);
2112 * Collect data for the 2PC state file. Note that in general, no actual
2113 * state change should happen in the called modules during this step,
2114 * since it's still possible to fail before commit, and in that case we
2115 * want transaction abort to be able to clean up. (In particular, the
2116 * AtPrepare routines may error out if they find cases they cannot
2117 * handle.) State cleanup should happen in the PostPrepare routines
2118 * below. However, some modules can go ahead and clear state here because
2119 * they wouldn't do anything with it during abort anyway.
2121 * Note: because the 2PC state file records will be replayed in the same
2122 * order they are made, the order of these calls has to match the order in
2123 * which we want things to happen during COMMIT PREPARED or ROLLBACK
2124 * PREPARED; in particular, pay attention to whether things should happen
2125 * before or after releasing the transaction's locks.
2127 StartPrepare(gxact);
2131 AtPrepare_PredicateLocks();
2133 AtPrepare_MultiXact();
2134 AtPrepare_RelationMap();
2137 * Here is where we really truly prepare.
2139 * We have to record transaction prepares even if we didn't make any
2140 * updates, because the transaction manager might get confused if we lose
2141 * a global transaction.
2146 * Now we clean up backend-internal state and release internal resources.
2149 /* Reset XactLastRecEnd until the next transaction writes something */
2153 * Let others know about no transaction in progress by me. This has to be
2154 * done *after* the prepared transaction has been marked valid, else
2155 * someone may think it is unlocked and recyclable.
2157 ProcArrayClearTransaction(MyProc);
2160 * This is all post-transaction cleanup. Note that if an error is raised
2161 * here, it's too late to abort the transaction. This should be just
2162 * noncritical resource releasing. See notes in CommitTransaction.
2165 CallXactCallbacks(XACT_EVENT_PREPARE);
2167 ResourceOwnerRelease(TopTransactionResourceOwner,
2168 RESOURCE_RELEASE_BEFORE_LOCKS,
2171 /* Check we've released all buffer pins */
2172 AtEOXact_Buffers(true);
2174 /* Clean up the relation cache */
2175 AtEOXact_RelationCache(true);
2177 /* notify doesn't need a postprepare call */
2179 PostPrepare_PgStat();
2181 PostPrepare_Inval();
2185 PostPrepare_MultiXact(xid);
2187 PostPrepare_Locks(xid);
2188 PostPrepare_PredicateLocks(xid);
2190 ResourceOwnerRelease(TopTransactionResourceOwner,
2191 RESOURCE_RELEASE_LOCKS,
2193 ResourceOwnerRelease(TopTransactionResourceOwner,
2194 RESOURCE_RELEASE_AFTER_LOCKS,
2197 /* Check we've released all catcache entries */
2198 AtEOXact_CatCache(true);
2200 /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2201 AtEOXact_GUC(true, 1);
2203 AtEOXact_on_commit_actions(true);
2204 AtEOXact_Namespace(true);
2207 AtEOXact_ComboCid();
2208 AtEOXact_HashTables(true);
2209 /* don't call AtEOXact_PgStat here */
2210 AtEOXact_Snapshot(true);
2212 CurrentResourceOwner = NULL;
2213 ResourceOwnerDelete(TopTransactionResourceOwner);
2214 s->curTransactionOwner = NULL;
2215 CurTransactionResourceOwner = NULL;
2216 TopTransactionResourceOwner = NULL;
2220 s->transactionId = InvalidTransactionId;
2221 s->subTransactionId = InvalidSubTransactionId;
2222 s->nestingLevel = 0;
2223 s->gucNestLevel = 0;
2224 s->childXids = NULL;
2226 s->maxChildXids = 0;
2229 * done with 1st phase commit processing, set current transaction state
2232 s->state = TRANS_DEFAULT;
2234 RESUME_INTERRUPTS();
2242 AbortTransaction(void)
2244 TransactionState s = CurrentTransactionState;
2245 TransactionId latestXid;
2247 /* Prevent cancel/die interrupt while cleaning up */
2250 /* Make sure we have a valid memory context and resource owner */
2252 AtAbort_ResourceOwner();
2255 * Release any LW locks we might be holding as quickly as possible.
2256 * (Regular locks, however, must be held till we finish aborting.)
2257 * Releasing LW locks is critical since we might try to grab them again
2258 * while cleaning up!
2262 /* Clean up buffer I/O and buffer context locks, too */
2267 * Also clean up any open wait for lock, since the lock manager will choke
2268 * if we try to wait for another lock before doing this.
2273 * check the current transaction state
2275 if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2276 elog(WARNING, "AbortTransaction while in %s state",
2277 TransStateAsString(s->state));
2278 Assert(s->parent == NULL);
2281 * set the current transaction state information appropriately during the
2284 s->state = TRANS_ABORT;
2287 * Reset user ID which might have been changed transiently. We need this
2288 * to clean up in case control escaped out of a SECURITY DEFINER function
2289 * or other local change of CurrentUserId; therefore, the prior value of
2290 * SecurityRestrictionContext also needs to be restored.
2292 * (Note: it is not necessary to restore session authorization or role
2293 * settings here because those can only be changed via GUC, and GUC will
2294 * take care of rolling them back if need be.)
2296 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2299 * do abort processing
2301 AfterTriggerEndXact(false); /* 'false' means it's abort */
2303 AtEOXact_LargeObject(false);
2305 AtEOXact_RelationMap(false);
2308 * Advertise the fact that we aborted in pg_clog (assuming that we got as
2309 * far as assigning an XID to advertise).
2311 latestXid = RecordTransactionAbort(false);
2313 TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2316 * Let others know about no transaction in progress by me. Note that this
2317 * must be done _before_ releasing locks we hold and _after_
2318 * RecordTransactionAbort.
2320 ProcArrayEndTransaction(MyProc, latestXid);
2323 * Post-abort cleanup. See notes in CommitTransaction() concerning
2324 * ordering. We can skip all of it if the transaction failed before
2325 * creating a resource owner.
2327 if (TopTransactionResourceOwner != NULL)
2329 CallXactCallbacks(XACT_EVENT_ABORT);
2331 ResourceOwnerRelease(TopTransactionResourceOwner,
2332 RESOURCE_RELEASE_BEFORE_LOCKS,
2334 AtEOXact_Buffers(false);
2335 AtEOXact_RelationCache(false);
2336 AtEOXact_Inval(false);
2337 AtEOXact_MultiXact();
2338 ResourceOwnerRelease(TopTransactionResourceOwner,
2339 RESOURCE_RELEASE_LOCKS,
2341 ResourceOwnerRelease(TopTransactionResourceOwner,
2342 RESOURCE_RELEASE_AFTER_LOCKS,
2344 smgrDoPendingDeletes(false);
2345 AtEOXact_CatCache(false);
2347 AtEOXact_GUC(false, 1);
2348 AtEOXact_SPI(false);
2349 AtEOXact_on_commit_actions(false);
2350 AtEOXact_Namespace(false);
2353 AtEOXact_ComboCid();
2354 AtEOXact_HashTables(false);
2355 AtEOXact_PgStat(false);
2356 pgstat_report_xact_timestamp(0);
2360 * State remains TRANS_ABORT until CleanupTransaction().
2362 RESUME_INTERRUPTS();
2366 * CleanupTransaction
2369 CleanupTransaction(void)
2371 TransactionState s = CurrentTransactionState;
2374 * State should still be TRANS_ABORT from AbortTransaction().
2376 if (s->state != TRANS_ABORT)
2377 elog(FATAL, "CleanupTransaction: unexpected state %s",
2378 TransStateAsString(s->state));
2381 * do abort cleanup processing
2383 AtCleanup_Portals(); /* now safe to release portal memory */
2384 AtEOXact_Snapshot(false); /* and release the transaction's snapshots */
2386 CurrentResourceOwner = NULL; /* and resource owner */
2387 if (TopTransactionResourceOwner)
2388 ResourceOwnerDelete(TopTransactionResourceOwner);
2389 s->curTransactionOwner = NULL;
2390 CurTransactionResourceOwner = NULL;
2391 TopTransactionResourceOwner = NULL;
2393 AtCleanup_Memory(); /* and transaction memory */
2395 s->transactionId = InvalidTransactionId;
2396 s->subTransactionId = InvalidSubTransactionId;
2397 s->nestingLevel = 0;
2398 s->gucNestLevel = 0;
2399 s->childXids = NULL;
2401 s->maxChildXids = 0;
2404 * done with abort processing, set current transaction state back to
2407 s->state = TRANS_DEFAULT;
2411 * StartTransactionCommand
2414 StartTransactionCommand(void)
2416 TransactionState s = CurrentTransactionState;
2418 switch (s->blockState)
2421 * if we aren't in a transaction block, we just do our usual start
2424 case TBLOCK_DEFAULT:
2426 s->blockState = TBLOCK_STARTED;
2430 * We are somewhere in a transaction block or subtransaction and
2431 * about to start a new command. For now we do nothing, but
2432 * someday we may do command-local resource initialization. (Note
2433 * that any needed CommandCounterIncrement was done by the
2434 * previous CommitTransactionCommand.)
2436 case TBLOCK_INPROGRESS:
2437 case TBLOCK_SUBINPROGRESS:
2441 * Here we are in a failed transaction block (one of the commands
2442 * caused an abort) so we do nothing but remain in the abort
2443 * state. Eventually we will get a ROLLBACK command which will
2444 * get us out of this state. (It is up to other code to ensure
2445 * that no commands other than ROLLBACK will be processed in these
2449 case TBLOCK_SUBABORT:
2452 /* These cases are invalid. */
2453 case TBLOCK_STARTED:
2455 case TBLOCK_SUBBEGIN:
2457 case TBLOCK_SUBRELEASE:
2458 case TBLOCK_SUBCOMMIT:
2459 case TBLOCK_ABORT_END:
2460 case TBLOCK_SUBABORT_END:
2461 case TBLOCK_ABORT_PENDING:
2462 case TBLOCK_SUBABORT_PENDING:
2463 case TBLOCK_SUBRESTART:
2464 case TBLOCK_SUBABORT_RESTART:
2465 case TBLOCK_PREPARE:
2466 elog(ERROR, "StartTransactionCommand: unexpected state %s",
2467 BlockStateAsString(s->blockState));
2472 * We must switch to CurTransactionContext before returning. This is
2473 * already done if we called StartTransaction, otherwise not.
2475 Assert(CurTransactionContext != NULL);
2476 MemoryContextSwitchTo(CurTransactionContext);
2480 * CommitTransactionCommand
2483 CommitTransactionCommand(void)
2485 TransactionState s = CurrentTransactionState;
2487 switch (s->blockState)
2490 * This shouldn't happen, because it means the previous
2491 * StartTransactionCommand didn't set the STARTED state
2494 case TBLOCK_DEFAULT:
2495 elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2496 BlockStateAsString(s->blockState));
2500 * If we aren't in a transaction block, just do our usual
2501 * transaction commit, and return to the idle state.
2503 case TBLOCK_STARTED:
2504 CommitTransaction();
2505 s->blockState = TBLOCK_DEFAULT;
2509 * We are completing a "BEGIN TRANSACTION" command, so we change
2510 * to the "transaction block in progress" state and return. (We
2511 * assume the BEGIN did nothing to the database, so we need no
2512 * CommandCounterIncrement.)
2515 s->blockState = TBLOCK_INPROGRESS;
2519 * This is the case when we have finished executing a command
2520 * someplace within a transaction block. We increment the command
2521 * counter and return.
2523 case TBLOCK_INPROGRESS:
2524 case TBLOCK_SUBINPROGRESS:
2525 CommandCounterIncrement();
2529 * We are completing a "COMMIT" command. Do it and return to the
2533 CommitTransaction();
2534 s->blockState = TBLOCK_DEFAULT;
2538 * Here we are in the middle of a transaction block but one of the
2539 * commands caused an abort so we do nothing but remain in the
2540 * abort state. Eventually we will get a ROLLBACK comand.
2543 case TBLOCK_SUBABORT:
2547 * Here we were in an aborted transaction block and we just got
2548 * the ROLLBACK command from the user, so clean up the
2549 * already-aborted transaction and return to the idle state.
2551 case TBLOCK_ABORT_END:
2552 CleanupTransaction();
2553 s->blockState = TBLOCK_DEFAULT;
2557 * Here we were in a perfectly good transaction block but the user
2558 * told us to ROLLBACK anyway. We have to abort the transaction
2559 * and then clean up.
2561 case TBLOCK_ABORT_PENDING:
2563 CleanupTransaction();
2564 s->blockState = TBLOCK_DEFAULT;
2568 * We are completing a "PREPARE TRANSACTION" command. Do it and
2569 * return to the idle state.
2571 case TBLOCK_PREPARE:
2572 PrepareTransaction();
2573 s->blockState = TBLOCK_DEFAULT;
2577 * We were just issued a SAVEPOINT inside a transaction block.
2578 * Start a subtransaction. (DefineSavepoint already did
2579 * PushTransaction, so as to have someplace to put the SUBBEGIN
2582 case TBLOCK_SUBBEGIN:
2583 StartSubTransaction();
2584 s->blockState = TBLOCK_SUBINPROGRESS;
2588 * We were issued a RELEASE command, so we end the current
2589 * subtransaction and return to the parent transaction. The parent
2590 * might be ended too, so repeat till we find an INPROGRESS
2591 * transaction or subtransaction.
2593 case TBLOCK_SUBRELEASE:
2596 CommitSubTransaction();
2597 s = CurrentTransactionState; /* changed by pop */
2598 } while (s->blockState == TBLOCK_SUBRELEASE);
2600 Assert(s->blockState == TBLOCK_INPROGRESS ||
2601 s->blockState == TBLOCK_SUBINPROGRESS);
2605 * We were issued a COMMIT, so we end the current subtransaction
2606 * hierarchy and perform final commit. We do this by rolling up
2607 * any subtransactions into their parent, which leads to O(N^2)
2608 * operations with respect to resource owners - this isn't that
2609 * bad until we approach a thousands of savepoints but is
2610 * necessary for correctness should after triggers create new
2613 case TBLOCK_SUBCOMMIT:
2616 CommitSubTransaction();
2617 s = CurrentTransactionState; /* changed by pop */
2618 } while (s->blockState == TBLOCK_SUBCOMMIT);
2619 /* If we had a COMMIT command, finish off the main xact too */
2620 if (s->blockState == TBLOCK_END)
2622 Assert(s->parent == NULL);
2623 CommitTransaction();
2624 s->blockState = TBLOCK_DEFAULT;
2626 else if (s->blockState == TBLOCK_PREPARE)
2628 Assert(s->parent == NULL);
2629 PrepareTransaction();
2630 s->blockState = TBLOCK_DEFAULT;
2633 elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2634 BlockStateAsString(s->blockState));
2638 * The current already-failed subtransaction is ending due to a
2639 * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2640 * examine the parent (which could be in any of several states).
2642 case TBLOCK_SUBABORT_END:
2643 CleanupSubTransaction();
2644 CommitTransactionCommand();
2648 * As above, but it's not dead yet, so abort first.
2650 case TBLOCK_SUBABORT_PENDING:
2651 AbortSubTransaction();
2652 CleanupSubTransaction();
2653 CommitTransactionCommand();
2657 * The current subtransaction is the target of a ROLLBACK TO
2658 * command. Abort and pop it, then start a new subtransaction
2659 * with the same name.
2661 case TBLOCK_SUBRESTART:
2666 /* save name and keep Cleanup from freeing it */
2669 savepointLevel = s->savepointLevel;
2671 AbortSubTransaction();
2672 CleanupSubTransaction();
2674 DefineSavepoint(NULL);
2675 s = CurrentTransactionState; /* changed by push */
2677 s->savepointLevel = savepointLevel;
2679 /* This is the same as TBLOCK_SUBBEGIN case */
2680 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2681 StartSubTransaction();
2682 s->blockState = TBLOCK_SUBINPROGRESS;
2687 * Same as above, but the subtransaction had already failed, so we
2688 * don't need AbortSubTransaction.
2690 case TBLOCK_SUBABORT_RESTART:
2695 /* save name and keep Cleanup from freeing it */
2698 savepointLevel = s->savepointLevel;
2700 CleanupSubTransaction();
2702 DefineSavepoint(NULL);
2703 s = CurrentTransactionState; /* changed by push */
2705 s->savepointLevel = savepointLevel;
2707 /* This is the same as TBLOCK_SUBBEGIN case */
2708 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2709 StartSubTransaction();
2710 s->blockState = TBLOCK_SUBINPROGRESS;
2717 * AbortCurrentTransaction
2720 AbortCurrentTransaction(void)
2722 TransactionState s = CurrentTransactionState;
2724 switch (s->blockState)
2726 case TBLOCK_DEFAULT:
2727 if (s->state == TRANS_DEFAULT)
2729 /* we are idle, so nothing to do */
2734 * We can get here after an error during transaction start
2735 * (state will be TRANS_START). Need to clean up the
2736 * incompletely started transaction. First, adjust the
2737 * low-level state to suppress warning message from
2740 if (s->state == TRANS_START)
2741 s->state = TRANS_INPROGRESS;
2743 CleanupTransaction();
2748 * if we aren't in a transaction block, we just do the basic abort
2749 * & cleanup transaction.
2751 case TBLOCK_STARTED:
2753 CleanupTransaction();
2754 s->blockState = TBLOCK_DEFAULT;
2758 * If we are in TBLOCK_BEGIN it means something screwed up right
2759 * after reading "BEGIN TRANSACTION". We assume that the user
2760 * will interpret the error as meaning the BEGIN failed to get him
2761 * into a transaction block, so we should abort and return to idle
2766 CleanupTransaction();
2767 s->blockState = TBLOCK_DEFAULT;
2771 * We are somewhere in a transaction block and we've gotten a
2772 * failure, so we abort the transaction and set up the persistent
2773 * ABORT state. We will stay in ABORT until we get a ROLLBACK.
2775 case TBLOCK_INPROGRESS:
2777 s->blockState = TBLOCK_ABORT;
2778 /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2782 * Here, we failed while trying to COMMIT. Clean up the
2783 * transaction and return to idle state (we do not want to stay in
2788 CleanupTransaction();
2789 s->blockState = TBLOCK_DEFAULT;
2793 * Here, we are already in an aborted transaction state and are
2794 * waiting for a ROLLBACK, but for some reason we failed again! So
2795 * we just remain in the abort state.
2798 case TBLOCK_SUBABORT:
2802 * We are in a failed transaction and we got the ROLLBACK command.
2803 * We have already aborted, we just need to cleanup and go to idle
2806 case TBLOCK_ABORT_END:
2807 CleanupTransaction();
2808 s->blockState = TBLOCK_DEFAULT;
2812 * We are in a live transaction and we got a ROLLBACK command.
2813 * Abort, cleanup, go to idle state.
2815 case TBLOCK_ABORT_PENDING:
2817 CleanupTransaction();
2818 s->blockState = TBLOCK_DEFAULT;
2822 * Here, we failed while trying to PREPARE. Clean up the
2823 * transaction and return to idle state (we do not want to stay in
2826 case TBLOCK_PREPARE:
2828 CleanupTransaction();
2829 s->blockState = TBLOCK_DEFAULT;
2833 * We got an error inside a subtransaction. Abort just the
2834 * subtransaction, and go to the persistent SUBABORT state until
2837 case TBLOCK_SUBINPROGRESS:
2838 AbortSubTransaction();
2839 s->blockState = TBLOCK_SUBABORT;
2843 * If we failed while trying to create a subtransaction, clean up
2844 * the broken subtransaction and abort the parent. The same
2845 * applies if we get a failure while ending a subtransaction.
2847 case TBLOCK_SUBBEGIN:
2848 case TBLOCK_SUBRELEASE:
2849 case TBLOCK_SUBCOMMIT:
2850 case TBLOCK_SUBABORT_PENDING:
2851 case TBLOCK_SUBRESTART:
2852 AbortSubTransaction();
2853 CleanupSubTransaction();
2854 AbortCurrentTransaction();
2858 * Same as above, except the Abort() was already done.
2860 case TBLOCK_SUBABORT_END:
2861 case TBLOCK_SUBABORT_RESTART:
2862 CleanupSubTransaction();
2863 AbortCurrentTransaction();
2869 * PreventTransactionChain
2871 * This routine is to be called by statements that must not run inside
2872 * a transaction block, typically because they have non-rollback-able
2873 * side effects or do internal commits.
2875 * If we have already started a transaction block, issue an error; also issue
2876 * an error if we appear to be running inside a user-defined function (which
2877 * could issue more commands and possibly cause a failure after the statement
2878 * completes). Subtransactions are verboten too.
2880 * isTopLevel: passed down from ProcessUtility to determine whether we are
2881 * inside a function or multi-query querystring. (We will always fail if
2882 * this is false, but it's convenient to centralize the check here instead of
2883 * making callers do it.)
2884 * stmtType: statement type name, for error messages.
2887 PreventTransactionChain(bool isTopLevel, const char *stmtType)
2890 * xact block already started?
2892 if (IsTransactionBlock())
2894 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2895 /* translator: %s represents an SQL statement name */
2896 errmsg("%s cannot run inside a transaction block",
2902 if (IsSubTransaction())
2904 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2905 /* translator: %s represents an SQL statement name */
2906 errmsg("%s cannot run inside a subtransaction",
2910 * inside a function call?
2914 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2915 /* translator: %s represents an SQL statement name */
2916 errmsg("%s cannot be executed from a function or multi-command string",
2919 /* If we got past IsTransactionBlock test, should be in default state */
2920 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2921 CurrentTransactionState->blockState != TBLOCK_STARTED)
2922 elog(FATAL, "cannot prevent transaction chain");
2927 * RequireTransactionChain
2929 * This routine is to be called by statements that must run inside
2930 * a transaction block, because they have no effects that persist past
2931 * transaction end (and so calling them outside a transaction block
2932 * is presumably an error). DECLARE CURSOR is an example.
2934 * If we appear to be running inside a user-defined function, we do not
2935 * issue an error, since the function could issue more commands that make
2936 * use of the current statement's results. Likewise subtransactions.
2937 * Thus this is an inverse for PreventTransactionChain.
2939 * isTopLevel: passed down from ProcessUtility to determine whether we are
2940 * inside a function.
2941 * stmtType: statement type name, for error messages.
2944 RequireTransactionChain(bool isTopLevel, const char *stmtType)
2947 * xact block already started?
2949 if (IsTransactionBlock())
2955 if (IsSubTransaction())
2959 * inside a function call?
2965 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
2966 /* translator: %s represents an SQL statement name */
2967 errmsg("%s can only be used in transaction blocks",
2972 * IsInTransactionChain
2974 * This routine is for statements that need to behave differently inside
2975 * a transaction block than when running as single commands. ANALYZE is
2976 * currently the only example.
2978 * isTopLevel: passed down from ProcessUtility to determine whether we are
2979 * inside a function.
2982 IsInTransactionChain(bool isTopLevel)
2985 * Return true on same conditions that would make PreventTransactionChain
2988 if (IsTransactionBlock())
2991 if (IsSubTransaction())
2997 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2998 CurrentTransactionState->blockState != TBLOCK_STARTED)
3006 * Register or deregister callback functions for start- and end-of-xact
3009 * These functions are intended for use by dynamically loaded modules.
3010 * For built-in modules we generally just hardwire the appropriate calls
3011 * (mainly because it's easier to control the order that way, where needed).
3013 * At transaction end, the callback occurs post-commit or post-abort, so the
3014 * callback functions can only do noncritical cleanup.
3017 RegisterXactCallback(XactCallback callback, void *arg)
3019 XactCallbackItem *item;
3021 item = (XactCallbackItem *)
3022 MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
3023 item->callback = callback;
3025 item->next = Xact_callbacks;
3026 Xact_callbacks = item;
3030 UnregisterXactCallback(XactCallback callback, void *arg)
3032 XactCallbackItem *item;
3033 XactCallbackItem *prev;
3036 for (item = Xact_callbacks; item; prev = item, item = item->next)
3038 if (item->callback == callback && item->arg == arg)
3041 prev->next = item->next;
3043 Xact_callbacks = item->next;
3051 CallXactCallbacks(XactEvent event)
3053 XactCallbackItem *item;
3055 for (item = Xact_callbacks; item; item = item->next)
3056 (*item->callback) (event, item->arg);
3061 * Register or deregister callback functions for start- and end-of-subxact
3064 * Pretty much same as above, but for subtransaction events.
3066 * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3067 * so the callback functions can only do noncritical cleanup. At
3068 * subtransaction start, the callback is called when the subtransaction has
3069 * finished initializing.
3072 RegisterSubXactCallback(SubXactCallback callback, void *arg)
3074 SubXactCallbackItem *item;
3076 item = (SubXactCallbackItem *)
3077 MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
3078 item->callback = callback;
3080 item->next = SubXact_callbacks;
3081 SubXact_callbacks = item;
3085 UnregisterSubXactCallback(SubXactCallback callback, void *arg)
3087 SubXactCallbackItem *item;
3088 SubXactCallbackItem *prev;
3091 for (item = SubXact_callbacks; item; prev = item, item = item->next)
3093 if (item->callback == callback && item->arg == arg)
3096 prev->next = item->next;
3098 SubXact_callbacks = item->next;
3106 CallSubXactCallbacks(SubXactEvent event,
3107 SubTransactionId mySubid,
3108 SubTransactionId parentSubid)
3110 SubXactCallbackItem *item;
3112 for (item = SubXact_callbacks; item; item = item->next)
3113 (*item->callback) (event, mySubid, parentSubid, item->arg);
3117 /* ----------------------------------------------------------------
3118 * transaction block support
3119 * ----------------------------------------------------------------
3123 * BeginTransactionBlock
3124 * This executes a BEGIN command.
3127 BeginTransactionBlock(void)
3129 TransactionState s = CurrentTransactionState;
3131 switch (s->blockState)
3134 * We are not inside a transaction block, so allow one to begin.
3136 case TBLOCK_STARTED:
3137 s->blockState = TBLOCK_BEGIN;
3141 * Already a transaction block in progress.
3143 case TBLOCK_INPROGRESS:
3144 case TBLOCK_SUBINPROGRESS:
3146 case TBLOCK_SUBABORT:
3148 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3149 errmsg("there is already a transaction in progress")));
3152 /* These cases are invalid. */
3153 case TBLOCK_DEFAULT:
3155 case TBLOCK_SUBBEGIN:
3157 case TBLOCK_SUBRELEASE:
3158 case TBLOCK_SUBCOMMIT:
3159 case TBLOCK_ABORT_END:
3160 case TBLOCK_SUBABORT_END:
3161 case TBLOCK_ABORT_PENDING:
3162 case TBLOCK_SUBABORT_PENDING:
3163 case TBLOCK_SUBRESTART:
3164 case TBLOCK_SUBABORT_RESTART:
3165 case TBLOCK_PREPARE:
3166 elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3167 BlockStateAsString(s->blockState));
3173 * PrepareTransactionBlock
3174 * This executes a PREPARE command.
3176 * Since PREPARE may actually do a ROLLBACK, the result indicates what
3177 * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3179 * Note that we don't actually do anything here except change blockState.
3180 * The real work will be done in the upcoming PrepareTransaction().
3181 * We do it this way because it's not convenient to change memory context,
3182 * resource owner, etc while executing inside a Portal.
3185 PrepareTransactionBlock(char *gid)
3190 /* Set up to commit the current transaction */
3191 result = EndTransactionBlock();
3193 /* If successful, change outer tblock state to PREPARE */
3196 s = CurrentTransactionState;
3198 while (s->parent != NULL)
3201 if (s->blockState == TBLOCK_END)
3203 /* Save GID where PrepareTransaction can find it again */
3204 prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
3206 s->blockState = TBLOCK_PREPARE;
3211 * ignore case where we are not in a transaction;
3212 * EndTransactionBlock already issued a warning.
3214 Assert(s->blockState == TBLOCK_STARTED);
3215 /* Don't send back a PREPARE result tag... */
3224 * EndTransactionBlock
3225 * This executes a COMMIT command.
3227 * Since COMMIT may actually do a ROLLBACK, the result indicates what
3228 * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3230 * Note that we don't actually do anything here except change blockState.
3231 * The real work will be done in the upcoming CommitTransactionCommand().
3232 * We do it this way because it's not convenient to change memory context,
3233 * resource owner, etc while executing inside a Portal.
3236 EndTransactionBlock(void)
3238 TransactionState s = CurrentTransactionState;
3239 bool result = false;
3241 switch (s->blockState)
3244 * We are in a transaction block, so tell CommitTransactionCommand
3247 case TBLOCK_INPROGRESS:
3248 s->blockState = TBLOCK_END;
3253 * We are in a failed transaction block. Tell
3254 * CommitTransactionCommand it's time to exit the block.
3257 s->blockState = TBLOCK_ABORT_END;
3261 * We are in a live subtransaction block. Set up to subcommit all
3262 * open subtransactions and then commit the main transaction.
3264 case TBLOCK_SUBINPROGRESS:
3265 while (s->parent != NULL)
3267 if (s->blockState == TBLOCK_SUBINPROGRESS)
3268 s->blockState = TBLOCK_SUBCOMMIT;
3270 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3271 BlockStateAsString(s->blockState));
3274 if (s->blockState == TBLOCK_INPROGRESS)
3275 s->blockState = TBLOCK_END;
3277 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3278 BlockStateAsString(s->blockState));
3283 * Here we are inside an aborted subtransaction. Treat the COMMIT
3284 * as ROLLBACK: set up to abort everything and exit the main
3287 case TBLOCK_SUBABORT:
3288 while (s->parent != NULL)
3290 if (s->blockState == TBLOCK_SUBINPROGRESS)
3291 s->blockState = TBLOCK_SUBABORT_PENDING;
3292 else if (s->blockState == TBLOCK_SUBABORT)
3293 s->blockState = TBLOCK_SUBABORT_END;
3295 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3296 BlockStateAsString(s->blockState));
3299 if (s->blockState == TBLOCK_INPROGRESS)
3300 s->blockState = TBLOCK_ABORT_PENDING;
3301 else if (s->blockState == TBLOCK_ABORT)
3302 s->blockState = TBLOCK_ABORT_END;
3304 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3305 BlockStateAsString(s->blockState));
3309 * The user issued COMMIT when not inside a transaction. Issue a
3310 * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3311 * CommitTransactionCommand() will then close the transaction and
3312 * put us back into the default state.
3314 case TBLOCK_STARTED:
3316 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3317 errmsg("there is no transaction in progress")));
3321 /* These cases are invalid. */
3322 case TBLOCK_DEFAULT:
3324 case TBLOCK_SUBBEGIN:
3326 case TBLOCK_SUBRELEASE:
3327 case TBLOCK_SUBCOMMIT:
3328 case TBLOCK_ABORT_END:
3329 case TBLOCK_SUBABORT_END:
3330 case TBLOCK_ABORT_PENDING:
3331 case TBLOCK_SUBABORT_PENDING:
3332 case TBLOCK_SUBRESTART:
3333 case TBLOCK_SUBABORT_RESTART:
3334 case TBLOCK_PREPARE:
3335 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3336 BlockStateAsString(s->blockState));
3344 * UserAbortTransactionBlock
3345 * This executes a ROLLBACK command.
3347 * As above, we don't actually do anything here except change blockState.
3350 UserAbortTransactionBlock(void)
3352 TransactionState s = CurrentTransactionState;
3354 switch (s->blockState)
3357 * We are inside a transaction block and we got a ROLLBACK command
3358 * from the user, so tell CommitTransactionCommand to abort and
3359 * exit the transaction block.
3361 case TBLOCK_INPROGRESS:
3362 s->blockState = TBLOCK_ABORT_PENDING;
3366 * We are inside a failed transaction block and we got a ROLLBACK
3367 * command from the user. Abort processing is already done, so
3368 * CommitTransactionCommand just has to cleanup and go back to
3372 s->blockState = TBLOCK_ABORT_END;
3376 * We are inside a subtransaction. Mark everything up to top
3377 * level as exitable.
3379 case TBLOCK_SUBINPROGRESS:
3380 case TBLOCK_SUBABORT:
3381 while (s->parent != NULL)
3383 if (s->blockState == TBLOCK_SUBINPROGRESS)
3384 s->blockState = TBLOCK_SUBABORT_PENDING;
3385 else if (s->blockState == TBLOCK_SUBABORT)
3386 s->blockState = TBLOCK_SUBABORT_END;
3388 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3389 BlockStateAsString(s->blockState));
3392 if (s->blockState == TBLOCK_INPROGRESS)
3393 s->blockState = TBLOCK_ABORT_PENDING;
3394 else if (s->blockState == TBLOCK_ABORT)
3395 s->blockState = TBLOCK_ABORT_END;
3397 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3398 BlockStateAsString(s->blockState));
3402 * The user issued ABORT when not inside a transaction. Issue a
3403 * NOTICE and go to abort state. The upcoming call to
3404 * CommitTransactionCommand() will then put us back into the
3407 case TBLOCK_STARTED:
3409 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3410 errmsg("there is no transaction in progress")));
3411 s->blockState = TBLOCK_ABORT_PENDING;
3414 /* These cases are invalid. */
3415 case TBLOCK_DEFAULT:
3417 case TBLOCK_SUBBEGIN:
3419 case TBLOCK_SUBRELEASE:
3420 case TBLOCK_SUBCOMMIT:
3421 case TBLOCK_ABORT_END:
3422 case TBLOCK_SUBABORT_END:
3423 case TBLOCK_ABORT_PENDING:
3424 case TBLOCK_SUBABORT_PENDING:
3425 case TBLOCK_SUBRESTART:
3426 case TBLOCK_SUBABORT_RESTART:
3427 case TBLOCK_PREPARE:
3428 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3429 BlockStateAsString(s->blockState));
3436 * This executes a SAVEPOINT command.
3439 DefineSavepoint(char *name)
3441 TransactionState s = CurrentTransactionState;
3443 switch (s->blockState)
3445 case TBLOCK_INPROGRESS:
3446 case TBLOCK_SUBINPROGRESS:
3447 /* Normal subtransaction start */
3449 s = CurrentTransactionState; /* changed by push */
3452 * Savepoint names, like the TransactionState block itself, live
3453 * in TopTransactionContext.
3456 s->name = MemoryContextStrdup(TopTransactionContext, name);
3459 /* These cases are invalid. */
3460 case TBLOCK_DEFAULT:
3461 case TBLOCK_STARTED:
3463 case TBLOCK_SUBBEGIN:
3465 case TBLOCK_SUBRELEASE:
3466 case TBLOCK_SUBCOMMIT:
3468 case TBLOCK_SUBABORT:
3469 case TBLOCK_ABORT_END:
3470 case TBLOCK_SUBABORT_END:
3471 case TBLOCK_ABORT_PENDING:
3472 case TBLOCK_SUBABORT_PENDING:
3473 case TBLOCK_SUBRESTART:
3474 case TBLOCK_SUBABORT_RESTART:
3475 case TBLOCK_PREPARE:
3476 elog(FATAL, "DefineSavepoint: unexpected state %s",
3477 BlockStateAsString(s->blockState));
3484 * This executes a RELEASE command.
3486 * As above, we don't actually do anything here except change blockState.
3489 ReleaseSavepoint(List *options)
3491 TransactionState s = CurrentTransactionState;
3492 TransactionState target,
3497 switch (s->blockState)
3500 * We can't rollback to a savepoint if there is no savepoint
3503 case TBLOCK_INPROGRESS:
3505 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3506 errmsg("no such savepoint")));
3510 * We are in a non-aborted subtransaction. This is the only valid
3513 case TBLOCK_SUBINPROGRESS:
3516 /* These cases are invalid. */
3517 case TBLOCK_DEFAULT:
3518 case TBLOCK_STARTED:
3520 case TBLOCK_SUBBEGIN:
3522 case TBLOCK_SUBRELEASE:
3523 case TBLOCK_SUBCOMMIT:
3525 case TBLOCK_SUBABORT:
3526 case TBLOCK_ABORT_END:
3527 case TBLOCK_SUBABORT_END:
3528 case TBLOCK_ABORT_PENDING:
3529 case TBLOCK_SUBABORT_PENDING:
3530 case TBLOCK_SUBRESTART:
3531 case TBLOCK_SUBABORT_RESTART:
3532 case TBLOCK_PREPARE:
3533 elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3534 BlockStateAsString(s->blockState));
3538 foreach(cell, options)
3540 DefElem *elem = lfirst(cell);
3542 if (strcmp(elem->defname, "savepoint_name") == 0)
3543 name = strVal(elem->arg);
3546 Assert(PointerIsValid(name));
3548 for (target = s; PointerIsValid(target); target = target->parent)
3550 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3554 if (!PointerIsValid(target))
3556 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3557 errmsg("no such savepoint")));
3559 /* disallow crossing savepoint level boundaries */
3560 if (target->savepointLevel != s->savepointLevel)
3562 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3563 errmsg("no such savepoint")));
3566 * Mark "commit pending" all subtransactions up to the target
3567 * subtransaction. The actual commits will happen when control gets to
3568 * CommitTransactionCommand.
3570 xact = CurrentTransactionState;
3573 Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
3574 xact->blockState = TBLOCK_SUBRELEASE;
3577 xact = xact->parent;
3578 Assert(PointerIsValid(xact));
3583 * RollbackToSavepoint
3584 * This executes a ROLLBACK TO <savepoint> command.
3586 * As above, we don't actually do anything here except change blockState.
3589 RollbackToSavepoint(List *options)
3591 TransactionState s = CurrentTransactionState;
3592 TransactionState target,
3597 switch (s->blockState)
3600 * We can't rollback to a savepoint if there is no savepoint
3603 case TBLOCK_INPROGRESS:
3606 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3607 errmsg("no such savepoint")));
3611 * There is at least one savepoint, so proceed.
3613 case TBLOCK_SUBINPROGRESS:
3614 case TBLOCK_SUBABORT:
3617 /* These cases are invalid. */
3618 case TBLOCK_DEFAULT:
3619 case TBLOCK_STARTED:
3621 case TBLOCK_SUBBEGIN:
3623 case TBLOCK_SUBRELEASE:
3624 case TBLOCK_SUBCOMMIT:
3625 case TBLOCK_ABORT_END:
3626 case TBLOCK_SUBABORT_END:
3627 case TBLOCK_ABORT_PENDING:
3628 case TBLOCK_SUBABORT_PENDING:
3629 case TBLOCK_SUBRESTART:
3630 case TBLOCK_SUBABORT_RESTART:
3631 case TBLOCK_PREPARE:
3632 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3633 BlockStateAsString(s->blockState));
3637 foreach(cell, options)
3639 DefElem *elem = lfirst(cell);
3641 if (strcmp(elem->defname, "savepoint_name") == 0)
3642 name = strVal(elem->arg);
3645 Assert(PointerIsValid(name));
3647 for (target = s; PointerIsValid(target); target = target->parent)
3649 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3653 if (!PointerIsValid(target))
3655 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3656 errmsg("no such savepoint")));
3658 /* disallow crossing savepoint level boundaries */
3659 if (target->savepointLevel != s->savepointLevel)
3661 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3662 errmsg("no such savepoint")));
3665 * Mark "abort pending" all subtransactions up to the target
3666 * subtransaction. The actual aborts will happen when control gets to
3667 * CommitTransactionCommand.
3669 xact = CurrentTransactionState;
3674 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3675 xact->blockState = TBLOCK_SUBABORT_PENDING;
3676 else if (xact->blockState == TBLOCK_SUBABORT)
3677 xact->blockState = TBLOCK_SUBABORT_END;
3679 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3680 BlockStateAsString(xact->blockState));
3681 xact = xact->parent;
3682 Assert(PointerIsValid(xact));
3685 /* And mark the target as "restart pending" */
3686 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3687 xact->blockState = TBLOCK_SUBRESTART;
3688 else if (xact->blockState == TBLOCK_SUBABORT)
3689 xact->blockState = TBLOCK_SUBABORT_RESTART;
3691 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3692 BlockStateAsString(xact->blockState));
3696 * BeginInternalSubTransaction
3697 * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
3698 * TBLOCK_END, and TBLOCK_PREPARE states, and therefore it can safely be
3699 * used in functions that might be called when not inside a BEGIN block
3700 * or when running deferred triggers at COMMIT/PREPARE time. Also, it
3701 * automatically does CommitTransactionCommand/StartTransactionCommand
3702 * instead of expecting the caller to do it.
3705 BeginInternalSubTransaction(char *name)
3707 TransactionState s = CurrentTransactionState;
3709 switch (s->blockState)
3711 case TBLOCK_STARTED:
3712 case TBLOCK_INPROGRESS:
3714 case TBLOCK_PREPARE:
3715 case TBLOCK_SUBINPROGRESS:
3716 /* Normal subtransaction start */
3718 s = CurrentTransactionState; /* changed by push */
3721 * Savepoint names, like the TransactionState block itself, live
3722 * in TopTransactionContext.
3725 s->name = MemoryContextStrdup(TopTransactionContext, name);
3728 /* These cases are invalid. */
3729 case TBLOCK_DEFAULT:
3731 case TBLOCK_SUBBEGIN:
3732 case TBLOCK_SUBRELEASE:
3733 case TBLOCK_SUBCOMMIT:
3735 case TBLOCK_SUBABORT:
3736 case TBLOCK_ABORT_END:
3737 case TBLOCK_SUBABORT_END:
3738 case TBLOCK_ABORT_PENDING:
3739 case TBLOCK_SUBABORT_PENDING:
3740 case TBLOCK_SUBRESTART:
3741 case TBLOCK_SUBABORT_RESTART:
3742 elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
3743 BlockStateAsString(s->blockState));
3747 CommitTransactionCommand();
3748 StartTransactionCommand();
3752 * ReleaseCurrentSubTransaction
3754 * RELEASE (ie, commit) the innermost subtransaction, regardless of its
3755 * savepoint name (if any).
3756 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3759 ReleaseCurrentSubTransaction(void)
3761 TransactionState s = CurrentTransactionState;
3763 if (s->blockState != TBLOCK_SUBINPROGRESS)
3764 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
3765 BlockStateAsString(s->blockState));
3766 Assert(s->state == TRANS_INPROGRESS);
3767 MemoryContextSwitchTo(CurTransactionContext);
3768 CommitSubTransaction();
3769 s = CurrentTransactionState; /* changed by pop */
3770 Assert(s->state == TRANS_INPROGRESS);
3774 * RollbackAndReleaseCurrentSubTransaction
3776 * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
3777 * of its savepoint name (if any).
3778 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3781 RollbackAndReleaseCurrentSubTransaction(void)
3783 TransactionState s = CurrentTransactionState;
3785 switch (s->blockState)
3787 /* Must be in a subtransaction */
3788 case TBLOCK_SUBINPROGRESS:
3789 case TBLOCK_SUBABORT:
3792 /* These cases are invalid. */
3793 case TBLOCK_DEFAULT:
3794 case TBLOCK_STARTED:
3796 case TBLOCK_SUBBEGIN:
3797 case TBLOCK_INPROGRESS:
3799 case TBLOCK_SUBRELEASE:
3800 case TBLOCK_SUBCOMMIT:
3802 case TBLOCK_ABORT_END:
3803 case TBLOCK_SUBABORT_END:
3804 case TBLOCK_ABORT_PENDING:
3805 case TBLOCK_SUBABORT_PENDING:
3806 case TBLOCK_SUBRESTART:
3807 case TBLOCK_SUBABORT_RESTART:
3808 case TBLOCK_PREPARE:
3809 elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
3810 BlockStateAsString(s->blockState));
3815 * Abort the current subtransaction, if needed.
3817 if (s->blockState == TBLOCK_SUBINPROGRESS)
3818 AbortSubTransaction();
3820 /* And clean it up, too */
3821 CleanupSubTransaction();
3823 s = CurrentTransactionState; /* changed by pop */
3824 AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
3825 s->blockState == TBLOCK_INPROGRESS ||
3826 s->blockState == TBLOCK_STARTED);
3830 * AbortOutOfAnyTransaction
3832 * This routine is provided for error recovery purposes. It aborts any
3833 * active transaction or transaction block, leaving the system in a known
3837 AbortOutOfAnyTransaction(void)
3839 TransactionState s = CurrentTransactionState;
3842 * Get out of any transaction or nested transaction
3846 switch (s->blockState)
3848 case TBLOCK_DEFAULT:
3849 if (s->state == TRANS_DEFAULT)
3851 /* Not in a transaction, do nothing */
3856 * We can get here after an error during transaction start
3857 * (state will be TRANS_START). Need to clean up the
3858 * incompletely started transaction. First, adjust the
3859 * low-level state to suppress warning message from
3862 if (s->state == TRANS_START)
3863 s->state = TRANS_INPROGRESS;
3865 CleanupTransaction();
3868 case TBLOCK_STARTED:
3870 case TBLOCK_INPROGRESS:
3872 case TBLOCK_ABORT_PENDING:
3873 case TBLOCK_PREPARE:
3874 /* In a transaction, so clean up */
3876 CleanupTransaction();
3877 s->blockState = TBLOCK_DEFAULT;
3880 case TBLOCK_ABORT_END:
3881 /* AbortTransaction already done, still need Cleanup */
3882 CleanupTransaction();
3883 s->blockState = TBLOCK_DEFAULT;
3887 * In a subtransaction, so clean it up and abort parent too
3889 case TBLOCK_SUBBEGIN:
3890 case TBLOCK_SUBINPROGRESS:
3891 case TBLOCK_SUBRELEASE:
3892 case TBLOCK_SUBCOMMIT:
3893 case TBLOCK_SUBABORT_PENDING:
3894 case TBLOCK_SUBRESTART:
3895 AbortSubTransaction();
3896 CleanupSubTransaction();
3897 s = CurrentTransactionState; /* changed by pop */
3900 case TBLOCK_SUBABORT:
3901 case TBLOCK_SUBABORT_END:
3902 case TBLOCK_SUBABORT_RESTART:
3903 /* As above, but AbortSubTransaction already done */
3904 CleanupSubTransaction();
3905 s = CurrentTransactionState; /* changed by pop */
3908 } while (s->blockState != TBLOCK_DEFAULT);
3910 /* Should be out of all subxacts now */
3911 Assert(s->parent == NULL);
3915 * IsTransactionBlock --- are we within a transaction block?
3918 IsTransactionBlock(void)
3920 TransactionState s = CurrentTransactionState;
3922 if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3929 * IsTransactionOrTransactionBlock --- are we within either a transaction
3930 * or a transaction block? (The backend is only really "idle" when this
3933 * This should match up with IsTransactionBlock and IsTransactionState.
3936 IsTransactionOrTransactionBlock(void)
3938 TransactionState s = CurrentTransactionState;
3940 if (s->blockState == TBLOCK_DEFAULT)
3947 * TransactionBlockStatusCode - return status code to send in ReadyForQuery
3950 TransactionBlockStatusCode(void)
3952 TransactionState s = CurrentTransactionState;
3954 switch (s->blockState)
3956 case TBLOCK_DEFAULT:
3957 case TBLOCK_STARTED:
3958 return 'I'; /* idle --- not in transaction */
3960 case TBLOCK_SUBBEGIN:
3961 case TBLOCK_INPROGRESS:
3962 case TBLOCK_SUBINPROGRESS:
3964 case TBLOCK_SUBRELEASE:
3965 case TBLOCK_SUBCOMMIT:
3966 case TBLOCK_PREPARE:
3967 return 'T'; /* in transaction */
3969 case TBLOCK_SUBABORT:
3970 case TBLOCK_ABORT_END:
3971 case TBLOCK_SUBABORT_END:
3972 case TBLOCK_ABORT_PENDING:
3973 case TBLOCK_SUBABORT_PENDING:
3974 case TBLOCK_SUBRESTART:
3975 case TBLOCK_SUBABORT_RESTART:
3976 return 'E'; /* in failed transaction */
3979 /* should never get here */
3980 elog(FATAL, "invalid transaction block state: %s",
3981 BlockStateAsString(s->blockState));
3982 return 0; /* keep compiler quiet */
3989 IsSubTransaction(void)
3991 TransactionState s = CurrentTransactionState;
3993 if (s->nestingLevel >= 2)
4000 * StartSubTransaction
4002 * If you're wondering why this is separate from PushTransaction: it's because
4003 * we can't conveniently do this stuff right inside DefineSavepoint. The
4004 * SAVEPOINT utility command will be executed inside a Portal, and if we
4005 * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4006 * the Portal will undo those settings. So we make DefineSavepoint just
4007 * push a dummy transaction block, and when control returns to the main
4008 * idle loop, CommitTransactionCommand will be called, and we'll come here
4009 * to finish starting the subtransaction.
4012 StartSubTransaction(void)
4014 TransactionState s = CurrentTransactionState;
4016 if (s->state != TRANS_DEFAULT)
4017 elog(WARNING, "StartSubTransaction while in %s state",
4018 TransStateAsString(s->state));
4020 s->state = TRANS_START;
4023 * Initialize subsystems for new subtransaction
4025 * must initialize resource-management stuff first
4027 AtSubStart_Memory();
4028 AtSubStart_ResourceOwner();
4030 AtSubStart_Notify();
4031 AfterTriggerBeginSubXact();
4033 s->state = TRANS_INPROGRESS;
4036 * Call start-of-subxact callbacks
4038 CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
4039 s->parent->subTransactionId);
4041 ShowTransactionState("StartSubTransaction");
4045 * CommitSubTransaction
4047 * The caller has to make sure to always reassign CurrentTransactionState
4048 * if it has a local pointer to it after calling this function.
4051 CommitSubTransaction(void)
4053 TransactionState s = CurrentTransactionState;
4055 ShowTransactionState("CommitSubTransaction");
4057 if (s->state != TRANS_INPROGRESS)
4058 elog(WARNING, "CommitSubTransaction while in %s state",
4059 TransStateAsString(s->state));
4061 /* Pre-commit processing goes here -- nothing to do at the moment */
4063 s->state = TRANS_COMMIT;
4065 /* Must CCI to ensure commands of subtransaction are seen as done */
4066 CommandCounterIncrement();
4069 * Prior to 8.4 we marked subcommit in clog at this point. We now only
4070 * perform that step, if required, as part of the atomic update of the
4071 * whole transaction tree at top level commit or abort.
4074 /* Post-commit cleanup */
4075 if (TransactionIdIsValid(s->transactionId))
4076 AtSubCommit_childXids();
4077 AfterTriggerEndSubXact(true);
4078 AtSubCommit_Portals(s->subTransactionId,
4079 s->parent->subTransactionId,
4080 s->parent->curTransactionOwner);
4081 AtEOSubXact_LargeObject(true, s->subTransactionId,
4082 s->parent->subTransactionId);
4083 AtSubCommit_Notify();
4085 CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
4086 s->parent->subTransactionId);
4088 ResourceOwnerRelease(s->curTransactionOwner,
4089 RESOURCE_RELEASE_BEFORE_LOCKS,
4091 AtEOSubXact_RelationCache(true, s->subTransactionId,
4092 s->parent->subTransactionId);
4093 AtEOSubXact_Inval(true);
4097 * The only lock we actually release here is the subtransaction XID lock.
4099 CurrentResourceOwner = s->curTransactionOwner;
4100 if (TransactionIdIsValid(s->transactionId))
4101 XactLockTableDelete(s->transactionId);
4104 * Other locks should get transferred to their parent resource owner.
4106 ResourceOwnerRelease(s->curTransactionOwner,
4107 RESOURCE_RELEASE_LOCKS,
4109 ResourceOwnerRelease(s->curTransactionOwner,
4110 RESOURCE_RELEASE_AFTER_LOCKS,
4113 AtEOXact_GUC(true, s->gucNestLevel);
4114 AtEOSubXact_SPI(true, s->subTransactionId);
4115 AtEOSubXact_on_commit_actions(true, s->subTransactionId,
4116 s->parent->subTransactionId);
4117 AtEOSubXact_Namespace(true, s->subTransactionId,
4118 s->parent->subTransactionId);
4119 AtEOSubXact_Files(true, s->subTransactionId,
4120 s->parent->subTransactionId);
4121 AtEOSubXact_HashTables(true, s->nestingLevel);
4122 AtEOSubXact_PgStat(true, s->nestingLevel);
4123 AtSubCommit_Snapshot(s->nestingLevel);
4126 * We need to restore the upper transaction's read-only state, in case the
4127 * upper is read-write while the child is read-only; GUC will incorrectly
4128 * think it should leave the child state in place.
4130 XactReadOnly = s->prevXactReadOnly;
4132 CurrentResourceOwner = s->parent->curTransactionOwner;
4133 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4134 ResourceOwnerDelete(s->curTransactionOwner);
4135 s->curTransactionOwner = NULL;
4137 AtSubCommit_Memory();
4139 s->state = TRANS_DEFAULT;
4145 * AbortSubTransaction
4148 AbortSubTransaction(void)
4150 TransactionState s = CurrentTransactionState;
4152 /* Prevent cancel/die interrupt while cleaning up */
4155 /* Make sure we have a valid memory context and resource owner */
4156 AtSubAbort_Memory();
4157 AtSubAbort_ResourceOwner();
4160 * Release any LW locks we might be holding as quickly as possible.
4161 * (Regular locks, however, must be held till we finish aborting.)
4162 * Releasing LW locks is critical since we might try to grab them again
4163 * while cleaning up!
4165 * FIXME This may be incorrect --- Are there some locks we should keep?
4166 * Buffer locks, for example? I don't think so but I'm not sure.
4176 * check the current transaction state
4178 ShowTransactionState("AbortSubTransaction");
4180 if (s->state != TRANS_INPROGRESS)
4181 elog(WARNING, "AbortSubTransaction while in %s state",
4182 TransStateAsString(s->state));
4184 s->state = TRANS_ABORT;
4187 * Reset user ID which might have been changed transiently. (See notes in
4188 * AbortTransaction.)
4190 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
4193 * We can skip all this stuff if the subxact failed before creating a
4196 if (s->curTransactionOwner)
4198 AfterTriggerEndSubXact(false);
4199 AtSubAbort_Portals(s->subTransactionId,
4200 s->parent->subTransactionId,
4201 s->parent->curTransactionOwner);
4202 AtEOSubXact_LargeObject(false, s->subTransactionId,
4203 s->parent->subTransactionId);
4204 AtSubAbort_Notify();
4206 /* Advertise the fact that we aborted in pg_clog. */
4207 (void) RecordTransactionAbort(true);
4209 /* Post-abort cleanup */
4210 if (TransactionIdIsValid(s->transactionId))
4211 AtSubAbort_childXids();
4213 CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
4214 s->parent->subTransactionId);
4216 ResourceOwnerRelease(s->curTransactionOwner,
4217 RESOURCE_RELEASE_BEFORE_LOCKS,
4219 AtEOSubXact_RelationCache(false, s->subTransactionId,
4220 s->parent->subTransactionId);
4221 AtEOSubXact_Inval(false);
4222 ResourceOwnerRelease(s->curTransactionOwner,
4223 RESOURCE_RELEASE_LOCKS,
4225 ResourceOwnerRelease(s->curTransactionOwner,
4226 RESOURCE_RELEASE_AFTER_LOCKS,
4230 AtEOXact_GUC(false, s->gucNestLevel);
4231 AtEOSubXact_SPI(false, s->subTransactionId);
4232 AtEOSubXact_on_commit_actions(false, s->subTransactionId,
4233 s->parent->subTransactionId);
4234 AtEOSubXact_Namespace(false, s->subTransactionId,
4235 s->parent->subTransactionId);
4236 AtEOSubXact_Files(false, s->subTransactionId,
4237 s->parent->subTransactionId);
4238 AtEOSubXact_HashTables(false, s->nestingLevel);
4239 AtEOSubXact_PgStat(false, s->nestingLevel);
4240 AtSubAbort_Snapshot(s->nestingLevel);
4244 * Restore the upper transaction's read-only state, too. This should be
4245 * redundant with GUC's cleanup but we may as well do it for consistency
4246 * with the commit case.
4248 XactReadOnly = s->prevXactReadOnly;
4250 RESUME_INTERRUPTS();
4254 * CleanupSubTransaction
4256 * The caller has to make sure to always reassign CurrentTransactionState
4257 * if it has a local pointer to it after calling this function.
4260 CleanupSubTransaction(void)
4262 TransactionState s = CurrentTransactionState;
4264 ShowTransactionState("CleanupSubTransaction");
4266 if (s->state != TRANS_ABORT)
4267 elog(WARNING, "CleanupSubTransaction while in %s state",
4268 TransStateAsString(s->state));
4270 AtSubCleanup_Portals(s->subTransactionId);
4272 CurrentResourceOwner = s->parent->curTransactionOwner;
4273 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4274 if (s->curTransactionOwner)
4275 ResourceOwnerDelete(s->curTransactionOwner);
4276 s->curTransactionOwner = NULL;
4278 AtSubCleanup_Memory();
4280 s->state = TRANS_DEFAULT;
4287 * Create transaction state stack entry for a subtransaction
4289 * The caller has to make sure to always reassign CurrentTransactionState
4290 * if it has a local pointer to it after calling this function.
4293 PushTransaction(void)
4295 TransactionState p = CurrentTransactionState;
4299 * We keep subtransaction state nodes in TopTransactionContext.
4301 s = (TransactionState)
4302 MemoryContextAllocZero(TopTransactionContext,
4303 sizeof(TransactionStateData));
4306 * Assign a subtransaction ID, watching out for counter wraparound.
4308 currentSubTransactionId += 1;
4309 if (currentSubTransactionId == InvalidSubTransactionId)
4311 currentSubTransactionId -= 1;
4314 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4315 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4319 * We can now stack a minimally valid subtransaction without fear of
4322 s->transactionId = InvalidTransactionId; /* until assigned */
4323 s->subTransactionId = currentSubTransactionId;
4325 s->nestingLevel = p->nestingLevel + 1;
4326 s->gucNestLevel = NewGUCNestLevel();
4327 s->savepointLevel = p->savepointLevel;
4328 s->state = TRANS_DEFAULT;
4329 s->blockState = TBLOCK_SUBBEGIN;
4330 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
4331 s->prevXactReadOnly = XactReadOnly;
4333 CurrentTransactionState = s;
4336 * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4337 * with the subtransaction from here on out; in particular they should not
4338 * assume that it necessarily has a transaction context, resource owner,
4345 * Pop back to parent transaction state
4347 * The caller has to make sure to always reassign CurrentTransactionState
4348 * if it has a local pointer to it after calling this function.
4351 PopTransaction(void)
4353 TransactionState s = CurrentTransactionState;
4355 if (s->state != TRANS_DEFAULT)
4356 elog(WARNING, "PopTransaction while in %s state",
4357 TransStateAsString(s->state));
4359 if (s->parent == NULL)
4360 elog(FATAL, "PopTransaction with no parent");
4362 CurrentTransactionState = s->parent;
4364 /* Let's just make sure CurTransactionContext is good */
4365 CurTransactionContext = s->parent->curTransactionContext;
4366 MemoryContextSwitchTo(CurTransactionContext);
4368 /* Ditto for ResourceOwner links */
4369 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4370 CurrentResourceOwner = s->parent->curTransactionOwner;
4372 /* Free the old child structure */
4379 * ShowTransactionState
4383 ShowTransactionState(const char *str)
4385 /* skip work if message will definitely not be printed */
4386 if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4388 elog(DEBUG3, "%s", str);
4389 ShowTransactionStateRec(CurrentTransactionState);
4394 * ShowTransactionStateRec
4395 * Recursive subroutine for ShowTransactionState
4398 ShowTransactionStateRec(TransactionState s)
4402 initStringInfo(&buf);
4404 if (s->nChildXids > 0)
4408 appendStringInfo(&buf, "%u", s->childXids[0]);
4409 for (i = 1; i < s->nChildXids; i++)
4410 appendStringInfo(&buf, " %u", s->childXids[i]);
4414 ShowTransactionStateRec(s->parent);
4416 /* use ereport to suppress computation if msg will not be printed */
4418 (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u%s, nestlvl: %d, children: %s",
4419 PointerIsValid(s->name) ? s->name : "unnamed",
4420 BlockStateAsString(s->blockState),
4421 TransStateAsString(s->state),
4422 (unsigned int) s->transactionId,
4423 (unsigned int) s->subTransactionId,
4424 (unsigned int) currentCommandId,
4425 currentCommandIdUsed ? " (used)" : "",
4426 s->nestingLevel, buf.data)));
4432 * BlockStateAsString
4436 BlockStateAsString(TBlockState blockState)
4440 case TBLOCK_DEFAULT:
4442 case TBLOCK_STARTED:
4446 case TBLOCK_INPROGRESS:
4447 return "INPROGRESS";
4452 case TBLOCK_ABORT_END:
4454 case TBLOCK_ABORT_PENDING:
4455 return "ABORT PEND";
4456 case TBLOCK_PREPARE:
4458 case TBLOCK_SUBBEGIN:
4460 case TBLOCK_SUBINPROGRESS:
4461 return "SUB INPROGRS";
4462 case TBLOCK_SUBRELEASE:
4463 return "SUB RELEASE";
4464 case TBLOCK_SUBCOMMIT:
4465 return "SUB COMMIT";
4466 case TBLOCK_SUBABORT:
4468 case TBLOCK_SUBABORT_END:
4469 return "SUB ABORT END";
4470 case TBLOCK_SUBABORT_PENDING:
4471 return "SUB ABRT PEND";
4472 case TBLOCK_SUBRESTART:
4473 return "SUB RESTART";
4474 case TBLOCK_SUBABORT_RESTART:
4475 return "SUB AB RESTRT";
4477 return "UNRECOGNIZED";
4481 * TransStateAsString
4485 TransStateAsString(TransState state)
4493 case TRANS_INPROGRESS:
4502 return "UNRECOGNIZED";
4506 * xactGetCommittedChildren
4508 * Gets the list of committed children of the current transaction. The return
4509 * value is the number of child transactions. *ptr is set to point to an
4510 * array of TransactionIds. The array is allocated in TopTransactionContext;
4511 * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
4512 * If there are no subxacts, *ptr is set to NULL.
4515 xactGetCommittedChildren(TransactionId **ptr)
4517 TransactionState s = CurrentTransactionState;
4519 if (s->nChildXids == 0)
4522 *ptr = s->childXids;
4524 return s->nChildXids;
4528 * XLOG support routines
4532 * Before 9.0 this was a fairly short function, but now it performs many
4533 * actions for which the order of execution is critical.
4536 xact_redo_commit_internal(TransactionId xid, XLogRecPtr lsn,
4537 TransactionId *sub_xids, int nsubxacts,
4538 SharedInvalidationMessage *inval_msgs, int nmsgs,
4539 RelFileNode *xnodes, int nrels,
4543 TransactionId max_xid;
4546 max_xid = TransactionIdLatest(xid, nsubxacts, sub_xids);
4549 * Make sure nextXid is beyond any XID mentioned in the record.
4551 * We don't expect anyone else to modify nextXid, hence we don't need to
4552 * hold a lock while checking this. We still acquire the lock to modify
4555 if (TransactionIdFollowsOrEquals(max_xid,
4556 ShmemVariableCache->nextXid))
4558 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4559 ShmemVariableCache->nextXid = max_xid;
4560 TransactionIdAdvance(ShmemVariableCache->nextXid);
4561 LWLockRelease(XidGenLock);
4564 if (standbyState == STANDBY_DISABLED)
4567 * Mark the transaction committed in pg_clog.
4569 TransactionIdCommitTree(xid, nsubxacts, sub_xids);
4574 * If a transaction completion record arrives that has as-yet
4575 * unobserved subtransactions then this will not have been fully
4576 * handled by the call to RecordKnownAssignedTransactionIds() in the
4577 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4578 * cover that case. This is confusing and it is easy to think this
4579 * call is irrelevant, which has happened three times in development
4580 * already. Leave it in.
4582 RecordKnownAssignedTransactionIds(max_xid);
4585 * Mark the transaction committed in pg_clog. We use async commit
4586 * protocol during recovery to provide information on database
4587 * consistency for when users try to set hint bits. It is important
4588 * that we do not set hint bits until the minRecoveryPoint is past
4589 * this commit record. This ensures that if we crash we don't see hint
4590 * bits set on changes made by transactions that haven't yet
4591 * recovered. It's unlikely but it's good to be safe.
4593 TransactionIdAsyncCommitTree(xid, nsubxacts, sub_xids, lsn);
4596 * We must mark clog before we update the ProcArray.
4598 ExpireTreeKnownAssignedTransactionIds(xid, nsubxacts, sub_xids, max_xid);
4601 * Send any cache invalidations attached to the commit. We must
4602 * maintain the same order of invalidation then release locks as
4603 * occurs in CommitTransaction().
4605 ProcessCommittedInvalidationMessages(inval_msgs, nmsgs,
4606 XactCompletionRelcacheInitFileInval(xinfo),
4610 * Release locks, if any. We do this for both two phase and normal one
4611 * phase transactions. In effect we are ignoring the prepare phase and
4612 * just going straight to lock release.
4614 StandbyReleaseLockTree(xid, nsubxacts, sub_xids);
4617 /* Make sure files supposed to be dropped are dropped */
4618 for (i = 0; i < nrels; i++)
4620 SMgrRelation srel = smgropen(xnodes[i], InvalidBackendId);
4623 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4624 XLogDropRelation(xnodes[i], fork);
4625 smgrdounlink(srel, true);
4630 * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
4631 * in normal operation. For example, in DROP DATABASE, we delete all the
4632 * files belonging to the database, and then commit the transaction. If we
4633 * crash after all the files have been deleted but before the commit, you
4634 * have an entry in pg_database without any files. To minimize the window
4635 * for that, we use ForceSyncCommit() to rush the commit record to disk as
4636 * quick as possible. We have the same window during recovery, and forcing
4637 * an XLogFlush() (which updates minRecoveryPoint during recovery) helps
4638 * to reduce that problem window, for any user that requested
4639 * ForceSyncCommit().
4641 if (XactCompletionForceSyncCommit(xinfo))
4647 * Utility function to call xact_redo_commit_internal after breaking down xlrec
4650 xact_redo_commit(xl_xact_commit *xlrec,
4651 TransactionId xid, XLogRecPtr lsn)
4653 TransactionId *subxacts;
4654 SharedInvalidationMessage *inval_msgs;
4656 /* subxid array follows relfilenodes */
4657 subxacts = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4658 /* invalidation messages array follows subxids */
4659 inval_msgs = (SharedInvalidationMessage *) &(subxacts[xlrec->nsubxacts]);
4661 xact_redo_commit_internal(xid, lsn, subxacts, xlrec->nsubxacts,
4662 inval_msgs, xlrec->nmsgs,
4663 xlrec->xnodes, xlrec->nrels,
4670 * Utility function to call xact_redo_commit_internal for compact form of message.
4673 xact_redo_commit_compact(xl_xact_commit_compact *xlrec,
4674 TransactionId xid, XLogRecPtr lsn)
4676 xact_redo_commit_internal(xid, lsn, xlrec->subxacts, xlrec->nsubxacts,
4677 NULL, 0, /* inval msgs */
4678 NULL, 0, /* relfilenodes */
4679 InvalidOid, /* dbId */
4680 InvalidOid, /* tsId */
4685 * Be careful with the order of execution, as with xact_redo_commit().
4686 * The two functions are similar but differ in key places.
4688 * Note also that an abort can be for a subtransaction and its children,
4689 * not just for a top level abort. That means we have to consider
4690 * topxid != xid, whereas in commit we would find topxid == xid always
4691 * because subtransaction commit is never WAL logged.
4694 xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
4696 TransactionId *sub_xids;
4697 TransactionId max_xid;
4700 sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4701 max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4704 * Make sure nextXid is beyond any XID mentioned in the record.
4706 * We don't expect anyone else to modify nextXid, hence we don't need to
4707 * hold a lock while checking this. We still acquire the lock to modify
4710 if (TransactionIdFollowsOrEquals(max_xid,
4711 ShmemVariableCache->nextXid))
4713 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4714 ShmemVariableCache->nextXid = max_xid;
4715 TransactionIdAdvance(ShmemVariableCache->nextXid);
4716 LWLockRelease(XidGenLock);
4719 if (standbyState == STANDBY_DISABLED)
4721 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4722 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4727 * If a transaction completion record arrives that has as-yet
4728 * unobserved subtransactions then this will not have been fully
4729 * handled by the call to RecordKnownAssignedTransactionIds() in the
4730 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4731 * cover that case. This is confusing and it is easy to think this
4732 * call is irrelevant, which has happened three times in development
4733 * already. Leave it in.
4735 RecordKnownAssignedTransactionIds(max_xid);
4737 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4738 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4741 * We must update the ProcArray after we have marked clog.
4743 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids, max_xid);
4746 * There are no flat files that need updating, nor invalidation
4747 * messages to send or undo.
4751 * Release locks, if any. There are no invalidations to send.
4753 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4756 /* Make sure files supposed to be dropped are dropped */
4757 for (i = 0; i < xlrec->nrels; i++)
4759 SMgrRelation srel = smgropen(xlrec->xnodes[i], InvalidBackendId);
4762 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4763 XLogDropRelation(xlrec->xnodes[i], fork);
4764 smgrdounlink(srel, true);
4770 xact_redo(XLogRecPtr lsn, XLogRecord *record)
4772 uint8 info = record->xl_info & ~XLR_INFO_MASK;
4774 /* Backup blocks are not used in xact records */
4775 Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
4777 if (info == XLOG_XACT_COMMIT_COMPACT)
4779 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) XLogRecGetData(record);
4781 xact_redo_commit_compact(xlrec, record->xl_xid, lsn);
4783 else if (info == XLOG_XACT_COMMIT)
4785 xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
4787 xact_redo_commit(xlrec, record->xl_xid, lsn);
4789 else if (info == XLOG_XACT_ABORT)
4791 xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4793 xact_redo_abort(xlrec, record->xl_xid);
4795 else if (info == XLOG_XACT_PREPARE)
4797 /* the record contents are exactly the 2PC file */
4798 RecreateTwoPhaseFile(record->xl_xid,
4799 XLogRecGetData(record), record->xl_len);
4801 else if (info == XLOG_XACT_COMMIT_PREPARED)
4803 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4805 xact_redo_commit(&xlrec->crec, xlrec->xid, lsn);
4806 RemoveTwoPhaseFile(xlrec->xid, false);
4808 else if (info == XLOG_XACT_ABORT_PREPARED)
4810 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4812 xact_redo_abort(&xlrec->arec, xlrec->xid);
4813 RemoveTwoPhaseFile(xlrec->xid, false);
4815 else if (info == XLOG_XACT_ASSIGNMENT)
4817 xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
4819 if (standbyState >= STANDBY_INITIALIZED)
4820 ProcArrayApplyXidAssignment(xlrec->xtop,
4821 xlrec->nsubxacts, xlrec->xsub);
4824 elog(PANIC, "xact_redo: unknown op code %u", info);
4828 xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4831 TransactionId *subxacts;
4833 subxacts = (TransactionId *) &xlrec->xnodes[xlrec->nrels];
4835 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4837 if (xlrec->nrels > 0)
4839 appendStringInfo(buf, "; rels:");
4840 for (i = 0; i < xlrec->nrels; i++)
4842 char *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4844 appendStringInfo(buf, " %s", path);
4848 if (xlrec->nsubxacts > 0)
4850 appendStringInfo(buf, "; subxacts:");
4851 for (i = 0; i < xlrec->nsubxacts; i++)
4852 appendStringInfo(buf, " %u", subxacts[i]);
4854 if (xlrec->nmsgs > 0)
4856 SharedInvalidationMessage *msgs;
4858 msgs = (SharedInvalidationMessage *) &subxacts[xlrec->nsubxacts];
4860 if (XactCompletionRelcacheInitFileInval(xlrec->xinfo))
4861 appendStringInfo(buf, "; relcache init file inval dbid %u tsid %u",
4862 xlrec->dbId, xlrec->tsId);
4864 appendStringInfo(buf, "; inval msgs:");
4865 for (i = 0; i < xlrec->nmsgs; i++)
4867 SharedInvalidationMessage *msg = &msgs[i];
4870 appendStringInfo(buf, " catcache %d", msg->id);
4871 else if (msg->id == SHAREDINVALCATALOG_ID)
4872 appendStringInfo(buf, " catalog %u", msg->cat.catId);
4873 else if (msg->id == SHAREDINVALRELCACHE_ID)
4874 appendStringInfo(buf, " relcache %u", msg->rc.relId);
4875 /* remaining cases not expected, but print something anyway */
4876 else if (msg->id == SHAREDINVALSMGR_ID)
4877 appendStringInfo(buf, " smgr");
4878 else if (msg->id == SHAREDINVALRELMAP_ID)
4879 appendStringInfo(buf, " relmap");
4881 appendStringInfo(buf, " unknown id %d", msg->id);
4887 xact_desc_commit_compact(StringInfo buf, xl_xact_commit_compact *xlrec)
4891 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4893 if (xlrec->nsubxacts > 0)
4895 appendStringInfo(buf, "; subxacts:");
4896 for (i = 0; i < xlrec->nsubxacts; i++)
4897 appendStringInfo(buf, " %u", xlrec->subxacts[i]);
4902 xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4906 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4907 if (xlrec->nrels > 0)
4909 appendStringInfo(buf, "; rels:");
4910 for (i = 0; i < xlrec->nrels; i++)
4912 char *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4914 appendStringInfo(buf, " %s", path);
4918 if (xlrec->nsubxacts > 0)
4920 TransactionId *xacts = (TransactionId *)
4921 &xlrec->xnodes[xlrec->nrels];
4923 appendStringInfo(buf, "; subxacts:");
4924 for (i = 0; i < xlrec->nsubxacts; i++)
4925 appendStringInfo(buf, " %u", xacts[i]);
4930 xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
4934 appendStringInfo(buf, "subxacts:");
4936 for (i = 0; i < xlrec->nsubxacts; i++)
4937 appendStringInfo(buf, " %u", xlrec->xsub[i]);
4941 xact_desc(StringInfo buf, uint8 xl_info, char *rec)
4943 uint8 info = xl_info & ~XLR_INFO_MASK;
4945 if (info == XLOG_XACT_COMMIT_COMPACT)
4947 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) rec;
4949 appendStringInfo(buf, "commit: ");
4950 xact_desc_commit_compact(buf, xlrec);
4952 else if (info == XLOG_XACT_COMMIT)
4954 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
4956 appendStringInfo(buf, "commit: ");
4957 xact_desc_commit(buf, xlrec);
4959 else if (info == XLOG_XACT_ABORT)
4961 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
4963 appendStringInfo(buf, "abort: ");
4964 xact_desc_abort(buf, xlrec);
4966 else if (info == XLOG_XACT_PREPARE)
4968 appendStringInfo(buf, "prepare");
4970 else if (info == XLOG_XACT_COMMIT_PREPARED)
4972 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
4974 appendStringInfo(buf, "commit prepared %u: ", xlrec->xid);
4975 xact_desc_commit(buf, &xlrec->crec);
4977 else if (info == XLOG_XACT_ABORT_PREPARED)
4979 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4981 appendStringInfo(buf, "abort prepared %u: ", xlrec->xid);
4982 xact_desc_abort(buf, &xlrec->arec);
4984 else if (info == XLOG_XACT_ASSIGNMENT)
4986 xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
4989 * Note that we ignore the WAL record's xid, since we're more
4990 * interested in the top-level xid that issued the record and which
4991 * xids are being reported here.
4993 appendStringInfo(buf, "xid assignment xtop %u: ", xlrec->xtop);
4994 xact_desc_assignment(buf, xlrec);
4997 appendStringInfo(buf, "UNKNOWN");