1 /*-------------------------------------------------------------------------
4 * top level transaction system support routines
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/transam/xact.c,v 1.68 2000/06/28 03:31:05 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"
157 #include "access/nbtree.h"
158 #include "catalog/heap.h"
159 #include "catalog/index.h"
160 #include "commands/async.h"
161 #include "commands/sequence.h"
162 #include "commands/trigger.h"
163 #include "executor/spi.h"
164 #include "libpq/be-fsstubs.h"
165 #include "storage/proc.h"
166 #include "storage/sinval.h"
167 #include "utils/inval.h"
168 #include "utils/memutils.h"
169 #include "utils/portal.h"
170 #include "utils/relcache.h"
171 #include "utils/temprel.h"
173 extern bool SharedBufferChanged;
175 static void AbortTransaction(void);
176 static void AtAbort_Cache(void);
177 static void AtAbort_Locks(void);
178 static void AtAbort_Memory(void);
179 static void AtCleanup_Memory(void);
180 static void AtCommit_Cache(void);
181 static void AtCommit_LocalCache(void);
182 static void AtCommit_Locks(void);
183 static void AtCommit_Memory(void);
184 static void AtStart_Cache(void);
185 static void AtStart_Locks(void);
186 static void AtStart_Memory(void);
187 static void CleanupTransaction(void);
188 static void CommitTransaction(void);
189 static void RecordTransactionAbort(void);
190 static void RecordTransactionCommit(void);
191 static void StartTransaction(void);
194 * global variables holding the current transaction state.
196 * Note: when we are running several slave processes, the
197 * current transaction state data is copied into shared memory
198 * and the CurrentTransactionState pointer changed to
199 * point to the shared copy. All this occurrs in slaves.c
202 TransactionStateData CurrentTransactionStateData = {
203 0, /* transaction id */
204 FirstCommandId, /* command id */
205 0, /* scan command id */
206 0x0, /* start time */
207 TRANS_DEFAULT, /* transaction state */
208 TBLOCK_DEFAULT /* transaction block state */
211 TransactionState CurrentTransactionState = &CurrentTransactionStateData;
213 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
217 * info returned when the system is disabled
219 * Apparently a lot of this code is inherited from other prototype systems.
220 * For DisabledStartTime, use a symbolic value to make the relationships clearer.
221 * The old value of 1073741823 corresponds to a date in y2004, which is coming closer
222 * every day. It appears that if we return a value guaranteed larger than
223 * any real time associated with a transaction then comparisons in other
224 * modules will still be correct. Let's use BIG_ABSTIME for this. tgl 2/14/97
226 * Note: I have no idea what the significance of the
227 * 1073741823 in DisabledStartTime.. I just carried
228 * this over when converting things from the old
229 * V1 transaction system. -cim 3/18/90
232 TransactionId DisabledTransactionId = (TransactionId) -1;
234 CommandId DisabledCommandId = (CommandId) -1;
236 AbsoluteTime DisabledStartTime = (AbsoluteTime) BIG_ABSTIME; /* 1073741823; */
242 bool CommandIdCounterOverflowFlag;
245 * catalog creation transaction bootstrapping flag.
246 * This should be eliminated and added to the transaction
247 * state stuff. -cim 3/19/90
250 bool AMI_OVERRIDE = false;
252 /* ----------------------------------------------------------------
253 * transaction state accessors
254 * ----------------------------------------------------------------
257 /* --------------------------------
258 * TranactionFlushEnabled()
259 * SetTransactionFlushEnabled()
261 * These are used to test and set the "TransactionFlushState"
262 * varable. If this variable is true (the default), then
263 * the system will flush all dirty buffers to disk at the end
264 * of each transaction. If false then we are assuming the
265 * buffer pool resides in stable main memory, in which case we
266 * only do writes as necessary.
267 * --------------------------------
269 static int TransactionFlushState = 1;
272 TransactionFlushEnabled(void)
274 return TransactionFlushState;
279 SetTransactionFlushEnabled(bool state)
281 TransactionFlushState = (state == true);
285 /* --------------------------------
288 * This returns true if we are currently running a query
289 * within an executing transaction.
290 * --------------------------------
293 IsTransactionState(void)
295 TransactionState s = CurrentTransactionState;
303 case TRANS_INPROGRESS:
314 * Shouldn't get here, but lint is not happy with this...
321 /* --------------------------------
322 * IsAbortedTransactionBlockState
324 * This returns true if we are currently running a query
325 * within an aborted transaction block.
326 * --------------------------------
329 IsAbortedTransactionBlockState()
331 TransactionState s = CurrentTransactionState;
333 if (s->blockState == TBLOCK_ABORT)
339 /* --------------------------------
340 * OverrideTransactionSystem
342 * This is used to temporarily disable the transaction
343 * processing system in order to do initialization of
344 * the transaction system data structures and relations
346 * --------------------------------
348 int SavedTransactionState;
351 OverrideTransactionSystem(bool flag)
353 TransactionState s = CurrentTransactionState;
357 if (s->state == TRANS_DISABLED)
360 SavedTransactionState = s->state;
361 s->state = TRANS_DISABLED;
365 if (s->state != TRANS_DISABLED)
368 s->state = SavedTransactionState;
372 /* --------------------------------
373 * GetCurrentTransactionId
375 * This returns the id of the current transaction, or
376 * the id of the "disabled" transaction.
377 * --------------------------------
380 GetCurrentTransactionId()
382 TransactionState s = CurrentTransactionState;
385 * if the transaction system is disabled, we return
386 * the special "disabled" transaction id.
389 if (s->state == TRANS_DISABLED)
390 return (TransactionId) DisabledTransactionId;
393 * otherwise return the current transaction id.
396 return (TransactionId) s->transactionIdData;
400 /* --------------------------------
401 * GetCurrentCommandId
402 * --------------------------------
405 GetCurrentCommandId()
407 TransactionState s = CurrentTransactionState;
410 * if the transaction system is disabled, we return
411 * the special "disabled" command id.
414 if (s->state == TRANS_DISABLED)
415 return (CommandId) DisabledCommandId;
423 TransactionState s = CurrentTransactionState;
426 * if the transaction system is disabled, we return
427 * the special "disabled" command id.
430 if (s->state == TRANS_DISABLED)
431 return (CommandId) DisabledCommandId;
433 return s->scanCommandId;
437 /* --------------------------------
438 * GetCurrentTransactionStartTime
439 * --------------------------------
442 GetCurrentTransactionStartTime()
444 TransactionState s = CurrentTransactionState;
447 * if the transaction system is disabled, we return
448 * the special "disabled" starting time.
451 if (s->state == TRANS_DISABLED)
452 return (AbsoluteTime) DisabledStartTime;
458 /* --------------------------------
459 * TransactionIdIsCurrentTransactionId
460 * --------------------------------
463 TransactionIdIsCurrentTransactionId(TransactionId xid)
465 TransactionState s = CurrentTransactionState;
471 TransactionIdEquals(xid, s->transactionIdData);
475 /* --------------------------------
476 * CommandIdIsCurrentCommandId
477 * --------------------------------
480 CommandIdIsCurrentCommandId(CommandId cid)
482 TransactionState s = CurrentTransactionState;
487 return (cid == s->commandId) ? true : false;
491 CommandIdGEScanCommandId(CommandId cid)
493 TransactionState s = CurrentTransactionState;
498 return (cid >= s->scanCommandId) ? true : false;
502 /* --------------------------------
503 * ClearCommandIdCounterOverflowFlag
504 * --------------------------------
508 ClearCommandIdCounterOverflowFlag()
510 CommandIdCounterOverflowFlag = false;
515 /* --------------------------------
516 * CommandCounterIncrement
517 * --------------------------------
520 CommandCounterIncrement()
522 CurrentTransactionStateData.commandId += 1;
523 if (CurrentTransactionStateData.commandId == FirstCommandId)
525 CommandIdCounterOverflowFlag = true;
526 elog(ERROR, "You may only have 2^32-1 commands per transaction");
529 CurrentTransactionStateData.scanCommandId = CurrentTransactionStateData.commandId;
532 * make cache changes visible to me. AtCommit_LocalCache() instead of
533 * AtCommit_Cache() is called here.
535 AtCommit_LocalCache();
541 SetScanCommandId(CommandId savedId)
544 CurrentTransactionStateData.scanCommandId = savedId;
548 /* ----------------------------------------------------------------
549 * initialization stuff
550 * ----------------------------------------------------------------
553 InitializeTransactionSystem()
555 InitializeTransactionLog();
558 /* ----------------------------------------------------------------
559 * StartTransaction stuff
560 * ----------------------------------------------------------------
563 /* --------------------------------
565 * --------------------------------
573 /* --------------------------------
575 * --------------------------------
582 * at present, it is unknown to me what belongs here -cim 3/18/90
584 * There isn't anything to do at the start of a xact for locks. -mer
589 /* --------------------------------
591 * --------------------------------
597 * We shouldn't have any transaction contexts already.
600 Assert(TopTransactionContext == NULL);
601 Assert(TransactionCommandContext == NULL);
604 * Create a toplevel context for the transaction.
607 TopTransactionContext =
608 AllocSetContextCreate(TopMemoryContext,
609 "TopTransactionContext",
610 ALLOCSET_DEFAULT_MINSIZE,
611 ALLOCSET_DEFAULT_INITSIZE,
612 ALLOCSET_DEFAULT_MAXSIZE);
615 * Create a statement-level context and make it active.
618 TransactionCommandContext =
619 AllocSetContextCreate(TopTransactionContext,
620 "TransactionCommandContext",
621 ALLOCSET_DEFAULT_MINSIZE,
622 ALLOCSET_DEFAULT_INITSIZE,
623 ALLOCSET_DEFAULT_MAXSIZE);
624 MemoryContextSwitchTo(TransactionCommandContext);
628 /* ----------------------------------------------------------------
629 * CommitTransaction stuff
630 * ----------------------------------------------------------------
633 /* --------------------------------
634 * RecordTransactionCommit
636 * Note: the two calls to BufferManagerFlush() exist to ensure
637 * that data pages are written before log pages. These
638 * explicit calls should be replaced by a more efficient
639 * ordered page write scheme in the buffer manager
641 * --------------------------------
644 RecordTransactionCommit()
650 * get the current transaction id
653 xid = GetCurrentTransactionId();
656 * flush the buffer manager pages. Note: if we have stable main
657 * memory, dirty shared buffers are not flushed plai 8/7/90
659 leak = BufferPoolCheckLeak();
662 * If no one shared buffer was changed by this transaction then we
663 * don't flush shared buffers and don't record commit status.
665 if (SharedBufferChanged)
669 ResetBufferPool(true);
672 * have the transaction access methods record the status of this
673 * transaction id in the pg_log relation.
675 TransactionIdCommit(xid);
678 * Now write the log info to the disk too.
680 leak = BufferPoolCheckLeak();
685 ResetBufferPool(true);
689 /* --------------------------------
691 * --------------------------------
697 * Make catalog changes visible to all backend.
700 RegisterInvalid(true);
703 /* --------------------------------
704 * AtCommit_LocalCache
705 * --------------------------------
708 AtCommit_LocalCache()
711 * Make catalog changes visible to me for the next command.
714 ImmediateLocalInvalidation(true);
717 /* --------------------------------
719 * --------------------------------
725 * XXX What if ProcReleaseLocks fails? (race condition?)
727 * Then you're up a creek! -mer 5/24/92
733 /* --------------------------------
735 * --------------------------------
741 * Now that we're "out" of a transaction, have the
742 * system allocate things in the top memory context instead
743 * of per-transaction contexts.
746 MemoryContextSwitchTo(TopMemoryContext);
749 * Release all transaction-local memory.
752 MemoryContextDelete(TopTransactionContext);
753 TopTransactionContext = NULL;
754 TransactionCommandContext = NULL;
757 /* ----------------------------------------------------------------
758 * AbortTransaction stuff
759 * ----------------------------------------------------------------
762 /* --------------------------------
763 * RecordTransactionAbort
764 * --------------------------------
767 RecordTransactionAbort()
772 * get the current transaction id
775 xid = GetCurrentTransactionId();
778 * Have the transaction access methods record the status of this
779 * transaction id in the pg_log relation. We skip it if no one shared
780 * buffer was changed by this transaction.
782 if (SharedBufferChanged && !TransactionIdDidCommit(xid))
783 TransactionIdAbort(xid);
786 * Tell bufmgr and smgr to release resources.
788 ResetBufferPool(false); /* false -> is abort */
791 /* --------------------------------
793 * --------------------------------
798 RelationCacheAbort();
799 RegisterInvalid(false);
802 /* --------------------------------
804 * --------------------------------
810 * XXX What if ProcReleaseLocks() fails? (race condition?)
812 * Then you're up a creek without a paddle! -mer
819 /* --------------------------------
821 * --------------------------------
827 * Make sure we are in a valid context (not a child of
828 * TransactionCommandContext...)
831 MemoryContextSwitchTo(TransactionCommandContext);
834 * We do not want to destroy transaction contexts yet,
835 * but it should be OK to delete any command-local memory.
838 MemoryContextResetAndDeleteChildren(TransactionCommandContext);
842 /* ----------------------------------------------------------------
843 * CleanupTransaction stuff
844 * ----------------------------------------------------------------
847 /* --------------------------------
849 * --------------------------------
855 * Now that we're "out" of a transaction, have the
856 * system allocate things in the top memory context instead
857 * of per-transaction contexts.
860 MemoryContextSwitchTo(TopMemoryContext);
863 * Release all transaction-local memory.
866 MemoryContextDelete(TopTransactionContext);
867 TopTransactionContext = NULL;
868 TransactionCommandContext = NULL;
872 /* ----------------------------------------------------------------
874 * ----------------------------------------------------------------
877 /* --------------------------------
880 * --------------------------------
885 TransactionState s = CurrentTransactionState;
888 XactIsoLevel = DefaultXactIsoLevel;
891 * Check the current transaction state. If the transaction system
892 * is switched off, or if we're already in a transaction, do nothing.
893 * We're already in a transaction when the monitor sends a null
894 * command to the backend to flush the comm channel. This is a
895 * hacky fix to a communications problem, and we keep having to
896 * deal with it here. We should fix the comm channel code. mao 080891
899 if (s->state == TRANS_DISABLED || s->state == TRANS_INPROGRESS)
903 * set the current transaction state information
904 * appropriately during start processing
907 s->state = TRANS_START;
909 SetReindexProcessing(false);
912 * generate a new transaction id
915 GetNewTransactionId(&(s->transactionIdData));
917 XactLockTableInsert(s->transactionIdData);
920 * initialize current transaction state fields
923 s->commandId = FirstCommandId;
924 s->scanCommandId = FirstCommandId;
925 s->startTime = GetCurrentAbsoluteTime();
928 * initialize the various transaction subsystems
936 * Tell the trigger manager to we're starting a transaction
939 DeferredTriggerBeginXact();
942 * done with start processing, set current transaction
943 * state to "in progress"
946 s->state = TRANS_INPROGRESS;
952 * Tell me if we are currently in progress
956 CurrentXactInProgress()
958 return CurrentTransactionState->state == TRANS_INPROGRESS;
962 /* --------------------------------
965 * --------------------------------
970 TransactionState s = CurrentTransactionState;
973 * check the current transaction state
976 if (s->state == TRANS_DISABLED)
979 if (s->state != TRANS_INPROGRESS)
980 elog(NOTICE, "CommitTransaction and not in in-progress state ");
983 * Tell the trigger manager that this transaction is about to be
984 * committed. He'll invoke all trigger deferred until XACT before
985 * we really start on committing the transaction.
988 DeferredTriggerEndXact();
991 * set the current transaction state information
992 * appropriately during the abort processing
995 s->state = TRANS_COMMIT;
998 * do commit processing
1002 /* handle commit for large objects [ PA, 7/17/98 ] */
1005 /* NOTIFY commit must also come before lower-level cleanup */
1010 RecordTransactionCommit();
1013 * Let others know about no transaction in progress by me. Note that
1014 * this must be done _before_ releasing locks we hold and
1015 * SpinAcquire(SInvalLock) is required: UPDATE with xid 0 is blocked
1016 * by xid 1' UPDATE, xid 1 is doing commit while xid 2 gets snapshot -
1017 * if xid 2' GetSnapshotData sees xid 1 as running then it must see
1018 * xid 0 as running as well or it will see two tuple versions - one
1019 * deleted by xid 1 and one inserted by xid 0.
1021 if (MyProc != (PROC *) NULL)
1023 /* Lock SInvalLock because that's what GetSnapshotData uses. */
1024 SpinAcquire(SInvalLock);
1025 MyProc->xid = InvalidTransactionId;
1026 MyProc->xmin = InvalidTransactionId;
1027 SpinRelease(SInvalLock);
1030 RelationPurgeLocalRelation(true);
1038 SharedBufferChanged = false; /* safest place to do it */
1041 * done with commit processing, set current transaction
1042 * state back to default
1045 s->state = TRANS_DEFAULT;
1048 /* --------------------------------
1051 * --------------------------------
1056 TransactionState s = CurrentTransactionState;
1059 * Let others to know about no transaction in progress - vadim
1062 if (MyProc != (PROC *) NULL)
1064 MyProc->xid = InvalidTransactionId;
1065 MyProc->xmin = InvalidTransactionId;
1069 * check the current transaction state
1072 if (s->state == TRANS_DISABLED)
1075 if (s->state != TRANS_INPROGRESS)
1076 elog(NOTICE, "AbortTransaction and not in in-progress state");
1079 * Tell the trigger manager that this transaction is about to be
1083 DeferredTriggerAbortXact();
1086 * set the current transaction state information
1087 * appropriately during the abort processing
1090 s->state = TRANS_ABORT;
1093 * do abort processing
1096 lo_commit(false); /* 'false' means it's abort */
1101 RecordTransactionAbort();
1102 RelationPurgeLocalRelation(false);
1103 invalidate_temp_relations();
1111 SharedBufferChanged = false; /* safest place to do it */
1114 * State remains TRANS_ABORT until CleanupTransaction().
1119 /* --------------------------------
1120 * CleanupTransaction
1122 * --------------------------------
1125 CleanupTransaction()
1127 TransactionState s = CurrentTransactionState;
1129 if (s->state == TRANS_DISABLED)
1133 * State should still be TRANS_ABORT from AbortTransaction().
1136 if (s->state != TRANS_ABORT)
1137 elog(FATAL, "CleanupTransaction and not in abort state");
1140 * do abort cleanup processing
1146 * done with abort processing, set current transaction
1147 * state back to default
1150 s->state = TRANS_DEFAULT;
1153 /* --------------------------------
1154 * StartTransactionCommand
1155 * --------------------------------
1158 StartTransactionCommand()
1160 TransactionState s = CurrentTransactionState;
1162 switch (s->blockState)
1165 * if we aren't in a transaction block, we
1166 * just do our usual start transaction.
1169 case TBLOCK_DEFAULT:
1174 * We should never experience this -- if we do it
1175 * means the BEGIN state was not changed in the previous
1176 * CommitTransactionCommand(). If we get it, we print
1177 * a warning and change to the in-progress state.
1181 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_BEGIN");
1182 s->blockState = TBLOCK_INPROGRESS;
1186 * This is the case when are somewhere in a transaction
1187 * block and about to start a new command. For now we
1188 * do nothing but someday we may do command-local resource
1192 case TBLOCK_INPROGRESS:
1196 * As with BEGIN, we should never experience this
1197 * if we do it means the END state was not changed in the
1198 * previous CommitTransactionCommand(). If we get it, we
1199 * print a warning, commit the transaction, start a new
1200 * transaction and change to the default state.
1204 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_END");
1205 s->blockState = TBLOCK_DEFAULT;
1206 CommitTransaction();
1211 * Here we are in the middle of a transaction block but
1212 * one of the commands caused an abort so we do nothing
1213 * but remain in the abort state. Eventually we will get
1214 * to the "END 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
1223 * we remain in the ENDABORT state and maybe next time
1224 * we get to CommitTransactionCommand() the state will
1225 * get reset to default.
1228 case TBLOCK_ENDABORT:
1229 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_ENDABORT");
1234 * We must switch to TransactionCommandContext before returning.
1235 * This is already done if we called StartTransaction, otherwise not.
1237 Assert(TransactionCommandContext != NULL);
1238 MemoryContextSwitchTo(TransactionCommandContext);
1241 /* --------------------------------
1242 * CommitTransactionCommand
1243 * --------------------------------
1246 CommitTransactionCommand()
1248 TransactionState s = CurrentTransactionState;
1250 switch (s->blockState)
1253 * if we aren't in a transaction block, we
1254 * just do our usual transaction commit
1257 case TBLOCK_DEFAULT:
1258 CommitTransaction();
1262 * This is the case right after we get a "BEGIN TRANSACTION"
1263 * command, but the user hasn't done anything else yet, so
1264 * we change to the "transaction block in progress" state
1269 s->blockState = TBLOCK_INPROGRESS;
1273 * This is the case when we have finished executing a command
1274 * someplace within a transaction block. We increment the
1275 * command counter and return. Someday we may free resources
1276 * local to the command.
1278 * That someday is today, at least for memory allocated in
1279 * TransactionCommandContext.
1283 case TBLOCK_INPROGRESS:
1284 CommandCounterIncrement();
1285 MemoryContextResetAndDeleteChildren(TransactionCommandContext);
1289 * This is the case when we just got the "END TRANSACTION"
1290 * statement, so we commit the transaction and go back to
1291 * the default state.
1295 CommitTransaction();
1296 s->blockState = TBLOCK_DEFAULT;
1300 * Here we are in the middle of a transaction block but
1301 * one of the commands caused an abort so we do nothing
1302 * but remain in the abort state. Eventually we will get
1303 * to the "END TRANSACTION" which will set things straight.
1310 * Here we were in an aborted transaction block which
1311 * just processed the "END TRANSACTION" command from the
1312 * user, so clean up and return to the default state.
1315 case TBLOCK_ENDABORT:
1316 CleanupTransaction();
1317 s->blockState = TBLOCK_DEFAULT;
1322 /* --------------------------------
1323 * AbortCurrentTransaction
1324 * --------------------------------
1327 AbortCurrentTransaction()
1329 TransactionState s = CurrentTransactionState;
1331 switch (s->blockState)
1334 * if we aren't in a transaction block, we
1335 * just do the basic abort & cleanup transaction.
1338 case TBLOCK_DEFAULT:
1340 CleanupTransaction();
1344 * If we are in the TBLOCK_BEGIN it means something
1345 * screwed up right after reading "BEGIN TRANSACTION"
1346 * so we enter the abort state. Eventually an "END
1347 * TRANSACTION" will fix things.
1351 s->blockState = TBLOCK_ABORT;
1353 /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1357 * This is the case when are somewhere in a transaction
1358 * block which aborted so we abort the transaction and
1359 * set the ABORT state. Eventually an "END TRANSACTION"
1360 * will fix things and restore us to a normal state.
1363 case TBLOCK_INPROGRESS:
1364 s->blockState = TBLOCK_ABORT;
1366 /* CleanupTransaction happens when we exit TBLOCK_ABORT */
1370 * Here, the system was fouled up just after the
1371 * user wanted to end the transaction block so we
1372 * abort the transaction and put us back into the
1377 s->blockState = TBLOCK_DEFAULT;
1379 CleanupTransaction();
1383 * Here, we are already in an aborted transaction
1384 * state and are waiting for an "END TRANSACTION" to
1385 * come along and lo and behold, we abort again!
1386 * So we just remain in the abort state.
1393 * Here we were in an aborted transaction block which
1394 * just processed the "END TRANSACTION" command but somehow
1395 * aborted again.. since we must have done the abort
1396 * processing, we clean up and return to the default state.
1399 case TBLOCK_ENDABORT:
1400 CleanupTransaction();
1401 s->blockState = TBLOCK_DEFAULT;
1406 /* ----------------------------------------------------------------
1407 * transaction block support
1408 * ----------------------------------------------------------------
1410 /* --------------------------------
1411 * BeginTransactionBlock
1412 * --------------------------------
1415 BeginTransactionBlock(void)
1417 TransactionState s = CurrentTransactionState;
1420 * check the current transaction state
1423 if (s->state == TRANS_DISABLED)
1426 if (s->blockState != TBLOCK_DEFAULT)
1427 elog(NOTICE, "BEGIN: already a transaction in progress");
1430 * set the current transaction block state information
1431 * appropriately during begin processing
1434 s->blockState = TBLOCK_BEGIN;
1437 * do begin processing
1442 * done with begin processing, set block state to inprogress
1445 s->blockState = TBLOCK_INPROGRESS;
1448 /* --------------------------------
1449 * EndTransactionBlock
1450 * --------------------------------
1453 EndTransactionBlock(void)
1455 TransactionState s = CurrentTransactionState;
1458 * check the current transaction state
1461 if (s->state == TRANS_DISABLED)
1464 if (s->blockState == TBLOCK_INPROGRESS)
1467 * here we are in a transaction block which should commit
1468 * when we get to the upcoming CommitTransactionCommand()
1469 * so we set the state to "END". CommitTransactionCommand()
1470 * will recognize this and commit the transaction and return
1471 * us to the default state
1474 s->blockState = TBLOCK_END;
1478 if (s->blockState == TBLOCK_ABORT)
1481 * here, we are in a transaction block which aborted
1482 * and since the AbortTransaction() was already done,
1483 * we do whatever is needed and change to the special
1484 * "END ABORT" state. The upcoming CommitTransactionCommand()
1485 * will recognise this and then put us back in the default
1489 s->blockState = TBLOCK_ENDABORT;
1494 * here, the user issued COMMIT when not inside a transaction.
1495 * Issue a notice and go to abort state. The upcoming call to
1496 * CommitTransactionCommand() will then put us back into the
1500 elog(NOTICE, "COMMIT: no transaction in progress");
1502 s->blockState = TBLOCK_ENDABORT;
1505 /* --------------------------------
1506 * AbortTransactionBlock
1507 * --------------------------------
1511 AbortTransactionBlock(void)
1513 TransactionState s = CurrentTransactionState;
1516 * check the current transaction state
1519 if (s->state == TRANS_DISABLED)
1522 if (s->blockState == TBLOCK_INPROGRESS)
1525 * here we were inside a transaction block something
1526 * screwed up inside the system so we enter the abort state,
1527 * do the abort processing and then return.
1528 * We remain in the abort state until we see an
1529 * END TRANSACTION command.
1532 s->blockState = TBLOCK_ABORT;
1538 * here, the user issued ABORT when not inside a transaction.
1539 * Issue a notice and go to abort state. The upcoming call to
1540 * CommitTransactionCommand() will then put us back into the
1544 elog(NOTICE, "ROLLBACK: no transaction in progress");
1546 s->blockState = TBLOCK_ENDABORT;
1551 /* --------------------------------
1552 * UserAbortTransactionBlock
1553 * --------------------------------
1556 UserAbortTransactionBlock()
1558 TransactionState s = CurrentTransactionState;
1561 * check the current transaction state
1564 if (s->state == TRANS_DISABLED)
1568 * if the transaction has already been automatically aborted with an
1569 * error, and the user subsequently types 'abort', allow it. (the
1570 * behavior is the same as if they had typed 'end'.)
1572 if (s->blockState == TBLOCK_ABORT)
1574 s->blockState = TBLOCK_ENDABORT;
1578 if (s->blockState == TBLOCK_INPROGRESS)
1581 * here we were inside a transaction block and we
1582 * got an abort command from the user, so we move to
1583 * the abort state, do the abort processing and
1584 * then change to the ENDABORT state so we will end up
1585 * in the default state after the upcoming
1586 * CommitTransactionCommand().
1589 s->blockState = TBLOCK_ABORT;
1591 s->blockState = TBLOCK_ENDABORT;
1596 * here, the user issued ABORT when not inside a transaction.
1597 * Issue a notice and go to abort state. The upcoming call to
1598 * CommitTransactionCommand() will then put us back into the
1602 elog(NOTICE, "ROLLBACK: no transaction in progress");
1604 s->blockState = TBLOCK_ENDABORT;
1607 /* --------------------------------
1608 * AbortOutOfAnyTransaction
1610 * This routine is provided for error recovery purposes. It aborts any
1611 * active transaction or transaction block, leaving the system in a known
1613 * --------------------------------
1616 AbortOutOfAnyTransaction()
1618 TransactionState s = CurrentTransactionState;
1621 * Get out of any low-level transaction
1623 if (s->state != TRANS_DEFAULT)
1626 CleanupTransaction();
1630 * Now reset the high-level state
1632 s->blockState = TBLOCK_DEFAULT;
1636 IsTransactionBlock()
1638 TransactionState s = CurrentTransactionState;
1640 if (s->blockState == TBLOCK_INPROGRESS
1641 || s->blockState == TBLOCK_ABORT
1642 || s->blockState == TBLOCK_ENDABORT)