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/lmgr.h"
42 #include "storage/predicate.h"
43 #include "storage/procarray.h"
44 #include "storage/sinvaladt.h"
45 #include "storage/smgr.h"
46 #include "utils/combocid.h"
47 #include "utils/guc.h"
48 #include "utils/inval.h"
49 #include "utils/memutils.h"
50 #include "utils/relmapper.h"
51 #include "utils/snapmgr.h"
52 #include "utils/timestamp.h"
57 * User-tweakable parameters
59 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
62 bool DefaultXactReadOnly = false;
65 bool DefaultXactDeferrable = false;
68 int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
70 int CommitDelay = 0; /* precommit delay in microseconds */
71 int CommitSiblings = 5; /* # concurrent xacts needed to sleep */
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;
395 * AssignTransactionId
397 * Assigns a new permanent XID to the given TransactionState.
398 * We do not assign XIDs to transactions until/unless this is called.
399 * Also, any parent TransactionStates that don't yet have XIDs are assigned
400 * one; this maintains the invariant that a child transaction has an XID
401 * following its parent's.
404 AssignTransactionId(TransactionState s)
406 bool isSubXact = (s->parent != NULL);
407 ResourceOwner currentOwner;
409 /* Assert that caller didn't screw up */
410 Assert(!TransactionIdIsValid(s->transactionId));
411 Assert(s->state == TRANS_INPROGRESS);
414 * Ensure parent(s) have XIDs, so that a child always has an XID later
415 * than its parent. Musn't recurse here, or we might get a stack overflow
416 * if we're at the bottom of a huge stack of subtransactions none of which
419 if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
421 TransactionState p = s->parent;
422 TransactionState *parents;
423 size_t parentOffset = 0;
425 parents = palloc(sizeof(TransactionState) * s->nestingLevel);
426 while (p != NULL && !TransactionIdIsValid(p->transactionId))
428 parents[parentOffset++] = p;
433 * This is technically a recursive call, but the recursion will never
434 * be more than one layer deep.
436 while (parentOffset != 0)
437 AssignTransactionId(parents[--parentOffset]);
443 * Generate a new Xid and record it in PG_PROC and pg_subtrans.
445 * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
446 * shared storage other than PG_PROC; because if there's no room for it in
447 * PG_PROC, the subtrans entry is needed to ensure that other backends see
448 * the Xid as "running". See GetNewTransactionId.
450 s->transactionId = GetNewTransactionId(isSubXact);
453 SubTransSetParent(s->transactionId, s->parent->transactionId, false);
456 * If it's a top-level transaction, the predicate locking system needs to
457 * be told about it too.
460 RegisterPredicateLockingXid(s->transactionId);
463 * Acquire lock on the transaction XID. (We assume this cannot block.) We
464 * have to ensure that the lock is assigned to the transaction's own
467 currentOwner = CurrentResourceOwner;
470 CurrentResourceOwner = s->curTransactionOwner;
471 XactLockTableInsert(s->transactionId);
475 /* Ensure CurrentResourceOwner is restored on error */
476 CurrentResourceOwner = currentOwner;
480 CurrentResourceOwner = currentOwner;
483 * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
484 * top-level transaction we issue a WAL record for the assignment. We
485 * include the top-level xid and all the subxids that have not yet been
486 * reported using XLOG_XACT_ASSIGNMENT records.
488 * This is required to limit the amount of shared memory required in a hot
489 * standby server to keep track of in-progress XIDs. See notes for
490 * RecordKnownAssignedTransactionIds().
492 * We don't keep track of the immediate parent of each subxid, only the
493 * top-level transaction that each subxact belongs to. This is correct in
494 * recovery only because aborted subtransactions are separately WAL
497 if (isSubXact && XLogStandbyInfoActive())
499 unreportedXids[nUnreportedXids] = s->transactionId;
503 * ensure this test matches similar one in
504 * RecoverPreparedTransactions()
506 if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS)
508 XLogRecData rdata[2];
509 xl_xact_assignment xlrec;
512 * xtop is always set by now because we recurse up transaction
513 * stack to the highest unassigned xid and then come back down
515 xlrec.xtop = GetTopTransactionId();
516 Assert(TransactionIdIsValid(xlrec.xtop));
517 xlrec.nsubxacts = nUnreportedXids;
519 rdata[0].data = (char *) &xlrec;
520 rdata[0].len = MinSizeOfXactAssignment;
521 rdata[0].buffer = InvalidBuffer;
522 rdata[0].next = &rdata[1];
524 rdata[1].data = (char *) unreportedXids;
525 rdata[1].len = PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId);
526 rdata[1].buffer = InvalidBuffer;
527 rdata[1].next = NULL;
529 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT, rdata);
537 * GetCurrentSubTransactionId
540 GetCurrentSubTransactionId(void)
542 TransactionState s = CurrentTransactionState;
544 return s->subTransactionId;
549 * GetCurrentCommandId
551 * "used" must be TRUE if the caller intends to use the command ID to mark
552 * inserted/updated/deleted tuples. FALSE means the ID is being fetched
553 * for read-only purposes (ie, as a snapshot validity cutoff). See
554 * CommandCounterIncrement() for discussion.
557 GetCurrentCommandId(bool used)
559 /* this is global to a transaction, not subtransaction-local */
561 currentCommandIdUsed = true;
562 return currentCommandId;
566 * GetCurrentTransactionStartTimestamp
569 GetCurrentTransactionStartTimestamp(void)
571 return xactStartTimestamp;
575 * GetCurrentStatementStartTimestamp
578 GetCurrentStatementStartTimestamp(void)
580 return stmtStartTimestamp;
584 * GetCurrentTransactionStopTimestamp
586 * We return current time if the transaction stop time hasn't been set
587 * (which can happen if we decide we don't need to log an XLOG record).
590 GetCurrentTransactionStopTimestamp(void)
592 if (xactStopTimestamp != 0)
593 return xactStopTimestamp;
594 return GetCurrentTimestamp();
598 * SetCurrentStatementStartTimestamp
601 SetCurrentStatementStartTimestamp(void)
603 stmtStartTimestamp = GetCurrentTimestamp();
607 * SetCurrentTransactionStopTimestamp
610 SetCurrentTransactionStopTimestamp(void)
612 xactStopTimestamp = GetCurrentTimestamp();
616 * GetCurrentTransactionNestLevel
618 * Note: this will return zero when not inside any transaction, one when
619 * inside a top-level transaction, etc.
622 GetCurrentTransactionNestLevel(void)
624 TransactionState s = CurrentTransactionState;
626 return s->nestingLevel;
631 * TransactionIdIsCurrentTransactionId
634 TransactionIdIsCurrentTransactionId(TransactionId xid)
639 * We always say that BootstrapTransactionId is "not my transaction ID"
640 * even when it is (ie, during bootstrap). Along with the fact that
641 * transam.c always treats BootstrapTransactionId as already committed,
642 * this causes the tqual.c routines to see all tuples as committed, which
643 * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
644 * it never updates or deletes them, so all tuples can be presumed good
647 * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
648 * not my transaction ID, so we can just return "false" immediately for
649 * any non-normal XID.
651 if (!TransactionIdIsNormal(xid))
655 * We will return true for the Xid of the current subtransaction, any of
656 * its subcommitted children, any of its parents, or any of their
657 * previously subcommitted children. However, a transaction being aborted
658 * is no longer "current", even though it may still have an entry on the
661 for (s = CurrentTransactionState; s != NULL; s = s->parent)
666 if (s->state == TRANS_ABORT)
668 if (!TransactionIdIsValid(s->transactionId))
669 continue; /* it can't have any child XIDs either */
670 if (TransactionIdEquals(xid, s->transactionId))
672 /* As the childXids array is ordered, we can use binary search */
674 high = s->nChildXids - 1;
680 middle = low + (high - low) / 2;
681 probe = s->childXids[middle];
682 if (TransactionIdEquals(probe, xid))
684 else if (TransactionIdPrecedes(probe, xid))
695 * TransactionStartedDuringRecovery
697 * Returns true if the current transaction started while recovery was still
698 * in progress. Recovery might have ended since so RecoveryInProgress() might
699 * return false already.
702 TransactionStartedDuringRecovery(void)
704 return CurrentTransactionState->startedInRecovery;
708 * CommandCounterIncrement
711 CommandCounterIncrement(void)
714 * If the current value of the command counter hasn't been "used" to mark
715 * tuples, we need not increment it, since there's no need to distinguish
716 * a read-only command from others. This helps postpone command counter
717 * overflow, and keeps no-op CommandCounterIncrement operations cheap.
719 if (currentCommandIdUsed)
721 currentCommandId += 1;
722 if (currentCommandId == FirstCommandId) /* check for overflow */
724 currentCommandId -= 1;
726 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
727 errmsg("cannot have more than 2^32-1 commands in a transaction")));
729 currentCommandIdUsed = false;
731 /* Propagate new command ID into static snapshots */
732 SnapshotSetCommandId(currentCommandId);
735 * Make any catalog changes done by the just-completed command visible
736 * in the local syscache. We obviously don't need to do this after a
737 * read-only command. (But see hacks in inval.c to make real sure we
738 * don't think a command that queued inval messages was read-only.)
747 * Interface routine to allow commands to force a synchronous commit of the
748 * current top-level transaction
751 ForceSyncCommit(void)
753 forceSyncCommit = true;
757 /* ----------------------------------------------------------------
758 * StartTransaction stuff
759 * ----------------------------------------------------------------
768 AcceptInvalidationMessages();
777 TransactionState s = CurrentTransactionState;
780 * If this is the first time through, create a private context for
781 * AbortTransaction to work in. By reserving some space now, we can
782 * insulate AbortTransaction from out-of-memory scenarios. Like
783 * ErrorContext, we set it up with slow growth rate and a nonzero minimum
784 * size, so that space will be reserved immediately.
786 if (TransactionAbortContext == NULL)
787 TransactionAbortContext =
788 AllocSetContextCreate(TopMemoryContext,
789 "TransactionAbortContext",
795 * We shouldn't have a transaction context already.
797 Assert(TopTransactionContext == NULL);
800 * Create a toplevel context for the transaction.
802 TopTransactionContext =
803 AllocSetContextCreate(TopMemoryContext,
804 "TopTransactionContext",
805 ALLOCSET_DEFAULT_MINSIZE,
806 ALLOCSET_DEFAULT_INITSIZE,
807 ALLOCSET_DEFAULT_MAXSIZE);
810 * In a top-level transaction, CurTransactionContext is the same as
811 * TopTransactionContext.
813 CurTransactionContext = TopTransactionContext;
814 s->curTransactionContext = CurTransactionContext;
816 /* Make the CurTransactionContext active. */
817 MemoryContextSwitchTo(CurTransactionContext);
821 * AtStart_ResourceOwner
824 AtStart_ResourceOwner(void)
826 TransactionState s = CurrentTransactionState;
829 * We shouldn't have a transaction resource owner already.
831 Assert(TopTransactionResourceOwner == NULL);
834 * Create a toplevel resource owner for the transaction.
836 s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
838 TopTransactionResourceOwner = s->curTransactionOwner;
839 CurTransactionResourceOwner = s->curTransactionOwner;
840 CurrentResourceOwner = s->curTransactionOwner;
843 /* ----------------------------------------------------------------
844 * StartSubTransaction stuff
845 * ----------------------------------------------------------------
852 AtSubStart_Memory(void)
854 TransactionState s = CurrentTransactionState;
856 Assert(CurTransactionContext != NULL);
859 * Create a CurTransactionContext, which will be used to hold data that
860 * survives subtransaction commit but disappears on subtransaction abort.
861 * We make it a child of the immediate parent's CurTransactionContext.
863 CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
864 "CurTransactionContext",
865 ALLOCSET_DEFAULT_MINSIZE,
866 ALLOCSET_DEFAULT_INITSIZE,
867 ALLOCSET_DEFAULT_MAXSIZE);
868 s->curTransactionContext = CurTransactionContext;
870 /* Make the CurTransactionContext active. */
871 MemoryContextSwitchTo(CurTransactionContext);
875 * AtSubStart_ResourceOwner
878 AtSubStart_ResourceOwner(void)
880 TransactionState s = CurrentTransactionState;
882 Assert(s->parent != NULL);
885 * Create a resource owner for the subtransaction. We make it a child of
886 * the immediate parent's resource owner.
888 s->curTransactionOwner =
889 ResourceOwnerCreate(s->parent->curTransactionOwner,
892 CurTransactionResourceOwner = s->curTransactionOwner;
893 CurrentResourceOwner = s->curTransactionOwner;
896 /* ----------------------------------------------------------------
897 * CommitTransaction stuff
898 * ----------------------------------------------------------------
902 * RecordTransactionCommit
904 * Returns latest XID among xact and its children, or InvalidTransactionId
905 * if the xact has no XID. (We compute that here just because it's easier.)
908 RecordTransactionCommit(void)
910 TransactionId xid = GetTopTransactionIdIfAny();
911 bool markXidCommitted = TransactionIdIsValid(xid);
912 TransactionId latestXid = InvalidTransactionId;
916 TransactionId *children;
918 SharedInvalidationMessage *invalMessages = NULL;
919 bool RelcacheInitFileInval = false;
922 /* Get data needed for commit record */
923 nrels = smgrGetPendingDeletes(true, &rels);
924 nchildren = xactGetCommittedChildren(&children);
925 if (XLogStandbyInfoActive())
926 nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
927 &RelcacheInitFileInval);
928 wrote_xlog = (XactLastRecEnd.xrecoff != 0);
931 * If we haven't been assigned an XID yet, we neither can, nor do we want
932 * to write a COMMIT record.
934 if (!markXidCommitted)
937 * We expect that every smgrscheduleunlink is followed by a catalog
938 * update, and hence XID assignment, so we shouldn't get here with any
939 * pending deletes. Use a real test not just an Assert to check this,
940 * since it's a bit fragile.
943 elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
945 /* Can't have child XIDs either; AssignTransactionId enforces this */
946 Assert(nchildren == 0);
949 * If we didn't create XLOG entries, we're done here; otherwise we
950 * should flush those entries the same as a commit record. (An
951 * example of a possible record that wouldn't cause an XID to be
952 * assigned is a sequence advance record due to nextval() --- we want
953 * to flush that to disk before reporting commit.)
961 * Begin commit critical section and insert the commit XLOG record.
963 /* Tell bufmgr and smgr to prepare for commit */
967 * Mark ourselves as within our "commit critical section". This
968 * forces any concurrent checkpoint to wait until we've updated
969 * pg_clog. Without this, it is possible for the checkpoint to set
970 * REDO after the XLOG record but fail to flush the pg_clog update to
971 * disk, leading to loss of the transaction commit if the system
972 * crashes a little later.
974 * Note: we could, but don't bother to, set this flag in
975 * RecordTransactionAbort. That's because loss of a transaction abort
976 * is noncritical; the presumption would be that it aborted, anyway.
978 * It's safe to change the inCommit flag of our own backend without
979 * holding the ProcArrayLock, since we're the only one modifying it.
980 * This makes checkpoint's determination of which xacts are inCommit a
981 * bit fuzzy, but it doesn't matter.
983 START_CRIT_SECTION();
984 MyPgXact->inCommit = true;
986 SetCurrentTransactionStopTimestamp();
989 * Do we need the long commit record? If not, use the compact format.
991 if (nrels > 0 || nmsgs > 0 || RelcacheInitFileInval || forceSyncCommit)
993 XLogRecData rdata[4];
995 xl_xact_commit xlrec;
997 * Set flags required for recovery processing of commits.
1000 if (RelcacheInitFileInval)
1001 xlrec.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
1002 if (forceSyncCommit)
1003 xlrec.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
1005 xlrec.dbId = MyDatabaseId;
1006 xlrec.tsId = MyDatabaseTableSpace;
1008 xlrec.xact_time = xactStopTimestamp;
1009 xlrec.nrels = nrels;
1010 xlrec.nsubxacts = nchildren;
1011 xlrec.nmsgs = nmsgs;
1012 rdata[0].data = (char *) (&xlrec);
1013 rdata[0].len = MinSizeOfXactCommit;
1014 rdata[0].buffer = InvalidBuffer;
1015 /* dump rels to delete */
1018 rdata[0].next = &(rdata[1]);
1019 rdata[1].data = (char *) rels;
1020 rdata[1].len = nrels * sizeof(RelFileNode);
1021 rdata[1].buffer = InvalidBuffer;
1024 /* dump committed child Xids */
1027 rdata[lastrdata].next = &(rdata[2]);
1028 rdata[2].data = (char *) children;
1029 rdata[2].len = nchildren * sizeof(TransactionId);
1030 rdata[2].buffer = InvalidBuffer;
1033 /* dump shared cache invalidation messages */
1036 rdata[lastrdata].next = &(rdata[3]);
1037 rdata[3].data = (char *) invalMessages;
1038 rdata[3].len = nmsgs * sizeof(SharedInvalidationMessage);
1039 rdata[3].buffer = InvalidBuffer;
1042 rdata[lastrdata].next = NULL;
1044 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
1048 XLogRecData rdata[2];
1050 xl_xact_commit_compact xlrec;
1051 xlrec.xact_time = xactStopTimestamp;
1052 xlrec.nsubxacts = nchildren;
1053 rdata[0].data = (char *) (&xlrec);
1054 rdata[0].len = MinSizeOfXactCommitCompact;
1055 rdata[0].buffer = InvalidBuffer;
1056 /* dump committed child Xids */
1059 rdata[0].next = &(rdata[1]);
1060 rdata[1].data = (char *) children;
1061 rdata[1].len = nchildren * sizeof(TransactionId);
1062 rdata[1].buffer = InvalidBuffer;
1065 rdata[lastrdata].next = NULL;
1067 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT_COMPACT, rdata);
1072 * Check if we want to commit asynchronously. We can allow the XLOG flush
1073 * to happen asynchronously if synchronous_commit=off, or if the current
1074 * transaction has not performed any WAL-logged operation. The latter
1075 * case can arise if the current transaction wrote only to temporary
1076 * and/or unlogged tables. In case of a crash, the loss of such a
1077 * transaction will be irrelevant since temp tables will be lost anyway,
1078 * and unlogged tables will be truncated. (Given the foregoing, you might
1079 * think that it would be unnecessary to emit the XLOG record at all in
1080 * this case, but we don't currently try to do that. It would certainly
1081 * cause problems at least in Hot Standby mode, where the
1082 * KnownAssignedXids machinery requires tracking every XID assignment. It
1083 * might be OK to skip it only when wal_level < hot_standby, but for now
1086 * However, if we're doing cleanup of any non-temp rels or committing any
1087 * command that wanted to force sync commit, then we must flush XLOG
1088 * immediately. (We must not allow asynchronous commit if there are any
1089 * non-temp tables to be deleted, because we might delete the files before
1090 * the COMMIT record is flushed to disk. We do allow asynchronous commit
1091 * if all to-be-deleted tables are temporary though, since they are lost
1092 * anyway if we crash.)
1094 if ((wrote_xlog && synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
1095 forceSyncCommit || nrels > 0)
1098 * Synchronous commit case:
1100 * Sleep before flush! So we can flush more than one commit records
1101 * per single fsync. (The idea is some other backend may do the
1102 * XLogFlush while we're sleeping. This needs work still, because on
1103 * most Unixen, the minimum select() delay is 10msec or more, which is
1106 * We do not sleep if enableFsync is not turned on, nor if there are
1107 * fewer than CommitSiblings other backends with active transactions.
1109 if (CommitDelay > 0 && enableFsync &&
1110 MinimumActiveBackends(CommitSiblings))
1111 pg_usleep(CommitDelay);
1113 XLogFlush(XactLastRecEnd);
1116 * Wake up all walsenders to send WAL up to the COMMIT record
1117 * immediately if replication is enabled
1119 if (max_wal_senders > 0)
1123 * Now we may update the CLOG, if we wrote a COMMIT record above
1125 if (markXidCommitted)
1126 TransactionIdCommitTree(xid, nchildren, children);
1131 * Asynchronous commit case:
1133 * This enables possible committed transaction loss in the case of a
1134 * postmaster crash because WAL buffers are left unwritten. Ideally we
1135 * could issue the WAL write without the fsync, but some
1136 * wal_sync_methods do not allow separate write/fsync.
1138 * Report the latest async commit LSN, so that the WAL writer knows to
1139 * flush this commit.
1141 XLogSetAsyncXactLSN(XactLastRecEnd);
1144 * We must not immediately update the CLOG, since we didn't flush the
1145 * XLOG. Instead, we store the LSN up to which the XLOG must be
1146 * flushed before the CLOG may be updated.
1148 if (markXidCommitted)
1149 TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1153 * If we entered a commit critical section, leave it now, and let
1154 * checkpoints proceed.
1156 if (markXidCommitted)
1158 MyPgXact->inCommit = false;
1162 /* Compute latestXid while we have the child XIDs handy */
1163 latestXid = TransactionIdLatest(xid, nchildren, children);
1166 * Wait for synchronous replication, if required.
1168 * Note that at this stage we have marked clog, but still show as running
1169 * in the procarray and continue to hold locks.
1172 SyncRepWaitForLSN(XactLastRecEnd);
1174 /* Reset XactLastRecEnd until the next transaction writes something */
1175 XactLastRecEnd.xrecoff = 0;
1178 /* Clean up local data */
1190 AtCCI_LocalCache(void)
1193 * Make any pending relation map changes visible. We must do this before
1194 * processing local sinval messages, so that the map changes will get
1195 * reflected into the relcache when relcache invals are processed.
1197 AtCCI_RelationMap();
1200 * Make catalog changes visible to me for the next command.
1202 CommandEndInvalidationMessages();
1209 AtCommit_Memory(void)
1212 * Now that we're "out" of a transaction, have the system allocate things
1213 * in the top memory context instead of per-transaction contexts.
1215 MemoryContextSwitchTo(TopMemoryContext);
1218 * Release all transaction-local memory.
1220 Assert(TopTransactionContext != NULL);
1221 MemoryContextDelete(TopTransactionContext);
1222 TopTransactionContext = NULL;
1223 CurTransactionContext = NULL;
1224 CurrentTransactionState->curTransactionContext = NULL;
1227 /* ----------------------------------------------------------------
1228 * CommitSubTransaction stuff
1229 * ----------------------------------------------------------------
1233 * AtSubCommit_Memory
1236 AtSubCommit_Memory(void)
1238 TransactionState s = CurrentTransactionState;
1240 Assert(s->parent != NULL);
1242 /* Return to parent transaction level's memory context. */
1243 CurTransactionContext = s->parent->curTransactionContext;
1244 MemoryContextSwitchTo(CurTransactionContext);
1247 * Ordinarily we cannot throw away the child's CurTransactionContext,
1248 * since the data it contains will be needed at upper commit. However, if
1249 * there isn't actually anything in it, we can throw it away. This avoids
1250 * a small memory leak in the common case of "trivial" subxacts.
1252 if (MemoryContextIsEmpty(s->curTransactionContext))
1254 MemoryContextDelete(s->curTransactionContext);
1255 s->curTransactionContext = NULL;
1260 * AtSubCommit_childXids
1262 * Pass my own XID and my child XIDs up to my parent as committed children.
1265 AtSubCommit_childXids(void)
1267 TransactionState s = CurrentTransactionState;
1270 Assert(s->parent != NULL);
1273 * The parent childXids array will need to hold my XID and all my
1274 * childXids, in addition to the XIDs already there.
1276 new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1278 /* Allocate or enlarge the parent array if necessary */
1279 if (s->parent->maxChildXids < new_nChildXids)
1281 int new_maxChildXids;
1282 TransactionId *new_childXids;
1285 * Make it 2x what's needed right now, to avoid having to enlarge it
1286 * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1287 * is what ensures that we don't need to worry about integer overflow
1288 * here or in the calculation of new_nChildXids.)
1290 new_maxChildXids = Min(new_nChildXids * 2,
1291 (int) (MaxAllocSize / sizeof(TransactionId)));
1293 if (new_maxChildXids < new_nChildXids)
1295 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1296 errmsg("maximum number of committed subtransactions (%d) exceeded",
1297 (int) (MaxAllocSize / sizeof(TransactionId)))));
1300 * We keep the child-XID arrays in TopTransactionContext; this avoids
1301 * setting up child-transaction contexts for what might be just a few
1302 * bytes of grandchild XIDs.
1304 if (s->parent->childXids == NULL)
1306 MemoryContextAlloc(TopTransactionContext,
1307 new_maxChildXids * sizeof(TransactionId));
1309 new_childXids = repalloc(s->parent->childXids,
1310 new_maxChildXids * sizeof(TransactionId));
1312 s->parent->childXids = new_childXids;
1313 s->parent->maxChildXids = new_maxChildXids;
1317 * Copy all my XIDs to parent's array.
1319 * Note: We rely on the fact that the XID of a child always follows that
1320 * of its parent. By copying the XID of this subtransaction before the
1321 * XIDs of its children, we ensure that the array stays ordered. Likewise,
1322 * all XIDs already in the array belong to subtransactions started and
1323 * subcommitted before us, so their XIDs must precede ours.
1325 s->parent->childXids[s->parent->nChildXids] = s->transactionId;
1327 if (s->nChildXids > 0)
1328 memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1330 s->nChildXids * sizeof(TransactionId));
1332 s->parent->nChildXids = new_nChildXids;
1334 /* Release child's array to avoid leakage */
1335 if (s->childXids != NULL)
1336 pfree(s->childXids);
1337 /* We must reset these to avoid double-free if fail later in commit */
1338 s->childXids = NULL;
1340 s->maxChildXids = 0;
1343 /* ----------------------------------------------------------------
1344 * AbortTransaction stuff
1345 * ----------------------------------------------------------------
1349 * RecordTransactionAbort
1351 * Returns latest XID among xact and its children, or InvalidTransactionId
1352 * if the xact has no XID. (We compute that here just because it's easier.)
1354 static TransactionId
1355 RecordTransactionAbort(bool isSubXact)
1357 TransactionId xid = GetCurrentTransactionIdIfAny();
1358 TransactionId latestXid;
1362 TransactionId *children;
1363 XLogRecData rdata[3];
1365 xl_xact_abort xlrec;
1368 * If we haven't been assigned an XID, nobody will care whether we aborted
1369 * or not. Hence, we're done in that case. It does not matter if we have
1370 * rels to delete (note that this routine is not responsible for actually
1371 * deleting 'em). We cannot have any child XIDs, either.
1373 if (!TransactionIdIsValid(xid))
1375 /* Reset XactLastRecEnd until the next transaction writes something */
1377 XactLastRecEnd.xrecoff = 0;
1378 return InvalidTransactionId;
1382 * We have a valid XID, so we should write an ABORT record for it.
1384 * We do not flush XLOG to disk here, since the default assumption after a
1385 * crash would be that we aborted, anyway. For the same reason, we don't
1386 * need to worry about interlocking against checkpoint start.
1390 * Check that we haven't aborted halfway through RecordTransactionCommit.
1392 if (TransactionIdDidCommit(xid))
1393 elog(PANIC, "cannot abort transaction %u, it was already committed",
1396 /* Fetch the data we need for the abort record */
1397 nrels = smgrGetPendingDeletes(false, &rels);
1398 nchildren = xactGetCommittedChildren(&children);
1400 /* XXX do we really need a critical section here? */
1401 START_CRIT_SECTION();
1403 /* Write the ABORT record */
1405 xlrec.xact_time = GetCurrentTimestamp();
1408 SetCurrentTransactionStopTimestamp();
1409 xlrec.xact_time = xactStopTimestamp;
1411 xlrec.nrels = nrels;
1412 xlrec.nsubxacts = nchildren;
1413 rdata[0].data = (char *) (&xlrec);
1414 rdata[0].len = MinSizeOfXactAbort;
1415 rdata[0].buffer = InvalidBuffer;
1416 /* dump rels to delete */
1419 rdata[0].next = &(rdata[1]);
1420 rdata[1].data = (char *) rels;
1421 rdata[1].len = nrels * sizeof(RelFileNode);
1422 rdata[1].buffer = InvalidBuffer;
1425 /* dump committed child Xids */
1428 rdata[lastrdata].next = &(rdata[2]);
1429 rdata[2].data = (char *) children;
1430 rdata[2].len = nchildren * sizeof(TransactionId);
1431 rdata[2].buffer = InvalidBuffer;
1434 rdata[lastrdata].next = NULL;
1436 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);
1439 * Report the latest async abort LSN, so that the WAL writer knows to
1440 * flush this abort. There's nothing to be gained by delaying this, since
1441 * WALWriter may as well do this when it can. This is important with
1442 * streaming replication because if we don't flush WAL regularly we will
1443 * find that large aborts leave us with a long backlog for when commits
1444 * occur after the abort, increasing our window of data loss should
1445 * problems occur at that point.
1448 XLogSetAsyncXactLSN(XactLastRecEnd);
1451 * Mark the transaction aborted in clog. This is not absolutely necessary
1452 * but we may as well do it while we are here; also, in the subxact case
1453 * it is helpful because XactLockTableWait makes use of it to avoid
1454 * waiting for already-aborted subtransactions. It is OK to do it without
1455 * having flushed the ABORT record to disk, because in event of a crash
1456 * we'd be assumed to have aborted anyway.
1458 TransactionIdAbortTree(xid, nchildren, children);
1462 /* Compute latestXid while we have the child XIDs handy */
1463 latestXid = TransactionIdLatest(xid, nchildren, children);
1466 * If we're aborting a subtransaction, we can immediately remove failed
1467 * XIDs from PGPROC's cache of running child XIDs. We do that here for
1468 * subxacts, because we already have the child XID array at hand. For
1469 * main xacts, the equivalent happens just after this function returns.
1472 XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1474 /* Reset XactLastRecEnd until the next transaction writes something */
1476 XactLastRecEnd.xrecoff = 0;
1478 /* And clean up local data */
1489 AtAbort_Memory(void)
1492 * Switch into TransactionAbortContext, which should have some free space
1493 * even if nothing else does. We'll work in this context until we've
1494 * finished cleaning up.
1496 * It is barely possible to get here when we've not been able to create
1497 * TransactionAbortContext yet; if so use TopMemoryContext.
1499 if (TransactionAbortContext != NULL)
1500 MemoryContextSwitchTo(TransactionAbortContext);
1502 MemoryContextSwitchTo(TopMemoryContext);
1509 AtSubAbort_Memory(void)
1511 Assert(TransactionAbortContext != NULL);
1513 MemoryContextSwitchTo(TransactionAbortContext);
1518 * AtAbort_ResourceOwner
1521 AtAbort_ResourceOwner(void)
1524 * Make sure we have a valid ResourceOwner, if possible (else it will be
1525 * NULL, which is OK)
1527 CurrentResourceOwner = TopTransactionResourceOwner;
1531 * AtSubAbort_ResourceOwner
1534 AtSubAbort_ResourceOwner(void)
1536 TransactionState s = CurrentTransactionState;
1538 /* Make sure we have a valid ResourceOwner */
1539 CurrentResourceOwner = s->curTransactionOwner;
1544 * AtSubAbort_childXids
1547 AtSubAbort_childXids(void)
1549 TransactionState s = CurrentTransactionState;
1552 * We keep the child-XID arrays in TopTransactionContext (see
1553 * AtSubCommit_childXids). This means we'd better free the array
1554 * explicitly at abort to avoid leakage.
1556 if (s->childXids != NULL)
1557 pfree(s->childXids);
1558 s->childXids = NULL;
1560 s->maxChildXids = 0;
1563 * We could prune the unreportedXids array here. But we don't bother. That
1564 * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1565 * would likely introduce more CPU time into the more common paths, so we
1566 * choose not to do that.
1570 /* ----------------------------------------------------------------
1571 * CleanupTransaction stuff
1572 * ----------------------------------------------------------------
1579 AtCleanup_Memory(void)
1581 Assert(CurrentTransactionState->parent == NULL);
1584 * Now that we're "out" of a transaction, have the system allocate things
1585 * in the top memory context instead of per-transaction contexts.
1587 MemoryContextSwitchTo(TopMemoryContext);
1590 * Clear the special abort context for next time.
1592 if (TransactionAbortContext != NULL)
1593 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1596 * Release all transaction-local memory.
1598 if (TopTransactionContext != NULL)
1599 MemoryContextDelete(TopTransactionContext);
1600 TopTransactionContext = NULL;
1601 CurTransactionContext = NULL;
1602 CurrentTransactionState->curTransactionContext = NULL;
1606 /* ----------------------------------------------------------------
1607 * CleanupSubTransaction stuff
1608 * ----------------------------------------------------------------
1612 * AtSubCleanup_Memory
1615 AtSubCleanup_Memory(void)
1617 TransactionState s = CurrentTransactionState;
1619 Assert(s->parent != NULL);
1621 /* Make sure we're not in an about-to-be-deleted context */
1622 MemoryContextSwitchTo(s->parent->curTransactionContext);
1623 CurTransactionContext = s->parent->curTransactionContext;
1626 * Clear the special abort context for next time.
1628 if (TransactionAbortContext != NULL)
1629 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1632 * Delete the subxact local memory contexts. Its CurTransactionContext can
1633 * go too (note this also kills CurTransactionContexts from any children
1636 if (s->curTransactionContext)
1637 MemoryContextDelete(s->curTransactionContext);
1638 s->curTransactionContext = NULL;
1641 /* ----------------------------------------------------------------
1642 * interface routines
1643 * ----------------------------------------------------------------
1650 StartTransaction(void)
1653 VirtualTransactionId vxid;
1656 * Let's just make sure the state stack is empty
1658 s = &TopTransactionStateData;
1659 CurrentTransactionState = s;
1662 * check the current transaction state
1664 if (s->state != TRANS_DEFAULT)
1665 elog(WARNING, "StartTransaction while in %s state",
1666 TransStateAsString(s->state));
1669 * set the current transaction state information appropriately during
1672 s->state = TRANS_START;
1673 s->transactionId = InvalidTransactionId; /* until assigned */
1676 * Make sure we've reset xact state variables
1678 * If recovery is still in progress, mark this transaction as read-only.
1679 * We have lower level defences in XLogInsert and elsewhere to stop us
1680 * from modifying data during recovery, but this gives the normal
1681 * indication to the user that the transaction is read-only.
1683 if (RecoveryInProgress())
1685 s->startedInRecovery = true;
1686 XactReadOnly = true;
1690 s->startedInRecovery = false;
1691 XactReadOnly = DefaultXactReadOnly;
1693 XactDeferrable = DefaultXactDeferrable;
1694 XactIsoLevel = DefaultXactIsoLevel;
1695 forceSyncCommit = false;
1696 MyXactAccessedTempRel = false;
1699 * reinitialize within-transaction counters
1701 s->subTransactionId = TopSubTransactionId;
1702 currentSubTransactionId = TopSubTransactionId;
1703 currentCommandId = FirstCommandId;
1704 currentCommandIdUsed = false;
1707 * initialize reported xid accounting
1709 nUnreportedXids = 0;
1712 * must initialize resource-management stuff first
1715 AtStart_ResourceOwner();
1718 * Assign a new LocalTransactionId, and combine it with the backendId to
1719 * form a virtual transaction id.
1721 vxid.backendId = MyBackendId;
1722 vxid.localTransactionId = GetNextLocalTransactionId();
1725 * Lock the virtual transaction id before we announce it in the proc array
1727 VirtualXactLockTableInsert(vxid);
1730 * Advertise it in the proc array. We assume assignment of
1731 * LocalTransactionID is atomic, and the backendId should be set already.
1733 Assert(MyProc->backendId == vxid.backendId);
1734 MyProc->lxid = vxid.localTransactionId;
1736 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1739 * set transaction_timestamp() (a/k/a now()). We want this to be the same
1740 * as the first command's statement_timestamp(), so don't do a fresh
1741 * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1742 * xactStopTimestamp as unset.
1744 xactStartTimestamp = stmtStartTimestamp;
1745 xactStopTimestamp = 0;
1746 pgstat_report_xact_timestamp(xactStartTimestamp);
1749 * initialize current transaction state fields
1751 * note: prevXactReadOnly is not used at the outermost level
1753 s->nestingLevel = 1;
1754 s->gucNestLevel = 1;
1755 s->childXids = NULL;
1757 s->maxChildXids = 0;
1758 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1759 /* SecurityRestrictionContext should never be set outside a transaction */
1760 Assert(s->prevSecContext == 0);
1763 * initialize other subsystems for new transaction
1768 AfterTriggerBeginXact();
1771 * done with start processing, set current transaction state to "in
1774 s->state = TRANS_INPROGRESS;
1776 ShowTransactionState("StartTransaction");
1783 * NB: if you change this routine, better look at PrepareTransaction too!
1786 CommitTransaction(void)
1788 TransactionState s = CurrentTransactionState;
1789 TransactionId latestXid;
1791 ShowTransactionState("CommitTransaction");
1794 * check the current transaction state
1796 if (s->state != TRANS_INPROGRESS)
1797 elog(WARNING, "CommitTransaction while in %s state",
1798 TransStateAsString(s->state));
1799 Assert(s->parent == NULL);
1802 * Do pre-commit processing that involves calling user-defined code, such
1803 * as triggers. Since closing cursors could queue trigger actions,
1804 * triggers could open cursors, etc, we have to keep looping until there's
1805 * nothing left to do.
1810 * Fire all currently pending deferred triggers.
1812 AfterTriggerFireDeferred();
1815 * Close open portals (converting holdable ones into static portals).
1816 * If there weren't any, we are done ... otherwise loop back to check
1817 * if they queued deferred triggers. Lather, rinse, repeat.
1819 if (!PreCommit_Portals(false))
1824 * The remaining actions cannot call any user-defined code, so it's safe
1825 * to start shutting down within-transaction services. But note that most
1826 * of this stuff could still throw an error, which would switch us into
1827 * the transaction-abort path.
1830 /* Shut down the deferred-trigger manager */
1831 AfterTriggerEndXact(true);
1834 * Let ON COMMIT management do its thing (must happen after closing
1835 * cursors, to avoid dangling-reference problems)
1837 PreCommit_on_commit_actions();
1839 /* close large objects before lower-level cleanup */
1840 AtEOXact_LargeObject(true);
1843 * Mark serializable transaction as complete for predicate locking
1844 * purposes. This should be done as late as we can put it and still allow
1845 * errors to be raised for failure patterns found at commit.
1847 PreCommit_CheckForSerializationFailure();
1850 * Insert notifications sent by NOTIFY commands into the queue. This
1851 * should be late in the pre-commit sequence to minimize time spent
1852 * holding the notify-insertion lock.
1856 /* Prevent cancel/die interrupt while cleaning up */
1859 /* Commit updates to the relation map --- do this as late as possible */
1860 AtEOXact_RelationMap(true);
1863 * set the current transaction state information appropriately during
1866 s->state = TRANS_COMMIT;
1869 * Here is where we really truly commit.
1871 latestXid = RecordTransactionCommit();
1873 TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
1876 * Let others know about no transaction in progress by me. Note that this
1877 * must be done _before_ releasing locks we hold and _after_
1878 * RecordTransactionCommit.
1880 ProcArrayEndTransaction(MyProc, latestXid);
1883 * This is all post-commit cleanup. Note that if an error is raised here,
1884 * it's too late to abort the transaction. This should be just
1885 * noncritical resource releasing.
1887 * The ordering of operations is not entirely random. The idea is:
1888 * release resources visible to other backends (eg, files, buffer pins);
1889 * then release locks; then release backend-local resources. We want to
1890 * release locks at the point where any backend waiting for us will see
1891 * our transaction as being fully cleaned up.
1893 * Resources that can be associated with individual queries are handled by
1894 * the ResourceOwner mechanism. The other calls here are for backend-wide
1898 CallXactCallbacks(XACT_EVENT_COMMIT);
1900 ResourceOwnerRelease(TopTransactionResourceOwner,
1901 RESOURCE_RELEASE_BEFORE_LOCKS,
1904 /* Check we've released all buffer pins */
1905 AtEOXact_Buffers(true);
1907 /* Clean up the relation cache */
1908 AtEOXact_RelationCache(true);
1911 * Make catalog changes visible to all backends. This has to happen after
1912 * relcache references are dropped (see comments for
1913 * AtEOXact_RelationCache), but before locks are released (if anyone is
1914 * waiting for lock on a relation we've modified, we want them to know
1915 * about the catalog change before they start using the relation).
1917 AtEOXact_Inval(true);
1920 * Likewise, dropping of files deleted during the transaction is best done
1921 * after releasing relcache and buffer pins. (This is not strictly
1922 * necessary during commit, since such pins should have been released
1923 * already, but this ordering is definitely critical during abort.)
1925 smgrDoPendingDeletes(true);
1927 AtEOXact_MultiXact();
1929 ResourceOwnerRelease(TopTransactionResourceOwner,
1930 RESOURCE_RELEASE_LOCKS,
1932 ResourceOwnerRelease(TopTransactionResourceOwner,
1933 RESOURCE_RELEASE_AFTER_LOCKS,
1936 /* Check we've released all catcache entries */
1937 AtEOXact_CatCache(true);
1940 AtEOXact_GUC(true, 1);
1942 AtEOXact_on_commit_actions(true);
1943 AtEOXact_Namespace(true);
1944 /* smgrcommit already done */
1946 AtEOXact_ComboCid();
1947 AtEOXact_HashTables(true);
1948 AtEOXact_PgStat(true);
1949 AtEOXact_Snapshot(true);
1950 pgstat_report_xact_timestamp(0);
1952 CurrentResourceOwner = NULL;
1953 ResourceOwnerDelete(TopTransactionResourceOwner);
1954 s->curTransactionOwner = NULL;
1955 CurTransactionResourceOwner = NULL;
1956 TopTransactionResourceOwner = NULL;
1960 s->transactionId = InvalidTransactionId;
1961 s->subTransactionId = InvalidSubTransactionId;
1962 s->nestingLevel = 0;
1963 s->gucNestLevel = 0;
1964 s->childXids = NULL;
1966 s->maxChildXids = 0;
1969 * done with commit processing, set current transaction state back to
1972 s->state = TRANS_DEFAULT;
1974 RESUME_INTERRUPTS();
1979 * PrepareTransaction
1981 * NB: if you change this routine, better look at CommitTransaction too!
1984 PrepareTransaction(void)
1986 TransactionState s = CurrentTransactionState;
1987 TransactionId xid = GetCurrentTransactionId();
1988 GlobalTransaction gxact;
1989 TimestampTz prepared_at;
1991 ShowTransactionState("PrepareTransaction");
1994 * check the current transaction state
1996 if (s->state != TRANS_INPROGRESS)
1997 elog(WARNING, "PrepareTransaction while in %s state",
1998 TransStateAsString(s->state));
1999 Assert(s->parent == NULL);
2002 * Do pre-commit processing that involves calling user-defined code, such
2003 * as triggers. Since closing cursors could queue trigger actions,
2004 * triggers could open cursors, etc, we have to keep looping until there's
2005 * nothing left to do.
2010 * Fire all currently pending deferred triggers.
2012 AfterTriggerFireDeferred();
2015 * Close open portals (converting holdable ones into static portals).
2016 * If there weren't any, we are done ... otherwise loop back to check
2017 * if they queued deferred triggers. Lather, rinse, repeat.
2019 if (!PreCommit_Portals(true))
2024 * The remaining actions cannot call any user-defined code, so it's safe
2025 * to start shutting down within-transaction services. But note that most
2026 * of this stuff could still throw an error, which would switch us into
2027 * the transaction-abort path.
2030 /* Shut down the deferred-trigger manager */
2031 AfterTriggerEndXact(true);
2034 * Let ON COMMIT management do its thing (must happen after closing
2035 * cursors, to avoid dangling-reference problems)
2037 PreCommit_on_commit_actions();
2039 /* close large objects before lower-level cleanup */
2040 AtEOXact_LargeObject(true);
2043 * Mark serializable transaction as complete for predicate locking
2044 * purposes. This should be done as late as we can put it and still allow
2045 * errors to be raised for failure patterns found at commit.
2047 PreCommit_CheckForSerializationFailure();
2049 /* NOTIFY will be handled below */
2052 * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2053 * this transaction. Having the prepared xact hold locks on another
2054 * backend's temp table seems a bad idea --- for instance it would prevent
2055 * the backend from exiting. There are other problems too, such as how to
2056 * clean up the source backend's local buffers and ON COMMIT state if the
2057 * prepared xact includes a DROP of a temp table.
2059 * We must check this after executing any ON COMMIT actions, because they
2060 * might still access a temp relation.
2062 * XXX In principle this could be relaxed to allow some useful special
2063 * cases, such as a temp table created and dropped all within the
2064 * transaction. That seems to require much more bookkeeping though.
2066 if (MyXactAccessedTempRel)
2068 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2069 errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2072 * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2073 * supported if we added cleanup logic to twophase.c, but for now it
2074 * doesn't seem worth the trouble.
2076 if (XactHasExportedSnapshots())
2078 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2079 errmsg("cannot PREPARE a transaction that has exported snapshots")));
2081 /* Prevent cancel/die interrupt while cleaning up */
2085 * set the current transaction state information appropriately during
2086 * prepare processing
2088 s->state = TRANS_PREPARE;
2090 prepared_at = GetCurrentTimestamp();
2092 /* Tell bufmgr and smgr to prepare for commit */
2096 * Reserve the GID for this transaction. This could fail if the requested
2097 * GID is invalid or already in use.
2099 gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2100 GetUserId(), MyDatabaseId);
2104 * Collect data for the 2PC state file. Note that in general, no actual
2105 * state change should happen in the called modules during this step,
2106 * since it's still possible to fail before commit, and in that case we
2107 * want transaction abort to be able to clean up. (In particular, the
2108 * AtPrepare routines may error out if they find cases they cannot
2109 * handle.) State cleanup should happen in the PostPrepare routines
2110 * below. However, some modules can go ahead and clear state here because
2111 * they wouldn't do anything with it during abort anyway.
2113 * Note: because the 2PC state file records will be replayed in the same
2114 * order they are made, the order of these calls has to match the order in
2115 * which we want things to happen during COMMIT PREPARED or ROLLBACK
2116 * PREPARED; in particular, pay attention to whether things should happen
2117 * before or after releasing the transaction's locks.
2119 StartPrepare(gxact);
2123 AtPrepare_PredicateLocks();
2125 AtPrepare_MultiXact();
2126 AtPrepare_RelationMap();
2129 * Here is where we really truly prepare.
2131 * We have to record transaction prepares even if we didn't make any
2132 * updates, because the transaction manager might get confused if we lose
2133 * a global transaction.
2138 * Now we clean up backend-internal state and release internal resources.
2141 /* Reset XactLastRecEnd until the next transaction writes something */
2142 XactLastRecEnd.xrecoff = 0;
2145 * Let others know about no transaction in progress by me. This has to be
2146 * done *after* the prepared transaction has been marked valid, else
2147 * someone may think it is unlocked and recyclable.
2149 ProcArrayClearTransaction(MyProc);
2152 * This is all post-transaction cleanup. Note that if an error is raised
2153 * here, it's too late to abort the transaction. This should be just
2154 * noncritical resource releasing. See notes in CommitTransaction.
2157 CallXactCallbacks(XACT_EVENT_PREPARE);
2159 ResourceOwnerRelease(TopTransactionResourceOwner,
2160 RESOURCE_RELEASE_BEFORE_LOCKS,
2163 /* Check we've released all buffer pins */
2164 AtEOXact_Buffers(true);
2166 /* Clean up the relation cache */
2167 AtEOXact_RelationCache(true);
2169 /* notify doesn't need a postprepare call */
2171 PostPrepare_PgStat();
2173 PostPrepare_Inval();
2177 PostPrepare_MultiXact(xid);
2179 PostPrepare_Locks(xid);
2180 PostPrepare_PredicateLocks(xid);
2182 ResourceOwnerRelease(TopTransactionResourceOwner,
2183 RESOURCE_RELEASE_LOCKS,
2185 ResourceOwnerRelease(TopTransactionResourceOwner,
2186 RESOURCE_RELEASE_AFTER_LOCKS,
2189 /* Check we've released all catcache entries */
2190 AtEOXact_CatCache(true);
2192 /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2193 AtEOXact_GUC(true, 1);
2195 AtEOXact_on_commit_actions(true);
2196 AtEOXact_Namespace(true);
2197 /* smgrcommit already done */
2199 AtEOXact_ComboCid();
2200 AtEOXact_HashTables(true);
2201 /* don't call AtEOXact_PgStat here */
2202 AtEOXact_Snapshot(true);
2204 CurrentResourceOwner = NULL;
2205 ResourceOwnerDelete(TopTransactionResourceOwner);
2206 s->curTransactionOwner = NULL;
2207 CurTransactionResourceOwner = NULL;
2208 TopTransactionResourceOwner = NULL;
2212 s->transactionId = InvalidTransactionId;
2213 s->subTransactionId = InvalidSubTransactionId;
2214 s->nestingLevel = 0;
2215 s->gucNestLevel = 0;
2216 s->childXids = NULL;
2218 s->maxChildXids = 0;
2221 * done with 1st phase commit processing, set current transaction state
2224 s->state = TRANS_DEFAULT;
2226 RESUME_INTERRUPTS();
2234 AbortTransaction(void)
2236 TransactionState s = CurrentTransactionState;
2237 TransactionId latestXid;
2239 /* Prevent cancel/die interrupt while cleaning up */
2242 /* Make sure we have a valid memory context and resource owner */
2244 AtAbort_ResourceOwner();
2247 * Release any LW locks we might be holding as quickly as possible.
2248 * (Regular locks, however, must be held till we finish aborting.)
2249 * Releasing LW locks is critical since we might try to grab them again
2250 * while cleaning up!
2254 /* Clean up buffer I/O and buffer context locks, too */
2259 * Also clean up any open wait for lock, since the lock manager will choke
2260 * if we try to wait for another lock before doing this.
2265 * check the current transaction state
2267 if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2268 elog(WARNING, "AbortTransaction while in %s state",
2269 TransStateAsString(s->state));
2270 Assert(s->parent == NULL);
2273 * set the current transaction state information appropriately during the
2276 s->state = TRANS_ABORT;
2279 * Reset user ID which might have been changed transiently. We need this
2280 * to clean up in case control escaped out of a SECURITY DEFINER function
2281 * or other local change of CurrentUserId; therefore, the prior value of
2282 * SecurityRestrictionContext also needs to be restored.
2284 * (Note: it is not necessary to restore session authorization or role
2285 * settings here because those can only be changed via GUC, and GUC will
2286 * take care of rolling them back if need be.)
2288 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2291 * do abort processing
2293 AfterTriggerEndXact(false); /* 'false' means it's abort */
2295 AtEOXact_LargeObject(false);
2297 AtEOXact_RelationMap(false);
2300 * Advertise the fact that we aborted in pg_clog (assuming that we got as
2301 * far as assigning an XID to advertise).
2303 latestXid = RecordTransactionAbort(false);
2305 TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2308 * Let others know about no transaction in progress by me. Note that this
2309 * must be done _before_ releasing locks we hold and _after_
2310 * RecordTransactionAbort.
2312 ProcArrayEndTransaction(MyProc, latestXid);
2315 * Post-abort cleanup. See notes in CommitTransaction() concerning
2316 * ordering. We can skip all of it if the transaction failed before
2317 * creating a resource owner.
2319 if (TopTransactionResourceOwner != NULL)
2321 CallXactCallbacks(XACT_EVENT_ABORT);
2323 ResourceOwnerRelease(TopTransactionResourceOwner,
2324 RESOURCE_RELEASE_BEFORE_LOCKS,
2326 AtEOXact_Buffers(false);
2327 AtEOXact_RelationCache(false);
2328 AtEOXact_Inval(false);
2329 smgrDoPendingDeletes(false);
2330 AtEOXact_MultiXact();
2331 ResourceOwnerRelease(TopTransactionResourceOwner,
2332 RESOURCE_RELEASE_LOCKS,
2334 ResourceOwnerRelease(TopTransactionResourceOwner,
2335 RESOURCE_RELEASE_AFTER_LOCKS,
2337 AtEOXact_CatCache(false);
2339 AtEOXact_GUC(false, 1);
2340 AtEOXact_SPI(false);
2341 AtEOXact_on_commit_actions(false);
2342 AtEOXact_Namespace(false);
2344 AtEOXact_ComboCid();
2345 AtEOXact_HashTables(false);
2346 AtEOXact_PgStat(false);
2347 pgstat_report_xact_timestamp(0);
2351 * State remains TRANS_ABORT until CleanupTransaction().
2353 RESUME_INTERRUPTS();
2357 * CleanupTransaction
2360 CleanupTransaction(void)
2362 TransactionState s = CurrentTransactionState;
2365 * State should still be TRANS_ABORT from AbortTransaction().
2367 if (s->state != TRANS_ABORT)
2368 elog(FATAL, "CleanupTransaction: unexpected state %s",
2369 TransStateAsString(s->state));
2372 * do abort cleanup processing
2374 AtCleanup_Portals(); /* now safe to release portal memory */
2375 AtEOXact_Snapshot(false); /* and release the transaction's snapshots */
2377 CurrentResourceOwner = NULL; /* and resource owner */
2378 if (TopTransactionResourceOwner)
2379 ResourceOwnerDelete(TopTransactionResourceOwner);
2380 s->curTransactionOwner = NULL;
2381 CurTransactionResourceOwner = NULL;
2382 TopTransactionResourceOwner = NULL;
2384 AtCleanup_Memory(); /* and transaction memory */
2386 s->transactionId = InvalidTransactionId;
2387 s->subTransactionId = InvalidSubTransactionId;
2388 s->nestingLevel = 0;
2389 s->gucNestLevel = 0;
2390 s->childXids = NULL;
2392 s->maxChildXids = 0;
2395 * done with abort processing, set current transaction state back to
2398 s->state = TRANS_DEFAULT;
2402 * StartTransactionCommand
2405 StartTransactionCommand(void)
2407 TransactionState s = CurrentTransactionState;
2409 switch (s->blockState)
2412 * if we aren't in a transaction block, we just do our usual start
2415 case TBLOCK_DEFAULT:
2417 s->blockState = TBLOCK_STARTED;
2421 * We are somewhere in a transaction block or subtransaction and
2422 * about to start a new command. For now we do nothing, but
2423 * someday we may do command-local resource initialization. (Note
2424 * that any needed CommandCounterIncrement was done by the
2425 * previous CommitTransactionCommand.)
2427 case TBLOCK_INPROGRESS:
2428 case TBLOCK_SUBINPROGRESS:
2432 * Here we are in a failed transaction block (one of the commands
2433 * caused an abort) so we do nothing but remain in the abort
2434 * state. Eventually we will get a ROLLBACK command which will
2435 * get us out of this state. (It is up to other code to ensure
2436 * that no commands other than ROLLBACK will be processed in these
2440 case TBLOCK_SUBABORT:
2443 /* These cases are invalid. */
2444 case TBLOCK_STARTED:
2446 case TBLOCK_SUBBEGIN:
2448 case TBLOCK_SUBRELEASE:
2449 case TBLOCK_SUBCOMMIT:
2450 case TBLOCK_ABORT_END:
2451 case TBLOCK_SUBABORT_END:
2452 case TBLOCK_ABORT_PENDING:
2453 case TBLOCK_SUBABORT_PENDING:
2454 case TBLOCK_SUBRESTART:
2455 case TBLOCK_SUBABORT_RESTART:
2456 case TBLOCK_PREPARE:
2457 elog(ERROR, "StartTransactionCommand: unexpected state %s",
2458 BlockStateAsString(s->blockState));
2463 * We must switch to CurTransactionContext before returning. This is
2464 * already done if we called StartTransaction, otherwise not.
2466 Assert(CurTransactionContext != NULL);
2467 MemoryContextSwitchTo(CurTransactionContext);
2471 * CommitTransactionCommand
2474 CommitTransactionCommand(void)
2476 TransactionState s = CurrentTransactionState;
2478 switch (s->blockState)
2481 * This shouldn't happen, because it means the previous
2482 * StartTransactionCommand didn't set the STARTED state
2485 case TBLOCK_DEFAULT:
2486 elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2487 BlockStateAsString(s->blockState));
2491 * If we aren't in a transaction block, just do our usual
2492 * transaction commit, and return to the idle state.
2494 case TBLOCK_STARTED:
2495 CommitTransaction();
2496 s->blockState = TBLOCK_DEFAULT;
2500 * We are completing a "BEGIN TRANSACTION" command, so we change
2501 * to the "transaction block in progress" state and return. (We
2502 * assume the BEGIN did nothing to the database, so we need no
2503 * CommandCounterIncrement.)
2506 s->blockState = TBLOCK_INPROGRESS;
2510 * This is the case when we have finished executing a command
2511 * someplace within a transaction block. We increment the command
2512 * counter and return.
2514 case TBLOCK_INPROGRESS:
2515 case TBLOCK_SUBINPROGRESS:
2516 CommandCounterIncrement();
2520 * We are completing a "COMMIT" command. Do it and return to the
2524 CommitTransaction();
2525 s->blockState = TBLOCK_DEFAULT;
2529 * Here we are in the middle of a transaction block but one of the
2530 * commands caused an abort so we do nothing but remain in the
2531 * abort state. Eventually we will get a ROLLBACK comand.
2534 case TBLOCK_SUBABORT:
2538 * Here we were in an aborted transaction block and we just got
2539 * the ROLLBACK command from the user, so clean up the
2540 * already-aborted transaction and return to the idle state.
2542 case TBLOCK_ABORT_END:
2543 CleanupTransaction();
2544 s->blockState = TBLOCK_DEFAULT;
2548 * Here we were in a perfectly good transaction block but the user
2549 * told us to ROLLBACK anyway. We have to abort the transaction
2550 * and then clean up.
2552 case TBLOCK_ABORT_PENDING:
2554 CleanupTransaction();
2555 s->blockState = TBLOCK_DEFAULT;
2559 * We are completing a "PREPARE TRANSACTION" command. Do it and
2560 * return to the idle state.
2562 case TBLOCK_PREPARE:
2563 PrepareTransaction();
2564 s->blockState = TBLOCK_DEFAULT;
2568 * We were just issued a SAVEPOINT inside a transaction block.
2569 * Start a subtransaction. (DefineSavepoint already did
2570 * PushTransaction, so as to have someplace to put the SUBBEGIN
2573 case TBLOCK_SUBBEGIN:
2574 StartSubTransaction();
2575 s->blockState = TBLOCK_SUBINPROGRESS;
2579 * We were issued a RELEASE command, so we end the
2580 * current subtransaction and return to the parent transaction.
2581 * The parent might be ended too, so repeat till we find an
2582 * INPROGRESS transaction or subtransaction.
2584 case TBLOCK_SUBRELEASE:
2587 CommitSubTransaction();
2588 s = CurrentTransactionState; /* changed by pop */
2589 } while (s->blockState == TBLOCK_SUBRELEASE);
2591 Assert(s->blockState == TBLOCK_INPROGRESS ||
2592 s->blockState == TBLOCK_SUBINPROGRESS);
2596 * We were issued a COMMIT, so we end the current subtransaction
2597 * hierarchy and perform final commit. We do this by rolling up
2598 * any subtransactions into their parent, which leads to O(N^2)
2599 * operations with respect to resource owners - this isn't that
2600 * bad until we approach a thousands of savepoints but is necessary
2601 * for correctness should after triggers create new resource
2604 case TBLOCK_SUBCOMMIT:
2607 CommitSubTransaction();
2608 s = CurrentTransactionState; /* changed by pop */
2609 } while (s->blockState == TBLOCK_SUBCOMMIT);
2610 /* If we had a COMMIT command, finish off the main xact too */
2611 if (s->blockState == TBLOCK_END)
2613 Assert(s->parent == NULL);
2614 CommitTransaction();
2615 s->blockState = TBLOCK_DEFAULT;
2617 else if (s->blockState == TBLOCK_PREPARE)
2619 Assert(s->parent == NULL);
2620 PrepareTransaction();
2621 s->blockState = TBLOCK_DEFAULT;
2624 elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2625 BlockStateAsString(s->blockState));
2629 * The current already-failed subtransaction is ending due to a
2630 * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2631 * examine the parent (which could be in any of several states).
2633 case TBLOCK_SUBABORT_END:
2634 CleanupSubTransaction();
2635 CommitTransactionCommand();
2639 * As above, but it's not dead yet, so abort first.
2641 case TBLOCK_SUBABORT_PENDING:
2642 AbortSubTransaction();
2643 CleanupSubTransaction();
2644 CommitTransactionCommand();
2648 * The current subtransaction is the target of a ROLLBACK TO
2649 * command. Abort and pop it, then start a new subtransaction
2650 * with the same name.
2652 case TBLOCK_SUBRESTART:
2657 /* save name and keep Cleanup from freeing it */
2660 savepointLevel = s->savepointLevel;
2662 AbortSubTransaction();
2663 CleanupSubTransaction();
2665 DefineSavepoint(NULL);
2666 s = CurrentTransactionState; /* changed by push */
2668 s->savepointLevel = savepointLevel;
2670 /* This is the same as TBLOCK_SUBBEGIN case */
2671 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2672 StartSubTransaction();
2673 s->blockState = TBLOCK_SUBINPROGRESS;
2678 * Same as above, but the subtransaction had already failed, so we
2679 * don't need AbortSubTransaction.
2681 case TBLOCK_SUBABORT_RESTART:
2686 /* save name and keep Cleanup from freeing it */
2689 savepointLevel = s->savepointLevel;
2691 CleanupSubTransaction();
2693 DefineSavepoint(NULL);
2694 s = CurrentTransactionState; /* changed by push */
2696 s->savepointLevel = savepointLevel;
2698 /* This is the same as TBLOCK_SUBBEGIN case */
2699 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2700 StartSubTransaction();
2701 s->blockState = TBLOCK_SUBINPROGRESS;
2708 * AbortCurrentTransaction
2711 AbortCurrentTransaction(void)
2713 TransactionState s = CurrentTransactionState;
2715 switch (s->blockState)
2717 case TBLOCK_DEFAULT:
2718 if (s->state == TRANS_DEFAULT)
2720 /* we are idle, so nothing to do */
2725 * We can get here after an error during transaction start
2726 * (state will be TRANS_START). Need to clean up the
2727 * incompletely started transaction. First, adjust the
2728 * low-level state to suppress warning message from
2731 if (s->state == TRANS_START)
2732 s->state = TRANS_INPROGRESS;
2734 CleanupTransaction();
2739 * if we aren't in a transaction block, we just do the basic abort
2740 * & cleanup transaction.
2742 case TBLOCK_STARTED:
2744 CleanupTransaction();
2745 s->blockState = TBLOCK_DEFAULT;
2749 * If we are in TBLOCK_BEGIN it means something screwed up right
2750 * after reading "BEGIN TRANSACTION". We assume that the user
2751 * will interpret the error as meaning the BEGIN failed to get him
2752 * into a transaction block, so we should abort and return to idle
2757 CleanupTransaction();
2758 s->blockState = TBLOCK_DEFAULT;
2762 * We are somewhere in a transaction block and we've gotten a
2763 * failure, so we abort the transaction and set up the persistent
2764 * ABORT state. We will stay in ABORT until we get a ROLLBACK.
2766 case TBLOCK_INPROGRESS:
2768 s->blockState = TBLOCK_ABORT;
2769 /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2773 * Here, we failed while trying to COMMIT. Clean up the
2774 * transaction and return to idle state (we do not want to stay in
2779 CleanupTransaction();
2780 s->blockState = TBLOCK_DEFAULT;
2784 * Here, we are already in an aborted transaction state and are
2785 * waiting for a ROLLBACK, but for some reason we failed again! So
2786 * we just remain in the abort state.
2789 case TBLOCK_SUBABORT:
2793 * We are in a failed transaction and we got the ROLLBACK command.
2794 * We have already aborted, we just need to cleanup and go to idle
2797 case TBLOCK_ABORT_END:
2798 CleanupTransaction();
2799 s->blockState = TBLOCK_DEFAULT;
2803 * We are in a live transaction and we got a ROLLBACK command.
2804 * Abort, cleanup, go to idle state.
2806 case TBLOCK_ABORT_PENDING:
2808 CleanupTransaction();
2809 s->blockState = TBLOCK_DEFAULT;
2813 * Here, we failed while trying to PREPARE. Clean up the
2814 * transaction and return to idle state (we do not want to stay in
2817 case TBLOCK_PREPARE:
2819 CleanupTransaction();
2820 s->blockState = TBLOCK_DEFAULT;
2824 * We got an error inside a subtransaction. Abort just the
2825 * subtransaction, and go to the persistent SUBABORT state until
2828 case TBLOCK_SUBINPROGRESS:
2829 AbortSubTransaction();
2830 s->blockState = TBLOCK_SUBABORT;
2834 * If we failed while trying to create a subtransaction, clean up
2835 * the broken subtransaction and abort the parent. The same
2836 * applies if we get a failure while ending a subtransaction.
2838 case TBLOCK_SUBBEGIN:
2839 case TBLOCK_SUBRELEASE:
2840 case TBLOCK_SUBCOMMIT:
2841 case TBLOCK_SUBABORT_PENDING:
2842 case TBLOCK_SUBRESTART:
2843 AbortSubTransaction();
2844 CleanupSubTransaction();
2845 AbortCurrentTransaction();
2849 * Same as above, except the Abort() was already done.
2851 case TBLOCK_SUBABORT_END:
2852 case TBLOCK_SUBABORT_RESTART:
2853 CleanupSubTransaction();
2854 AbortCurrentTransaction();
2860 * PreventTransactionChain
2862 * This routine is to be called by statements that must not run inside
2863 * a transaction block, typically because they have non-rollback-able
2864 * side effects or do internal commits.
2866 * If we have already started a transaction block, issue an error; also issue
2867 * an error if we appear to be running inside a user-defined function (which
2868 * could issue more commands and possibly cause a failure after the statement
2869 * completes). Subtransactions are verboten too.
2871 * isTopLevel: passed down from ProcessUtility to determine whether we are
2872 * inside a function or multi-query querystring. (We will always fail if
2873 * this is false, but it's convenient to centralize the check here instead of
2874 * making callers do it.)
2875 * stmtType: statement type name, for error messages.
2878 PreventTransactionChain(bool isTopLevel, const char *stmtType)
2881 * xact block already started?
2883 if (IsTransactionBlock())
2885 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2886 /* translator: %s represents an SQL statement name */
2887 errmsg("%s cannot run inside a transaction block",
2893 if (IsSubTransaction())
2895 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2896 /* translator: %s represents an SQL statement name */
2897 errmsg("%s cannot run inside a subtransaction",
2901 * inside a function call?
2905 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2906 /* translator: %s represents an SQL statement name */
2907 errmsg("%s cannot be executed from a function or multi-command string",
2910 /* If we got past IsTransactionBlock test, should be in default state */
2911 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2912 CurrentTransactionState->blockState != TBLOCK_STARTED)
2913 elog(FATAL, "cannot prevent transaction chain");
2918 * RequireTransactionChain
2920 * This routine is to be called by statements that must run inside
2921 * a transaction block, because they have no effects that persist past
2922 * transaction end (and so calling them outside a transaction block
2923 * is presumably an error). DECLARE CURSOR is an example.
2925 * If we appear to be running inside a user-defined function, we do not
2926 * issue an error, since the function could issue more commands that make
2927 * use of the current statement's results. Likewise subtransactions.
2928 * Thus this is an inverse for PreventTransactionChain.
2930 * isTopLevel: passed down from ProcessUtility to determine whether we are
2931 * inside a function.
2932 * stmtType: statement type name, for error messages.
2935 RequireTransactionChain(bool isTopLevel, const char *stmtType)
2938 * xact block already started?
2940 if (IsTransactionBlock())
2946 if (IsSubTransaction())
2950 * inside a function call?
2956 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
2957 /* translator: %s represents an SQL statement name */
2958 errmsg("%s can only be used in transaction blocks",
2963 * IsInTransactionChain
2965 * This routine is for statements that need to behave differently inside
2966 * a transaction block than when running as single commands. ANALYZE is
2967 * currently the only example.
2969 * isTopLevel: passed down from ProcessUtility to determine whether we are
2970 * inside a function.
2973 IsInTransactionChain(bool isTopLevel)
2976 * Return true on same conditions that would make PreventTransactionChain
2979 if (IsTransactionBlock())
2982 if (IsSubTransaction())
2988 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2989 CurrentTransactionState->blockState != TBLOCK_STARTED)
2997 * Register or deregister callback functions for start- and end-of-xact
3000 * These functions are intended for use by dynamically loaded modules.
3001 * For built-in modules we generally just hardwire the appropriate calls
3002 * (mainly because it's easier to control the order that way, where needed).
3004 * At transaction end, the callback occurs post-commit or post-abort, so the
3005 * callback functions can only do noncritical cleanup.
3008 RegisterXactCallback(XactCallback callback, void *arg)
3010 XactCallbackItem *item;
3012 item = (XactCallbackItem *)
3013 MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
3014 item->callback = callback;
3016 item->next = Xact_callbacks;
3017 Xact_callbacks = item;
3021 UnregisterXactCallback(XactCallback callback, void *arg)
3023 XactCallbackItem *item;
3024 XactCallbackItem *prev;
3027 for (item = Xact_callbacks; item; prev = item, item = item->next)
3029 if (item->callback == callback && item->arg == arg)
3032 prev->next = item->next;
3034 Xact_callbacks = item->next;
3042 CallXactCallbacks(XactEvent event)
3044 XactCallbackItem *item;
3046 for (item = Xact_callbacks; item; item = item->next)
3047 (*item->callback) (event, item->arg);
3052 * Register or deregister callback functions for start- and end-of-subxact
3055 * Pretty much same as above, but for subtransaction events.
3057 * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3058 * so the callback functions can only do noncritical cleanup. At
3059 * subtransaction start, the callback is called when the subtransaction has
3060 * finished initializing.
3063 RegisterSubXactCallback(SubXactCallback callback, void *arg)
3065 SubXactCallbackItem *item;
3067 item = (SubXactCallbackItem *)
3068 MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
3069 item->callback = callback;
3071 item->next = SubXact_callbacks;
3072 SubXact_callbacks = item;
3076 UnregisterSubXactCallback(SubXactCallback callback, void *arg)
3078 SubXactCallbackItem *item;
3079 SubXactCallbackItem *prev;
3082 for (item = SubXact_callbacks; item; prev = item, item = item->next)
3084 if (item->callback == callback && item->arg == arg)
3087 prev->next = item->next;
3089 SubXact_callbacks = item->next;
3097 CallSubXactCallbacks(SubXactEvent event,
3098 SubTransactionId mySubid,
3099 SubTransactionId parentSubid)
3101 SubXactCallbackItem *item;
3103 for (item = SubXact_callbacks; item; item = item->next)
3104 (*item->callback) (event, mySubid, parentSubid, item->arg);
3108 /* ----------------------------------------------------------------
3109 * transaction block support
3110 * ----------------------------------------------------------------
3114 * BeginTransactionBlock
3115 * This executes a BEGIN command.
3118 BeginTransactionBlock(void)
3120 TransactionState s = CurrentTransactionState;
3122 switch (s->blockState)
3125 * We are not inside a transaction block, so allow one to begin.
3127 case TBLOCK_STARTED:
3128 s->blockState = TBLOCK_BEGIN;
3132 * Already a transaction block in progress.
3134 case TBLOCK_INPROGRESS:
3135 case TBLOCK_SUBINPROGRESS:
3137 case TBLOCK_SUBABORT:
3139 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3140 errmsg("there is already a transaction in progress")));
3143 /* These cases are invalid. */
3144 case TBLOCK_DEFAULT:
3146 case TBLOCK_SUBBEGIN:
3148 case TBLOCK_SUBRELEASE:
3149 case TBLOCK_SUBCOMMIT:
3150 case TBLOCK_ABORT_END:
3151 case TBLOCK_SUBABORT_END:
3152 case TBLOCK_ABORT_PENDING:
3153 case TBLOCK_SUBABORT_PENDING:
3154 case TBLOCK_SUBRESTART:
3155 case TBLOCK_SUBABORT_RESTART:
3156 case TBLOCK_PREPARE:
3157 elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3158 BlockStateAsString(s->blockState));
3164 * PrepareTransactionBlock
3165 * This executes a PREPARE command.
3167 * Since PREPARE may actually do a ROLLBACK, the result indicates what
3168 * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3170 * Note that we don't actually do anything here except change blockState.
3171 * The real work will be done in the upcoming PrepareTransaction().
3172 * We do it this way because it's not convenient to change memory context,
3173 * resource owner, etc while executing inside a Portal.
3176 PrepareTransactionBlock(char *gid)
3181 /* Set up to commit the current transaction */
3182 result = EndTransactionBlock();
3184 /* If successful, change outer tblock state to PREPARE */
3187 s = CurrentTransactionState;
3189 while (s->parent != NULL)
3192 if (s->blockState == TBLOCK_END)
3194 /* Save GID where PrepareTransaction can find it again */
3195 prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
3197 s->blockState = TBLOCK_PREPARE;
3202 * ignore case where we are not in a transaction;
3203 * EndTransactionBlock already issued a warning.
3205 Assert(s->blockState == TBLOCK_STARTED);
3206 /* Don't send back a PREPARE result tag... */
3215 * EndTransactionBlock
3216 * This executes a COMMIT command.
3218 * Since COMMIT may actually do a ROLLBACK, the result indicates what
3219 * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3221 * Note that we don't actually do anything here except change blockState.
3222 * The real work will be done in the upcoming CommitTransactionCommand().
3223 * We do it this way because it's not convenient to change memory context,
3224 * resource owner, etc while executing inside a Portal.
3227 EndTransactionBlock(void)
3229 TransactionState s = CurrentTransactionState;
3230 bool result = false;
3232 switch (s->blockState)
3235 * We are in a transaction block, so tell CommitTransactionCommand
3238 case TBLOCK_INPROGRESS:
3239 s->blockState = TBLOCK_END;
3244 * We are in a failed transaction block. Tell
3245 * CommitTransactionCommand it's time to exit the block.
3248 s->blockState = TBLOCK_ABORT_END;
3252 * We are in a live subtransaction block. Set up to subcommit all
3253 * open subtransactions and then commit the main transaction.
3255 case TBLOCK_SUBINPROGRESS:
3256 while (s->parent != NULL)
3258 if (s->blockState == TBLOCK_SUBINPROGRESS)
3259 s->blockState = TBLOCK_SUBCOMMIT;
3261 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3262 BlockStateAsString(s->blockState));
3265 if (s->blockState == TBLOCK_INPROGRESS)
3266 s->blockState = TBLOCK_END;
3268 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3269 BlockStateAsString(s->blockState));
3274 * Here we are inside an aborted subtransaction. Treat the COMMIT
3275 * as ROLLBACK: set up to abort everything and exit the main
3278 case TBLOCK_SUBABORT:
3279 while (s->parent != NULL)
3281 if (s->blockState == TBLOCK_SUBINPROGRESS)
3282 s->blockState = TBLOCK_SUBABORT_PENDING;
3283 else if (s->blockState == TBLOCK_SUBABORT)
3284 s->blockState = TBLOCK_SUBABORT_END;
3286 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3287 BlockStateAsString(s->blockState));
3290 if (s->blockState == TBLOCK_INPROGRESS)
3291 s->blockState = TBLOCK_ABORT_PENDING;
3292 else if (s->blockState == TBLOCK_ABORT)
3293 s->blockState = TBLOCK_ABORT_END;
3295 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3296 BlockStateAsString(s->blockState));
3300 * The user issued COMMIT when not inside a transaction. Issue a
3301 * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3302 * CommitTransactionCommand() will then close the transaction and
3303 * put us back into the default state.
3305 case TBLOCK_STARTED:
3307 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3308 errmsg("there is no transaction in progress")));
3312 /* These cases are invalid. */
3313 case TBLOCK_DEFAULT:
3315 case TBLOCK_SUBBEGIN:
3317 case TBLOCK_SUBRELEASE:
3318 case TBLOCK_SUBCOMMIT:
3319 case TBLOCK_ABORT_END:
3320 case TBLOCK_SUBABORT_END:
3321 case TBLOCK_ABORT_PENDING:
3322 case TBLOCK_SUBABORT_PENDING:
3323 case TBLOCK_SUBRESTART:
3324 case TBLOCK_SUBABORT_RESTART:
3325 case TBLOCK_PREPARE:
3326 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3327 BlockStateAsString(s->blockState));
3335 * UserAbortTransactionBlock
3336 * This executes a ROLLBACK command.
3338 * As above, we don't actually do anything here except change blockState.
3341 UserAbortTransactionBlock(void)
3343 TransactionState s = CurrentTransactionState;
3345 switch (s->blockState)
3348 * We are inside a transaction block and we got a ROLLBACK command
3349 * from the user, so tell CommitTransactionCommand to abort and
3350 * exit the transaction block.
3352 case TBLOCK_INPROGRESS:
3353 s->blockState = TBLOCK_ABORT_PENDING;
3357 * We are inside a failed transaction block and we got a ROLLBACK
3358 * command from the user. Abort processing is already done, so
3359 * CommitTransactionCommand just has to cleanup and go back to
3363 s->blockState = TBLOCK_ABORT_END;
3367 * We are inside a subtransaction. Mark everything up to top
3368 * level as exitable.
3370 case TBLOCK_SUBINPROGRESS:
3371 case TBLOCK_SUBABORT:
3372 while (s->parent != NULL)
3374 if (s->blockState == TBLOCK_SUBINPROGRESS)
3375 s->blockState = TBLOCK_SUBABORT_PENDING;
3376 else if (s->blockState == TBLOCK_SUBABORT)
3377 s->blockState = TBLOCK_SUBABORT_END;
3379 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3380 BlockStateAsString(s->blockState));
3383 if (s->blockState == TBLOCK_INPROGRESS)
3384 s->blockState = TBLOCK_ABORT_PENDING;
3385 else if (s->blockState == TBLOCK_ABORT)
3386 s->blockState = TBLOCK_ABORT_END;
3388 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3389 BlockStateAsString(s->blockState));
3393 * The user issued ABORT when not inside a transaction. Issue a
3394 * WARNING and go to abort state. The upcoming call to
3395 * CommitTransactionCommand() will then put us back into the
3398 case TBLOCK_STARTED:
3400 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3401 errmsg("there is no transaction in progress")));
3402 s->blockState = TBLOCK_ABORT_PENDING;
3405 /* These cases are invalid. */
3406 case TBLOCK_DEFAULT:
3408 case TBLOCK_SUBBEGIN:
3410 case TBLOCK_SUBRELEASE:
3411 case TBLOCK_SUBCOMMIT:
3412 case TBLOCK_ABORT_END:
3413 case TBLOCK_SUBABORT_END:
3414 case TBLOCK_ABORT_PENDING:
3415 case TBLOCK_SUBABORT_PENDING:
3416 case TBLOCK_SUBRESTART:
3417 case TBLOCK_SUBABORT_RESTART:
3418 case TBLOCK_PREPARE:
3419 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3420 BlockStateAsString(s->blockState));
3427 * This executes a SAVEPOINT command.
3430 DefineSavepoint(char *name)
3432 TransactionState s = CurrentTransactionState;
3434 switch (s->blockState)
3436 case TBLOCK_INPROGRESS:
3437 case TBLOCK_SUBINPROGRESS:
3438 /* Normal subtransaction start */
3440 s = CurrentTransactionState; /* changed by push */
3443 * Savepoint names, like the TransactionState block itself, live
3444 * in TopTransactionContext.
3447 s->name = MemoryContextStrdup(TopTransactionContext, name);
3450 /* These cases are invalid. */
3451 case TBLOCK_DEFAULT:
3452 case TBLOCK_STARTED:
3454 case TBLOCK_SUBBEGIN:
3456 case TBLOCK_SUBRELEASE:
3457 case TBLOCK_SUBCOMMIT:
3459 case TBLOCK_SUBABORT:
3460 case TBLOCK_ABORT_END:
3461 case TBLOCK_SUBABORT_END:
3462 case TBLOCK_ABORT_PENDING:
3463 case TBLOCK_SUBABORT_PENDING:
3464 case TBLOCK_SUBRESTART:
3465 case TBLOCK_SUBABORT_RESTART:
3466 case TBLOCK_PREPARE:
3467 elog(FATAL, "DefineSavepoint: unexpected state %s",
3468 BlockStateAsString(s->blockState));
3475 * This executes a RELEASE command.
3477 * As above, we don't actually do anything here except change blockState.
3480 ReleaseSavepoint(List *options)
3482 TransactionState s = CurrentTransactionState;
3483 TransactionState target,
3488 switch (s->blockState)
3491 * We can't rollback to a savepoint if there is no savepoint
3494 case TBLOCK_INPROGRESS:
3496 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3497 errmsg("no such savepoint")));
3501 * We are in a non-aborted subtransaction. This is the only valid
3504 case TBLOCK_SUBINPROGRESS:
3507 /* These cases are invalid. */
3508 case TBLOCK_DEFAULT:
3509 case TBLOCK_STARTED:
3511 case TBLOCK_SUBBEGIN:
3513 case TBLOCK_SUBRELEASE:
3514 case TBLOCK_SUBCOMMIT:
3516 case TBLOCK_SUBABORT:
3517 case TBLOCK_ABORT_END:
3518 case TBLOCK_SUBABORT_END:
3519 case TBLOCK_ABORT_PENDING:
3520 case TBLOCK_SUBABORT_PENDING:
3521 case TBLOCK_SUBRESTART:
3522 case TBLOCK_SUBABORT_RESTART:
3523 case TBLOCK_PREPARE:
3524 elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3525 BlockStateAsString(s->blockState));
3529 foreach(cell, options)
3531 DefElem *elem = lfirst(cell);
3533 if (strcmp(elem->defname, "savepoint_name") == 0)
3534 name = strVal(elem->arg);
3537 Assert(PointerIsValid(name));
3539 for (target = s; PointerIsValid(target); target = target->parent)
3541 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3545 if (!PointerIsValid(target))
3547 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3548 errmsg("no such savepoint")));
3550 /* disallow crossing savepoint level boundaries */
3551 if (target->savepointLevel != s->savepointLevel)
3553 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3554 errmsg("no such savepoint")));
3557 * Mark "commit pending" all subtransactions up to the target
3558 * subtransaction. The actual commits will happen when control gets to
3559 * CommitTransactionCommand.
3561 xact = CurrentTransactionState;
3564 Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
3565 xact->blockState = TBLOCK_SUBRELEASE;
3568 xact = xact->parent;
3569 Assert(PointerIsValid(xact));
3574 * RollbackToSavepoint
3575 * This executes a ROLLBACK TO <savepoint> command.
3577 * As above, we don't actually do anything here except change blockState.
3580 RollbackToSavepoint(List *options)
3582 TransactionState s = CurrentTransactionState;
3583 TransactionState target,
3588 switch (s->blockState)
3591 * We can't rollback to a savepoint if there is no savepoint
3594 case TBLOCK_INPROGRESS:
3597 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3598 errmsg("no such savepoint")));
3602 * There is at least one savepoint, so proceed.
3604 case TBLOCK_SUBINPROGRESS:
3605 case TBLOCK_SUBABORT:
3608 /* These cases are invalid. */
3609 case TBLOCK_DEFAULT:
3610 case TBLOCK_STARTED:
3612 case TBLOCK_SUBBEGIN:
3614 case TBLOCK_SUBRELEASE:
3615 case TBLOCK_SUBCOMMIT:
3616 case TBLOCK_ABORT_END:
3617 case TBLOCK_SUBABORT_END:
3618 case TBLOCK_ABORT_PENDING:
3619 case TBLOCK_SUBABORT_PENDING:
3620 case TBLOCK_SUBRESTART:
3621 case TBLOCK_SUBABORT_RESTART:
3622 case TBLOCK_PREPARE:
3623 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3624 BlockStateAsString(s->blockState));
3628 foreach(cell, options)
3630 DefElem *elem = lfirst(cell);
3632 if (strcmp(elem->defname, "savepoint_name") == 0)
3633 name = strVal(elem->arg);
3636 Assert(PointerIsValid(name));
3638 for (target = s; PointerIsValid(target); target = target->parent)
3640 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3644 if (!PointerIsValid(target))
3646 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3647 errmsg("no such savepoint")));
3649 /* disallow crossing savepoint level boundaries */
3650 if (target->savepointLevel != s->savepointLevel)
3652 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3653 errmsg("no such savepoint")));
3656 * Mark "abort pending" all subtransactions up to the target
3657 * subtransaction. The actual aborts will happen when control gets to
3658 * CommitTransactionCommand.
3660 xact = CurrentTransactionState;
3665 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3666 xact->blockState = TBLOCK_SUBABORT_PENDING;
3667 else if (xact->blockState == TBLOCK_SUBABORT)
3668 xact->blockState = TBLOCK_SUBABORT_END;
3670 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3671 BlockStateAsString(xact->blockState));
3672 xact = xact->parent;
3673 Assert(PointerIsValid(xact));
3676 /* And mark the target as "restart pending" */
3677 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3678 xact->blockState = TBLOCK_SUBRESTART;
3679 else if (xact->blockState == TBLOCK_SUBABORT)
3680 xact->blockState = TBLOCK_SUBABORT_RESTART;
3682 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3683 BlockStateAsString(xact->blockState));
3687 * BeginInternalSubTransaction
3688 * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
3689 * TBLOCK_END, and TBLOCK_PREPARE states, and therefore it can safely be
3690 * used in functions that might be called when not inside a BEGIN block
3691 * or when running deferred triggers at COMMIT/PREPARE time. Also, it
3692 * automatically does CommitTransactionCommand/StartTransactionCommand
3693 * instead of expecting the caller to do it.
3696 BeginInternalSubTransaction(char *name)
3698 TransactionState s = CurrentTransactionState;
3700 switch (s->blockState)
3702 case TBLOCK_STARTED:
3703 case TBLOCK_INPROGRESS:
3705 case TBLOCK_PREPARE:
3706 case TBLOCK_SUBINPROGRESS:
3707 /* Normal subtransaction start */
3709 s = CurrentTransactionState; /* changed by push */
3712 * Savepoint names, like the TransactionState block itself, live
3713 * in TopTransactionContext.
3716 s->name = MemoryContextStrdup(TopTransactionContext, name);
3719 /* These cases are invalid. */
3720 case TBLOCK_DEFAULT:
3722 case TBLOCK_SUBBEGIN:
3723 case TBLOCK_SUBRELEASE:
3724 case TBLOCK_SUBCOMMIT:
3726 case TBLOCK_SUBABORT:
3727 case TBLOCK_ABORT_END:
3728 case TBLOCK_SUBABORT_END:
3729 case TBLOCK_ABORT_PENDING:
3730 case TBLOCK_SUBABORT_PENDING:
3731 case TBLOCK_SUBRESTART:
3732 case TBLOCK_SUBABORT_RESTART:
3733 elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
3734 BlockStateAsString(s->blockState));
3738 CommitTransactionCommand();
3739 StartTransactionCommand();
3743 * ReleaseCurrentSubTransaction
3745 * RELEASE (ie, commit) the innermost subtransaction, regardless of its
3746 * savepoint name (if any).
3747 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3750 ReleaseCurrentSubTransaction(void)
3752 TransactionState s = CurrentTransactionState;
3754 if (s->blockState != TBLOCK_SUBINPROGRESS)
3755 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
3756 BlockStateAsString(s->blockState));
3757 Assert(s->state == TRANS_INPROGRESS);
3758 MemoryContextSwitchTo(CurTransactionContext);
3759 CommitSubTransaction();
3760 s = CurrentTransactionState; /* changed by pop */
3761 Assert(s->state == TRANS_INPROGRESS);
3765 * RollbackAndReleaseCurrentSubTransaction
3767 * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
3768 * of its savepoint name (if any).
3769 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3772 RollbackAndReleaseCurrentSubTransaction(void)
3774 TransactionState s = CurrentTransactionState;
3776 switch (s->blockState)
3778 /* Must be in a subtransaction */
3779 case TBLOCK_SUBINPROGRESS:
3780 case TBLOCK_SUBABORT:
3783 /* These cases are invalid. */
3784 case TBLOCK_DEFAULT:
3785 case TBLOCK_STARTED:
3787 case TBLOCK_SUBBEGIN:
3788 case TBLOCK_INPROGRESS:
3790 case TBLOCK_SUBRELEASE:
3791 case TBLOCK_SUBCOMMIT:
3793 case TBLOCK_ABORT_END:
3794 case TBLOCK_SUBABORT_END:
3795 case TBLOCK_ABORT_PENDING:
3796 case TBLOCK_SUBABORT_PENDING:
3797 case TBLOCK_SUBRESTART:
3798 case TBLOCK_SUBABORT_RESTART:
3799 case TBLOCK_PREPARE:
3800 elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
3801 BlockStateAsString(s->blockState));
3806 * Abort the current subtransaction, if needed.
3808 if (s->blockState == TBLOCK_SUBINPROGRESS)
3809 AbortSubTransaction();
3811 /* And clean it up, too */
3812 CleanupSubTransaction();
3814 s = CurrentTransactionState; /* changed by pop */
3815 AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
3816 s->blockState == TBLOCK_INPROGRESS ||
3817 s->blockState == TBLOCK_STARTED);
3821 * AbortOutOfAnyTransaction
3823 * This routine is provided for error recovery purposes. It aborts any
3824 * active transaction or transaction block, leaving the system in a known
3828 AbortOutOfAnyTransaction(void)
3830 TransactionState s = CurrentTransactionState;
3833 * Get out of any transaction or nested transaction
3837 switch (s->blockState)
3839 case TBLOCK_DEFAULT:
3840 /* Not in a transaction, do nothing */
3842 case TBLOCK_STARTED:
3844 case TBLOCK_INPROGRESS:
3846 case TBLOCK_ABORT_PENDING:
3847 case TBLOCK_PREPARE:
3848 /* In a transaction, so clean up */
3850 CleanupTransaction();
3851 s->blockState = TBLOCK_DEFAULT;
3854 case TBLOCK_ABORT_END:
3855 /* AbortTransaction already done, still need Cleanup */
3856 CleanupTransaction();
3857 s->blockState = TBLOCK_DEFAULT;
3861 * In a subtransaction, so clean it up and abort parent too
3863 case TBLOCK_SUBBEGIN:
3864 case TBLOCK_SUBINPROGRESS:
3865 case TBLOCK_SUBRELEASE:
3866 case TBLOCK_SUBCOMMIT:
3867 case TBLOCK_SUBABORT_PENDING:
3868 case TBLOCK_SUBRESTART:
3869 AbortSubTransaction();
3870 CleanupSubTransaction();
3871 s = CurrentTransactionState; /* changed by pop */
3874 case TBLOCK_SUBABORT:
3875 case TBLOCK_SUBABORT_END:
3876 case TBLOCK_SUBABORT_RESTART:
3877 /* As above, but AbortSubTransaction already done */
3878 CleanupSubTransaction();
3879 s = CurrentTransactionState; /* changed by pop */
3882 } while (s->blockState != TBLOCK_DEFAULT);
3884 /* Should be out of all subxacts now */
3885 Assert(s->parent == NULL);
3889 * IsTransactionBlock --- are we within a transaction block?
3892 IsTransactionBlock(void)
3894 TransactionState s = CurrentTransactionState;
3896 if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3903 * IsTransactionOrTransactionBlock --- are we within either a transaction
3904 * or a transaction block? (The backend is only really "idle" when this
3907 * This should match up with IsTransactionBlock and IsTransactionState.
3910 IsTransactionOrTransactionBlock(void)
3912 TransactionState s = CurrentTransactionState;
3914 if (s->blockState == TBLOCK_DEFAULT)
3921 * TransactionBlockStatusCode - return status code to send in ReadyForQuery
3924 TransactionBlockStatusCode(void)
3926 TransactionState s = CurrentTransactionState;
3928 switch (s->blockState)
3930 case TBLOCK_DEFAULT:
3931 case TBLOCK_STARTED:
3932 return 'I'; /* idle --- not in transaction */
3934 case TBLOCK_SUBBEGIN:
3935 case TBLOCK_INPROGRESS:
3936 case TBLOCK_SUBINPROGRESS:
3938 case TBLOCK_SUBRELEASE:
3939 case TBLOCK_SUBCOMMIT:
3940 case TBLOCK_PREPARE:
3941 return 'T'; /* in transaction */
3943 case TBLOCK_SUBABORT:
3944 case TBLOCK_ABORT_END:
3945 case TBLOCK_SUBABORT_END:
3946 case TBLOCK_ABORT_PENDING:
3947 case TBLOCK_SUBABORT_PENDING:
3948 case TBLOCK_SUBRESTART:
3949 case TBLOCK_SUBABORT_RESTART:
3950 return 'E'; /* in failed transaction */
3953 /* should never get here */
3954 elog(FATAL, "invalid transaction block state: %s",
3955 BlockStateAsString(s->blockState));
3956 return 0; /* keep compiler quiet */
3963 IsSubTransaction(void)
3965 TransactionState s = CurrentTransactionState;
3967 if (s->nestingLevel >= 2)
3974 * StartSubTransaction
3976 * If you're wondering why this is separate from PushTransaction: it's because
3977 * we can't conveniently do this stuff right inside DefineSavepoint. The
3978 * SAVEPOINT utility command will be executed inside a Portal, and if we
3979 * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
3980 * the Portal will undo those settings. So we make DefineSavepoint just
3981 * push a dummy transaction block, and when control returns to the main
3982 * idle loop, CommitTransactionCommand will be called, and we'll come here
3983 * to finish starting the subtransaction.
3986 StartSubTransaction(void)
3988 TransactionState s = CurrentTransactionState;
3990 if (s->state != TRANS_DEFAULT)
3991 elog(WARNING, "StartSubTransaction while in %s state",
3992 TransStateAsString(s->state));
3994 s->state = TRANS_START;
3997 * Initialize subsystems for new subtransaction
3999 * must initialize resource-management stuff first
4001 AtSubStart_Memory();
4002 AtSubStart_ResourceOwner();
4004 AtSubStart_Notify();
4005 AfterTriggerBeginSubXact();
4007 s->state = TRANS_INPROGRESS;
4010 * Call start-of-subxact callbacks
4012 CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
4013 s->parent->subTransactionId);
4015 ShowTransactionState("StartSubTransaction");
4019 * CommitSubTransaction
4021 * The caller has to make sure to always reassign CurrentTransactionState
4022 * if it has a local pointer to it after calling this function.
4025 CommitSubTransaction(void)
4027 TransactionState s = CurrentTransactionState;
4029 ShowTransactionState("CommitSubTransaction");
4031 if (s->state != TRANS_INPROGRESS)
4032 elog(WARNING, "CommitSubTransaction while in %s state",
4033 TransStateAsString(s->state));
4035 /* Pre-commit processing goes here -- nothing to do at the moment */
4037 s->state = TRANS_COMMIT;
4039 /* Must CCI to ensure commands of subtransaction are seen as done */
4040 CommandCounterIncrement();
4043 * Prior to 8.4 we marked subcommit in clog at this point. We now only
4044 * perform that step, if required, as part of the atomic update of the
4045 * whole transaction tree at top level commit or abort.
4048 /* Post-commit cleanup */
4049 if (TransactionIdIsValid(s->transactionId))
4050 AtSubCommit_childXids();
4051 AfterTriggerEndSubXact(true);
4052 AtSubCommit_Portals(s->subTransactionId,
4053 s->parent->subTransactionId,
4054 s->parent->curTransactionOwner);
4055 AtEOSubXact_LargeObject(true, s->subTransactionId,
4056 s->parent->subTransactionId);
4057 AtSubCommit_Notify();
4059 CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
4060 s->parent->subTransactionId);
4062 ResourceOwnerRelease(s->curTransactionOwner,
4063 RESOURCE_RELEASE_BEFORE_LOCKS,
4065 AtEOSubXact_RelationCache(true, s->subTransactionId,
4066 s->parent->subTransactionId);
4067 AtEOSubXact_Inval(true);
4071 * The only lock we actually release here is the subtransaction XID lock.
4073 CurrentResourceOwner = s->curTransactionOwner;
4074 if (TransactionIdIsValid(s->transactionId))
4075 XactLockTableDelete(s->transactionId);
4078 * Other locks should get transferred to their parent resource owner.
4080 ResourceOwnerRelease(s->curTransactionOwner,
4081 RESOURCE_RELEASE_LOCKS,
4083 ResourceOwnerRelease(s->curTransactionOwner,
4084 RESOURCE_RELEASE_AFTER_LOCKS,
4087 AtEOXact_GUC(true, s->gucNestLevel);
4088 AtEOSubXact_SPI(true, s->subTransactionId);
4089 AtEOSubXact_on_commit_actions(true, s->subTransactionId,
4090 s->parent->subTransactionId);
4091 AtEOSubXact_Namespace(true, s->subTransactionId,
4092 s->parent->subTransactionId);
4093 AtEOSubXact_Files(true, s->subTransactionId,
4094 s->parent->subTransactionId);
4095 AtEOSubXact_HashTables(true, s->nestingLevel);
4096 AtEOSubXact_PgStat(true, s->nestingLevel);
4097 AtSubCommit_Snapshot(s->nestingLevel);
4100 * We need to restore the upper transaction's read-only state, in case the
4101 * upper is read-write while the child is read-only; GUC will incorrectly
4102 * think it should leave the child state in place.
4104 XactReadOnly = s->prevXactReadOnly;
4106 CurrentResourceOwner = s->parent->curTransactionOwner;
4107 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4108 ResourceOwnerDelete(s->curTransactionOwner);
4109 s->curTransactionOwner = NULL;
4111 AtSubCommit_Memory();
4113 s->state = TRANS_DEFAULT;
4119 * AbortSubTransaction
4122 AbortSubTransaction(void)
4124 TransactionState s = CurrentTransactionState;
4126 /* Prevent cancel/die interrupt while cleaning up */
4129 /* Make sure we have a valid memory context and resource owner */
4130 AtSubAbort_Memory();
4131 AtSubAbort_ResourceOwner();
4134 * Release any LW locks we might be holding as quickly as possible.
4135 * (Regular locks, however, must be held till we finish aborting.)
4136 * Releasing LW locks is critical since we might try to grab them again
4137 * while cleaning up!
4139 * FIXME This may be incorrect --- Are there some locks we should keep?
4140 * Buffer locks, for example? I don't think so but I'm not sure.
4150 * check the current transaction state
4152 ShowTransactionState("AbortSubTransaction");
4154 if (s->state != TRANS_INPROGRESS)
4155 elog(WARNING, "AbortSubTransaction while in %s state",
4156 TransStateAsString(s->state));
4158 s->state = TRANS_ABORT;
4161 * Reset user ID which might have been changed transiently. (See notes in
4162 * AbortTransaction.)
4164 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
4167 * We can skip all this stuff if the subxact failed before creating a
4170 if (s->curTransactionOwner)
4172 AfterTriggerEndSubXact(false);
4173 AtSubAbort_Portals(s->subTransactionId,
4174 s->parent->subTransactionId,
4175 s->parent->curTransactionOwner);
4176 AtEOSubXact_LargeObject(false, s->subTransactionId,
4177 s->parent->subTransactionId);
4178 AtSubAbort_Notify();
4180 /* Advertise the fact that we aborted in pg_clog. */
4181 (void) RecordTransactionAbort(true);
4183 /* Post-abort cleanup */
4184 if (TransactionIdIsValid(s->transactionId))
4185 AtSubAbort_childXids();
4187 CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
4188 s->parent->subTransactionId);
4190 ResourceOwnerRelease(s->curTransactionOwner,
4191 RESOURCE_RELEASE_BEFORE_LOCKS,
4193 AtEOSubXact_RelationCache(false, s->subTransactionId,
4194 s->parent->subTransactionId);
4195 AtEOSubXact_Inval(false);
4197 ResourceOwnerRelease(s->curTransactionOwner,
4198 RESOURCE_RELEASE_LOCKS,
4200 ResourceOwnerRelease(s->curTransactionOwner,
4201 RESOURCE_RELEASE_AFTER_LOCKS,
4204 AtEOXact_GUC(false, s->gucNestLevel);
4205 AtEOSubXact_SPI(false, s->subTransactionId);
4206 AtEOSubXact_on_commit_actions(false, s->subTransactionId,
4207 s->parent->subTransactionId);
4208 AtEOSubXact_Namespace(false, s->subTransactionId,
4209 s->parent->subTransactionId);
4210 AtEOSubXact_Files(false, s->subTransactionId,
4211 s->parent->subTransactionId);
4212 AtEOSubXact_HashTables(false, s->nestingLevel);
4213 AtEOSubXact_PgStat(false, s->nestingLevel);
4214 AtSubAbort_Snapshot(s->nestingLevel);
4218 * Restore the upper transaction's read-only state, too. This should be
4219 * redundant with GUC's cleanup but we may as well do it for consistency
4220 * with the commit case.
4222 XactReadOnly = s->prevXactReadOnly;
4224 RESUME_INTERRUPTS();
4228 * CleanupSubTransaction
4230 * The caller has to make sure to always reassign CurrentTransactionState
4231 * if it has a local pointer to it after calling this function.
4234 CleanupSubTransaction(void)
4236 TransactionState s = CurrentTransactionState;
4238 ShowTransactionState("CleanupSubTransaction");
4240 if (s->state != TRANS_ABORT)
4241 elog(WARNING, "CleanupSubTransaction while in %s state",
4242 TransStateAsString(s->state));
4244 AtSubCleanup_Portals(s->subTransactionId);
4246 CurrentResourceOwner = s->parent->curTransactionOwner;
4247 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4248 if (s->curTransactionOwner)
4249 ResourceOwnerDelete(s->curTransactionOwner);
4250 s->curTransactionOwner = NULL;
4252 AtSubCleanup_Memory();
4254 s->state = TRANS_DEFAULT;
4261 * Create transaction state stack entry for a subtransaction
4263 * The caller has to make sure to always reassign CurrentTransactionState
4264 * if it has a local pointer to it after calling this function.
4267 PushTransaction(void)
4269 TransactionState p = CurrentTransactionState;
4273 * We keep subtransaction state nodes in TopTransactionContext.
4275 s = (TransactionState)
4276 MemoryContextAllocZero(TopTransactionContext,
4277 sizeof(TransactionStateData));
4280 * Assign a subtransaction ID, watching out for counter wraparound.
4282 currentSubTransactionId += 1;
4283 if (currentSubTransactionId == InvalidSubTransactionId)
4285 currentSubTransactionId -= 1;
4288 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4289 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4293 * We can now stack a minimally valid subtransaction without fear of
4296 s->transactionId = InvalidTransactionId; /* until assigned */
4297 s->subTransactionId = currentSubTransactionId;
4299 s->nestingLevel = p->nestingLevel + 1;
4300 s->gucNestLevel = NewGUCNestLevel();
4301 s->savepointLevel = p->savepointLevel;
4302 s->state = TRANS_DEFAULT;
4303 s->blockState = TBLOCK_SUBBEGIN;
4304 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
4305 s->prevXactReadOnly = XactReadOnly;
4307 CurrentTransactionState = s;
4310 * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4311 * with the subtransaction from here on out; in particular they should not
4312 * assume that it necessarily has a transaction context, resource owner,
4319 * Pop back to parent transaction state
4321 * The caller has to make sure to always reassign CurrentTransactionState
4322 * if it has a local pointer to it after calling this function.
4325 PopTransaction(void)
4327 TransactionState s = CurrentTransactionState;
4329 if (s->state != TRANS_DEFAULT)
4330 elog(WARNING, "PopTransaction while in %s state",
4331 TransStateAsString(s->state));
4333 if (s->parent == NULL)
4334 elog(FATAL, "PopTransaction with no parent");
4336 CurrentTransactionState = s->parent;
4338 /* Let's just make sure CurTransactionContext is good */
4339 CurTransactionContext = s->parent->curTransactionContext;
4340 MemoryContextSwitchTo(CurTransactionContext);
4342 /* Ditto for ResourceOwner links */
4343 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4344 CurrentResourceOwner = s->parent->curTransactionOwner;
4346 /* Free the old child structure */
4353 * ShowTransactionState
4357 ShowTransactionState(const char *str)
4359 /* skip work if message will definitely not be printed */
4360 if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4362 elog(DEBUG3, "%s", str);
4363 ShowTransactionStateRec(CurrentTransactionState);
4368 * ShowTransactionStateRec
4369 * Recursive subroutine for ShowTransactionState
4372 ShowTransactionStateRec(TransactionState s)
4376 initStringInfo(&buf);
4378 if (s->nChildXids > 0)
4382 appendStringInfo(&buf, "%u", s->childXids[0]);
4383 for (i = 1; i < s->nChildXids; i++)
4384 appendStringInfo(&buf, " %u", s->childXids[i]);
4388 ShowTransactionStateRec(s->parent);
4390 /* use ereport to suppress computation if msg will not be printed */
4392 (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u%s, nestlvl: %d, children: %s",
4393 PointerIsValid(s->name) ? s->name : "unnamed",
4394 BlockStateAsString(s->blockState),
4395 TransStateAsString(s->state),
4396 (unsigned int) s->transactionId,
4397 (unsigned int) s->subTransactionId,
4398 (unsigned int) currentCommandId,
4399 currentCommandIdUsed ? " (used)" : "",
4400 s->nestingLevel, buf.data)));
4406 * BlockStateAsString
4410 BlockStateAsString(TBlockState blockState)
4414 case TBLOCK_DEFAULT:
4416 case TBLOCK_STARTED:
4420 case TBLOCK_INPROGRESS:
4421 return "INPROGRESS";
4426 case TBLOCK_ABORT_END:
4428 case TBLOCK_ABORT_PENDING:
4429 return "ABORT PEND";
4430 case TBLOCK_PREPARE:
4432 case TBLOCK_SUBBEGIN:
4434 case TBLOCK_SUBINPROGRESS:
4435 return "SUB INPROGRS";
4436 case TBLOCK_SUBRELEASE:
4437 return "SUB RELEASE";
4438 case TBLOCK_SUBCOMMIT:
4439 return "SUB COMMIT";
4440 case TBLOCK_SUBABORT:
4442 case TBLOCK_SUBABORT_END:
4443 return "SUB ABORT END";
4444 case TBLOCK_SUBABORT_PENDING:
4445 return "SUB ABRT PEND";
4446 case TBLOCK_SUBRESTART:
4447 return "SUB RESTART";
4448 case TBLOCK_SUBABORT_RESTART:
4449 return "SUB AB RESTRT";
4451 return "UNRECOGNIZED";
4455 * TransStateAsString
4459 TransStateAsString(TransState state)
4467 case TRANS_INPROGRESS:
4476 return "UNRECOGNIZED";
4480 * xactGetCommittedChildren
4482 * Gets the list of committed children of the current transaction. The return
4483 * value is the number of child transactions. *ptr is set to point to an
4484 * array of TransactionIds. The array is allocated in TopTransactionContext;
4485 * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
4486 * If there are no subxacts, *ptr is set to NULL.
4489 xactGetCommittedChildren(TransactionId **ptr)
4491 TransactionState s = CurrentTransactionState;
4493 if (s->nChildXids == 0)
4496 *ptr = s->childXids;
4498 return s->nChildXids;
4502 * XLOG support routines
4506 * Before 9.0 this was a fairly short function, but now it performs many
4507 * actions for which the order of execution is critical.
4510 xact_redo_commit_internal(TransactionId xid, XLogRecPtr lsn,
4511 TransactionId *sub_xids, int nsubxacts,
4512 SharedInvalidationMessage *inval_msgs, int nmsgs,
4513 RelFileNode *xnodes, int nrels,
4517 TransactionId max_xid;
4520 max_xid = TransactionIdLatest(xid, nsubxacts, sub_xids);
4523 * Make sure nextXid is beyond any XID mentioned in the record.
4525 * We don't expect anyone else to modify nextXid, hence we don't need to
4526 * hold a lock while checking this. We still acquire the lock to modify
4529 if (TransactionIdFollowsOrEquals(max_xid,
4530 ShmemVariableCache->nextXid))
4532 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4533 ShmemVariableCache->nextXid = max_xid;
4534 TransactionIdAdvance(ShmemVariableCache->nextXid);
4535 LWLockRelease(XidGenLock);
4538 if (standbyState == STANDBY_DISABLED)
4541 * Mark the transaction committed in pg_clog.
4543 TransactionIdCommitTree(xid, nsubxacts, sub_xids);
4548 * If a transaction completion record arrives that has as-yet
4549 * unobserved subtransactions then this will not have been fully
4550 * handled by the call to RecordKnownAssignedTransactionIds() in the
4551 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4552 * cover that case. This is confusing and it is easy to think this
4553 * call is irrelevant, which has happened three times in development
4554 * already. Leave it in.
4556 RecordKnownAssignedTransactionIds(max_xid);
4559 * Mark the transaction committed in pg_clog. We use async commit
4560 * protocol during recovery to provide information on database
4561 * consistency for when users try to set hint bits. It is important
4562 * that we do not set hint bits until the minRecoveryPoint is past
4563 * this commit record. This ensures that if we crash we don't see hint
4564 * bits set on changes made by transactions that haven't yet
4565 * recovered. It's unlikely but it's good to be safe.
4567 TransactionIdAsyncCommitTree(xid, nsubxacts, sub_xids, lsn);
4570 * We must mark clog before we update the ProcArray.
4572 ExpireTreeKnownAssignedTransactionIds(xid, nsubxacts, sub_xids, max_xid);
4575 * Send any cache invalidations attached to the commit. We must
4576 * maintain the same order of invalidation then release locks as
4577 * occurs in CommitTransaction().
4579 ProcessCommittedInvalidationMessages(inval_msgs, nmsgs,
4580 XactCompletionRelcacheInitFileInval(xinfo),
4584 * Release locks, if any. We do this for both two phase and normal one
4585 * phase transactions. In effect we are ignoring the prepare phase and
4586 * just going straight to lock release.
4588 StandbyReleaseLockTree(xid, nsubxacts, sub_xids);
4591 /* Make sure files supposed to be dropped are dropped */
4592 for (i = 0; i < nrels; i++)
4594 SMgrRelation srel = smgropen(xnodes[i], InvalidBackendId);
4597 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4599 XLogDropRelation(xnodes[i], fork);
4600 smgrdounlink(srel, fork, true);
4606 * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
4607 * in normal operation. For example, in DROP DATABASE, we delete all the
4608 * files belonging to the database, and then commit the transaction. If we
4609 * crash after all the files have been deleted but before the commit, you
4610 * have an entry in pg_database without any files. To minimize the window
4611 * for that, we use ForceSyncCommit() to rush the commit record to disk as
4612 * quick as possible. We have the same window during recovery, and forcing
4613 * an XLogFlush() (which updates minRecoveryPoint during recovery) helps
4614 * to reduce that problem window, for any user that requested
4615 * ForceSyncCommit().
4617 if (XactCompletionForceSyncCommit(xinfo))
4622 * Utility function to call xact_redo_commit_internal after breaking down xlrec
4625 xact_redo_commit(xl_xact_commit *xlrec,
4626 TransactionId xid, XLogRecPtr lsn)
4628 TransactionId *subxacts;
4629 SharedInvalidationMessage *inval_msgs;
4631 /* subxid array follows relfilenodes */
4632 subxacts = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4633 /* invalidation messages array follows subxids */
4634 inval_msgs = (SharedInvalidationMessage *) &(subxacts[xlrec->nsubxacts]);
4636 xact_redo_commit_internal(xid, lsn, subxacts, xlrec->nsubxacts,
4637 inval_msgs, xlrec->nmsgs,
4638 xlrec->xnodes, xlrec->nrels,
4645 * Utility function to call xact_redo_commit_internal for compact form of message.
4648 xact_redo_commit_compact(xl_xact_commit_compact *xlrec,
4649 TransactionId xid, XLogRecPtr lsn)
4651 xact_redo_commit_internal(xid, lsn, xlrec->subxacts, xlrec->nsubxacts,
4652 NULL, 0, /* inval msgs */
4653 NULL, 0, /* relfilenodes */
4654 InvalidOid, /* dbId */
4655 InvalidOid, /* tsId */
4660 * Be careful with the order of execution, as with xact_redo_commit().
4661 * The two functions are similar but differ in key places.
4663 * Note also that an abort can be for a subtransaction and its children,
4664 * not just for a top level abort. That means we have to consider
4665 * topxid != xid, whereas in commit we would find topxid == xid always
4666 * because subtransaction commit is never WAL logged.
4669 xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
4671 TransactionId *sub_xids;
4672 TransactionId max_xid;
4675 sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4676 max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4679 * Make sure nextXid is beyond any XID mentioned in the record.
4681 * We don't expect anyone else to modify nextXid, hence we don't need to
4682 * hold a lock while checking this. We still acquire the lock to modify
4685 if (TransactionIdFollowsOrEquals(max_xid,
4686 ShmemVariableCache->nextXid))
4688 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4689 ShmemVariableCache->nextXid = max_xid;
4690 TransactionIdAdvance(ShmemVariableCache->nextXid);
4691 LWLockRelease(XidGenLock);
4694 if (standbyState == STANDBY_DISABLED)
4696 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4697 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4702 * If a transaction completion record arrives that has as-yet
4703 * unobserved subtransactions then this will not have been fully
4704 * handled by the call to RecordKnownAssignedTransactionIds() in the
4705 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4706 * cover that case. This is confusing and it is easy to think this
4707 * call is irrelevant, which has happened three times in development
4708 * already. Leave it in.
4710 RecordKnownAssignedTransactionIds(max_xid);
4712 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4713 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4716 * We must update the ProcArray after we have marked clog.
4718 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids, max_xid);
4721 * There are no flat files that need updating, nor invalidation
4722 * messages to send or undo.
4726 * Release locks, if any. There are no invalidations to send.
4728 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4731 /* Make sure files supposed to be dropped are dropped */
4732 for (i = 0; i < xlrec->nrels; i++)
4734 SMgrRelation srel = smgropen(xlrec->xnodes[i], InvalidBackendId);
4737 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4739 XLogDropRelation(xlrec->xnodes[i], fork);
4740 smgrdounlink(srel, fork, true);
4747 xact_redo(XLogRecPtr lsn, XLogRecord *record)
4749 uint8 info = record->xl_info & ~XLR_INFO_MASK;
4751 /* Backup blocks are not used in xact records */
4752 Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
4754 if (info == XLOG_XACT_COMMIT_COMPACT)
4756 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) XLogRecGetData(record);
4758 xact_redo_commit_compact(xlrec, record->xl_xid, lsn);
4760 else if (info == XLOG_XACT_COMMIT)
4762 xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
4764 xact_redo_commit(xlrec, record->xl_xid, lsn);
4766 else if (info == XLOG_XACT_ABORT)
4768 xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4770 xact_redo_abort(xlrec, record->xl_xid);
4772 else if (info == XLOG_XACT_PREPARE)
4774 /* the record contents are exactly the 2PC file */
4775 RecreateTwoPhaseFile(record->xl_xid,
4776 XLogRecGetData(record), record->xl_len);
4778 else if (info == XLOG_XACT_COMMIT_PREPARED)
4780 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4782 xact_redo_commit(&xlrec->crec, xlrec->xid, lsn);
4783 RemoveTwoPhaseFile(xlrec->xid, false);
4785 else if (info == XLOG_XACT_ABORT_PREPARED)
4787 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4789 xact_redo_abort(&xlrec->arec, xlrec->xid);
4790 RemoveTwoPhaseFile(xlrec->xid, false);
4792 else if (info == XLOG_XACT_ASSIGNMENT)
4794 xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
4796 if (standbyState >= STANDBY_INITIALIZED)
4797 ProcArrayApplyXidAssignment(xlrec->xtop,
4798 xlrec->nsubxacts, xlrec->xsub);
4801 elog(PANIC, "xact_redo: unknown op code %u", info);
4805 xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4808 TransactionId *subxacts;
4810 subxacts = (TransactionId *) &xlrec->xnodes[xlrec->nrels];
4812 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4814 if (xlrec->nrels > 0)
4816 appendStringInfo(buf, "; rels:");
4817 for (i = 0; i < xlrec->nrels; i++)
4819 char *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4821 appendStringInfo(buf, " %s", path);
4825 if (xlrec->nsubxacts > 0)
4827 appendStringInfo(buf, "; subxacts:");
4828 for (i = 0; i < xlrec->nsubxacts; i++)
4829 appendStringInfo(buf, " %u", subxacts[i]);
4831 if (xlrec->nmsgs > 0)
4833 SharedInvalidationMessage *msgs;
4835 msgs = (SharedInvalidationMessage *) &subxacts[xlrec->nsubxacts];
4837 if (XactCompletionRelcacheInitFileInval(xlrec->xinfo))
4838 appendStringInfo(buf, "; relcache init file inval dbid %u tsid %u",
4839 xlrec->dbId, xlrec->tsId);
4841 appendStringInfo(buf, "; inval msgs:");
4842 for (i = 0; i < xlrec->nmsgs; i++)
4844 SharedInvalidationMessage *msg = &msgs[i];
4847 appendStringInfo(buf, " catcache %d", msg->id);
4848 else if (msg->id == SHAREDINVALCATALOG_ID)
4849 appendStringInfo(buf, " catalog %u", msg->cat.catId);
4850 else if (msg->id == SHAREDINVALRELCACHE_ID)
4851 appendStringInfo(buf, " relcache %u", msg->rc.relId);
4852 /* remaining cases not expected, but print something anyway */
4853 else if (msg->id == SHAREDINVALSMGR_ID)
4854 appendStringInfo(buf, " smgr");
4855 else if (msg->id == SHAREDINVALRELMAP_ID)
4856 appendStringInfo(buf, " relmap");
4858 appendStringInfo(buf, " unknown id %d", msg->id);
4864 xact_desc_commit_compact(StringInfo buf, xl_xact_commit_compact *xlrec)
4868 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4870 if (xlrec->nsubxacts > 0)
4872 appendStringInfo(buf, "; subxacts:");
4873 for (i = 0; i < xlrec->nsubxacts; i++)
4874 appendStringInfo(buf, " %u", xlrec->subxacts[i]);
4879 xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4883 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4884 if (xlrec->nrels > 0)
4886 appendStringInfo(buf, "; rels:");
4887 for (i = 0; i < xlrec->nrels; i++)
4889 char *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4891 appendStringInfo(buf, " %s", path);
4895 if (xlrec->nsubxacts > 0)
4897 TransactionId *xacts = (TransactionId *)
4898 &xlrec->xnodes[xlrec->nrels];
4900 appendStringInfo(buf, "; subxacts:");
4901 for (i = 0; i < xlrec->nsubxacts; i++)
4902 appendStringInfo(buf, " %u", xacts[i]);
4907 xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
4911 appendStringInfo(buf, "subxacts:");
4913 for (i = 0; i < xlrec->nsubxacts; i++)
4914 appendStringInfo(buf, " %u", xlrec->xsub[i]);
4918 xact_desc(StringInfo buf, uint8 xl_info, char *rec)
4920 uint8 info = xl_info & ~XLR_INFO_MASK;
4922 if (info == XLOG_XACT_COMMIT_COMPACT)
4924 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) rec;
4926 appendStringInfo(buf, "commit: ");
4927 xact_desc_commit_compact(buf, xlrec);
4929 else if (info == XLOG_XACT_COMMIT)
4931 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
4933 appendStringInfo(buf, "commit: ");
4934 xact_desc_commit(buf, xlrec);
4936 else if (info == XLOG_XACT_ABORT)
4938 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
4940 appendStringInfo(buf, "abort: ");
4941 xact_desc_abort(buf, xlrec);
4943 else if (info == XLOG_XACT_PREPARE)
4945 appendStringInfo(buf, "prepare");
4947 else if (info == XLOG_XACT_COMMIT_PREPARED)
4949 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
4951 appendStringInfo(buf, "commit prepared %u: ", xlrec->xid);
4952 xact_desc_commit(buf, &xlrec->crec);
4954 else if (info == XLOG_XACT_ABORT_PREPARED)
4956 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4958 appendStringInfo(buf, "abort prepared %u: ", xlrec->xid);
4959 xact_desc_abort(buf, &xlrec->arec);
4961 else if (info == XLOG_XACT_ASSIGNMENT)
4963 xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
4966 * Note that we ignore the WAL record's xid, since we're more
4967 * interested in the top-level xid that issued the record and which
4968 * xids are being reported here.
4970 appendStringInfo(buf, "xid assignment xtop %u: ", xlrec->xtop);
4971 xact_desc_assignment(buf, xlrec);
4974 appendStringInfo(buf, "UNKNOWN");