1 /*-------------------------------------------------------------------------
4 * top level transaction system support routines
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.118 2002/03/15 19:20:29 tgl Exp $
14 * Transaction aborts can now occur two ways:
16 * 1) system dies from some internal cause (Assert, etc..)
19 * These two cases used to be treated identically, but now
20 * we need to distinguish them. Why? consider the following
25 * 1) user types BEGIN 1) user types BEGIN
26 * 2) user does something 2) user does something
27 * 3) user does not like what 3) system aborts for some reason
28 * she sees and types ABORT
30 * In case 1, we want to abort the transaction and return to the
31 * default state. In case 2, there may be more commands coming
32 * our way which are part of the same transaction block and we have
33 * to ignore these commands until we see an END transaction.
34 * (or an ABORT! --djm)
36 * Internal aborts are now handled by AbortTransactionBlock(), just as
37 * they always have been, and user aborts are now handled by
38 * UserAbortTransactionBlock(). Both of them rely on AbortTransaction()
39 * to do all the real work. The only difference is what state we
40 * enter after AbortTransaction() does its work:
42 * * AbortTransactionBlock() leaves us in TBLOCK_ABORT and
43 * * UserAbortTransactionBlock() leaves us in TBLOCK_ENDABORT
45 * Low-level transaction abort handling is divided into two phases:
46 * * AbortTransaction() executes as soon as we realize the transaction
47 * has failed. It should release all shared resources (locks etc)
48 * so that we do not delay other backends unnecessarily.
49 * * CleanupTransaction() executes when we finally see a user COMMIT
50 * or ROLLBACK command; it cleans things up and gets us out of
51 * the transaction internally. In particular, we mustn't destroy
52 * TransactionCommandContext until this point.
55 * This file is an attempt at a redesign of the upper layer
56 * of the V1 transaction system which was too poorly thought
57 * out to describe. This new system hopes to be both simpler
58 * in design, simpler to extend and needs to contain added
59 * functionality to solve problems beyond the scope of the V1
60 * system. (In particuler, communication of transaction
61 * information between parallel backends has to be supported)
63 * The essential aspects of the transaction system are:
65 * o transaction id generation
66 * o transaction log updating
68 * o cache invalidation
71 * Hence, the functional division of the transaction code is
72 * based on what of the above things need to be done during
73 * a start/commit/abort transaction. For instance, the
74 * routine AtCommit_Memory() takes care of all the memory
75 * cleanup stuff done at commit time.
77 * The code is layered as follows:
84 * are provided to do the lower level work like recording
85 * the transaction status in the log and doing memory cleanup.
86 * above these routines are another set of functions:
88 * StartTransactionCommand
89 * CommitTransactionCommand
90 * AbortCurrentTransaction
92 * These are the routines used in the postgres main processing
93 * loop. They are sensitive to the current transaction block state
94 * and make calls to the lower level routines appropriately.
96 * Support for transaction blocks is provided via the functions:
98 * StartTransactionBlock
99 * CommitTransactionBlock
100 * AbortTransactionBlock
102 * These are invoked only in responce to a user "BEGIN", "END",
103 * or "ABORT" command. The tricky part about these functions
104 * is that they are called within the postgres main loop, in between
105 * the StartTransactionCommand() and CommitTransactionCommand().
107 * For example, consider the following sequence of user commands:
110 * 2) retrieve (foo.all)
111 * 3) append foo (bar = baz)
114 * in the main processing loop, this results in the following
115 * transaction sequence:
117 * / StartTransactionCommand();
118 * 1) / ProcessUtility(); << begin
119 * \ StartTransactionBlock();
120 * \ CommitTransactionCommand();
122 * / StartTransactionCommand();
123 * 2) < ProcessQuery(); << retrieve (foo.all)
124 * \ CommitTransactionCommand();
126 * / StartTransactionCommand();
127 * 3) < ProcessQuery(); << append foo (bar = baz)
128 * \ CommitTransactionCommand();
130 * / StartTransactionCommand();
131 * 4) / ProcessUtility(); << end
132 * \ CommitTransactionBlock();
133 * \ CommitTransactionCommand();
135 * The point of this example is to demonstrate the need for
136 * StartTransactionCommand() and CommitTransactionCommand() to
137 * be state smart -- they should do nothing in between the calls
138 * to StartTransactionBlock() and EndTransactionBlock() and
139 * outside these calls they need to do normal start/commit
142 * Furthermore, suppose the "retrieve (foo.all)" caused an abort
143 * condition. We would then want to abort the transaction and
144 * ignore all subsequent commands up to the "end".
147 *-------------------------------------------------------------------------
151 * Large object clean up added in CommitTransaction() to prevent buffer leaks.
153 * [PA] is Pascal André <andre@via.ecp.fr>
155 #include "postgres.h"
158 #include <sys/time.h>
160 #include "access/gistscan.h"
161 #include "access/hash.h"
162 #include "access/nbtree.h"
163 #include "access/rtree.h"
164 #include "access/xact.h"
165 #include "catalog/heap.h"
166 #include "catalog/index.h"
167 #include "commands/async.h"
168 #include "commands/sequence.h"
169 #include "commands/trigger.h"
170 #include "executor/spi.h"
171 #include "libpq/be-fsstubs.h"
172 #include "miscadmin.h"
173 #include "storage/proc.h"
174 #include "storage/sinval.h"
175 #include "storage/smgr.h"
176 #include "utils/inval.h"
177 #include "utils/memutils.h"
178 #include "utils/portal.h"
179 #include "utils/catcache.h"
180 #include "utils/relcache.h"
181 #include "utils/temprel.h"
185 extern bool SharedBufferChanged;
187 static void AbortTransaction(void);
188 static void AtAbort_Cache(void);
189 static void AtAbort_Locks(void);
190 static void AtAbort_Memory(void);
191 static void AtCleanup_Memory(void);
192 static void AtCommit_Cache(void);
193 static void AtCommit_LocalCache(void);
194 static void AtCommit_Locks(void);
195 static void AtCommit_Memory(void);
196 static void AtStart_Cache(void);
197 static void AtStart_Locks(void);
198 static void AtStart_Memory(void);
199 static void CleanupTransaction(void);
200 static void CommitTransaction(void);
201 static void RecordTransactionAbort(void);
202 static void StartTransaction(void);
205 * global variables holding the current transaction state.
208 static TransactionStateData CurrentTransactionStateData = {
209 0, /* transaction id */
210 FirstCommandId, /* command id */
211 0, /* scan command id */
212 0x0, /* start time */
213 TRANS_DEFAULT, /* transaction state */
214 TBLOCK_DEFAULT /* transaction block state */
217 TransactionState CurrentTransactionState = &CurrentTransactionStateData;
220 * User-tweakable parameters
222 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
225 int CommitDelay = 0; /* precommit delay in microseconds */
226 int CommitSiblings = 5; /* number of concurrent xacts needed to
229 static void (*_RollbackFunc) (void *) = NULL;
230 static void *_RollbackData = NULL;
233 * catalog creation transaction bootstrapping flag.
234 * This should be eliminated and added to the transaction
235 * state stuff. -cim 3/19/90
238 bool AMI_OVERRIDE = false;
240 /* ----------------------------------------------------------------
241 * transaction state accessors
242 * ----------------------------------------------------------------
245 /* --------------------------------
246 * TranactionFlushEnabled()
247 * SetTransactionFlushEnabled()
249 * These are used to test and set the "TransactionFlushState"
250 * varable. If this variable is true (the default), then
251 * the system will flush all dirty buffers to disk at the end
252 * of each transaction. If false then we are assuming the
253 * buffer pool resides in stable main memory, in which case we
254 * only do writes as necessary.
255 * --------------------------------
257 static int TransactionFlushState = 1;
260 TransactionFlushEnabled(void)
262 return TransactionFlushState;
267 SetTransactionFlushEnabled(bool state)
269 TransactionFlushState = (state == true);
273 /* --------------------------------
276 * This returns true if we are currently running a query
277 * within an executing transaction.
278 * --------------------------------
281 IsTransactionState(void)
283 TransactionState s = CurrentTransactionState;
291 case TRANS_INPROGRESS:
300 * Shouldn't get here, but lint is not happy with this...
306 /* --------------------------------
307 * IsAbortedTransactionBlockState
309 * This returns true if we are currently running a query
310 * within an aborted transaction block.
311 * --------------------------------
314 IsAbortedTransactionBlockState(void)
316 TransactionState s = CurrentTransactionState;
318 if (s->blockState == TBLOCK_ABORT)
325 /* --------------------------------
326 * GetCurrentTransactionId
327 * --------------------------------
330 GetCurrentTransactionId(void)
332 TransactionState s = CurrentTransactionState;
334 return s->transactionIdData;
338 /* --------------------------------
339 * GetCurrentCommandId
340 * --------------------------------
343 GetCurrentCommandId(void)
345 TransactionState s = CurrentTransactionState;
351 GetScanCommandId(void)
353 TransactionState s = CurrentTransactionState;
355 return s->scanCommandId;
359 /* --------------------------------
360 * GetCurrentTransactionStartTime
361 * --------------------------------
364 GetCurrentTransactionStartTime(void)
366 TransactionState s = CurrentTransactionState;
372 /* --------------------------------
373 * GetCurrentTransactionStartTimeUsec
374 * --------------------------------
377 GetCurrentTransactionStartTimeUsec(int *msec)
379 TransactionState s = CurrentTransactionState;
381 *msec = s->startTimeUsec;
387 /* --------------------------------
388 * TransactionIdIsCurrentTransactionId
389 * --------------------------------
392 TransactionIdIsCurrentTransactionId(TransactionId xid)
394 TransactionState s = CurrentTransactionState;
399 return TransactionIdEquals(xid, s->transactionIdData);
403 /* --------------------------------
404 * CommandIdIsCurrentCommandId
405 * --------------------------------
408 CommandIdIsCurrentCommandId(CommandId cid)
410 TransactionState s = CurrentTransactionState;
415 return (cid == s->commandId) ? true : false;
419 CommandIdGEScanCommandId(CommandId cid)
421 TransactionState s = CurrentTransactionState;
426 return (cid >= s->scanCommandId) ? true : false;
430 /* --------------------------------
431 * CommandCounterIncrement
432 * --------------------------------
435 CommandCounterIncrement(void)
437 CurrentTransactionStateData.commandId += 1;
438 if (CurrentTransactionStateData.commandId == FirstCommandId)
439 elog(ERROR, "You may only have 2^32-1 commands per transaction");
441 CurrentTransactionStateData.scanCommandId = CurrentTransactionStateData.commandId;
444 * make cache changes visible to me. AtCommit_LocalCache() instead of
445 * AtCommit_Cache() is called here.
447 AtCommit_LocalCache();
452 SetScanCommandId(CommandId savedId)
454 CurrentTransactionStateData.scanCommandId = savedId;
457 /* ----------------------------------------------------------------
458 * StartTransaction stuff
459 * ----------------------------------------------------------------
462 /* --------------------------------
464 * --------------------------------
469 AcceptInvalidationMessages();
472 /* --------------------------------
474 * --------------------------------
480 * at present, it is unknown to me what belongs here -cim 3/18/90
482 * There isn't anything to do at the start of a xact for locks. -mer
487 /* --------------------------------
489 * --------------------------------
495 * We shouldn't have any transaction contexts already.
497 Assert(TopTransactionContext == NULL);
498 Assert(TransactionCommandContext == NULL);
501 * Create a toplevel context for the transaction.
503 TopTransactionContext =
504 AllocSetContextCreate(TopMemoryContext,
505 "TopTransactionContext",
506 ALLOCSET_DEFAULT_MINSIZE,
507 ALLOCSET_DEFAULT_INITSIZE,
508 ALLOCSET_DEFAULT_MAXSIZE);
511 * Create a statement-level context and make it active.
513 TransactionCommandContext =
514 AllocSetContextCreate(TopTransactionContext,
515 "TransactionCommandContext",
516 ALLOCSET_DEFAULT_MINSIZE,
517 ALLOCSET_DEFAULT_INITSIZE,
518 ALLOCSET_DEFAULT_MAXSIZE);
519 MemoryContextSwitchTo(TransactionCommandContext);
523 /* ----------------------------------------------------------------
524 * CommitTransaction stuff
525 * ----------------------------------------------------------------
528 /* --------------------------------
529 * RecordTransactionCommit
531 * Note: the two calls to BufferManagerFlush() exist to ensure
532 * that data pages are written before log pages. These
533 * explicit calls should be replaced by a more efficient
534 * ordered page write scheme in the buffer manager
536 * --------------------------------
539 RecordTransactionCommit(void)
544 leak = BufferPoolCheckLeak();
546 xid = GetCurrentTransactionId();
549 * We only need to log the commit in xlog and clog if the transaction made
550 * any transaction-controlled XLOG entries. (Otherwise, its XID appears
551 * nowhere in permanent storage, so no one will ever care if it
552 * committed.) However, we must flush XLOG to disk if we made any XLOG
553 * entries, whether in or out of transaction control. For example, if we
554 * reported a nextval() result to the client, this ensures that any XLOG
555 * record generated by nextval will hit the disk before we report the
556 * transaction committed.
558 if (MyXactMadeXLogEntry)
564 START_CRIT_SECTION();
566 if (MyLastRecPtr.xrecoff != 0)
568 /* Need to emit a commit record */
570 xl_xact_commit xlrec;
572 xlrec.xtime = time(NULL);
573 rdata.buffer = InvalidBuffer;
574 rdata.data = (char *) (&xlrec);
575 rdata.len = SizeOfXactCommit;
579 * XXX SHOULD SAVE ARRAY OF RELFILENODE-s TO DROP
581 recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, &rdata);
585 /* Just flush through last record written by me */
586 recptr = ProcLastRecEnd;
590 * Sleep before flush! So we can flush more than one commit
591 * records per single fsync. (The idea is some other backend may
592 * do the XLogFlush while we're sleeping. This needs work still,
593 * because on most Unixen, the minimum select() delay is 10msec or
594 * more, which is way too long.)
596 * We do not sleep if enableFsync is not turned on, nor if there are
597 * fewer than CommitSiblings other backends with active
600 if (CommitDelay > 0 && enableFsync &&
601 CountActiveBackends() >= CommitSiblings)
603 struct timeval delay;
606 delay.tv_usec = CommitDelay;
607 (void) select(0, NULL, NULL, NULL, &delay);
612 /* Mark the transaction committed in clog, if needed */
613 if (MyLastRecPtr.xrecoff != 0)
614 TransactionIdCommit(xid);
619 /* Break the chain of back-links in the XLOG records I output */
620 MyLastRecPtr.xrecoff = 0;
621 MyXactMadeXLogEntry = false;
623 /* Show myself as out of the transaction in PROC array */
624 MyProc->logRec.xrecoff = 0;
627 ResetBufferPool(true);
631 /* --------------------------------
633 * --------------------------------
639 * Make catalog changes visible to all backends.
641 AtEOXactInvalidationMessages(true);
644 /* --------------------------------
645 * AtCommit_LocalCache
646 * --------------------------------
649 AtCommit_LocalCache(void)
652 * Make catalog changes visible to me for the next command.
654 CommandEndInvalidationMessages(true);
657 /* --------------------------------
659 * --------------------------------
665 * XXX What if ProcReleaseLocks fails? (race condition?)
667 * Then you're up a creek! -mer 5/24/92
669 ProcReleaseLocks(true);
672 /* --------------------------------
674 * --------------------------------
677 AtCommit_Memory(void)
680 * Now that we're "out" of a transaction, have the system allocate
681 * things in the top memory context instead of per-transaction
684 MemoryContextSwitchTo(TopMemoryContext);
687 * Release all transaction-local memory.
689 Assert(TopTransactionContext != NULL);
690 MemoryContextDelete(TopTransactionContext);
691 TopTransactionContext = NULL;
692 TransactionCommandContext = NULL;
695 /* ----------------------------------------------------------------
696 * AbortTransaction stuff
697 * ----------------------------------------------------------------
700 /* --------------------------------
701 * RecordTransactionAbort
702 * --------------------------------
705 RecordTransactionAbort(void)
707 TransactionId xid = GetCurrentTransactionId();
710 * We only need to log the abort in xlog and clog if the transaction made
711 * any transaction-controlled XLOG entries. (Otherwise, its XID appears
712 * nowhere in permanent storage, so no one will ever care if it
713 * committed.) We do not flush XLOG to disk in any case, since the
714 * default assumption after a crash would be that we aborted, anyway.
716 * Extra check here is to catch case that we aborted partway through
717 * RecordTransactionCommit ...
719 if (MyLastRecPtr.xrecoff != 0 && !TransactionIdDidCommit(xid))
725 xlrec.xtime = time(NULL);
726 rdata.buffer = InvalidBuffer;
727 rdata.data = (char *) (&xlrec);
728 rdata.len = SizeOfXactAbort;
731 START_CRIT_SECTION();
734 * SHOULD SAVE ARRAY OF RELFILENODE-s TO DROP
736 recptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, &rdata);
738 /* Mark the transaction aborted in clog */
739 TransactionIdAbort(xid);
744 /* Break the chain of back-links in the XLOG records I output */
745 MyLastRecPtr.xrecoff = 0;
746 MyXactMadeXLogEntry = false;
748 /* Show myself as out of the transaction in PROC array */
749 MyProc->logRec.xrecoff = 0;
752 * Tell bufmgr and smgr to release resources.
754 ResetBufferPool(false); /* false -> is abort */
757 /* --------------------------------
759 * --------------------------------
764 RelationCacheAbort();
765 AtEOXactInvalidationMessages(false);
768 /* --------------------------------
770 * --------------------------------
776 * XXX What if ProcReleaseLocks() fails? (race condition?)
778 * Then you're up a creek without a paddle! -mer
780 ProcReleaseLocks(false);
784 /* --------------------------------
786 * --------------------------------
792 * Make sure we are in a valid context (not a child of
793 * TransactionCommandContext...). Note that it is possible for this
794 * code to be called when we aren't in a transaction at all; go
795 * directly to TopMemoryContext in that case.
797 if (TransactionCommandContext != NULL)
799 MemoryContextSwitchTo(TransactionCommandContext);
802 * We do not want to destroy transaction contexts yet, but it
803 * should be OK to delete any command-local memory.
805 MemoryContextResetAndDeleteChildren(TransactionCommandContext);
808 MemoryContextSwitchTo(TopMemoryContext);
812 /* ----------------------------------------------------------------
813 * CleanupTransaction stuff
814 * ----------------------------------------------------------------
817 /* --------------------------------
819 * --------------------------------
822 AtCleanup_Memory(void)
825 * Now that we're "out" of a transaction, have the system allocate
826 * things in the top memory context instead of per-transaction
829 MemoryContextSwitchTo(TopMemoryContext);
832 * Release all transaction-local memory.
834 if (TopTransactionContext != NULL)
835 MemoryContextDelete(TopTransactionContext);
836 TopTransactionContext = NULL;
837 TransactionCommandContext = NULL;
841 /* ----------------------------------------------------------------
843 * ----------------------------------------------------------------
846 /* --------------------------------
849 * --------------------------------
852 StartTransaction(void)
854 TransactionState s = CurrentTransactionState;
857 XactIsoLevel = DefaultXactIsoLevel;
860 * Check the current transaction state. If the transaction system is
861 * switched off, or if we're already in a transaction, do nothing.
862 * We're already in a transaction when the monitor sends a null
863 * command to the backend to flush the comm channel. This is a hacky
864 * fix to a communications problem, and we keep having to deal with it
865 * here. We should fix the comm channel code. mao 080891
867 if (s->state == TRANS_INPROGRESS)
871 * set the current transaction state information appropriately during
874 s->state = TRANS_START;
876 SetReindexProcessing(false);
879 * generate a new transaction id
881 s->transactionIdData = GetNewTransactionId();
883 XactLockTableInsert(s->transactionIdData);
886 * initialize current transaction state fields
888 s->commandId = FirstCommandId;
889 s->scanCommandId = FirstCommandId;
891 s->startTime = GetCurrentAbsoluteTime();
893 s->startTime = GetCurrentAbsoluteTimeUsec(&(s->startTimeUsec));
896 * initialize the various transaction subsystems
903 * Tell the trigger manager to we're starting a transaction
905 DeferredTriggerBeginXact();
908 * done with start processing, set current transaction state to "in
911 s->state = TRANS_INPROGRESS;
917 * Tell me if we are currently in progress
921 CurrentXactInProgress(void)
923 return CurrentTransactionState->state == TRANS_INPROGRESS;
927 /* --------------------------------
930 * --------------------------------
933 CommitTransaction(void)
935 TransactionState s = CurrentTransactionState;
938 * check the current transaction state
940 if (s->state != TRANS_INPROGRESS)
941 elog(WARNING, "CommitTransaction and not in in-progress state");
944 * Tell the trigger manager that this transaction is about to be
945 * committed. He'll invoke all trigger deferred until XACT before we
946 * really start on committing the transaction.
948 DeferredTriggerEndXact();
950 /* Prevent cancel/die interrupt while cleaning up */
954 * set the current transaction state information appropriately during
955 * the abort processing
957 s->state = TRANS_COMMIT;
960 * do commit processing
963 /* handle commit for large objects [ PA, 7/17/98 ] */
966 /* NOTIFY commit must also come before lower-level cleanup */
972 /* Here is where we really truly commit. */
973 RecordTransactionCommit();
976 * Let others know about no transaction in progress by me. Note that
977 * this must be done _before_ releasing locks we hold and _after_
978 * RecordTransactionCommit.
980 * LWLockAcquire(SInvalLock) is required: UPDATE with xid 0 is blocked by
981 * xid 1' UPDATE, xid 1 is doing commit while xid 2 gets snapshot - if
982 * xid 2' GetSnapshotData sees xid 1 as running then it must see xid 0
983 * as running as well or it will see two tuple versions - one deleted
984 * by xid 1 and one inserted by xid 0. See notes in GetSnapshotData.
986 if (MyProc != (PROC *) NULL)
988 /* Lock SInvalLock because that's what GetSnapshotData uses. */
989 LWLockAcquire(SInvalLock, LW_EXCLUSIVE);
990 MyProc->xid = InvalidTransactionId;
991 MyProc->xmin = InvalidTransactionId;
992 LWLockRelease(SInvalLock);
996 * This is all post-commit cleanup. Note that if an error is raised
997 * here, it's too late to abort the transaction. This should be just
998 * noncritical resource releasing.
1001 RelationPurgeLocalRelation(true);
1002 AtEOXact_temp_relations(true);
1003 smgrDoPendingDeletes(true);
1012 AtEOXact_CatCache(true);
1016 SharedBufferChanged = false; /* safest place to do it */
1018 /* Count transaction commit in statistics collector */
1019 pgstat_count_xact_commit();
1022 * done with commit processing, set current transaction state back to
1025 s->state = TRANS_DEFAULT;
1027 RESUME_INTERRUPTS();
1030 /* --------------------------------
1033 * --------------------------------
1036 AbortTransaction(void)
1038 TransactionState s = CurrentTransactionState;
1040 /* Prevent cancel/die interrupt while cleaning up */
1044 * Release any LW locks we might be holding as quickly as possible.
1045 * (Regular locks, however, must be held till we finish aborting.)
1046 * Releasing LW locks is critical since we might try to grab them
1047 * again while cleaning up!
1051 /* Clean up buffer I/O and buffer context locks, too */
1056 * Also clean up any open wait for lock, since the lock manager will
1057 * choke if we try to wait for another lock before doing this.
1062 * check the current transaction state
1064 if (s->state != TRANS_INPROGRESS)
1065 elog(WARNING, "AbortTransaction and not in in-progress state");
1068 * set the current transaction state information appropriately during
1069 * the abort processing
1071 s->state = TRANS_ABORT;
1074 * Reset user id which might have been changed transiently
1076 SetUserId(GetSessionUserId());
1079 * do abort processing
1081 DeferredTriggerAbortXact();
1082 lo_commit(false); /* 'false' means it's abort */
1087 /* Advertise the fact that we aborted in pg_clog. */
1088 RecordTransactionAbort();
1091 * Let others know about no transaction in progress by me. Note that
1092 * this must be done _before_ releasing locks we hold and _after_
1093 * RecordTransactionAbort.
1095 if (MyProc != (PROC *) NULL)
1097 /* Lock SInvalLock because that's what GetSnapshotData uses. */
1098 LWLockAcquire(SInvalLock, LW_EXCLUSIVE);
1099 MyProc->xid = InvalidTransactionId;
1100 MyProc->xmin = InvalidTransactionId;
1101 LWLockRelease(SInvalLock);
1104 RelationPurgeLocalRelation(false);
1105 AtEOXact_temp_relations(false);
1106 smgrDoPendingDeletes(false);
1114 AtEOXact_CatCache(false);
1119 SharedBufferChanged = false; /* safest place to do it */
1121 /* Count transaction abort in statistics collector */
1122 pgstat_count_xact_rollback();
1125 * State remains TRANS_ABORT until CleanupTransaction().
1127 RESUME_INTERRUPTS();
1130 /* --------------------------------
1131 * CleanupTransaction
1133 * --------------------------------
1136 CleanupTransaction(void)
1138 TransactionState s = CurrentTransactionState;
1141 * State should still be TRANS_ABORT from AbortTransaction().
1143 if (s->state != TRANS_ABORT)
1144 elog(FATAL, "CleanupTransaction and not in abort state");
1147 * do abort cleanup processing
1152 * done with abort processing, set current transaction state back to
1155 s->state = TRANS_DEFAULT;
1158 /* --------------------------------
1159 * StartTransactionCommand
1160 * --------------------------------
1163 StartTransactionCommand(void)
1165 TransactionState s = CurrentTransactionState;
1167 switch (s->blockState)
1170 * if we aren't in a transaction block, we just do our usual
1171 * start transaction.
1173 case TBLOCK_DEFAULT:
1178 * We should never experience this -- if we do it means the
1179 * BEGIN state was not changed in the previous
1180 * CommitTransactionCommand(). If we get it, we print a
1181 * warning and change to the in-progress state.
1184 elog(WARNING, "StartTransactionCommand: unexpected TBLOCK_BEGIN");
1185 s->blockState = TBLOCK_INPROGRESS;
1189 * This is the case when are somewhere in a transaction block
1190 * and about to start a new command. For now we do nothing
1191 * but someday we may do command-local resource
1194 case TBLOCK_INPROGRESS:
1198 * As with BEGIN, we should never experience this if we do it
1199 * means the END state was not changed in the previous
1200 * CommitTransactionCommand(). If we get it, we print a
1201 * warning, commit the transaction, start a new transaction
1202 * and change to the default state.
1205 elog(WARNING, "StartTransactionCommand: unexpected TBLOCK_END");
1206 s->blockState = TBLOCK_DEFAULT;
1207 CommitTransaction();
1212 * Here we are in the middle of a transaction block but one of
1213 * the commands caused an abort so we do nothing but remain in
1214 * the abort state. Eventually we will get to the "END
1215 * TRANSACTION" which will set things straight.
1221 * This means we somehow aborted and the last call to
1222 * CommitTransactionCommand() didn't clear the state so we
1223 * remain in the ENDABORT state and maybe next time we get to
1224 * CommitTransactionCommand() the state will get reset to
1227 case TBLOCK_ENDABORT:
1228 elog(WARNING, "StartTransactionCommand: unexpected TBLOCK_ENDABORT");
1233 * We must switch to TransactionCommandContext before returning. This
1234 * is already done if we called StartTransaction, otherwise not.
1236 Assert(TransactionCommandContext != NULL);
1237 MemoryContextSwitchTo(TransactionCommandContext);
1240 /* --------------------------------
1241 * CommitTransactionCommand
1242 * --------------------------------
1245 CommitTransactionCommand(void)
1247 TransactionState s = CurrentTransactionState;
1249 switch (s->blockState)
1252 * if we aren't in a transaction block, we just do our usual
1253 * transaction commit
1255 case TBLOCK_DEFAULT:
1256 CommitTransaction();
1260 * This is the case right after we get a "BEGIN TRANSACTION"
1261 * command, but the user hasn't done anything else yet, so we
1262 * change to the "transaction block in progress" state and
1266 s->blockState = TBLOCK_INPROGRESS;
1270 * This is the case when we have finished executing a command
1271 * someplace within a transaction block. We increment the
1272 * command counter and return. Someday we may free resources
1273 * local to the command.
1275 * That someday is today, at least for memory allocated in
1276 * TransactionCommandContext. - vadim 03/25/97
1278 case TBLOCK_INPROGRESS:
1279 CommandCounterIncrement();
1280 MemoryContextResetAndDeleteChildren(TransactionCommandContext);
1284 * This is the case when we just got the "END TRANSACTION"
1285 * statement, so we commit the transaction and go back to the
1289 CommitTransaction();
1290 s->blockState = TBLOCK_DEFAULT;
1294 * Here we are in the middle of a transaction block but one of
1295 * the commands caused an abort so we do nothing but remain in
1296 * the abort state. Eventually we will get to the "END
1297 * TRANSACTION" which will set things straight.
1303 * Here we were in an aborted transaction block which just
1304 * processed the "END TRANSACTION" command from the user, so
1305 * clean up and return to the default state.
1307 case TBLOCK_ENDABORT:
1308 CleanupTransaction();
1309 s->blockState = TBLOCK_DEFAULT;
1314 /* --------------------------------
1315 * AbortCurrentTransaction
1316 * --------------------------------
1319 AbortCurrentTransaction(void)
1321 TransactionState s = CurrentTransactionState;
1323 switch (s->blockState)
1326 * if we aren't in a transaction block, we just do the basic
1327 * abort & cleanup transaction.
1329 case TBLOCK_DEFAULT:
1331 CleanupTransaction();
1335 * If we are in the TBLOCK_BEGIN it means something screwed up
1336 * right after reading "BEGIN TRANSACTION" so we enter the
1337 * abort state. Eventually an "END TRANSACTION" will fix
1341 s->blockState = TBLOCK_ABORT;
1343 /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1347 * This is the case when are somewhere in a transaction block
1348 * which aborted so we abort the transaction and set the ABORT
1349 * state. Eventually an "END TRANSACTION" will fix things and
1350 * restore us to a normal state.
1352 case TBLOCK_INPROGRESS:
1353 s->blockState = TBLOCK_ABORT;
1355 /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1359 * Here, the system was fouled up just after the user wanted
1360 * to end the transaction block so we abort the transaction
1361 * and put us back into the default state.
1364 s->blockState = TBLOCK_DEFAULT;
1366 CleanupTransaction();
1370 * Here, we are already in an aborted transaction state and
1371 * are waiting for an "END TRANSACTION" to come along and lo
1372 * and behold, we abort again! So we just remain in the abort
1379 * Here we were in an aborted transaction block which just
1380 * processed the "END TRANSACTION" command but somehow aborted
1381 * again.. since we must have done the abort processing, we
1382 * clean up and return to the default state.
1384 case TBLOCK_ENDABORT:
1385 CleanupTransaction();
1386 s->blockState = TBLOCK_DEFAULT;
1391 /* ----------------------------------------------------------------
1392 * transaction block support
1393 * ----------------------------------------------------------------
1395 /* --------------------------------
1396 * BeginTransactionBlock
1397 * --------------------------------
1400 BeginTransactionBlock(void)
1402 TransactionState s = CurrentTransactionState;
1405 * check the current transaction state
1407 if (s->blockState != TBLOCK_DEFAULT)
1408 elog(WARNING, "BEGIN: already a transaction in progress");
1411 * set the current transaction block state information appropriately
1412 * during begin processing
1414 s->blockState = TBLOCK_BEGIN;
1417 * do begin processing
1421 * done with begin processing, set block state to inprogress
1423 s->blockState = TBLOCK_INPROGRESS;
1426 /* --------------------------------
1427 * EndTransactionBlock
1428 * --------------------------------
1431 EndTransactionBlock(void)
1433 TransactionState s = CurrentTransactionState;
1436 * check the current transaction state
1438 if (s->blockState == TBLOCK_INPROGRESS)
1441 * here we are in a transaction block which should commit when we
1442 * get to the upcoming CommitTransactionCommand() so we set the
1443 * state to "END". CommitTransactionCommand() will recognize this
1444 * and commit the transaction and return us to the default state
1446 s->blockState = TBLOCK_END;
1450 if (s->blockState == TBLOCK_ABORT)
1453 * here, we are in a transaction block which aborted and since the
1454 * AbortTransaction() was already done, we do whatever is needed
1455 * and change to the special "END ABORT" state. The upcoming
1456 * CommitTransactionCommand() will recognise this and then put us
1457 * back in the default state.
1459 s->blockState = TBLOCK_ENDABORT;
1464 * here, the user issued COMMIT when not inside a transaction. Issue a
1465 * WARNING and go to abort state. The upcoming call to
1466 * CommitTransactionCommand() will then put us back into the default
1469 elog(WARNING, "COMMIT: no transaction in progress");
1471 s->blockState = TBLOCK_ENDABORT;
1474 /* --------------------------------
1475 * AbortTransactionBlock
1476 * --------------------------------
1480 AbortTransactionBlock(void)
1482 TransactionState s = CurrentTransactionState;
1485 * check the current transaction state
1487 if (s->blockState == TBLOCK_INPROGRESS)
1490 * here we were inside a transaction block something screwed up
1491 * inside the system so we enter the abort state, do the abort
1492 * processing and then return. We remain in the abort state until
1493 * we see an END TRANSACTION command.
1495 s->blockState = TBLOCK_ABORT;
1501 * here, the user issued ABORT when not inside a transaction. Issue a
1502 * WARNING and go to abort state. The upcoming call to
1503 * CommitTransactionCommand() will then put us back into the default
1506 elog(WARNING, "ROLLBACK: no transaction in progress");
1508 s->blockState = TBLOCK_ENDABORT;
1512 /* --------------------------------
1513 * UserAbortTransactionBlock
1514 * --------------------------------
1517 UserAbortTransactionBlock(void)
1519 TransactionState s = CurrentTransactionState;
1522 * if the transaction has already been automatically aborted with an
1523 * error, and the user subsequently types 'abort', allow it. (the
1524 * behavior is the same as if they had typed 'end'.)
1526 if (s->blockState == TBLOCK_ABORT)
1528 s->blockState = TBLOCK_ENDABORT;
1532 if (s->blockState == TBLOCK_INPROGRESS)
1535 * here we were inside a transaction block and we got an abort
1536 * command from the user, so we move to the abort state, do the
1537 * abort processing and then change to the ENDABORT state so we
1538 * will end up in the default state after the upcoming
1539 * CommitTransactionCommand().
1541 s->blockState = TBLOCK_ABORT;
1543 s->blockState = TBLOCK_ENDABORT;
1548 * here, the user issued ABORT when not inside a transaction. Issue a
1549 * WARNING and go to abort state. The upcoming call to
1550 * CommitTransactionCommand() will then put us back into the default
1553 elog(WARNING, "ROLLBACK: no transaction in progress");
1555 s->blockState = TBLOCK_ENDABORT;
1558 /* --------------------------------
1559 * AbortOutOfAnyTransaction
1561 * This routine is provided for error recovery purposes. It aborts any
1562 * active transaction or transaction block, leaving the system in a known
1564 * --------------------------------
1567 AbortOutOfAnyTransaction(void)
1569 TransactionState s = CurrentTransactionState;
1572 * Get out of any low-level transaction
1577 case TRANS_INPROGRESS:
1579 /* In a transaction, so clean up */
1581 CleanupTransaction();
1584 /* AbortTransaction already done, still need Cleanup */
1585 CleanupTransaction();
1588 /* Not in a transaction, do nothing */
1593 * Now reset the high-level state
1595 s->blockState = TBLOCK_DEFAULT;
1599 IsTransactionBlock(void)
1601 TransactionState s = CurrentTransactionState;
1603 if (s->blockState == TBLOCK_INPROGRESS
1604 || s->blockState == TBLOCK_ABORT
1605 || s->blockState == TBLOCK_ENDABORT)
1612 xact_redo(XLogRecPtr lsn, XLogRecord *record)
1614 uint8 info = record->xl_info & ~XLR_INFO_MASK;
1616 if (info == XLOG_XACT_COMMIT)
1618 TransactionIdCommit(record->xl_xid);
1619 /* SHOULD REMOVE FILES OF ALL DROPPED RELATIONS */
1621 else if (info == XLOG_XACT_ABORT)
1623 TransactionIdAbort(record->xl_xid);
1624 /* SHOULD REMOVE FILES OF ALL FAILED-TO-BE-CREATED RELATIONS */
1627 elog(PANIC, "xact_redo: unknown op code %u", info);
1631 xact_undo(XLogRecPtr lsn, XLogRecord *record)
1633 uint8 info = record->xl_info & ~XLR_INFO_MASK;
1635 if (info == XLOG_XACT_COMMIT) /* shouldn't be called by XLOG */
1636 elog(PANIC, "xact_undo: can't undo committed xaction");
1637 else if (info != XLOG_XACT_ABORT)
1638 elog(PANIC, "xact_redo: unknown op code %u", info);
1642 xact_desc(char *buf, uint8 xl_info, char *rec)
1644 uint8 info = xl_info & ~XLR_INFO_MASK;
1646 if (info == XLOG_XACT_COMMIT)
1648 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
1649 struct tm *tm = localtime(&xlrec->xtime);
1651 sprintf(buf + strlen(buf), "commit: %04u-%02u-%02u %02u:%02u:%02u",
1652 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1653 tm->tm_hour, tm->tm_min, tm->tm_sec);
1655 else if (info == XLOG_XACT_ABORT)
1657 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
1658 struct tm *tm = localtime(&xlrec->xtime);
1660 sprintf(buf + strlen(buf), "abort: %04u-%02u-%02u %02u:%02u:%02u",
1661 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1662 tm->tm_hour, tm->tm_min, tm->tm_sec);
1665 strcat(buf, "UNKNOWN");
1669 XactPushRollback(void (*func) (void *), void *data)
1672 if (_RollbackFunc != NULL)
1673 elog(PANIC, "XactPushRollback: already installed");
1676 _RollbackFunc = func;
1677 _RollbackData = data;
1681 XactPopRollback(void)
1683 _RollbackFunc = NULL;