]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xact.c
Teach standby conflict resolution to use SIGUSR1
[postgresql] / src / backend / access / transam / xact.c
1 /*-------------------------------------------------------------------------
2  *
3  * xact.c
4  *        top level transaction system support routines
5  *
6  * See src/backend/access/transam/README for more information.
7  *
8  * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *        $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.281 2010/01/16 10:05:50 sriggs Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17
18 #include "postgres.h"
19
20 #include <time.h>
21 #include <unistd.h>
22
23 #include "access/multixact.h"
24 #include "access/subtrans.h"
25 #include "access/transam.h"
26 #include "access/twophase.h"
27 #include "access/xact.h"
28 #include "access/xlogutils.h"
29 #include "catalog/catalog.h"
30 #include "catalog/namespace.h"
31 #include "catalog/storage.h"
32 #include "commands/async.h"
33 #include "commands/tablecmds.h"
34 #include "commands/trigger.h"
35 #include "executor/spi.h"
36 #include "libpq/be-fsstubs.h"
37 #include "miscadmin.h"
38 #include "pgstat.h"
39 #include "storage/bufmgr.h"
40 #include "storage/fd.h"
41 #include "storage/lmgr.h"
42 #include "storage/procarray.h"
43 #include "storage/sinvaladt.h"
44 #include "storage/smgr.h"
45 #include "storage/standby.h"
46 #include "utils/combocid.h"
47 #include "utils/guc.h"
48 #include "utils/inval.h"
49 #include "utils/memutils.h"
50 #include "utils/relcache.h"
51 #include "utils/snapmgr.h"
52 #include "pg_trace.h"
53
54
55 /*
56  *      User-tweakable parameters
57  */
58 int                     DefaultXactIsoLevel = XACT_READ_COMMITTED;
59 int                     XactIsoLevel;
60
61 bool            DefaultXactReadOnly = false;
62 bool            XactReadOnly;
63
64 bool            XactSyncCommit = true;
65
66 int                     CommitDelay = 0;        /* precommit delay in microseconds */
67 int                     CommitSiblings = 5; /* # concurrent xacts needed to sleep */
68
69 /*
70  * MyXactAccessedTempRel is set when a temporary relation is accessed.
71  * We don't allow PREPARE TRANSACTION in that case.  (This is global
72  * so that it can be set from heapam.c.)
73  */
74 bool            MyXactAccessedTempRel = false;
75
76
77 /*
78  *      transaction states - transaction state from server perspective
79  */
80 typedef enum TransState
81 {
82         TRANS_DEFAULT,                          /* idle */
83         TRANS_START,                            /* transaction starting */
84         TRANS_INPROGRESS,                       /* inside a valid transaction */
85         TRANS_COMMIT,                           /* commit in progress */
86         TRANS_ABORT,                            /* abort in progress */
87         TRANS_PREPARE                           /* prepare in progress */
88 } TransState;
89
90 /*
91  *      transaction block states - transaction state of client queries
92  *
93  * Note: the subtransaction states are used only for non-topmost
94  * transactions; the others appear only in the topmost transaction.
95  */
96 typedef enum TBlockState
97 {
98         /* not-in-transaction-block states */
99         TBLOCK_DEFAULT,                         /* idle */
100         TBLOCK_STARTED,                         /* running single-query transaction */
101
102         /* transaction block states */
103         TBLOCK_BEGIN,                           /* starting transaction block */
104         TBLOCK_INPROGRESS,                      /* live transaction */
105         TBLOCK_END,                                     /* COMMIT received */
106         TBLOCK_ABORT,                           /* failed xact, awaiting ROLLBACK */
107         TBLOCK_ABORT_END,                       /* failed xact, ROLLBACK received */
108         TBLOCK_ABORT_PENDING,           /* live xact, ROLLBACK received */
109         TBLOCK_PREPARE,                         /* live xact, PREPARE received */
110
111         /* subtransaction states */
112         TBLOCK_SUBBEGIN,                        /* starting a subtransaction */
113         TBLOCK_SUBINPROGRESS,           /* live subtransaction */
114         TBLOCK_SUBEND,                          /* RELEASE received */
115         TBLOCK_SUBABORT,                        /* failed subxact, awaiting ROLLBACK */
116         TBLOCK_SUBABORT_END,            /* failed subxact, ROLLBACK received */
117         TBLOCK_SUBABORT_PENDING,        /* live subxact, ROLLBACK received */
118         TBLOCK_SUBRESTART,                      /* live subxact, ROLLBACK TO received */
119         TBLOCK_SUBABORT_RESTART         /* failed subxact, ROLLBACK TO received */
120 } TBlockState;
121
122 /*
123  *      transaction state structure
124  */
125 typedef struct TransactionStateData
126 {
127         TransactionId transactionId;    /* my XID, or Invalid if none */
128         SubTransactionId subTransactionId;      /* my subxact ID */
129         char       *name;                       /* savepoint name, if any */
130         int                     savepointLevel; /* savepoint level */
131         TransState      state;                  /* low-level state */
132         TBlockState blockState;         /* high-level state */
133         int                     nestingLevel;   /* transaction nesting depth */
134         int                     gucNestLevel;   /* GUC context nesting depth */
135         MemoryContext curTransactionContext;            /* my xact-lifetime context */
136         ResourceOwner curTransactionOwner;      /* my query resources */
137         TransactionId *childXids;       /* subcommitted child XIDs, in XID order */
138         int                     nChildXids;             /* # of subcommitted child XIDs */
139         int                     maxChildXids;   /* allocated size of childXids[] */
140         Oid                     prevUser;               /* previous CurrentUserId setting */
141         int                     prevSecContext; /* previous SecurityRestrictionContext */
142         bool            prevXactReadOnly;               /* entry-time xact r/o state */
143         bool            startedInRecovery;      /* did we start in recovery? */
144         struct TransactionStateData *parent;            /* back link to parent */
145 } TransactionStateData;
146
147 typedef TransactionStateData *TransactionState;
148
149 /*
150  * CurrentTransactionState always points to the current transaction state
151  * block.  It will point to TopTransactionStateData when not in a
152  * transaction at all, or when in a top-level transaction.
153  */
154 static TransactionStateData TopTransactionStateData = {
155         0,                                                      /* transaction id */
156         0,                                                      /* subtransaction id */
157         NULL,                                           /* savepoint name */
158         0,                                                      /* savepoint level */
159         TRANS_DEFAULT,                          /* transaction state */
160         TBLOCK_DEFAULT,                         /* transaction block state from the client
161                                                                  * perspective */
162         0,                                                      /* transaction nesting depth */
163         0,                                                      /* GUC context nesting depth */
164         NULL,                                           /* cur transaction context */
165         NULL,                                           /* cur transaction resource owner */
166         NULL,                                           /* subcommitted child Xids */
167         0,                                                      /* # of subcommitted child Xids */
168         0,                                                      /* allocated size of childXids[] */
169         InvalidOid,                                     /* previous CurrentUserId setting */
170         0,                                                      /* previous SecurityRestrictionContext */
171         false,                                          /* entry-time xact r/o state */
172         false,                                          /* startedInRecovery */
173         NULL                                            /* link to parent state block */
174 };
175
176 /*
177  * unreportedXids holds XIDs of all subtransactions that have not yet been
178  * reported in a XLOG_XACT_ASSIGNMENT record.
179  */
180 static int nUnreportedXids;
181 static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS];
182
183 static TransactionState CurrentTransactionState = &TopTransactionStateData;
184
185 /*
186  * The subtransaction ID and command ID assignment counters are global
187  * to a whole transaction, so we do not keep them in the state stack.
188  */
189 static SubTransactionId currentSubTransactionId;
190 static CommandId currentCommandId;
191 static bool currentCommandIdUsed;
192
193 /*
194  * xactStartTimestamp is the value of transaction_timestamp().
195  * stmtStartTimestamp is the value of statement_timestamp().
196  * xactStopTimestamp is the time at which we log a commit or abort WAL record.
197  * These do not change as we enter and exit subtransactions, so we don't
198  * keep them inside the TransactionState stack.
199  */
200 static TimestampTz xactStartTimestamp;
201 static TimestampTz stmtStartTimestamp;
202 static TimestampTz xactStopTimestamp;
203
204 /*
205  * GID to be used for preparing the current transaction.  This is also
206  * global to a whole transaction, so we don't keep it in the state stack.
207  */
208 static char *prepareGID;
209
210 /*
211  * Some commands want to force synchronous commit.
212  */
213 static bool forceSyncCommit = false;
214
215 /*
216  * Private context for transaction-abort work --- we reserve space for this
217  * at startup to ensure that AbortTransaction and AbortSubTransaction can work
218  * when we've run out of memory.
219  */
220 static MemoryContext TransactionAbortContext = NULL;
221
222 /*
223  * List of add-on start- and end-of-xact callbacks
224  */
225 typedef struct XactCallbackItem
226 {
227         struct XactCallbackItem *next;
228         XactCallback callback;
229         void       *arg;
230 } XactCallbackItem;
231
232 static XactCallbackItem *Xact_callbacks = NULL;
233
234 /*
235  * List of add-on start- and end-of-subxact callbacks
236  */
237 typedef struct SubXactCallbackItem
238 {
239         struct SubXactCallbackItem *next;
240         SubXactCallback callback;
241         void       *arg;
242 } SubXactCallbackItem;
243
244 static SubXactCallbackItem *SubXact_callbacks = NULL;
245
246
247 /* local function prototypes */
248 static void AssignTransactionId(TransactionState s);
249 static void AbortTransaction(void);
250 static void AtAbort_Memory(void);
251 static void AtCleanup_Memory(void);
252 static void AtAbort_ResourceOwner(void);
253 static void AtCommit_LocalCache(void);
254 static void AtCommit_Memory(void);
255 static void AtStart_Cache(void);
256 static void AtStart_Memory(void);
257 static void AtStart_ResourceOwner(void);
258 static void CallXactCallbacks(XactEvent event);
259 static void CallSubXactCallbacks(SubXactEvent event,
260                                          SubTransactionId mySubid,
261                                          SubTransactionId parentSubid);
262 static void CleanupTransaction(void);
263 static void CommitTransaction(void);
264 static TransactionId RecordTransactionAbort(bool isSubXact);
265 static void StartTransaction(void);
266
267 static void StartSubTransaction(void);
268 static void CommitSubTransaction(void);
269 static void AbortSubTransaction(void);
270 static void CleanupSubTransaction(void);
271 static void PushTransaction(void);
272 static void PopTransaction(void);
273
274 static void AtSubAbort_Memory(void);
275 static void AtSubCleanup_Memory(void);
276 static void AtSubAbort_ResourceOwner(void);
277 static void AtSubCommit_Memory(void);
278 static void AtSubStart_Memory(void);
279 static void AtSubStart_ResourceOwner(void);
280
281 static void ShowTransactionState(const char *str);
282 static void ShowTransactionStateRec(TransactionState state);
283 static const char *BlockStateAsString(TBlockState blockState);
284 static const char *TransStateAsString(TransState state);
285
286
287 /* ----------------------------------------------------------------
288  *      transaction state accessors
289  * ----------------------------------------------------------------
290  */
291
292 /*
293  *      IsTransactionState
294  *
295  *      This returns true if we are inside a valid transaction; that is,
296  *      it is safe to initiate database access, take heavyweight locks, etc.
297  */
298 bool
299 IsTransactionState(void)
300 {
301         TransactionState s = CurrentTransactionState;
302
303         /*
304          * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states.  However, we
305          * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
306          * TRANS_PREPARE since it might be too soon or too late within those
307          * transition states to do anything interesting.  Hence, the only "valid"
308          * state is TRANS_INPROGRESS.
309          */
310         return (s->state == TRANS_INPROGRESS);
311 }
312
313 /*
314  *      IsAbortedTransactionBlockState
315  *
316  *      This returns true if we are within an aborted transaction block.
317  */
318 bool
319 IsAbortedTransactionBlockState(void)
320 {
321         TransactionState s = CurrentTransactionState;
322
323         if (s->blockState == TBLOCK_ABORT ||
324                 s->blockState == TBLOCK_SUBABORT)
325                 return true;
326
327         return false;
328 }
329
330
331 /*
332  *      GetTopTransactionId
333  *
334  * This will return the XID of the main transaction, assigning one if
335  * it's not yet set.  Be careful to call this only inside a valid xact.
336  */
337 TransactionId
338 GetTopTransactionId(void)
339 {
340         if (!TransactionIdIsValid(TopTransactionStateData.transactionId))
341                 AssignTransactionId(&TopTransactionStateData);
342         return TopTransactionStateData.transactionId;
343 }
344
345 /*
346  *      GetTopTransactionIdIfAny
347  *
348  * This will return the XID of the main transaction, if one is assigned.
349  * It will return InvalidTransactionId if we are not currently inside a
350  * transaction, or inside a transaction that hasn't yet been assigned an XID.
351  */
352 TransactionId
353 GetTopTransactionIdIfAny(void)
354 {
355         return TopTransactionStateData.transactionId;
356 }
357
358 /*
359  *      GetCurrentTransactionId
360  *
361  * This will return the XID of the current transaction (main or sub
362  * transaction), assigning one if it's not yet set.  Be careful to call this
363  * only inside a valid xact.
364  */
365 TransactionId
366 GetCurrentTransactionId(void)
367 {
368         TransactionState s = CurrentTransactionState;
369
370         if (!TransactionIdIsValid(s->transactionId))
371                 AssignTransactionId(s);
372         return s->transactionId;
373 }
374
375 /*
376  *      GetCurrentTransactionIdIfAny
377  *
378  * This will return the XID of the current sub xact, if one is assigned.
379  * It will return InvalidTransactionId if we are not currently inside a
380  * transaction, or inside a transaction that hasn't been assigned an XID yet.
381  */
382 TransactionId
383 GetCurrentTransactionIdIfAny(void)
384 {
385         return CurrentTransactionState->transactionId;
386 }
387
388
389 /*
390  * AssignTransactionId
391  *
392  * Assigns a new permanent XID to the given TransactionState.
393  * We do not assign XIDs to transactions until/unless this is called.
394  * Also, any parent TransactionStates that don't yet have XIDs are assigned
395  * one; this maintains the invariant that a child transaction has an XID
396  * following its parent's.
397  */
398 static void
399 AssignTransactionId(TransactionState s)
400 {
401         bool            isSubXact = (s->parent != NULL);
402         ResourceOwner currentOwner;
403
404         if (RecoveryInProgress())
405                 elog(ERROR, "cannot assign TransactionIds during recovery");
406
407         /* Assert that caller didn't screw up */
408         Assert(!TransactionIdIsValid(s->transactionId));
409         Assert(s->state == TRANS_INPROGRESS);
410
411         /*
412          * Ensure parent(s) have XIDs, so that a child always has an XID later
413          * than its parent.
414          */
415         if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
416                 AssignTransactionId(s->parent);
417
418         /*
419          * Generate a new Xid and record it in PG_PROC and pg_subtrans.
420          *
421          * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
422          * shared storage other than PG_PROC; because if there's no room for it in
423          * PG_PROC, the subtrans entry is needed to ensure that other backends see
424          * the Xid as "running".  See GetNewTransactionId.
425          */
426         s->transactionId = GetNewTransactionId(isSubXact);
427
428         if (isSubXact)
429                 SubTransSetParent(s->transactionId, s->parent->transactionId, false);
430
431         /*
432          * Acquire lock on the transaction XID.  (We assume this cannot block.) We
433          * have to ensure that the lock is assigned to the transaction's own
434          * ResourceOwner.
435          */
436         currentOwner = CurrentResourceOwner;
437         PG_TRY();
438         {
439                 CurrentResourceOwner = s->curTransactionOwner;
440                 XactLockTableInsert(s->transactionId);
441         }
442         PG_CATCH();
443         {
444                 /* Ensure CurrentResourceOwner is restored on error */
445                 CurrentResourceOwner = currentOwner;
446                 PG_RE_THROW();
447         }
448         PG_END_TRY();
449         CurrentResourceOwner = currentOwner;
450
451         /*
452          * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
453          * top-level transaction we issue a WAL record for the assignment. We
454          * include the top-level xid and all the subxids that have not yet been
455          * reported using XLOG_XACT_ASSIGNMENT records.
456          *
457          * This is required to limit the amount of shared memory required in a
458          * hot standby server to keep track of in-progress XIDs. See notes for
459          * RecordKnownAssignedTransactionIds().
460          *
461          * We don't keep track of the immediate parent of each subxid,
462          * only the top-level transaction that each subxact belongs to. This
463          * is correct in recovery only because aborted subtransactions are
464          * separately WAL logged.
465          */
466         if (isSubXact && XLogStandbyInfoActive())
467         {
468                 unreportedXids[nUnreportedXids] = s->transactionId;
469                 nUnreportedXids++;
470
471                 /* ensure this test matches similar one in RecoverPreparedTransactions() */
472                 if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS)
473                 {
474                         XLogRecData rdata[2];
475                         xl_xact_assignment      xlrec;
476
477                         /*
478                          * xtop is always set by now because we recurse up transaction
479                          * stack to the highest unassigned xid and then come back down
480                          */
481                         xlrec.xtop = GetTopTransactionId();
482                         Assert(TransactionIdIsValid(xlrec.xtop));
483                         xlrec.nsubxacts = nUnreportedXids;
484
485                         rdata[0].data = (char *) &xlrec;
486                         rdata[0].len = MinSizeOfXactAssignment;
487                         rdata[0].buffer = InvalidBuffer;
488                         rdata[0].next = &rdata[1];
489
490                         rdata[1].data = (char *) unreportedXids;
491                         rdata[1].len = PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId);
492                         rdata[1].buffer = InvalidBuffer;
493                         rdata[1].next = NULL;
494
495                         (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT, rdata);
496
497                         nUnreportedXids = 0;
498                 }
499         }
500 }
501
502 /*
503  *      GetCurrentSubTransactionId
504  */
505 SubTransactionId
506 GetCurrentSubTransactionId(void)
507 {
508         TransactionState s = CurrentTransactionState;
509
510         return s->subTransactionId;
511 }
512
513
514 /*
515  *      GetCurrentCommandId
516  *
517  * "used" must be TRUE if the caller intends to use the command ID to mark
518  * inserted/updated/deleted tuples.  FALSE means the ID is being fetched
519  * for read-only purposes (ie, as a snapshot validity cutoff).  See
520  * CommandCounterIncrement() for discussion.
521  */
522 CommandId
523 GetCurrentCommandId(bool used)
524 {
525         /* this is global to a transaction, not subtransaction-local */
526         if (used)
527                 currentCommandIdUsed = true;
528         return currentCommandId;
529 }
530
531 /*
532  *      GetCurrentTransactionStartTimestamp
533  */
534 TimestampTz
535 GetCurrentTransactionStartTimestamp(void)
536 {
537         return xactStartTimestamp;
538 }
539
540 /*
541  *      GetCurrentStatementStartTimestamp
542  */
543 TimestampTz
544 GetCurrentStatementStartTimestamp(void)
545 {
546         return stmtStartTimestamp;
547 }
548
549 /*
550  *      GetCurrentTransactionStopTimestamp
551  *
552  * We return current time if the transaction stop time hasn't been set
553  * (which can happen if we decide we don't need to log an XLOG record).
554  */
555 TimestampTz
556 GetCurrentTransactionStopTimestamp(void)
557 {
558         if (xactStopTimestamp != 0)
559                 return xactStopTimestamp;
560         return GetCurrentTimestamp();
561 }
562
563 /*
564  *      SetCurrentStatementStartTimestamp
565  */
566 void
567 SetCurrentStatementStartTimestamp(void)
568 {
569         stmtStartTimestamp = GetCurrentTimestamp();
570 }
571
572 /*
573  *      SetCurrentTransactionStopTimestamp
574  */
575 static inline void
576 SetCurrentTransactionStopTimestamp(void)
577 {
578         xactStopTimestamp = GetCurrentTimestamp();
579 }
580
581 /*
582  *      GetCurrentTransactionNestLevel
583  *
584  * Note: this will return zero when not inside any transaction, one when
585  * inside a top-level transaction, etc.
586  */
587 int
588 GetCurrentTransactionNestLevel(void)
589 {
590         TransactionState s = CurrentTransactionState;
591
592         return s->nestingLevel;
593 }
594
595
596 /*
597  *      TransactionIdIsCurrentTransactionId
598  */
599 bool
600 TransactionIdIsCurrentTransactionId(TransactionId xid)
601 {
602         TransactionState s;
603
604         /*
605          * We always say that BootstrapTransactionId is "not my transaction ID"
606          * even when it is (ie, during bootstrap).      Along with the fact that
607          * transam.c always treats BootstrapTransactionId as already committed,
608          * this causes the tqual.c routines to see all tuples as committed, which
609          * is what we need during bootstrap.  (Bootstrap mode only inserts tuples,
610          * it never updates or deletes them, so all tuples can be presumed good
611          * immediately.)
612          *
613          * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
614          * not my transaction ID, so we can just return "false" immediately for
615          * any non-normal XID.
616          */
617         if (!TransactionIdIsNormal(xid))
618                 return false;
619
620         /*
621          * We will return true for the Xid of the current subtransaction, any of
622          * its subcommitted children, any of its parents, or any of their
623          * previously subcommitted children.  However, a transaction being aborted
624          * is no longer "current", even though it may still have an entry on the
625          * state stack.
626          */
627         for (s = CurrentTransactionState; s != NULL; s = s->parent)
628         {
629                 int                     low,
630                                         high;
631
632                 if (s->state == TRANS_ABORT)
633                         continue;
634                 if (!TransactionIdIsValid(s->transactionId))
635                         continue;                       /* it can't have any child XIDs either */
636                 if (TransactionIdEquals(xid, s->transactionId))
637                         return true;
638                 /* As the childXids array is ordered, we can use binary search */
639                 low = 0;
640                 high = s->nChildXids - 1;
641                 while (low <= high)
642                 {
643                         int                     middle;
644                         TransactionId probe;
645
646                         middle = low + (high - low) / 2;
647                         probe = s->childXids[middle];
648                         if (TransactionIdEquals(probe, xid))
649                                 return true;
650                         else if (TransactionIdPrecedes(probe, xid))
651                                 low = middle + 1;
652                         else
653                                 high = middle - 1;
654                 }
655         }
656
657         return false;
658 }
659
660 /*
661  *      TransactionStartedDuringRecovery
662  *
663  * Returns true if the current transaction started while recovery was still
664  * in progress. Recovery might have ended since so RecoveryInProgress() might
665  * return false already.
666  */
667 bool
668 TransactionStartedDuringRecovery(void)
669 {
670         return CurrentTransactionState->startedInRecovery;
671 }
672
673 /*
674  *      CommandCounterIncrement
675  */
676 void
677 CommandCounterIncrement(void)
678 {
679         /*
680          * If the current value of the command counter hasn't been "used" to mark
681          * tuples, we need not increment it, since there's no need to distinguish
682          * a read-only command from others.  This helps postpone command counter
683          * overflow, and keeps no-op CommandCounterIncrement operations cheap.
684          */
685         if (currentCommandIdUsed)
686         {
687                 currentCommandId += 1;
688                 if (currentCommandId == FirstCommandId) /* check for overflow */
689                 {
690                         currentCommandId -= 1;
691                         ereport(ERROR,
692                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
693                                          errmsg("cannot have more than 2^32-1 commands in a transaction")));
694                 }
695                 currentCommandIdUsed = false;
696
697                 /* Propagate new command ID into static snapshots */
698                 SnapshotSetCommandId(currentCommandId);
699
700                 /*
701                  * Make any catalog changes done by the just-completed command visible
702                  * in the local syscache.  We obviously don't need to do this after a
703                  * read-only command.  (But see hacks in inval.c to make real sure we
704                  * don't think a command that queued inval messages was read-only.)
705                  */
706                 AtCommit_LocalCache();
707         }
708
709         /*
710          * Make any other backends' catalog changes visible to me.
711          *
712          * XXX this is probably in the wrong place: CommandCounterIncrement should
713          * be purely a local operation, most likely.  However fooling with this
714          * will affect asynchronous cross-backend interactions, which doesn't seem
715          * like a wise thing to do in late beta, so save improving this for
716          * another day - tgl 2007-11-30
717          */
718         AtStart_Cache();
719 }
720
721 /*
722  * ForceSyncCommit
723  *
724  * Interface routine to allow commands to force a synchronous commit of the
725  * current top-level transaction
726  */
727 void
728 ForceSyncCommit(void)
729 {
730         forceSyncCommit = true;
731 }
732
733
734 /* ----------------------------------------------------------------
735  *                                              StartTransaction stuff
736  * ----------------------------------------------------------------
737  */
738
739 /*
740  *      AtStart_Cache
741  */
742 static void
743 AtStart_Cache(void)
744 {
745         AcceptInvalidationMessages();
746 }
747
748 /*
749  *      AtStart_Memory
750  */
751 static void
752 AtStart_Memory(void)
753 {
754         TransactionState s = CurrentTransactionState;
755
756         /*
757          * If this is the first time through, create a private context for
758          * AbortTransaction to work in.  By reserving some space now, we can
759          * insulate AbortTransaction from out-of-memory scenarios.      Like
760          * ErrorContext, we set it up with slow growth rate and a nonzero minimum
761          * size, so that space will be reserved immediately.
762          */
763         if (TransactionAbortContext == NULL)
764                 TransactionAbortContext =
765                         AllocSetContextCreate(TopMemoryContext,
766                                                                   "TransactionAbortContext",
767                                                                   32 * 1024,
768                                                                   32 * 1024,
769                                                                   32 * 1024);
770
771         /*
772          * We shouldn't have a transaction context already.
773          */
774         Assert(TopTransactionContext == NULL);
775
776         /*
777          * Create a toplevel context for the transaction.
778          */
779         TopTransactionContext =
780                 AllocSetContextCreate(TopMemoryContext,
781                                                           "TopTransactionContext",
782                                                           ALLOCSET_DEFAULT_MINSIZE,
783                                                           ALLOCSET_DEFAULT_INITSIZE,
784                                                           ALLOCSET_DEFAULT_MAXSIZE);
785
786         /*
787          * In a top-level transaction, CurTransactionContext is the same as
788          * TopTransactionContext.
789          */
790         CurTransactionContext = TopTransactionContext;
791         s->curTransactionContext = CurTransactionContext;
792
793         /* Make the CurTransactionContext active. */
794         MemoryContextSwitchTo(CurTransactionContext);
795 }
796
797 /*
798  *      AtStart_ResourceOwner
799  */
800 static void
801 AtStart_ResourceOwner(void)
802 {
803         TransactionState s = CurrentTransactionState;
804
805         /*
806          * We shouldn't have a transaction resource owner already.
807          */
808         Assert(TopTransactionResourceOwner == NULL);
809
810         /*
811          * Create a toplevel resource owner for the transaction.
812          */
813         s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
814
815         TopTransactionResourceOwner = s->curTransactionOwner;
816         CurTransactionResourceOwner = s->curTransactionOwner;
817         CurrentResourceOwner = s->curTransactionOwner;
818 }
819
820 /* ----------------------------------------------------------------
821  *                                              StartSubTransaction stuff
822  * ----------------------------------------------------------------
823  */
824
825 /*
826  * AtSubStart_Memory
827  */
828 static void
829 AtSubStart_Memory(void)
830 {
831         TransactionState s = CurrentTransactionState;
832
833         Assert(CurTransactionContext != NULL);
834
835         /*
836          * Create a CurTransactionContext, which will be used to hold data that
837          * survives subtransaction commit but disappears on subtransaction abort.
838          * We make it a child of the immediate parent's CurTransactionContext.
839          */
840         CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
841                                                                                                   "CurTransactionContext",
842                                                                                                   ALLOCSET_DEFAULT_MINSIZE,
843                                                                                                   ALLOCSET_DEFAULT_INITSIZE,
844                                                                                                   ALLOCSET_DEFAULT_MAXSIZE);
845         s->curTransactionContext = CurTransactionContext;
846
847         /* Make the CurTransactionContext active. */
848         MemoryContextSwitchTo(CurTransactionContext);
849 }
850
851 /*
852  * AtSubStart_ResourceOwner
853  */
854 static void
855 AtSubStart_ResourceOwner(void)
856 {
857         TransactionState s = CurrentTransactionState;
858
859         Assert(s->parent != NULL);
860
861         /*
862          * Create a resource owner for the subtransaction.      We make it a child of
863          * the immediate parent's resource owner.
864          */
865         s->curTransactionOwner =
866                 ResourceOwnerCreate(s->parent->curTransactionOwner,
867                                                         "SubTransaction");
868
869         CurTransactionResourceOwner = s->curTransactionOwner;
870         CurrentResourceOwner = s->curTransactionOwner;
871 }
872
873 /* ----------------------------------------------------------------
874  *                                              CommitTransaction stuff
875  * ----------------------------------------------------------------
876  */
877
878 /*
879  *      RecordTransactionCommit
880  *
881  * Returns latest XID among xact and its children, or InvalidTransactionId
882  * if the xact has no XID.      (We compute that here just because it's easier.)
883  *
884  * This is exported only to support an ugly hack in VACUUM FULL.
885  */
886 TransactionId
887 RecordTransactionCommit(bool isVacuumFull)
888 {
889         TransactionId xid = GetTopTransactionIdIfAny();
890         bool            markXidCommitted = TransactionIdIsValid(xid);
891         TransactionId latestXid = InvalidTransactionId;
892         int                     nrels;
893         RelFileNode *rels;
894         bool            haveNonTemp;
895         int                     nchildren;
896         TransactionId *children;
897         int                     nmsgs;
898         SharedInvalidationMessage *invalMessages = NULL;
899         bool            RelcacheInitFileInval;
900
901         /* Get data needed for commit record */
902         nrels = smgrGetPendingDeletes(true, &rels, &haveNonTemp);
903         nchildren = xactGetCommittedChildren(&children);
904         nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
905                                                                                                  &RelcacheInitFileInval);
906         /*
907          * If we haven't been assigned an XID yet, we neither can, nor do we want
908          * to write a COMMIT record.
909          */
910         if (!markXidCommitted)
911         {
912                 /*
913                  * We expect that every smgrscheduleunlink is followed by a catalog
914                  * update, and hence XID assignment, so we shouldn't get here with any
915                  * pending deletes.  Use a real test not just an Assert to check this,
916                  * since it's a bit fragile.
917                  */
918                 if (nrels != 0)
919                         elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
920
921                 /* Can't have child XIDs either; AssignTransactionId enforces this */
922                 Assert(nchildren == 0);
923
924                 /*
925                  * If we didn't create XLOG entries, we're done here; otherwise we
926                  * should flush those entries the same as a commit record.      (An
927                  * example of a possible record that wouldn't cause an XID to be
928                  * assigned is a sequence advance record due to nextval() --- we want
929                  * to flush that to disk before reporting commit.)
930                  */
931                 if (XactLastRecEnd.xrecoff == 0)
932                         goto cleanup;
933         }
934         else
935         {
936                 /*
937                  * Begin commit critical section and insert the commit XLOG record.
938                  */
939                 XLogRecData rdata[4];
940                 int                     lastrdata = 0;
941                 xl_xact_commit xlrec;
942
943                 /* Tell bufmgr and smgr to prepare for commit */
944                 BufmgrCommit();
945
946                 /*
947                  * Set flags required for recovery processing of commits.
948                  */
949                 xlrec.xinfo = 0;
950                 if (RelcacheInitFileInval)
951                         xlrec.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
952                 if (isVacuumFull)
953                         xlrec.xinfo |= XACT_COMPLETION_VACUUM_FULL;
954                 if (forceSyncCommit)
955                         xlrec.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
956
957                 /*
958                  * Mark ourselves as within our "commit critical section".      This
959                  * forces any concurrent checkpoint to wait until we've updated
960                  * pg_clog.  Without this, it is possible for the checkpoint to set
961                  * REDO after the XLOG record but fail to flush the pg_clog update to
962                  * disk, leading to loss of the transaction commit if the system
963                  * crashes a little later.
964                  *
965                  * Note: we could, but don't bother to, set this flag in
966                  * RecordTransactionAbort.      That's because loss of a transaction abort
967                  * is noncritical; the presumption would be that it aborted, anyway.
968                  *
969                  * It's safe to change the inCommit flag of our own backend without
970                  * holding the ProcArrayLock, since we're the only one modifying it.
971                  * This makes checkpoint's determination of which xacts are inCommit a
972                  * bit fuzzy, but it doesn't matter.
973                  */
974                 START_CRIT_SECTION();
975                 MyProc->inCommit = true;
976
977                 SetCurrentTransactionStopTimestamp();
978                 xlrec.xact_time = xactStopTimestamp;
979                 xlrec.nrels = nrels;
980                 xlrec.nsubxacts = nchildren;
981                 xlrec.nmsgs = nmsgs;
982                 rdata[0].data = (char *) (&xlrec);
983                 rdata[0].len = MinSizeOfXactCommit;
984                 rdata[0].buffer = InvalidBuffer;
985                 /* dump rels to delete */
986                 if (nrels > 0)
987                 {
988                         rdata[0].next = &(rdata[1]);
989                         rdata[1].data = (char *) rels;
990                         rdata[1].len = nrels * sizeof(RelFileNode);
991                         rdata[1].buffer = InvalidBuffer;
992                         lastrdata = 1;
993                 }
994                 /* dump committed child Xids */
995                 if (nchildren > 0)
996                 {
997                         rdata[lastrdata].next = &(rdata[2]);
998                         rdata[2].data = (char *) children;
999                         rdata[2].len = nchildren * sizeof(TransactionId);
1000                         rdata[2].buffer = InvalidBuffer;
1001                         lastrdata = 2;
1002                 }
1003                 /* dump shared cache invalidation messages */
1004                 if (nmsgs > 0)
1005                 {
1006                         rdata[lastrdata].next = &(rdata[3]);
1007                         rdata[3].data = (char *) invalMessages;
1008                         rdata[3].len = nmsgs * sizeof(SharedInvalidationMessage);
1009                         rdata[3].buffer = InvalidBuffer;
1010                         lastrdata = 3;
1011                 }
1012                 rdata[lastrdata].next = NULL;
1013
1014                 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
1015         }
1016
1017         /*
1018          * Check if we want to commit asynchronously.  If the user has set
1019          * synchronous_commit = off, and we're not doing cleanup of any non-temp
1020          * rels nor committing any command that wanted to force sync commit, then
1021          * we can defer flushing XLOG.  (We must not allow asynchronous commit if
1022          * there are any non-temp tables to be deleted, because we might delete
1023          * the files before the COMMIT record is flushed to disk.  We do allow
1024          * asynchronous commit if all to-be-deleted tables are temporary though,
1025          * since they are lost anyway if we crash.)
1026          */
1027         if (XactSyncCommit || forceSyncCommit || haveNonTemp)
1028         {
1029                 /*
1030                  * Synchronous commit case.
1031                  *
1032                  * Sleep before flush! So we can flush more than one commit records
1033                  * per single fsync.  (The idea is some other backend may do the
1034                  * XLogFlush while we're sleeping.  This needs work still, because on
1035                  * most Unixen, the minimum select() delay is 10msec or more, which is
1036                  * way too long.)
1037                  *
1038                  * We do not sleep if enableFsync is not turned on, nor if there are
1039                  * fewer than CommitSiblings other backends with active transactions.
1040                  */
1041                 if (CommitDelay > 0 && enableFsync &&
1042                         CountActiveBackends() >= CommitSiblings)
1043                         pg_usleep(CommitDelay);
1044
1045                 XLogFlush(XactLastRecEnd);
1046
1047                 /*
1048                  * Now we may update the CLOG, if we wrote a COMMIT record above
1049                  */
1050                 if (markXidCommitted)
1051                         TransactionIdCommitTree(xid, nchildren, children);
1052         }
1053         else
1054         {
1055                 /*
1056                  * Asynchronous commit case.
1057                  *
1058                  * Report the latest async commit LSN, so that the WAL writer knows to
1059                  * flush this commit.
1060                  */
1061                 XLogSetAsyncCommitLSN(XactLastRecEnd);
1062
1063                 /*
1064                  * We must not immediately update the CLOG, since we didn't flush the
1065                  * XLOG. Instead, we store the LSN up to which the XLOG must be
1066                  * flushed before the CLOG may be updated.
1067                  */
1068                 if (markXidCommitted)
1069                         TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1070         }
1071
1072         /*
1073          * If we entered a commit critical section, leave it now, and let
1074          * checkpoints proceed.
1075          */
1076         if (markXidCommitted)
1077         {
1078                 MyProc->inCommit = false;
1079                 END_CRIT_SECTION();
1080         }
1081
1082         /* Compute latestXid while we have the child XIDs handy */
1083         latestXid = TransactionIdLatest(xid, nchildren, children);
1084
1085         /* Reset XactLastRecEnd until the next transaction writes something */
1086         XactLastRecEnd.xrecoff = 0;
1087
1088 cleanup:
1089         /* Clean up local data */
1090         if (rels)
1091                 pfree(rels);
1092
1093         return latestXid;
1094 }
1095
1096
1097 /*
1098  *      AtCommit_LocalCache
1099  */
1100 static void
1101 AtCommit_LocalCache(void)
1102 {
1103         /*
1104          * Make catalog changes visible to me for the next command.
1105          */
1106         CommandEndInvalidationMessages();
1107 }
1108
1109 /*
1110  *      AtCommit_Memory
1111  */
1112 static void
1113 AtCommit_Memory(void)
1114 {
1115         /*
1116          * Now that we're "out" of a transaction, have the system allocate things
1117          * in the top memory context instead of per-transaction contexts.
1118          */
1119         MemoryContextSwitchTo(TopMemoryContext);
1120
1121         /*
1122          * Release all transaction-local memory.
1123          */
1124         Assert(TopTransactionContext != NULL);
1125         MemoryContextDelete(TopTransactionContext);
1126         TopTransactionContext = NULL;
1127         CurTransactionContext = NULL;
1128         CurrentTransactionState->curTransactionContext = NULL;
1129 }
1130
1131 /* ----------------------------------------------------------------
1132  *                                              CommitSubTransaction stuff
1133  * ----------------------------------------------------------------
1134  */
1135
1136 /*
1137  * AtSubCommit_Memory
1138  */
1139 static void
1140 AtSubCommit_Memory(void)
1141 {
1142         TransactionState s = CurrentTransactionState;
1143
1144         Assert(s->parent != NULL);
1145
1146         /* Return to parent transaction level's memory context. */
1147         CurTransactionContext = s->parent->curTransactionContext;
1148         MemoryContextSwitchTo(CurTransactionContext);
1149
1150         /*
1151          * Ordinarily we cannot throw away the child's CurTransactionContext,
1152          * since the data it contains will be needed at upper commit.  However, if
1153          * there isn't actually anything in it, we can throw it away.  This avoids
1154          * a small memory leak in the common case of "trivial" subxacts.
1155          */
1156         if (MemoryContextIsEmpty(s->curTransactionContext))
1157         {
1158                 MemoryContextDelete(s->curTransactionContext);
1159                 s->curTransactionContext = NULL;
1160         }
1161 }
1162
1163 /*
1164  * AtSubCommit_childXids
1165  *
1166  * Pass my own XID and my child XIDs up to my parent as committed children.
1167  */
1168 static void
1169 AtSubCommit_childXids(void)
1170 {
1171         TransactionState s = CurrentTransactionState;
1172         int                     new_nChildXids;
1173
1174         Assert(s->parent != NULL);
1175
1176         /*
1177          * The parent childXids array will need to hold my XID and all my
1178          * childXids, in addition to the XIDs already there.
1179          */
1180         new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1181
1182         /* Allocate or enlarge the parent array if necessary */
1183         if (s->parent->maxChildXids < new_nChildXids)
1184         {
1185                 int                     new_maxChildXids;
1186                 TransactionId *new_childXids;
1187
1188                 /*
1189                  * Make it 2x what's needed right now, to avoid having to enlarge it
1190                  * repeatedly. But we can't go above MaxAllocSize.  (The latter limit
1191                  * is what ensures that we don't need to worry about integer overflow
1192                  * here or in the calculation of new_nChildXids.)
1193                  */
1194                 new_maxChildXids = Min(new_nChildXids * 2,
1195                                                            (int) (MaxAllocSize / sizeof(TransactionId)));
1196
1197                 if (new_maxChildXids < new_nChildXids)
1198                         ereport(ERROR,
1199                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1200                                          errmsg("maximum number of committed subtransactions (%d) exceeded",
1201                                                         (int) (MaxAllocSize / sizeof(TransactionId)))));
1202
1203                 /*
1204                  * We keep the child-XID arrays in TopTransactionContext; this avoids
1205                  * setting up child-transaction contexts for what might be just a few
1206                  * bytes of grandchild XIDs.
1207                  */
1208                 if (s->parent->childXids == NULL)
1209                         new_childXids =
1210                                 MemoryContextAlloc(TopTransactionContext,
1211                                                                    new_maxChildXids * sizeof(TransactionId));
1212                 else
1213                         new_childXids = repalloc(s->parent->childXids,
1214                                                                    new_maxChildXids * sizeof(TransactionId));
1215
1216                 s->parent->childXids = new_childXids;
1217                 s->parent->maxChildXids = new_maxChildXids;
1218         }
1219
1220         /*
1221          * Copy all my XIDs to parent's array.
1222          *
1223          * Note: We rely on the fact that the XID of a child always follows that
1224          * of its parent.  By copying the XID of this subtransaction before the
1225          * XIDs of its children, we ensure that the array stays ordered.
1226          * Likewise, all XIDs already in the array belong to subtransactions
1227          * started and subcommitted before us, so their XIDs must precede ours.
1228          */
1229         s->parent->childXids[s->parent->nChildXids] = s->transactionId;
1230
1231         if (s->nChildXids > 0)
1232                 memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1233                            s->childXids,
1234                            s->nChildXids * sizeof(TransactionId));
1235
1236         s->parent->nChildXids = new_nChildXids;
1237
1238         /* Release child's array to avoid leakage */
1239         if (s->childXids != NULL)
1240                 pfree(s->childXids);
1241         /* We must reset these to avoid double-free if fail later in commit */
1242         s->childXids = NULL;
1243         s->nChildXids = 0;
1244         s->maxChildXids = 0;
1245 }
1246
1247 /* ----------------------------------------------------------------
1248  *                                              AbortTransaction stuff
1249  * ----------------------------------------------------------------
1250  */
1251
1252 /*
1253  *      RecordTransactionAbort
1254  *
1255  * Returns latest XID among xact and its children, or InvalidTransactionId
1256  * if the xact has no XID.      (We compute that here just because it's easier.)
1257  */
1258 static TransactionId
1259 RecordTransactionAbort(bool isSubXact)
1260 {
1261         TransactionId xid = GetCurrentTransactionIdIfAny();
1262         TransactionId latestXid;
1263         int                     nrels;
1264         RelFileNode *rels;
1265         int                     nchildren;
1266         TransactionId *children;
1267         XLogRecData rdata[3];
1268         int                     lastrdata = 0;
1269         xl_xact_abort xlrec;
1270
1271         /*
1272          * If we haven't been assigned an XID, nobody will care whether we aborted
1273          * or not.      Hence, we're done in that case.  It does not matter if we have
1274          * rels to delete (note that this routine is not responsible for actually
1275          * deleting 'em).  We cannot have any child XIDs, either.
1276          */
1277         if (!TransactionIdIsValid(xid))
1278         {
1279                 /* Reset XactLastRecEnd until the next transaction writes something */
1280                 if (!isSubXact)
1281                         XactLastRecEnd.xrecoff = 0;
1282                 return InvalidTransactionId;
1283         }
1284
1285         /*
1286          * We have a valid XID, so we should write an ABORT record for it.
1287          *
1288          * We do not flush XLOG to disk here, since the default assumption after a
1289          * crash would be that we aborted, anyway.      For the same reason, we don't
1290          * need to worry about interlocking against checkpoint start.
1291          */
1292
1293         /*
1294          * Check that we haven't aborted halfway through RecordTransactionCommit.
1295          */
1296         if (TransactionIdDidCommit(xid))
1297                 elog(PANIC, "cannot abort transaction %u, it was already committed",
1298                          xid);
1299
1300         /* Fetch the data we need for the abort record */
1301         nrels = smgrGetPendingDeletes(false, &rels, NULL);
1302         nchildren = xactGetCommittedChildren(&children);
1303
1304         /* XXX do we really need a critical section here? */
1305         START_CRIT_SECTION();
1306
1307         /* Write the ABORT record */
1308         if (isSubXact)
1309                 xlrec.xact_time = GetCurrentTimestamp();
1310         else
1311         {
1312                 SetCurrentTransactionStopTimestamp();
1313                 xlrec.xact_time = xactStopTimestamp;
1314         }
1315         xlrec.nrels = nrels;
1316         xlrec.nsubxacts = nchildren;
1317         rdata[0].data = (char *) (&xlrec);
1318         rdata[0].len = MinSizeOfXactAbort;
1319         rdata[0].buffer = InvalidBuffer;
1320         /* dump rels to delete */
1321         if (nrels > 0)
1322         {
1323                 rdata[0].next = &(rdata[1]);
1324                 rdata[1].data = (char *) rels;
1325                 rdata[1].len = nrels * sizeof(RelFileNode);
1326                 rdata[1].buffer = InvalidBuffer;
1327                 lastrdata = 1;
1328         }
1329         /* dump committed child Xids */
1330         if (nchildren > 0)
1331         {
1332                 rdata[lastrdata].next = &(rdata[2]);
1333                 rdata[2].data = (char *) children;
1334                 rdata[2].len = nchildren * sizeof(TransactionId);
1335                 rdata[2].buffer = InvalidBuffer;
1336                 lastrdata = 2;
1337         }
1338         rdata[lastrdata].next = NULL;
1339
1340         (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ABORT, rdata);
1341
1342         /*
1343          * Mark the transaction aborted in clog.  This is not absolutely necessary
1344          * but we may as well do it while we are here; also, in the subxact case
1345          * it is helpful because XactLockTableWait makes use of it to avoid
1346          * waiting for already-aborted subtransactions.  It is OK to do it without
1347          * having flushed the ABORT record to disk, because in event of a crash
1348          * we'd be assumed to have aborted anyway.
1349          */
1350         TransactionIdAbortTree(xid, nchildren, children);
1351
1352         END_CRIT_SECTION();
1353
1354         /* Compute latestXid while we have the child XIDs handy */
1355         latestXid = TransactionIdLatest(xid, nchildren, children);
1356
1357         /*
1358          * If we're aborting a subtransaction, we can immediately remove failed
1359          * XIDs from PGPROC's cache of running child XIDs.  We do that here for
1360          * subxacts, because we already have the child XID array at hand.  For
1361          * main xacts, the equivalent happens just after this function returns.
1362          */
1363         if (isSubXact)
1364                 XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1365
1366         /* Reset XactLastRecEnd until the next transaction writes something */
1367         if (!isSubXact)
1368                 XactLastRecEnd.xrecoff = 0;
1369
1370         /* And clean up local data */
1371         if (rels)
1372                 pfree(rels);
1373
1374         return latestXid;
1375 }
1376
1377 /*
1378  *      AtAbort_Memory
1379  */
1380 static void
1381 AtAbort_Memory(void)
1382 {
1383         /*
1384          * Switch into TransactionAbortContext, which should have some free space
1385          * even if nothing else does.  We'll work in this context until we've
1386          * finished cleaning up.
1387          *
1388          * It is barely possible to get here when we've not been able to create
1389          * TransactionAbortContext yet; if so use TopMemoryContext.
1390          */
1391         if (TransactionAbortContext != NULL)
1392                 MemoryContextSwitchTo(TransactionAbortContext);
1393         else
1394                 MemoryContextSwitchTo(TopMemoryContext);
1395 }
1396
1397 /*
1398  * AtSubAbort_Memory
1399  */
1400 static void
1401 AtSubAbort_Memory(void)
1402 {
1403         Assert(TransactionAbortContext != NULL);
1404
1405         MemoryContextSwitchTo(TransactionAbortContext);
1406 }
1407
1408
1409 /*
1410  *      AtAbort_ResourceOwner
1411  */
1412 static void
1413 AtAbort_ResourceOwner(void)
1414 {
1415         /*
1416          * Make sure we have a valid ResourceOwner, if possible (else it will be
1417          * NULL, which is OK)
1418          */
1419         CurrentResourceOwner = TopTransactionResourceOwner;
1420 }
1421
1422 /*
1423  * AtSubAbort_ResourceOwner
1424  */
1425 static void
1426 AtSubAbort_ResourceOwner(void)
1427 {
1428         TransactionState s = CurrentTransactionState;
1429
1430         /* Make sure we have a valid ResourceOwner */
1431         CurrentResourceOwner = s->curTransactionOwner;
1432 }
1433
1434
1435 /*
1436  * AtSubAbort_childXids
1437  */
1438 static void
1439 AtSubAbort_childXids(void)
1440 {
1441         TransactionState s = CurrentTransactionState;
1442
1443         /*
1444          * We keep the child-XID arrays in TopTransactionContext (see
1445          * AtSubCommit_childXids).      This means we'd better free the array
1446          * explicitly at abort to avoid leakage.
1447          */
1448         if (s->childXids != NULL)
1449                 pfree(s->childXids);
1450         s->childXids = NULL;
1451         s->nChildXids = 0;
1452         s->maxChildXids = 0;
1453
1454         /*
1455          * We could prune the unreportedXids array here. But we don't bother.
1456          * That would potentially reduce number of XLOG_XACT_ASSIGNMENT records
1457          * but it would likely introduce more CPU time into the more common
1458          * paths, so we choose not to do that.
1459          */
1460 }
1461
1462 /* ----------------------------------------------------------------
1463  *                                              CleanupTransaction stuff
1464  * ----------------------------------------------------------------
1465  */
1466
1467 /*
1468  *      AtCleanup_Memory
1469  */
1470 static void
1471 AtCleanup_Memory(void)
1472 {
1473         Assert(CurrentTransactionState->parent == NULL);
1474
1475         /*
1476          * Now that we're "out" of a transaction, have the system allocate things
1477          * in the top memory context instead of per-transaction contexts.
1478          */
1479         MemoryContextSwitchTo(TopMemoryContext);
1480
1481         /*
1482          * Clear the special abort context for next time.
1483          */
1484         if (TransactionAbortContext != NULL)
1485                 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1486
1487         /*
1488          * Release all transaction-local memory.
1489          */
1490         if (TopTransactionContext != NULL)
1491                 MemoryContextDelete(TopTransactionContext);
1492         TopTransactionContext = NULL;
1493         CurTransactionContext = NULL;
1494         CurrentTransactionState->curTransactionContext = NULL;
1495 }
1496
1497
1498 /* ----------------------------------------------------------------
1499  *                                              CleanupSubTransaction stuff
1500  * ----------------------------------------------------------------
1501  */
1502
1503 /*
1504  * AtSubCleanup_Memory
1505  */
1506 static void
1507 AtSubCleanup_Memory(void)
1508 {
1509         TransactionState s = CurrentTransactionState;
1510
1511         Assert(s->parent != NULL);
1512
1513         /* Make sure we're not in an about-to-be-deleted context */
1514         MemoryContextSwitchTo(s->parent->curTransactionContext);
1515         CurTransactionContext = s->parent->curTransactionContext;
1516
1517         /*
1518          * Clear the special abort context for next time.
1519          */
1520         if (TransactionAbortContext != NULL)
1521                 MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1522
1523         /*
1524          * Delete the subxact local memory contexts. Its CurTransactionContext can
1525          * go too (note this also kills CurTransactionContexts from any children
1526          * of the subxact).
1527          */
1528         if (s->curTransactionContext)
1529                 MemoryContextDelete(s->curTransactionContext);
1530         s->curTransactionContext = NULL;
1531 }
1532
1533 /* ----------------------------------------------------------------
1534  *                                              interface routines
1535  * ----------------------------------------------------------------
1536  */
1537
1538 /*
1539  *      StartTransaction
1540  */
1541 static void
1542 StartTransaction(void)
1543 {
1544         TransactionState s;
1545         VirtualTransactionId vxid;
1546
1547         /*
1548          * Let's just make sure the state stack is empty
1549          */
1550         s = &TopTransactionStateData;
1551         CurrentTransactionState = s;
1552
1553         /*
1554          * check the current transaction state
1555          */
1556         if (s->state != TRANS_DEFAULT)
1557                 elog(WARNING, "StartTransaction while in %s state",
1558                          TransStateAsString(s->state));
1559
1560         /*
1561          * set the current transaction state information appropriately during
1562          * start processing
1563          */
1564         s->state = TRANS_START;
1565         s->transactionId = InvalidTransactionId;        /* until assigned */
1566
1567         /*
1568          * Make sure we've reset xact state variables
1569          *
1570          * If recovery is still in progress, mark this transaction as read-only.
1571          * We have lower level defences in XLogInsert and elsewhere to stop us
1572          * from modifying data during recovery, but this gives the normal
1573          * indication to the user that the transaction is read-only.
1574          */
1575         if (RecoveryInProgress())
1576         {
1577                 s->startedInRecovery = true;
1578                 XactReadOnly = true;
1579         }
1580         else
1581         {
1582                 s->startedInRecovery = false;
1583                 XactReadOnly = DefaultXactReadOnly;
1584         }
1585         XactIsoLevel = DefaultXactIsoLevel;
1586         forceSyncCommit = false;
1587         MyXactAccessedTempRel = false;
1588
1589         /*
1590          * reinitialize within-transaction counters
1591          */
1592         s->subTransactionId = TopSubTransactionId;
1593         currentSubTransactionId = TopSubTransactionId;
1594         currentCommandId = FirstCommandId;
1595         currentCommandIdUsed = false;
1596
1597         /*
1598          * initialize reported xid accounting
1599          */
1600         nUnreportedXids = 0;
1601
1602         /*
1603          * must initialize resource-management stuff first
1604          */
1605         AtStart_Memory();
1606         AtStart_ResourceOwner();
1607
1608         /*
1609          * Assign a new LocalTransactionId, and combine it with the backendId to
1610          * form a virtual transaction id.
1611          */
1612         vxid.backendId = MyBackendId;
1613         vxid.localTransactionId = GetNextLocalTransactionId();
1614
1615         /*
1616          * Lock the virtual transaction id before we announce it in the proc array
1617          */
1618         VirtualXactLockTableInsert(vxid);
1619
1620         /*
1621          * Advertise it in the proc array.      We assume assignment of
1622          * LocalTransactionID is atomic, and the backendId should be set already.
1623          */
1624         Assert(MyProc->backendId == vxid.backendId);
1625         MyProc->lxid = vxid.localTransactionId;
1626
1627         TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1628
1629         /*
1630          * set transaction_timestamp() (a/k/a now()).  We want this to be the same
1631          * as the first command's statement_timestamp(), so don't do a fresh
1632          * GetCurrentTimestamp() call (which'd be expensive anyway).  Also, mark
1633          * xactStopTimestamp as unset.
1634          */
1635         xactStartTimestamp = stmtStartTimestamp;
1636         xactStopTimestamp = 0;
1637         pgstat_report_xact_timestamp(xactStartTimestamp);
1638
1639         /*
1640          * initialize current transaction state fields
1641          *
1642          * note: prevXactReadOnly is not used at the outermost level
1643          */
1644         s->nestingLevel = 1;
1645         s->gucNestLevel = 1;
1646         s->childXids = NULL;
1647         s->nChildXids = 0;
1648         s->maxChildXids = 0;
1649         GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
1650         /* SecurityRestrictionContext should never be set outside a transaction */
1651         Assert(s->prevSecContext == 0);
1652
1653         /*
1654          * initialize other subsystems for new transaction
1655          */
1656         AtStart_GUC();
1657         AtStart_Inval();
1658         AtStart_Cache();
1659         AfterTriggerBeginXact();
1660
1661         /*
1662          * done with start processing, set current transaction state to "in
1663          * progress"
1664          */
1665         s->state = TRANS_INPROGRESS;
1666
1667         ShowTransactionState("StartTransaction");
1668 }
1669
1670
1671 /*
1672  *      CommitTransaction
1673  *
1674  * NB: if you change this routine, better look at PrepareTransaction too!
1675  */
1676 static void
1677 CommitTransaction(void)
1678 {
1679         TransactionState s = CurrentTransactionState;
1680         TransactionId latestXid;
1681
1682         ShowTransactionState("CommitTransaction");
1683
1684         /*
1685          * check the current transaction state
1686          */
1687         if (s->state != TRANS_INPROGRESS)
1688                 elog(WARNING, "CommitTransaction while in %s state",
1689                          TransStateAsString(s->state));
1690         Assert(s->parent == NULL);
1691
1692         /*
1693          * Do pre-commit processing (most of this stuff requires database access,
1694          * and in fact could still cause an error...)
1695          *
1696          * It is possible for CommitHoldablePortals to invoke functions that queue
1697          * deferred triggers, and it's also possible that triggers create holdable
1698          * cursors.  So we have to loop until there's nothing left to do.
1699          */
1700         for (;;)
1701         {
1702                 /*
1703                  * Fire all currently pending deferred triggers.
1704                  */
1705                 AfterTriggerFireDeferred();
1706
1707                 /*
1708                  * Convert any open holdable cursors into static portals.  If there
1709                  * weren't any, we are done ... otherwise loop back to check if they
1710                  * queued deferred triggers.  Lather, rinse, repeat.
1711                  */
1712                 if (!CommitHoldablePortals())
1713                         break;
1714         }
1715
1716         /* Now we can shut down the deferred-trigger manager */
1717         AfterTriggerEndXact(true);
1718
1719         /* Close any open regular cursors */
1720         AtCommit_Portals();
1721
1722         /*
1723          * Let ON COMMIT management do its thing (must happen after closing
1724          * cursors, to avoid dangling-reference problems)
1725          */
1726         PreCommit_on_commit_actions();
1727
1728         /* close large objects before lower-level cleanup */
1729         AtEOXact_LargeObject(true);
1730
1731         /* NOTIFY commit must come before lower-level cleanup */
1732         AtCommit_Notify();
1733
1734         /* Prevent cancel/die interrupt while cleaning up */
1735         HOLD_INTERRUPTS();
1736
1737         /*
1738          * set the current transaction state information appropriately during
1739          * commit processing
1740          */
1741         s->state = TRANS_COMMIT;
1742
1743         /*
1744          * Here is where we really truly commit.
1745          */
1746         latestXid = RecordTransactionCommit(false);
1747
1748         TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
1749
1750         /*
1751          * Let others know about no transaction in progress by me. Note that this
1752          * must be done _before_ releasing locks we hold and _after_
1753          * RecordTransactionCommit.
1754          */
1755         ProcArrayEndTransaction(MyProc, latestXid);
1756
1757         /*
1758          * This is all post-commit cleanup.  Note that if an error is raised here,
1759          * it's too late to abort the transaction.  This should be just
1760          * noncritical resource releasing.
1761          *
1762          * The ordering of operations is not entirely random.  The idea is:
1763          * release resources visible to other backends (eg, files, buffer pins);
1764          * then release locks; then release backend-local resources. We want to
1765          * release locks at the point where any backend waiting for us will see
1766          * our transaction as being fully cleaned up.
1767          *
1768          * Resources that can be associated with individual queries are handled by
1769          * the ResourceOwner mechanism.  The other calls here are for backend-wide
1770          * state.
1771          */
1772
1773         CallXactCallbacks(XACT_EVENT_COMMIT);
1774
1775         ResourceOwnerRelease(TopTransactionResourceOwner,
1776                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
1777                                                  true, true);
1778
1779         /* Check we've released all buffer pins */
1780         AtEOXact_Buffers(true);
1781
1782         /* Clean up the relation cache */
1783         AtEOXact_RelationCache(true);
1784
1785         /* Clean up the snapshot manager */
1786         AtEarlyCommit_Snapshot();
1787
1788         /*
1789          * Make catalog changes visible to all backends.  This has to happen after
1790          * relcache references are dropped (see comments for
1791          * AtEOXact_RelationCache), but before locks are released (if anyone is
1792          * waiting for lock on a relation we've modified, we want them to know
1793          * about the catalog change before they start using the relation).
1794          */
1795         AtEOXact_Inval(true);
1796
1797         /*
1798          * Likewise, dropping of files deleted during the transaction is best done
1799          * after releasing relcache and buffer pins.  (This is not strictly
1800          * necessary during commit, since such pins should have been released
1801          * already, but this ordering is definitely critical during abort.)
1802          */
1803         smgrDoPendingDeletes(true);
1804
1805         AtEOXact_MultiXact();
1806
1807         ResourceOwnerRelease(TopTransactionResourceOwner,
1808                                                  RESOURCE_RELEASE_LOCKS,
1809                                                  true, true);
1810         ResourceOwnerRelease(TopTransactionResourceOwner,
1811                                                  RESOURCE_RELEASE_AFTER_LOCKS,
1812                                                  true, true);
1813
1814         /* Check we've released all catcache entries */
1815         AtEOXact_CatCache(true);
1816
1817         AtEOXact_GUC(true, 1);
1818         AtEOXact_SPI(true);
1819         AtEOXact_on_commit_actions(true);
1820         AtEOXact_Namespace(true);
1821         /* smgrcommit already done */
1822         AtEOXact_Files();
1823         AtEOXact_ComboCid();
1824         AtEOXact_HashTables(true);
1825         AtEOXact_PgStat(true);
1826         AtEOXact_Snapshot(true);
1827         pgstat_report_xact_timestamp(0);
1828
1829         CurrentResourceOwner = NULL;
1830         ResourceOwnerDelete(TopTransactionResourceOwner);
1831         s->curTransactionOwner = NULL;
1832         CurTransactionResourceOwner = NULL;
1833         TopTransactionResourceOwner = NULL;
1834
1835         AtCommit_Memory();
1836
1837         s->transactionId = InvalidTransactionId;
1838         s->subTransactionId = InvalidSubTransactionId;
1839         s->nestingLevel = 0;
1840         s->gucNestLevel = 0;
1841         s->childXids = NULL;
1842         s->nChildXids = 0;
1843         s->maxChildXids = 0;
1844
1845         /*
1846          * done with commit processing, set current transaction state back to
1847          * default
1848          */
1849         s->state = TRANS_DEFAULT;
1850
1851         RESUME_INTERRUPTS();
1852 }
1853
1854
1855 /*
1856  *      PrepareTransaction
1857  *
1858  * NB: if you change this routine, better look at CommitTransaction too!
1859  */
1860 static void
1861 PrepareTransaction(void)
1862 {
1863         TransactionState s = CurrentTransactionState;
1864         TransactionId xid = GetCurrentTransactionId();
1865         GlobalTransaction gxact;
1866         TimestampTz prepared_at;
1867
1868         ShowTransactionState("PrepareTransaction");
1869
1870         /*
1871          * check the current transaction state
1872          */
1873         if (s->state != TRANS_INPROGRESS)
1874                 elog(WARNING, "PrepareTransaction while in %s state",
1875                          TransStateAsString(s->state));
1876         Assert(s->parent == NULL);
1877
1878         /*
1879          * Do pre-commit processing (most of this stuff requires database access,
1880          * and in fact could still cause an error...)
1881          *
1882          * It is possible for PrepareHoldablePortals to invoke functions that
1883          * queue deferred triggers, and it's also possible that triggers create
1884          * holdable cursors.  So we have to loop until there's nothing left to do.
1885          */
1886         for (;;)
1887         {
1888                 /*
1889                  * Fire all currently pending deferred triggers.
1890                  */
1891                 AfterTriggerFireDeferred();
1892
1893                 /*
1894                  * Convert any open holdable cursors into static portals.  If there
1895                  * weren't any, we are done ... otherwise loop back to check if they
1896                  * queued deferred triggers.  Lather, rinse, repeat.
1897                  */
1898                 if (!PrepareHoldablePortals())
1899                         break;
1900         }
1901
1902         /* Now we can shut down the deferred-trigger manager */
1903         AfterTriggerEndXact(true);
1904
1905         /* Close any open regular cursors */
1906         AtCommit_Portals();
1907
1908         /*
1909          * Let ON COMMIT management do its thing (must happen after closing
1910          * cursors, to avoid dangling-reference problems)
1911          */
1912         PreCommit_on_commit_actions();
1913
1914         /* close large objects before lower-level cleanup */
1915         AtEOXact_LargeObject(true);
1916
1917         /* NOTIFY will be handled below */
1918
1919         /*
1920          * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
1921          * this transaction.  Having the prepared xact hold locks on another
1922          * backend's temp table seems a bad idea --- for instance it would prevent
1923          * the backend from exiting.  There are other problems too, such as how to
1924          * clean up the source backend's local buffers and ON COMMIT state if the
1925          * prepared xact includes a DROP of a temp table.
1926          *
1927          * We must check this after executing any ON COMMIT actions, because they
1928          * might still access a temp relation.
1929          *
1930          * XXX In principle this could be relaxed to allow some useful special
1931          * cases, such as a temp table created and dropped all within the
1932          * transaction.  That seems to require much more bookkeeping though.
1933          */
1934         if (MyXactAccessedTempRel)
1935                 ereport(ERROR,
1936                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1937                                  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
1938
1939         /* Prevent cancel/die interrupt while cleaning up */
1940         HOLD_INTERRUPTS();
1941
1942         /*
1943          * set the current transaction state information appropriately during
1944          * prepare processing
1945          */
1946         s->state = TRANS_PREPARE;
1947
1948         prepared_at = GetCurrentTimestamp();
1949
1950         /* Tell bufmgr and smgr to prepare for commit */
1951         BufmgrCommit();
1952
1953         /*
1954          * Reserve the GID for this transaction. This could fail if the requested
1955          * GID is invalid or already in use.
1956          */
1957         gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
1958                                                         GetUserId(), MyDatabaseId);
1959         prepareGID = NULL;
1960
1961         /*
1962          * Collect data for the 2PC state file.  Note that in general, no actual
1963          * state change should happen in the called modules during this step,
1964          * since it's still possible to fail before commit, and in that case we
1965          * want transaction abort to be able to clean up.  (In particular, the
1966          * AtPrepare routines may error out if they find cases they cannot
1967          * handle.)  State cleanup should happen in the PostPrepare routines
1968          * below.  However, some modules can go ahead and clear state here because
1969          * they wouldn't do anything with it during abort anyway.
1970          *
1971          * Note: because the 2PC state file records will be replayed in the same
1972          * order they are made, the order of these calls has to match the order in
1973          * which we want things to happen during COMMIT PREPARED or ROLLBACK
1974          * PREPARED; in particular, pay attention to whether things should happen
1975          * before or after releasing the transaction's locks.
1976          */
1977         StartPrepare(gxact);
1978
1979         AtPrepare_Notify();
1980         AtPrepare_Locks();
1981         AtPrepare_PgStat();
1982         AtPrepare_MultiXact();
1983
1984         /*
1985          * Here is where we really truly prepare.
1986          *
1987          * We have to record transaction prepares even if we didn't make any
1988          * updates, because the transaction manager might get confused if we lose
1989          * a global transaction.
1990          */
1991         EndPrepare(gxact);
1992
1993         /*
1994          * Now we clean up backend-internal state and release internal resources.
1995          */
1996
1997         /* Reset XactLastRecEnd until the next transaction writes something */
1998         XactLastRecEnd.xrecoff = 0;
1999
2000         /*
2001          * Let others know about no transaction in progress by me.      This has to be
2002          * done *after* the prepared transaction has been marked valid, else
2003          * someone may think it is unlocked and recyclable.
2004          */
2005         ProcArrayClearTransaction(MyProc);
2006
2007         /*
2008          * This is all post-transaction cleanup.  Note that if an error is raised
2009          * here, it's too late to abort the transaction.  This should be just
2010          * noncritical resource releasing.      See notes in CommitTransaction.
2011          */
2012
2013         CallXactCallbacks(XACT_EVENT_PREPARE);
2014
2015         ResourceOwnerRelease(TopTransactionResourceOwner,
2016                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
2017                                                  true, true);
2018
2019         /* Check we've released all buffer pins */
2020         AtEOXact_Buffers(true);
2021
2022         /* Clean up the relation cache */
2023         AtEOXact_RelationCache(true);
2024
2025         /* Clean up the snapshot manager */
2026         AtEarlyCommit_Snapshot();
2027
2028         /* notify doesn't need a postprepare call */
2029
2030         PostPrepare_PgStat();
2031
2032         PostPrepare_Inval();
2033
2034         PostPrepare_smgr();
2035
2036         PostPrepare_MultiXact(xid);
2037
2038         PostPrepare_Locks(xid);
2039
2040         ResourceOwnerRelease(TopTransactionResourceOwner,
2041                                                  RESOURCE_RELEASE_LOCKS,
2042                                                  true, true);
2043         ResourceOwnerRelease(TopTransactionResourceOwner,
2044                                                  RESOURCE_RELEASE_AFTER_LOCKS,
2045                                                  true, true);
2046
2047         /* Check we've released all catcache entries */
2048         AtEOXact_CatCache(true);
2049
2050         /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2051         AtEOXact_GUC(true, 1);
2052         AtEOXact_SPI(true);
2053         AtEOXact_on_commit_actions(true);
2054         AtEOXact_Namespace(true);
2055         /* smgrcommit already done */
2056         AtEOXact_Files();
2057         AtEOXact_ComboCid();
2058         AtEOXact_HashTables(true);
2059         /* don't call AtEOXact_PgStat here */
2060         AtEOXact_Snapshot(true);
2061
2062         CurrentResourceOwner = NULL;
2063         ResourceOwnerDelete(TopTransactionResourceOwner);
2064         s->curTransactionOwner = NULL;
2065         CurTransactionResourceOwner = NULL;
2066         TopTransactionResourceOwner = NULL;
2067
2068         AtCommit_Memory();
2069
2070         s->transactionId = InvalidTransactionId;
2071         s->subTransactionId = InvalidSubTransactionId;
2072         s->nestingLevel = 0;
2073         s->gucNestLevel = 0;
2074         s->childXids = NULL;
2075         s->nChildXids = 0;
2076         s->maxChildXids = 0;
2077
2078         /*
2079          * done with 1st phase commit processing, set current transaction state
2080          * back to default
2081          */
2082         s->state = TRANS_DEFAULT;
2083
2084         RESUME_INTERRUPTS();
2085 }
2086
2087
2088 /*
2089  *      AbortTransaction
2090  */
2091 static void
2092 AbortTransaction(void)
2093 {
2094         TransactionState s = CurrentTransactionState;
2095         TransactionId latestXid;
2096
2097         /* Prevent cancel/die interrupt while cleaning up */
2098         HOLD_INTERRUPTS();
2099
2100         /* Make sure we have a valid memory context and resource owner */
2101         AtAbort_Memory();
2102         AtAbort_ResourceOwner();
2103
2104         /*
2105          * Release any LW locks we might be holding as quickly as possible.
2106          * (Regular locks, however, must be held till we finish aborting.)
2107          * Releasing LW locks is critical since we might try to grab them again
2108          * while cleaning up!
2109          */
2110         LWLockReleaseAll();
2111
2112         /* Clean up buffer I/O and buffer context locks, too */
2113         AbortBufferIO();
2114         UnlockBuffers();
2115
2116         /*
2117          * Also clean up any open wait for lock, since the lock manager will choke
2118          * if we try to wait for another lock before doing this.
2119          */
2120         LockWaitCancel();
2121
2122         /*
2123          * check the current transaction state
2124          */
2125         if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2126                 elog(WARNING, "AbortTransaction while in %s state",
2127                          TransStateAsString(s->state));
2128         Assert(s->parent == NULL);
2129
2130         /*
2131          * set the current transaction state information appropriately during the
2132          * abort processing
2133          */
2134         s->state = TRANS_ABORT;
2135
2136         /*
2137          * Reset user ID which might have been changed transiently.  We need this
2138          * to clean up in case control escaped out of a SECURITY DEFINER function
2139          * or other local change of CurrentUserId; therefore, the prior value of
2140          * SecurityRestrictionContext also needs to be restored.
2141          *
2142          * (Note: it is not necessary to restore session authorization or role
2143          * settings here because those can only be changed via GUC, and GUC will
2144          * take care of rolling them back if need be.)
2145          */
2146         SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2147
2148         /*
2149          * do abort processing
2150          */
2151         AfterTriggerEndXact(false);
2152         AtAbort_Portals();
2153         AtEOXact_LargeObject(false);    /* 'false' means it's abort */
2154         AtAbort_Notify();
2155
2156         /*
2157          * Advertise the fact that we aborted in pg_clog (assuming that we got as
2158          * far as assigning an XID to advertise).
2159          */
2160         latestXid = RecordTransactionAbort(false);
2161
2162         TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2163
2164         /*
2165          * Let others know about no transaction in progress by me. Note that this
2166          * must be done _before_ releasing locks we hold and _after_
2167          * RecordTransactionAbort.
2168          */
2169         ProcArrayEndTransaction(MyProc, latestXid);
2170
2171         /*
2172          * Post-abort cleanup.  See notes in CommitTransaction() concerning
2173          * ordering.
2174          */
2175
2176         CallXactCallbacks(XACT_EVENT_ABORT);
2177
2178         ResourceOwnerRelease(TopTransactionResourceOwner,
2179                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
2180                                                  false, true);
2181         AtEOXact_Buffers(false);
2182         AtEOXact_RelationCache(false);
2183         AtEOXact_Inval(false);
2184         smgrDoPendingDeletes(false);
2185         AtEOXact_MultiXact();
2186         ResourceOwnerRelease(TopTransactionResourceOwner,
2187                                                  RESOURCE_RELEASE_LOCKS,
2188                                                  false, true);
2189         ResourceOwnerRelease(TopTransactionResourceOwner,
2190                                                  RESOURCE_RELEASE_AFTER_LOCKS,
2191                                                  false, true);
2192         AtEOXact_CatCache(false);
2193
2194         AtEOXact_GUC(false, 1);
2195         AtEOXact_SPI(false);
2196         AtEOXact_on_commit_actions(false);
2197         AtEOXact_Namespace(false);
2198         AtEOXact_Files();
2199         AtEOXact_ComboCid();
2200         AtEOXact_HashTables(false);
2201         AtEOXact_PgStat(false);
2202         AtEOXact_Snapshot(false);
2203         pgstat_report_xact_timestamp(0);
2204
2205         /*
2206          * State remains TRANS_ABORT until CleanupTransaction().
2207          */
2208         RESUME_INTERRUPTS();
2209 }
2210
2211 /*
2212  *      CleanupTransaction
2213  */
2214 static void
2215 CleanupTransaction(void)
2216 {
2217         TransactionState s = CurrentTransactionState;
2218
2219         /*
2220          * State should still be TRANS_ABORT from AbortTransaction().
2221          */
2222         if (s->state != TRANS_ABORT)
2223                 elog(FATAL, "CleanupTransaction: unexpected state %s",
2224                          TransStateAsString(s->state));
2225
2226         /*
2227          * do abort cleanup processing
2228          */
2229         AtCleanup_Portals();            /* now safe to release portal memory */
2230
2231         CurrentResourceOwner = NULL;    /* and resource owner */
2232         if (TopTransactionResourceOwner)
2233                 ResourceOwnerDelete(TopTransactionResourceOwner);
2234         s->curTransactionOwner = NULL;
2235         CurTransactionResourceOwner = NULL;
2236         TopTransactionResourceOwner = NULL;
2237
2238         AtCleanup_Memory();                     /* and transaction memory */
2239
2240         s->transactionId = InvalidTransactionId;
2241         s->subTransactionId = InvalidSubTransactionId;
2242         s->nestingLevel = 0;
2243         s->gucNestLevel = 0;
2244         s->childXids = NULL;
2245         s->nChildXids = 0;
2246         s->maxChildXids = 0;
2247
2248         /*
2249          * done with abort processing, set current transaction state back to
2250          * default
2251          */
2252         s->state = TRANS_DEFAULT;
2253 }
2254
2255 /*
2256  *      StartTransactionCommand
2257  */
2258 void
2259 StartTransactionCommand(void)
2260 {
2261         TransactionState s = CurrentTransactionState;
2262
2263         switch (s->blockState)
2264         {
2265                         /*
2266                          * if we aren't in a transaction block, we just do our usual start
2267                          * transaction.
2268                          */
2269                 case TBLOCK_DEFAULT:
2270                         StartTransaction();
2271                         s->blockState = TBLOCK_STARTED;
2272                         break;
2273
2274                         /*
2275                          * We are somewhere in a transaction block or subtransaction and
2276                          * about to start a new command.  For now we do nothing, but
2277                          * someday we may do command-local resource initialization. (Note
2278                          * that any needed CommandCounterIncrement was done by the
2279                          * previous CommitTransactionCommand.)
2280                          */
2281                 case TBLOCK_INPROGRESS:
2282                 case TBLOCK_SUBINPROGRESS:
2283                         break;
2284
2285                         /*
2286                          * Here we are in a failed transaction block (one of the commands
2287                          * caused an abort) so we do nothing but remain in the abort
2288                          * state.  Eventually we will get a ROLLBACK command which will
2289                          * get us out of this state.  (It is up to other code to ensure
2290                          * that no commands other than ROLLBACK will be processed in these
2291                          * states.)
2292                          */
2293                 case TBLOCK_ABORT:
2294                 case TBLOCK_SUBABORT:
2295                         break;
2296
2297                         /* These cases are invalid. */
2298                 case TBLOCK_STARTED:
2299                 case TBLOCK_BEGIN:
2300                 case TBLOCK_SUBBEGIN:
2301                 case TBLOCK_END:
2302                 case TBLOCK_SUBEND:
2303                 case TBLOCK_ABORT_END:
2304                 case TBLOCK_SUBABORT_END:
2305                 case TBLOCK_ABORT_PENDING:
2306                 case TBLOCK_SUBABORT_PENDING:
2307                 case TBLOCK_SUBRESTART:
2308                 case TBLOCK_SUBABORT_RESTART:
2309                 case TBLOCK_PREPARE:
2310                         elog(ERROR, "StartTransactionCommand: unexpected state %s",
2311                                  BlockStateAsString(s->blockState));
2312                         break;
2313         }
2314
2315         /*
2316          * We must switch to CurTransactionContext before returning. This is
2317          * already done if we called StartTransaction, otherwise not.
2318          */
2319         Assert(CurTransactionContext != NULL);
2320         MemoryContextSwitchTo(CurTransactionContext);
2321 }
2322
2323 /*
2324  *      CommitTransactionCommand
2325  */
2326 void
2327 CommitTransactionCommand(void)
2328 {
2329         TransactionState s = CurrentTransactionState;
2330
2331         switch (s->blockState)
2332         {
2333                         /*
2334                          * This shouldn't happen, because it means the previous
2335                          * StartTransactionCommand didn't set the STARTED state
2336                          * appropriately.
2337                          */
2338                 case TBLOCK_DEFAULT:
2339                         elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2340                                  BlockStateAsString(s->blockState));
2341                         break;
2342
2343                         /*
2344                          * If we aren't in a transaction block, just do our usual
2345                          * transaction commit, and return to the idle state.
2346                          */
2347                 case TBLOCK_STARTED:
2348                         CommitTransaction();
2349                         s->blockState = TBLOCK_DEFAULT;
2350                         break;
2351
2352                         /*
2353                          * We are completing a "BEGIN TRANSACTION" command, so we change
2354                          * to the "transaction block in progress" state and return.  (We
2355                          * assume the BEGIN did nothing to the database, so we need no
2356                          * CommandCounterIncrement.)
2357                          */
2358                 case TBLOCK_BEGIN:
2359                         s->blockState = TBLOCK_INPROGRESS;
2360                         break;
2361
2362                         /*
2363                          * This is the case when we have finished executing a command
2364                          * someplace within a transaction block.  We increment the command
2365                          * counter and return.
2366                          */
2367                 case TBLOCK_INPROGRESS:
2368                 case TBLOCK_SUBINPROGRESS:
2369                         CommandCounterIncrement();
2370                         break;
2371
2372                         /*
2373                          * We are completing a "COMMIT" command.  Do it and return to the
2374                          * idle state.
2375                          */
2376                 case TBLOCK_END:
2377                         CommitTransaction();
2378                         s->blockState = TBLOCK_DEFAULT;
2379                         break;
2380
2381                         /*
2382                          * Here we are in the middle of a transaction block but one of the
2383                          * commands caused an abort so we do nothing but remain in the
2384                          * abort state.  Eventually we will get a ROLLBACK comand.
2385                          */
2386                 case TBLOCK_ABORT:
2387                 case TBLOCK_SUBABORT:
2388                         break;
2389
2390                         /*
2391                          * Here we were in an aborted transaction block and we just got
2392                          * the ROLLBACK command from the user, so clean up the
2393                          * already-aborted transaction and return to the idle state.
2394                          */
2395                 case TBLOCK_ABORT_END:
2396                         CleanupTransaction();
2397                         s->blockState = TBLOCK_DEFAULT;
2398                         break;
2399
2400                         /*
2401                          * Here we were in a perfectly good transaction block but the user
2402                          * told us to ROLLBACK anyway.  We have to abort the transaction
2403                          * and then clean up.
2404                          */
2405                 case TBLOCK_ABORT_PENDING:
2406                         AbortTransaction();
2407                         CleanupTransaction();
2408                         s->blockState = TBLOCK_DEFAULT;
2409                         break;
2410
2411                         /*
2412                          * We are completing a "PREPARE TRANSACTION" command.  Do it and
2413                          * return to the idle state.
2414                          */
2415                 case TBLOCK_PREPARE:
2416                         PrepareTransaction();
2417                         s->blockState = TBLOCK_DEFAULT;
2418                         break;
2419
2420                         /*
2421                          * We were just issued a SAVEPOINT inside a transaction block.
2422                          * Start a subtransaction.      (DefineSavepoint already did
2423                          * PushTransaction, so as to have someplace to put the SUBBEGIN
2424                          * state.)
2425                          */
2426                 case TBLOCK_SUBBEGIN:
2427                         StartSubTransaction();
2428                         s->blockState = TBLOCK_SUBINPROGRESS;
2429                         break;
2430
2431                         /*
2432                          * We were issued a COMMIT or RELEASE command, so we end the
2433                          * current subtransaction and return to the parent transaction.
2434                          * The parent might be ended too, so repeat till we are all the
2435                          * way out or find an INPROGRESS transaction.
2436                          */
2437                 case TBLOCK_SUBEND:
2438                         do
2439                         {
2440                                 CommitSubTransaction();
2441                                 s = CurrentTransactionState;    /* changed by pop */
2442                         } while (s->blockState == TBLOCK_SUBEND);
2443                         /* If we had a COMMIT command, finish off the main xact too */
2444                         if (s->blockState == TBLOCK_END)
2445                         {
2446                                 Assert(s->parent == NULL);
2447                                 CommitTransaction();
2448                                 s->blockState = TBLOCK_DEFAULT;
2449                         }
2450                         else if (s->blockState == TBLOCK_PREPARE)
2451                         {
2452                                 Assert(s->parent == NULL);
2453                                 PrepareTransaction();
2454                                 s->blockState = TBLOCK_DEFAULT;
2455                         }
2456                         else
2457                         {
2458                                 Assert(s->blockState == TBLOCK_INPROGRESS ||
2459                                            s->blockState == TBLOCK_SUBINPROGRESS);
2460                         }
2461                         break;
2462
2463                         /*
2464                          * The current already-failed subtransaction is ending due to a
2465                          * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2466                          * examine the parent (which could be in any of several states).
2467                          */
2468                 case TBLOCK_SUBABORT_END:
2469                         CleanupSubTransaction();
2470                         CommitTransactionCommand();
2471                         break;
2472
2473                         /*
2474                          * As above, but it's not dead yet, so abort first.
2475                          */
2476                 case TBLOCK_SUBABORT_PENDING:
2477                         AbortSubTransaction();
2478                         CleanupSubTransaction();
2479                         CommitTransactionCommand();
2480                         break;
2481
2482                         /*
2483                          * The current subtransaction is the target of a ROLLBACK TO
2484                          * command.  Abort and pop it, then start a new subtransaction
2485                          * with the same name.
2486                          */
2487                 case TBLOCK_SUBRESTART:
2488                         {
2489                                 char       *name;
2490                                 int                     savepointLevel;
2491
2492                                 /* save name and keep Cleanup from freeing it */
2493                                 name = s->name;
2494                                 s->name = NULL;
2495                                 savepointLevel = s->savepointLevel;
2496
2497                                 AbortSubTransaction();
2498                                 CleanupSubTransaction();
2499
2500                                 DefineSavepoint(NULL);
2501                                 s = CurrentTransactionState;    /* changed by push */
2502                                 s->name = name;
2503                                 s->savepointLevel = savepointLevel;
2504
2505                                 /* This is the same as TBLOCK_SUBBEGIN case */
2506                                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2507                                 StartSubTransaction();
2508                                 s->blockState = TBLOCK_SUBINPROGRESS;
2509                         }
2510                         break;
2511
2512                         /*
2513                          * Same as above, but the subtransaction had already failed, so we
2514                          * don't need AbortSubTransaction.
2515                          */
2516                 case TBLOCK_SUBABORT_RESTART:
2517                         {
2518                                 char       *name;
2519                                 int                     savepointLevel;
2520
2521                                 /* save name and keep Cleanup from freeing it */
2522                                 name = s->name;
2523                                 s->name = NULL;
2524                                 savepointLevel = s->savepointLevel;
2525
2526                                 CleanupSubTransaction();
2527
2528                                 DefineSavepoint(NULL);
2529                                 s = CurrentTransactionState;    /* changed by push */
2530                                 s->name = name;
2531                                 s->savepointLevel = savepointLevel;
2532
2533                                 /* This is the same as TBLOCK_SUBBEGIN case */
2534                                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2535                                 StartSubTransaction();
2536                                 s->blockState = TBLOCK_SUBINPROGRESS;
2537                         }
2538                         break;
2539         }
2540 }
2541
2542 /*
2543  *      AbortCurrentTransaction
2544  */
2545 void
2546 AbortCurrentTransaction(void)
2547 {
2548         TransactionState s = CurrentTransactionState;
2549
2550         switch (s->blockState)
2551         {
2552                 case TBLOCK_DEFAULT:
2553                         if (s->state == TRANS_DEFAULT)
2554                         {
2555                                 /* we are idle, so nothing to do */
2556                         }
2557                         else
2558                         {
2559                                 /*
2560                                  * We can get here after an error during transaction start
2561                                  * (state will be TRANS_START).  Need to clean up the
2562                                  * incompletely started transaction.  First, adjust the
2563                                  * low-level state to suppress warning message from
2564                                  * AbortTransaction.
2565                                  */
2566                                 if (s->state == TRANS_START)
2567                                         s->state = TRANS_INPROGRESS;
2568                                 AbortTransaction();
2569                                 CleanupTransaction();
2570                         }
2571                         break;
2572
2573                         /*
2574                          * if we aren't in a transaction block, we just do the basic abort
2575                          * & cleanup transaction.
2576                          */
2577                 case TBLOCK_STARTED:
2578                         AbortTransaction();
2579                         CleanupTransaction();
2580                         s->blockState = TBLOCK_DEFAULT;
2581                         break;
2582
2583                         /*
2584                          * If we are in TBLOCK_BEGIN it means something screwed up right
2585                          * after reading "BEGIN TRANSACTION".  We assume that the user
2586                          * will interpret the error as meaning the BEGIN failed to get him
2587                          * into a transaction block, so we should abort and return to idle
2588                          * state.
2589                          */
2590                 case TBLOCK_BEGIN:
2591                         AbortTransaction();
2592                         CleanupTransaction();
2593                         s->blockState = TBLOCK_DEFAULT;
2594                         break;
2595
2596                         /*
2597                          * We are somewhere in a transaction block and we've gotten a
2598                          * failure, so we abort the transaction and set up the persistent
2599                          * ABORT state.  We will stay in ABORT until we get a ROLLBACK.
2600                          */
2601                 case TBLOCK_INPROGRESS:
2602                         AbortTransaction();
2603                         s->blockState = TBLOCK_ABORT;
2604                         /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2605                         break;
2606
2607                         /*
2608                          * Here, we failed while trying to COMMIT.      Clean up the
2609                          * transaction and return to idle state (we do not want to stay in
2610                          * the transaction).
2611                          */
2612                 case TBLOCK_END:
2613                         AbortTransaction();
2614                         CleanupTransaction();
2615                         s->blockState = TBLOCK_DEFAULT;
2616                         break;
2617
2618                         /*
2619                          * Here, we are already in an aborted transaction state and are
2620                          * waiting for a ROLLBACK, but for some reason we failed again! So
2621                          * we just remain in the abort state.
2622                          */
2623                 case TBLOCK_ABORT:
2624                 case TBLOCK_SUBABORT:
2625                         break;
2626
2627                         /*
2628                          * We are in a failed transaction and we got the ROLLBACK command.
2629                          * We have already aborted, we just need to cleanup and go to idle
2630                          * state.
2631                          */
2632                 case TBLOCK_ABORT_END:
2633                         CleanupTransaction();
2634                         s->blockState = TBLOCK_DEFAULT;
2635                         break;
2636
2637                         /*
2638                          * We are in a live transaction and we got a ROLLBACK command.
2639                          * Abort, cleanup, go to idle state.
2640                          */
2641                 case TBLOCK_ABORT_PENDING:
2642                         AbortTransaction();
2643                         CleanupTransaction();
2644                         s->blockState = TBLOCK_DEFAULT;
2645                         break;
2646
2647                         /*
2648                          * Here, we failed while trying to PREPARE.  Clean up the
2649                          * transaction and return to idle state (we do not want to stay in
2650                          * the transaction).
2651                          */
2652                 case TBLOCK_PREPARE:
2653                         AbortTransaction();
2654                         CleanupTransaction();
2655                         s->blockState = TBLOCK_DEFAULT;
2656                         break;
2657
2658                         /*
2659                          * We got an error inside a subtransaction.  Abort just the
2660                          * subtransaction, and go to the persistent SUBABORT state until
2661                          * we get ROLLBACK.
2662                          */
2663                 case TBLOCK_SUBINPROGRESS:
2664                         AbortSubTransaction();
2665                         s->blockState = TBLOCK_SUBABORT;
2666                         break;
2667
2668                         /*
2669                          * If we failed while trying to create a subtransaction, clean up
2670                          * the broken subtransaction and abort the parent.      The same
2671                          * applies if we get a failure while ending a subtransaction.
2672                          */
2673                 case TBLOCK_SUBBEGIN:
2674                 case TBLOCK_SUBEND:
2675                 case TBLOCK_SUBABORT_PENDING:
2676                 case TBLOCK_SUBRESTART:
2677                         AbortSubTransaction();
2678                         CleanupSubTransaction();
2679                         AbortCurrentTransaction();
2680                         break;
2681
2682                         /*
2683                          * Same as above, except the Abort() was already done.
2684                          */
2685                 case TBLOCK_SUBABORT_END:
2686                 case TBLOCK_SUBABORT_RESTART:
2687                         CleanupSubTransaction();
2688                         AbortCurrentTransaction();
2689                         break;
2690         }
2691 }
2692
2693 /*
2694  *      PreventTransactionChain
2695  *
2696  *      This routine is to be called by statements that must not run inside
2697  *      a transaction block, typically because they have non-rollback-able
2698  *      side effects or do internal commits.
2699  *
2700  *      If we have already started a transaction block, issue an error; also issue
2701  *      an error if we appear to be running inside a user-defined function (which
2702  *      could issue more commands and possibly cause a failure after the statement
2703  *      completes).  Subtransactions are verboten too.
2704  *
2705  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2706  *      inside a function or multi-query querystring.  (We will always fail if
2707  *      this is false, but it's convenient to centralize the check here instead of
2708  *      making callers do it.)
2709  *      stmtType: statement type name, for error messages.
2710  */
2711 void
2712 PreventTransactionChain(bool isTopLevel, const char *stmtType)
2713 {
2714         /*
2715          * xact block already started?
2716          */
2717         if (IsTransactionBlock())
2718                 ereport(ERROR,
2719                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2720                 /* translator: %s represents an SQL statement name */
2721                                  errmsg("%s cannot run inside a transaction block",
2722                                                 stmtType)));
2723
2724         /*
2725          * subtransaction?
2726          */
2727         if (IsSubTransaction())
2728                 ereport(ERROR,
2729                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2730                 /* translator: %s represents an SQL statement name */
2731                                  errmsg("%s cannot run inside a subtransaction",
2732                                                 stmtType)));
2733
2734         /*
2735          * inside a function call?
2736          */
2737         if (!isTopLevel)
2738                 ereport(ERROR,
2739                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2740                 /* translator: %s represents an SQL statement name */
2741                                  errmsg("%s cannot be executed from a function or multi-command string",
2742                                                 stmtType)));
2743
2744         /* If we got past IsTransactionBlock test, should be in default state */
2745         if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2746                 CurrentTransactionState->blockState != TBLOCK_STARTED)
2747                 elog(FATAL, "cannot prevent transaction chain");
2748         /* all okay */
2749 }
2750
2751 /*
2752  *      RequireTransactionChain
2753  *
2754  *      This routine is to be called by statements that must run inside
2755  *      a transaction block, because they have no effects that persist past
2756  *      transaction end (and so calling them outside a transaction block
2757  *      is presumably an error).  DECLARE CURSOR is an example.
2758  *
2759  *      If we appear to be running inside a user-defined function, we do not
2760  *      issue an error, since the function could issue more commands that make
2761  *      use of the current statement's results.  Likewise subtransactions.
2762  *      Thus this is an inverse for PreventTransactionChain.
2763  *
2764  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2765  *      inside a function.
2766  *      stmtType: statement type name, for error messages.
2767  */
2768 void
2769 RequireTransactionChain(bool isTopLevel, const char *stmtType)
2770 {
2771         /*
2772          * xact block already started?
2773          */
2774         if (IsTransactionBlock())
2775                 return;
2776
2777         /*
2778          * subtransaction?
2779          */
2780         if (IsSubTransaction())
2781                 return;
2782
2783         /*
2784          * inside a function call?
2785          */
2786         if (!isTopLevel)
2787                 return;
2788
2789         ereport(ERROR,
2790                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
2791         /* translator: %s represents an SQL statement name */
2792                          errmsg("%s can only be used in transaction blocks",
2793                                         stmtType)));
2794 }
2795
2796 /*
2797  *      IsInTransactionChain
2798  *
2799  *      This routine is for statements that need to behave differently inside
2800  *      a transaction block than when running as single commands.  ANALYZE is
2801  *      currently the only example.
2802  *
2803  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2804  *      inside a function.
2805  */
2806 bool
2807 IsInTransactionChain(bool isTopLevel)
2808 {
2809         /*
2810          * Return true on same conditions that would make PreventTransactionChain
2811          * error out
2812          */
2813         if (IsTransactionBlock())
2814                 return true;
2815
2816         if (IsSubTransaction())
2817                 return true;
2818
2819         if (!isTopLevel)
2820                 return true;
2821
2822         if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2823                 CurrentTransactionState->blockState != TBLOCK_STARTED)
2824                 return true;
2825
2826         return false;
2827 }
2828
2829
2830 /*
2831  * Register or deregister callback functions for start- and end-of-xact
2832  * operations.
2833  *
2834  * These functions are intended for use by dynamically loaded modules.
2835  * For built-in modules we generally just hardwire the appropriate calls
2836  * (mainly because it's easier to control the order that way, where needed).
2837  *
2838  * At transaction end, the callback occurs post-commit or post-abort, so the
2839  * callback functions can only do noncritical cleanup.
2840  */
2841 void
2842 RegisterXactCallback(XactCallback callback, void *arg)
2843 {
2844         XactCallbackItem *item;
2845
2846         item = (XactCallbackItem *)
2847                 MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
2848         item->callback = callback;
2849         item->arg = arg;
2850         item->next = Xact_callbacks;
2851         Xact_callbacks = item;
2852 }
2853
2854 void
2855 UnregisterXactCallback(XactCallback callback, void *arg)
2856 {
2857         XactCallbackItem *item;
2858         XactCallbackItem *prev;
2859
2860         prev = NULL;
2861         for (item = Xact_callbacks; item; prev = item, item = item->next)
2862         {
2863                 if (item->callback == callback && item->arg == arg)
2864                 {
2865                         if (prev)
2866                                 prev->next = item->next;
2867                         else
2868                                 Xact_callbacks = item->next;
2869                         pfree(item);
2870                         break;
2871                 }
2872         }
2873 }
2874
2875 static void
2876 CallXactCallbacks(XactEvent event)
2877 {
2878         XactCallbackItem *item;
2879
2880         for (item = Xact_callbacks; item; item = item->next)
2881                 (*item->callback) (event, item->arg);
2882 }
2883
2884
2885 /*
2886  * Register or deregister callback functions for start- and end-of-subxact
2887  * operations.
2888  *
2889  * Pretty much same as above, but for subtransaction events.
2890  *
2891  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
2892  * so the callback functions can only do noncritical cleanup.  At
2893  * subtransaction start, the callback is called when the subtransaction has
2894  * finished initializing.
2895  */
2896 void
2897 RegisterSubXactCallback(SubXactCallback callback, void *arg)
2898 {
2899         SubXactCallbackItem *item;
2900
2901         item = (SubXactCallbackItem *)
2902                 MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
2903         item->callback = callback;
2904         item->arg = arg;
2905         item->next = SubXact_callbacks;
2906         SubXact_callbacks = item;
2907 }
2908
2909 void
2910 UnregisterSubXactCallback(SubXactCallback callback, void *arg)
2911 {
2912         SubXactCallbackItem *item;
2913         SubXactCallbackItem *prev;
2914
2915         prev = NULL;
2916         for (item = SubXact_callbacks; item; prev = item, item = item->next)
2917         {
2918                 if (item->callback == callback && item->arg == arg)
2919                 {
2920                         if (prev)
2921                                 prev->next = item->next;
2922                         else
2923                                 SubXact_callbacks = item->next;
2924                         pfree(item);
2925                         break;
2926                 }
2927         }
2928 }
2929
2930 static void
2931 CallSubXactCallbacks(SubXactEvent event,
2932                                          SubTransactionId mySubid,
2933                                          SubTransactionId parentSubid)
2934 {
2935         SubXactCallbackItem *item;
2936
2937         for (item = SubXact_callbacks; item; item = item->next)
2938                 (*item->callback) (event, mySubid, parentSubid, item->arg);
2939 }
2940
2941
2942 /* ----------------------------------------------------------------
2943  *                                         transaction block support
2944  * ----------------------------------------------------------------
2945  */
2946
2947 /*
2948  *      BeginTransactionBlock
2949  *              This executes a BEGIN command.
2950  */
2951 void
2952 BeginTransactionBlock(void)
2953 {
2954         TransactionState s = CurrentTransactionState;
2955
2956         switch (s->blockState)
2957         {
2958                         /*
2959                          * We are not inside a transaction block, so allow one to begin.
2960                          */
2961                 case TBLOCK_STARTED:
2962                         s->blockState = TBLOCK_BEGIN;
2963                         break;
2964
2965                         /*
2966                          * Already a transaction block in progress.
2967                          */
2968                 case TBLOCK_INPROGRESS:
2969                 case TBLOCK_SUBINPROGRESS:
2970                 case TBLOCK_ABORT:
2971                 case TBLOCK_SUBABORT:
2972                         ereport(WARNING,
2973                                         (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2974                                          errmsg("there is already a transaction in progress")));
2975                         break;
2976
2977                         /* These cases are invalid. */
2978                 case TBLOCK_DEFAULT:
2979                 case TBLOCK_BEGIN:
2980                 case TBLOCK_SUBBEGIN:
2981                 case TBLOCK_END:
2982                 case TBLOCK_SUBEND:
2983                 case TBLOCK_ABORT_END:
2984                 case TBLOCK_SUBABORT_END:
2985                 case TBLOCK_ABORT_PENDING:
2986                 case TBLOCK_SUBABORT_PENDING:
2987                 case TBLOCK_SUBRESTART:
2988                 case TBLOCK_SUBABORT_RESTART:
2989                 case TBLOCK_PREPARE:
2990                         elog(FATAL, "BeginTransactionBlock: unexpected state %s",
2991                                  BlockStateAsString(s->blockState));
2992                         break;
2993         }
2994 }
2995
2996 /*
2997  *      PrepareTransactionBlock
2998  *              This executes a PREPARE command.
2999  *
3000  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3001  * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3002  *
3003  * Note that we don't actually do anything here except change blockState.
3004  * The real work will be done in the upcoming PrepareTransaction().
3005  * We do it this way because it's not convenient to change memory context,
3006  * resource owner, etc while executing inside a Portal.
3007  */
3008 bool
3009 PrepareTransactionBlock(char *gid)
3010 {
3011         TransactionState s;
3012         bool            result;
3013
3014         /* Set up to commit the current transaction */
3015         result = EndTransactionBlock();
3016
3017         /* If successful, change outer tblock state to PREPARE */
3018         if (result)
3019         {
3020                 s = CurrentTransactionState;
3021
3022                 while (s->parent != NULL)
3023                         s = s->parent;
3024
3025                 if (s->blockState == TBLOCK_END)
3026                 {
3027                         /* Save GID where PrepareTransaction can find it again */
3028                         prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
3029
3030                         s->blockState = TBLOCK_PREPARE;
3031                 }
3032                 else
3033                 {
3034                         /*
3035                          * ignore case where we are not in a transaction;
3036                          * EndTransactionBlock already issued a warning.
3037                          */
3038                         Assert(s->blockState == TBLOCK_STARTED);
3039                         /* Don't send back a PREPARE result tag... */
3040                         result = false;
3041                 }
3042         }
3043
3044         return result;
3045 }
3046
3047 /*
3048  *      EndTransactionBlock
3049  *              This executes a COMMIT command.
3050  *
3051  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3052  * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3053  *
3054  * Note that we don't actually do anything here except change blockState.
3055  * The real work will be done in the upcoming CommitTransactionCommand().
3056  * We do it this way because it's not convenient to change memory context,
3057  * resource owner, etc while executing inside a Portal.
3058  */
3059 bool
3060 EndTransactionBlock(void)
3061 {
3062         TransactionState s = CurrentTransactionState;
3063         bool            result = false;
3064
3065         switch (s->blockState)
3066         {
3067                         /*
3068                          * We are in a transaction block, so tell CommitTransactionCommand
3069                          * to COMMIT.
3070                          */
3071                 case TBLOCK_INPROGRESS:
3072                         s->blockState = TBLOCK_END;
3073                         result = true;
3074                         break;
3075
3076                         /*
3077                          * We are in a failed transaction block.  Tell
3078                          * CommitTransactionCommand it's time to exit the block.
3079                          */
3080                 case TBLOCK_ABORT:
3081                         s->blockState = TBLOCK_ABORT_END;
3082                         break;
3083
3084                         /*
3085                          * We are in a live subtransaction block.  Set up to subcommit all
3086                          * open subtransactions and then commit the main transaction.
3087                          */
3088                 case TBLOCK_SUBINPROGRESS:
3089                         while (s->parent != NULL)
3090                         {
3091                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3092                                         s->blockState = TBLOCK_SUBEND;
3093                                 else
3094                                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3095                                                  BlockStateAsString(s->blockState));
3096                                 s = s->parent;
3097                         }
3098                         if (s->blockState == TBLOCK_INPROGRESS)
3099                                 s->blockState = TBLOCK_END;
3100                         else
3101                                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3102                                          BlockStateAsString(s->blockState));
3103                         result = true;
3104                         break;
3105
3106                         /*
3107                          * Here we are inside an aborted subtransaction.  Treat the COMMIT
3108                          * as ROLLBACK: set up to abort everything and exit the main
3109                          * transaction.
3110                          */
3111                 case TBLOCK_SUBABORT:
3112                         while (s->parent != NULL)
3113                         {
3114                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3115                                         s->blockState = TBLOCK_SUBABORT_PENDING;
3116                                 else if (s->blockState == TBLOCK_SUBABORT)
3117                                         s->blockState = TBLOCK_SUBABORT_END;
3118                                 else
3119                                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3120                                                  BlockStateAsString(s->blockState));
3121                                 s = s->parent;
3122                         }
3123                         if (s->blockState == TBLOCK_INPROGRESS)
3124                                 s->blockState = TBLOCK_ABORT_PENDING;
3125                         else if (s->blockState == TBLOCK_ABORT)
3126                                 s->blockState = TBLOCK_ABORT_END;
3127                         else
3128                                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3129                                          BlockStateAsString(s->blockState));
3130                         break;
3131
3132                         /*
3133                          * The user issued COMMIT when not inside a transaction.  Issue a
3134                          * WARNING, staying in TBLOCK_STARTED state.  The upcoming call to
3135                          * CommitTransactionCommand() will then close the transaction and
3136                          * put us back into the default state.
3137                          */
3138                 case TBLOCK_STARTED:
3139                         ereport(WARNING,
3140                                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3141                                          errmsg("there is no transaction in progress")));
3142                         result = true;
3143                         break;
3144
3145                         /* These cases are invalid. */
3146                 case TBLOCK_DEFAULT:
3147                 case TBLOCK_BEGIN:
3148                 case TBLOCK_SUBBEGIN:
3149                 case TBLOCK_END:
3150                 case TBLOCK_SUBEND:
3151                 case TBLOCK_ABORT_END:
3152                 case TBLOCK_SUBABORT_END:
3153                 case TBLOCK_ABORT_PENDING:
3154                 case TBLOCK_SUBABORT_PENDING:
3155                 case TBLOCK_SUBRESTART:
3156                 case TBLOCK_SUBABORT_RESTART:
3157                 case TBLOCK_PREPARE:
3158                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3159                                  BlockStateAsString(s->blockState));
3160                         break;
3161         }
3162
3163         return result;
3164 }
3165
3166 /*
3167  *      UserAbortTransactionBlock
3168  *              This executes a ROLLBACK command.
3169  *
3170  * As above, we don't actually do anything here except change blockState.
3171  */
3172 void
3173 UserAbortTransactionBlock(void)
3174 {
3175         TransactionState s = CurrentTransactionState;
3176
3177         switch (s->blockState)
3178         {
3179                         /*
3180                          * We are inside a transaction block and we got a ROLLBACK command
3181                          * from the user, so tell CommitTransactionCommand to abort and
3182                          * exit the transaction block.
3183                          */
3184                 case TBLOCK_INPROGRESS:
3185                         s->blockState = TBLOCK_ABORT_PENDING;
3186                         break;
3187
3188                         /*
3189                          * We are inside a failed transaction block and we got a ROLLBACK
3190                          * command from the user.  Abort processing is already done, so
3191                          * CommitTransactionCommand just has to cleanup and go back to
3192                          * idle state.
3193                          */
3194                 case TBLOCK_ABORT:
3195                         s->blockState = TBLOCK_ABORT_END;
3196                         break;
3197
3198                         /*
3199                          * We are inside a subtransaction.      Mark everything up to top
3200                          * level as exitable.
3201                          */
3202                 case TBLOCK_SUBINPROGRESS:
3203                 case TBLOCK_SUBABORT:
3204                         while (s->parent != NULL)
3205                         {
3206                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3207                                         s->blockState = TBLOCK_SUBABORT_PENDING;
3208                                 else if (s->blockState == TBLOCK_SUBABORT)
3209                                         s->blockState = TBLOCK_SUBABORT_END;
3210                                 else
3211                                         elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3212                                                  BlockStateAsString(s->blockState));
3213                                 s = s->parent;
3214                         }
3215                         if (s->blockState == TBLOCK_INPROGRESS)
3216                                 s->blockState = TBLOCK_ABORT_PENDING;
3217                         else if (s->blockState == TBLOCK_ABORT)
3218                                 s->blockState = TBLOCK_ABORT_END;
3219                         else
3220                                 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3221                                          BlockStateAsString(s->blockState));
3222                         break;
3223
3224                         /*
3225                          * The user issued ABORT when not inside a transaction. Issue a
3226                          * WARNING and go to abort state.  The upcoming call to
3227                          * CommitTransactionCommand() will then put us back into the
3228                          * default state.
3229                          */
3230                 case TBLOCK_STARTED:
3231                         ereport(NOTICE,
3232                                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3233                                          errmsg("there is no transaction in progress")));
3234                         s->blockState = TBLOCK_ABORT_PENDING;
3235                         break;
3236
3237                         /* These cases are invalid. */
3238                 case TBLOCK_DEFAULT:
3239                 case TBLOCK_BEGIN:
3240                 case TBLOCK_SUBBEGIN:
3241                 case TBLOCK_END:
3242                 case TBLOCK_SUBEND:
3243                 case TBLOCK_ABORT_END:
3244                 case TBLOCK_SUBABORT_END:
3245                 case TBLOCK_ABORT_PENDING:
3246                 case TBLOCK_SUBABORT_PENDING:
3247                 case TBLOCK_SUBRESTART:
3248                 case TBLOCK_SUBABORT_RESTART:
3249                 case TBLOCK_PREPARE:
3250                         elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3251                                  BlockStateAsString(s->blockState));
3252                         break;
3253         }
3254 }
3255
3256 /*
3257  * DefineSavepoint
3258  *              This executes a SAVEPOINT command.
3259  */
3260 void
3261 DefineSavepoint(char *name)
3262 {
3263         TransactionState s = CurrentTransactionState;
3264
3265         switch (s->blockState)
3266         {
3267                 case TBLOCK_INPROGRESS:
3268                 case TBLOCK_SUBINPROGRESS:
3269                         /* Normal subtransaction start */
3270                         PushTransaction();
3271                         s = CurrentTransactionState;            /* changed by push */
3272
3273                         /*
3274                          * Savepoint names, like the TransactionState block itself, live
3275                          * in TopTransactionContext.
3276                          */
3277                         if (name)
3278                                 s->name = MemoryContextStrdup(TopTransactionContext, name);
3279                         break;
3280
3281                         /* These cases are invalid. */
3282                 case TBLOCK_DEFAULT:
3283                 case TBLOCK_STARTED:
3284                 case TBLOCK_BEGIN:
3285                 case TBLOCK_SUBBEGIN:
3286                 case TBLOCK_END:
3287                 case TBLOCK_SUBEND:
3288                 case TBLOCK_ABORT:
3289                 case TBLOCK_SUBABORT:
3290                 case TBLOCK_ABORT_END:
3291                 case TBLOCK_SUBABORT_END:
3292                 case TBLOCK_ABORT_PENDING:
3293                 case TBLOCK_SUBABORT_PENDING:
3294                 case TBLOCK_SUBRESTART:
3295                 case TBLOCK_SUBABORT_RESTART:
3296                 case TBLOCK_PREPARE:
3297                         elog(FATAL, "DefineSavepoint: unexpected state %s",
3298                                  BlockStateAsString(s->blockState));
3299                         break;
3300         }
3301 }
3302
3303 /*
3304  * ReleaseSavepoint
3305  *              This executes a RELEASE command.
3306  *
3307  * As above, we don't actually do anything here except change blockState.
3308  */
3309 void
3310 ReleaseSavepoint(List *options)
3311 {
3312         TransactionState s = CurrentTransactionState;
3313         TransactionState target,
3314                                 xact;
3315         ListCell   *cell;
3316         char       *name = NULL;
3317
3318         switch (s->blockState)
3319         {
3320                         /*
3321                          * We can't rollback to a savepoint if there is no savepoint
3322                          * defined.
3323                          */
3324                 case TBLOCK_INPROGRESS:
3325                         ereport(ERROR,
3326                                         (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3327                                          errmsg("no such savepoint")));
3328                         break;
3329
3330                         /*
3331                          * We are in a non-aborted subtransaction.      This is the only valid
3332                          * case.
3333                          */
3334                 case TBLOCK_SUBINPROGRESS:
3335                         break;
3336
3337                         /* These cases are invalid. */
3338                 case TBLOCK_DEFAULT:
3339                 case TBLOCK_STARTED:
3340                 case TBLOCK_BEGIN:
3341                 case TBLOCK_SUBBEGIN:
3342                 case TBLOCK_END:
3343                 case TBLOCK_SUBEND:
3344                 case TBLOCK_ABORT:
3345                 case TBLOCK_SUBABORT:
3346                 case TBLOCK_ABORT_END:
3347                 case TBLOCK_SUBABORT_END:
3348                 case TBLOCK_ABORT_PENDING:
3349                 case TBLOCK_SUBABORT_PENDING:
3350                 case TBLOCK_SUBRESTART:
3351                 case TBLOCK_SUBABORT_RESTART:
3352                 case TBLOCK_PREPARE:
3353                         elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3354                                  BlockStateAsString(s->blockState));
3355                         break;
3356         }
3357
3358         foreach(cell, options)
3359         {
3360                 DefElem    *elem = lfirst(cell);
3361
3362                 if (strcmp(elem->defname, "savepoint_name") == 0)
3363                         name = strVal(elem->arg);
3364         }
3365
3366         Assert(PointerIsValid(name));
3367
3368         for (target = s; PointerIsValid(target); target = target->parent)
3369         {
3370                 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3371                         break;
3372         }
3373
3374         if (!PointerIsValid(target))
3375                 ereport(ERROR,
3376                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3377                                  errmsg("no such savepoint")));
3378
3379         /* disallow crossing savepoint level boundaries */
3380         if (target->savepointLevel != s->savepointLevel)
3381                 ereport(ERROR,
3382                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3383                                  errmsg("no such savepoint")));
3384
3385         /*
3386          * Mark "commit pending" all subtransactions up to the target
3387          * subtransaction.      The actual commits will happen when control gets to
3388          * CommitTransactionCommand.
3389          */
3390         xact = CurrentTransactionState;
3391         for (;;)
3392         {
3393                 Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
3394                 xact->blockState = TBLOCK_SUBEND;
3395                 if (xact == target)
3396                         break;
3397                 xact = xact->parent;
3398                 Assert(PointerIsValid(xact));
3399         }
3400 }
3401
3402 /*
3403  * RollbackToSavepoint
3404  *              This executes a ROLLBACK TO <savepoint> command.
3405  *
3406  * As above, we don't actually do anything here except change blockState.
3407  */
3408 void
3409 RollbackToSavepoint(List *options)
3410 {
3411         TransactionState s = CurrentTransactionState;
3412         TransactionState target,
3413                                 xact;
3414         ListCell   *cell;
3415         char       *name = NULL;
3416
3417         switch (s->blockState)
3418         {
3419                         /*
3420                          * We can't rollback to a savepoint if there is no savepoint
3421                          * defined.
3422                          */
3423                 case TBLOCK_INPROGRESS:
3424                 case TBLOCK_ABORT:
3425                         ereport(ERROR,
3426                                         (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3427                                          errmsg("no such savepoint")));
3428                         break;
3429
3430                         /*
3431                          * There is at least one savepoint, so proceed.
3432                          */
3433                 case TBLOCK_SUBINPROGRESS:
3434                 case TBLOCK_SUBABORT:
3435                         break;
3436
3437                         /* These cases are invalid. */
3438                 case TBLOCK_DEFAULT:
3439                 case TBLOCK_STARTED:
3440                 case TBLOCK_BEGIN:
3441                 case TBLOCK_SUBBEGIN:
3442                 case TBLOCK_END:
3443                 case TBLOCK_SUBEND:
3444                 case TBLOCK_ABORT_END:
3445                 case TBLOCK_SUBABORT_END:
3446                 case TBLOCK_ABORT_PENDING:
3447                 case TBLOCK_SUBABORT_PENDING:
3448                 case TBLOCK_SUBRESTART:
3449                 case TBLOCK_SUBABORT_RESTART:
3450                 case TBLOCK_PREPARE:
3451                         elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3452                                  BlockStateAsString(s->blockState));
3453                         break;
3454         }
3455
3456         foreach(cell, options)
3457         {
3458                 DefElem    *elem = lfirst(cell);
3459
3460                 if (strcmp(elem->defname, "savepoint_name") == 0)
3461                         name = strVal(elem->arg);
3462         }
3463
3464         Assert(PointerIsValid(name));
3465
3466         for (target = s; PointerIsValid(target); target = target->parent)
3467         {
3468                 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3469                         break;
3470         }
3471
3472         if (!PointerIsValid(target))
3473                 ereport(ERROR,
3474                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3475                                  errmsg("no such savepoint")));
3476
3477         /* disallow crossing savepoint level boundaries */
3478         if (target->savepointLevel != s->savepointLevel)
3479                 ereport(ERROR,
3480                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3481                                  errmsg("no such savepoint")));
3482
3483         /*
3484          * Mark "abort pending" all subtransactions up to the target
3485          * subtransaction.      The actual aborts will happen when control gets to
3486          * CommitTransactionCommand.
3487          */
3488         xact = CurrentTransactionState;
3489         for (;;)
3490         {
3491                 if (xact == target)
3492                         break;
3493                 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3494                         xact->blockState = TBLOCK_SUBABORT_PENDING;
3495                 else if (xact->blockState == TBLOCK_SUBABORT)
3496                         xact->blockState = TBLOCK_SUBABORT_END;
3497                 else
3498                         elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3499                                  BlockStateAsString(xact->blockState));
3500                 xact = xact->parent;
3501                 Assert(PointerIsValid(xact));
3502         }
3503
3504         /* And mark the target as "restart pending" */
3505         if (xact->blockState == TBLOCK_SUBINPROGRESS)
3506                 xact->blockState = TBLOCK_SUBRESTART;
3507         else if (xact->blockState == TBLOCK_SUBABORT)
3508                 xact->blockState = TBLOCK_SUBABORT_RESTART;
3509         else
3510                 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3511                          BlockStateAsString(xact->blockState));
3512 }
3513
3514 /*
3515  * BeginInternalSubTransaction
3516  *              This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
3517  *              TBLOCK_END, and TBLOCK_PREPARE states, and therefore it can safely be
3518  *              used in functions that might be called when not inside a BEGIN block
3519  *              or when running deferred triggers at COMMIT/PREPARE time.  Also, it
3520  *              automatically does CommitTransactionCommand/StartTransactionCommand
3521  *              instead of expecting the caller to do it.
3522  */
3523 void
3524 BeginInternalSubTransaction(char *name)
3525 {
3526         TransactionState s = CurrentTransactionState;
3527
3528         switch (s->blockState)
3529         {
3530                 case TBLOCK_STARTED:
3531                 case TBLOCK_INPROGRESS:
3532                 case TBLOCK_END:
3533                 case TBLOCK_PREPARE:
3534                 case TBLOCK_SUBINPROGRESS:
3535                         /* Normal subtransaction start */
3536                         PushTransaction();
3537                         s = CurrentTransactionState;            /* changed by push */
3538
3539                         /*
3540                          * Savepoint names, like the TransactionState block itself, live
3541                          * in TopTransactionContext.
3542                          */
3543                         if (name)
3544                                 s->name = MemoryContextStrdup(TopTransactionContext, name);
3545                         break;
3546
3547                         /* These cases are invalid. */
3548                 case TBLOCK_DEFAULT:
3549                 case TBLOCK_BEGIN:
3550                 case TBLOCK_SUBBEGIN:
3551                 case TBLOCK_SUBEND:
3552                 case TBLOCK_ABORT:
3553                 case TBLOCK_SUBABORT:
3554                 case TBLOCK_ABORT_END:
3555                 case TBLOCK_SUBABORT_END:
3556                 case TBLOCK_ABORT_PENDING:
3557                 case TBLOCK_SUBABORT_PENDING:
3558                 case TBLOCK_SUBRESTART:
3559                 case TBLOCK_SUBABORT_RESTART:
3560                         elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
3561                                  BlockStateAsString(s->blockState));
3562                         break;
3563         }
3564
3565         CommitTransactionCommand();
3566         StartTransactionCommand();
3567 }
3568
3569 /*
3570  * ReleaseCurrentSubTransaction
3571  *
3572  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
3573  * savepoint name (if any).
3574  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3575  */
3576 void
3577 ReleaseCurrentSubTransaction(void)
3578 {
3579         TransactionState s = CurrentTransactionState;
3580
3581         if (s->blockState != TBLOCK_SUBINPROGRESS)
3582                 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
3583                          BlockStateAsString(s->blockState));
3584         Assert(s->state == TRANS_INPROGRESS);
3585         MemoryContextSwitchTo(CurTransactionContext);
3586         CommitSubTransaction();
3587         s = CurrentTransactionState;    /* changed by pop */
3588         Assert(s->state == TRANS_INPROGRESS);
3589 }
3590
3591 /*
3592  * RollbackAndReleaseCurrentSubTransaction
3593  *
3594  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
3595  * of its savepoint name (if any).
3596  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3597  */
3598 void
3599 RollbackAndReleaseCurrentSubTransaction(void)
3600 {
3601         TransactionState s = CurrentTransactionState;
3602
3603         switch (s->blockState)
3604         {
3605                         /* Must be in a subtransaction */
3606                 case TBLOCK_SUBINPROGRESS:
3607                 case TBLOCK_SUBABORT:
3608                         break;
3609
3610                         /* These cases are invalid. */
3611                 case TBLOCK_DEFAULT:
3612                 case TBLOCK_STARTED:
3613                 case TBLOCK_BEGIN:
3614                 case TBLOCK_SUBBEGIN:
3615                 case TBLOCK_INPROGRESS:
3616                 case TBLOCK_END:
3617                 case TBLOCK_SUBEND:
3618                 case TBLOCK_ABORT:
3619                 case TBLOCK_ABORT_END:
3620                 case TBLOCK_SUBABORT_END:
3621                 case TBLOCK_ABORT_PENDING:
3622                 case TBLOCK_SUBABORT_PENDING:
3623                 case TBLOCK_SUBRESTART:
3624                 case TBLOCK_SUBABORT_RESTART:
3625                 case TBLOCK_PREPARE:
3626                         elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
3627                                  BlockStateAsString(s->blockState));
3628                         break;
3629         }
3630
3631         /*
3632          * Abort the current subtransaction, if needed.
3633          */
3634         if (s->blockState == TBLOCK_SUBINPROGRESS)
3635                 AbortSubTransaction();
3636
3637         /* And clean it up, too */
3638         CleanupSubTransaction();
3639
3640         s = CurrentTransactionState;    /* changed by pop */
3641         AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
3642                                 s->blockState == TBLOCK_INPROGRESS ||
3643                                 s->blockState == TBLOCK_STARTED);
3644 }
3645
3646 /*
3647  *      AbortOutOfAnyTransaction
3648  *
3649  *      This routine is provided for error recovery purposes.  It aborts any
3650  *      active transaction or transaction block, leaving the system in a known
3651  *      idle state.
3652  */
3653 void
3654 AbortOutOfAnyTransaction(void)
3655 {
3656         TransactionState s = CurrentTransactionState;
3657
3658         /*
3659          * Get out of any transaction or nested transaction
3660          */
3661         do
3662         {
3663                 switch (s->blockState)
3664                 {
3665                         case TBLOCK_DEFAULT:
3666                                 /* Not in a transaction, do nothing */
3667                                 break;
3668                         case TBLOCK_STARTED:
3669                         case TBLOCK_BEGIN:
3670                         case TBLOCK_INPROGRESS:
3671                         case TBLOCK_END:
3672                         case TBLOCK_ABORT_PENDING:
3673                         case TBLOCK_PREPARE:
3674                                 /* In a transaction, so clean up */
3675                                 AbortTransaction();
3676                                 CleanupTransaction();
3677                                 s->blockState = TBLOCK_DEFAULT;
3678                                 break;
3679                         case TBLOCK_ABORT:
3680                         case TBLOCK_ABORT_END:
3681                                 /* AbortTransaction already done, still need Cleanup */
3682                                 CleanupTransaction();
3683                                 s->blockState = TBLOCK_DEFAULT;
3684                                 break;
3685
3686                                 /*
3687                                  * In a subtransaction, so clean it up and abort parent too
3688                                  */
3689                         case TBLOCK_SUBBEGIN:
3690                         case TBLOCK_SUBINPROGRESS:
3691                         case TBLOCK_SUBEND:
3692                         case TBLOCK_SUBABORT_PENDING:
3693                         case TBLOCK_SUBRESTART:
3694                                 AbortSubTransaction();
3695                                 CleanupSubTransaction();
3696                                 s = CurrentTransactionState;    /* changed by pop */
3697                                 break;
3698
3699                         case TBLOCK_SUBABORT:
3700                         case TBLOCK_SUBABORT_END:
3701                         case TBLOCK_SUBABORT_RESTART:
3702                                 /* As above, but AbortSubTransaction already done */
3703                                 CleanupSubTransaction();
3704                                 s = CurrentTransactionState;    /* changed by pop */
3705                                 break;
3706                 }
3707         } while (s->blockState != TBLOCK_DEFAULT);
3708
3709         /* Should be out of all subxacts now */
3710         Assert(s->parent == NULL);
3711 }
3712
3713 /*
3714  * IsTransactionBlock --- are we within a transaction block?
3715  */
3716 bool
3717 IsTransactionBlock(void)
3718 {
3719         TransactionState s = CurrentTransactionState;
3720
3721         if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3722                 return false;
3723
3724         return true;
3725 }
3726
3727 /*
3728  * IsTransactionOrTransactionBlock --- are we within either a transaction
3729  * or a transaction block?      (The backend is only really "idle" when this
3730  * returns false.)
3731  *
3732  * This should match up with IsTransactionBlock and IsTransactionState.
3733  */
3734 bool
3735 IsTransactionOrTransactionBlock(void)
3736 {
3737         TransactionState s = CurrentTransactionState;
3738
3739         if (s->blockState == TBLOCK_DEFAULT)
3740                 return false;
3741
3742         return true;
3743 }
3744
3745 /*
3746  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
3747  */
3748 char
3749 TransactionBlockStatusCode(void)
3750 {
3751         TransactionState s = CurrentTransactionState;
3752
3753         switch (s->blockState)
3754         {
3755                 case TBLOCK_DEFAULT:
3756                 case TBLOCK_STARTED:
3757                         return 'I';                     /* idle --- not in transaction */
3758                 case TBLOCK_BEGIN:
3759                 case TBLOCK_SUBBEGIN:
3760                 case TBLOCK_INPROGRESS:
3761                 case TBLOCK_SUBINPROGRESS:
3762                 case TBLOCK_END:
3763                 case TBLOCK_SUBEND:
3764                 case TBLOCK_PREPARE:
3765                         return 'T';                     /* in transaction */
3766                 case TBLOCK_ABORT:
3767                 case TBLOCK_SUBABORT:
3768                 case TBLOCK_ABORT_END:
3769                 case TBLOCK_SUBABORT_END:
3770                 case TBLOCK_ABORT_PENDING:
3771                 case TBLOCK_SUBABORT_PENDING:
3772                 case TBLOCK_SUBRESTART:
3773                 case TBLOCK_SUBABORT_RESTART:
3774                         return 'E';                     /* in failed transaction */
3775         }
3776
3777         /* should never get here */
3778         elog(FATAL, "invalid transaction block state: %s",
3779                  BlockStateAsString(s->blockState));
3780         return 0;                                       /* keep compiler quiet */
3781 }
3782
3783 /*
3784  * IsSubTransaction
3785  */
3786 bool
3787 IsSubTransaction(void)
3788 {
3789         TransactionState s = CurrentTransactionState;
3790
3791         if (s->nestingLevel >= 2)
3792                 return true;
3793
3794         return false;
3795 }
3796
3797 /*
3798  * StartSubTransaction
3799  *
3800  * If you're wondering why this is separate from PushTransaction: it's because
3801  * we can't conveniently do this stuff right inside DefineSavepoint.  The
3802  * SAVEPOINT utility command will be executed inside a Portal, and if we
3803  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
3804  * the Portal will undo those settings.  So we make DefineSavepoint just
3805  * push a dummy transaction block, and when control returns to the main
3806  * idle loop, CommitTransactionCommand will be called, and we'll come here
3807  * to finish starting the subtransaction.
3808  */
3809 static void
3810 StartSubTransaction(void)
3811 {
3812         TransactionState s = CurrentTransactionState;
3813
3814         if (s->state != TRANS_DEFAULT)
3815                 elog(WARNING, "StartSubTransaction while in %s state",
3816                          TransStateAsString(s->state));
3817
3818         s->state = TRANS_START;
3819
3820         /*
3821          * Initialize subsystems for new subtransaction
3822          *
3823          * must initialize resource-management stuff first
3824          */
3825         AtSubStart_Memory();
3826         AtSubStart_ResourceOwner();
3827         AtSubStart_Inval();
3828         AtSubStart_Notify();
3829         AfterTriggerBeginSubXact();
3830
3831         s->state = TRANS_INPROGRESS;
3832
3833         /*
3834          * Call start-of-subxact callbacks
3835          */
3836         CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
3837                                                  s->parent->subTransactionId);
3838
3839         ShowTransactionState("StartSubTransaction");
3840 }
3841
3842 /*
3843  * CommitSubTransaction
3844  *
3845  *      The caller has to make sure to always reassign CurrentTransactionState
3846  *      if it has a local pointer to it after calling this function.
3847  */
3848 static void
3849 CommitSubTransaction(void)
3850 {
3851         TransactionState s = CurrentTransactionState;
3852
3853         ShowTransactionState("CommitSubTransaction");
3854
3855         if (s->state != TRANS_INPROGRESS)
3856                 elog(WARNING, "CommitSubTransaction while in %s state",
3857                          TransStateAsString(s->state));
3858
3859         /* Pre-commit processing goes here -- nothing to do at the moment */
3860
3861         s->state = TRANS_COMMIT;
3862
3863         /* Must CCI to ensure commands of subtransaction are seen as done */
3864         CommandCounterIncrement();
3865
3866         /*
3867          * Prior to 8.4 we marked subcommit in clog at this point.      We now only
3868          * perform that step, if required, as part of the atomic update of the
3869          * whole transaction tree at top level commit or abort.
3870          */
3871
3872         /* Post-commit cleanup */
3873         if (TransactionIdIsValid(s->transactionId))
3874                 AtSubCommit_childXids();
3875         AfterTriggerEndSubXact(true);
3876         AtSubCommit_Portals(s->subTransactionId,
3877                                                 s->parent->subTransactionId,
3878                                                 s->parent->curTransactionOwner);
3879         AtEOSubXact_LargeObject(true, s->subTransactionId,
3880                                                         s->parent->subTransactionId);
3881         AtSubCommit_Notify();
3882
3883         CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
3884                                                  s->parent->subTransactionId);
3885
3886         ResourceOwnerRelease(s->curTransactionOwner,
3887                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
3888                                                  true, false);
3889         AtEOSubXact_RelationCache(true, s->subTransactionId,
3890                                                           s->parent->subTransactionId);
3891         AtEOSubXact_Inval(true);
3892         AtSubCommit_smgr();
3893
3894         /*
3895          * The only lock we actually release here is the subtransaction XID lock.
3896          * The rest just get transferred to the parent resource owner.
3897          */
3898         CurrentResourceOwner = s->curTransactionOwner;
3899         if (TransactionIdIsValid(s->transactionId))
3900                 XactLockTableDelete(s->transactionId);
3901
3902         ResourceOwnerRelease(s->curTransactionOwner,
3903                                                  RESOURCE_RELEASE_LOCKS,
3904                                                  true, false);
3905         ResourceOwnerRelease(s->curTransactionOwner,
3906                                                  RESOURCE_RELEASE_AFTER_LOCKS,
3907                                                  true, false);
3908
3909         AtEOXact_GUC(true, s->gucNestLevel);
3910         AtEOSubXact_SPI(true, s->subTransactionId);
3911         AtEOSubXact_on_commit_actions(true, s->subTransactionId,
3912                                                                   s->parent->subTransactionId);
3913         AtEOSubXact_Namespace(true, s->subTransactionId,
3914                                                   s->parent->subTransactionId);
3915         AtEOSubXact_Files(true, s->subTransactionId,
3916                                           s->parent->subTransactionId);
3917         AtEOSubXact_HashTables(true, s->nestingLevel);
3918         AtEOSubXact_PgStat(true, s->nestingLevel);
3919         AtSubCommit_Snapshot(s->nestingLevel);
3920
3921         /*
3922          * We need to restore the upper transaction's read-only state, in case the
3923          * upper is read-write while the child is read-only; GUC will incorrectly
3924          * think it should leave the child state in place.
3925          */
3926         XactReadOnly = s->prevXactReadOnly;
3927
3928         CurrentResourceOwner = s->parent->curTransactionOwner;
3929         CurTransactionResourceOwner = s->parent->curTransactionOwner;
3930         ResourceOwnerDelete(s->curTransactionOwner);
3931         s->curTransactionOwner = NULL;
3932
3933         AtSubCommit_Memory();
3934
3935         s->state = TRANS_DEFAULT;
3936
3937         PopTransaction();
3938 }
3939
3940 /*
3941  * AbortSubTransaction
3942  */
3943 static void
3944 AbortSubTransaction(void)
3945 {
3946         TransactionState s = CurrentTransactionState;
3947
3948         /* Prevent cancel/die interrupt while cleaning up */
3949         HOLD_INTERRUPTS();
3950
3951         /* Make sure we have a valid memory context and resource owner */
3952         AtSubAbort_Memory();
3953         AtSubAbort_ResourceOwner();
3954
3955         /*
3956          * Release any LW locks we might be holding as quickly as possible.
3957          * (Regular locks, however, must be held till we finish aborting.)
3958          * Releasing LW locks is critical since we might try to grab them again
3959          * while cleaning up!
3960          *
3961          * FIXME This may be incorrect --- Are there some locks we should keep?
3962          * Buffer locks, for example?  I don't think so but I'm not sure.
3963          */
3964         LWLockReleaseAll();
3965
3966         AbortBufferIO();
3967         UnlockBuffers();
3968
3969         LockWaitCancel();
3970
3971         /*
3972          * check the current transaction state
3973          */
3974         ShowTransactionState("AbortSubTransaction");
3975
3976         if (s->state != TRANS_INPROGRESS)
3977                 elog(WARNING, "AbortSubTransaction while in %s state",
3978                          TransStateAsString(s->state));
3979
3980         s->state = TRANS_ABORT;
3981
3982         /*
3983          * Reset user ID which might have been changed transiently.  (See notes in
3984          * AbortTransaction.)
3985          */
3986         SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
3987
3988         /*
3989          * We can skip all this stuff if the subxact failed before creating a
3990          * ResourceOwner...
3991          */
3992         if (s->curTransactionOwner)
3993         {
3994                 AfterTriggerEndSubXact(false);
3995                 AtSubAbort_Portals(s->subTransactionId,
3996                                                    s->parent->subTransactionId,
3997                                                    s->parent->curTransactionOwner);
3998                 AtEOSubXact_LargeObject(false, s->subTransactionId,
3999                                                                 s->parent->subTransactionId);
4000                 AtSubAbort_Notify();
4001
4002                 /* Advertise the fact that we aborted in pg_clog. */
4003                 (void) RecordTransactionAbort(true);
4004
4005                 /* Post-abort cleanup */
4006                 if (TransactionIdIsValid(s->transactionId))
4007                         AtSubAbort_childXids();
4008
4009                 CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
4010                                                          s->parent->subTransactionId);
4011
4012                 ResourceOwnerRelease(s->curTransactionOwner,
4013                                                          RESOURCE_RELEASE_BEFORE_LOCKS,
4014                                                          false, false);
4015                 AtEOSubXact_RelationCache(false, s->subTransactionId,
4016                                                                   s->parent->subTransactionId);
4017                 AtEOSubXact_Inval(false);
4018                 AtSubAbort_smgr();
4019                 ResourceOwnerRelease(s->curTransactionOwner,
4020                                                          RESOURCE_RELEASE_LOCKS,
4021                                                          false, false);
4022                 ResourceOwnerRelease(s->curTransactionOwner,
4023                                                          RESOURCE_RELEASE_AFTER_LOCKS,
4024                                                          false, false);
4025
4026                 AtEOXact_GUC(false, s->gucNestLevel);
4027                 AtEOSubXact_SPI(false, s->subTransactionId);
4028                 AtEOSubXact_on_commit_actions(false, s->subTransactionId,
4029                                                                           s->parent->subTransactionId);
4030                 AtEOSubXact_Namespace(false, s->subTransactionId,
4031                                                           s->parent->subTransactionId);
4032                 AtEOSubXact_Files(false, s->subTransactionId,
4033                                                   s->parent->subTransactionId);
4034                 AtEOSubXact_HashTables(false, s->nestingLevel);
4035                 AtEOSubXact_PgStat(false, s->nestingLevel);
4036                 AtSubAbort_Snapshot(s->nestingLevel);
4037         }
4038
4039         /*
4040          * Restore the upper transaction's read-only state, too.  This should be
4041          * redundant with GUC's cleanup but we may as well do it for consistency
4042          * with the commit case.
4043          */
4044         XactReadOnly = s->prevXactReadOnly;
4045
4046         RESUME_INTERRUPTS();
4047 }
4048
4049 /*
4050  * CleanupSubTransaction
4051  *
4052  *      The caller has to make sure to always reassign CurrentTransactionState
4053  *      if it has a local pointer to it after calling this function.
4054  */
4055 static void
4056 CleanupSubTransaction(void)
4057 {
4058         TransactionState s = CurrentTransactionState;
4059
4060         ShowTransactionState("CleanupSubTransaction");
4061
4062         if (s->state != TRANS_ABORT)
4063                 elog(WARNING, "CleanupSubTransaction while in %s state",
4064                          TransStateAsString(s->state));
4065
4066         AtSubCleanup_Portals(s->subTransactionId);
4067
4068         CurrentResourceOwner = s->parent->curTransactionOwner;
4069         CurTransactionResourceOwner = s->parent->curTransactionOwner;
4070         if (s->curTransactionOwner)
4071                 ResourceOwnerDelete(s->curTransactionOwner);
4072         s->curTransactionOwner = NULL;
4073
4074         AtSubCleanup_Memory();
4075
4076         s->state = TRANS_DEFAULT;
4077
4078         PopTransaction();
4079 }
4080
4081 /*
4082  * PushTransaction
4083  *              Create transaction state stack entry for a subtransaction
4084  *
4085  *      The caller has to make sure to always reassign CurrentTransactionState
4086  *      if it has a local pointer to it after calling this function.
4087  */
4088 static void
4089 PushTransaction(void)
4090 {
4091         TransactionState p = CurrentTransactionState;
4092         TransactionState s;
4093
4094         /*
4095          * We keep subtransaction state nodes in TopTransactionContext.
4096          */
4097         s = (TransactionState)
4098                 MemoryContextAllocZero(TopTransactionContext,
4099                                                            sizeof(TransactionStateData));
4100
4101         /*
4102          * Assign a subtransaction ID, watching out for counter wraparound.
4103          */
4104         currentSubTransactionId += 1;
4105         if (currentSubTransactionId == InvalidSubTransactionId)
4106         {
4107                 currentSubTransactionId -= 1;
4108                 pfree(s);
4109                 ereport(ERROR,
4110                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4111                                  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4112         }
4113
4114         /*
4115          * We can now stack a minimally valid subtransaction without fear of
4116          * failure.
4117          */
4118         s->transactionId = InvalidTransactionId;        /* until assigned */
4119         s->subTransactionId = currentSubTransactionId;
4120         s->parent = p;
4121         s->nestingLevel = p->nestingLevel + 1;
4122         s->gucNestLevel = NewGUCNestLevel();
4123         s->savepointLevel = p->savepointLevel;
4124         s->state = TRANS_DEFAULT;
4125         s->blockState = TBLOCK_SUBBEGIN;
4126         GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
4127         s->prevXactReadOnly = XactReadOnly;
4128
4129         CurrentTransactionState = s;
4130
4131         /*
4132          * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4133          * with the subtransaction from here on out; in particular they should not
4134          * assume that it necessarily has a transaction context, resource owner,
4135          * or XID.
4136          */
4137 }
4138
4139 /*
4140  * PopTransaction
4141  *              Pop back to parent transaction state
4142  *
4143  *      The caller has to make sure to always reassign CurrentTransactionState
4144  *      if it has a local pointer to it after calling this function.
4145  */
4146 static void
4147 PopTransaction(void)
4148 {
4149         TransactionState s = CurrentTransactionState;
4150
4151         if (s->state != TRANS_DEFAULT)
4152                 elog(WARNING, "PopTransaction while in %s state",
4153                          TransStateAsString(s->state));
4154
4155         if (s->parent == NULL)
4156                 elog(FATAL, "PopTransaction with no parent");
4157
4158         CurrentTransactionState = s->parent;
4159
4160         /* Let's just make sure CurTransactionContext is good */
4161         CurTransactionContext = s->parent->curTransactionContext;
4162         MemoryContextSwitchTo(CurTransactionContext);
4163
4164         /* Ditto for ResourceOwner links */
4165         CurTransactionResourceOwner = s->parent->curTransactionOwner;
4166         CurrentResourceOwner = s->parent->curTransactionOwner;
4167
4168         /* Free the old child structure */
4169         if (s->name)
4170                 pfree(s->name);
4171         pfree(s);
4172 }
4173
4174 /*
4175  * ShowTransactionState
4176  *              Debug support
4177  */
4178 static void
4179 ShowTransactionState(const char *str)
4180 {
4181         /* skip work if message will definitely not be printed */
4182         if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4183         {
4184                 elog(DEBUG3, "%s", str);
4185                 ShowTransactionStateRec(CurrentTransactionState);
4186         }
4187 }
4188
4189 /*
4190  * ShowTransactionStateRec
4191  *              Recursive subroutine for ShowTransactionState
4192  */
4193 static void
4194 ShowTransactionStateRec(TransactionState s)
4195 {
4196         StringInfoData buf;
4197
4198         initStringInfo(&buf);
4199
4200         if (s->nChildXids > 0)
4201         {
4202                 int                     i;
4203
4204                 appendStringInfo(&buf, "%u", s->childXids[0]);
4205                 for (i = 1; i < s->nChildXids; i++)
4206                         appendStringInfo(&buf, " %u", s->childXids[i]);
4207         }
4208
4209         if (s->parent)
4210                 ShowTransactionStateRec(s->parent);
4211
4212         /* use ereport to suppress computation if msg will not be printed */
4213         ereport(DEBUG3,
4214                         (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u%s, nestlvl: %d, children: %s",
4215                                                          PointerIsValid(s->name) ? s->name : "unnamed",
4216                                                          BlockStateAsString(s->blockState),
4217                                                          TransStateAsString(s->state),
4218                                                          (unsigned int) s->transactionId,
4219                                                          (unsigned int) s->subTransactionId,
4220                                                          (unsigned int) currentCommandId,
4221                                                          currentCommandIdUsed ? " (used)" : "",
4222                                                          s->nestingLevel, buf.data)));
4223
4224         pfree(buf.data);
4225 }
4226
4227 /*
4228  * BlockStateAsString
4229  *              Debug support
4230  */
4231 static const char *
4232 BlockStateAsString(TBlockState blockState)
4233 {
4234         switch (blockState)
4235         {
4236                 case TBLOCK_DEFAULT:
4237                         return "DEFAULT";
4238                 case TBLOCK_STARTED:
4239                         return "STARTED";
4240                 case TBLOCK_BEGIN:
4241                         return "BEGIN";
4242                 case TBLOCK_INPROGRESS:
4243                         return "INPROGRESS";
4244                 case TBLOCK_END:
4245                         return "END";
4246                 case TBLOCK_ABORT:
4247                         return "ABORT";
4248                 case TBLOCK_ABORT_END:
4249                         return "ABORT END";
4250                 case TBLOCK_ABORT_PENDING:
4251                         return "ABORT PEND";
4252                 case TBLOCK_PREPARE:
4253                         return "PREPARE";
4254                 case TBLOCK_SUBBEGIN:
4255                         return "SUB BEGIN";
4256                 case TBLOCK_SUBINPROGRESS:
4257                         return "SUB INPROGRS";
4258                 case TBLOCK_SUBEND:
4259                         return "SUB END";
4260                 case TBLOCK_SUBABORT:
4261                         return "SUB ABORT";
4262                 case TBLOCK_SUBABORT_END:
4263                         return "SUB ABORT END";
4264                 case TBLOCK_SUBABORT_PENDING:
4265                         return "SUB ABRT PEND";
4266                 case TBLOCK_SUBRESTART:
4267                         return "SUB RESTART";
4268                 case TBLOCK_SUBABORT_RESTART:
4269                         return "SUB AB RESTRT";
4270         }
4271         return "UNRECOGNIZED";
4272 }
4273
4274 /*
4275  * TransStateAsString
4276  *              Debug support
4277  */
4278 static const char *
4279 TransStateAsString(TransState state)
4280 {
4281         switch (state)
4282         {
4283                 case TRANS_DEFAULT:
4284                         return "DEFAULT";
4285                 case TRANS_START:
4286                         return "START";
4287                 case TRANS_INPROGRESS:
4288                         return "INPROGR";
4289                 case TRANS_COMMIT:
4290                         return "COMMIT";
4291                 case TRANS_ABORT:
4292                         return "ABORT";
4293                 case TRANS_PREPARE:
4294                         return "PREPARE";
4295         }
4296         return "UNRECOGNIZED";
4297 }
4298
4299 /*
4300  * xactGetCommittedChildren
4301  *
4302  * Gets the list of committed children of the current transaction.      The return
4303  * value is the number of child transactions.  *ptr is set to point to an
4304  * array of TransactionIds.  The array is allocated in TopTransactionContext;
4305  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
4306  * If there are no subxacts, *ptr is set to NULL.
4307  */
4308 int
4309 xactGetCommittedChildren(TransactionId **ptr)
4310 {
4311         TransactionState s = CurrentTransactionState;
4312
4313         if (s->nChildXids == 0)
4314                 *ptr = NULL;
4315         else
4316                 *ptr = s->childXids;
4317
4318         return s->nChildXids;
4319 }
4320
4321 /*
4322  *      XLOG support routines
4323  */
4324
4325 /*
4326  * Before 8.5 this was a fairly short function, but now it performs many
4327  * actions for which the order of execution is critical.
4328  */
4329 static void
4330 xact_redo_commit(xl_xact_commit *xlrec, TransactionId xid, XLogRecPtr lsn)
4331 {
4332         TransactionId *sub_xids;
4333         SharedInvalidationMessage *inval_msgs;
4334         TransactionId max_xid;
4335         int                     i;
4336
4337         /* subxid array follows relfilenodes */
4338         sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4339         /* invalidation messages array follows subxids */
4340         inval_msgs = (SharedInvalidationMessage *) &(sub_xids[xlrec->nsubxacts]);
4341
4342         max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4343
4344         /*
4345          * Make sure nextXid is beyond any XID mentioned in the record.
4346          *
4347          * We don't expect anyone else to modify nextXid, hence we
4348          * don't need to hold a lock while checking this. We still acquire
4349          * the lock to modify it, though.
4350          */
4351         if (TransactionIdFollowsOrEquals(max_xid,
4352                                                                          ShmemVariableCache->nextXid))
4353         {
4354                 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4355                 ShmemVariableCache->nextXid = max_xid;
4356                 TransactionIdAdvance(ShmemVariableCache->nextXid);
4357                 LWLockRelease(XidGenLock);
4358         }
4359
4360         if (!InHotStandby || XactCompletionVacuumFull(xlrec))
4361         {
4362                 /*
4363                  * Mark the transaction committed in pg_clog.
4364                  *
4365                  * If InHotStandby and this is the first commit of a VACUUM FULL INPLACE
4366                  * we perform only the actual commit to clog. Strangely, there are two
4367                  * commits that share the same xid for every VFI, so we need to skip
4368                  * some steps for the first commit. It's OK to repeat the clog update
4369                  * when we see the second commit on a VFI.
4370                  */
4371                 TransactionIdCommitTree(xid, xlrec->nsubxacts, sub_xids);
4372         }
4373         else
4374         {
4375                 /*
4376                  * If a transaction completion record arrives that has as-yet unobserved
4377                  * subtransactions then this will not have been fully handled by the call
4378                  * to RecordKnownAssignedTransactionIds() in the main recovery loop in
4379                  * xlog.c. So we need to do bookkeeping again to cover that case. This is
4380                  * confusing and it is easy to think this call is irrelevant, which has
4381                  * happened three times in development already. Leave it in.
4382                  */
4383                 RecordKnownAssignedTransactionIds(max_xid);
4384
4385                 /*
4386                  * Mark the transaction committed in pg_clog. We use async commit
4387                  * protocol during recovery to provide information on database
4388                  * consistency for when users try to set hint bits. It is important
4389                  * that we do not set hint bits until the minRecoveryPoint is past
4390                  * this commit record. This ensures that if we crash we don't see
4391                  * hint bits set on changes made by transactions that haven't yet
4392                  * recovered. It's unlikely but it's good to be safe.
4393                  */
4394                 TransactionIdAsyncCommitTree(xid, xlrec->nsubxacts, sub_xids, lsn);
4395
4396                 /*
4397                  * We must mark clog before we update the ProcArray.
4398                  */
4399                 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids);
4400
4401                 /*
4402                  * Send any cache invalidations attached to the commit. We must
4403                  * maintain the same order of invalidation then release locks
4404                  * as occurs in         .
4405                  */
4406                 ProcessCommittedInvalidationMessages(inval_msgs, xlrec->nmsgs,
4407                                                                         XactCompletionRelcacheInitFileInval(xlrec));
4408
4409                 /*
4410                  * Release locks, if any. We do this for both two phase and normal
4411                  * one phase transactions. In effect we are ignoring the prepare
4412                  * phase and just going straight to lock release.
4413                  */
4414                 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4415         }
4416
4417         /* Make sure files supposed to be dropped are dropped */
4418         for (i = 0; i < xlrec->nrels; i++)
4419         {
4420                 SMgrRelation srel = smgropen(xlrec->xnodes[i]);
4421                 ForkNumber      fork;
4422
4423                 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4424                 {
4425                         if (smgrexists(srel, fork))
4426                         {
4427                                 XLogDropRelation(xlrec->xnodes[i], fork);
4428                                 smgrdounlink(srel, fork, false, true);
4429                         }
4430                 }
4431                 smgrclose(srel);
4432         }
4433
4434         /*
4435          * We issue an XLogFlush() for the same reason we emit ForceSyncCommit() in
4436          * normal operation. For example, in DROP DATABASE, we delete all the files
4437          * belonging to the database, and then commit the transaction. If we crash
4438          * after all the files have been deleted but before the commit, you have an
4439          * entry in pg_database without any files. To minimize the window for that,
4440          * we use ForceSyncCommit() to rush the commit record to disk as quick as
4441          * possible. We have the same window during recovery, and forcing an
4442          * XLogFlush() (which updates minRecoveryPoint during recovery) helps
4443          * to reduce that problem window, for any user that requested ForceSyncCommit().
4444          */
4445         if (XactCompletionForceSyncCommit(xlrec))
4446                 XLogFlush(lsn);
4447 }
4448
4449 /*
4450  * Be careful with the order of execution, as with xact_redo_commit().
4451  * The two functions are similar but differ in key places.
4452  *
4453  * Note also that an abort can be for a subtransaction and its children,
4454  * not just for a top level abort. That means we have to consider
4455  * topxid != xid, whereas in commit we would find topxid == xid always
4456  * because subtransaction commit is never WAL logged.
4457  */
4458 static void
4459 xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
4460 {
4461         TransactionId *sub_xids;
4462         TransactionId max_xid;
4463         int                     i;
4464
4465         sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4466         max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4467
4468         /* Make sure nextXid is beyond any XID mentioned in the record */
4469         /* We don't expect anyone else to modify nextXid, hence we
4470          * don't need to hold a lock while checking this. We still acquire
4471          * the lock to modify it, though.
4472          */
4473         if (TransactionIdFollowsOrEquals(max_xid,
4474                                                                          ShmemVariableCache->nextXid))
4475         {
4476                 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4477                 ShmemVariableCache->nextXid = max_xid;
4478                 TransactionIdAdvance(ShmemVariableCache->nextXid);
4479                 LWLockRelease(XidGenLock);
4480         }
4481
4482         if (InHotStandby)
4483         {
4484                 /*
4485                  * If a transaction completion record arrives that has as-yet unobserved
4486                  * subtransactions then this will not have been fully handled by the call
4487                  * to RecordKnownAssignedTransactionIds() in the main recovery loop in
4488                  * xlog.c. So we need to do bookkeeping again to cover that case. This is
4489                  * confusing and it is easy to think this call is irrelevant, which has
4490                  * happened three times in development already. Leave it in.
4491                  */
4492                 RecordKnownAssignedTransactionIds(max_xid);
4493         }
4494
4495         /* Mark the transaction aborted in pg_clog, no need for async stuff */
4496         TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4497
4498         if (InHotStandby)
4499         {
4500                 /*
4501                  * We must mark clog before we update the ProcArray.
4502                  */
4503                 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids);
4504
4505                 /*
4506                  * There are no flat files that need updating, nor invalidation
4507                  * messages to send or undo.
4508                  */
4509
4510                 /*
4511                  * Release locks, if any. There are no invalidations to send.
4512                  */
4513                 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4514         }
4515
4516         /* Make sure files supposed to be dropped are dropped */
4517         for (i = 0; i < xlrec->nrels; i++)
4518         {
4519                 SMgrRelation srel = smgropen(xlrec->xnodes[i]);
4520                 ForkNumber      fork;
4521
4522                 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4523                 {
4524                         if (smgrexists(srel, fork))
4525                         {
4526                                 XLogDropRelation(xlrec->xnodes[i], fork);
4527                                 smgrdounlink(srel, fork, false, true);
4528                         }
4529                 }
4530                 smgrclose(srel);
4531         }
4532 }
4533
4534 void
4535 xact_redo(XLogRecPtr lsn, XLogRecord *record)
4536 {
4537         uint8           info = record->xl_info & ~XLR_INFO_MASK;
4538
4539         /* Backup blocks are not used in xact records */
4540         Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
4541
4542         if (info == XLOG_XACT_COMMIT)
4543         {
4544                 xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
4545
4546                 xact_redo_commit(xlrec, record->xl_xid, lsn);
4547         }
4548         else if (info == XLOG_XACT_ABORT)
4549         {
4550                 xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4551
4552                 xact_redo_abort(xlrec, record->xl_xid);
4553         }
4554         else if (info == XLOG_XACT_PREPARE)
4555         {
4556                 /* the record contents are exactly the 2PC file */
4557                 RecreateTwoPhaseFile(record->xl_xid,
4558                                                          XLogRecGetData(record), record->xl_len);
4559         }
4560         else if (info == XLOG_XACT_COMMIT_PREPARED)
4561         {
4562                 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4563
4564                 xact_redo_commit(&xlrec->crec, xlrec->xid, lsn);
4565                 RemoveTwoPhaseFile(xlrec->xid, false);
4566         }
4567         else if (info == XLOG_XACT_ABORT_PREPARED)
4568         {
4569                 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4570
4571                 xact_redo_abort(&xlrec->arec, xlrec->xid);
4572                 RemoveTwoPhaseFile(xlrec->xid, false);
4573         }
4574         else if (info == XLOG_XACT_ASSIGNMENT)
4575         {
4576                 xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
4577
4578                 if (InHotStandby)
4579                         ProcArrayApplyXidAssignment(xlrec->xtop,
4580                                                                                 xlrec->nsubxacts, xlrec->xsub);
4581         }
4582         else
4583                 elog(PANIC, "xact_redo: unknown op code %u", info);
4584 }
4585
4586 static void
4587 xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4588 {
4589         int                     i;
4590         TransactionId *xacts;
4591         SharedInvalidationMessage *msgs;
4592
4593         xacts = (TransactionId *) &xlrec->xnodes[xlrec->nrels];
4594         msgs = (SharedInvalidationMessage *) &xacts[xlrec->nsubxacts];
4595
4596         if (XactCompletionRelcacheInitFileInval(xlrec))
4597                 appendStringInfo(buf, "; relcache init file inval");
4598
4599         appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4600         if (xlrec->nrels > 0)
4601         {
4602                 appendStringInfo(buf, "; rels:");
4603                 for (i = 0; i < xlrec->nrels; i++)
4604                 {
4605                         char       *path = relpath(xlrec->xnodes[i], MAIN_FORKNUM);
4606
4607                         appendStringInfo(buf, " %s", path);
4608                         pfree(path);
4609                 }
4610         }
4611         if (xlrec->nsubxacts > 0)
4612         {
4613                 appendStringInfo(buf, "; subxacts:");
4614                 for (i = 0; i < xlrec->nsubxacts; i++)
4615                         appendStringInfo(buf, " %u", xacts[i]);
4616         }
4617         if (xlrec->nmsgs > 0)
4618         {
4619                 appendStringInfo(buf, "; inval msgs:");
4620                 for (i = 0; i < xlrec->nmsgs; i++)
4621                 {
4622                         SharedInvalidationMessage *msg = &msgs[i];
4623
4624                         if (msg->id >= 0)
4625                                 appendStringInfo(buf,  "catcache id%d ", msg->id);
4626                         else if (msg->id == SHAREDINVALRELCACHE_ID)
4627                                 appendStringInfo(buf,  "relcache ");
4628                         else if (msg->id == SHAREDINVALSMGR_ID)
4629                                 appendStringInfo(buf,  "smgr ");
4630                 }
4631         }
4632 }
4633
4634 static void
4635 xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4636 {
4637         int                     i;
4638
4639         appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4640         if (xlrec->nrels > 0)
4641         {
4642                 appendStringInfo(buf, "; rels:");
4643                 for (i = 0; i < xlrec->nrels; i++)
4644                 {
4645                         char       *path = relpath(xlrec->xnodes[i], MAIN_FORKNUM);
4646
4647                         appendStringInfo(buf, " %s", path);
4648                         pfree(path);
4649                 }
4650         }
4651         if (xlrec->nsubxacts > 0)
4652         {
4653                 TransactionId *xacts = (TransactionId *)
4654                 &xlrec->xnodes[xlrec->nrels];
4655
4656                 appendStringInfo(buf, "; subxacts:");
4657                 for (i = 0; i < xlrec->nsubxacts; i++)
4658                         appendStringInfo(buf, " %u", xacts[i]);
4659         }
4660 }
4661
4662 static void
4663 xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
4664 {
4665         int                     i;
4666
4667         appendStringInfo(buf, "subxacts:");
4668
4669         for (i = 0; i < xlrec->nsubxacts; i++)
4670                 appendStringInfo(buf, " %u", xlrec->xsub[i]);
4671 }
4672
4673 void
4674 xact_desc(StringInfo buf, uint8 xl_info, char *rec)
4675 {
4676         uint8           info = xl_info & ~XLR_INFO_MASK;
4677
4678         if (info == XLOG_XACT_COMMIT)
4679         {
4680                 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
4681
4682                 appendStringInfo(buf, "commit: ");
4683                 xact_desc_commit(buf, xlrec);
4684         }
4685         else if (info == XLOG_XACT_ABORT)
4686         {
4687                 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
4688
4689                 appendStringInfo(buf, "abort: ");
4690                 xact_desc_abort(buf, xlrec);
4691         }
4692         else if (info == XLOG_XACT_PREPARE)
4693         {
4694                 appendStringInfo(buf, "prepare");
4695         }
4696         else if (info == XLOG_XACT_COMMIT_PREPARED)
4697         {
4698                 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
4699
4700                 appendStringInfo(buf, "commit prepared %u: ", xlrec->xid);
4701                 xact_desc_commit(buf, &xlrec->crec);
4702         }
4703         else if (info == XLOG_XACT_ABORT_PREPARED)
4704         {
4705                 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4706
4707                 appendStringInfo(buf, "abort prepared %u: ", xlrec->xid);
4708                 xact_desc_abort(buf, &xlrec->arec);
4709         }
4710         else if (info == XLOG_XACT_ASSIGNMENT)
4711         {
4712                 xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
4713
4714                 /*
4715                  * Note that we ignore the WAL record's xid, since we're more
4716                  * interested in the top-level xid that issued the record
4717                  * and which xids are being reported here.
4718                  */
4719                 appendStringInfo(buf, "xid assignment xtop %u: ", xlrec->xtop);
4720                 xact_desc_assignment(buf, xlrec);
4721         }
4722         else
4723                 appendStringInfo(buf, "UNKNOWN");
4724 }