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.66 2000/06/08 22:36:54 momjian 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/trigger.h"
154 #include "libpq/be-fsstubs.h"
155 #include "storage/proc.h"
156 #include "storage/sinval.h"
157 #include "utils/temprel.h"
158 #include "utils/inval.h"
159 #include "utils/portal.h"
160 #include "utils/relcache.h"
162 extern bool SharedBufferChanged;
164 static void AbortTransaction(void);
165 static void AtAbort_Cache(void);
166 static void AtAbort_Locks(void);
167 static void AtAbort_Memory(void);
168 static void AtCommit_Cache(void);
169 static void AtCommit_LocalCache(void);
170 static void AtCommit_Locks(void);
171 static void AtCommit_Memory(void);
172 static void AtStart_Cache(void);
173 static void AtStart_Locks(void);
174 static void AtStart_Memory(void);
175 static void CommitTransaction(void);
176 static void RecordTransactionAbort(void);
177 static void RecordTransactionCommit(void);
178 static void StartTransaction(void);
181 * global variables holding the current transaction state.
183 * Note: when we are running several slave processes, the
184 * current transaction state data is copied into shared memory
185 * and the CurrentTransactionState pointer changed to
186 * point to the shared copy. All this occurrs in slaves.c
189 TransactionStateData CurrentTransactionStateData = {
190 0, /* transaction id */
191 FirstCommandId, /* command id */
192 0, /* scan 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() instead of
520 * 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 main
631 * memory, dirty shared buffers are not flushed plai 8/7/90
633 leak = BufferPoolCheckLeak();
636 * If no one shared buffer was changed by this transaction then we
637 * don't flush shared buffers and don't record commit status.
639 if (SharedBufferChanged)
643 ResetBufferPool(true);
646 * have the transaction access methods record the status of this
647 * transaction id in the pg_log relation.
649 TransactionIdCommit(xid);
652 * Now write the log info to the disk too.
654 leak = BufferPoolCheckLeak();
659 ResetBufferPool(true);
663 /* --------------------------------
665 * --------------------------------
671 * Make catalog changes visible to all backend.
674 RegisterInvalid(true);
677 /* --------------------------------
678 * AtCommit_LocalCache
679 * --------------------------------
682 AtCommit_LocalCache()
685 * Make catalog changes visible to me for the next command.
688 ImmediateLocalInvalidation(true);
691 /* --------------------------------
693 * --------------------------------
699 * XXX What if ProcReleaseLocks fails? (race condition?)
701 * Then you're up a creek! -mer 5/24/92
707 /* --------------------------------
709 * --------------------------------
717 * Release all heap memory in the blank portal.
720 portal = GetPortalByName(NULL);
721 PortalResetHeapMemory(portal);
724 * Now that we're "out" of a transaction, have the
725 * system allocate things in the top memory context instead
726 * of the blank portal memory context.
729 MemoryContextSwitchTo(TopMemoryContext);
732 /* ----------------------------------------------------------------
733 * AbortTransaction stuff
734 * ----------------------------------------------------------------
737 /* --------------------------------
738 * RecordTransactionAbort
739 * --------------------------------
742 RecordTransactionAbort()
747 * get the current transaction id
750 xid = GetCurrentTransactionId();
753 * Have the transaction access methods record the status of this
754 * transaction id in the pg_log relation. We skip it if no one shared
755 * buffer was changed by this transaction.
757 if (SharedBufferChanged && !TransactionIdDidCommit(xid))
758 TransactionIdAbort(xid);
761 * Tell bufmgr and smgr to release resources.
763 ResetBufferPool(false); /* false -> is abort */
766 /* --------------------------------
768 * --------------------------------
773 RelationCacheAbort();
774 RegisterInvalid(false);
777 /* --------------------------------
779 * --------------------------------
785 * XXX What if ProcReleaseLocks() fails? (race condition?)
787 * Then you're up a creek without a paddle! -mer
794 /* --------------------------------
796 * --------------------------------
804 * Release all heap memory in the blank portal.
807 portal = GetPortalByName(NULL);
808 PortalResetHeapMemory(portal);
811 * Now that we're "out" of a transaction, have the
812 * system allocate things in the top memory context instead
813 * of the blank portal memory context.
816 MemoryContextSwitchTo(TopMemoryContext);
819 /* ----------------------------------------------------------------
821 * ----------------------------------------------------------------
824 /* --------------------------------
827 * --------------------------------
832 TransactionState s = CurrentTransactionState;
835 XactIsoLevel = DefaultXactIsoLevel;
838 * Check the current transaction state. If the transaction system
839 * is switched off, or if we're already in a transaction, do nothing.
840 * We're already in a transaction when the monitor sends a null
841 * command to the backend to flush the comm channel. This is a
842 * hacky fix to a communications problem, and we keep having to
843 * deal with it here. We should fix the comm channel code. mao 080891
846 if (s->state == TRANS_DISABLED || s->state == TRANS_INPROGRESS)
850 * set the current transaction state information
851 * appropriately during start processing
854 s->state = TRANS_START;
856 SetReindexProcessing(false);
858 * generate a new transaction id
861 GetNewTransactionId(&(s->transactionIdData));
863 XactLockTableInsert(s->transactionIdData);
866 * initialize current transaction state fields
869 s->commandId = FirstCommandId;
870 s->scanCommandId = FirstCommandId;
871 s->startTime = GetCurrentAbsoluteTime();
874 * initialize the various transaction subsystems
882 initialize temporary relations list
883 the tempRelList is a list of temporary relations that
884 are created in the course of the transactions
885 they need to be destroyed properly at the end of the transactions
890 * Tell the trigger manager to we're starting a transaction
893 DeferredTriggerBeginXact();
896 * done with start processing, set current transaction
897 * state to "in progress"
900 s->state = TRANS_INPROGRESS;
906 * Tell me if we are currently in progress
910 CurrentXactInProgress()
912 return CurrentTransactionState->state == TRANS_INPROGRESS;
916 /* --------------------------------
919 * --------------------------------
924 TransactionState s = CurrentTransactionState;
927 * check the current transaction state
930 if (s->state == TRANS_DISABLED)
933 if (s->state != TRANS_INPROGRESS)
934 elog(NOTICE, "CommitTransaction and not in in-progress state ");
937 * Tell the trigger manager that this transaction is about to be
938 * committed. He'll invoke all trigger deferred until XACT before
939 * we really start on committing the transaction.
942 DeferredTriggerEndXact();
945 * set the current transaction state information
946 * appropriately during the abort processing
949 s->state = TRANS_COMMIT;
952 * do commit processing
956 /* handle commit for large objects [ PA, 7/17/98 ] */
959 /* NOTIFY commit must also come before lower-level cleanup */
965 RecordTransactionCommit();
968 * Let others know about no transaction in progress by me. Note that
969 * this must be done _before_ releasing locks we hold and
970 * SpinAcquire(SInvalLock) is required: UPDATE with xid 0 is blocked
971 * by xid 1' UPDATE, xid 1 is doing commit while xid 2 gets snapshot -
972 * if xid 2' GetSnapshotData sees xid 1 as running then it must see
973 * xid 0 as running as well or it will see two tuple versions - one
974 * deleted by xid 1 and one inserted by xid 0.
976 if (MyProc != (PROC *) NULL)
978 /* Lock SInvalLock because that's what GetSnapshotData uses. */
979 SpinAcquire(SInvalLock);
980 MyProc->xid = InvalidTransactionId;
981 MyProc->xmin = InvalidTransactionId;
982 SpinRelease(SInvalLock);
985 RelationPurgeLocalRelation(true);
993 * done with commit processing, set current transaction
994 * state back to default
997 s->state = TRANS_DEFAULT;
998 SharedBufferChanged = false;/* safest place to do it */
1002 /* --------------------------------
1005 * --------------------------------
1010 TransactionState s = CurrentTransactionState;
1013 * Let others to know about no transaction in progress - vadim
1016 if (MyProc != (PROC *) NULL)
1018 MyProc->xid = InvalidTransactionId;
1019 MyProc->xmin = InvalidTransactionId;
1023 * check the current transaction state
1026 if (s->state == TRANS_DISABLED)
1029 if (s->state != TRANS_INPROGRESS)
1030 elog(NOTICE, "AbortTransaction and not in in-progress state ");
1033 * Tell the trigger manager that this transaction is about to be
1037 DeferredTriggerAbortXact();
1040 * set the current transaction state information
1041 * appropriately during the abort processing
1044 s->state = TRANS_ABORT;
1047 * do abort processing
1050 lo_commit(false); /* 'false' means it's abort */
1055 if (CommonSpecialPortalIsOpen())
1056 CommonSpecialPortalClose();
1057 RecordTransactionAbort();
1058 RelationPurgeLocalRelation(false);
1060 invalidate_temp_relations();
1068 * done with abort processing, set current transaction
1069 * state back to default
1072 s->state = TRANS_DEFAULT;
1073 SharedBufferChanged = false;/* safest place to do it */
1076 /* --------------------------------
1077 * StartTransactionCommand
1078 * --------------------------------
1081 StartTransactionCommand()
1083 TransactionState s = CurrentTransactionState;
1085 switch (s->blockState)
1088 * if we aren't in a transaction block, we
1089 * just do our usual start transaction.
1092 case TBLOCK_DEFAULT:
1097 * We should never experience this -- if we do it
1098 * means the BEGIN state was not changed in the previous
1099 * CommitTransactionCommand(). If we get it, we print
1100 * a warning and change to the in-progress state.
1104 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_BEGIN");
1105 s->blockState = TBLOCK_INPROGRESS;
1109 * This is the case when are somewhere in a transaction
1110 * block and about to start a new command. For now we
1111 * do nothing but someday we may do command-local resource
1115 case TBLOCK_INPROGRESS:
1119 * As with BEGIN, we should never experience this
1120 * if we do it means the END state was not changed in the
1121 * previous CommitTransactionCommand(). If we get it, we
1122 * print a warning, commit the transaction, start a new
1123 * transaction and change to the default state.
1127 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_END");
1128 s->blockState = TBLOCK_DEFAULT;
1129 CommitTransaction();
1134 * Here we are in the middle of a transaction block but
1135 * one of the commands caused an abort so we do nothing
1136 * but remain in the abort state. Eventually we will get
1137 * to the "END TRANSACTION" which will set things straight.
1144 * This means we somehow aborted and the last call to
1145 * CommitTransactionCommand() didn't clear the state so
1146 * we remain in the ENDABORT state and mabey next time
1147 * we get to CommitTransactionCommand() the state will
1148 * get reset to default.
1151 case TBLOCK_ENDABORT:
1152 elog(NOTICE, "StartTransactionCommand: unexpected TBLOCK_ENDABORT");
1157 /* --------------------------------
1158 * CommitTransactionCommand
1159 * --------------------------------
1162 CommitTransactionCommand()
1164 TransactionState s = CurrentTransactionState;
1166 switch (s->blockState)
1169 * if we aren't in a transaction block, we
1170 * just do our usual transaction commit
1173 case TBLOCK_DEFAULT:
1174 CommitTransaction();
1178 * This is the case right after we get a "BEGIN TRANSACTION"
1179 * command, but the user hasn't done anything else yet, so
1180 * we change to the "transaction block in progress" state
1185 s->blockState = TBLOCK_INPROGRESS;
1189 * This is the case when we have finished executing a command
1190 * someplace within a transaction block. We increment the
1191 * command counter and return. Someday we may free resources
1192 * local to the command.
1194 * That someday is today, at least for memory allocated by
1195 * command in the BlankPortal' HeapMemory context.
1199 case TBLOCK_INPROGRESS:
1200 CommandCounterIncrement();
1201 #ifdef TBL_FREE_CMD_MEMORY
1202 EndPortalAllocMode();
1203 StartPortalAllocMode(DefaultAllocMode, 0);
1208 * This is the case when we just got the "END TRANSACTION"
1209 * statement, so we go back to the default state and
1210 * commit the transaction.
1214 s->blockState = TBLOCK_DEFAULT;
1215 CommitTransaction();
1219 * Here we are in the middle of a transaction block but
1220 * one of the commands caused an abort so we do nothing
1221 * but remain in the abort state. Eventually we will get
1222 * to the "END TRANSACTION" which will set things straight.
1229 * Here we were in an aborted transaction block which
1230 * just processed the "END TRANSACTION" command from the
1231 * user, so now we return the to default state.
1234 case TBLOCK_ENDABORT:
1235 s->blockState = TBLOCK_DEFAULT;
1240 /* --------------------------------
1241 * AbortCurrentTransaction
1242 * --------------------------------
1245 AbortCurrentTransaction()
1247 TransactionState s = CurrentTransactionState;
1249 switch (s->blockState)
1252 * if we aren't in a transaction block, we
1253 * just do our usual abort transaction.
1256 case TBLOCK_DEFAULT:
1261 * If we are in the TBLOCK_BEGIN it means something
1262 * screwed up right after reading "BEGIN TRANSACTION"
1263 * so we enter the abort state. Eventually an "END
1264 * TRANSACTION" will fix things.
1268 s->blockState = TBLOCK_ABORT;
1273 * This is the case when are somewhere in a transaction
1274 * block which aborted so we abort the transaction and
1275 * set the ABORT state. Eventually an "END TRANSACTION"
1276 * will fix things and restore us to a normal state.
1279 case TBLOCK_INPROGRESS:
1280 s->blockState = TBLOCK_ABORT;
1285 * Here, the system was fouled up just after the
1286 * user wanted to end the transaction block so we
1287 * abort the transaction and put us back into the
1292 s->blockState = TBLOCK_DEFAULT;
1297 * Here, we are already in an aborted transaction
1298 * state and are waiting for an "END TRANSACTION" to
1299 * come along and lo and behold, we abort again!
1300 * So we just remain in the abort state.
1307 * Here we were in an aborted transaction block which
1308 * just processed the "END TRANSACTION" command but somehow
1309 * aborted again.. since we must have done the abort
1310 * processing, we return to the default state.
1313 case TBLOCK_ENDABORT:
1314 s->blockState = TBLOCK_DEFAULT;
1319 /* ----------------------------------------------------------------
1320 * transaction block support
1321 * ----------------------------------------------------------------
1323 /* --------------------------------
1324 * BeginTransactionBlock
1325 * --------------------------------
1328 BeginTransactionBlock(void)
1330 TransactionState s = CurrentTransactionState;
1333 * check the current transaction state
1336 if (s->state == TRANS_DISABLED)
1339 if (s->blockState != TBLOCK_DEFAULT)
1340 elog(NOTICE, "BEGIN: already a transaction in progress");
1343 * set the current transaction block state information
1344 * appropriately during begin processing
1347 s->blockState = TBLOCK_BEGIN;
1350 * do begin processing
1355 * done with begin processing, set block state to inprogress
1358 s->blockState = TBLOCK_INPROGRESS;
1361 /* --------------------------------
1362 * EndTransactionBlock
1363 * --------------------------------
1366 EndTransactionBlock(void)
1368 TransactionState s = CurrentTransactionState;
1371 * check the current transaction state
1374 if (s->state == TRANS_DISABLED)
1377 if (s->blockState == TBLOCK_INPROGRESS)
1380 * here we are in a transaction block which should commit
1381 * when we get to the upcoming CommitTransactionCommand()
1382 * so we set the state to "END". CommitTransactionCommand()
1383 * will recognize this and commit the transaction and return
1384 * us to the default state
1387 s->blockState = TBLOCK_END;
1391 if (s->blockState == TBLOCK_ABORT)
1394 * here, we are in a transaction block which aborted
1395 * and since the AbortTransaction() was already done,
1396 * we do whatever is needed and change to the special
1397 * "END ABORT" state. The upcoming CommitTransactionCommand()
1398 * will recognise this and then put us back in the default
1402 s->blockState = TBLOCK_ENDABORT;
1407 * We should not get here, but if we do, we go to the ENDABORT
1408 * state after printing a warning. The upcoming call to
1409 * CommitTransactionCommand() will then put us back into the
1413 elog(NOTICE, "COMMIT: no transaction in progress");
1414 s->blockState = TBLOCK_ENDABORT;
1417 /* --------------------------------
1418 * AbortTransactionBlock
1419 * --------------------------------
1423 AbortTransactionBlock(void)
1425 TransactionState s = CurrentTransactionState;
1428 * check the current transaction state
1431 if (s->state == TRANS_DISABLED)
1434 if (s->blockState == TBLOCK_INPROGRESS)
1437 * here we were inside a transaction block something
1438 * screwed up inside the system so we enter the abort state,
1439 * do the abort processing and then return.
1440 * We remain in the abort state until we see the upcoming
1441 * END TRANSACTION command.
1444 s->blockState = TBLOCK_ABORT;
1447 * do abort processing and return
1455 * this case should not be possible, because it would mean
1456 * the user entered an "abort" from outside a transaction block.
1457 * So we print an error message, abort the transaction and
1458 * enter the "ENDABORT" state so we will end up in the default
1459 * state after the upcoming CommitTransactionCommand().
1462 elog(NOTICE, "AbortTransactionBlock and not in in-progress state");
1464 s->blockState = TBLOCK_ENDABORT;
1469 /* --------------------------------
1470 * UserAbortTransactionBlock
1471 * --------------------------------
1474 UserAbortTransactionBlock()
1476 TransactionState s = CurrentTransactionState;
1479 * check the current transaction state
1482 if (s->state == TRANS_DISABLED)
1486 * if the transaction has already been automatically aborted with an
1487 * error, and the user subsequently types 'abort', allow it. (the
1488 * behavior is the same as if they had typed 'end'.)
1490 if (s->blockState == TBLOCK_ABORT)
1492 s->blockState = TBLOCK_ENDABORT;
1496 if (s->blockState == TBLOCK_INPROGRESS)
1499 * here we were inside a transaction block and we
1500 * got an abort command from the user, so we move to
1501 * the abort state, do the abort processing and
1502 * then change to the ENDABORT state so we will end up
1503 * in the default state after the upcoming
1504 * CommitTransactionCommand().
1507 s->blockState = TBLOCK_ABORT;
1510 * do abort processing
1516 * change to the end abort state and return
1519 s->blockState = TBLOCK_ENDABORT;
1524 * this case should not be possible, because it would mean
1525 * the user entered a "rollback" from outside a transaction block.
1526 * So we print an error message, abort the transaction and
1527 * enter the "ENDABORT" state so we will end up in the default
1528 * state after the upcoming CommitTransactionCommand().
1531 elog(NOTICE, "ROLLBACK: no transaction in progress");
1533 s->blockState = TBLOCK_ENDABORT;
1536 /* --------------------------------
1537 * AbortOutOfAnyTransaction
1539 * This routine is provided for error recovery purposes. It aborts any
1540 * active transaction or transaction block, leaving the system in a known
1542 * --------------------------------
1545 AbortOutOfAnyTransaction()
1547 TransactionState s = CurrentTransactionState;
1550 * Get out of any low-level transaction
1552 if (s->state != TRANS_DEFAULT)
1556 * Now reset the high-level state
1558 s->blockState = TBLOCK_DEFAULT;
1562 IsTransactionBlock()
1564 TransactionState s = CurrentTransactionState;
1566 if (s->blockState == TBLOCK_INPROGRESS
1567 || s->blockState == TBLOCK_ABORT
1568 || s->blockState == TBLOCK_ENDABORT)