1 /*-------------------------------------------------------------------------
4 * top level transaction system support routines
6 * See src/backend/access/transam/README for more information.
8 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.295 2010/07/29 22:27:27 sriggs Exp $
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 "storage/bufmgr.h"
40 #include "storage/fd.h"
41 #include "storage/lmgr.h"
42 #include "storage/procarray.h"
43 #include "storage/sinvaladt.h"
44 #include "storage/smgr.h"
45 #include "storage/standby.h"
46 #include "utils/combocid.h"
47 #include "utils/guc.h"
48 #include "utils/inval.h"
49 #include "utils/memutils.h"
50 #include "utils/relcache.h"
51 #include "utils/relmapper.h"
52 #include "utils/snapmgr.h"
57 * User-tweakable parameters
59 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
62 bool DefaultXactReadOnly = false;
65 bool XactSyncCommit = true;
67 int CommitDelay = 0; /* precommit delay in microseconds */
68 int CommitSiblings = 5; /* # concurrent xacts needed to sleep */
71 * MyXactAccessedTempRel is set when a temporary relation is accessed.
72 * We don't allow PREPARE TRANSACTION in that case. (This is global
73 * so that it can be set from heapam.c.)
75 bool MyXactAccessedTempRel = false;
79 * transaction states - transaction state from server perspective
81 typedef enum TransState
83 TRANS_DEFAULT, /* idle */
84 TRANS_START, /* transaction starting */
85 TRANS_INPROGRESS, /* inside a valid transaction */
86 TRANS_COMMIT, /* commit in progress */
87 TRANS_ABORT, /* abort in progress */
88 TRANS_PREPARE /* prepare in progress */
92 * transaction block states - transaction state of client queries
94 * Note: the subtransaction states are used only for non-topmost
95 * transactions; the others appear only in the topmost transaction.
97 typedef enum TBlockState
99 /* not-in-transaction-block states */
100 TBLOCK_DEFAULT, /* idle */
101 TBLOCK_STARTED, /* running single-query transaction */
103 /* transaction block states */
104 TBLOCK_BEGIN, /* starting transaction block */
105 TBLOCK_INPROGRESS, /* live transaction */
106 TBLOCK_END, /* COMMIT received */
107 TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
108 TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
109 TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
110 TBLOCK_PREPARE, /* live xact, PREPARE received */
112 /* subtransaction states */
113 TBLOCK_SUBBEGIN, /* starting a subtransaction */
114 TBLOCK_SUBINPROGRESS, /* live subtransaction */
115 TBLOCK_SUBEND, /* RELEASE received */
116 TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
117 TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
118 TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
119 TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
120 TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
124 * transaction state structure
126 typedef struct TransactionStateData
128 TransactionId transactionId; /* my XID, or Invalid if none */
129 SubTransactionId subTransactionId; /* my subxact ID */
130 char *name; /* savepoint name, if any */
131 int savepointLevel; /* savepoint level */
132 TransState state; /* low-level state */
133 TBlockState blockState; /* high-level state */
134 int nestingLevel; /* transaction nesting depth */
135 int gucNestLevel; /* GUC context nesting depth */
136 MemoryContext curTransactionContext; /* my xact-lifetime context */
137 ResourceOwner curTransactionOwner; /* my query resources */
138 TransactionId *childXids; /* subcommitted child XIDs, in XID order */
139 int nChildXids; /* # of subcommitted child XIDs */
140 int maxChildXids; /* allocated size of childXids[] */
141 Oid prevUser; /* previous CurrentUserId setting */
142 int prevSecContext; /* previous SecurityRestrictionContext */
143 bool prevXactReadOnly; /* entry-time xact r/o state */
144 bool startedInRecovery; /* did we start in recovery? */
145 struct TransactionStateData *parent; /* back link to parent */
146 } TransactionStateData;
148 typedef TransactionStateData *TransactionState;
151 * CurrentTransactionState always points to the current transaction state
152 * block. It will point to TopTransactionStateData when not in a
153 * transaction at all, or when in a top-level transaction.
155 static TransactionStateData TopTransactionStateData = {
156 0, /* transaction id */
157 0, /* subtransaction id */
158 NULL, /* savepoint name */
159 0, /* savepoint level */
160 TRANS_DEFAULT, /* transaction state */
161 TBLOCK_DEFAULT, /* transaction block state from the client
163 0, /* transaction nesting depth */
164 0, /* GUC context nesting depth */
165 NULL, /* cur transaction context */
166 NULL, /* cur transaction resource owner */
167 NULL, /* subcommitted child Xids */
168 0, /* # of subcommitted child Xids */
169 0, /* allocated size of childXids[] */
170 InvalidOid, /* previous CurrentUserId setting */
171 0, /* previous SecurityRestrictionContext */
172 false, /* entry-time xact r/o state */
173 false, /* startedInRecovery */
174 NULL /* link to parent state block */
178 * unreportedXids holds XIDs of all subtransactions that have not yet been
179 * reported in a XLOG_XACT_ASSIGNMENT record.
181 static int nUnreportedXids;
182 static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS];
184 static TransactionState CurrentTransactionState = &TopTransactionStateData;
187 * The subtransaction ID and command ID assignment counters are global
188 * to a whole transaction, so we do not keep them in the state stack.
190 static SubTransactionId currentSubTransactionId;
191 static CommandId currentCommandId;
192 static bool currentCommandIdUsed;
195 * xactStartTimestamp is the value of transaction_timestamp().
196 * stmtStartTimestamp is the value of statement_timestamp().
197 * xactStopTimestamp is the time at which we log a commit or abort WAL record.
198 * These do not change as we enter and exit subtransactions, so we don't
199 * keep them inside the TransactionState stack.
201 static TimestampTz xactStartTimestamp;
202 static TimestampTz stmtStartTimestamp;
203 static TimestampTz xactStopTimestamp;
206 * GID to be used for preparing the current transaction. This is also
207 * global to a whole transaction, so we don't keep it in the state stack.
209 static char *prepareGID;
212 * Some commands want to force synchronous commit.
214 static bool forceSyncCommit = false;
217 * Private context for transaction-abort work --- we reserve space for this
218 * at startup to ensure that AbortTransaction and AbortSubTransaction can work
219 * when we've run out of memory.
221 static MemoryContext TransactionAbortContext = NULL;
224 * List of add-on start- and end-of-xact callbacks
226 typedef struct XactCallbackItem
228 struct XactCallbackItem *next;
229 XactCallback callback;
233 static XactCallbackItem *Xact_callbacks = NULL;
236 * List of add-on start- and end-of-subxact callbacks
238 typedef struct SubXactCallbackItem
240 struct SubXactCallbackItem *next;
241 SubXactCallback callback;
243 } SubXactCallbackItem;
245 static SubXactCallbackItem *SubXact_callbacks = NULL;
248 /* local function prototypes */
249 static void AssignTransactionId(TransactionState s);
250 static void AbortTransaction(void);
251 static void AtAbort_Memory(void);
252 static void AtCleanup_Memory(void);
253 static void AtAbort_ResourceOwner(void);
254 static void AtCCI_LocalCache(void);
255 static void AtCommit_Memory(void);
256 static void AtStart_Cache(void);
257 static void AtStart_Memory(void);
258 static void AtStart_ResourceOwner(void);
259 static void CallXactCallbacks(XactEvent event);
260 static void CallSubXactCallbacks(SubXactEvent event,
261 SubTransactionId mySubid,
262 SubTransactionId parentSubid);
263 static void CleanupTransaction(void);
264 static void CommitTransaction(void);
265 static TransactionId RecordTransactionAbort(bool isSubXact);
266 static void StartTransaction(void);
268 static void StartSubTransaction(void);
269 static void CommitSubTransaction(void);
270 static void AbortSubTransaction(void);
271 static void CleanupSubTransaction(void);
272 static void PushTransaction(void);
273 static void PopTransaction(void);
275 static void AtSubAbort_Memory(void);
276 static void AtSubCleanup_Memory(void);
277 static void AtSubAbort_ResourceOwner(void);
278 static void AtSubCommit_Memory(void);
279 static void AtSubStart_Memory(void);
280 static void AtSubStart_ResourceOwner(void);
282 static void ShowTransactionState(const char *str);
283 static void ShowTransactionStateRec(TransactionState state);
284 static const char *BlockStateAsString(TBlockState blockState);
285 static const char *TransStateAsString(TransState state);
288 /* ----------------------------------------------------------------
289 * transaction state accessors
290 * ----------------------------------------------------------------
296 * This returns true if we are inside a valid transaction; that is,
297 * it is safe to initiate database access, take heavyweight locks, etc.
300 IsTransactionState(void)
302 TransactionState s = CurrentTransactionState;
305 * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
306 * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
307 * TRANS_PREPARE since it might be too soon or too late within those
308 * transition states to do anything interesting. Hence, the only "valid"
309 * state is TRANS_INPROGRESS.
311 return (s->state == TRANS_INPROGRESS);
315 * IsAbortedTransactionBlockState
317 * This returns true if we are within an aborted transaction block.
320 IsAbortedTransactionBlockState(void)
322 TransactionState s = CurrentTransactionState;
324 if (s->blockState == TBLOCK_ABORT ||
325 s->blockState == TBLOCK_SUBABORT)
333 * GetTopTransactionId
335 * This will return the XID of the main transaction, assigning one if
336 * it's not yet set. Be careful to call this only inside a valid xact.
339 GetTopTransactionId(void)
341 if (!TransactionIdIsValid(TopTransactionStateData.transactionId))
342 AssignTransactionId(&TopTransactionStateData);
343 return TopTransactionStateData.transactionId;
347 * GetTopTransactionIdIfAny
349 * This will return the XID of the main transaction, if one is assigned.
350 * It will return InvalidTransactionId if we are not currently inside a
351 * transaction, or inside a transaction that hasn't yet been assigned an XID.
354 GetTopTransactionIdIfAny(void)
356 return TopTransactionStateData.transactionId;
360 * GetCurrentTransactionId
362 * This will return the XID of the current transaction (main or sub
363 * transaction), assigning one if it's not yet set. Be careful to call this
364 * only inside a valid xact.
367 GetCurrentTransactionId(void)
369 TransactionState s = CurrentTransactionState;
371 if (!TransactionIdIsValid(s->transactionId))
372 AssignTransactionId(s);
373 return s->transactionId;
377 * GetCurrentTransactionIdIfAny
379 * This will return the XID of the current sub xact, if one is assigned.
380 * It will return InvalidTransactionId if we are not currently inside a
381 * transaction, or inside a transaction that hasn't been assigned an XID yet.
384 GetCurrentTransactionIdIfAny(void)
386 return CurrentTransactionState->transactionId;
391 * AssignTransactionId
393 * Assigns a new permanent XID to the given TransactionState.
394 * We do not assign XIDs to transactions until/unless this is called.
395 * Also, any parent TransactionStates that don't yet have XIDs are assigned
396 * one; this maintains the invariant that a child transaction has an XID
397 * following its parent's.
400 AssignTransactionId(TransactionState s)
402 bool isSubXact = (s->parent != NULL);
403 ResourceOwner currentOwner;
405 /* Assert that caller didn't screw up */
406 Assert(!TransactionIdIsValid(s->transactionId));
407 Assert(s->state == TRANS_INPROGRESS);
410 * Ensure parent(s) have XIDs, so that a child always has an XID later
411 * than its parent. Musn't recurse here, or we might get a stack overflow
412 * if we're at the bottom of a huge stack of subtransactions none of which
415 if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
417 TransactionState p = s->parent;
418 TransactionState *parents;
419 size_t parentOffset = 0;
421 parents = palloc(sizeof(TransactionState) * s->nestingLevel);
422 while (p != NULL && !TransactionIdIsValid(p->transactionId))
424 parents[parentOffset++] = p;
429 * This is technically a recursive call, but the recursion will
430 * never be more than one layer deep.
432 while (parentOffset != 0)
433 AssignTransactionId(parents[--parentOffset]);
439 * Generate a new Xid and record it in PG_PROC and pg_subtrans.
441 * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
442 * shared storage other than PG_PROC; because if there's no room for it in
443 * PG_PROC, the subtrans entry is needed to ensure that other backends see
444 * the Xid as "running". See GetNewTransactionId.
446 s->transactionId = GetNewTransactionId(isSubXact);
449 SubTransSetParent(s->transactionId, s->parent->transactionId, false);
452 * Acquire lock on the transaction XID. (We assume this cannot block.) We
453 * have to ensure that the lock is assigned to the transaction's own
456 currentOwner = CurrentResourceOwner;
459 CurrentResourceOwner = s->curTransactionOwner;
460 XactLockTableInsert(s->transactionId);
464 /* Ensure CurrentResourceOwner is restored on error */
465 CurrentResourceOwner = currentOwner;
469 CurrentResourceOwner = currentOwner;
472 * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
473 * top-level transaction we issue a WAL record for the assignment. We
474 * include the top-level xid and all the subxids that have not yet been
475 * reported using XLOG_XACT_ASSIGNMENT records.
477 * This is required to limit the amount of shared memory required in a hot
478 * standby server to keep track of in-progress XIDs. See notes for
479 * RecordKnownAssignedTransactionIds().
481 * We don't keep track of the immediate parent of each subxid, only the
482 * top-level transaction that each subxact belongs to. This is correct in
483 * recovery only because aborted subtransactions are separately WAL
486 if (isSubXact && XLogStandbyInfoActive())
488 unreportedXids[nUnreportedXids] = s->transactionId;
492 * ensure this test matches similar one in
493 * RecoverPreparedTransactions()
495 if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS)
497 XLogRecData rdata[2];
498 xl_xact_assignment xlrec;
501 * xtop is always set by now because we recurse up transaction
502 * stack to the highest unassigned xid and then come back down
504 xlrec.xtop = GetTopTransactionId();
505 Assert(TransactionIdIsValid(xlrec.xtop));
506 xlrec.nsubxacts = nUnreportedXids;
508 rdata[0].data = (char *) &xlrec;
509 rdata[0].len = MinSizeOfXactAssignment;
510 rdata[0].buffer = InvalidBuffer;
511 rdata[0].next = &rdata[1];
513 rdata[1].data = (char *) unreportedXids;
514 rdata[1].len = PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId);
515 rdata[1].buffer = InvalidBuffer;
516 rdata[1].next = NULL;
518 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT, rdata);
526 * GetCurrentSubTransactionId
529 GetCurrentSubTransactionId(void)
531 TransactionState s = CurrentTransactionState;
533 return s->subTransactionId;
538 * GetCurrentCommandId
540 * "used" must be TRUE if the caller intends to use the command ID to mark
541 * inserted/updated/deleted tuples. FALSE means the ID is being fetched
542 * for read-only purposes (ie, as a snapshot validity cutoff). See
543 * CommandCounterIncrement() for discussion.
546 GetCurrentCommandId(bool used)
548 /* this is global to a transaction, not subtransaction-local */
550 currentCommandIdUsed = true;
551 return currentCommandId;
555 * GetCurrentTransactionStartTimestamp
558 GetCurrentTransactionStartTimestamp(void)
560 return xactStartTimestamp;
564 * GetCurrentStatementStartTimestamp
567 GetCurrentStatementStartTimestamp(void)
569 return stmtStartTimestamp;
573 * GetCurrentTransactionStopTimestamp
575 * We return current time if the transaction stop time hasn't been set
576 * (which can happen if we decide we don't need to log an XLOG record).
579 GetCurrentTransactionStopTimestamp(void)
581 if (xactStopTimestamp != 0)
582 return xactStopTimestamp;
583 return GetCurrentTimestamp();
587 * SetCurrentStatementStartTimestamp
590 SetCurrentStatementStartTimestamp(void)
592 stmtStartTimestamp = GetCurrentTimestamp();
596 * SetCurrentTransactionStopTimestamp
599 SetCurrentTransactionStopTimestamp(void)
601 xactStopTimestamp = GetCurrentTimestamp();
605 * GetCurrentTransactionNestLevel
607 * Note: this will return zero when not inside any transaction, one when
608 * inside a top-level transaction, etc.
611 GetCurrentTransactionNestLevel(void)
613 TransactionState s = CurrentTransactionState;
615 return s->nestingLevel;
620 * TransactionIdIsCurrentTransactionId
623 TransactionIdIsCurrentTransactionId(TransactionId xid)
628 * We always say that BootstrapTransactionId is "not my transaction ID"
629 * even when it is (ie, during bootstrap). Along with the fact that
630 * transam.c always treats BootstrapTransactionId as already committed,
631 * this causes the tqual.c routines to see all tuples as committed, which
632 * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
633 * it never updates or deletes them, so all tuples can be presumed good
636 * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
637 * not my transaction ID, so we can just return "false" immediately for
638 * any non-normal XID.
640 if (!TransactionIdIsNormal(xid))
644 * We will return true for the Xid of the current subtransaction, any of
645 * its subcommitted children, any of its parents, or any of their
646 * previously subcommitted children. However, a transaction being aborted
647 * is no longer "current", even though it may still have an entry on the
650 for (s = CurrentTransactionState; s != NULL; s = s->parent)
655 if (s->state == TRANS_ABORT)
657 if (!TransactionIdIsValid(s->transactionId))
658 continue; /* it can't have any child XIDs either */
659 if (TransactionIdEquals(xid, s->transactionId))
661 /* As the childXids array is ordered, we can use binary search */
663 high = s->nChildXids - 1;
669 middle = low + (high - low) / 2;
670 probe = s->childXids[middle];
671 if (TransactionIdEquals(probe, xid))
673 else if (TransactionIdPrecedes(probe, xid))
684 * TransactionStartedDuringRecovery
686 * Returns true if the current transaction started while recovery was still
687 * in progress. Recovery might have ended since so RecoveryInProgress() might
688 * return false already.
691 TransactionStartedDuringRecovery(void)
693 return CurrentTransactionState->startedInRecovery;
697 * CommandCounterIncrement
700 CommandCounterIncrement(void)
703 * If the current value of the command counter hasn't been "used" to mark
704 * tuples, we need not increment it, since there's no need to distinguish
705 * a read-only command from others. This helps postpone command counter
706 * overflow, and keeps no-op CommandCounterIncrement operations cheap.
708 if (currentCommandIdUsed)
710 currentCommandId += 1;
711 if (currentCommandId == FirstCommandId) /* check for overflow */
713 currentCommandId -= 1;
715 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
716 errmsg("cannot have more than 2^32-1 commands in a transaction")));
718 currentCommandIdUsed = false;
720 /* Propagate new command ID into static snapshots */
721 SnapshotSetCommandId(currentCommandId);
724 * Make any catalog changes done by the just-completed command visible
725 * in the local syscache. We obviously don't need to do this after a
726 * read-only command. (But see hacks in inval.c to make real sure we
727 * don't think a command that queued inval messages was read-only.)
733 * Make any other backends' catalog changes visible to me.
735 * XXX this is probably in the wrong place: CommandCounterIncrement should
736 * be purely a local operation, most likely. However fooling with this
737 * will affect asynchronous cross-backend interactions, which doesn't seem
738 * like a wise thing to do in late beta, so save improving this for
739 * another day - tgl 2007-11-30
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;
917 TransactionId *children;
919 SharedInvalidationMessage *invalMessages = NULL;
920 bool RelcacheInitFileInval;
922 /* Get data needed for commit record */
923 nrels = smgrGetPendingDeletes(true, &rels, &haveNonTemp);
924 nchildren = xactGetCommittedChildren(&children);
925 nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
926 &RelcacheInitFileInval);
929 * If we haven't been assigned an XID yet, we neither can, nor do we want
930 * to write a COMMIT record.
932 if (!markXidCommitted)
935 * We expect that every smgrscheduleunlink is followed by a catalog
936 * update, and hence XID assignment, so we shouldn't get here with any
937 * pending deletes. Use a real test not just an Assert to check this,
938 * since it's a bit fragile.
941 elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
943 /* Can't have child XIDs either; AssignTransactionId enforces this */
944 Assert(nchildren == 0);
947 * If we didn't create XLOG entries, we're done here; otherwise we
948 * should flush those entries the same as a commit record. (An
949 * example of a possible record that wouldn't cause an XID to be
950 * assigned is a sequence advance record due to nextval() --- we want
951 * to flush that to disk before reporting commit.)
953 if (XactLastRecEnd.xrecoff == 0)
959 * Begin commit critical section and insert the commit XLOG record.
961 XLogRecData rdata[4];
963 xl_xact_commit xlrec;
965 /* Tell bufmgr and smgr to prepare for commit */
969 * Set flags required for recovery processing of commits.
972 if (RelcacheInitFileInval)
973 xlrec.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
975 xlrec.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
977 xlrec.dbId = MyDatabaseId;
978 xlrec.tsId = MyDatabaseTableSpace;
981 * Mark ourselves as within our "commit critical section". This
982 * forces any concurrent checkpoint to wait until we've updated
983 * pg_clog. Without this, it is possible for the checkpoint to set
984 * REDO after the XLOG record but fail to flush the pg_clog update to
985 * disk, leading to loss of the transaction commit if the system
986 * crashes a little later.
988 * Note: we could, but don't bother to, set this flag in
989 * RecordTransactionAbort. That's because loss of a transaction abort
990 * is noncritical; the presumption would be that it aborted, anyway.
992 * It's safe to change the inCommit flag of our own backend without
993 * holding the ProcArrayLock, since we're the only one modifying it.
994 * This makes checkpoint's determination of which xacts are inCommit a
995 * bit fuzzy, but it doesn't matter.
997 START_CRIT_SECTION();
998 MyProc->inCommit = true;
1000 SetCurrentTransactionStopTimestamp();
1001 xlrec.xact_time = xactStopTimestamp;
1002 xlrec.nrels = nrels;
1003 xlrec.nsubxacts = nchildren;
1004 xlrec.nmsgs = nmsgs;
1005 rdata[0].data = (char *) (&xlrec);
1006 rdata[0].len = MinSizeOfXactCommit;
1007 rdata[0].buffer = InvalidBuffer;
1008 /* dump rels to delete */
1011 rdata[0].next = &(rdata[1]);
1012 rdata[1].data = (char *) rels;
1013 rdata[1].len = nrels * sizeof(RelFileNode);
1014 rdata[1].buffer = InvalidBuffer;
1017 /* dump committed child Xids */
1020 rdata[lastrdata].next = &(rdata[2]);
1021 rdata[2].data = (char *) children;
1022 rdata[2].len = nchildren * sizeof(TransactionId);
1023 rdata[2].buffer = InvalidBuffer;
1026 /* dump shared cache invalidation messages */
1029 rdata[lastrdata].next = &(rdata[3]);
1030 rdata[3].data = (char *) invalMessages;
1031 rdata[3].len = nmsgs * sizeof(SharedInvalidationMessage);
1032 rdata[3].buffer = InvalidBuffer;
1035 rdata[lastrdata].next = NULL;
1037 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
1041 * Check if we want to commit asynchronously. If the user has set
1042 * synchronous_commit = off, and we're not doing cleanup of any non-temp
1043 * rels nor committing any command that wanted to force sync commit, then
1044 * we can defer flushing XLOG. (We must not allow asynchronous commit if
1045 * there are any non-temp tables to be deleted, because we might delete
1046 * the files before the COMMIT record is flushed to disk. We do allow
1047 * asynchronous commit if all to-be-deleted tables are temporary though,
1048 * since they are lost anyway if we crash.)
1050 if (XactSyncCommit || forceSyncCommit || haveNonTemp)
1053 * Synchronous commit case:
1055 * Sleep before flush! So we can flush more than one commit records
1056 * per single fsync. (The idea is some other backend may do the
1057 * XLogFlush while we're sleeping. This needs work still, because on
1058 * most Unixen, the minimum select() delay is 10msec or more, which is
1061 * We do not sleep if enableFsync is not turned on, nor if there are
1062 * fewer than CommitSiblings other backends with active transactions.
1064 if (CommitDelay > 0 && enableFsync &&
1065 CountActiveBackends() >= CommitSiblings)
1066 pg_usleep(CommitDelay);
1068 XLogFlush(XactLastRecEnd);
1071 * Now we may update the CLOG, if we wrote a COMMIT record above
1073 if (markXidCommitted)
1074 TransactionIdCommitTree(xid, nchildren, children);
1079 * Asynchronous commit case:
1081 * This enables possible committed transaction loss in the case of a
1082 * postmaster crash because WAL buffers are left unwritten. Ideally we
1083 * could issue the WAL write without the fsync, but some
1084 * wal_sync_methods do not allow separate write/fsync.
1086 * Report the latest async commit LSN, so that the WAL writer knows to
1087 * flush this commit.
1089 XLogSetAsyncXactLSN(XactLastRecEnd);
1092 * We must not immediately update the CLOG, since we didn't flush the
1093 * XLOG. Instead, we store the LSN up to which the XLOG must be
1094 * flushed before the CLOG may be updated.
1096 if (markXidCommitted)
1097 TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1101 * If we entered a commit critical section, leave it now, and let
1102 * checkpoints proceed.
1104 if (markXidCommitted)
1106 MyProc->inCommit = false;
1110 /* Compute latestXid while we have the child XIDs handy */
1111 latestXid = TransactionIdLatest(xid, nchildren, children);
1113 /* Reset XactLastRecEnd until the next transaction writes something */
1114 XactLastRecEnd.xrecoff = 0;
1117 /* Clean up local data */
1129 AtCCI_LocalCache(void)
1132 * Make any pending relation map changes visible. We must do this before
1133 * processing local sinval messages, so that the map changes will get
1134 * reflected into the relcache when relcache invals are processed.
1136 AtCCI_RelationMap();
1139 * Make catalog changes visible to me for the next command.
1141 CommandEndInvalidationMessages();
1148 AtCommit_Memory(void)
1151 * Now that we're "out" of a transaction, have the system allocate things
1152 * in the top memory context instead of per-transaction contexts.
1154 MemoryContextSwitchTo(TopMemoryContext);
1157 * Release all transaction-local memory.
1159 Assert(TopTransactionContext != NULL);
1160 MemoryContextDelete(TopTransactionContext);
1161 TopTransactionContext = NULL;
1162 CurTransactionContext = NULL;
1163 CurrentTransactionState->curTransactionContext = NULL;
1166 /* ----------------------------------------------------------------
1167 * CommitSubTransaction stuff
1168 * ----------------------------------------------------------------
1172 * AtSubCommit_Memory
1175 AtSubCommit_Memory(void)
1177 TransactionState s = CurrentTransactionState;
1179 Assert(s->parent != NULL);
1181 /* Return to parent transaction level's memory context. */
1182 CurTransactionContext = s->parent->curTransactionContext;
1183 MemoryContextSwitchTo(CurTransactionContext);
1186 * Ordinarily we cannot throw away the child's CurTransactionContext,
1187 * since the data it contains will be needed at upper commit. However, if
1188 * there isn't actually anything in it, we can throw it away. This avoids
1189 * a small memory leak in the common case of "trivial" subxacts.
1191 if (MemoryContextIsEmpty(s->curTransactionContext))
1193 MemoryContextDelete(s->curTransactionContext);
1194 s->curTransactionContext = NULL;
1199 * AtSubCommit_childXids
1201 * Pass my own XID and my child XIDs up to my parent as committed children.
1204 AtSubCommit_childXids(void)
1206 TransactionState s = CurrentTransactionState;
1209 Assert(s->parent != NULL);
1212 * The parent childXids array will need to hold my XID and all my
1213 * childXids, in addition to the XIDs already there.
1215 new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1217 /* Allocate or enlarge the parent array if necessary */
1218 if (s->parent->maxChildXids < new_nChildXids)
1220 int new_maxChildXids;
1221 TransactionId *new_childXids;
1224 * Make it 2x what's needed right now, to avoid having to enlarge it
1225 * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1226 * is what ensures that we don't need to worry about integer overflow
1227 * here or in the calculation of new_nChildXids.)
1229 new_maxChildXids = Min(new_nChildXids * 2,
1230 (int) (MaxAllocSize / sizeof(TransactionId)));
1232 if (new_maxChildXids < new_nChildXids)
1234 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1235 errmsg("maximum number of committed subtransactions (%d) exceeded",
1236 (int) (MaxAllocSize / sizeof(TransactionId)))));
1239 * We keep the child-XID arrays in TopTransactionContext; this avoids
1240 * setting up child-transaction contexts for what might be just a few
1241 * bytes of grandchild XIDs.
1243 if (s->parent->childXids == NULL)
1245 MemoryContextAlloc(TopTransactionContext,
1246 new_maxChildXids * sizeof(TransactionId));
1248 new_childXids = repalloc(s->parent->childXids,
1249 new_maxChildXids * sizeof(TransactionId));
1251 s->parent->childXids = new_childXids;
1252 s->parent->maxChildXids = new_maxChildXids;
1256 * Copy all my XIDs to parent's array.
1258 * Note: We rely on the fact that the XID of a child always follows that
1259 * of its parent. By copying the XID of this subtransaction before the
1260 * XIDs of its children, we ensure that the array stays ordered. Likewise,
1261 * all XIDs already in the array belong to subtransactions started and
1262 * subcommitted before us, so their XIDs must precede ours.
1264 s->parent->childXids[s->parent->nChildXids] = s->transactionId;
1266 if (s->nChildXids > 0)
1267 memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1269 s->nChildXids * sizeof(TransactionId));
1271 s->parent->nChildXids = new_nChildXids;
1273 /* Release child's array to avoid leakage */
1274 if (s->childXids != NULL)
1275 pfree(s->childXids);
1276 /* We must reset these to avoid double-free if fail later in commit */
1277 s->childXids = NULL;
1279 s->maxChildXids = 0;
1282 /* ----------------------------------------------------------------
1283 * AbortTransaction stuff
1284 * ----------------------------------------------------------------
1288 * RecordTransactionAbort
1290 * Returns latest XID among xact and its children, or InvalidTransactionId
1291 * if the xact has no XID. (We compute that here just because it's easier.)
1293 static TransactionId
1294 RecordTransactionAbort(bool isSubXact)
1296 TransactionId xid = GetCurrentTransactionIdIfAny();
1297 TransactionId latestXid;
1301 TransactionId *children;
1302 XLogRecData rdata[3];
1304 xl_xact_abort xlrec;
1307 * If we haven't been assigned an XID, nobody will care whether we aborted
1308 * or not. Hence, we're done in that case. It does not matter if we have
1309 * rels to delete (note that this routine is not responsible for actually
1310 * deleting 'em). We cannot have any child XIDs, either.
1312 if (!TransactionIdIsValid(xid))
1314 /* Reset XactLastRecEnd until the next transaction writes something */
1316 XactLastRecEnd.xrecoff = 0;
1317 return InvalidTransactionId;
1321 * We have a valid XID, so we should write an ABORT record for it.
1323 * We do not flush XLOG to disk here, since the default assumption after a
1324 * crash would be that we aborted, anyway. For the same reason, we don't
1325 * need to worry about interlocking against checkpoint start.
1329 * Check that we haven't aborted halfway through RecordTransactionCommit.
1331 if (TransactionIdDidCommit(xid))
1332 elog(PANIC, "cannot abort transaction %u, it was already committed",
1335 /* Fetch the data we need for the abort record */
1336 nrels = smgrGetPendingDeletes(false, &rels, NULL);
1337 nchildren = xactGetCommittedChildren(&children);
1339 /* XXX do we really need a critical section here? */
1340 START_CRIT_SECTION();
1342 /* Write the ABORT record */
1344 xlrec.xact_time = GetCurrentTimestamp();
1347 SetCurrentTransactionStopTimestamp();
1348 xlrec.xact_time = xactStopTimestamp;
1350 xlrec.nrels = nrels;
1351 xlrec.nsubxacts = nchildren;
1352 rdata[0].data = (char *) (&xlrec);
1353 rdata[0].len = MinSizeOfXactAbort;
1354 rdata[0].buffer = InvalidBuffer;
1355 /* dump rels to delete */
1358 rdata[0].next = &(rdata[1]);
1359 rdata[1].data = (char *) rels;
1360 rdata[1].len = nrels * sizeof(RelFileNode);
1361 rdata[1].buffer = InvalidBuffer;
1364 /* dump committed child Xids */
1367 rdata[lastrdata].next = &(rdata[2]);
1368 rdata[2].data = (char *) children;
1369 rdata[2].len = nchildren * sizeof(TransactionId);
1370 rdata[2].buffer = InvalidBuffer;
1373 rdata[lastrdata].next = NULL;
1375 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);
1378 * Report the latest async abort LSN, so that the WAL writer knows to
1379 * flush this abort. There's nothing to be gained by delaying this, since
1380 * WALWriter may as well do this when it can. This is important with
1381 * streaming replication because if we don't flush WAL regularly we will
1382 * find that large aborts leave us with a long backlog for when commits
1383 * occur after the abort, increasing our window of data loss should
1384 * problems occur at that point.
1387 XLogSetAsyncXactLSN(XactLastRecEnd);
1390 * Mark the transaction aborted in clog. This is not absolutely necessary
1391 * but we may as well do it while we are here; also, in the subxact case
1392 * it is helpful because XactLockTableWait makes use of it to avoid
1393 * waiting for already-aborted subtransactions. It is OK to do it without
1394 * having flushed the ABORT record to disk, because in event of a crash
1395 * we'd be assumed to have aborted anyway.
1397 TransactionIdAbortTree(xid, nchildren, children);
1401 /* Compute latestXid while we have the child XIDs handy */
1402 latestXid = TransactionIdLatest(xid, nchildren, children);
1405 * If we're aborting a subtransaction, we can immediately remove failed
1406 * XIDs from PGPROC's cache of running child XIDs. We do that here for
1407 * subxacts, because we already have the child XID array at hand. For
1408 * main xacts, the equivalent happens just after this function returns.
1411 XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1413 /* Reset XactLastRecEnd until the next transaction writes something */
1415 XactLastRecEnd.xrecoff = 0;
1417 /* And clean up local data */
1428 AtAbort_Memory(void)
1431 * Switch into TransactionAbortContext, which should have some free space
1432 * even if nothing else does. We'll work in this context until we've
1433 * finished cleaning up.
1435 * It is barely possible to get here when we've not been able to create
1436 * TransactionAbortContext yet; if so use TopMemoryContext.
1438 if (TransactionAbortContext != NULL)
1439 MemoryContextSwitchTo(TransactionAbortContext);
1441 MemoryContextSwitchTo(TopMemoryContext);
1448 AtSubAbort_Memory(void)
1450 Assert(TransactionAbortContext != NULL);
1452 MemoryContextSwitchTo(TransactionAbortContext);
1457 * AtAbort_ResourceOwner
1460 AtAbort_ResourceOwner(void)
1463 * Make sure we have a valid ResourceOwner, if possible (else it will be
1464 * NULL, which is OK)
1466 CurrentResourceOwner = TopTransactionResourceOwner;
1470 * AtSubAbort_ResourceOwner
1473 AtSubAbort_ResourceOwner(void)
1475 TransactionState s = CurrentTransactionState;
1477 /* Make sure we have a valid ResourceOwner */
1478 CurrentResourceOwner = s->curTransactionOwner;
1483 * AtSubAbort_childXids
1486 AtSubAbort_childXids(void)
1488 TransactionState s = CurrentTransactionState;
1491 * We keep the child-XID arrays in TopTransactionContext (see
1492 * AtSubCommit_childXids). This means we'd better free the array
1493 * explicitly at abort to avoid leakage.
1495 if (s->childXids != NULL)
1496 pfree(s->childXids);
1497 s->childXids = NULL;
1499 s->maxChildXids = 0;
1502 * We could prune the unreportedXids array here. But we don't bother. That
1503 * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1504 * would likely introduce more CPU time into the more common paths, so we
1505 * choose not to do that.
1509 /* ----------------------------------------------------------------
1510 * CleanupTransaction stuff
1511 * ----------------------------------------------------------------
1518 AtCleanup_Memory(void)
1520 Assert(CurrentTransactionState->parent == NULL);
1523 * Now that we're "out" of a transaction, have the system allocate things
1524 * in the top memory context instead of per-transaction contexts.
1526 MemoryContextSwitchTo(TopMemoryContext);
1529 * Clear the special abort context for next time.
1531 if (TransactionAbortContext != NULL)
1532 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1535 * Release all transaction-local memory.
1537 if (TopTransactionContext != NULL)
1538 MemoryContextDelete(TopTransactionContext);
1539 TopTransactionContext = NULL;
1540 CurTransactionContext = NULL;
1541 CurrentTransactionState->curTransactionContext = NULL;
1545 /* ----------------------------------------------------------------
1546 * CleanupSubTransaction stuff
1547 * ----------------------------------------------------------------
1551 * AtSubCleanup_Memory
1554 AtSubCleanup_Memory(void)
1556 TransactionState s = CurrentTransactionState;
1558 Assert(s->parent != NULL);
1560 /* Make sure we're not in an about-to-be-deleted context */
1561 MemoryContextSwitchTo(s->parent->curTransactionContext);
1562 CurTransactionContext = s->parent->curTransactionContext;
1565 * Clear the special abort context for next time.
1567 if (TransactionAbortContext != NULL)
1568 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1571 * Delete the subxact local memory contexts. Its CurTransactionContext can
1572 * go too (note this also kills CurTransactionContexts from any children
1575 if (s->curTransactionContext)
1576 MemoryContextDelete(s->curTransactionContext);
1577 s->curTransactionContext = NULL;
1580 /* ----------------------------------------------------------------
1581 * interface routines
1582 * ----------------------------------------------------------------
1589 StartTransaction(void)
1592 VirtualTransactionId vxid;
1595 * Let's just make sure the state stack is empty
1597 s = &TopTransactionStateData;
1598 CurrentTransactionState = s;
1601 * check the current transaction state
1603 if (s->state != TRANS_DEFAULT)
1604 elog(WARNING, "StartTransaction while in %s state",
1605 TransStateAsString(s->state));
1608 * set the current transaction state information appropriately during
1611 s->state = TRANS_START;
1612 s->transactionId = InvalidTransactionId; /* until assigned */
1615 * Make sure we've reset xact state variables
1617 * If recovery is still in progress, mark this transaction as read-only.
1618 * We have lower level defences in XLogInsert and elsewhere to stop us
1619 * from modifying data during recovery, but this gives the normal
1620 * indication to the user that the transaction is read-only.
1622 if (RecoveryInProgress())
1624 s->startedInRecovery = true;
1625 XactReadOnly = true;
1629 s->startedInRecovery = false;
1630 XactReadOnly = DefaultXactReadOnly;
1632 XactIsoLevel = DefaultXactIsoLevel;
1633 forceSyncCommit = false;
1634 MyXactAccessedTempRel = false;
1637 * reinitialize within-transaction counters
1639 s->subTransactionId = TopSubTransactionId;
1640 currentSubTransactionId = TopSubTransactionId;
1641 currentCommandId = FirstCommandId;
1642 currentCommandIdUsed = false;
1645 * initialize reported xid accounting
1647 nUnreportedXids = 0;
1650 * must initialize resource-management stuff first
1653 AtStart_ResourceOwner();
1656 * Assign a new LocalTransactionId, and combine it with the backendId to
1657 * form a virtual transaction id.
1659 vxid.backendId = MyBackendId;
1660 vxid.localTransactionId = GetNextLocalTransactionId();
1663 * Lock the virtual transaction id before we announce it in the proc array
1665 VirtualXactLockTableInsert(vxid);
1668 * Advertise it in the proc array. We assume assignment of
1669 * LocalTransactionID is atomic, and the backendId should be set already.
1671 Assert(MyProc->backendId == vxid.backendId);
1672 MyProc->lxid = vxid.localTransactionId;
1674 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1677 * set transaction_timestamp() (a/k/a now()). We want this to be the same
1678 * as the first command's statement_timestamp(), so don't do a fresh
1679 * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1680 * xactStopTimestamp as unset.
1682 xactStartTimestamp = stmtStartTimestamp;
1683 xactStopTimestamp = 0;
1684 pgstat_report_xact_timestamp(xactStartTimestamp);
1687 * initialize current transaction state fields
1689 * note: prevXactReadOnly is not used at the outermost level
1691 s->nestingLevel = 1;
1692 s->gucNestLevel = 1;
1693 s->childXids = NULL;
1695 s->maxChildXids = 0;
1696 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1697 /* SecurityRestrictionContext should never be set outside a transaction */
1698 Assert(s->prevSecContext == 0);
1701 * initialize other subsystems for new transaction
1706 AfterTriggerBeginXact();
1709 * done with start processing, set current transaction state to "in
1712 s->state = TRANS_INPROGRESS;
1714 ShowTransactionState("StartTransaction");
1721 * NB: if you change this routine, better look at PrepareTransaction too!
1724 CommitTransaction(void)
1726 TransactionState s = CurrentTransactionState;
1727 TransactionId latestXid;
1729 ShowTransactionState("CommitTransaction");
1732 * check the current transaction state
1734 if (s->state != TRANS_INPROGRESS)
1735 elog(WARNING, "CommitTransaction while in %s state",
1736 TransStateAsString(s->state));
1737 Assert(s->parent == NULL);
1740 * Do pre-commit processing (most of this stuff requires database access,
1741 * and in fact could still cause an error...)
1743 * It is possible for CommitHoldablePortals to invoke functions that queue
1744 * deferred triggers, and it's also possible that triggers create holdable
1745 * cursors. So we have to loop until there's nothing left to do.
1750 * Fire all currently pending deferred triggers.
1752 AfterTriggerFireDeferred();
1755 * Convert any open holdable cursors into static portals. If there
1756 * weren't any, we are done ... otherwise loop back to check if they
1757 * queued deferred triggers. Lather, rinse, repeat.
1759 if (!CommitHoldablePortals())
1763 /* Now we can shut down the deferred-trigger manager */
1764 AfterTriggerEndXact(true);
1766 /* Close any open regular cursors */
1770 * Let ON COMMIT management do its thing (must happen after closing
1771 * cursors, to avoid dangling-reference problems)
1773 PreCommit_on_commit_actions();
1775 /* close large objects before lower-level cleanup */
1776 AtEOXact_LargeObject(true);
1779 * Insert notifications sent by NOTIFY commands into the queue. This
1780 * should be late in the pre-commit sequence to minimize time spent
1781 * holding the notify-insertion lock.
1785 /* Prevent cancel/die interrupt while cleaning up */
1788 /* Commit updates to the relation map --- do this as late as possible */
1789 AtEOXact_RelationMap(true);
1792 * set the current transaction state information appropriately during
1795 s->state = TRANS_COMMIT;
1798 * Here is where we really truly commit.
1800 latestXid = RecordTransactionCommit();
1802 TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
1805 * Let others know about no transaction in progress by me. Note that this
1806 * must be done _before_ releasing locks we hold and _after_
1807 * RecordTransactionCommit.
1809 ProcArrayEndTransaction(MyProc, latestXid);
1812 * This is all post-commit cleanup. Note that if an error is raised here,
1813 * it's too late to abort the transaction. This should be just
1814 * noncritical resource releasing.
1816 * The ordering of operations is not entirely random. The idea is:
1817 * release resources visible to other backends (eg, files, buffer pins);
1818 * then release locks; then release backend-local resources. We want to
1819 * release locks at the point where any backend waiting for us will see
1820 * our transaction as being fully cleaned up.
1822 * Resources that can be associated with individual queries are handled by
1823 * the ResourceOwner mechanism. The other calls here are for backend-wide
1827 CallXactCallbacks(XACT_EVENT_COMMIT);
1829 ResourceOwnerRelease(TopTransactionResourceOwner,
1830 RESOURCE_RELEASE_BEFORE_LOCKS,
1833 /* Check we've released all buffer pins */
1834 AtEOXact_Buffers(true);
1836 /* Clean up the relation cache */
1837 AtEOXact_RelationCache(true);
1839 /* Clean up the snapshot manager */
1840 AtEarlyCommit_Snapshot();
1843 * Make catalog changes visible to all backends. This has to happen after
1844 * relcache references are dropped (see comments for
1845 * AtEOXact_RelationCache), but before locks are released (if anyone is
1846 * waiting for lock on a relation we've modified, we want them to know
1847 * about the catalog change before they start using the relation).
1849 AtEOXact_Inval(true);
1852 * Likewise, dropping of files deleted during the transaction is best done
1853 * after releasing relcache and buffer pins. (This is not strictly
1854 * necessary during commit, since such pins should have been released
1855 * already, but this ordering is definitely critical during abort.)
1857 smgrDoPendingDeletes(true);
1859 AtEOXact_MultiXact();
1861 ResourceOwnerRelease(TopTransactionResourceOwner,
1862 RESOURCE_RELEASE_LOCKS,
1864 ResourceOwnerRelease(TopTransactionResourceOwner,
1865 RESOURCE_RELEASE_AFTER_LOCKS,
1868 /* Check we've released all catcache entries */
1869 AtEOXact_CatCache(true);
1872 AtEOXact_GUC(true, 1);
1874 AtEOXact_on_commit_actions(true);
1875 AtEOXact_Namespace(true);
1876 /* smgrcommit already done */
1878 AtEOXact_ComboCid();
1879 AtEOXact_HashTables(true);
1880 AtEOXact_PgStat(true);
1881 AtEOXact_Snapshot(true);
1882 pgstat_report_xact_timestamp(0);
1884 CurrentResourceOwner = NULL;
1885 ResourceOwnerDelete(TopTransactionResourceOwner);
1886 s->curTransactionOwner = NULL;
1887 CurTransactionResourceOwner = NULL;
1888 TopTransactionResourceOwner = NULL;
1892 s->transactionId = InvalidTransactionId;
1893 s->subTransactionId = InvalidSubTransactionId;
1894 s->nestingLevel = 0;
1895 s->gucNestLevel = 0;
1896 s->childXids = NULL;
1898 s->maxChildXids = 0;
1901 * done with commit processing, set current transaction state back to
1904 s->state = TRANS_DEFAULT;
1906 RESUME_INTERRUPTS();
1911 * PrepareTransaction
1913 * NB: if you change this routine, better look at CommitTransaction too!
1916 PrepareTransaction(void)
1918 TransactionState s = CurrentTransactionState;
1919 TransactionId xid = GetCurrentTransactionId();
1920 GlobalTransaction gxact;
1921 TimestampTz prepared_at;
1923 ShowTransactionState("PrepareTransaction");
1926 * check the current transaction state
1928 if (s->state != TRANS_INPROGRESS)
1929 elog(WARNING, "PrepareTransaction while in %s state",
1930 TransStateAsString(s->state));
1931 Assert(s->parent == NULL);
1934 * Do pre-commit processing (most of this stuff requires database access,
1935 * and in fact could still cause an error...)
1937 * It is possible for PrepareHoldablePortals to invoke functions that
1938 * queue deferred triggers, and it's also possible that triggers create
1939 * holdable cursors. So we have to loop until there's nothing left to do.
1944 * Fire all currently pending deferred triggers.
1946 AfterTriggerFireDeferred();
1949 * Convert any open holdable cursors into static portals. If there
1950 * weren't any, we are done ... otherwise loop back to check if they
1951 * queued deferred triggers. Lather, rinse, repeat.
1953 if (!PrepareHoldablePortals())
1957 /* Now we can shut down the deferred-trigger manager */
1958 AfterTriggerEndXact(true);
1960 /* Close any open regular cursors */
1964 * Let ON COMMIT management do its thing (must happen after closing
1965 * cursors, to avoid dangling-reference problems)
1967 PreCommit_on_commit_actions();
1969 /* close large objects before lower-level cleanup */
1970 AtEOXact_LargeObject(true);
1972 /* NOTIFY will be handled below */
1975 * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
1976 * this transaction. Having the prepared xact hold locks on another
1977 * backend's temp table seems a bad idea --- for instance it would prevent
1978 * the backend from exiting. There are other problems too, such as how to
1979 * clean up the source backend's local buffers and ON COMMIT state if the
1980 * prepared xact includes a DROP of a temp table.
1982 * We must check this after executing any ON COMMIT actions, because they
1983 * might still access a temp relation.
1985 * XXX In principle this could be relaxed to allow some useful special
1986 * cases, such as a temp table created and dropped all within the
1987 * transaction. That seems to require much more bookkeeping though.
1989 if (MyXactAccessedTempRel)
1991 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1992 errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
1994 /* Prevent cancel/die interrupt while cleaning up */
1998 * set the current transaction state information appropriately during
1999 * prepare processing
2001 s->state = TRANS_PREPARE;
2003 prepared_at = GetCurrentTimestamp();
2005 /* Tell bufmgr and smgr to prepare for commit */
2009 * Reserve the GID for this transaction. This could fail if the requested
2010 * GID is invalid or already in use.
2012 gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2013 GetUserId(), MyDatabaseId);
2017 * Collect data for the 2PC state file. Note that in general, no actual
2018 * state change should happen in the called modules during this step,
2019 * since it's still possible to fail before commit, and in that case we
2020 * want transaction abort to be able to clean up. (In particular, the
2021 * AtPrepare routines may error out if they find cases they cannot
2022 * handle.) State cleanup should happen in the PostPrepare routines
2023 * below. However, some modules can go ahead and clear state here because
2024 * they wouldn't do anything with it during abort anyway.
2026 * Note: because the 2PC state file records will be replayed in the same
2027 * order they are made, the order of these calls has to match the order in
2028 * which we want things to happen during COMMIT PREPARED or ROLLBACK
2029 * PREPARED; in particular, pay attention to whether things should happen
2030 * before or after releasing the transaction's locks.
2032 StartPrepare(gxact);
2037 AtPrepare_MultiXact();
2038 AtPrepare_RelationMap();
2041 * Here is where we really truly prepare.
2043 * We have to record transaction prepares even if we didn't make any
2044 * updates, because the transaction manager might get confused if we lose
2045 * a global transaction.
2050 * Now we clean up backend-internal state and release internal resources.
2053 /* Reset XactLastRecEnd until the next transaction writes something */
2054 XactLastRecEnd.xrecoff = 0;
2057 * Let others know about no transaction in progress by me. This has to be
2058 * done *after* the prepared transaction has been marked valid, else
2059 * someone may think it is unlocked and recyclable.
2061 ProcArrayClearTransaction(MyProc);
2064 * This is all post-transaction cleanup. Note that if an error is raised
2065 * here, it's too late to abort the transaction. This should be just
2066 * noncritical resource releasing. See notes in CommitTransaction.
2069 CallXactCallbacks(XACT_EVENT_PREPARE);
2071 ResourceOwnerRelease(TopTransactionResourceOwner,
2072 RESOURCE_RELEASE_BEFORE_LOCKS,
2075 /* Check we've released all buffer pins */
2076 AtEOXact_Buffers(true);
2078 /* Clean up the relation cache */
2079 AtEOXact_RelationCache(true);
2081 /* Clean up the snapshot manager */
2082 AtEarlyCommit_Snapshot();
2084 /* notify doesn't need a postprepare call */
2086 PostPrepare_PgStat();
2088 PostPrepare_Inval();
2092 PostPrepare_MultiXact(xid);
2094 PostPrepare_Locks(xid);
2096 ResourceOwnerRelease(TopTransactionResourceOwner,
2097 RESOURCE_RELEASE_LOCKS,
2099 ResourceOwnerRelease(TopTransactionResourceOwner,
2100 RESOURCE_RELEASE_AFTER_LOCKS,
2103 /* Check we've released all catcache entries */
2104 AtEOXact_CatCache(true);
2106 /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2107 AtEOXact_GUC(true, 1);
2109 AtEOXact_on_commit_actions(true);
2110 AtEOXact_Namespace(true);
2111 /* smgrcommit already done */
2113 AtEOXact_ComboCid();
2114 AtEOXact_HashTables(true);
2115 /* don't call AtEOXact_PgStat here */
2116 AtEOXact_Snapshot(true);
2118 CurrentResourceOwner = NULL;
2119 ResourceOwnerDelete(TopTransactionResourceOwner);
2120 s->curTransactionOwner = NULL;
2121 CurTransactionResourceOwner = NULL;
2122 TopTransactionResourceOwner = NULL;
2126 s->transactionId = InvalidTransactionId;
2127 s->subTransactionId = InvalidSubTransactionId;
2128 s->nestingLevel = 0;
2129 s->gucNestLevel = 0;
2130 s->childXids = NULL;
2132 s->maxChildXids = 0;
2135 * done with 1st phase commit processing, set current transaction state
2138 s->state = TRANS_DEFAULT;
2140 RESUME_INTERRUPTS();
2148 AbortTransaction(void)
2150 TransactionState s = CurrentTransactionState;
2151 TransactionId latestXid;
2153 /* Prevent cancel/die interrupt while cleaning up */
2156 /* Make sure we have a valid memory context and resource owner */
2158 AtAbort_ResourceOwner();
2161 * Release any LW locks we might be holding as quickly as possible.
2162 * (Regular locks, however, must be held till we finish aborting.)
2163 * Releasing LW locks is critical since we might try to grab them again
2164 * while cleaning up!
2168 /* Clean up buffer I/O and buffer context locks, too */
2173 * Also clean up any open wait for lock, since the lock manager will choke
2174 * if we try to wait for another lock before doing this.
2179 * check the current transaction state
2181 if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2182 elog(WARNING, "AbortTransaction while in %s state",
2183 TransStateAsString(s->state));
2184 Assert(s->parent == NULL);
2187 * set the current transaction state information appropriately during the
2190 s->state = TRANS_ABORT;
2193 * Reset user ID which might have been changed transiently. We need this
2194 * to clean up in case control escaped out of a SECURITY DEFINER function
2195 * or other local change of CurrentUserId; therefore, the prior value of
2196 * SecurityRestrictionContext also needs to be restored.
2198 * (Note: it is not necessary to restore session authorization or role
2199 * settings here because those can only be changed via GUC, and GUC will
2200 * take care of rolling them back if need be.)
2202 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2205 * do abort processing
2207 AfterTriggerEndXact(false); /* 'false' means it's abort */
2209 AtEOXact_LargeObject(false);
2211 AtEOXact_RelationMap(false);
2214 * Advertise the fact that we aborted in pg_clog (assuming that we got as
2215 * far as assigning an XID to advertise).
2217 latestXid = RecordTransactionAbort(false);
2219 TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2222 * Let others know about no transaction in progress by me. Note that this
2223 * must be done _before_ releasing locks we hold and _after_
2224 * RecordTransactionAbort.
2226 ProcArrayEndTransaction(MyProc, latestXid);
2229 * Post-abort cleanup. See notes in CommitTransaction() concerning
2230 * ordering. We can skip all of it if the transaction failed before
2231 * creating a resource owner.
2233 if (TopTransactionResourceOwner != NULL)
2235 CallXactCallbacks(XACT_EVENT_ABORT);
2237 ResourceOwnerRelease(TopTransactionResourceOwner,
2238 RESOURCE_RELEASE_BEFORE_LOCKS,
2240 AtEOXact_Buffers(false);
2241 AtEOXact_RelationCache(false);
2242 AtEOXact_Inval(false);
2243 smgrDoPendingDeletes(false);
2244 AtEOXact_MultiXact();
2245 ResourceOwnerRelease(TopTransactionResourceOwner,
2246 RESOURCE_RELEASE_LOCKS,
2248 ResourceOwnerRelease(TopTransactionResourceOwner,
2249 RESOURCE_RELEASE_AFTER_LOCKS,
2251 AtEOXact_CatCache(false);
2253 AtEOXact_GUC(false, 1);
2254 AtEOXact_SPI(false);
2255 AtEOXact_on_commit_actions(false);
2256 AtEOXact_Namespace(false);
2258 AtEOXact_ComboCid();
2259 AtEOXact_HashTables(false);
2260 AtEOXact_PgStat(false);
2261 AtEOXact_Snapshot(false);
2262 pgstat_report_xact_timestamp(0);
2266 * State remains TRANS_ABORT until CleanupTransaction().
2268 RESUME_INTERRUPTS();
2272 * CleanupTransaction
2275 CleanupTransaction(void)
2277 TransactionState s = CurrentTransactionState;
2280 * State should still be TRANS_ABORT from AbortTransaction().
2282 if (s->state != TRANS_ABORT)
2283 elog(FATAL, "CleanupTransaction: unexpected state %s",
2284 TransStateAsString(s->state));
2287 * do abort cleanup processing
2289 AtCleanup_Portals(); /* now safe to release portal memory */
2291 CurrentResourceOwner = NULL; /* and resource owner */
2292 if (TopTransactionResourceOwner)
2293 ResourceOwnerDelete(TopTransactionResourceOwner);
2294 s->curTransactionOwner = NULL;
2295 CurTransactionResourceOwner = NULL;
2296 TopTransactionResourceOwner = NULL;
2298 AtCleanup_Memory(); /* and transaction memory */
2300 s->transactionId = InvalidTransactionId;
2301 s->subTransactionId = InvalidSubTransactionId;
2302 s->nestingLevel = 0;
2303 s->gucNestLevel = 0;
2304 s->childXids = NULL;
2306 s->maxChildXids = 0;
2309 * done with abort processing, set current transaction state back to
2312 s->state = TRANS_DEFAULT;
2316 * StartTransactionCommand
2319 StartTransactionCommand(void)
2321 TransactionState s = CurrentTransactionState;
2323 switch (s->blockState)
2326 * if we aren't in a transaction block, we just do our usual start
2329 case TBLOCK_DEFAULT:
2331 s->blockState = TBLOCK_STARTED;
2335 * We are somewhere in a transaction block or subtransaction and
2336 * about to start a new command. For now we do nothing, but
2337 * someday we may do command-local resource initialization. (Note
2338 * that any needed CommandCounterIncrement was done by the
2339 * previous CommitTransactionCommand.)
2341 case TBLOCK_INPROGRESS:
2342 case TBLOCK_SUBINPROGRESS:
2346 * Here we are in a failed transaction block (one of the commands
2347 * caused an abort) so we do nothing but remain in the abort
2348 * state. Eventually we will get a ROLLBACK command which will
2349 * get us out of this state. (It is up to other code to ensure
2350 * that no commands other than ROLLBACK will be processed in these
2354 case TBLOCK_SUBABORT:
2357 /* These cases are invalid. */
2358 case TBLOCK_STARTED:
2360 case TBLOCK_SUBBEGIN:
2363 case TBLOCK_ABORT_END:
2364 case TBLOCK_SUBABORT_END:
2365 case TBLOCK_ABORT_PENDING:
2366 case TBLOCK_SUBABORT_PENDING:
2367 case TBLOCK_SUBRESTART:
2368 case TBLOCK_SUBABORT_RESTART:
2369 case TBLOCK_PREPARE:
2370 elog(ERROR, "StartTransactionCommand: unexpected state %s",
2371 BlockStateAsString(s->blockState));
2376 * We must switch to CurTransactionContext before returning. This is
2377 * already done if we called StartTransaction, otherwise not.
2379 Assert(CurTransactionContext != NULL);
2380 MemoryContextSwitchTo(CurTransactionContext);
2384 * CommitTransactionCommand
2387 CommitTransactionCommand(void)
2389 TransactionState s = CurrentTransactionState;
2391 switch (s->blockState)
2394 * This shouldn't happen, because it means the previous
2395 * StartTransactionCommand didn't set the STARTED state
2398 case TBLOCK_DEFAULT:
2399 elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2400 BlockStateAsString(s->blockState));
2404 * If we aren't in a transaction block, just do our usual
2405 * transaction commit, and return to the idle state.
2407 case TBLOCK_STARTED:
2408 CommitTransaction();
2409 s->blockState = TBLOCK_DEFAULT;
2413 * We are completing a "BEGIN TRANSACTION" command, so we change
2414 * to the "transaction block in progress" state and return. (We
2415 * assume the BEGIN did nothing to the database, so we need no
2416 * CommandCounterIncrement.)
2419 s->blockState = TBLOCK_INPROGRESS;
2423 * This is the case when we have finished executing a command
2424 * someplace within a transaction block. We increment the command
2425 * counter and return.
2427 case TBLOCK_INPROGRESS:
2428 case TBLOCK_SUBINPROGRESS:
2429 CommandCounterIncrement();
2433 * We are completing a "COMMIT" command. Do it and return to the
2437 CommitTransaction();
2438 s->blockState = TBLOCK_DEFAULT;
2442 * Here we are in the middle of a transaction block but one of the
2443 * commands caused an abort so we do nothing but remain in the
2444 * abort state. Eventually we will get a ROLLBACK comand.
2447 case TBLOCK_SUBABORT:
2451 * Here we were in an aborted transaction block and we just got
2452 * the ROLLBACK command from the user, so clean up the
2453 * already-aborted transaction and return to the idle state.
2455 case TBLOCK_ABORT_END:
2456 CleanupTransaction();
2457 s->blockState = TBLOCK_DEFAULT;
2461 * Here we were in a perfectly good transaction block but the user
2462 * told us to ROLLBACK anyway. We have to abort the transaction
2463 * and then clean up.
2465 case TBLOCK_ABORT_PENDING:
2467 CleanupTransaction();
2468 s->blockState = TBLOCK_DEFAULT;
2472 * We are completing a "PREPARE TRANSACTION" command. Do it and
2473 * return to the idle state.
2475 case TBLOCK_PREPARE:
2476 PrepareTransaction();
2477 s->blockState = TBLOCK_DEFAULT;
2481 * We were just issued a SAVEPOINT inside a transaction block.
2482 * Start a subtransaction. (DefineSavepoint already did
2483 * PushTransaction, so as to have someplace to put the SUBBEGIN
2486 case TBLOCK_SUBBEGIN:
2487 StartSubTransaction();
2488 s->blockState = TBLOCK_SUBINPROGRESS;
2492 * We were issued a COMMIT or RELEASE command, so we end the
2493 * current subtransaction and return to the parent transaction.
2494 * The parent might be ended too, so repeat till we are all the
2495 * way out or find an INPROGRESS transaction.
2500 CommitSubTransaction();
2501 s = CurrentTransactionState; /* changed by pop */
2502 } while (s->blockState == TBLOCK_SUBEND);
2503 /* If we had a COMMIT command, finish off the main xact too */
2504 if (s->blockState == TBLOCK_END)
2506 Assert(s->parent == NULL);
2507 CommitTransaction();
2508 s->blockState = TBLOCK_DEFAULT;
2510 else if (s->blockState == TBLOCK_PREPARE)
2512 Assert(s->parent == NULL);
2513 PrepareTransaction();
2514 s->blockState = TBLOCK_DEFAULT;
2518 Assert(s->blockState == TBLOCK_INPROGRESS ||
2519 s->blockState == TBLOCK_SUBINPROGRESS);
2524 * The current already-failed subtransaction is ending due to a
2525 * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2526 * examine the parent (which could be in any of several states).
2528 case TBLOCK_SUBABORT_END:
2529 CleanupSubTransaction();
2530 CommitTransactionCommand();
2534 * As above, but it's not dead yet, so abort first.
2536 case TBLOCK_SUBABORT_PENDING:
2537 AbortSubTransaction();
2538 CleanupSubTransaction();
2539 CommitTransactionCommand();
2543 * The current subtransaction is the target of a ROLLBACK TO
2544 * command. Abort and pop it, then start a new subtransaction
2545 * with the same name.
2547 case TBLOCK_SUBRESTART:
2552 /* save name and keep Cleanup from freeing it */
2555 savepointLevel = s->savepointLevel;
2557 AbortSubTransaction();
2558 CleanupSubTransaction();
2560 DefineSavepoint(NULL);
2561 s = CurrentTransactionState; /* changed by push */
2563 s->savepointLevel = savepointLevel;
2565 /* This is the same as TBLOCK_SUBBEGIN case */
2566 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2567 StartSubTransaction();
2568 s->blockState = TBLOCK_SUBINPROGRESS;
2573 * Same as above, but the subtransaction had already failed, so we
2574 * don't need AbortSubTransaction.
2576 case TBLOCK_SUBABORT_RESTART:
2581 /* save name and keep Cleanup from freeing it */
2584 savepointLevel = s->savepointLevel;
2586 CleanupSubTransaction();
2588 DefineSavepoint(NULL);
2589 s = CurrentTransactionState; /* changed by push */
2591 s->savepointLevel = savepointLevel;
2593 /* This is the same as TBLOCK_SUBBEGIN case */
2594 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2595 StartSubTransaction();
2596 s->blockState = TBLOCK_SUBINPROGRESS;
2603 * AbortCurrentTransaction
2606 AbortCurrentTransaction(void)
2608 TransactionState s = CurrentTransactionState;
2610 switch (s->blockState)
2612 case TBLOCK_DEFAULT:
2613 if (s->state == TRANS_DEFAULT)
2615 /* we are idle, so nothing to do */
2620 * We can get here after an error during transaction start
2621 * (state will be TRANS_START). Need to clean up the
2622 * incompletely started transaction. First, adjust the
2623 * low-level state to suppress warning message from
2626 if (s->state == TRANS_START)
2627 s->state = TRANS_INPROGRESS;
2629 CleanupTransaction();
2634 * if we aren't in a transaction block, we just do the basic abort
2635 * & cleanup transaction.
2637 case TBLOCK_STARTED:
2639 CleanupTransaction();
2640 s->blockState = TBLOCK_DEFAULT;
2644 * If we are in TBLOCK_BEGIN it means something screwed up right
2645 * after reading "BEGIN TRANSACTION". We assume that the user
2646 * will interpret the error as meaning the BEGIN failed to get him
2647 * into a transaction block, so we should abort and return to idle
2652 CleanupTransaction();
2653 s->blockState = TBLOCK_DEFAULT;
2657 * We are somewhere in a transaction block and we've gotten a
2658 * failure, so we abort the transaction and set up the persistent
2659 * ABORT state. We will stay in ABORT until we get a ROLLBACK.
2661 case TBLOCK_INPROGRESS:
2663 s->blockState = TBLOCK_ABORT;
2664 /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2668 * Here, we failed while trying to COMMIT. Clean up the
2669 * transaction and return to idle state (we do not want to stay in
2674 CleanupTransaction();
2675 s->blockState = TBLOCK_DEFAULT;
2679 * Here, we are already in an aborted transaction state and are
2680 * waiting for a ROLLBACK, but for some reason we failed again! So
2681 * we just remain in the abort state.
2684 case TBLOCK_SUBABORT:
2688 * We are in a failed transaction and we got the ROLLBACK command.
2689 * We have already aborted, we just need to cleanup and go to idle
2692 case TBLOCK_ABORT_END:
2693 CleanupTransaction();
2694 s->blockState = TBLOCK_DEFAULT;
2698 * We are in a live transaction and we got a ROLLBACK command.
2699 * Abort, cleanup, go to idle state.
2701 case TBLOCK_ABORT_PENDING:
2703 CleanupTransaction();
2704 s->blockState = TBLOCK_DEFAULT;
2708 * Here, we failed while trying to PREPARE. Clean up the
2709 * transaction and return to idle state (we do not want to stay in
2712 case TBLOCK_PREPARE:
2714 CleanupTransaction();
2715 s->blockState = TBLOCK_DEFAULT;
2719 * We got an error inside a subtransaction. Abort just the
2720 * subtransaction, and go to the persistent SUBABORT state until
2723 case TBLOCK_SUBINPROGRESS:
2724 AbortSubTransaction();
2725 s->blockState = TBLOCK_SUBABORT;
2729 * If we failed while trying to create a subtransaction, clean up
2730 * the broken subtransaction and abort the parent. The same
2731 * applies if we get a failure while ending a subtransaction.
2733 case TBLOCK_SUBBEGIN:
2735 case TBLOCK_SUBABORT_PENDING:
2736 case TBLOCK_SUBRESTART:
2737 AbortSubTransaction();
2738 CleanupSubTransaction();
2739 AbortCurrentTransaction();
2743 * Same as above, except the Abort() was already done.
2745 case TBLOCK_SUBABORT_END:
2746 case TBLOCK_SUBABORT_RESTART:
2747 CleanupSubTransaction();
2748 AbortCurrentTransaction();
2754 * PreventTransactionChain
2756 * This routine is to be called by statements that must not run inside
2757 * a transaction block, typically because they have non-rollback-able
2758 * side effects or do internal commits.
2760 * If we have already started a transaction block, issue an error; also issue
2761 * an error if we appear to be running inside a user-defined function (which
2762 * could issue more commands and possibly cause a failure after the statement
2763 * completes). Subtransactions are verboten too.
2765 * isTopLevel: passed down from ProcessUtility to determine whether we are
2766 * inside a function or multi-query querystring. (We will always fail if
2767 * this is false, but it's convenient to centralize the check here instead of
2768 * making callers do it.)
2769 * stmtType: statement type name, for error messages.
2772 PreventTransactionChain(bool isTopLevel, const char *stmtType)
2775 * xact block already started?
2777 if (IsTransactionBlock())
2779 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2780 /* translator: %s represents an SQL statement name */
2781 errmsg("%s cannot run inside a transaction block",
2787 if (IsSubTransaction())
2789 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2790 /* translator: %s represents an SQL statement name */
2791 errmsg("%s cannot run inside a subtransaction",
2795 * inside a function call?
2799 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2800 /* translator: %s represents an SQL statement name */
2801 errmsg("%s cannot be executed from a function or multi-command string",
2804 /* If we got past IsTransactionBlock test, should be in default state */
2805 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2806 CurrentTransactionState->blockState != TBLOCK_STARTED)
2807 elog(FATAL, "cannot prevent transaction chain");
2812 * RequireTransactionChain
2814 * This routine is to be called by statements that must run inside
2815 * a transaction block, because they have no effects that persist past
2816 * transaction end (and so calling them outside a transaction block
2817 * is presumably an error). DECLARE CURSOR is an example.
2819 * If we appear to be running inside a user-defined function, we do not
2820 * issue an error, since the function could issue more commands that make
2821 * use of the current statement's results. Likewise subtransactions.
2822 * Thus this is an inverse for PreventTransactionChain.
2824 * isTopLevel: passed down from ProcessUtility to determine whether we are
2825 * inside a function.
2826 * stmtType: statement type name, for error messages.
2829 RequireTransactionChain(bool isTopLevel, const char *stmtType)
2832 * xact block already started?
2834 if (IsTransactionBlock())
2840 if (IsSubTransaction())
2844 * inside a function call?
2850 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
2851 /* translator: %s represents an SQL statement name */
2852 errmsg("%s can only be used in transaction blocks",
2857 * IsInTransactionChain
2859 * This routine is for statements that need to behave differently inside
2860 * a transaction block than when running as single commands. ANALYZE is
2861 * currently the only example.
2863 * isTopLevel: passed down from ProcessUtility to determine whether we are
2864 * inside a function.
2867 IsInTransactionChain(bool isTopLevel)
2870 * Return true on same conditions that would make PreventTransactionChain
2873 if (IsTransactionBlock())
2876 if (IsSubTransaction())
2882 if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2883 CurrentTransactionState->blockState != TBLOCK_STARTED)
2891 * Register or deregister callback functions for start- and end-of-xact
2894 * These functions are intended for use by dynamically loaded modules.
2895 * For built-in modules we generally just hardwire the appropriate calls
2896 * (mainly because it's easier to control the order that way, where needed).
2898 * At transaction end, the callback occurs post-commit or post-abort, so the
2899 * callback functions can only do noncritical cleanup.
2902 RegisterXactCallback(XactCallback callback, void *arg)
2904 XactCallbackItem *item;
2906 item = (XactCallbackItem *)
2907 MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
2908 item->callback = callback;
2910 item->next = Xact_callbacks;
2911 Xact_callbacks = item;
2915 UnregisterXactCallback(XactCallback callback, void *arg)
2917 XactCallbackItem *item;
2918 XactCallbackItem *prev;
2921 for (item = Xact_callbacks; item; prev = item, item = item->next)
2923 if (item->callback == callback && item->arg == arg)
2926 prev->next = item->next;
2928 Xact_callbacks = item->next;
2936 CallXactCallbacks(XactEvent event)
2938 XactCallbackItem *item;
2940 for (item = Xact_callbacks; item; item = item->next)
2941 (*item->callback) (event, item->arg);
2946 * Register or deregister callback functions for start- and end-of-subxact
2949 * Pretty much same as above, but for subtransaction events.
2951 * At subtransaction end, the callback occurs post-subcommit or post-subabort,
2952 * so the callback functions can only do noncritical cleanup. At
2953 * subtransaction start, the callback is called when the subtransaction has
2954 * finished initializing.
2957 RegisterSubXactCallback(SubXactCallback callback, void *arg)
2959 SubXactCallbackItem *item;
2961 item = (SubXactCallbackItem *)
2962 MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
2963 item->callback = callback;
2965 item->next = SubXact_callbacks;
2966 SubXact_callbacks = item;
2970 UnregisterSubXactCallback(SubXactCallback callback, void *arg)
2972 SubXactCallbackItem *item;
2973 SubXactCallbackItem *prev;
2976 for (item = SubXact_callbacks; item; prev = item, item = item->next)
2978 if (item->callback == callback && item->arg == arg)
2981 prev->next = item->next;
2983 SubXact_callbacks = item->next;
2991 CallSubXactCallbacks(SubXactEvent event,
2992 SubTransactionId mySubid,
2993 SubTransactionId parentSubid)
2995 SubXactCallbackItem *item;
2997 for (item = SubXact_callbacks; item; item = item->next)
2998 (*item->callback) (event, mySubid, parentSubid, item->arg);
3002 /* ----------------------------------------------------------------
3003 * transaction block support
3004 * ----------------------------------------------------------------
3008 * BeginTransactionBlock
3009 * This executes a BEGIN command.
3012 BeginTransactionBlock(void)
3014 TransactionState s = CurrentTransactionState;
3016 switch (s->blockState)
3019 * We are not inside a transaction block, so allow one to begin.
3021 case TBLOCK_STARTED:
3022 s->blockState = TBLOCK_BEGIN;
3026 * Already a transaction block in progress.
3028 case TBLOCK_INPROGRESS:
3029 case TBLOCK_SUBINPROGRESS:
3031 case TBLOCK_SUBABORT:
3033 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3034 errmsg("there is already a transaction in progress")));
3037 /* These cases are invalid. */
3038 case TBLOCK_DEFAULT:
3040 case TBLOCK_SUBBEGIN:
3043 case TBLOCK_ABORT_END:
3044 case TBLOCK_SUBABORT_END:
3045 case TBLOCK_ABORT_PENDING:
3046 case TBLOCK_SUBABORT_PENDING:
3047 case TBLOCK_SUBRESTART:
3048 case TBLOCK_SUBABORT_RESTART:
3049 case TBLOCK_PREPARE:
3050 elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3051 BlockStateAsString(s->blockState));
3057 * PrepareTransactionBlock
3058 * This executes a PREPARE command.
3060 * Since PREPARE may actually do a ROLLBACK, the result indicates what
3061 * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3063 * Note that we don't actually do anything here except change blockState.
3064 * The real work will be done in the upcoming PrepareTransaction().
3065 * We do it this way because it's not convenient to change memory context,
3066 * resource owner, etc while executing inside a Portal.
3069 PrepareTransactionBlock(char *gid)
3074 /* Set up to commit the current transaction */
3075 result = EndTransactionBlock();
3077 /* If successful, change outer tblock state to PREPARE */
3080 s = CurrentTransactionState;
3082 while (s->parent != NULL)
3085 if (s->blockState == TBLOCK_END)
3087 /* Save GID where PrepareTransaction can find it again */
3088 prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
3090 s->blockState = TBLOCK_PREPARE;
3095 * ignore case where we are not in a transaction;
3096 * EndTransactionBlock already issued a warning.
3098 Assert(s->blockState == TBLOCK_STARTED);
3099 /* Don't send back a PREPARE result tag... */
3108 * EndTransactionBlock
3109 * This executes a COMMIT command.
3111 * Since COMMIT may actually do a ROLLBACK, the result indicates what
3112 * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3114 * Note that we don't actually do anything here except change blockState.
3115 * The real work will be done in the upcoming CommitTransactionCommand().
3116 * We do it this way because it's not convenient to change memory context,
3117 * resource owner, etc while executing inside a Portal.
3120 EndTransactionBlock(void)
3122 TransactionState s = CurrentTransactionState;
3123 bool result = false;
3125 switch (s->blockState)
3128 * We are in a transaction block, so tell CommitTransactionCommand
3131 case TBLOCK_INPROGRESS:
3132 s->blockState = TBLOCK_END;
3137 * We are in a failed transaction block. Tell
3138 * CommitTransactionCommand it's time to exit the block.
3141 s->blockState = TBLOCK_ABORT_END;
3145 * We are in a live subtransaction block. Set up to subcommit all
3146 * open subtransactions and then commit the main transaction.
3148 case TBLOCK_SUBINPROGRESS:
3149 while (s->parent != NULL)
3151 if (s->blockState == TBLOCK_SUBINPROGRESS)
3152 s->blockState = TBLOCK_SUBEND;
3154 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3155 BlockStateAsString(s->blockState));
3158 if (s->blockState == TBLOCK_INPROGRESS)
3159 s->blockState = TBLOCK_END;
3161 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3162 BlockStateAsString(s->blockState));
3167 * Here we are inside an aborted subtransaction. Treat the COMMIT
3168 * as ROLLBACK: set up to abort everything and exit the main
3171 case TBLOCK_SUBABORT:
3172 while (s->parent != NULL)
3174 if (s->blockState == TBLOCK_SUBINPROGRESS)
3175 s->blockState = TBLOCK_SUBABORT_PENDING;
3176 else if (s->blockState == TBLOCK_SUBABORT)
3177 s->blockState = TBLOCK_SUBABORT_END;
3179 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3180 BlockStateAsString(s->blockState));
3183 if (s->blockState == TBLOCK_INPROGRESS)
3184 s->blockState = TBLOCK_ABORT_PENDING;
3185 else if (s->blockState == TBLOCK_ABORT)
3186 s->blockState = TBLOCK_ABORT_END;
3188 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3189 BlockStateAsString(s->blockState));
3193 * The user issued COMMIT when not inside a transaction. Issue a
3194 * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3195 * CommitTransactionCommand() will then close the transaction and
3196 * put us back into the default state.
3198 case TBLOCK_STARTED:
3200 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3201 errmsg("there is no transaction in progress")));
3205 /* These cases are invalid. */
3206 case TBLOCK_DEFAULT:
3208 case TBLOCK_SUBBEGIN:
3211 case TBLOCK_ABORT_END:
3212 case TBLOCK_SUBABORT_END:
3213 case TBLOCK_ABORT_PENDING:
3214 case TBLOCK_SUBABORT_PENDING:
3215 case TBLOCK_SUBRESTART:
3216 case TBLOCK_SUBABORT_RESTART:
3217 case TBLOCK_PREPARE:
3218 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3219 BlockStateAsString(s->blockState));
3227 * UserAbortTransactionBlock
3228 * This executes a ROLLBACK command.
3230 * As above, we don't actually do anything here except change blockState.
3233 UserAbortTransactionBlock(void)
3235 TransactionState s = CurrentTransactionState;
3237 switch (s->blockState)
3240 * We are inside a transaction block and we got a ROLLBACK command
3241 * from the user, so tell CommitTransactionCommand to abort and
3242 * exit the transaction block.
3244 case TBLOCK_INPROGRESS:
3245 s->blockState = TBLOCK_ABORT_PENDING;
3249 * We are inside a failed transaction block and we got a ROLLBACK
3250 * command from the user. Abort processing is already done, so
3251 * CommitTransactionCommand just has to cleanup and go back to
3255 s->blockState = TBLOCK_ABORT_END;
3259 * We are inside a subtransaction. Mark everything up to top
3260 * level as exitable.
3262 case TBLOCK_SUBINPROGRESS:
3263 case TBLOCK_SUBABORT:
3264 while (s->parent != NULL)
3266 if (s->blockState == TBLOCK_SUBINPROGRESS)
3267 s->blockState = TBLOCK_SUBABORT_PENDING;
3268 else if (s->blockState == TBLOCK_SUBABORT)
3269 s->blockState = TBLOCK_SUBABORT_END;
3271 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3272 BlockStateAsString(s->blockState));
3275 if (s->blockState == TBLOCK_INPROGRESS)
3276 s->blockState = TBLOCK_ABORT_PENDING;
3277 else if (s->blockState == TBLOCK_ABORT)
3278 s->blockState = TBLOCK_ABORT_END;
3280 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3281 BlockStateAsString(s->blockState));
3285 * The user issued ABORT when not inside a transaction. Issue a
3286 * WARNING and go to abort state. The upcoming call to
3287 * CommitTransactionCommand() will then put us back into the
3290 case TBLOCK_STARTED:
3292 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3293 errmsg("there is no transaction in progress")));
3294 s->blockState = TBLOCK_ABORT_PENDING;
3297 /* These cases are invalid. */
3298 case TBLOCK_DEFAULT:
3300 case TBLOCK_SUBBEGIN:
3303 case TBLOCK_ABORT_END:
3304 case TBLOCK_SUBABORT_END:
3305 case TBLOCK_ABORT_PENDING:
3306 case TBLOCK_SUBABORT_PENDING:
3307 case TBLOCK_SUBRESTART:
3308 case TBLOCK_SUBABORT_RESTART:
3309 case TBLOCK_PREPARE:
3310 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3311 BlockStateAsString(s->blockState));
3318 * This executes a SAVEPOINT command.
3321 DefineSavepoint(char *name)
3323 TransactionState s = CurrentTransactionState;
3325 switch (s->blockState)
3327 case TBLOCK_INPROGRESS:
3328 case TBLOCK_SUBINPROGRESS:
3329 /* Normal subtransaction start */
3331 s = CurrentTransactionState; /* changed by push */
3334 * Savepoint names, like the TransactionState block itself, live
3335 * in TopTransactionContext.
3338 s->name = MemoryContextStrdup(TopTransactionContext, name);
3341 /* These cases are invalid. */
3342 case TBLOCK_DEFAULT:
3343 case TBLOCK_STARTED:
3345 case TBLOCK_SUBBEGIN:
3349 case TBLOCK_SUBABORT:
3350 case TBLOCK_ABORT_END:
3351 case TBLOCK_SUBABORT_END:
3352 case TBLOCK_ABORT_PENDING:
3353 case TBLOCK_SUBABORT_PENDING:
3354 case TBLOCK_SUBRESTART:
3355 case TBLOCK_SUBABORT_RESTART:
3356 case TBLOCK_PREPARE:
3357 elog(FATAL, "DefineSavepoint: unexpected state %s",
3358 BlockStateAsString(s->blockState));
3365 * This executes a RELEASE command.
3367 * As above, we don't actually do anything here except change blockState.
3370 ReleaseSavepoint(List *options)
3372 TransactionState s = CurrentTransactionState;
3373 TransactionState target,
3378 switch (s->blockState)
3381 * We can't rollback to a savepoint if there is no savepoint
3384 case TBLOCK_INPROGRESS:
3386 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3387 errmsg("no such savepoint")));
3391 * We are in a non-aborted subtransaction. This is the only valid
3394 case TBLOCK_SUBINPROGRESS:
3397 /* These cases are invalid. */
3398 case TBLOCK_DEFAULT:
3399 case TBLOCK_STARTED:
3401 case TBLOCK_SUBBEGIN:
3405 case TBLOCK_SUBABORT:
3406 case TBLOCK_ABORT_END:
3407 case TBLOCK_SUBABORT_END:
3408 case TBLOCK_ABORT_PENDING:
3409 case TBLOCK_SUBABORT_PENDING:
3410 case TBLOCK_SUBRESTART:
3411 case TBLOCK_SUBABORT_RESTART:
3412 case TBLOCK_PREPARE:
3413 elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3414 BlockStateAsString(s->blockState));
3418 foreach(cell, options)
3420 DefElem *elem = lfirst(cell);
3422 if (strcmp(elem->defname, "savepoint_name") == 0)
3423 name = strVal(elem->arg);
3426 Assert(PointerIsValid(name));
3428 for (target = s; PointerIsValid(target); target = target->parent)
3430 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3434 if (!PointerIsValid(target))
3436 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3437 errmsg("no such savepoint")));
3439 /* disallow crossing savepoint level boundaries */
3440 if (target->savepointLevel != s->savepointLevel)
3442 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3443 errmsg("no such savepoint")));
3446 * Mark "commit pending" all subtransactions up to the target
3447 * subtransaction. The actual commits will happen when control gets to
3448 * CommitTransactionCommand.
3450 xact = CurrentTransactionState;
3453 Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
3454 xact->blockState = TBLOCK_SUBEND;
3457 xact = xact->parent;
3458 Assert(PointerIsValid(xact));
3463 * RollbackToSavepoint
3464 * This executes a ROLLBACK TO <savepoint> command.
3466 * As above, we don't actually do anything here except change blockState.
3469 RollbackToSavepoint(List *options)
3471 TransactionState s = CurrentTransactionState;
3472 TransactionState target,
3477 switch (s->blockState)
3480 * We can't rollback to a savepoint if there is no savepoint
3483 case TBLOCK_INPROGRESS:
3486 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3487 errmsg("no such savepoint")));
3491 * There is at least one savepoint, so proceed.
3493 case TBLOCK_SUBINPROGRESS:
3494 case TBLOCK_SUBABORT:
3497 /* These cases are invalid. */
3498 case TBLOCK_DEFAULT:
3499 case TBLOCK_STARTED:
3501 case TBLOCK_SUBBEGIN:
3504 case TBLOCK_ABORT_END:
3505 case TBLOCK_SUBABORT_END:
3506 case TBLOCK_ABORT_PENDING:
3507 case TBLOCK_SUBABORT_PENDING:
3508 case TBLOCK_SUBRESTART:
3509 case TBLOCK_SUBABORT_RESTART:
3510 case TBLOCK_PREPARE:
3511 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3512 BlockStateAsString(s->blockState));
3516 foreach(cell, options)
3518 DefElem *elem = lfirst(cell);
3520 if (strcmp(elem->defname, "savepoint_name") == 0)
3521 name = strVal(elem->arg);
3524 Assert(PointerIsValid(name));
3526 for (target = s; PointerIsValid(target); target = target->parent)
3528 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3532 if (!PointerIsValid(target))
3534 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3535 errmsg("no such savepoint")));
3537 /* disallow crossing savepoint level boundaries */
3538 if (target->savepointLevel != s->savepointLevel)
3540 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3541 errmsg("no such savepoint")));
3544 * Mark "abort pending" all subtransactions up to the target
3545 * subtransaction. The actual aborts will happen when control gets to
3546 * CommitTransactionCommand.
3548 xact = CurrentTransactionState;
3553 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3554 xact->blockState = TBLOCK_SUBABORT_PENDING;
3555 else if (xact->blockState == TBLOCK_SUBABORT)
3556 xact->blockState = TBLOCK_SUBABORT_END;
3558 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3559 BlockStateAsString(xact->blockState));
3560 xact = xact->parent;
3561 Assert(PointerIsValid(xact));
3564 /* And mark the target as "restart pending" */
3565 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3566 xact->blockState = TBLOCK_SUBRESTART;
3567 else if (xact->blockState == TBLOCK_SUBABORT)
3568 xact->blockState = TBLOCK_SUBABORT_RESTART;
3570 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3571 BlockStateAsString(xact->blockState));
3575 * BeginInternalSubTransaction
3576 * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
3577 * TBLOCK_END, and TBLOCK_PREPARE states, and therefore it can safely be
3578 * used in functions that might be called when not inside a BEGIN block
3579 * or when running deferred triggers at COMMIT/PREPARE time. Also, it
3580 * automatically does CommitTransactionCommand/StartTransactionCommand
3581 * instead of expecting the caller to do it.
3584 BeginInternalSubTransaction(char *name)
3586 TransactionState s = CurrentTransactionState;
3588 switch (s->blockState)
3590 case TBLOCK_STARTED:
3591 case TBLOCK_INPROGRESS:
3593 case TBLOCK_PREPARE:
3594 case TBLOCK_SUBINPROGRESS:
3595 /* Normal subtransaction start */
3597 s = CurrentTransactionState; /* changed by push */
3600 * Savepoint names, like the TransactionState block itself, live
3601 * in TopTransactionContext.
3604 s->name = MemoryContextStrdup(TopTransactionContext, name);
3607 /* These cases are invalid. */
3608 case TBLOCK_DEFAULT:
3610 case TBLOCK_SUBBEGIN:
3613 case TBLOCK_SUBABORT:
3614 case TBLOCK_ABORT_END:
3615 case TBLOCK_SUBABORT_END:
3616 case TBLOCK_ABORT_PENDING:
3617 case TBLOCK_SUBABORT_PENDING:
3618 case TBLOCK_SUBRESTART:
3619 case TBLOCK_SUBABORT_RESTART:
3620 elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
3621 BlockStateAsString(s->blockState));
3625 CommitTransactionCommand();
3626 StartTransactionCommand();
3630 * ReleaseCurrentSubTransaction
3632 * RELEASE (ie, commit) the innermost subtransaction, regardless of its
3633 * savepoint name (if any).
3634 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3637 ReleaseCurrentSubTransaction(void)
3639 TransactionState s = CurrentTransactionState;
3641 if (s->blockState != TBLOCK_SUBINPROGRESS)
3642 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
3643 BlockStateAsString(s->blockState));
3644 Assert(s->state == TRANS_INPROGRESS);
3645 MemoryContextSwitchTo(CurTransactionContext);
3646 CommitSubTransaction();
3647 s = CurrentTransactionState; /* changed by pop */
3648 Assert(s->state == TRANS_INPROGRESS);
3652 * RollbackAndReleaseCurrentSubTransaction
3654 * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
3655 * of its savepoint name (if any).
3656 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3659 RollbackAndReleaseCurrentSubTransaction(void)
3661 TransactionState s = CurrentTransactionState;
3663 switch (s->blockState)
3665 /* Must be in a subtransaction */
3666 case TBLOCK_SUBINPROGRESS:
3667 case TBLOCK_SUBABORT:
3670 /* These cases are invalid. */
3671 case TBLOCK_DEFAULT:
3672 case TBLOCK_STARTED:
3674 case TBLOCK_SUBBEGIN:
3675 case TBLOCK_INPROGRESS:
3679 case TBLOCK_ABORT_END:
3680 case TBLOCK_SUBABORT_END:
3681 case TBLOCK_ABORT_PENDING:
3682 case TBLOCK_SUBABORT_PENDING:
3683 case TBLOCK_SUBRESTART:
3684 case TBLOCK_SUBABORT_RESTART:
3685 case TBLOCK_PREPARE:
3686 elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
3687 BlockStateAsString(s->blockState));
3692 * Abort the current subtransaction, if needed.
3694 if (s->blockState == TBLOCK_SUBINPROGRESS)
3695 AbortSubTransaction();
3697 /* And clean it up, too */
3698 CleanupSubTransaction();
3700 s = CurrentTransactionState; /* changed by pop */
3701 AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
3702 s->blockState == TBLOCK_INPROGRESS ||
3703 s->blockState == TBLOCK_STARTED);
3707 * AbortOutOfAnyTransaction
3709 * This routine is provided for error recovery purposes. It aborts any
3710 * active transaction or transaction block, leaving the system in a known
3714 AbortOutOfAnyTransaction(void)
3716 TransactionState s = CurrentTransactionState;
3719 * Get out of any transaction or nested transaction
3723 switch (s->blockState)
3725 case TBLOCK_DEFAULT:
3726 /* Not in a transaction, do nothing */
3728 case TBLOCK_STARTED:
3730 case TBLOCK_INPROGRESS:
3732 case TBLOCK_ABORT_PENDING:
3733 case TBLOCK_PREPARE:
3734 /* In a transaction, so clean up */
3736 CleanupTransaction();
3737 s->blockState = TBLOCK_DEFAULT;
3740 case TBLOCK_ABORT_END:
3741 /* AbortTransaction already done, still need Cleanup */
3742 CleanupTransaction();
3743 s->blockState = TBLOCK_DEFAULT;
3747 * In a subtransaction, so clean it up and abort parent too
3749 case TBLOCK_SUBBEGIN:
3750 case TBLOCK_SUBINPROGRESS:
3752 case TBLOCK_SUBABORT_PENDING:
3753 case TBLOCK_SUBRESTART:
3754 AbortSubTransaction();
3755 CleanupSubTransaction();
3756 s = CurrentTransactionState; /* changed by pop */
3759 case TBLOCK_SUBABORT:
3760 case TBLOCK_SUBABORT_END:
3761 case TBLOCK_SUBABORT_RESTART:
3762 /* As above, but AbortSubTransaction already done */
3763 CleanupSubTransaction();
3764 s = CurrentTransactionState; /* changed by pop */
3767 } while (s->blockState != TBLOCK_DEFAULT);
3769 /* Should be out of all subxacts now */
3770 Assert(s->parent == NULL);
3774 * IsTransactionBlock --- are we within a transaction block?
3777 IsTransactionBlock(void)
3779 TransactionState s = CurrentTransactionState;
3781 if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3788 * IsTransactionOrTransactionBlock --- are we within either a transaction
3789 * or a transaction block? (The backend is only really "idle" when this
3792 * This should match up with IsTransactionBlock and IsTransactionState.
3795 IsTransactionOrTransactionBlock(void)
3797 TransactionState s = CurrentTransactionState;
3799 if (s->blockState == TBLOCK_DEFAULT)
3806 * TransactionBlockStatusCode - return status code to send in ReadyForQuery
3809 TransactionBlockStatusCode(void)
3811 TransactionState s = CurrentTransactionState;
3813 switch (s->blockState)
3815 case TBLOCK_DEFAULT:
3816 case TBLOCK_STARTED:
3817 return 'I'; /* idle --- not in transaction */
3819 case TBLOCK_SUBBEGIN:
3820 case TBLOCK_INPROGRESS:
3821 case TBLOCK_SUBINPROGRESS:
3824 case TBLOCK_PREPARE:
3825 return 'T'; /* in transaction */
3827 case TBLOCK_SUBABORT:
3828 case TBLOCK_ABORT_END:
3829 case TBLOCK_SUBABORT_END:
3830 case TBLOCK_ABORT_PENDING:
3831 case TBLOCK_SUBABORT_PENDING:
3832 case TBLOCK_SUBRESTART:
3833 case TBLOCK_SUBABORT_RESTART:
3834 return 'E'; /* in failed transaction */
3837 /* should never get here */
3838 elog(FATAL, "invalid transaction block state: %s",
3839 BlockStateAsString(s->blockState));
3840 return 0; /* keep compiler quiet */
3847 IsSubTransaction(void)
3849 TransactionState s = CurrentTransactionState;
3851 if (s->nestingLevel >= 2)
3858 * StartSubTransaction
3860 * If you're wondering why this is separate from PushTransaction: it's because
3861 * we can't conveniently do this stuff right inside DefineSavepoint. The
3862 * SAVEPOINT utility command will be executed inside a Portal, and if we
3863 * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
3864 * the Portal will undo those settings. So we make DefineSavepoint just
3865 * push a dummy transaction block, and when control returns to the main
3866 * idle loop, CommitTransactionCommand will be called, and we'll come here
3867 * to finish starting the subtransaction.
3870 StartSubTransaction(void)
3872 TransactionState s = CurrentTransactionState;
3874 if (s->state != TRANS_DEFAULT)
3875 elog(WARNING, "StartSubTransaction while in %s state",
3876 TransStateAsString(s->state));
3878 s->state = TRANS_START;
3881 * Initialize subsystems for new subtransaction
3883 * must initialize resource-management stuff first
3885 AtSubStart_Memory();
3886 AtSubStart_ResourceOwner();
3888 AtSubStart_Notify();
3889 AfterTriggerBeginSubXact();
3891 s->state = TRANS_INPROGRESS;
3894 * Call start-of-subxact callbacks
3896 CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
3897 s->parent->subTransactionId);
3899 ShowTransactionState("StartSubTransaction");
3903 * CommitSubTransaction
3905 * The caller has to make sure to always reassign CurrentTransactionState
3906 * if it has a local pointer to it after calling this function.
3909 CommitSubTransaction(void)
3911 TransactionState s = CurrentTransactionState;
3913 ShowTransactionState("CommitSubTransaction");
3915 if (s->state != TRANS_INPROGRESS)
3916 elog(WARNING, "CommitSubTransaction while in %s state",
3917 TransStateAsString(s->state));
3919 /* Pre-commit processing goes here -- nothing to do at the moment */
3921 s->state = TRANS_COMMIT;
3923 /* Must CCI to ensure commands of subtransaction are seen as done */
3924 CommandCounterIncrement();
3927 * Prior to 8.4 we marked subcommit in clog at this point. We now only
3928 * perform that step, if required, as part of the atomic update of the
3929 * whole transaction tree at top level commit or abort.
3932 /* Post-commit cleanup */
3933 if (TransactionIdIsValid(s->transactionId))
3934 AtSubCommit_childXids();
3935 AfterTriggerEndSubXact(true);
3936 AtSubCommit_Portals(s->subTransactionId,
3937 s->parent->subTransactionId,
3938 s->parent->curTransactionOwner);
3939 AtEOSubXact_LargeObject(true, s->subTransactionId,
3940 s->parent->subTransactionId);
3941 AtSubCommit_Notify();
3943 CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
3944 s->parent->subTransactionId);
3946 ResourceOwnerRelease(s->curTransactionOwner,
3947 RESOURCE_RELEASE_BEFORE_LOCKS,
3949 AtEOSubXact_RelationCache(true, s->subTransactionId,
3950 s->parent->subTransactionId);
3951 AtEOSubXact_Inval(true);
3955 * The only lock we actually release here is the subtransaction XID lock.
3956 * The rest just get transferred to the parent resource owner.
3958 CurrentResourceOwner = s->curTransactionOwner;
3959 if (TransactionIdIsValid(s->transactionId))
3960 XactLockTableDelete(s->transactionId);
3962 ResourceOwnerRelease(s->curTransactionOwner,
3963 RESOURCE_RELEASE_LOCKS,
3965 ResourceOwnerRelease(s->curTransactionOwner,
3966 RESOURCE_RELEASE_AFTER_LOCKS,
3969 AtEOXact_GUC(true, s->gucNestLevel);
3970 AtEOSubXact_SPI(true, s->subTransactionId);
3971 AtEOSubXact_on_commit_actions(true, s->subTransactionId,
3972 s->parent->subTransactionId);
3973 AtEOSubXact_Namespace(true, s->subTransactionId,
3974 s->parent->subTransactionId);
3975 AtEOSubXact_Files(true, s->subTransactionId,
3976 s->parent->subTransactionId);
3977 AtEOSubXact_HashTables(true, s->nestingLevel);
3978 AtEOSubXact_PgStat(true, s->nestingLevel);
3979 AtSubCommit_Snapshot(s->nestingLevel);
3982 * We need to restore the upper transaction's read-only state, in case the
3983 * upper is read-write while the child is read-only; GUC will incorrectly
3984 * think it should leave the child state in place.
3986 XactReadOnly = s->prevXactReadOnly;
3988 CurrentResourceOwner = s->parent->curTransactionOwner;
3989 CurTransactionResourceOwner = s->parent->curTransactionOwner;
3990 ResourceOwnerDelete(s->curTransactionOwner);
3991 s->curTransactionOwner = NULL;
3993 AtSubCommit_Memory();
3995 s->state = TRANS_DEFAULT;
4001 * AbortSubTransaction
4004 AbortSubTransaction(void)
4006 TransactionState s = CurrentTransactionState;
4008 /* Prevent cancel/die interrupt while cleaning up */
4011 /* Make sure we have a valid memory context and resource owner */
4012 AtSubAbort_Memory();
4013 AtSubAbort_ResourceOwner();
4016 * Release any LW locks we might be holding as quickly as possible.
4017 * (Regular locks, however, must be held till we finish aborting.)
4018 * Releasing LW locks is critical since we might try to grab them again
4019 * while cleaning up!
4021 * FIXME This may be incorrect --- Are there some locks we should keep?
4022 * Buffer locks, for example? I don't think so but I'm not sure.
4032 * check the current transaction state
4034 ShowTransactionState("AbortSubTransaction");
4036 if (s->state != TRANS_INPROGRESS)
4037 elog(WARNING, "AbortSubTransaction while in %s state",
4038 TransStateAsString(s->state));
4040 s->state = TRANS_ABORT;
4043 * Reset user ID which might have been changed transiently. (See notes in
4044 * AbortTransaction.)
4046 SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
4049 * We can skip all this stuff if the subxact failed before creating a
4052 if (s->curTransactionOwner)
4054 AfterTriggerEndSubXact(false);
4055 AtSubAbort_Portals(s->subTransactionId,
4056 s->parent->subTransactionId,
4057 s->parent->curTransactionOwner);
4058 AtEOSubXact_LargeObject(false, s->subTransactionId,
4059 s->parent->subTransactionId);
4060 AtSubAbort_Notify();
4062 /* Advertise the fact that we aborted in pg_clog. */
4063 (void) RecordTransactionAbort(true);
4065 /* Post-abort cleanup */
4066 if (TransactionIdIsValid(s->transactionId))
4067 AtSubAbort_childXids();
4069 CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
4070 s->parent->subTransactionId);
4072 ResourceOwnerRelease(s->curTransactionOwner,
4073 RESOURCE_RELEASE_BEFORE_LOCKS,
4075 AtEOSubXact_RelationCache(false, s->subTransactionId,
4076 s->parent->subTransactionId);
4077 AtEOSubXact_Inval(false);
4079 ResourceOwnerRelease(s->curTransactionOwner,
4080 RESOURCE_RELEASE_LOCKS,
4082 ResourceOwnerRelease(s->curTransactionOwner,
4083 RESOURCE_RELEASE_AFTER_LOCKS,
4086 AtEOXact_GUC(false, s->gucNestLevel);
4087 AtEOSubXact_SPI(false, s->subTransactionId);
4088 AtEOSubXact_on_commit_actions(false, s->subTransactionId,
4089 s->parent->subTransactionId);
4090 AtEOSubXact_Namespace(false, s->subTransactionId,
4091 s->parent->subTransactionId);
4092 AtEOSubXact_Files(false, s->subTransactionId,
4093 s->parent->subTransactionId);
4094 AtEOSubXact_HashTables(false, s->nestingLevel);
4095 AtEOSubXact_PgStat(false, s->nestingLevel);
4096 AtSubAbort_Snapshot(s->nestingLevel);
4100 * Restore the upper transaction's read-only state, too. This should be
4101 * redundant with GUC's cleanup but we may as well do it for consistency
4102 * with the commit case.
4104 XactReadOnly = s->prevXactReadOnly;
4106 RESUME_INTERRUPTS();
4110 * CleanupSubTransaction
4112 * The caller has to make sure to always reassign CurrentTransactionState
4113 * if it has a local pointer to it after calling this function.
4116 CleanupSubTransaction(void)
4118 TransactionState s = CurrentTransactionState;
4120 ShowTransactionState("CleanupSubTransaction");
4122 if (s->state != TRANS_ABORT)
4123 elog(WARNING, "CleanupSubTransaction while in %s state",
4124 TransStateAsString(s->state));
4126 AtSubCleanup_Portals(s->subTransactionId);
4128 CurrentResourceOwner = s->parent->curTransactionOwner;
4129 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4130 if (s->curTransactionOwner)
4131 ResourceOwnerDelete(s->curTransactionOwner);
4132 s->curTransactionOwner = NULL;
4134 AtSubCleanup_Memory();
4136 s->state = TRANS_DEFAULT;
4143 * Create transaction state stack entry for a subtransaction
4145 * The caller has to make sure to always reassign CurrentTransactionState
4146 * if it has a local pointer to it after calling this function.
4149 PushTransaction(void)
4151 TransactionState p = CurrentTransactionState;
4155 * We keep subtransaction state nodes in TopTransactionContext.
4157 s = (TransactionState)
4158 MemoryContextAllocZero(TopTransactionContext,
4159 sizeof(TransactionStateData));
4162 * Assign a subtransaction ID, watching out for counter wraparound.
4164 currentSubTransactionId += 1;
4165 if (currentSubTransactionId == InvalidSubTransactionId)
4167 currentSubTransactionId -= 1;
4170 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4171 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4175 * We can now stack a minimally valid subtransaction without fear of
4178 s->transactionId = InvalidTransactionId; /* until assigned */
4179 s->subTransactionId = currentSubTransactionId;
4181 s->nestingLevel = p->nestingLevel + 1;
4182 s->gucNestLevel = NewGUCNestLevel();
4183 s->savepointLevel = p->savepointLevel;
4184 s->state = TRANS_DEFAULT;
4185 s->blockState = TBLOCK_SUBBEGIN;
4186 GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
4187 s->prevXactReadOnly = XactReadOnly;
4189 CurrentTransactionState = s;
4192 * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4193 * with the subtransaction from here on out; in particular they should not
4194 * assume that it necessarily has a transaction context, resource owner,
4201 * Pop back to parent transaction state
4203 * The caller has to make sure to always reassign CurrentTransactionState
4204 * if it has a local pointer to it after calling this function.
4207 PopTransaction(void)
4209 TransactionState s = CurrentTransactionState;
4211 if (s->state != TRANS_DEFAULT)
4212 elog(WARNING, "PopTransaction while in %s state",
4213 TransStateAsString(s->state));
4215 if (s->parent == NULL)
4216 elog(FATAL, "PopTransaction with no parent");
4218 CurrentTransactionState = s->parent;
4220 /* Let's just make sure CurTransactionContext is good */
4221 CurTransactionContext = s->parent->curTransactionContext;
4222 MemoryContextSwitchTo(CurTransactionContext);
4224 /* Ditto for ResourceOwner links */
4225 CurTransactionResourceOwner = s->parent->curTransactionOwner;
4226 CurrentResourceOwner = s->parent->curTransactionOwner;
4228 /* Free the old child structure */
4235 * ShowTransactionState
4239 ShowTransactionState(const char *str)
4241 /* skip work if message will definitely not be printed */
4242 if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4244 elog(DEBUG3, "%s", str);
4245 ShowTransactionStateRec(CurrentTransactionState);
4250 * ShowTransactionStateRec
4251 * Recursive subroutine for ShowTransactionState
4254 ShowTransactionStateRec(TransactionState s)
4258 initStringInfo(&buf);
4260 if (s->nChildXids > 0)
4264 appendStringInfo(&buf, "%u", s->childXids[0]);
4265 for (i = 1; i < s->nChildXids; i++)
4266 appendStringInfo(&buf, " %u", s->childXids[i]);
4270 ShowTransactionStateRec(s->parent);
4272 /* use ereport to suppress computation if msg will not be printed */
4274 (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u%s, nestlvl: %d, children: %s",
4275 PointerIsValid(s->name) ? s->name : "unnamed",
4276 BlockStateAsString(s->blockState),
4277 TransStateAsString(s->state),
4278 (unsigned int) s->transactionId,
4279 (unsigned int) s->subTransactionId,
4280 (unsigned int) currentCommandId,
4281 currentCommandIdUsed ? " (used)" : "",
4282 s->nestingLevel, buf.data)));
4288 * BlockStateAsString
4292 BlockStateAsString(TBlockState blockState)
4296 case TBLOCK_DEFAULT:
4298 case TBLOCK_STARTED:
4302 case TBLOCK_INPROGRESS:
4303 return "INPROGRESS";
4308 case TBLOCK_ABORT_END:
4310 case TBLOCK_ABORT_PENDING:
4311 return "ABORT PEND";
4312 case TBLOCK_PREPARE:
4314 case TBLOCK_SUBBEGIN:
4316 case TBLOCK_SUBINPROGRESS:
4317 return "SUB INPROGRS";
4320 case TBLOCK_SUBABORT:
4322 case TBLOCK_SUBABORT_END:
4323 return "SUB ABORT END";
4324 case TBLOCK_SUBABORT_PENDING:
4325 return "SUB ABRT PEND";
4326 case TBLOCK_SUBRESTART:
4327 return "SUB RESTART";
4328 case TBLOCK_SUBABORT_RESTART:
4329 return "SUB AB RESTRT";
4331 return "UNRECOGNIZED";
4335 * TransStateAsString
4339 TransStateAsString(TransState state)
4347 case TRANS_INPROGRESS:
4356 return "UNRECOGNIZED";
4360 * xactGetCommittedChildren
4362 * Gets the list of committed children of the current transaction. The return
4363 * value is the number of child transactions. *ptr is set to point to an
4364 * array of TransactionIds. The array is allocated in TopTransactionContext;
4365 * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
4366 * If there are no subxacts, *ptr is set to NULL.
4369 xactGetCommittedChildren(TransactionId **ptr)
4371 TransactionState s = CurrentTransactionState;
4373 if (s->nChildXids == 0)
4376 *ptr = s->childXids;
4378 return s->nChildXids;
4382 * XLOG support routines
4386 * Before 9.0 this was a fairly short function, but now it performs many
4387 * actions for which the order of execution is critical.
4390 xact_redo_commit(xl_xact_commit *xlrec, TransactionId xid, XLogRecPtr lsn)
4392 TransactionId *sub_xids;
4393 SharedInvalidationMessage *inval_msgs;
4394 TransactionId max_xid;
4397 /* subxid array follows relfilenodes */
4398 sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4399 /* invalidation messages array follows subxids */
4400 inval_msgs = (SharedInvalidationMessage *) &(sub_xids[xlrec->nsubxacts]);
4402 max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4405 * Make sure nextXid is beyond any XID mentioned in the record.
4407 * We don't expect anyone else to modify nextXid, hence we don't need to
4408 * hold a lock while checking this. We still acquire the lock to modify
4411 if (TransactionIdFollowsOrEquals(max_xid,
4412 ShmemVariableCache->nextXid))
4414 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4415 ShmemVariableCache->nextXid = max_xid;
4416 TransactionIdAdvance(ShmemVariableCache->nextXid);
4417 LWLockRelease(XidGenLock);
4420 if (standbyState == STANDBY_DISABLED)
4423 * Mark the transaction committed in pg_clog.
4425 TransactionIdCommitTree(xid, xlrec->nsubxacts, sub_xids);
4430 * If a transaction completion record arrives that has as-yet
4431 * unobserved subtransactions then this will not have been fully
4432 * handled by the call to RecordKnownAssignedTransactionIds() in the
4433 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4434 * cover that case. This is confusing and it is easy to think this
4435 * call is irrelevant, which has happened three times in development
4436 * already. Leave it in.
4438 RecordKnownAssignedTransactionIds(max_xid);
4441 * Mark the transaction committed in pg_clog. We use async commit
4442 * protocol during recovery to provide information on database
4443 * consistency for when users try to set hint bits. It is important
4444 * that we do not set hint bits until the minRecoveryPoint is past
4445 * this commit record. This ensures that if we crash we don't see hint
4446 * bits set on changes made by transactions that haven't yet
4447 * recovered. It's unlikely but it's good to be safe.
4449 TransactionIdAsyncCommitTree(xid, xlrec->nsubxacts, sub_xids, lsn);
4452 * We must mark clog before we update the ProcArray.
4454 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids, max_xid);
4457 * Send any cache invalidations attached to the commit. We must
4458 * maintain the same order of invalidation then release locks as
4459 * occurs in CommitTransaction().
4461 ProcessCommittedInvalidationMessages(inval_msgs, xlrec->nmsgs,
4462 XactCompletionRelcacheInitFileInval(xlrec),
4463 xlrec->dbId, xlrec->tsId);
4466 * Release locks, if any. We do this for both two phase and normal one
4467 * phase transactions. In effect we are ignoring the prepare phase and
4468 * just going straight to lock release.
4470 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4473 /* Make sure files supposed to be dropped are dropped */
4474 for (i = 0; i < xlrec->nrels; i++)
4476 SMgrRelation srel = smgropen(xlrec->xnodes[i]);
4479 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4481 if (smgrexists(srel, fork))
4483 XLogDropRelation(xlrec->xnodes[i], fork);
4484 smgrdounlink(srel, fork, false, true);
4491 * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
4492 * in normal operation. For example, in DROP DATABASE, we delete all the
4493 * files belonging to the database, and then commit the transaction. If we
4494 * crash after all the files have been deleted but before the commit, you
4495 * have an entry in pg_database without any files. To minimize the window
4496 * for that, we use ForceSyncCommit() to rush the commit record to disk as
4497 * quick as possible. We have the same window during recovery, and forcing
4498 * an XLogFlush() (which updates minRecoveryPoint during recovery) helps
4499 * to reduce that problem window, for any user that requested
4500 * ForceSyncCommit().
4502 if (XactCompletionForceSyncCommit(xlrec))
4507 * Be careful with the order of execution, as with xact_redo_commit().
4508 * The two functions are similar but differ in key places.
4510 * Note also that an abort can be for a subtransaction and its children,
4511 * not just for a top level abort. That means we have to consider
4512 * topxid != xid, whereas in commit we would find topxid == xid always
4513 * because subtransaction commit is never WAL logged.
4516 xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
4518 TransactionId *sub_xids;
4519 TransactionId max_xid;
4522 sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4523 max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4525 /* Make sure nextXid is beyond any XID mentioned in the record */
4528 * We don't expect anyone else to modify nextXid, hence we don't need to
4529 * hold a lock while checking this. We still acquire the lock to modify
4532 if (TransactionIdFollowsOrEquals(max_xid,
4533 ShmemVariableCache->nextXid))
4535 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4536 ShmemVariableCache->nextXid = max_xid;
4537 TransactionIdAdvance(ShmemVariableCache->nextXid);
4538 LWLockRelease(XidGenLock);
4541 if (standbyState == STANDBY_DISABLED)
4543 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4544 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4549 * If a transaction completion record arrives that has as-yet
4550 * unobserved subtransactions then this will not have been fully
4551 * handled by the call to RecordKnownAssignedTransactionIds() in the
4552 * main recovery loop in xlog.c. So we need to do bookkeeping again to
4553 * cover that case. This is confusing and it is easy to think this
4554 * call is irrelevant, which has happened three times in development
4555 * already. Leave it in.
4557 RecordKnownAssignedTransactionIds(max_xid);
4559 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4560 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4563 * We must update the ProcArray after we have marked clog.
4565 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids, max_xid);
4568 * There are no flat files that need updating, nor invalidation
4569 * messages to send or undo.
4573 * Release locks, if any. There are no invalidations to send.
4575 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4578 /* Make sure files supposed to be dropped are dropped */
4579 for (i = 0; i < xlrec->nrels; i++)
4581 SMgrRelation srel = smgropen(xlrec->xnodes[i]);
4584 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4586 if (smgrexists(srel, fork))
4588 XLogDropRelation(xlrec->xnodes[i], fork);
4589 smgrdounlink(srel, fork, false, true);
4597 xact_redo(XLogRecPtr lsn, XLogRecord *record)
4599 uint8 info = record->xl_info & ~XLR_INFO_MASK;
4601 /* Backup blocks are not used in xact records */
4602 Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
4604 if (info == XLOG_XACT_COMMIT)
4606 xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
4608 xact_redo_commit(xlrec, record->xl_xid, lsn);
4610 else if (info == XLOG_XACT_ABORT)
4612 xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4614 xact_redo_abort(xlrec, record->xl_xid);
4616 else if (info == XLOG_XACT_PREPARE)
4618 /* the record contents are exactly the 2PC file */
4619 RecreateTwoPhaseFile(record->xl_xid,
4620 XLogRecGetData(record), record->xl_len);
4622 else if (info == XLOG_XACT_COMMIT_PREPARED)
4624 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4626 xact_redo_commit(&xlrec->crec, xlrec->xid, lsn);
4627 RemoveTwoPhaseFile(xlrec->xid, false);
4629 else if (info == XLOG_XACT_ABORT_PREPARED)
4631 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4633 xact_redo_abort(&xlrec->arec, xlrec->xid);
4634 RemoveTwoPhaseFile(xlrec->xid, false);
4636 else if (info == XLOG_XACT_ASSIGNMENT)
4638 xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
4640 if (standbyState >= STANDBY_INITIALIZED)
4641 ProcArrayApplyXidAssignment(xlrec->xtop,
4642 xlrec->nsubxacts, xlrec->xsub);
4645 elog(PANIC, "xact_redo: unknown op code %u", info);
4649 xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4652 TransactionId *xacts;
4654 xacts = (TransactionId *) &xlrec->xnodes[xlrec->nrels];
4656 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4658 if (xlrec->nrels > 0)
4660 appendStringInfo(buf, "; rels:");
4661 for (i = 0; i < xlrec->nrels; i++)
4663 char *path = relpath(xlrec->xnodes[i], MAIN_FORKNUM);
4665 appendStringInfo(buf, " %s", path);
4669 if (xlrec->nsubxacts > 0)
4671 appendStringInfo(buf, "; subxacts:");
4672 for (i = 0; i < xlrec->nsubxacts; i++)
4673 appendStringInfo(buf, " %u", xacts[i]);
4675 if (xlrec->nmsgs > 0)
4677 SharedInvalidationMessage *msgs;
4679 msgs = (SharedInvalidationMessage *) &xacts[xlrec->nsubxacts];
4681 if (XactCompletionRelcacheInitFileInval(xlrec))
4682 appendStringInfo(buf, "; relcache init file inval dbid %u tsid %u",
4683 xlrec->dbId, xlrec->tsId);
4685 appendStringInfo(buf, "; inval msgs:");
4686 for (i = 0; i < xlrec->nmsgs; i++)
4688 SharedInvalidationMessage *msg = &msgs[i];
4691 appendStringInfo(buf, " catcache %d", msg->id);
4692 else if (msg->id == SHAREDINVALCATALOG_ID)
4693 appendStringInfo(buf, " catalog %u", msg->cat.catId);
4694 else if (msg->id == SHAREDINVALRELCACHE_ID)
4695 appendStringInfo(buf, " relcache %u", msg->rc.relId);
4696 /* remaining cases not expected, but print something anyway */
4697 else if (msg->id == SHAREDINVALSMGR_ID)
4698 appendStringInfo(buf, " smgr");
4699 else if (msg->id == SHAREDINVALRELMAP_ID)
4700 appendStringInfo(buf, " relmap");
4702 appendStringInfo(buf, " unknown id %d", msg->id);
4708 xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4712 appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4713 if (xlrec->nrels > 0)
4715 appendStringInfo(buf, "; rels:");
4716 for (i = 0; i < xlrec->nrels; i++)
4718 char *path = relpath(xlrec->xnodes[i], MAIN_FORKNUM);
4720 appendStringInfo(buf, " %s", path);
4724 if (xlrec->nsubxacts > 0)
4726 TransactionId *xacts = (TransactionId *)
4727 &xlrec->xnodes[xlrec->nrels];
4729 appendStringInfo(buf, "; subxacts:");
4730 for (i = 0; i < xlrec->nsubxacts; i++)
4731 appendStringInfo(buf, " %u", xacts[i]);
4736 xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
4740 appendStringInfo(buf, "subxacts:");
4742 for (i = 0; i < xlrec->nsubxacts; i++)
4743 appendStringInfo(buf, " %u", xlrec->xsub[i]);
4747 xact_desc(StringInfo buf, uint8 xl_info, char *rec)
4749 uint8 info = xl_info & ~XLR_INFO_MASK;
4751 if (info == XLOG_XACT_COMMIT)
4753 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
4755 appendStringInfo(buf, "commit: ");
4756 xact_desc_commit(buf, xlrec);
4758 else if (info == XLOG_XACT_ABORT)
4760 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
4762 appendStringInfo(buf, "abort: ");
4763 xact_desc_abort(buf, xlrec);
4765 else if (info == XLOG_XACT_PREPARE)
4767 appendStringInfo(buf, "prepare");
4769 else if (info == XLOG_XACT_COMMIT_PREPARED)
4771 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
4773 appendStringInfo(buf, "commit prepared %u: ", xlrec->xid);
4774 xact_desc_commit(buf, &xlrec->crec);
4776 else if (info == XLOG_XACT_ABORT_PREPARED)
4778 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4780 appendStringInfo(buf, "abort prepared %u: ", xlrec->xid);
4781 xact_desc_abort(buf, &xlrec->arec);
4783 else if (info == XLOG_XACT_ASSIGNMENT)
4785 xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
4788 * Note that we ignore the WAL record's xid, since we're more
4789 * interested in the top-level xid that issued the record and which
4790 * xids are being reported here.
4792 appendStringInfo(buf, "xid assignment xtop %u: ", xlrec->xtop);
4793 xact_desc_assignment(buf, xlrec);
4796 appendStringInfo(buf, "UNKNOWN");