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.61 2000/02/18 09:30:20 inoue 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 shes 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
46 * This file is an attempt at a redesign of the upper layer
47 * of the V1 transaction system which was too poorly thought
48 * out to describe. This new system hopes to be both simpler
49 * in design, simpler to extend and needs to contain added
50 * functionality to solve problems beyond the scope of the V1
51 * system. (In particuler, communication of transaction
52 * information between parallel backends has to be supported)
54 * The essential aspects of the transaction system are:
56 * o transaction id generation
57 * o transaction log updating
59 * o cache invalidation
62 * Hence, the functional division of the transaction code is
63 * based on what of the above things need to be done during
64 * a start/commit/abort transaction. For instance, the
65 * routine AtCommit_Memory() takes care of all the memory
66 * cleanup stuff done at commit time.
68 * The code is layered as follows:
73 * UserAbortTransaction
75 * are provided to do the lower level work like recording
76 * the transaction status in the log and doing memory cleanup.
77 * above these routines are another set of functions:
79 * StartTransactionCommand
80 * CommitTransactionCommand
81 * AbortCurrentTransaction
83 * These are the routines used in the postgres main processing
84 * loop. They are sensitive to the current transaction block state
85 * and make calls to the lower level routines appropriately.
87 * Support for transaction blocks is provided via the functions:
89 * StartTransactionBlock
90 * CommitTransactionBlock
91 * AbortTransactionBlock
93 * These are invoked only in responce to a user "BEGIN", "END",
94 * or "ABORT" command. The tricky part about these functions
95 * is that they are called within the postgres main loop, in between
96 * the StartTransactionCommand() and CommitTransactionCommand().
98 * For example, consider the following sequence of user commands:
101 * 2) retrieve (foo.all)
102 * 3) append foo (bar = baz)
105 * in the main processing loop, this results in the following
106 * transaction sequence:
108 * / StartTransactionCommand();
109 * 1) / ProcessUtility(); << begin
110 * \ StartTransactionBlock();
111 * \ CommitTransactionCommand();
113 * / StartTransactionCommand();
114 * 2) < ProcessQuery(); << retrieve (foo.all)
115 * \ CommitTransactionCommand();
117 * / StartTransactionCommand();
118 * 3) < ProcessQuery(); << append foo (bar = baz)
119 * \ CommitTransactionCommand();
121 * / StartTransactionCommand();
122 * 4) / ProcessUtility(); << end
123 * \ CommitTransactionBlock();
124 * \ CommitTransactionCommand();
126 * The point of this example is to demonstrate the need for
127 * StartTransactionCommand() and CommitTransactionCommand() to
128 * be state smart -- they should do nothing in between the calls
129 * to StartTransactionBlock() and EndTransactionBlock() and
130 * outside these calls they need to do normal start/commit
133 * Furthermore, suppose the "retrieve (foo.all)" caused an abort
134 * condition. We would then want to abort the transaction and
135 * ignore all subsequent commands up to the "end".
138 *-------------------------------------------------------------------------
142 * Large object clean up added in CommitTransaction() to prevent buffer leaks.
144 * [PA] is Pascal André <andre@via.ecp.fr>
146 #include "postgres.h"
148 #include "access/nbtree.h"
149 #include "catalog/heap.h"
150 #include "catalog/index.h"
151 #include "commands/async.h"
152 #include "commands/sequence.h"
153 #include "commands/vacuum.h"
154 #include "commands/trigger.h"
155 #include "libpq/be-fsstubs.h"
156 #include "storage/proc.h"
157 #include "storage/sinval.h"
158 #include "utils/temprel.h"
159 #include "utils/inval.h"
160 #include "utils/portal.h"
161 #include "utils/relcache.h"
163 extern bool SharedBufferChanged;
165 static void AbortTransaction(void);
166 static void AtAbort_Cache(void);
167 static void AtAbort_Locks(void);
168 static void AtAbort_Memory(void);
169 static void AtCommit_Cache(void);
170 static void AtCommit_LocalCache(void);
171 static void AtCommit_Locks(void);
172 static void AtCommit_Memory(void);
173 static void AtStart_Cache(void);
174 static void AtStart_Locks(void);
175 static void AtStart_Memory(void);
176 static void CommitTransaction(void);
177 static void RecordTransactionAbort(void);
178 static void RecordTransactionCommit(void);
179 static void StartTransaction(void);
182 * global variables holding the current transaction state.
184 * Note: when we are running several slave processes, the
185 * current transaction state data is copied into shared memory
186 * and the CurrentTransactionState pointer changed to
187 * point to the shared copy. All this occurrs in slaves.c
190 TransactionStateData CurrentTransactionStateData = {
191 0, /* transaction id */
192 FirstCommandId, /* command id */
193 0x0, /* start time */
194 TRANS_DEFAULT, /* transaction state */
195 TBLOCK_DEFAULT /* transaction block state */
198 TransactionState CurrentTransactionState = &CurrentTransactionStateData;
200 int DefaultXactIsoLevel = XACT_READ_COMMITTED;
204 * info returned when the system is disabled
206 * Apparently a lot of this code is inherited from other prototype systems.
207 * For DisabledStartTime, use a symbolic value to make the relationships clearer.
208 * The old value of 1073741823 corresponds to a date in y2004, which is coming closer
209 * every day. It appears that if we return a value guaranteed larger than
210 * any real time associated with a transaction then comparisons in other
211 * modules will still be correct. Let's use BIG_ABSTIME for this. tgl 2/14/97
213 * Note: I have no idea what the significance of the
214 * 1073741823 in DisabledStartTime.. I just carried
215 * this over when converting things from the old
216 * V1 transaction system. -cim 3/18/90
219 TransactionId DisabledTransactionId = (TransactionId) -1;
221 CommandId DisabledCommandId = (CommandId) -1;
223 AbsoluteTime DisabledStartTime = (AbsoluteTime) BIG_ABSTIME; /* 1073741823; */
229 bool CommandIdCounterOverflowFlag;
232 * catalog creation transaction bootstrapping flag.
233 * This should be eliminated and added to the transaction
234 * state stuff. -cim 3/19/90
237 bool AMI_OVERRIDE = false;
239 /* ----------------------------------------------------------------
240 * transaction state accessors
241 * ----------------------------------------------------------------
244 /* --------------------------------
245 * TranactionFlushEnabled()
246 * SetTranactionFlushEnabled()
248 * These are used to test and set the "TransactionFlushState"
249 * varable. If this variable is true (the default), then
250 * the system will flush all dirty buffers to disk at the end
251 * of each transaction. If false then we are assuming the
252 * buffer pool resides in stable main memory, in which case we
253 * only do writes as necessary.
254 * --------------------------------
256 static int TransactionFlushState = 1;
259 TransactionFlushEnabled(void)
261 return TransactionFlushState;
266 SetTransactionFlushEnabled(bool state)
268 TransactionFlushState = (state == true);
272 /* --------------------------------
275 * This returns true if we are currently running a query
276 * within an executing transaction.
277 * --------------------------------
280 IsTransactionState(void)
282 TransactionState s = CurrentTransactionState;
290 case TRANS_INPROGRESS:
301 * Shouldn't get here, but lint is not happy with this...
308 /* --------------------------------
309 * IsAbortedTransactionBlockState
311 * This returns true if we are currently running a query
312 * within an aborted transaction block.
313 * --------------------------------
316 IsAbortedTransactionBlockState()
318 TransactionState s = CurrentTransactionState;
320 if (s->blockState == TBLOCK_ABORT)
326 /* --------------------------------
327 * OverrideTransactionSystem
329 * This is used to temporarily disable the transaction
330 * processing system in order to do initialization of
331 * the transaction system data structures and relations
333 * --------------------------------
335 int SavedTransactionState;
338 OverrideTransactionSystem(bool flag)
340 TransactionState s = CurrentTransactionState;
344 if (s->state == TRANS_DISABLED)
347 SavedTransactionState = s->state;
348 s->state = TRANS_DISABLED;
352 if (s->state != TRANS_DISABLED)
355 s->state = SavedTransactionState;
359 /* --------------------------------
360 * GetCurrentTransactionId
362 * This returns the id of the current transaction, or
363 * the id of the "disabled" transaction.
364 * --------------------------------
367 GetCurrentTransactionId()
369 TransactionState s = CurrentTransactionState;
372 * if the transaction system is disabled, we return
373 * the special "disabled" transaction id.
376 if (s->state == TRANS_DISABLED)
377 return (TransactionId) DisabledTransactionId;
380 * otherwise return the current transaction id.
383 return (TransactionId) s->transactionIdData;
387 /* --------------------------------
388 * GetCurrentCommandId
389 * --------------------------------
392 GetCurrentCommandId()
394 TransactionState s = CurrentTransactionState;
397 * if the transaction system is disabled, we return
398 * the special "disabled" command id.
401 if (s->state == TRANS_DISABLED)
402 return (CommandId) DisabledCommandId;
410 TransactionState s = CurrentTransactionState;
413 * if the transaction system is disabled, we return
414 * the special "disabled" command id.
417 if (s->state == TRANS_DISABLED)
418 return (CommandId) DisabledCommandId;
420 return s->scanCommandId;
424 /* --------------------------------
425 * GetCurrentTransactionStartTime
426 * --------------------------------
429 GetCurrentTransactionStartTime()
431 TransactionState s = CurrentTransactionState;
434 * if the transaction system is disabled, we return
435 * the special "disabled" starting time.
438 if (s->state == TRANS_DISABLED)
439 return (AbsoluteTime) DisabledStartTime;
445 /* --------------------------------
446 * TransactionIdIsCurrentTransactionId
447 * --------------------------------
450 TransactionIdIsCurrentTransactionId(TransactionId xid)
452 TransactionState s = CurrentTransactionState;
458 TransactionIdEquals(xid, s->transactionIdData);
462 /* --------------------------------
463 * CommandIdIsCurrentCommandId
464 * --------------------------------
467 CommandIdIsCurrentCommandId(CommandId cid)
469 TransactionState s = CurrentTransactionState;
474 return (cid == s->commandId) ? true : false;
478 CommandIdGEScanCommandId(CommandId cid)
480 TransactionState s = CurrentTransactionState;
485 return (cid >= s->scanCommandId) ? true : false;
489 /* --------------------------------
490 * ClearCommandIdCounterOverflowFlag
491 * --------------------------------
495 ClearCommandIdCounterOverflowFlag()
497 CommandIdCounterOverflowFlag = false;
502 /* --------------------------------
503 * CommandCounterIncrement
504 * --------------------------------
507 CommandCounterIncrement()
509 CurrentTransactionStateData.commandId += 1;
510 if (CurrentTransactionStateData.commandId == FirstCommandId)
512 CommandIdCounterOverflowFlag = true;
513 elog(ERROR, "You may only have 2^32-1 commands per transaction");
516 CurrentTransactionStateData.scanCommandId = CurrentTransactionStateData.commandId;
519 * make cache changes visible to me. AtCommit_LocalCache()
520 * instead of AtCommit_Cache() is called here.
522 AtCommit_LocalCache();
528 SetScanCommandId(CommandId savedId)
531 CurrentTransactionStateData.scanCommandId = savedId;
535 /* ----------------------------------------------------------------
536 * initialization stuff
537 * ----------------------------------------------------------------
540 InitializeTransactionSystem()
542 InitializeTransactionLog();
545 /* ----------------------------------------------------------------
546 * StartTransaction stuff
547 * ----------------------------------------------------------------
550 /* --------------------------------
552 * --------------------------------
560 /* --------------------------------
562 * --------------------------------
569 * at present, it is unknown to me what belongs here -cim 3/18/90
571 * There isn't anything to do at the start of a xact for locks. -mer
576 /* --------------------------------
578 * --------------------------------
584 MemoryContext portalContext;
587 * get the blank portal and its memory context
590 portal = GetPortalByName(NULL);
591 portalContext = (MemoryContext) PortalGetHeapMemory(portal);
594 * tell system to allocate in the blank portal context
597 MemoryContextSwitchTo(portalContext);
598 StartPortalAllocMode(DefaultAllocMode, 0);
602 /* ----------------------------------------------------------------
603 * CommitTransaction stuff
604 * ----------------------------------------------------------------
607 /* --------------------------------
608 * RecordTransactionCommit
610 * Note: the two calls to BufferManagerFlush() exist to ensure
611 * that data pages are written before log pages. These
612 * explicit calls should be replaced by a more efficient
613 * ordered page write scheme in the buffer manager
615 * --------------------------------
618 RecordTransactionCommit()
624 * get the current transaction id
627 xid = GetCurrentTransactionId();
630 * flush the buffer manager pages. Note: if we have stable
631 * main memory, dirty shared buffers are not flushed
634 leak = BufferPoolCheckLeak();
637 * If no one shared buffer was changed by this transaction then
638 * we don't flush shared buffers and don't record commit status.
640 if (SharedBufferChanged)
647 * have the transaction access methods record the status
648 * of this transaction id in the pg_log relation.
650 TransactionIdCommit(xid);
653 * Now write the log info to the disk too.
655 leak = BufferPoolCheckLeak();
664 /* --------------------------------
666 * --------------------------------
672 * Make catalog changes visible to all backend.
675 RegisterInvalid(true);
678 /* --------------------------------
679 * AtCommit_LocalCache
680 * --------------------------------
683 AtCommit_LocalCache()
686 * Make catalog changes visible to me for the next command.
689 ImmediateLocalInvalidation(true);
692 /* --------------------------------
694 * --------------------------------
700 * XXX What if ProcReleaseLocks fails? (race condition?)
702 * Then you're up a creek! -mer 5/24/92
708 /* --------------------------------
710 * --------------------------------
718 * Release all heap memory in the blank portal.
721 portal = GetPortalByName(NULL);
722 PortalResetHeapMemory(portal);
725 * Now that we're "out" of a transaction, have the
726 * system allocate things in the top memory context instead
727 * of the blank portal memory context.
730 MemoryContextSwitchTo(TopMemoryContext);
733 /* ----------------------------------------------------------------
734 * AbortTransaction stuff
735 * ----------------------------------------------------------------
738 /* --------------------------------
739 * RecordTransactionAbort
740 * --------------------------------
743 RecordTransactionAbort()
748 * get the current transaction id
751 xid = GetCurrentTransactionId();
754 * Have the transaction access methods record the status of
755 * this transaction id in the pg_log relation. We skip it
756 * if no one shared buffer was changed by this transaction.
758 if (SharedBufferChanged && !TransactionIdDidCommit(xid))
759 TransactionIdAbort(xid);
764 /* --------------------------------
766 * --------------------------------
771 RelationCacheAbort();
772 RegisterInvalid(false);
775 /* --------------------------------
777 * --------------------------------
783 * XXX What if ProcReleaseLocks() fails? (race condition?)
785 * Then you're up a creek without a paddle! -mer
792 /* --------------------------------
794 * --------------------------------
802 * Release all heap memory in the blank portal.
805 portal = GetPortalByName(NULL);
806 PortalResetHeapMemory(portal);
809 * Now that we're "out" of a transaction, have the
810 * system allocate things in the top memory context instead
811 * of the blank portal memory context.
814 MemoryContextSwitchTo(TopMemoryContext);
817 /* ----------------------------------------------------------------
819 * ----------------------------------------------------------------
822 /* --------------------------------
825 * --------------------------------
830 TransactionState s = CurrentTransactionState;
833 XactIsoLevel = DefaultXactIsoLevel;
836 * Check the current transaction state. If the transaction system
837 * is switched off, or if we're already in a transaction, do nothing.
838 * We're already in a transaction when the monitor sends a null
839 * command to the backend to flush the comm channel. This is a
840 * hacky fix to a communications problem, and we keep having to
841 * deal with it here. We should fix the comm channel code. mao 080891
844 if (s->state == TRANS_DISABLED || s->state == TRANS_INPROGRESS)
848 * set the current transaction state information
849 * appropriately during start processing
852 s->state = TRANS_START;
854 SetReindexProcessing(false);
856 * generate a new transaction id
859 GetNewTransactionId(&(s->transactionIdData));
861 XactLockTableInsert(s->transactionIdData);
864 * initialize current transaction state fields
867 s->commandId = FirstCommandId;
868 s->scanCommandId = FirstCommandId;
869 s->startTime = GetCurrentAbsoluteTime();
872 * initialize the various transaction subsystems
880 initialize temporary relations list
881 the tempRelList is a list of temporary relations that
882 are created in the course of the transactions
883 they need to be destroyed properly at the end of the transactions
888 * Tell the trigger manager to we're starting a transaction
891 DeferredTriggerBeginXact();
894 * done with start processing, set current transaction
895 * state to "in progress"
898 s->state = TRANS_INPROGRESS;
903 * Tell me if we are currently in progress
907 CurrentXactInProgress()
909 return CurrentTransactionState->state == TRANS_INPROGRESS;
912 /* --------------------------------
915 * --------------------------------
920 TransactionState s = CurrentTransactionState;
923 * check the current transaction state
926 if (s->state == TRANS_DISABLED)
929 if (s->state != TRANS_INPROGRESS)
930 elog(NOTICE, "CommitTransaction and not in in-progress state ");
933 * Tell the trigger manager that this transaction is about to be
934 * committed. He'll invoke all trigger deferred until XACT before
935 * we really start on committing the transaction.
938 DeferredTriggerEndXact();
941 * set the current transaction state information
942 * appropriately during the abort processing
945 s->state = TRANS_COMMIT;
948 * do commit processing
952 /* handle commit for large objects [ PA, 7/17/98 ] */
955 /* NOTIFY commit must also come before lower-level cleanup */
961 RecordTransactionCommit();
964 * Let others know about no transaction in progress by me.
965 * Note that this must be done _before_ releasing locks we hold
966 * and SpinAcquire(SInvalLock) is required: UPDATE with xid 0 is
967 * blocked by xid 1' UPDATE, xid 1 is doing commit while xid 2
968 * gets snapshot - if xid 2' GetSnapshotData sees xid 1 as running
969 * then it must see xid 0 as running as well or it will see two
970 * tuple versions - one deleted by xid 1 and one inserted by xid 0.
972 if (MyProc != (PROC *) NULL)
974 /* Lock SInvalLock because that's what GetSnapshotData uses. */
975 SpinAcquire(SInvalLock);
976 MyProc->xid = InvalidTransactionId;
977 MyProc->xmin = InvalidTransactionId;
978 SpinRelease(SInvalLock);
981 RelationPurgeLocalRelation(true);
989 * done with commit processing, set current transaction
990 * state back to default
993 s->state = TRANS_DEFAULT;
994 SharedBufferChanged = false; /* safest place to do it */
998 /* --------------------------------
1001 * --------------------------------
1006 TransactionState s = CurrentTransactionState;
1009 * Let others to know about no transaction in progress - vadim
1012 if (MyProc != (PROC *) NULL)
1014 MyProc->xid = InvalidTransactionId;
1015 MyProc->xmin = InvalidTransactionId;
1019 * check the current transaction state
1022 if (s->state == TRANS_DISABLED)
1025 if (s->state != TRANS_INPROGRESS)
1026 elog(NOTICE, "AbortTransaction and not in in-progress state ");
1029 * Tell the trigger manager that this transaction is about to be
1033 DeferredTriggerAbortXact();
1036 * set the current transaction state information
1037 * appropriately during the abort processing
1040 s->state = TRANS_ABORT;
1043 * do abort processing
1046 lo_commit(false); /* 'false' means it's abort */
1051 if (CommonSpecialPortalIsOpen())
1052 CommonSpecialPortalClose();
1053 RecordTransactionAbort();
1054 RelationPurgeLocalRelation(false);
1056 invalidate_temp_relations();
1064 * done with abort processing, set current transaction
1065 * state back to default
1068 s->state = TRANS_DEFAULT;
1069 SharedBufferChanged = false; /* safest place to do it */
1072 /* --------------------------------
1073 * StartTransactionCommand
1074 * --------------------------------
1077 StartTransactionCommand()
1079 TransactionState s = CurrentTransactionState;
1081 switch (s->blockState)
1084 * if we aren't in a transaction block, we
1085 * just do our usual start transaction.
1088 case TBLOCK_DEFAULT:
1093 * We should never experience this -- if we do it
1094 * means the BEGIN state was not changed in the previous
1095 * CommitTransactionCommand(). If we get it, we print
1096 * a warning and change to the in-progress state.
1100 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_BEGIN");
1101 s->blockState = TBLOCK_INPROGRESS;
1105 * This is the case when are somewhere in a transaction
1106 * block and about to start a new command. For now we
1107 * do nothing but someday we may do command-local resource
1111 case TBLOCK_INPROGRESS:
1115 * As with BEGIN, we should never experience this
1116 * if we do it means the END state was not changed in the
1117 * previous CommitTransactionCommand(). If we get it, we
1118 * print a warning, commit the transaction, start a new
1119 * transaction and change to the default state.
1123 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_END");
1124 s->blockState = TBLOCK_DEFAULT;
1125 CommitTransaction();
1130 * Here we are in the middle of a transaction block but
1131 * one of the commands caused an abort so we do nothing
1132 * but remain in the abort state. Eventually we will get
1133 * to the "END TRANSACTION" which will set things straight.
1140 * This means we somehow aborted and the last call to
1141 * CommitTransactionCommand() didn't clear the state so
1142 * we remain in the ENDABORT state and mabey next time
1143 * we get to CommitTransactionCommand() the state will
1144 * get reset to default.
1147 case TBLOCK_ENDABORT:
1148 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_ENDABORT");
1153 /* --------------------------------
1154 * CommitTransactionCommand
1155 * --------------------------------
1158 CommitTransactionCommand()
1160 TransactionState s = CurrentTransactionState;
1162 switch (s->blockState)
1165 * if we aren't in a transaction block, we
1166 * just do our usual transaction commit
1169 case TBLOCK_DEFAULT:
1170 CommitTransaction();
1174 * This is the case right after we get a "BEGIN TRANSACTION"
1175 * command, but the user hasn't done anything else yet, so
1176 * we change to the "transaction block in progress" state
1181 s->blockState = TBLOCK_INPROGRESS;
1185 * This is the case when we have finished executing a command
1186 * someplace within a transaction block. We increment the
1187 * command counter and return. Someday we may free resources
1188 * local to the command.
1190 * That someday is today, at least for memory allocated by
1191 * command in the BlankPortal' HeapMemory context.
1195 case TBLOCK_INPROGRESS:
1196 CommandCounterIncrement();
1197 #ifdef TBL_FREE_CMD_MEMORY
1198 EndPortalAllocMode();
1199 StartPortalAllocMode(DefaultAllocMode, 0);
1204 * This is the case when we just got the "END TRANSACTION"
1205 * statement, so we go back to the default state and
1206 * commit the transaction.
1210 s->blockState = TBLOCK_DEFAULT;
1211 CommitTransaction();
1215 * Here we are in the middle of a transaction block but
1216 * one of the commands caused an abort so we do nothing
1217 * but remain in the abort state. Eventually we will get
1218 * to the "END TRANSACTION" which will set things straight.
1225 * Here we were in an aborted transaction block which
1226 * just processed the "END TRANSACTION" command from the
1227 * user, so now we return the to default state.
1230 case TBLOCK_ENDABORT:
1231 s->blockState = TBLOCK_DEFAULT;
1236 /* --------------------------------
1237 * AbortCurrentTransaction
1238 * --------------------------------
1241 AbortCurrentTransaction()
1243 TransactionState s = CurrentTransactionState;
1245 switch (s->blockState)
1248 * if we aren't in a transaction block, we
1249 * just do our usual abort transaction.
1252 case TBLOCK_DEFAULT:
1257 * If we are in the TBLOCK_BEGIN it means something
1258 * screwed up right after reading "BEGIN TRANSACTION"
1259 * so we enter the abort state. Eventually an "END
1260 * TRANSACTION" will fix things.
1264 s->blockState = TBLOCK_ABORT;
1269 * This is the case when are somewhere in a transaction
1270 * block which aborted so we abort the transaction and
1271 * set the ABORT state. Eventually an "END TRANSACTION"
1272 * will fix things and restore us to a normal state.
1275 case TBLOCK_INPROGRESS:
1276 s->blockState = TBLOCK_ABORT;
1281 * Here, the system was fouled up just after the
1282 * user wanted to end the transaction block so we
1283 * abort the transaction and put us back into the
1288 s->blockState = TBLOCK_DEFAULT;
1293 * Here, we are already in an aborted transaction
1294 * state and are waiting for an "END TRANSACTION" to
1295 * come along and lo and behold, we abort again!
1296 * So we just remain in the abort state.
1303 * Here we were in an aborted transaction block which
1304 * just processed the "END TRANSACTION" command but somehow
1305 * aborted again.. since we must have done the abort
1306 * processing, we return to the default state.
1309 case TBLOCK_ENDABORT:
1310 s->blockState = TBLOCK_DEFAULT;
1315 /* ----------------------------------------------------------------
1316 * transaction block support
1317 * ----------------------------------------------------------------
1319 /* --------------------------------
1320 * BeginTransactionBlock
1321 * --------------------------------
1324 BeginTransactionBlock(void)
1326 TransactionState s = CurrentTransactionState;
1329 * check the current transaction state
1332 if (s->state == TRANS_DISABLED)
1335 if (s->blockState != TBLOCK_DEFAULT)
1336 elog(NOTICE, "BEGIN: already a transaction in progress");
1339 * set the current transaction block state information
1340 * appropriately during begin processing
1343 s->blockState = TBLOCK_BEGIN;
1346 * do begin processing
1351 * done with begin processing, set block state to inprogress
1354 s->blockState = TBLOCK_INPROGRESS;
1357 /* --------------------------------
1358 * EndTransactionBlock
1359 * --------------------------------
1362 EndTransactionBlock(void)
1364 TransactionState s = CurrentTransactionState;
1367 * check the current transaction state
1370 if (s->state == TRANS_DISABLED)
1373 if (s->blockState == TBLOCK_INPROGRESS)
1376 * here we are in a transaction block which should commit
1377 * when we get to the upcoming CommitTransactionCommand()
1378 * so we set the state to "END". CommitTransactionCommand()
1379 * will recognize this and commit the transaction and return
1380 * us to the default state
1383 s->blockState = TBLOCK_END;
1387 if (s->blockState == TBLOCK_ABORT)
1390 * here, we are in a transaction block which aborted
1391 * and since the AbortTransaction() was already done,
1392 * we do whatever is needed and change to the special
1393 * "END ABORT" state. The upcoming CommitTransactionCommand()
1394 * will recognise this and then put us back in the default
1398 s->blockState = TBLOCK_ENDABORT;
1403 * We should not get here, but if we do, we go to the ENDABORT
1404 * state after printing a warning. The upcoming call to
1405 * CommitTransactionCommand() will then put us back into the
1409 elog(NOTICE, "COMMIT: no transaction in progress");
1410 s->blockState = TBLOCK_ENDABORT;
1413 /* --------------------------------
1414 * AbortTransactionBlock
1415 * --------------------------------
1419 AbortTransactionBlock(void)
1421 TransactionState s = CurrentTransactionState;
1424 * check the current transaction state
1427 if (s->state == TRANS_DISABLED)
1430 if (s->blockState == TBLOCK_INPROGRESS)
1433 * here we were inside a transaction block something
1434 * screwed up inside the system so we enter the abort state,
1435 * do the abort processing and then return.
1436 * We remain in the abort state until we see the upcoming
1437 * END TRANSACTION command.
1440 s->blockState = TBLOCK_ABORT;
1443 * do abort processing and return
1451 * this case should not be possible, because it would mean
1452 * the user entered an "abort" from outside a transaction block.
1453 * So we print an error message, abort the transaction and
1454 * enter the "ENDABORT" state so we will end up in the default
1455 * state after the upcoming CommitTransactionCommand().
1458 elog(NOTICE, "AbortTransactionBlock and not in in-progress state");
1460 s->blockState = TBLOCK_ENDABORT;
1465 /* --------------------------------
1466 * UserAbortTransactionBlock
1467 * --------------------------------
1470 UserAbortTransactionBlock()
1472 TransactionState s = CurrentTransactionState;
1475 * check the current transaction state
1478 if (s->state == TRANS_DISABLED)
1482 * if the transaction has already been automatically aborted with an
1483 * error, and the user subsequently types 'abort', allow it. (the
1484 * behavior is the same as if they had typed 'end'.)
1486 if (s->blockState == TBLOCK_ABORT)
1488 s->blockState = TBLOCK_ENDABORT;
1492 if (s->blockState == TBLOCK_INPROGRESS)
1495 * here we were inside a transaction block and we
1496 * got an abort command from the user, so we move to
1497 * the abort state, do the abort processing and
1498 * then change to the ENDABORT state so we will end up
1499 * in the default state after the upcoming
1500 * CommitTransactionCommand().
1503 s->blockState = TBLOCK_ABORT;
1506 * do abort processing
1512 * change to the end abort state and return
1515 s->blockState = TBLOCK_ENDABORT;
1520 * this case should not be possible, because it would mean
1521 * the user entered a "rollback" from outside a transaction block.
1522 * So we print an error message, abort the transaction and
1523 * enter the "ENDABORT" state so we will end up in the default
1524 * state after the upcoming CommitTransactionCommand().
1527 elog(NOTICE, "ROLLBACK: no transaction in progress");
1529 s->blockState = TBLOCK_ENDABORT;
1532 /* --------------------------------
1533 * AbortOutOfAnyTransaction
1535 * This routine is provided for error recovery purposes. It aborts any
1536 * active transaction or transaction block, leaving the system in a known
1538 * --------------------------------
1541 AbortOutOfAnyTransaction()
1543 TransactionState s = CurrentTransactionState;
1546 * Get out of any low-level transaction
1548 if (s->state != TRANS_DEFAULT)
1552 * Now reset the high-level state
1554 s->blockState = TBLOCK_DEFAULT;
1558 IsTransactionBlock()
1560 TransactionState s = CurrentTransactionState;
1562 if (s->blockState == TBLOCK_INPROGRESS
1563 || s->blockState == TBLOCK_ABORT
1564 || s->blockState == TBLOCK_ENDABORT)