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