]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xact.c
Scan the buffer pool just once, not once per fork, during relation drop.
[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-2012, 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/lmgr.h"
42 #include "storage/predicate.h"
43 #include "storage/procarray.h"
44 #include "storage/sinvaladt.h"
45 #include "storage/smgr.h"
46 #include "utils/combocid.h"
47 #include "utils/guc.h"
48 #include "utils/inval.h"
49 #include "utils/memutils.h"
50 #include "utils/relmapper.h"
51 #include "utils/snapmgr.h"
52 #include "utils/timestamp.h"
53 #include "pg_trace.h"
54
55
56 /*
57  *      User-tweakable parameters
58  */
59 int                     DefaultXactIsoLevel = XACT_READ_COMMITTED;
60 int                     XactIsoLevel;
61
62 bool            DefaultXactReadOnly = false;
63 bool            XactReadOnly;
64
65 bool            DefaultXactDeferrable = false;
66 bool            XactDeferrable;
67
68 int                     synchronous_commit = SYNCHRONOUS_COMMIT_ON;
69
70 int                     CommitDelay = 0;        /* precommit delay in microseconds */
71 int                     CommitSiblings = 5; /* # concurrent xacts needed to sleep */
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 /*
575  *      GetCurrentCommandId
576  *
577  * "used" must be TRUE if the caller intends to use the command ID to mark
578  * inserted/updated/deleted tuples.  FALSE means the ID is being fetched
579  * for read-only purposes (ie, as a snapshot validity cutoff).  See
580  * CommandCounterIncrement() for discussion.
581  */
582 CommandId
583 GetCurrentCommandId(bool used)
584 {
585         /* this is global to a transaction, not subtransaction-local */
586         if (used)
587                 currentCommandIdUsed = true;
588         return currentCommandId;
589 }
590
591 /*
592  *      GetCurrentTransactionStartTimestamp
593  */
594 TimestampTz
595 GetCurrentTransactionStartTimestamp(void)
596 {
597         return xactStartTimestamp;
598 }
599
600 /*
601  *      GetCurrentStatementStartTimestamp
602  */
603 TimestampTz
604 GetCurrentStatementStartTimestamp(void)
605 {
606         return stmtStartTimestamp;
607 }
608
609 /*
610  *      GetCurrentTransactionStopTimestamp
611  *
612  * We return current time if the transaction stop time hasn't been set
613  * (which can happen if we decide we don't need to log an XLOG record).
614  */
615 TimestampTz
616 GetCurrentTransactionStopTimestamp(void)
617 {
618         if (xactStopTimestamp != 0)
619                 return xactStopTimestamp;
620         return GetCurrentTimestamp();
621 }
622
623 /*
624  *      SetCurrentStatementStartTimestamp
625  */
626 void
627 SetCurrentStatementStartTimestamp(void)
628 {
629         stmtStartTimestamp = GetCurrentTimestamp();
630 }
631
632 /*
633  *      SetCurrentTransactionStopTimestamp
634  */
635 static inline void
636 SetCurrentTransactionStopTimestamp(void)
637 {
638         xactStopTimestamp = GetCurrentTimestamp();
639 }
640
641 /*
642  *      GetCurrentTransactionNestLevel
643  *
644  * Note: this will return zero when not inside any transaction, one when
645  * inside a top-level transaction, etc.
646  */
647 int
648 GetCurrentTransactionNestLevel(void)
649 {
650         TransactionState s = CurrentTransactionState;
651
652         return s->nestingLevel;
653 }
654
655
656 /*
657  *      TransactionIdIsCurrentTransactionId
658  */
659 bool
660 TransactionIdIsCurrentTransactionId(TransactionId xid)
661 {
662         TransactionState s;
663
664         /*
665          * We always say that BootstrapTransactionId is "not my transaction ID"
666          * even when it is (ie, during bootstrap).      Along with the fact that
667          * transam.c always treats BootstrapTransactionId as already committed,
668          * this causes the tqual.c routines to see all tuples as committed, which
669          * is what we need during bootstrap.  (Bootstrap mode only inserts tuples,
670          * it never updates or deletes them, so all tuples can be presumed good
671          * immediately.)
672          *
673          * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
674          * not my transaction ID, so we can just return "false" immediately for
675          * any non-normal XID.
676          */
677         if (!TransactionIdIsNormal(xid))
678                 return false;
679
680         /*
681          * We will return true for the Xid of the current subtransaction, any of
682          * its subcommitted children, any of its parents, or any of their
683          * previously subcommitted children.  However, a transaction being aborted
684          * is no longer "current", even though it may still have an entry on the
685          * state stack.
686          */
687         for (s = CurrentTransactionState; s != NULL; s = s->parent)
688         {
689                 int                     low,
690                                         high;
691
692                 if (s->state == TRANS_ABORT)
693                         continue;
694                 if (!TransactionIdIsValid(s->transactionId))
695                         continue;                       /* it can't have any child XIDs either */
696                 if (TransactionIdEquals(xid, s->transactionId))
697                         return true;
698                 /* As the childXids array is ordered, we can use binary search */
699                 low = 0;
700                 high = s->nChildXids - 1;
701                 while (low <= high)
702                 {
703                         int                     middle;
704                         TransactionId probe;
705
706                         middle = low + (high - low) / 2;
707                         probe = s->childXids[middle];
708                         if (TransactionIdEquals(probe, xid))
709                                 return true;
710                         else if (TransactionIdPrecedes(probe, xid))
711                                 low = middle + 1;
712                         else
713                                 high = middle - 1;
714                 }
715         }
716
717         return false;
718 }
719
720 /*
721  *      TransactionStartedDuringRecovery
722  *
723  * Returns true if the current transaction started while recovery was still
724  * in progress. Recovery might have ended since so RecoveryInProgress() might
725  * return false already.
726  */
727 bool
728 TransactionStartedDuringRecovery(void)
729 {
730         return CurrentTransactionState->startedInRecovery;
731 }
732
733 /*
734  *      CommandCounterIncrement
735  */
736 void
737 CommandCounterIncrement(void)
738 {
739         /*
740          * If the current value of the command counter hasn't been "used" to mark
741          * tuples, we need not increment it, since there's no need to distinguish
742          * a read-only command from others.  This helps postpone command counter
743          * overflow, and keeps no-op CommandCounterIncrement operations cheap.
744          */
745         if (currentCommandIdUsed)
746         {
747                 currentCommandId += 1;
748                 if (currentCommandId == FirstCommandId) /* check for overflow */
749                 {
750                         currentCommandId -= 1;
751                         ereport(ERROR,
752                                         (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
753                                          errmsg("cannot have more than 2^32-1 commands in a transaction")));
754                 }
755                 currentCommandIdUsed = false;
756
757                 /* Propagate new command ID into static snapshots */
758                 SnapshotSetCommandId(currentCommandId);
759
760                 /*
761                  * Make any catalog changes done by the just-completed command visible
762                  * in the local syscache.  We obviously don't need to do this after a
763                  * read-only command.  (But see hacks in inval.c to make real sure we
764                  * don't think a command that queued inval messages was read-only.)
765                  */
766                 AtCCI_LocalCache();
767         }
768 }
769
770 /*
771  * ForceSyncCommit
772  *
773  * Interface routine to allow commands to force a synchronous commit of the
774  * current top-level transaction
775  */
776 void
777 ForceSyncCommit(void)
778 {
779         forceSyncCommit = true;
780 }
781
782
783 /* ----------------------------------------------------------------
784  *                                              StartTransaction stuff
785  * ----------------------------------------------------------------
786  */
787
788 /*
789  *      AtStart_Cache
790  */
791 static void
792 AtStart_Cache(void)
793 {
794         AcceptInvalidationMessages();
795 }
796
797 /*
798  *      AtStart_Memory
799  */
800 static void
801 AtStart_Memory(void)
802 {
803         TransactionState s = CurrentTransactionState;
804
805         /*
806          * If this is the first time through, create a private context for
807          * AbortTransaction to work in.  By reserving some space now, we can
808          * insulate AbortTransaction from out-of-memory scenarios.      Like
809          * ErrorContext, we set it up with slow growth rate and a nonzero minimum
810          * size, so that space will be reserved immediately.
811          */
812         if (TransactionAbortContext == NULL)
813                 TransactionAbortContext =
814                         AllocSetContextCreate(TopMemoryContext,
815                                                                   "TransactionAbortContext",
816                                                                   32 * 1024,
817                                                                   32 * 1024,
818                                                                   32 * 1024);
819
820         /*
821          * We shouldn't have a transaction context already.
822          */
823         Assert(TopTransactionContext == NULL);
824
825         /*
826          * Create a toplevel context for the transaction.
827          */
828         TopTransactionContext =
829                 AllocSetContextCreate(TopMemoryContext,
830                                                           "TopTransactionContext",
831                                                           ALLOCSET_DEFAULT_MINSIZE,
832                                                           ALLOCSET_DEFAULT_INITSIZE,
833                                                           ALLOCSET_DEFAULT_MAXSIZE);
834
835         /*
836          * In a top-level transaction, CurTransactionContext is the same as
837          * TopTransactionContext.
838          */
839         CurTransactionContext = TopTransactionContext;
840         s->curTransactionContext = CurTransactionContext;
841
842         /* Make the CurTransactionContext active. */
843         MemoryContextSwitchTo(CurTransactionContext);
844 }
845
846 /*
847  *      AtStart_ResourceOwner
848  */
849 static void
850 AtStart_ResourceOwner(void)
851 {
852         TransactionState s = CurrentTransactionState;
853
854         /*
855          * We shouldn't have a transaction resource owner already.
856          */
857         Assert(TopTransactionResourceOwner == NULL);
858
859         /*
860          * Create a toplevel resource owner for the transaction.
861          */
862         s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
863
864         TopTransactionResourceOwner = s->curTransactionOwner;
865         CurTransactionResourceOwner = s->curTransactionOwner;
866         CurrentResourceOwner = s->curTransactionOwner;
867 }
868
869 /* ----------------------------------------------------------------
870  *                                              StartSubTransaction stuff
871  * ----------------------------------------------------------------
872  */
873
874 /*
875  * AtSubStart_Memory
876  */
877 static void
878 AtSubStart_Memory(void)
879 {
880         TransactionState s = CurrentTransactionState;
881
882         Assert(CurTransactionContext != NULL);
883
884         /*
885          * Create a CurTransactionContext, which will be used to hold data that
886          * survives subtransaction commit but disappears on subtransaction abort.
887          * We make it a child of the immediate parent's CurTransactionContext.
888          */
889         CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
890                                                                                                   "CurTransactionContext",
891                                                                                                   ALLOCSET_DEFAULT_MINSIZE,
892                                                                                                   ALLOCSET_DEFAULT_INITSIZE,
893                                                                                                   ALLOCSET_DEFAULT_MAXSIZE);
894         s->curTransactionContext = CurTransactionContext;
895
896         /* Make the CurTransactionContext active. */
897         MemoryContextSwitchTo(CurTransactionContext);
898 }
899
900 /*
901  * AtSubStart_ResourceOwner
902  */
903 static void
904 AtSubStart_ResourceOwner(void)
905 {
906         TransactionState s = CurrentTransactionState;
907
908         Assert(s->parent != NULL);
909
910         /*
911          * Create a resource owner for the subtransaction.      We make it a child of
912          * the immediate parent's resource owner.
913          */
914         s->curTransactionOwner =
915                 ResourceOwnerCreate(s->parent->curTransactionOwner,
916                                                         "SubTransaction");
917
918         CurTransactionResourceOwner = s->curTransactionOwner;
919         CurrentResourceOwner = s->curTransactionOwner;
920 }
921
922 /* ----------------------------------------------------------------
923  *                                              CommitTransaction stuff
924  * ----------------------------------------------------------------
925  */
926
927 /*
928  *      RecordTransactionCommit
929  *
930  * Returns latest XID among xact and its children, or InvalidTransactionId
931  * if the xact has no XID.      (We compute that here just because it's easier.)
932  */
933 static TransactionId
934 RecordTransactionCommit(void)
935 {
936         TransactionId xid = GetTopTransactionIdIfAny();
937         bool            markXidCommitted = TransactionIdIsValid(xid);
938         TransactionId latestXid = InvalidTransactionId;
939         int                     nrels;
940         RelFileNode *rels;
941         int                     nchildren;
942         TransactionId *children;
943         int                     nmsgs = 0;
944         SharedInvalidationMessage *invalMessages = NULL;
945         bool            RelcacheInitFileInval = false;
946         bool            wrote_xlog;
947
948         /* Get data needed for commit record */
949         nrels = smgrGetPendingDeletes(true, &rels);
950         nchildren = xactGetCommittedChildren(&children);
951         if (XLogStandbyInfoActive())
952                 nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
953                                                                                                          &RelcacheInitFileInval);
954         wrote_xlog = (XactLastRecEnd.xrecoff != 0);
955
956         /*
957          * If we haven't been assigned an XID yet, we neither can, nor do we want
958          * to write a COMMIT record.
959          */
960         if (!markXidCommitted)
961         {
962                 /*
963                  * We expect that every smgrscheduleunlink is followed by a catalog
964                  * update, and hence XID assignment, so we shouldn't get here with any
965                  * pending deletes.  Use a real test not just an Assert to check this,
966                  * since it's a bit fragile.
967                  */
968                 if (nrels != 0)
969                         elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
970
971                 /* Can't have child XIDs either; AssignTransactionId enforces this */
972                 Assert(nchildren == 0);
973
974                 /*
975                  * If we didn't create XLOG entries, we're done here; otherwise we
976                  * should flush those entries the same as a commit record.      (An
977                  * example of a possible record that wouldn't cause an XID to be
978                  * assigned is a sequence advance record due to nextval() --- we want
979                  * to flush that to disk before reporting commit.)
980                  */
981                 if (!wrote_xlog)
982                         goto cleanup;
983         }
984         else
985         {
986                 /*
987                  * Begin commit critical section and insert the commit XLOG record.
988                  */
989                 /* Tell bufmgr and smgr to prepare for commit */
990                 BufmgrCommit();
991
992                 /*
993                  * Mark ourselves as within our "commit critical section".      This
994                  * forces any concurrent checkpoint to wait until we've updated
995                  * pg_clog.  Without this, it is possible for the checkpoint to set
996                  * REDO after the XLOG record but fail to flush the pg_clog update to
997                  * disk, leading to loss of the transaction commit if the system
998                  * crashes a little later.
999                  *
1000                  * Note: we could, but don't bother to, set this flag in
1001                  * RecordTransactionAbort.      That's because loss of a transaction abort
1002                  * is noncritical; the presumption would be that it aborted, anyway.
1003                  *
1004                  * It's safe to change the inCommit flag of our own backend without
1005                  * holding the ProcArrayLock, since we're the only one modifying it.
1006                  * This makes checkpoint's determination of which xacts are inCommit a
1007                  * bit fuzzy, but it doesn't matter.
1008                  */
1009                 START_CRIT_SECTION();
1010                 MyPgXact->inCommit = true;
1011
1012                 SetCurrentTransactionStopTimestamp();
1013
1014                 /*
1015                  * Do we need the long commit record? If not, use the compact format.
1016                  */
1017                 if (nrels > 0 || nmsgs > 0 || RelcacheInitFileInval || forceSyncCommit)
1018                 {
1019                         XLogRecData rdata[4];
1020                         int                     lastrdata = 0;
1021                         xl_xact_commit xlrec;
1022                         /*
1023                          * Set flags required for recovery processing of commits.
1024                          */
1025                         xlrec.xinfo = 0;
1026                         if (RelcacheInitFileInval)
1027                                 xlrec.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
1028                         if (forceSyncCommit)
1029                                 xlrec.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
1030
1031                         xlrec.dbId = MyDatabaseId;
1032                         xlrec.tsId = MyDatabaseTableSpace;
1033
1034                         xlrec.xact_time = xactStopTimestamp;
1035                         xlrec.nrels = nrels;
1036                         xlrec.nsubxacts = nchildren;
1037                         xlrec.nmsgs = nmsgs;
1038                         rdata[0].data = (char *) (&xlrec);
1039                         rdata[0].len = MinSizeOfXactCommit;
1040                         rdata[0].buffer = InvalidBuffer;
1041                         /* dump rels to delete */
1042                         if (nrels > 0)
1043                         {
1044                                 rdata[0].next = &(rdata[1]);
1045                                 rdata[1].data = (char *) rels;
1046                                 rdata[1].len = nrels * sizeof(RelFileNode);
1047                                 rdata[1].buffer = InvalidBuffer;
1048                                 lastrdata = 1;
1049                         }
1050                         /* dump committed child Xids */
1051                         if (nchildren > 0)
1052                         {
1053                                 rdata[lastrdata].next = &(rdata[2]);
1054                                 rdata[2].data = (char *) children;
1055                                 rdata[2].len = nchildren * sizeof(TransactionId);
1056                                 rdata[2].buffer = InvalidBuffer;
1057                                 lastrdata = 2;
1058                         }
1059                         /* dump shared cache invalidation messages */
1060                         if (nmsgs > 0)
1061                         {
1062                                 rdata[lastrdata].next = &(rdata[3]);
1063                                 rdata[3].data = (char *) invalMessages;
1064                                 rdata[3].len = nmsgs * sizeof(SharedInvalidationMessage);
1065                                 rdata[3].buffer = InvalidBuffer;
1066                                 lastrdata = 3;
1067                         }
1068                         rdata[lastrdata].next = NULL;
1069
1070                         (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT, rdata);
1071                 }
1072                 else
1073                 {
1074                         XLogRecData rdata[2];
1075                         int                     lastrdata = 0;
1076                         xl_xact_commit_compact  xlrec;
1077                         xlrec.xact_time = xactStopTimestamp;
1078                         xlrec.nsubxacts = nchildren;
1079                         rdata[0].data = (char *) (&xlrec);
1080                         rdata[0].len = MinSizeOfXactCommitCompact;
1081                         rdata[0].buffer = InvalidBuffer;
1082                         /* dump committed child Xids */
1083                         if (nchildren > 0)
1084                         {
1085                                 rdata[0].next = &(rdata[1]);
1086                                 rdata[1].data = (char *) children;
1087                                 rdata[1].len = nchildren * sizeof(TransactionId);
1088                                 rdata[1].buffer = InvalidBuffer;
1089                                 lastrdata = 1;
1090                         }
1091                         rdata[lastrdata].next = NULL;
1092
1093                         (void) XLogInsert(RM_XACT_ID, XLOG_XACT_COMMIT_COMPACT, rdata);
1094                 }
1095         }
1096
1097         /*
1098          * Check if we want to commit asynchronously.  We can allow the XLOG flush
1099          * to happen asynchronously if synchronous_commit=off, or if the current
1100          * transaction has not performed any WAL-logged operation.      The latter
1101          * case can arise if the current transaction wrote only to temporary
1102          * and/or unlogged tables.      In case of a crash, the loss of such a
1103          * transaction will be irrelevant since temp tables will be lost anyway,
1104          * and unlogged tables will be truncated.  (Given the foregoing, you might
1105          * think that it would be unnecessary to emit the XLOG record at all in
1106          * this case, but we don't currently try to do that.  It would certainly
1107          * cause problems at least in Hot Standby mode, where the
1108          * KnownAssignedXids machinery requires tracking every XID assignment.  It
1109          * might be OK to skip it only when wal_level < hot_standby, but for now
1110          * we don't.)
1111          *
1112          * However, if we're doing cleanup of any non-temp rels or committing any
1113          * command that wanted to force sync commit, then we must flush XLOG
1114          * immediately.  (We must not allow asynchronous commit if there are any
1115          * non-temp tables to be deleted, because we might delete the files before
1116          * the COMMIT record is flushed to disk.  We do allow asynchronous commit
1117          * if all to-be-deleted tables are temporary though, since they are lost
1118          * anyway if we crash.)
1119          */
1120         if ((wrote_xlog && synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
1121                 forceSyncCommit || nrels > 0)
1122         {
1123                 /*
1124                  * Synchronous commit case:
1125                  *
1126                  * Sleep before flush! So we can flush more than one commit records
1127                  * per single fsync.  (The idea is some other backend may do the
1128                  * XLogFlush while we're sleeping.  This needs work still, because on
1129                  * most Unixen, the minimum select() delay is 10msec or more, which is
1130                  * way too long.)
1131                  *
1132                  * We do not sleep if enableFsync is not turned on, nor if there are
1133                  * fewer than CommitSiblings other backends with active transactions.
1134                  */
1135                 if (CommitDelay > 0 && enableFsync &&
1136                         MinimumActiveBackends(CommitSiblings))
1137                         pg_usleep(CommitDelay);
1138
1139                 XLogFlush(XactLastRecEnd);
1140
1141                 /*
1142                  * Wake up all walsenders to send WAL up to the COMMIT record
1143                  * immediately if replication is enabled
1144                  */
1145                 if (max_wal_senders > 0)
1146                         WalSndWakeup();
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->inCommit = 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.xrecoff = 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.xrecoff = 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.xrecoff = 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         /*
1850          * The remaining actions cannot call any user-defined code, so it's safe
1851          * to start shutting down within-transaction services.  But note that most
1852          * of this stuff could still throw an error, which would switch us into
1853          * the transaction-abort path.
1854          */
1855
1856         /* Shut down the deferred-trigger manager */
1857         AfterTriggerEndXact(true);
1858
1859         /*
1860          * Let ON COMMIT management do its thing (must happen after closing
1861          * cursors, to avoid dangling-reference problems)
1862          */
1863         PreCommit_on_commit_actions();
1864
1865         /* close large objects before lower-level cleanup */
1866         AtEOXact_LargeObject(true);
1867
1868         /*
1869          * Mark serializable transaction as complete for predicate locking
1870          * purposes.  This should be done as late as we can put it and still allow
1871          * errors to be raised for failure patterns found at commit.
1872          */
1873         PreCommit_CheckForSerializationFailure();
1874
1875         /*
1876          * Insert notifications sent by NOTIFY commands into the queue.  This
1877          * should be late in the pre-commit sequence to minimize time spent
1878          * holding the notify-insertion lock.
1879          */
1880         PreCommit_Notify();
1881
1882         /* Prevent cancel/die interrupt while cleaning up */
1883         HOLD_INTERRUPTS();
1884
1885         /* Commit updates to the relation map --- do this as late as possible */
1886         AtEOXact_RelationMap(true);
1887
1888         /*
1889          * set the current transaction state information appropriately during
1890          * commit processing
1891          */
1892         s->state = TRANS_COMMIT;
1893
1894         /*
1895          * Here is where we really truly commit.
1896          */
1897         latestXid = RecordTransactionCommit();
1898
1899         TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
1900
1901         /*
1902          * Let others know about no transaction in progress by me. Note that this
1903          * must be done _before_ releasing locks we hold and _after_
1904          * RecordTransactionCommit.
1905          */
1906         ProcArrayEndTransaction(MyProc, latestXid);
1907
1908         /*
1909          * This is all post-commit cleanup.  Note that if an error is raised here,
1910          * it's too late to abort the transaction.  This should be just
1911          * noncritical resource releasing.
1912          *
1913          * The ordering of operations is not entirely random.  The idea is:
1914          * release resources visible to other backends (eg, files, buffer pins);
1915          * then release locks; then release backend-local resources. We want to
1916          * release locks at the point where any backend waiting for us will see
1917          * our transaction as being fully cleaned up.
1918          *
1919          * Resources that can be associated with individual queries are handled by
1920          * the ResourceOwner mechanism.  The other calls here are for backend-wide
1921          * state.
1922          */
1923
1924         CallXactCallbacks(XACT_EVENT_COMMIT);
1925
1926         ResourceOwnerRelease(TopTransactionResourceOwner,
1927                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
1928                                                  true, true);
1929
1930         /* Check we've released all buffer pins */
1931         AtEOXact_Buffers(true);
1932
1933         /* Clean up the relation cache */
1934         AtEOXact_RelationCache(true);
1935
1936         /*
1937          * Make catalog changes visible to all backends.  This has to happen after
1938          * relcache references are dropped (see comments for
1939          * AtEOXact_RelationCache), but before locks are released (if anyone is
1940          * waiting for lock on a relation we've modified, we want them to know
1941          * about the catalog change before they start using the relation).
1942          */
1943         AtEOXact_Inval(true);
1944
1945         /*
1946          * Likewise, dropping of files deleted during the transaction is best done
1947          * after releasing relcache and buffer pins.  (This is not strictly
1948          * necessary during commit, since such pins should have been released
1949          * already, but this ordering is definitely critical during abort.)
1950          */
1951         smgrDoPendingDeletes(true);
1952
1953         AtEOXact_MultiXact();
1954
1955         ResourceOwnerRelease(TopTransactionResourceOwner,
1956                                                  RESOURCE_RELEASE_LOCKS,
1957                                                  true, true);
1958         ResourceOwnerRelease(TopTransactionResourceOwner,
1959                                                  RESOURCE_RELEASE_AFTER_LOCKS,
1960                                                  true, true);
1961
1962         /* Check we've released all catcache entries */
1963         AtEOXact_CatCache(true);
1964
1965         AtCommit_Notify();
1966         AtEOXact_GUC(true, 1);
1967         AtEOXact_SPI(true);
1968         AtEOXact_on_commit_actions(true);
1969         AtEOXact_Namespace(true);
1970         /* smgrcommit already done */
1971         AtEOXact_Files();
1972         AtEOXact_ComboCid();
1973         AtEOXact_HashTables(true);
1974         AtEOXact_PgStat(true);
1975         AtEOXact_Snapshot(true);
1976         pgstat_report_xact_timestamp(0);
1977
1978         CurrentResourceOwner = NULL;
1979         ResourceOwnerDelete(TopTransactionResourceOwner);
1980         s->curTransactionOwner = NULL;
1981         CurTransactionResourceOwner = NULL;
1982         TopTransactionResourceOwner = NULL;
1983
1984         AtCommit_Memory();
1985
1986         s->transactionId = InvalidTransactionId;
1987         s->subTransactionId = InvalidSubTransactionId;
1988         s->nestingLevel = 0;
1989         s->gucNestLevel = 0;
1990         s->childXids = NULL;
1991         s->nChildXids = 0;
1992         s->maxChildXids = 0;
1993
1994         /*
1995          * done with commit processing, set current transaction state back to
1996          * default
1997          */
1998         s->state = TRANS_DEFAULT;
1999
2000         RESUME_INTERRUPTS();
2001 }
2002
2003
2004 /*
2005  *      PrepareTransaction
2006  *
2007  * NB: if you change this routine, better look at CommitTransaction too!
2008  */
2009 static void
2010 PrepareTransaction(void)
2011 {
2012         TransactionState s = CurrentTransactionState;
2013         TransactionId xid = GetCurrentTransactionId();
2014         GlobalTransaction gxact;
2015         TimestampTz prepared_at;
2016
2017         ShowTransactionState("PrepareTransaction");
2018
2019         /*
2020          * check the current transaction state
2021          */
2022         if (s->state != TRANS_INPROGRESS)
2023                 elog(WARNING, "PrepareTransaction while in %s state",
2024                          TransStateAsString(s->state));
2025         Assert(s->parent == NULL);
2026
2027         /*
2028          * Do pre-commit processing that involves calling user-defined code, such
2029          * as triggers.  Since closing cursors could queue trigger actions,
2030          * triggers could open cursors, etc, we have to keep looping until there's
2031          * nothing left to do.
2032          */
2033         for (;;)
2034         {
2035                 /*
2036                  * Fire all currently pending deferred triggers.
2037                  */
2038                 AfterTriggerFireDeferred();
2039
2040                 /*
2041                  * Close open portals (converting holdable ones into static portals).
2042                  * If there weren't any, we are done ... otherwise loop back to check
2043                  * if they queued deferred triggers.  Lather, rinse, repeat.
2044                  */
2045                 if (!PreCommit_Portals(true))
2046                         break;
2047         }
2048
2049         /*
2050          * The remaining actions cannot call any user-defined code, so it's safe
2051          * to start shutting down within-transaction services.  But note that most
2052          * of this stuff could still throw an error, which would switch us into
2053          * the transaction-abort path.
2054          */
2055
2056         /* Shut down the deferred-trigger manager */
2057         AfterTriggerEndXact(true);
2058
2059         /*
2060          * Let ON COMMIT management do its thing (must happen after closing
2061          * cursors, to avoid dangling-reference problems)
2062          */
2063         PreCommit_on_commit_actions();
2064
2065         /* close large objects before lower-level cleanup */
2066         AtEOXact_LargeObject(true);
2067
2068         /*
2069          * Mark serializable transaction as complete for predicate locking
2070          * purposes.  This should be done as late as we can put it and still allow
2071          * errors to be raised for failure patterns found at commit.
2072          */
2073         PreCommit_CheckForSerializationFailure();
2074
2075         /* NOTIFY will be handled below */
2076
2077         /*
2078          * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2079          * this transaction.  Having the prepared xact hold locks on another
2080          * backend's temp table seems a bad idea --- for instance it would prevent
2081          * the backend from exiting.  There are other problems too, such as how to
2082          * clean up the source backend's local buffers and ON COMMIT state if the
2083          * prepared xact includes a DROP of a temp table.
2084          *
2085          * We must check this after executing any ON COMMIT actions, because they
2086          * might still access a temp relation.
2087          *
2088          * XXX In principle this could be relaxed to allow some useful special
2089          * cases, such as a temp table created and dropped all within the
2090          * transaction.  That seems to require much more bookkeeping though.
2091          */
2092         if (MyXactAccessedTempRel)
2093                 ereport(ERROR,
2094                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2095                                  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2096
2097         /*
2098          * Likewise, don't allow PREPARE after pg_export_snapshot.  This could be
2099          * supported if we added cleanup logic to twophase.c, but for now it
2100          * doesn't seem worth the trouble.
2101          */
2102         if (XactHasExportedSnapshots())
2103                 ereport(ERROR,
2104                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2105                                  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2106
2107         /* Prevent cancel/die interrupt while cleaning up */
2108         HOLD_INTERRUPTS();
2109
2110         /*
2111          * set the current transaction state information appropriately during
2112          * prepare processing
2113          */
2114         s->state = TRANS_PREPARE;
2115
2116         prepared_at = GetCurrentTimestamp();
2117
2118         /* Tell bufmgr and smgr to prepare for commit */
2119         BufmgrCommit();
2120
2121         /*
2122          * Reserve the GID for this transaction. This could fail if the requested
2123          * GID is invalid or already in use.
2124          */
2125         gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2126                                                         GetUserId(), MyDatabaseId);
2127         prepareGID = NULL;
2128
2129         /*
2130          * Collect data for the 2PC state file.  Note that in general, no actual
2131          * state change should happen in the called modules during this step,
2132          * since it's still possible to fail before commit, and in that case we
2133          * want transaction abort to be able to clean up.  (In particular, the
2134          * AtPrepare routines may error out if they find cases they cannot
2135          * handle.)  State cleanup should happen in the PostPrepare routines
2136          * below.  However, some modules can go ahead and clear state here because
2137          * they wouldn't do anything with it during abort anyway.
2138          *
2139          * Note: because the 2PC state file records will be replayed in the same
2140          * order they are made, the order of these calls has to match the order in
2141          * which we want things to happen during COMMIT PREPARED or ROLLBACK
2142          * PREPARED; in particular, pay attention to whether things should happen
2143          * before or after releasing the transaction's locks.
2144          */
2145         StartPrepare(gxact);
2146
2147         AtPrepare_Notify();
2148         AtPrepare_Locks();
2149         AtPrepare_PredicateLocks();
2150         AtPrepare_PgStat();
2151         AtPrepare_MultiXact();
2152         AtPrepare_RelationMap();
2153
2154         /*
2155          * Here is where we really truly prepare.
2156          *
2157          * We have to record transaction prepares even if we didn't make any
2158          * updates, because the transaction manager might get confused if we lose
2159          * a global transaction.
2160          */
2161         EndPrepare(gxact);
2162
2163         /*
2164          * Now we clean up backend-internal state and release internal resources.
2165          */
2166
2167         /* Reset XactLastRecEnd until the next transaction writes something */
2168         XactLastRecEnd.xrecoff = 0;
2169
2170         /*
2171          * Let others know about no transaction in progress by me.      This has to be
2172          * done *after* the prepared transaction has been marked valid, else
2173          * someone may think it is unlocked and recyclable.
2174          */
2175         ProcArrayClearTransaction(MyProc);
2176
2177         /*
2178          * This is all post-transaction cleanup.  Note that if an error is raised
2179          * here, it's too late to abort the transaction.  This should be just
2180          * noncritical resource releasing.      See notes in CommitTransaction.
2181          */
2182
2183         CallXactCallbacks(XACT_EVENT_PREPARE);
2184
2185         ResourceOwnerRelease(TopTransactionResourceOwner,
2186                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
2187                                                  true, true);
2188
2189         /* Check we've released all buffer pins */
2190         AtEOXact_Buffers(true);
2191
2192         /* Clean up the relation cache */
2193         AtEOXact_RelationCache(true);
2194
2195         /* notify doesn't need a postprepare call */
2196
2197         PostPrepare_PgStat();
2198
2199         PostPrepare_Inval();
2200
2201         PostPrepare_smgr();
2202
2203         PostPrepare_MultiXact(xid);
2204
2205         PostPrepare_Locks(xid);
2206         PostPrepare_PredicateLocks(xid);
2207
2208         ResourceOwnerRelease(TopTransactionResourceOwner,
2209                                                  RESOURCE_RELEASE_LOCKS,
2210                                                  true, true);
2211         ResourceOwnerRelease(TopTransactionResourceOwner,
2212                                                  RESOURCE_RELEASE_AFTER_LOCKS,
2213                                                  true, true);
2214
2215         /* Check we've released all catcache entries */
2216         AtEOXact_CatCache(true);
2217
2218         /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2219         AtEOXact_GUC(true, 1);
2220         AtEOXact_SPI(true);
2221         AtEOXact_on_commit_actions(true);
2222         AtEOXact_Namespace(true);
2223         /* smgrcommit already done */
2224         AtEOXact_Files();
2225         AtEOXact_ComboCid();
2226         AtEOXact_HashTables(true);
2227         /* don't call AtEOXact_PgStat here */
2228         AtEOXact_Snapshot(true);
2229
2230         CurrentResourceOwner = NULL;
2231         ResourceOwnerDelete(TopTransactionResourceOwner);
2232         s->curTransactionOwner = NULL;
2233         CurTransactionResourceOwner = NULL;
2234         TopTransactionResourceOwner = NULL;
2235
2236         AtCommit_Memory();
2237
2238         s->transactionId = InvalidTransactionId;
2239         s->subTransactionId = InvalidSubTransactionId;
2240         s->nestingLevel = 0;
2241         s->gucNestLevel = 0;
2242         s->childXids = NULL;
2243         s->nChildXids = 0;
2244         s->maxChildXids = 0;
2245
2246         /*
2247          * done with 1st phase commit processing, set current transaction state
2248          * back to default
2249          */
2250         s->state = TRANS_DEFAULT;
2251
2252         RESUME_INTERRUPTS();
2253 }
2254
2255
2256 /*
2257  *      AbortTransaction
2258  */
2259 static void
2260 AbortTransaction(void)
2261 {
2262         TransactionState s = CurrentTransactionState;
2263         TransactionId latestXid;
2264
2265         /* Prevent cancel/die interrupt while cleaning up */
2266         HOLD_INTERRUPTS();
2267
2268         /* Make sure we have a valid memory context and resource owner */
2269         AtAbort_Memory();
2270         AtAbort_ResourceOwner();
2271
2272         /*
2273          * Release any LW locks we might be holding as quickly as possible.
2274          * (Regular locks, however, must be held till we finish aborting.)
2275          * Releasing LW locks is critical since we might try to grab them again
2276          * while cleaning up!
2277          */
2278         LWLockReleaseAll();
2279
2280         /* Clean up buffer I/O and buffer context locks, too */
2281         AbortBufferIO();
2282         UnlockBuffers();
2283
2284         /*
2285          * Also clean up any open wait for lock, since the lock manager will choke
2286          * if we try to wait for another lock before doing this.
2287          */
2288         LockErrorCleanup();
2289
2290         /*
2291          * check the current transaction state
2292          */
2293         if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2294                 elog(WARNING, "AbortTransaction while in %s state",
2295                          TransStateAsString(s->state));
2296         Assert(s->parent == NULL);
2297
2298         /*
2299          * set the current transaction state information appropriately during the
2300          * abort processing
2301          */
2302         s->state = TRANS_ABORT;
2303
2304         /*
2305          * Reset user ID which might have been changed transiently.  We need this
2306          * to clean up in case control escaped out of a SECURITY DEFINER function
2307          * or other local change of CurrentUserId; therefore, the prior value of
2308          * SecurityRestrictionContext also needs to be restored.
2309          *
2310          * (Note: it is not necessary to restore session authorization or role
2311          * settings here because those can only be changed via GUC, and GUC will
2312          * take care of rolling them back if need be.)
2313          */
2314         SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2315
2316         /*
2317          * do abort processing
2318          */
2319         AfterTriggerEndXact(false); /* 'false' means it's abort */
2320         AtAbort_Portals();
2321         AtEOXact_LargeObject(false);
2322         AtAbort_Notify();
2323         AtEOXact_RelationMap(false);
2324
2325         /*
2326          * Advertise the fact that we aborted in pg_clog (assuming that we got as
2327          * far as assigning an XID to advertise).
2328          */
2329         latestXid = RecordTransactionAbort(false);
2330
2331         TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2332
2333         /*
2334          * Let others know about no transaction in progress by me. Note that this
2335          * must be done _before_ releasing locks we hold and _after_
2336          * RecordTransactionAbort.
2337          */
2338         ProcArrayEndTransaction(MyProc, latestXid);
2339
2340         /*
2341          * Post-abort cleanup.  See notes in CommitTransaction() concerning
2342          * ordering.  We can skip all of it if the transaction failed before
2343          * creating a resource owner.
2344          */
2345         if (TopTransactionResourceOwner != NULL)
2346         {
2347                 CallXactCallbacks(XACT_EVENT_ABORT);
2348
2349                 ResourceOwnerRelease(TopTransactionResourceOwner,
2350                                                          RESOURCE_RELEASE_BEFORE_LOCKS,
2351                                                          false, true);
2352                 AtEOXact_Buffers(false);
2353                 AtEOXact_RelationCache(false);
2354                 AtEOXact_Inval(false);
2355                 smgrDoPendingDeletes(false);
2356                 AtEOXact_MultiXact();
2357                 ResourceOwnerRelease(TopTransactionResourceOwner,
2358                                                          RESOURCE_RELEASE_LOCKS,
2359                                                          false, true);
2360                 ResourceOwnerRelease(TopTransactionResourceOwner,
2361                                                          RESOURCE_RELEASE_AFTER_LOCKS,
2362                                                          false, true);
2363                 AtEOXact_CatCache(false);
2364
2365                 AtEOXact_GUC(false, 1);
2366                 AtEOXact_SPI(false);
2367                 AtEOXact_on_commit_actions(false);
2368                 AtEOXact_Namespace(false);
2369                 AtEOXact_Files();
2370                 AtEOXact_ComboCid();
2371                 AtEOXact_HashTables(false);
2372                 AtEOXact_PgStat(false);
2373                 pgstat_report_xact_timestamp(0);
2374         }
2375
2376         /*
2377          * State remains TRANS_ABORT until CleanupTransaction().
2378          */
2379         RESUME_INTERRUPTS();
2380 }
2381
2382 /*
2383  *      CleanupTransaction
2384  */
2385 static void
2386 CleanupTransaction(void)
2387 {
2388         TransactionState s = CurrentTransactionState;
2389
2390         /*
2391          * State should still be TRANS_ABORT from AbortTransaction().
2392          */
2393         if (s->state != TRANS_ABORT)
2394                 elog(FATAL, "CleanupTransaction: unexpected state %s",
2395                          TransStateAsString(s->state));
2396
2397         /*
2398          * do abort cleanup processing
2399          */
2400         AtCleanup_Portals();            /* now safe to release portal memory */
2401         AtEOXact_Snapshot(false);       /* and release the transaction's snapshots */
2402
2403         CurrentResourceOwner = NULL;    /* and resource owner */
2404         if (TopTransactionResourceOwner)
2405                 ResourceOwnerDelete(TopTransactionResourceOwner);
2406         s->curTransactionOwner = NULL;
2407         CurTransactionResourceOwner = NULL;
2408         TopTransactionResourceOwner = NULL;
2409
2410         AtCleanup_Memory();                     /* and transaction memory */
2411
2412         s->transactionId = InvalidTransactionId;
2413         s->subTransactionId = InvalidSubTransactionId;
2414         s->nestingLevel = 0;
2415         s->gucNestLevel = 0;
2416         s->childXids = NULL;
2417         s->nChildXids = 0;
2418         s->maxChildXids = 0;
2419
2420         /*
2421          * done with abort processing, set current transaction state back to
2422          * default
2423          */
2424         s->state = TRANS_DEFAULT;
2425 }
2426
2427 /*
2428  *      StartTransactionCommand
2429  */
2430 void
2431 StartTransactionCommand(void)
2432 {
2433         TransactionState s = CurrentTransactionState;
2434
2435         switch (s->blockState)
2436         {
2437                         /*
2438                          * if we aren't in a transaction block, we just do our usual start
2439                          * transaction.
2440                          */
2441                 case TBLOCK_DEFAULT:
2442                         StartTransaction();
2443                         s->blockState = TBLOCK_STARTED;
2444                         break;
2445
2446                         /*
2447                          * We are somewhere in a transaction block or subtransaction and
2448                          * about to start a new command.  For now we do nothing, but
2449                          * someday we may do command-local resource initialization. (Note
2450                          * that any needed CommandCounterIncrement was done by the
2451                          * previous CommitTransactionCommand.)
2452                          */
2453                 case TBLOCK_INPROGRESS:
2454                 case TBLOCK_SUBINPROGRESS:
2455                         break;
2456
2457                         /*
2458                          * Here we are in a failed transaction block (one of the commands
2459                          * caused an abort) so we do nothing but remain in the abort
2460                          * state.  Eventually we will get a ROLLBACK command which will
2461                          * get us out of this state.  (It is up to other code to ensure
2462                          * that no commands other than ROLLBACK will be processed in these
2463                          * states.)
2464                          */
2465                 case TBLOCK_ABORT:
2466                 case TBLOCK_SUBABORT:
2467                         break;
2468
2469                         /* These cases are invalid. */
2470                 case TBLOCK_STARTED:
2471                 case TBLOCK_BEGIN:
2472                 case TBLOCK_SUBBEGIN:
2473                 case TBLOCK_END:
2474                 case TBLOCK_SUBRELEASE:
2475                 case TBLOCK_SUBCOMMIT:
2476                 case TBLOCK_ABORT_END:
2477                 case TBLOCK_SUBABORT_END:
2478                 case TBLOCK_ABORT_PENDING:
2479                 case TBLOCK_SUBABORT_PENDING:
2480                 case TBLOCK_SUBRESTART:
2481                 case TBLOCK_SUBABORT_RESTART:
2482                 case TBLOCK_PREPARE:
2483                         elog(ERROR, "StartTransactionCommand: unexpected state %s",
2484                                  BlockStateAsString(s->blockState));
2485                         break;
2486         }
2487
2488         /*
2489          * We must switch to CurTransactionContext before returning. This is
2490          * already done if we called StartTransaction, otherwise not.
2491          */
2492         Assert(CurTransactionContext != NULL);
2493         MemoryContextSwitchTo(CurTransactionContext);
2494 }
2495
2496 /*
2497  *      CommitTransactionCommand
2498  */
2499 void
2500 CommitTransactionCommand(void)
2501 {
2502         TransactionState s = CurrentTransactionState;
2503
2504         switch (s->blockState)
2505         {
2506                         /*
2507                          * This shouldn't happen, because it means the previous
2508                          * StartTransactionCommand didn't set the STARTED state
2509                          * appropriately.
2510                          */
2511                 case TBLOCK_DEFAULT:
2512                         elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2513                                  BlockStateAsString(s->blockState));
2514                         break;
2515
2516                         /*
2517                          * If we aren't in a transaction block, just do our usual
2518                          * transaction commit, and return to the idle state.
2519                          */
2520                 case TBLOCK_STARTED:
2521                         CommitTransaction();
2522                         s->blockState = TBLOCK_DEFAULT;
2523                         break;
2524
2525                         /*
2526                          * We are completing a "BEGIN TRANSACTION" command, so we change
2527                          * to the "transaction block in progress" state and return.  (We
2528                          * assume the BEGIN did nothing to the database, so we need no
2529                          * CommandCounterIncrement.)
2530                          */
2531                 case TBLOCK_BEGIN:
2532                         s->blockState = TBLOCK_INPROGRESS;
2533                         break;
2534
2535                         /*
2536                          * This is the case when we have finished executing a command
2537                          * someplace within a transaction block.  We increment the command
2538                          * counter and return.
2539                          */
2540                 case TBLOCK_INPROGRESS:
2541                 case TBLOCK_SUBINPROGRESS:
2542                         CommandCounterIncrement();
2543                         break;
2544
2545                         /*
2546                          * We are completing a "COMMIT" command.  Do it and return to the
2547                          * idle state.
2548                          */
2549                 case TBLOCK_END:
2550                         CommitTransaction();
2551                         s->blockState = TBLOCK_DEFAULT;
2552                         break;
2553
2554                         /*
2555                          * Here we are in the middle of a transaction block but one of the
2556                          * commands caused an abort so we do nothing but remain in the
2557                          * abort state.  Eventually we will get a ROLLBACK comand.
2558                          */
2559                 case TBLOCK_ABORT:
2560                 case TBLOCK_SUBABORT:
2561                         break;
2562
2563                         /*
2564                          * Here we were in an aborted transaction block and we just got
2565                          * the ROLLBACK command from the user, so clean up the
2566                          * already-aborted transaction and return to the idle state.
2567                          */
2568                 case TBLOCK_ABORT_END:
2569                         CleanupTransaction();
2570                         s->blockState = TBLOCK_DEFAULT;
2571                         break;
2572
2573                         /*
2574                          * Here we were in a perfectly good transaction block but the user
2575                          * told us to ROLLBACK anyway.  We have to abort the transaction
2576                          * and then clean up.
2577                          */
2578                 case TBLOCK_ABORT_PENDING:
2579                         AbortTransaction();
2580                         CleanupTransaction();
2581                         s->blockState = TBLOCK_DEFAULT;
2582                         break;
2583
2584                         /*
2585                          * We are completing a "PREPARE TRANSACTION" command.  Do it and
2586                          * return to the idle state.
2587                          */
2588                 case TBLOCK_PREPARE:
2589                         PrepareTransaction();
2590                         s->blockState = TBLOCK_DEFAULT;
2591                         break;
2592
2593                         /*
2594                          * We were just issued a SAVEPOINT inside a transaction block.
2595                          * Start a subtransaction.      (DefineSavepoint already did
2596                          * PushTransaction, so as to have someplace to put the SUBBEGIN
2597                          * state.)
2598                          */
2599                 case TBLOCK_SUBBEGIN:
2600                         StartSubTransaction();
2601                         s->blockState = TBLOCK_SUBINPROGRESS;
2602                         break;
2603
2604                         /*
2605                          * We were issued a RELEASE command, so we end the
2606                          * current subtransaction and return to the parent transaction.
2607                          * The parent might be ended too, so repeat till we find an
2608                          * INPROGRESS transaction or subtransaction.
2609                          */
2610                 case TBLOCK_SUBRELEASE:
2611                         do
2612                         {
2613                                 CommitSubTransaction();
2614                                 s = CurrentTransactionState;    /* changed by pop */
2615                         } while (s->blockState == TBLOCK_SUBRELEASE);
2616
2617                         Assert(s->blockState == TBLOCK_INPROGRESS ||
2618                                    s->blockState == TBLOCK_SUBINPROGRESS);
2619                         break;
2620
2621                         /*
2622                          * We were issued a COMMIT, so we end the current subtransaction
2623                          * hierarchy and perform final commit. We do this by rolling up
2624                          * any subtransactions into their parent, which leads to O(N^2)
2625                          * operations with respect to resource owners - this isn't that
2626                          * bad until we approach a thousands of savepoints but is necessary
2627                          * for correctness should after triggers create new resource
2628                          * owners.
2629                          */
2630                 case TBLOCK_SUBCOMMIT:
2631                         do
2632                         {
2633                                 CommitSubTransaction();
2634                                 s = CurrentTransactionState;    /* changed by pop */
2635                         } while (s->blockState == TBLOCK_SUBCOMMIT);
2636                         /* If we had a COMMIT command, finish off the main xact too */
2637                         if (s->blockState == TBLOCK_END)
2638                         {
2639                                 Assert(s->parent == NULL);
2640                                 CommitTransaction();
2641                                 s->blockState = TBLOCK_DEFAULT;
2642                         }
2643                         else if (s->blockState == TBLOCK_PREPARE)
2644                         {
2645                                 Assert(s->parent == NULL);
2646                                 PrepareTransaction();
2647                                 s->blockState = TBLOCK_DEFAULT;
2648                         }
2649                         else
2650                                 elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2651                                          BlockStateAsString(s->blockState));
2652                         break;
2653
2654                         /*
2655                          * The current already-failed subtransaction is ending due to a
2656                          * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2657                          * examine the parent (which could be in any of several states).
2658                          */
2659                 case TBLOCK_SUBABORT_END:
2660                         CleanupSubTransaction();
2661                         CommitTransactionCommand();
2662                         break;
2663
2664                         /*
2665                          * As above, but it's not dead yet, so abort first.
2666                          */
2667                 case TBLOCK_SUBABORT_PENDING:
2668                         AbortSubTransaction();
2669                         CleanupSubTransaction();
2670                         CommitTransactionCommand();
2671                         break;
2672
2673                         /*
2674                          * The current subtransaction is the target of a ROLLBACK TO
2675                          * command.  Abort and pop it, then start a new subtransaction
2676                          * with the same name.
2677                          */
2678                 case TBLOCK_SUBRESTART:
2679                         {
2680                                 char       *name;
2681                                 int                     savepointLevel;
2682
2683                                 /* save name and keep Cleanup from freeing it */
2684                                 name = s->name;
2685                                 s->name = NULL;
2686                                 savepointLevel = s->savepointLevel;
2687
2688                                 AbortSubTransaction();
2689                                 CleanupSubTransaction();
2690
2691                                 DefineSavepoint(NULL);
2692                                 s = CurrentTransactionState;    /* changed by push */
2693                                 s->name = name;
2694                                 s->savepointLevel = savepointLevel;
2695
2696                                 /* This is the same as TBLOCK_SUBBEGIN case */
2697                                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2698                                 StartSubTransaction();
2699                                 s->blockState = TBLOCK_SUBINPROGRESS;
2700                         }
2701                         break;
2702
2703                         /*
2704                          * Same as above, but the subtransaction had already failed, so we
2705                          * don't need AbortSubTransaction.
2706                          */
2707                 case TBLOCK_SUBABORT_RESTART:
2708                         {
2709                                 char       *name;
2710                                 int                     savepointLevel;
2711
2712                                 /* save name and keep Cleanup from freeing it */
2713                                 name = s->name;
2714                                 s->name = NULL;
2715                                 savepointLevel = s->savepointLevel;
2716
2717                                 CleanupSubTransaction();
2718
2719                                 DefineSavepoint(NULL);
2720                                 s = CurrentTransactionState;    /* changed by push */
2721                                 s->name = name;
2722                                 s->savepointLevel = savepointLevel;
2723
2724                                 /* This is the same as TBLOCK_SUBBEGIN case */
2725                                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
2726                                 StartSubTransaction();
2727                                 s->blockState = TBLOCK_SUBINPROGRESS;
2728                         }
2729                         break;
2730         }
2731 }
2732
2733 /*
2734  *      AbortCurrentTransaction
2735  */
2736 void
2737 AbortCurrentTransaction(void)
2738 {
2739         TransactionState s = CurrentTransactionState;
2740
2741         switch (s->blockState)
2742         {
2743                 case TBLOCK_DEFAULT:
2744                         if (s->state == TRANS_DEFAULT)
2745                         {
2746                                 /* we are idle, so nothing to do */
2747                         }
2748                         else
2749                         {
2750                                 /*
2751                                  * We can get here after an error during transaction start
2752                                  * (state will be TRANS_START).  Need to clean up the
2753                                  * incompletely started transaction.  First, adjust the
2754                                  * low-level state to suppress warning message from
2755                                  * AbortTransaction.
2756                                  */
2757                                 if (s->state == TRANS_START)
2758                                         s->state = TRANS_INPROGRESS;
2759                                 AbortTransaction();
2760                                 CleanupTransaction();
2761                         }
2762                         break;
2763
2764                         /*
2765                          * if we aren't in a transaction block, we just do the basic abort
2766                          * & cleanup transaction.
2767                          */
2768                 case TBLOCK_STARTED:
2769                         AbortTransaction();
2770                         CleanupTransaction();
2771                         s->blockState = TBLOCK_DEFAULT;
2772                         break;
2773
2774                         /*
2775                          * If we are in TBLOCK_BEGIN it means something screwed up right
2776                          * after reading "BEGIN TRANSACTION".  We assume that the user
2777                          * will interpret the error as meaning the BEGIN failed to get him
2778                          * into a transaction block, so we should abort and return to idle
2779                          * state.
2780                          */
2781                 case TBLOCK_BEGIN:
2782                         AbortTransaction();
2783                         CleanupTransaction();
2784                         s->blockState = TBLOCK_DEFAULT;
2785                         break;
2786
2787                         /*
2788                          * We are somewhere in a transaction block and we've gotten a
2789                          * failure, so we abort the transaction and set up the persistent
2790                          * ABORT state.  We will stay in ABORT until we get a ROLLBACK.
2791                          */
2792                 case TBLOCK_INPROGRESS:
2793                         AbortTransaction();
2794                         s->blockState = TBLOCK_ABORT;
2795                         /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
2796                         break;
2797
2798                         /*
2799                          * Here, we failed while trying to COMMIT.      Clean up the
2800                          * transaction and return to idle state (we do not want to stay in
2801                          * the transaction).
2802                          */
2803                 case TBLOCK_END:
2804                         AbortTransaction();
2805                         CleanupTransaction();
2806                         s->blockState = TBLOCK_DEFAULT;
2807                         break;
2808
2809                         /*
2810                          * Here, we are already in an aborted transaction state and are
2811                          * waiting for a ROLLBACK, but for some reason we failed again! So
2812                          * we just remain in the abort state.
2813                          */
2814                 case TBLOCK_ABORT:
2815                 case TBLOCK_SUBABORT:
2816                         break;
2817
2818                         /*
2819                          * We are in a failed transaction and we got the ROLLBACK command.
2820                          * We have already aborted, we just need to cleanup and go to idle
2821                          * state.
2822                          */
2823                 case TBLOCK_ABORT_END:
2824                         CleanupTransaction();
2825                         s->blockState = TBLOCK_DEFAULT;
2826                         break;
2827
2828                         /*
2829                          * We are in a live transaction and we got a ROLLBACK command.
2830                          * Abort, cleanup, go to idle state.
2831                          */
2832                 case TBLOCK_ABORT_PENDING:
2833                         AbortTransaction();
2834                         CleanupTransaction();
2835                         s->blockState = TBLOCK_DEFAULT;
2836                         break;
2837
2838                         /*
2839                          * Here, we failed while trying to PREPARE.  Clean up the
2840                          * transaction and return to idle state (we do not want to stay in
2841                          * the transaction).
2842                          */
2843                 case TBLOCK_PREPARE:
2844                         AbortTransaction();
2845                         CleanupTransaction();
2846                         s->blockState = TBLOCK_DEFAULT;
2847                         break;
2848
2849                         /*
2850                          * We got an error inside a subtransaction.  Abort just the
2851                          * subtransaction, and go to the persistent SUBABORT state until
2852                          * we get ROLLBACK.
2853                          */
2854                 case TBLOCK_SUBINPROGRESS:
2855                         AbortSubTransaction();
2856                         s->blockState = TBLOCK_SUBABORT;
2857                         break;
2858
2859                         /*
2860                          * If we failed while trying to create a subtransaction, clean up
2861                          * the broken subtransaction and abort the parent.      The same
2862                          * applies if we get a failure while ending a subtransaction.
2863                          */
2864                 case TBLOCK_SUBBEGIN:
2865                 case TBLOCK_SUBRELEASE:
2866                 case TBLOCK_SUBCOMMIT:
2867                 case TBLOCK_SUBABORT_PENDING:
2868                 case TBLOCK_SUBRESTART:
2869                         AbortSubTransaction();
2870                         CleanupSubTransaction();
2871                         AbortCurrentTransaction();
2872                         break;
2873
2874                         /*
2875                          * Same as above, except the Abort() was already done.
2876                          */
2877                 case TBLOCK_SUBABORT_END:
2878                 case TBLOCK_SUBABORT_RESTART:
2879                         CleanupSubTransaction();
2880                         AbortCurrentTransaction();
2881                         break;
2882         }
2883 }
2884
2885 /*
2886  *      PreventTransactionChain
2887  *
2888  *      This routine is to be called by statements that must not run inside
2889  *      a transaction block, typically because they have non-rollback-able
2890  *      side effects or do internal commits.
2891  *
2892  *      If we have already started a transaction block, issue an error; also issue
2893  *      an error if we appear to be running inside a user-defined function (which
2894  *      could issue more commands and possibly cause a failure after the statement
2895  *      completes).  Subtransactions are verboten too.
2896  *
2897  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2898  *      inside a function or multi-query querystring.  (We will always fail if
2899  *      this is false, but it's convenient to centralize the check here instead of
2900  *      making callers do it.)
2901  *      stmtType: statement type name, for error messages.
2902  */
2903 void
2904 PreventTransactionChain(bool isTopLevel, const char *stmtType)
2905 {
2906         /*
2907          * xact block already started?
2908          */
2909         if (IsTransactionBlock())
2910                 ereport(ERROR,
2911                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2912                 /* translator: %s represents an SQL statement name */
2913                                  errmsg("%s cannot run inside a transaction block",
2914                                                 stmtType)));
2915
2916         /*
2917          * subtransaction?
2918          */
2919         if (IsSubTransaction())
2920                 ereport(ERROR,
2921                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2922                 /* translator: %s represents an SQL statement name */
2923                                  errmsg("%s cannot run inside a subtransaction",
2924                                                 stmtType)));
2925
2926         /*
2927          * inside a function call?
2928          */
2929         if (!isTopLevel)
2930                 ereport(ERROR,
2931                                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
2932                 /* translator: %s represents an SQL statement name */
2933                                  errmsg("%s cannot be executed from a function or multi-command string",
2934                                                 stmtType)));
2935
2936         /* If we got past IsTransactionBlock test, should be in default state */
2937         if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
2938                 CurrentTransactionState->blockState != TBLOCK_STARTED)
2939                 elog(FATAL, "cannot prevent transaction chain");
2940         /* all okay */
2941 }
2942
2943 /*
2944  *      RequireTransactionChain
2945  *
2946  *      This routine is to be called by statements that must run inside
2947  *      a transaction block, because they have no effects that persist past
2948  *      transaction end (and so calling them outside a transaction block
2949  *      is presumably an error).  DECLARE CURSOR is an example.
2950  *
2951  *      If we appear to be running inside a user-defined function, we do not
2952  *      issue an error, since the function could issue more commands that make
2953  *      use of the current statement's results.  Likewise subtransactions.
2954  *      Thus this is an inverse for PreventTransactionChain.
2955  *
2956  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2957  *      inside a function.
2958  *      stmtType: statement type name, for error messages.
2959  */
2960 void
2961 RequireTransactionChain(bool isTopLevel, const char *stmtType)
2962 {
2963         /*
2964          * xact block already started?
2965          */
2966         if (IsTransactionBlock())
2967                 return;
2968
2969         /*
2970          * subtransaction?
2971          */
2972         if (IsSubTransaction())
2973                 return;
2974
2975         /*
2976          * inside a function call?
2977          */
2978         if (!isTopLevel)
2979                 return;
2980
2981         ereport(ERROR,
2982                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
2983         /* translator: %s represents an SQL statement name */
2984                          errmsg("%s can only be used in transaction blocks",
2985                                         stmtType)));
2986 }
2987
2988 /*
2989  *      IsInTransactionChain
2990  *
2991  *      This routine is for statements that need to behave differently inside
2992  *      a transaction block than when running as single commands.  ANALYZE is
2993  *      currently the only example.
2994  *
2995  *      isTopLevel: passed down from ProcessUtility to determine whether we are
2996  *      inside a function.
2997  */
2998 bool
2999 IsInTransactionChain(bool isTopLevel)
3000 {
3001         /*
3002          * Return true on same conditions that would make PreventTransactionChain
3003          * error out
3004          */
3005         if (IsTransactionBlock())
3006                 return true;
3007
3008         if (IsSubTransaction())
3009                 return true;
3010
3011         if (!isTopLevel)
3012                 return true;
3013
3014         if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3015                 CurrentTransactionState->blockState != TBLOCK_STARTED)
3016                 return true;
3017
3018         return false;
3019 }
3020
3021
3022 /*
3023  * Register or deregister callback functions for start- and end-of-xact
3024  * operations.
3025  *
3026  * These functions are intended for use by dynamically loaded modules.
3027  * For built-in modules we generally just hardwire the appropriate calls
3028  * (mainly because it's easier to control the order that way, where needed).
3029  *
3030  * At transaction end, the callback occurs post-commit or post-abort, so the
3031  * callback functions can only do noncritical cleanup.
3032  */
3033 void
3034 RegisterXactCallback(XactCallback callback, void *arg)
3035 {
3036         XactCallbackItem *item;
3037
3038         item = (XactCallbackItem *)
3039                 MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
3040         item->callback = callback;
3041         item->arg = arg;
3042         item->next = Xact_callbacks;
3043         Xact_callbacks = item;
3044 }
3045
3046 void
3047 UnregisterXactCallback(XactCallback callback, void *arg)
3048 {
3049         XactCallbackItem *item;
3050         XactCallbackItem *prev;
3051
3052         prev = NULL;
3053         for (item = Xact_callbacks; item; prev = item, item = item->next)
3054         {
3055                 if (item->callback == callback && item->arg == arg)
3056                 {
3057                         if (prev)
3058                                 prev->next = item->next;
3059                         else
3060                                 Xact_callbacks = item->next;
3061                         pfree(item);
3062                         break;
3063                 }
3064         }
3065 }
3066
3067 static void
3068 CallXactCallbacks(XactEvent event)
3069 {
3070         XactCallbackItem *item;
3071
3072         for (item = Xact_callbacks; item; item = item->next)
3073                 (*item->callback) (event, item->arg);
3074 }
3075
3076
3077 /*
3078  * Register or deregister callback functions for start- and end-of-subxact
3079  * operations.
3080  *
3081  * Pretty much same as above, but for subtransaction events.
3082  *
3083  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3084  * so the callback functions can only do noncritical cleanup.  At
3085  * subtransaction start, the callback is called when the subtransaction has
3086  * finished initializing.
3087  */
3088 void
3089 RegisterSubXactCallback(SubXactCallback callback, void *arg)
3090 {
3091         SubXactCallbackItem *item;
3092
3093         item = (SubXactCallbackItem *)
3094                 MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
3095         item->callback = callback;
3096         item->arg = arg;
3097         item->next = SubXact_callbacks;
3098         SubXact_callbacks = item;
3099 }
3100
3101 void
3102 UnregisterSubXactCallback(SubXactCallback callback, void *arg)
3103 {
3104         SubXactCallbackItem *item;
3105         SubXactCallbackItem *prev;
3106
3107         prev = NULL;
3108         for (item = SubXact_callbacks; item; prev = item, item = item->next)
3109         {
3110                 if (item->callback == callback && item->arg == arg)
3111                 {
3112                         if (prev)
3113                                 prev->next = item->next;
3114                         else
3115                                 SubXact_callbacks = item->next;
3116                         pfree(item);
3117                         break;
3118                 }
3119         }
3120 }
3121
3122 static void
3123 CallSubXactCallbacks(SubXactEvent event,
3124                                          SubTransactionId mySubid,
3125                                          SubTransactionId parentSubid)
3126 {
3127         SubXactCallbackItem *item;
3128
3129         for (item = SubXact_callbacks; item; item = item->next)
3130                 (*item->callback) (event, mySubid, parentSubid, item->arg);
3131 }
3132
3133
3134 /* ----------------------------------------------------------------
3135  *                                         transaction block support
3136  * ----------------------------------------------------------------
3137  */
3138
3139 /*
3140  *      BeginTransactionBlock
3141  *              This executes a BEGIN command.
3142  */
3143 void
3144 BeginTransactionBlock(void)
3145 {
3146         TransactionState s = CurrentTransactionState;
3147
3148         switch (s->blockState)
3149         {
3150                         /*
3151                          * We are not inside a transaction block, so allow one to begin.
3152                          */
3153                 case TBLOCK_STARTED:
3154                         s->blockState = TBLOCK_BEGIN;
3155                         break;
3156
3157                         /*
3158                          * Already a transaction block in progress.
3159                          */
3160                 case TBLOCK_INPROGRESS:
3161                 case TBLOCK_SUBINPROGRESS:
3162                 case TBLOCK_ABORT:
3163                 case TBLOCK_SUBABORT:
3164                         ereport(WARNING,
3165                                         (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3166                                          errmsg("there is already a transaction in progress")));
3167                         break;
3168
3169                         /* These cases are invalid. */
3170                 case TBLOCK_DEFAULT:
3171                 case TBLOCK_BEGIN:
3172                 case TBLOCK_SUBBEGIN:
3173                 case TBLOCK_END:
3174                 case TBLOCK_SUBRELEASE:
3175                 case TBLOCK_SUBCOMMIT:
3176                 case TBLOCK_ABORT_END:
3177                 case TBLOCK_SUBABORT_END:
3178                 case TBLOCK_ABORT_PENDING:
3179                 case TBLOCK_SUBABORT_PENDING:
3180                 case TBLOCK_SUBRESTART:
3181                 case TBLOCK_SUBABORT_RESTART:
3182                 case TBLOCK_PREPARE:
3183                         elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3184                                  BlockStateAsString(s->blockState));
3185                         break;
3186         }
3187 }
3188
3189 /*
3190  *      PrepareTransactionBlock
3191  *              This executes a PREPARE command.
3192  *
3193  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3194  * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3195  *
3196  * Note that we don't actually do anything here except change blockState.
3197  * The real work will be done in the upcoming PrepareTransaction().
3198  * We do it this way because it's not convenient to change memory context,
3199  * resource owner, etc while executing inside a Portal.
3200  */
3201 bool
3202 PrepareTransactionBlock(char *gid)
3203 {
3204         TransactionState s;
3205         bool            result;
3206
3207         /* Set up to commit the current transaction */
3208         result = EndTransactionBlock();
3209
3210         /* If successful, change outer tblock state to PREPARE */
3211         if (result)
3212         {
3213                 s = CurrentTransactionState;
3214
3215                 while (s->parent != NULL)
3216                         s = s->parent;
3217
3218                 if (s->blockState == TBLOCK_END)
3219                 {
3220                         /* Save GID where PrepareTransaction can find it again */
3221                         prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
3222
3223                         s->blockState = TBLOCK_PREPARE;
3224                 }
3225                 else
3226                 {
3227                         /*
3228                          * ignore case where we are not in a transaction;
3229                          * EndTransactionBlock already issued a warning.
3230                          */
3231                         Assert(s->blockState == TBLOCK_STARTED);
3232                         /* Don't send back a PREPARE result tag... */
3233                         result = false;
3234                 }
3235         }
3236
3237         return result;
3238 }
3239
3240 /*
3241  *      EndTransactionBlock
3242  *              This executes a COMMIT command.
3243  *
3244  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3245  * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3246  *
3247  * Note that we don't actually do anything here except change blockState.
3248  * The real work will be done in the upcoming CommitTransactionCommand().
3249  * We do it this way because it's not convenient to change memory context,
3250  * resource owner, etc while executing inside a Portal.
3251  */
3252 bool
3253 EndTransactionBlock(void)
3254 {
3255         TransactionState s = CurrentTransactionState;
3256         bool            result = false;
3257
3258         switch (s->blockState)
3259         {
3260                         /*
3261                          * We are in a transaction block, so tell CommitTransactionCommand
3262                          * to COMMIT.
3263                          */
3264                 case TBLOCK_INPROGRESS:
3265                         s->blockState = TBLOCK_END;
3266                         result = true;
3267                         break;
3268
3269                         /*
3270                          * We are in a failed transaction block.  Tell
3271                          * CommitTransactionCommand it's time to exit the block.
3272                          */
3273                 case TBLOCK_ABORT:
3274                         s->blockState = TBLOCK_ABORT_END;
3275                         break;
3276
3277                         /*
3278                          * We are in a live subtransaction block.  Set up to subcommit all
3279                          * open subtransactions and then commit the main transaction.
3280                          */
3281                 case TBLOCK_SUBINPROGRESS:
3282                         while (s->parent != NULL)
3283                         {
3284                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3285                                         s->blockState = TBLOCK_SUBCOMMIT;
3286                                 else
3287                                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3288                                                  BlockStateAsString(s->blockState));
3289                                 s = s->parent;
3290                         }
3291                         if (s->blockState == TBLOCK_INPROGRESS)
3292                                 s->blockState = TBLOCK_END;
3293                         else
3294                                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3295                                          BlockStateAsString(s->blockState));
3296                         result = true;
3297                         break;
3298
3299                         /*
3300                          * Here we are inside an aborted subtransaction.  Treat the COMMIT
3301                          * as ROLLBACK: set up to abort everything and exit the main
3302                          * transaction.
3303                          */
3304                 case TBLOCK_SUBABORT:
3305                         while (s->parent != NULL)
3306                         {
3307                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3308                                         s->blockState = TBLOCK_SUBABORT_PENDING;
3309                                 else if (s->blockState == TBLOCK_SUBABORT)
3310                                         s->blockState = TBLOCK_SUBABORT_END;
3311                                 else
3312                                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3313                                                  BlockStateAsString(s->blockState));
3314                                 s = s->parent;
3315                         }
3316                         if (s->blockState == TBLOCK_INPROGRESS)
3317                                 s->blockState = TBLOCK_ABORT_PENDING;
3318                         else if (s->blockState == TBLOCK_ABORT)
3319                                 s->blockState = TBLOCK_ABORT_END;
3320                         else
3321                                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
3322                                          BlockStateAsString(s->blockState));
3323                         break;
3324
3325                         /*
3326                          * The user issued COMMIT when not inside a transaction.  Issue a
3327                          * WARNING, staying in TBLOCK_STARTED state.  The upcoming call to
3328                          * CommitTransactionCommand() will then close the transaction and
3329                          * put us back into the default state.
3330                          */
3331                 case TBLOCK_STARTED:
3332                         ereport(WARNING,
3333                                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3334                                          errmsg("there is no transaction in progress")));
3335                         result = true;
3336                         break;
3337
3338                         /* These cases are invalid. */
3339                 case TBLOCK_DEFAULT:
3340                 case TBLOCK_BEGIN:
3341                 case TBLOCK_SUBBEGIN:
3342                 case TBLOCK_END:
3343                 case TBLOCK_SUBRELEASE:
3344                 case TBLOCK_SUBCOMMIT:
3345                 case TBLOCK_ABORT_END:
3346                 case TBLOCK_SUBABORT_END:
3347                 case TBLOCK_ABORT_PENDING:
3348                 case TBLOCK_SUBABORT_PENDING:
3349                 case TBLOCK_SUBRESTART:
3350                 case TBLOCK_SUBABORT_RESTART:
3351                 case TBLOCK_PREPARE:
3352                         elog(FATAL, "EndTransactionBlock: unexpected state %s",
3353                                  BlockStateAsString(s->blockState));
3354                         break;
3355         }
3356
3357         return result;
3358 }
3359
3360 /*
3361  *      UserAbortTransactionBlock
3362  *              This executes a ROLLBACK command.
3363  *
3364  * As above, we don't actually do anything here except change blockState.
3365  */
3366 void
3367 UserAbortTransactionBlock(void)
3368 {
3369         TransactionState s = CurrentTransactionState;
3370
3371         switch (s->blockState)
3372         {
3373                         /*
3374                          * We are inside a transaction block and we got a ROLLBACK command
3375                          * from the user, so tell CommitTransactionCommand to abort and
3376                          * exit the transaction block.
3377                          */
3378                 case TBLOCK_INPROGRESS:
3379                         s->blockState = TBLOCK_ABORT_PENDING;
3380                         break;
3381
3382                         /*
3383                          * We are inside a failed transaction block and we got a ROLLBACK
3384                          * command from the user.  Abort processing is already done, so
3385                          * CommitTransactionCommand just has to cleanup and go back to
3386                          * idle state.
3387                          */
3388                 case TBLOCK_ABORT:
3389                         s->blockState = TBLOCK_ABORT_END;
3390                         break;
3391
3392                         /*
3393                          * We are inside a subtransaction.      Mark everything up to top
3394                          * level as exitable.
3395                          */
3396                 case TBLOCK_SUBINPROGRESS:
3397                 case TBLOCK_SUBABORT:
3398                         while (s->parent != NULL)
3399                         {
3400                                 if (s->blockState == TBLOCK_SUBINPROGRESS)
3401                                         s->blockState = TBLOCK_SUBABORT_PENDING;
3402                                 else if (s->blockState == TBLOCK_SUBABORT)
3403                                         s->blockState = TBLOCK_SUBABORT_END;
3404                                 else
3405                                         elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3406                                                  BlockStateAsString(s->blockState));
3407                                 s = s->parent;
3408                         }
3409                         if (s->blockState == TBLOCK_INPROGRESS)
3410                                 s->blockState = TBLOCK_ABORT_PENDING;
3411                         else if (s->blockState == TBLOCK_ABORT)
3412                                 s->blockState = TBLOCK_ABORT_END;
3413                         else
3414                                 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3415                                          BlockStateAsString(s->blockState));
3416                         break;
3417
3418                         /*
3419                          * The user issued ABORT when not inside a transaction. Issue a
3420                          * WARNING and go to abort state.  The upcoming call to
3421                          * CommitTransactionCommand() will then put us back into the
3422                          * default state.
3423                          */
3424                 case TBLOCK_STARTED:
3425                         ereport(NOTICE,
3426                                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3427                                          errmsg("there is no transaction in progress")));
3428                         s->blockState = TBLOCK_ABORT_PENDING;
3429                         break;
3430
3431                         /* These cases are invalid. */
3432                 case TBLOCK_DEFAULT:
3433                 case TBLOCK_BEGIN:
3434                 case TBLOCK_SUBBEGIN:
3435                 case TBLOCK_END:
3436                 case TBLOCK_SUBRELEASE:
3437                 case TBLOCK_SUBCOMMIT:
3438                 case TBLOCK_ABORT_END:
3439                 case TBLOCK_SUBABORT_END:
3440                 case TBLOCK_ABORT_PENDING:
3441                 case TBLOCK_SUBABORT_PENDING:
3442                 case TBLOCK_SUBRESTART:
3443                 case TBLOCK_SUBABORT_RESTART:
3444                 case TBLOCK_PREPARE:
3445                         elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3446                                  BlockStateAsString(s->blockState));
3447                         break;
3448         }
3449 }
3450
3451 /*
3452  * DefineSavepoint
3453  *              This executes a SAVEPOINT command.
3454  */
3455 void
3456 DefineSavepoint(char *name)
3457 {
3458         TransactionState s = CurrentTransactionState;
3459
3460         switch (s->blockState)
3461         {
3462                 case TBLOCK_INPROGRESS:
3463                 case TBLOCK_SUBINPROGRESS:
3464                         /* Normal subtransaction start */
3465                         PushTransaction();
3466                         s = CurrentTransactionState;            /* changed by push */
3467
3468                         /*
3469                          * Savepoint names, like the TransactionState block itself, live
3470                          * in TopTransactionContext.
3471                          */
3472                         if (name)
3473                                 s->name = MemoryContextStrdup(TopTransactionContext, name);
3474                         break;
3475
3476                         /* These cases are invalid. */
3477                 case TBLOCK_DEFAULT:
3478                 case TBLOCK_STARTED:
3479                 case TBLOCK_BEGIN:
3480                 case TBLOCK_SUBBEGIN:
3481                 case TBLOCK_END:
3482                 case TBLOCK_SUBRELEASE:
3483                 case TBLOCK_SUBCOMMIT:
3484                 case TBLOCK_ABORT:
3485                 case TBLOCK_SUBABORT:
3486                 case TBLOCK_ABORT_END:
3487                 case TBLOCK_SUBABORT_END:
3488                 case TBLOCK_ABORT_PENDING:
3489                 case TBLOCK_SUBABORT_PENDING:
3490                 case TBLOCK_SUBRESTART:
3491                 case TBLOCK_SUBABORT_RESTART:
3492                 case TBLOCK_PREPARE:
3493                         elog(FATAL, "DefineSavepoint: unexpected state %s",
3494                                  BlockStateAsString(s->blockState));
3495                         break;
3496         }
3497 }
3498
3499 /*
3500  * ReleaseSavepoint
3501  *              This executes a RELEASE command.
3502  *
3503  * As above, we don't actually do anything here except change blockState.
3504  */
3505 void
3506 ReleaseSavepoint(List *options)
3507 {
3508         TransactionState s = CurrentTransactionState;
3509         TransactionState target,
3510                                 xact;
3511         ListCell   *cell;
3512         char       *name = NULL;
3513
3514         switch (s->blockState)
3515         {
3516                         /*
3517                          * We can't rollback to a savepoint if there is no savepoint
3518                          * defined.
3519                          */
3520                 case TBLOCK_INPROGRESS:
3521                         ereport(ERROR,
3522                                         (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3523                                          errmsg("no such savepoint")));
3524                         break;
3525
3526                         /*
3527                          * We are in a non-aborted subtransaction.      This is the only valid
3528                          * case.
3529                          */
3530                 case TBLOCK_SUBINPROGRESS:
3531                         break;
3532
3533                         /* These cases are invalid. */
3534                 case TBLOCK_DEFAULT:
3535                 case TBLOCK_STARTED:
3536                 case TBLOCK_BEGIN:
3537                 case TBLOCK_SUBBEGIN:
3538                 case TBLOCK_END:
3539                 case TBLOCK_SUBRELEASE:
3540                 case TBLOCK_SUBCOMMIT:
3541                 case TBLOCK_ABORT:
3542                 case TBLOCK_SUBABORT:
3543                 case TBLOCK_ABORT_END:
3544                 case TBLOCK_SUBABORT_END:
3545                 case TBLOCK_ABORT_PENDING:
3546                 case TBLOCK_SUBABORT_PENDING:
3547                 case TBLOCK_SUBRESTART:
3548                 case TBLOCK_SUBABORT_RESTART:
3549                 case TBLOCK_PREPARE:
3550                         elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3551                                  BlockStateAsString(s->blockState));
3552                         break;
3553         }
3554
3555         foreach(cell, options)
3556         {
3557                 DefElem    *elem = lfirst(cell);
3558
3559                 if (strcmp(elem->defname, "savepoint_name") == 0)
3560                         name = strVal(elem->arg);
3561         }
3562
3563         Assert(PointerIsValid(name));
3564
3565         for (target = s; PointerIsValid(target); target = target->parent)
3566         {
3567                 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3568                         break;
3569         }
3570
3571         if (!PointerIsValid(target))
3572                 ereport(ERROR,
3573                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3574                                  errmsg("no such savepoint")));
3575
3576         /* disallow crossing savepoint level boundaries */
3577         if (target->savepointLevel != s->savepointLevel)
3578                 ereport(ERROR,
3579                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3580                                  errmsg("no such savepoint")));
3581
3582         /*
3583          * Mark "commit pending" all subtransactions up to the target
3584          * subtransaction.      The actual commits will happen when control gets to
3585          * CommitTransactionCommand.
3586          */
3587         xact = CurrentTransactionState;
3588         for (;;)
3589         {
3590                 Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
3591                 xact->blockState = TBLOCK_SUBRELEASE;
3592                 if (xact == target)
3593                         break;
3594                 xact = xact->parent;
3595                 Assert(PointerIsValid(xact));
3596         }
3597 }
3598
3599 /*
3600  * RollbackToSavepoint
3601  *              This executes a ROLLBACK TO <savepoint> command.
3602  *
3603  * As above, we don't actually do anything here except change blockState.
3604  */
3605 void
3606 RollbackToSavepoint(List *options)
3607 {
3608         TransactionState s = CurrentTransactionState;
3609         TransactionState target,
3610                                 xact;
3611         ListCell   *cell;
3612         char       *name = NULL;
3613
3614         switch (s->blockState)
3615         {
3616                         /*
3617                          * We can't rollback to a savepoint if there is no savepoint
3618                          * defined.
3619                          */
3620                 case TBLOCK_INPROGRESS:
3621                 case TBLOCK_ABORT:
3622                         ereport(ERROR,
3623                                         (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3624                                          errmsg("no such savepoint")));
3625                         break;
3626
3627                         /*
3628                          * There is at least one savepoint, so proceed.
3629                          */
3630                 case TBLOCK_SUBINPROGRESS:
3631                 case TBLOCK_SUBABORT:
3632                         break;
3633
3634                         /* These cases are invalid. */
3635                 case TBLOCK_DEFAULT:
3636                 case TBLOCK_STARTED:
3637                 case TBLOCK_BEGIN:
3638                 case TBLOCK_SUBBEGIN:
3639                 case TBLOCK_END:
3640                 case TBLOCK_SUBRELEASE:
3641                 case TBLOCK_SUBCOMMIT:
3642                 case TBLOCK_ABORT_END:
3643                 case TBLOCK_SUBABORT_END:
3644                 case TBLOCK_ABORT_PENDING:
3645                 case TBLOCK_SUBABORT_PENDING:
3646                 case TBLOCK_SUBRESTART:
3647                 case TBLOCK_SUBABORT_RESTART:
3648                 case TBLOCK_PREPARE:
3649                         elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3650                                  BlockStateAsString(s->blockState));
3651                         break;
3652         }
3653
3654         foreach(cell, options)
3655         {
3656                 DefElem    *elem = lfirst(cell);
3657
3658                 if (strcmp(elem->defname, "savepoint_name") == 0)
3659                         name = strVal(elem->arg);
3660         }
3661
3662         Assert(PointerIsValid(name));
3663
3664         for (target = s; PointerIsValid(target); target = target->parent)
3665         {
3666                 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3667                         break;
3668         }
3669
3670         if (!PointerIsValid(target))
3671                 ereport(ERROR,
3672                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3673                                  errmsg("no such savepoint")));
3674
3675         /* disallow crossing savepoint level boundaries */
3676         if (target->savepointLevel != s->savepointLevel)
3677                 ereport(ERROR,
3678                                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3679                                  errmsg("no such savepoint")));
3680
3681         /*
3682          * Mark "abort pending" all subtransactions up to the target
3683          * subtransaction.      The actual aborts will happen when control gets to
3684          * CommitTransactionCommand.
3685          */
3686         xact = CurrentTransactionState;
3687         for (;;)
3688         {
3689                 if (xact == target)
3690                         break;
3691                 if (xact->blockState == TBLOCK_SUBINPROGRESS)
3692                         xact->blockState = TBLOCK_SUBABORT_PENDING;
3693                 else if (xact->blockState == TBLOCK_SUBABORT)
3694                         xact->blockState = TBLOCK_SUBABORT_END;
3695                 else
3696                         elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3697                                  BlockStateAsString(xact->blockState));
3698                 xact = xact->parent;
3699                 Assert(PointerIsValid(xact));
3700         }
3701
3702         /* And mark the target as "restart pending" */
3703         if (xact->blockState == TBLOCK_SUBINPROGRESS)
3704                 xact->blockState = TBLOCK_SUBRESTART;
3705         else if (xact->blockState == TBLOCK_SUBABORT)
3706                 xact->blockState = TBLOCK_SUBABORT_RESTART;
3707         else
3708                 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3709                          BlockStateAsString(xact->blockState));
3710 }
3711
3712 /*
3713  * BeginInternalSubTransaction
3714  *              This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
3715  *              TBLOCK_END, and TBLOCK_PREPARE states, and therefore it can safely be
3716  *              used in functions that might be called when not inside a BEGIN block
3717  *              or when running deferred triggers at COMMIT/PREPARE time.  Also, it
3718  *              automatically does CommitTransactionCommand/StartTransactionCommand
3719  *              instead of expecting the caller to do it.
3720  */
3721 void
3722 BeginInternalSubTransaction(char *name)
3723 {
3724         TransactionState s = CurrentTransactionState;
3725
3726         switch (s->blockState)
3727         {
3728                 case TBLOCK_STARTED:
3729                 case TBLOCK_INPROGRESS:
3730                 case TBLOCK_END:
3731                 case TBLOCK_PREPARE:
3732                 case TBLOCK_SUBINPROGRESS:
3733                         /* Normal subtransaction start */
3734                         PushTransaction();
3735                         s = CurrentTransactionState;            /* changed by push */
3736
3737                         /*
3738                          * Savepoint names, like the TransactionState block itself, live
3739                          * in TopTransactionContext.
3740                          */
3741                         if (name)
3742                                 s->name = MemoryContextStrdup(TopTransactionContext, name);
3743                         break;
3744
3745                         /* These cases are invalid. */
3746                 case TBLOCK_DEFAULT:
3747                 case TBLOCK_BEGIN:
3748                 case TBLOCK_SUBBEGIN:
3749                 case TBLOCK_SUBRELEASE:
3750                 case TBLOCK_SUBCOMMIT:
3751                 case TBLOCK_ABORT:
3752                 case TBLOCK_SUBABORT:
3753                 case TBLOCK_ABORT_END:
3754                 case TBLOCK_SUBABORT_END:
3755                 case TBLOCK_ABORT_PENDING:
3756                 case TBLOCK_SUBABORT_PENDING:
3757                 case TBLOCK_SUBRESTART:
3758                 case TBLOCK_SUBABORT_RESTART:
3759                         elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
3760                                  BlockStateAsString(s->blockState));
3761                         break;
3762         }
3763
3764         CommitTransactionCommand();
3765         StartTransactionCommand();
3766 }
3767
3768 /*
3769  * ReleaseCurrentSubTransaction
3770  *
3771  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
3772  * savepoint name (if any).
3773  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3774  */
3775 void
3776 ReleaseCurrentSubTransaction(void)
3777 {
3778         TransactionState s = CurrentTransactionState;
3779
3780         if (s->blockState != TBLOCK_SUBINPROGRESS)
3781                 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
3782                          BlockStateAsString(s->blockState));
3783         Assert(s->state == TRANS_INPROGRESS);
3784         MemoryContextSwitchTo(CurTransactionContext);
3785         CommitSubTransaction();
3786         s = CurrentTransactionState;    /* changed by pop */
3787         Assert(s->state == TRANS_INPROGRESS);
3788 }
3789
3790 /*
3791  * RollbackAndReleaseCurrentSubTransaction
3792  *
3793  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
3794  * of its savepoint name (if any).
3795  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
3796  */
3797 void
3798 RollbackAndReleaseCurrentSubTransaction(void)
3799 {
3800         TransactionState s = CurrentTransactionState;
3801
3802         switch (s->blockState)
3803         {
3804                         /* Must be in a subtransaction */
3805                 case TBLOCK_SUBINPROGRESS:
3806                 case TBLOCK_SUBABORT:
3807                         break;
3808
3809                         /* These cases are invalid. */
3810                 case TBLOCK_DEFAULT:
3811                 case TBLOCK_STARTED:
3812                 case TBLOCK_BEGIN:
3813                 case TBLOCK_SUBBEGIN:
3814                 case TBLOCK_INPROGRESS:
3815                 case TBLOCK_END:
3816                 case TBLOCK_SUBRELEASE:
3817                 case TBLOCK_SUBCOMMIT:
3818                 case TBLOCK_ABORT:
3819                 case TBLOCK_ABORT_END:
3820                 case TBLOCK_SUBABORT_END:
3821                 case TBLOCK_ABORT_PENDING:
3822                 case TBLOCK_SUBABORT_PENDING:
3823                 case TBLOCK_SUBRESTART:
3824                 case TBLOCK_SUBABORT_RESTART:
3825                 case TBLOCK_PREPARE:
3826                         elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
3827                                  BlockStateAsString(s->blockState));
3828                         break;
3829         }
3830
3831         /*
3832          * Abort the current subtransaction, if needed.
3833          */
3834         if (s->blockState == TBLOCK_SUBINPROGRESS)
3835                 AbortSubTransaction();
3836
3837         /* And clean it up, too */
3838         CleanupSubTransaction();
3839
3840         s = CurrentTransactionState;    /* changed by pop */
3841         AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
3842                                 s->blockState == TBLOCK_INPROGRESS ||
3843                                 s->blockState == TBLOCK_STARTED);
3844 }
3845
3846 /*
3847  *      AbortOutOfAnyTransaction
3848  *
3849  *      This routine is provided for error recovery purposes.  It aborts any
3850  *      active transaction or transaction block, leaving the system in a known
3851  *      idle state.
3852  */
3853 void
3854 AbortOutOfAnyTransaction(void)
3855 {
3856         TransactionState s = CurrentTransactionState;
3857
3858         /*
3859          * Get out of any transaction or nested transaction
3860          */
3861         do
3862         {
3863                 switch (s->blockState)
3864                 {
3865                         case TBLOCK_DEFAULT:
3866                                 if (s->state == TRANS_DEFAULT)
3867                                 {
3868                                         /* Not in a transaction, do nothing */
3869                                 }
3870                                 else
3871                                 {
3872                                         /*
3873                                          * We can get here after an error during transaction start
3874                                          * (state will be TRANS_START).  Need to clean up the
3875                                          * incompletely started transaction.  First, adjust the
3876                                          * low-level state to suppress warning message from
3877                                          * AbortTransaction.
3878                                          */
3879                                         if (s->state == TRANS_START)
3880                                                 s->state = TRANS_INPROGRESS;
3881                                         AbortTransaction();
3882                                         CleanupTransaction();
3883                                 }
3884                                 break;
3885                         case TBLOCK_STARTED:
3886                         case TBLOCK_BEGIN:
3887                         case TBLOCK_INPROGRESS:
3888                         case TBLOCK_END:
3889                         case TBLOCK_ABORT_PENDING:
3890                         case TBLOCK_PREPARE:
3891                                 /* In a transaction, so clean up */
3892                                 AbortTransaction();
3893                                 CleanupTransaction();
3894                                 s->blockState = TBLOCK_DEFAULT;
3895                                 break;
3896                         case TBLOCK_ABORT:
3897                         case TBLOCK_ABORT_END:
3898                                 /* AbortTransaction already done, still need Cleanup */
3899                                 CleanupTransaction();
3900                                 s->blockState = TBLOCK_DEFAULT;
3901                                 break;
3902
3903                                 /*
3904                                  * In a subtransaction, so clean it up and abort parent too
3905                                  */
3906                         case TBLOCK_SUBBEGIN:
3907                         case TBLOCK_SUBINPROGRESS:
3908                         case TBLOCK_SUBRELEASE:
3909                         case TBLOCK_SUBCOMMIT:
3910                         case TBLOCK_SUBABORT_PENDING:
3911                         case TBLOCK_SUBRESTART:
3912                                 AbortSubTransaction();
3913                                 CleanupSubTransaction();
3914                                 s = CurrentTransactionState;    /* changed by pop */
3915                                 break;
3916
3917                         case TBLOCK_SUBABORT:
3918                         case TBLOCK_SUBABORT_END:
3919                         case TBLOCK_SUBABORT_RESTART:
3920                                 /* As above, but AbortSubTransaction already done */
3921                                 CleanupSubTransaction();
3922                                 s = CurrentTransactionState;    /* changed by pop */
3923                                 break;
3924                 }
3925         } while (s->blockState != TBLOCK_DEFAULT);
3926
3927         /* Should be out of all subxacts now */
3928         Assert(s->parent == NULL);
3929 }
3930
3931 /*
3932  * IsTransactionBlock --- are we within a transaction block?
3933  */
3934 bool
3935 IsTransactionBlock(void)
3936 {
3937         TransactionState s = CurrentTransactionState;
3938
3939         if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
3940                 return false;
3941
3942         return true;
3943 }
3944
3945 /*
3946  * IsTransactionOrTransactionBlock --- are we within either a transaction
3947  * or a transaction block?      (The backend is only really "idle" when this
3948  * returns false.)
3949  *
3950  * This should match up with IsTransactionBlock and IsTransactionState.
3951  */
3952 bool
3953 IsTransactionOrTransactionBlock(void)
3954 {
3955         TransactionState s = CurrentTransactionState;
3956
3957         if (s->blockState == TBLOCK_DEFAULT)
3958                 return false;
3959
3960         return true;
3961 }
3962
3963 /*
3964  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
3965  */
3966 char
3967 TransactionBlockStatusCode(void)
3968 {
3969         TransactionState s = CurrentTransactionState;
3970
3971         switch (s->blockState)
3972         {
3973                 case TBLOCK_DEFAULT:
3974                 case TBLOCK_STARTED:
3975                         return 'I';                     /* idle --- not in transaction */
3976                 case TBLOCK_BEGIN:
3977                 case TBLOCK_SUBBEGIN:
3978                 case TBLOCK_INPROGRESS:
3979                 case TBLOCK_SUBINPROGRESS:
3980                 case TBLOCK_END:
3981                 case TBLOCK_SUBRELEASE:
3982                 case TBLOCK_SUBCOMMIT:
3983                 case TBLOCK_PREPARE:
3984                         return 'T';                     /* in transaction */
3985                 case TBLOCK_ABORT:
3986                 case TBLOCK_SUBABORT:
3987                 case TBLOCK_ABORT_END:
3988                 case TBLOCK_SUBABORT_END:
3989                 case TBLOCK_ABORT_PENDING:
3990                 case TBLOCK_SUBABORT_PENDING:
3991                 case TBLOCK_SUBRESTART:
3992                 case TBLOCK_SUBABORT_RESTART:
3993                         return 'E';                     /* in failed transaction */
3994         }
3995
3996         /* should never get here */
3997         elog(FATAL, "invalid transaction block state: %s",
3998                  BlockStateAsString(s->blockState));
3999         return 0;                                       /* keep compiler quiet */
4000 }
4001
4002 /*
4003  * IsSubTransaction
4004  */
4005 bool
4006 IsSubTransaction(void)
4007 {
4008         TransactionState s = CurrentTransactionState;
4009
4010         if (s->nestingLevel >= 2)
4011                 return true;
4012
4013         return false;
4014 }
4015
4016 /*
4017  * StartSubTransaction
4018  *
4019  * If you're wondering why this is separate from PushTransaction: it's because
4020  * we can't conveniently do this stuff right inside DefineSavepoint.  The
4021  * SAVEPOINT utility command will be executed inside a Portal, and if we
4022  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4023  * the Portal will undo those settings.  So we make DefineSavepoint just
4024  * push a dummy transaction block, and when control returns to the main
4025  * idle loop, CommitTransactionCommand will be called, and we'll come here
4026  * to finish starting the subtransaction.
4027  */
4028 static void
4029 StartSubTransaction(void)
4030 {
4031         TransactionState s = CurrentTransactionState;
4032
4033         if (s->state != TRANS_DEFAULT)
4034                 elog(WARNING, "StartSubTransaction while in %s state",
4035                          TransStateAsString(s->state));
4036
4037         s->state = TRANS_START;
4038
4039         /*
4040          * Initialize subsystems for new subtransaction
4041          *
4042          * must initialize resource-management stuff first
4043          */
4044         AtSubStart_Memory();
4045         AtSubStart_ResourceOwner();
4046         AtSubStart_Inval();
4047         AtSubStart_Notify();
4048         AfterTriggerBeginSubXact();
4049
4050         s->state = TRANS_INPROGRESS;
4051
4052         /*
4053          * Call start-of-subxact callbacks
4054          */
4055         CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
4056                                                  s->parent->subTransactionId);
4057
4058         ShowTransactionState("StartSubTransaction");
4059 }
4060
4061 /*
4062  * CommitSubTransaction
4063  *
4064  *      The caller has to make sure to always reassign CurrentTransactionState
4065  *      if it has a local pointer to it after calling this function.
4066  */
4067 static void
4068 CommitSubTransaction(void)
4069 {
4070         TransactionState s = CurrentTransactionState;
4071
4072         ShowTransactionState("CommitSubTransaction");
4073
4074         if (s->state != TRANS_INPROGRESS)
4075                 elog(WARNING, "CommitSubTransaction while in %s state",
4076                          TransStateAsString(s->state));
4077
4078         /* Pre-commit processing goes here -- nothing to do at the moment */
4079
4080         s->state = TRANS_COMMIT;
4081
4082         /* Must CCI to ensure commands of subtransaction are seen as done */
4083         CommandCounterIncrement();
4084
4085         /*
4086          * Prior to 8.4 we marked subcommit in clog at this point.      We now only
4087          * perform that step, if required, as part of the atomic update of the
4088          * whole transaction tree at top level commit or abort.
4089          */
4090
4091         /* Post-commit cleanup */
4092         if (TransactionIdIsValid(s->transactionId))
4093                 AtSubCommit_childXids();
4094         AfterTriggerEndSubXact(true);
4095         AtSubCommit_Portals(s->subTransactionId,
4096                                                 s->parent->subTransactionId,
4097                                                 s->parent->curTransactionOwner);
4098         AtEOSubXact_LargeObject(true, s->subTransactionId,
4099                                                         s->parent->subTransactionId);
4100         AtSubCommit_Notify();
4101
4102         CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
4103                                                  s->parent->subTransactionId);
4104
4105         ResourceOwnerRelease(s->curTransactionOwner,
4106                                                  RESOURCE_RELEASE_BEFORE_LOCKS,
4107                                                  true, false);
4108         AtEOSubXact_RelationCache(true, s->subTransactionId,
4109                                                           s->parent->subTransactionId);
4110         AtEOSubXact_Inval(true);
4111         AtSubCommit_smgr();
4112
4113         /*
4114          * The only lock we actually release here is the subtransaction XID lock.
4115          */
4116         CurrentResourceOwner = s->curTransactionOwner;
4117         if (TransactionIdIsValid(s->transactionId))
4118                 XactLockTableDelete(s->transactionId);
4119
4120         /*
4121          * Other locks should get transferred to their parent resource owner.
4122          */
4123         ResourceOwnerRelease(s->curTransactionOwner,
4124                                                  RESOURCE_RELEASE_LOCKS,
4125                                                  true, false);
4126         ResourceOwnerRelease(s->curTransactionOwner,
4127                                                  RESOURCE_RELEASE_AFTER_LOCKS,
4128                                                  true, false);
4129
4130         AtEOXact_GUC(true, s->gucNestLevel);
4131         AtEOSubXact_SPI(true, s->subTransactionId);
4132         AtEOSubXact_on_commit_actions(true, s->subTransactionId,
4133                                                                   s->parent->subTransactionId);
4134         AtEOSubXact_Namespace(true, s->subTransactionId,
4135                                                   s->parent->subTransactionId);
4136         AtEOSubXact_Files(true, s->subTransactionId,
4137                                           s->parent->subTransactionId);
4138         AtEOSubXact_HashTables(true, s->nestingLevel);
4139         AtEOSubXact_PgStat(true, s->nestingLevel);
4140         AtSubCommit_Snapshot(s->nestingLevel);
4141
4142         /*
4143          * We need to restore the upper transaction's read-only state, in case the
4144          * upper is read-write while the child is read-only; GUC will incorrectly
4145          * think it should leave the child state in place.
4146          */
4147         XactReadOnly = s->prevXactReadOnly;
4148
4149         CurrentResourceOwner = s->parent->curTransactionOwner;
4150         CurTransactionResourceOwner = s->parent->curTransactionOwner;
4151         ResourceOwnerDelete(s->curTransactionOwner);
4152         s->curTransactionOwner = NULL;
4153
4154         AtSubCommit_Memory();
4155
4156         s->state = TRANS_DEFAULT;
4157
4158         PopTransaction();
4159 }
4160
4161 /*
4162  * AbortSubTransaction
4163  */
4164 static void
4165 AbortSubTransaction(void)
4166 {
4167         TransactionState s = CurrentTransactionState;
4168
4169         /* Prevent cancel/die interrupt while cleaning up */
4170         HOLD_INTERRUPTS();
4171
4172         /* Make sure we have a valid memory context and resource owner */
4173         AtSubAbort_Memory();
4174         AtSubAbort_ResourceOwner();
4175
4176         /*
4177          * Release any LW locks we might be holding as quickly as possible.
4178          * (Regular locks, however, must be held till we finish aborting.)
4179          * Releasing LW locks is critical since we might try to grab them again
4180          * while cleaning up!
4181          *
4182          * FIXME This may be incorrect --- Are there some locks we should keep?
4183          * Buffer locks, for example?  I don't think so but I'm not sure.
4184          */
4185         LWLockReleaseAll();
4186
4187         AbortBufferIO();
4188         UnlockBuffers();
4189
4190         LockErrorCleanup();
4191
4192         /*
4193          * check the current transaction state
4194          */
4195         ShowTransactionState("AbortSubTransaction");
4196
4197         if (s->state != TRANS_INPROGRESS)
4198                 elog(WARNING, "AbortSubTransaction while in %s state",
4199                          TransStateAsString(s->state));
4200
4201         s->state = TRANS_ABORT;
4202
4203         /*
4204          * Reset user ID which might have been changed transiently.  (See notes in
4205          * AbortTransaction.)
4206          */
4207         SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
4208
4209         /*
4210          * We can skip all this stuff if the subxact failed before creating a
4211          * ResourceOwner...
4212          */
4213         if (s->curTransactionOwner)
4214         {
4215                 AfterTriggerEndSubXact(false);
4216                 AtSubAbort_Portals(s->subTransactionId,
4217                                                    s->parent->subTransactionId,
4218                                                    s->parent->curTransactionOwner);
4219                 AtEOSubXact_LargeObject(false, s->subTransactionId,
4220                                                                 s->parent->subTransactionId);
4221                 AtSubAbort_Notify();
4222
4223                 /* Advertise the fact that we aborted in pg_clog. */
4224                 (void) RecordTransactionAbort(true);
4225
4226                 /* Post-abort cleanup */
4227                 if (TransactionIdIsValid(s->transactionId))
4228                         AtSubAbort_childXids();
4229
4230                 CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
4231                                                          s->parent->subTransactionId);
4232
4233                 ResourceOwnerRelease(s->curTransactionOwner,
4234                                                          RESOURCE_RELEASE_BEFORE_LOCKS,
4235                                                          false, false);
4236                 AtEOSubXact_RelationCache(false, s->subTransactionId,
4237                                                                   s->parent->subTransactionId);
4238                 AtEOSubXact_Inval(false);
4239                 AtSubAbort_smgr();
4240                 ResourceOwnerRelease(s->curTransactionOwner,
4241                                                          RESOURCE_RELEASE_LOCKS,
4242                                                          false, false);
4243                 ResourceOwnerRelease(s->curTransactionOwner,
4244                                                          RESOURCE_RELEASE_AFTER_LOCKS,
4245                                                          false, false);
4246
4247                 AtEOXact_GUC(false, s->gucNestLevel);
4248                 AtEOSubXact_SPI(false, s->subTransactionId);
4249                 AtEOSubXact_on_commit_actions(false, s->subTransactionId,
4250                                                                           s->parent->subTransactionId);
4251                 AtEOSubXact_Namespace(false, s->subTransactionId,
4252                                                           s->parent->subTransactionId);
4253                 AtEOSubXact_Files(false, s->subTransactionId,
4254                                                   s->parent->subTransactionId);
4255                 AtEOSubXact_HashTables(false, s->nestingLevel);
4256                 AtEOSubXact_PgStat(false, s->nestingLevel);
4257                 AtSubAbort_Snapshot(s->nestingLevel);
4258         }
4259
4260         /*
4261          * Restore the upper transaction's read-only state, too.  This should be
4262          * redundant with GUC's cleanup but we may as well do it for consistency
4263          * with the commit case.
4264          */
4265         XactReadOnly = s->prevXactReadOnly;
4266
4267         RESUME_INTERRUPTS();
4268 }
4269
4270 /*
4271  * CleanupSubTransaction
4272  *
4273  *      The caller has to make sure to always reassign CurrentTransactionState
4274  *      if it has a local pointer to it after calling this function.
4275  */
4276 static void
4277 CleanupSubTransaction(void)
4278 {
4279         TransactionState s = CurrentTransactionState;
4280
4281         ShowTransactionState("CleanupSubTransaction");
4282
4283         if (s->state != TRANS_ABORT)
4284                 elog(WARNING, "CleanupSubTransaction while in %s state",
4285                          TransStateAsString(s->state));
4286
4287         AtSubCleanup_Portals(s->subTransactionId);
4288
4289         CurrentResourceOwner = s->parent->curTransactionOwner;
4290         CurTransactionResourceOwner = s->parent->curTransactionOwner;
4291         if (s->curTransactionOwner)
4292                 ResourceOwnerDelete(s->curTransactionOwner);
4293         s->curTransactionOwner = NULL;
4294
4295         AtSubCleanup_Memory();
4296
4297         s->state = TRANS_DEFAULT;
4298
4299         PopTransaction();
4300 }
4301
4302 /*
4303  * PushTransaction
4304  *              Create transaction state stack entry for a subtransaction
4305  *
4306  *      The caller has to make sure to always reassign CurrentTransactionState
4307  *      if it has a local pointer to it after calling this function.
4308  */
4309 static void
4310 PushTransaction(void)
4311 {
4312         TransactionState p = CurrentTransactionState;
4313         TransactionState s;
4314
4315         /*
4316          * We keep subtransaction state nodes in TopTransactionContext.
4317          */
4318         s = (TransactionState)
4319                 MemoryContextAllocZero(TopTransactionContext,
4320                                                            sizeof(TransactionStateData));
4321
4322         /*
4323          * Assign a subtransaction ID, watching out for counter wraparound.
4324          */
4325         currentSubTransactionId += 1;
4326         if (currentSubTransactionId == InvalidSubTransactionId)
4327         {
4328                 currentSubTransactionId -= 1;
4329                 pfree(s);
4330                 ereport(ERROR,
4331                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4332                                  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4333         }
4334
4335         /*
4336          * We can now stack a minimally valid subtransaction without fear of
4337          * failure.
4338          */
4339         s->transactionId = InvalidTransactionId;        /* until assigned */
4340         s->subTransactionId = currentSubTransactionId;
4341         s->parent = p;
4342         s->nestingLevel = p->nestingLevel + 1;
4343         s->gucNestLevel = NewGUCNestLevel();
4344         s->savepointLevel = p->savepointLevel;
4345         s->state = TRANS_DEFAULT;
4346         s->blockState = TBLOCK_SUBBEGIN;
4347         GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
4348         s->prevXactReadOnly = XactReadOnly;
4349
4350         CurrentTransactionState = s;
4351
4352         /*
4353          * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4354          * with the subtransaction from here on out; in particular they should not
4355          * assume that it necessarily has a transaction context, resource owner,
4356          * or XID.
4357          */
4358 }
4359
4360 /*
4361  * PopTransaction
4362  *              Pop back to parent transaction state
4363  *
4364  *      The caller has to make sure to always reassign CurrentTransactionState
4365  *      if it has a local pointer to it after calling this function.
4366  */
4367 static void
4368 PopTransaction(void)
4369 {
4370         TransactionState s = CurrentTransactionState;
4371
4372         if (s->state != TRANS_DEFAULT)
4373                 elog(WARNING, "PopTransaction while in %s state",
4374                          TransStateAsString(s->state));
4375
4376         if (s->parent == NULL)
4377                 elog(FATAL, "PopTransaction with no parent");
4378
4379         CurrentTransactionState = s->parent;
4380
4381         /* Let's just make sure CurTransactionContext is good */
4382         CurTransactionContext = s->parent->curTransactionContext;
4383         MemoryContextSwitchTo(CurTransactionContext);
4384
4385         /* Ditto for ResourceOwner links */
4386         CurTransactionResourceOwner = s->parent->curTransactionOwner;
4387         CurrentResourceOwner = s->parent->curTransactionOwner;
4388
4389         /* Free the old child structure */
4390         if (s->name)
4391                 pfree(s->name);
4392         pfree(s);
4393 }
4394
4395 /*
4396  * ShowTransactionState
4397  *              Debug support
4398  */
4399 static void
4400 ShowTransactionState(const char *str)
4401 {
4402         /* skip work if message will definitely not be printed */
4403         if (log_min_messages <= DEBUG3 || client_min_messages <= DEBUG3)
4404         {
4405                 elog(DEBUG3, "%s", str);
4406                 ShowTransactionStateRec(CurrentTransactionState);
4407         }
4408 }
4409
4410 /*
4411  * ShowTransactionStateRec
4412  *              Recursive subroutine for ShowTransactionState
4413  */
4414 static void
4415 ShowTransactionStateRec(TransactionState s)
4416 {
4417         StringInfoData buf;
4418
4419         initStringInfo(&buf);
4420
4421         if (s->nChildXids > 0)
4422         {
4423                 int                     i;
4424
4425                 appendStringInfo(&buf, "%u", s->childXids[0]);
4426                 for (i = 1; i < s->nChildXids; i++)
4427                         appendStringInfo(&buf, " %u", s->childXids[i]);
4428         }
4429
4430         if (s->parent)
4431                 ShowTransactionStateRec(s->parent);
4432
4433         /* use ereport to suppress computation if msg will not be printed */
4434         ereport(DEBUG3,
4435                         (errmsg_internal("name: %s; blockState: %13s; state: %7s, xid/subid/cid: %u/%u/%u%s, nestlvl: %d, children: %s",
4436                                                          PointerIsValid(s->name) ? s->name : "unnamed",
4437                                                          BlockStateAsString(s->blockState),
4438                                                          TransStateAsString(s->state),
4439                                                          (unsigned int) s->transactionId,
4440                                                          (unsigned int) s->subTransactionId,
4441                                                          (unsigned int) currentCommandId,
4442                                                          currentCommandIdUsed ? " (used)" : "",
4443                                                          s->nestingLevel, buf.data)));
4444
4445         pfree(buf.data);
4446 }
4447
4448 /*
4449  * BlockStateAsString
4450  *              Debug support
4451  */
4452 static const char *
4453 BlockStateAsString(TBlockState blockState)
4454 {
4455         switch (blockState)
4456         {
4457                 case TBLOCK_DEFAULT:
4458                         return "DEFAULT";
4459                 case TBLOCK_STARTED:
4460                         return "STARTED";
4461                 case TBLOCK_BEGIN:
4462                         return "BEGIN";
4463                 case TBLOCK_INPROGRESS:
4464                         return "INPROGRESS";
4465                 case TBLOCK_END:
4466                         return "END";
4467                 case TBLOCK_ABORT:
4468                         return "ABORT";
4469                 case TBLOCK_ABORT_END:
4470                         return "ABORT END";
4471                 case TBLOCK_ABORT_PENDING:
4472                         return "ABORT PEND";
4473                 case TBLOCK_PREPARE:
4474                         return "PREPARE";
4475                 case TBLOCK_SUBBEGIN:
4476                         return "SUB BEGIN";
4477                 case TBLOCK_SUBINPROGRESS:
4478                         return "SUB INPROGRS";
4479                 case TBLOCK_SUBRELEASE:
4480                         return "SUB RELEASE";
4481                 case TBLOCK_SUBCOMMIT:
4482                         return "SUB COMMIT";
4483                 case TBLOCK_SUBABORT:
4484                         return "SUB ABORT";
4485                 case TBLOCK_SUBABORT_END:
4486                         return "SUB ABORT END";
4487                 case TBLOCK_SUBABORT_PENDING:
4488                         return "SUB ABRT PEND";
4489                 case TBLOCK_SUBRESTART:
4490                         return "SUB RESTART";
4491                 case TBLOCK_SUBABORT_RESTART:
4492                         return "SUB AB RESTRT";
4493         }
4494         return "UNRECOGNIZED";
4495 }
4496
4497 /*
4498  * TransStateAsString
4499  *              Debug support
4500  */
4501 static const char *
4502 TransStateAsString(TransState state)
4503 {
4504         switch (state)
4505         {
4506                 case TRANS_DEFAULT:
4507                         return "DEFAULT";
4508                 case TRANS_START:
4509                         return "START";
4510                 case TRANS_INPROGRESS:
4511                         return "INPROGR";
4512                 case TRANS_COMMIT:
4513                         return "COMMIT";
4514                 case TRANS_ABORT:
4515                         return "ABORT";
4516                 case TRANS_PREPARE:
4517                         return "PREPARE";
4518         }
4519         return "UNRECOGNIZED";
4520 }
4521
4522 /*
4523  * xactGetCommittedChildren
4524  *
4525  * Gets the list of committed children of the current transaction.      The return
4526  * value is the number of child transactions.  *ptr is set to point to an
4527  * array of TransactionIds.  The array is allocated in TopTransactionContext;
4528  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
4529  * If there are no subxacts, *ptr is set to NULL.
4530  */
4531 int
4532 xactGetCommittedChildren(TransactionId **ptr)
4533 {
4534         TransactionState s = CurrentTransactionState;
4535
4536         if (s->nChildXids == 0)
4537                 *ptr = NULL;
4538         else
4539                 *ptr = s->childXids;
4540
4541         return s->nChildXids;
4542 }
4543
4544 /*
4545  *      XLOG support routines
4546  */
4547
4548 /*
4549  * Before 9.0 this was a fairly short function, but now it performs many
4550  * actions for which the order of execution is critical.
4551  */
4552 static void
4553 xact_redo_commit_internal(TransactionId xid, XLogRecPtr lsn,
4554                                         TransactionId *sub_xids, int nsubxacts,
4555                                         SharedInvalidationMessage *inval_msgs, int nmsgs,
4556                                         RelFileNode *xnodes, int nrels,
4557                                         Oid dbId, Oid tsId,
4558                                         uint32 xinfo)
4559 {
4560         TransactionId max_xid;
4561         int                     i;
4562
4563         max_xid = TransactionIdLatest(xid, nsubxacts, sub_xids);
4564
4565         /*
4566          * Make sure nextXid is beyond any XID mentioned in the record.
4567          *
4568          * We don't expect anyone else to modify nextXid, hence we don't need to
4569          * hold a lock while checking this. We still acquire the lock to modify
4570          * it, though.
4571          */
4572         if (TransactionIdFollowsOrEquals(max_xid,
4573                                                                          ShmemVariableCache->nextXid))
4574         {
4575                 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4576                 ShmemVariableCache->nextXid = max_xid;
4577                 TransactionIdAdvance(ShmemVariableCache->nextXid);
4578                 LWLockRelease(XidGenLock);
4579         }
4580
4581         if (standbyState == STANDBY_DISABLED)
4582         {
4583                 /*
4584                  * Mark the transaction committed in pg_clog.
4585                  */
4586                 TransactionIdCommitTree(xid, nsubxacts, sub_xids);
4587         }
4588         else
4589         {
4590                 /*
4591                  * If a transaction completion record arrives that has as-yet
4592                  * unobserved subtransactions then this will not have been fully
4593                  * handled by the call to RecordKnownAssignedTransactionIds() in the
4594                  * main recovery loop in xlog.c. So we need to do bookkeeping again to
4595                  * cover that case. This is confusing and it is easy to think this
4596                  * call is irrelevant, which has happened three times in development
4597                  * already. Leave it in.
4598                  */
4599                 RecordKnownAssignedTransactionIds(max_xid);
4600
4601                 /*
4602                  * Mark the transaction committed in pg_clog. We use async commit
4603                  * protocol during recovery to provide information on database
4604                  * consistency for when users try to set hint bits. It is important
4605                  * that we do not set hint bits until the minRecoveryPoint is past
4606                  * this commit record. This ensures that if we crash we don't see hint
4607                  * bits set on changes made by transactions that haven't yet
4608                  * recovered. It's unlikely but it's good to be safe.
4609                  */
4610                 TransactionIdAsyncCommitTree(xid, nsubxacts, sub_xids, lsn);
4611
4612                 /*
4613                  * We must mark clog before we update the ProcArray.
4614                  */
4615                 ExpireTreeKnownAssignedTransactionIds(xid, nsubxacts, sub_xids, max_xid);
4616
4617                 /*
4618                  * Send any cache invalidations attached to the commit. We must
4619                  * maintain the same order of invalidation then release locks as
4620                  * occurs in CommitTransaction().
4621                  */
4622                 ProcessCommittedInvalidationMessages(inval_msgs, nmsgs,
4623                                                                   XactCompletionRelcacheInitFileInval(xinfo),
4624                                                                                          dbId, tsId);
4625
4626                 /*
4627                  * Release locks, if any. We do this for both two phase and normal one
4628                  * phase transactions. In effect we are ignoring the prepare phase and
4629                  * just going straight to lock release.
4630                  */
4631                 StandbyReleaseLockTree(xid, nsubxacts, sub_xids);
4632         }
4633
4634         /* Make sure files supposed to be dropped are dropped */
4635         for (i = 0; i < nrels; i++)
4636         {
4637                 SMgrRelation srel = smgropen(xnodes[i], InvalidBackendId);
4638                 ForkNumber      fork;
4639
4640                 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4641                         XLogDropRelation(xnodes[i], fork);
4642                 smgrdounlink(srel, true);
4643                 smgrclose(srel);
4644         }
4645
4646         /*
4647          * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
4648          * in normal operation. For example, in DROP DATABASE, we delete all the
4649          * files belonging to the database, and then commit the transaction. If we
4650          * crash after all the files have been deleted but before the commit, you
4651          * have an entry in pg_database without any files. To minimize the window
4652          * for that, we use ForceSyncCommit() to rush the commit record to disk as
4653          * quick as possible. We have the same window during recovery, and forcing
4654          * an XLogFlush() (which updates minRecoveryPoint during recovery) helps
4655          * to reduce that problem window, for any user that requested
4656          * ForceSyncCommit().
4657          */
4658         if (XactCompletionForceSyncCommit(xinfo))
4659                 XLogFlush(lsn);
4660
4661 }
4662 /*
4663  * Utility function to call xact_redo_commit_internal after breaking down xlrec
4664  */
4665 static void
4666 xact_redo_commit(xl_xact_commit *xlrec,
4667                                                         TransactionId xid, XLogRecPtr lsn)
4668 {
4669         TransactionId *subxacts;
4670         SharedInvalidationMessage *inval_msgs;
4671
4672         /* subxid array follows relfilenodes */
4673         subxacts = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4674         /* invalidation messages array follows subxids */
4675         inval_msgs = (SharedInvalidationMessage *) &(subxacts[xlrec->nsubxacts]);
4676
4677         xact_redo_commit_internal(xid, lsn, subxacts, xlrec->nsubxacts,
4678                                                                 inval_msgs, xlrec->nmsgs,
4679                                                                 xlrec->xnodes, xlrec->nrels,
4680                                                                 xlrec->dbId,
4681                                                                 xlrec->tsId,
4682                                                                 xlrec->xinfo);
4683 }
4684
4685 /*
4686  * Utility function to call xact_redo_commit_internal  for compact form of message.
4687  */
4688 static void
4689 xact_redo_commit_compact(xl_xact_commit_compact *xlrec,
4690                                                         TransactionId xid, XLogRecPtr lsn)
4691 {
4692         xact_redo_commit_internal(xid, lsn, xlrec->subxacts, xlrec->nsubxacts,
4693                                                                 NULL, 0,                /* inval msgs */
4694                                                                 NULL, 0,                /* relfilenodes */
4695                                                                 InvalidOid,             /* dbId */
4696                                                                 InvalidOid,             /* tsId */
4697                                                                 0);                             /* xinfo */
4698 }
4699
4700 /*
4701  * Be careful with the order of execution, as with xact_redo_commit().
4702  * The two functions are similar but differ in key places.
4703  *
4704  * Note also that an abort can be for a subtransaction and its children,
4705  * not just for a top level abort. That means we have to consider
4706  * topxid != xid, whereas in commit we would find topxid == xid always
4707  * because subtransaction commit is never WAL logged.
4708  */
4709 static void
4710 xact_redo_abort(xl_xact_abort *xlrec, TransactionId xid)
4711 {
4712         TransactionId *sub_xids;
4713         TransactionId max_xid;
4714         int                     i;
4715
4716         sub_xids = (TransactionId *) &(xlrec->xnodes[xlrec->nrels]);
4717         max_xid = TransactionIdLatest(xid, xlrec->nsubxacts, sub_xids);
4718
4719         /*
4720          * Make sure nextXid is beyond any XID mentioned in the record.
4721          *
4722          * We don't expect anyone else to modify nextXid, hence we don't need to
4723          * hold a lock while checking this. We still acquire the lock to modify
4724          * it, though.
4725          */
4726         if (TransactionIdFollowsOrEquals(max_xid,
4727                                                                          ShmemVariableCache->nextXid))
4728         {
4729                 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
4730                 ShmemVariableCache->nextXid = max_xid;
4731                 TransactionIdAdvance(ShmemVariableCache->nextXid);
4732                 LWLockRelease(XidGenLock);
4733         }
4734
4735         if (standbyState == STANDBY_DISABLED)
4736         {
4737                 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4738                 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4739         }
4740         else
4741         {
4742                 /*
4743                  * If a transaction completion record arrives that has as-yet
4744                  * unobserved subtransactions then this will not have been fully
4745                  * handled by the call to RecordKnownAssignedTransactionIds() in the
4746                  * main recovery loop in xlog.c. So we need to do bookkeeping again to
4747                  * cover that case. This is confusing and it is easy to think this
4748                  * call is irrelevant, which has happened three times in development
4749                  * already. Leave it in.
4750                  */
4751                 RecordKnownAssignedTransactionIds(max_xid);
4752
4753                 /* Mark the transaction aborted in pg_clog, no need for async stuff */
4754                 TransactionIdAbortTree(xid, xlrec->nsubxacts, sub_xids);
4755
4756                 /*
4757                  * We must update the ProcArray after we have marked clog.
4758                  */
4759                 ExpireTreeKnownAssignedTransactionIds(xid, xlrec->nsubxacts, sub_xids, max_xid);
4760
4761                 /*
4762                  * There are no flat files that need updating, nor invalidation
4763                  * messages to send or undo.
4764                  */
4765
4766                 /*
4767                  * Release locks, if any. There are no invalidations to send.
4768                  */
4769                 StandbyReleaseLockTree(xid, xlrec->nsubxacts, sub_xids);
4770         }
4771
4772         /* Make sure files supposed to be dropped are dropped */
4773         for (i = 0; i < xlrec->nrels; i++)
4774         {
4775                 SMgrRelation srel = smgropen(xlrec->xnodes[i], InvalidBackendId);
4776                 ForkNumber      fork;
4777
4778                 for (fork = 0; fork <= MAX_FORKNUM; fork++)
4779                         XLogDropRelation(xlrec->xnodes[i], fork);
4780                 smgrdounlink(srel, true);
4781                 smgrclose(srel);
4782         }
4783 }
4784
4785 void
4786 xact_redo(XLogRecPtr lsn, XLogRecord *record)
4787 {
4788         uint8           info = record->xl_info & ~XLR_INFO_MASK;
4789
4790         /* Backup blocks are not used in xact records */
4791         Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
4792
4793         if (info == XLOG_XACT_COMMIT_COMPACT)
4794         {
4795                 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) XLogRecGetData(record);
4796
4797                 xact_redo_commit_compact(xlrec, record->xl_xid, lsn);
4798         }
4799         else if (info == XLOG_XACT_COMMIT)
4800         {
4801                 xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
4802
4803                 xact_redo_commit(xlrec, record->xl_xid, lsn);
4804         }
4805         else if (info == XLOG_XACT_ABORT)
4806         {
4807                 xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
4808
4809                 xact_redo_abort(xlrec, record->xl_xid);
4810         }
4811         else if (info == XLOG_XACT_PREPARE)
4812         {
4813                 /* the record contents are exactly the 2PC file */
4814                 RecreateTwoPhaseFile(record->xl_xid,
4815                                                          XLogRecGetData(record), record->xl_len);
4816         }
4817         else if (info == XLOG_XACT_COMMIT_PREPARED)
4818         {
4819                 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) XLogRecGetData(record);
4820
4821                 xact_redo_commit(&xlrec->crec, xlrec->xid, lsn);
4822                 RemoveTwoPhaseFile(xlrec->xid, false);
4823         }
4824         else if (info == XLOG_XACT_ABORT_PREPARED)
4825         {
4826                 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) XLogRecGetData(record);
4827
4828                 xact_redo_abort(&xlrec->arec, xlrec->xid);
4829                 RemoveTwoPhaseFile(xlrec->xid, false);
4830         }
4831         else if (info == XLOG_XACT_ASSIGNMENT)
4832         {
4833                 xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
4834
4835                 if (standbyState >= STANDBY_INITIALIZED)
4836                         ProcArrayApplyXidAssignment(xlrec->xtop,
4837                                                                                 xlrec->nsubxacts, xlrec->xsub);
4838         }
4839         else
4840                 elog(PANIC, "xact_redo: unknown op code %u", info);
4841 }
4842
4843 static void
4844 xact_desc_commit(StringInfo buf, xl_xact_commit *xlrec)
4845 {
4846         int                     i;
4847         TransactionId *subxacts;
4848
4849         subxacts = (TransactionId *) &xlrec->xnodes[xlrec->nrels];
4850
4851         appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4852
4853         if (xlrec->nrels > 0)
4854         {
4855                 appendStringInfo(buf, "; rels:");
4856                 for (i = 0; i < xlrec->nrels; i++)
4857                 {
4858                         char       *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4859
4860                         appendStringInfo(buf, " %s", path);
4861                         pfree(path);
4862                 }
4863         }
4864         if (xlrec->nsubxacts > 0)
4865         {
4866                 appendStringInfo(buf, "; subxacts:");
4867                 for (i = 0; i < xlrec->nsubxacts; i++)
4868                         appendStringInfo(buf, " %u", subxacts[i]);
4869         }
4870         if (xlrec->nmsgs > 0)
4871         {
4872                 SharedInvalidationMessage *msgs;
4873
4874                 msgs = (SharedInvalidationMessage *) &subxacts[xlrec->nsubxacts];
4875
4876                 if (XactCompletionRelcacheInitFileInval(xlrec->xinfo))
4877                         appendStringInfo(buf, "; relcache init file inval dbid %u tsid %u",
4878                                                          xlrec->dbId, xlrec->tsId);
4879
4880                 appendStringInfo(buf, "; inval msgs:");
4881                 for (i = 0; i < xlrec->nmsgs; i++)
4882                 {
4883                         SharedInvalidationMessage *msg = &msgs[i];
4884
4885                         if (msg->id >= 0)
4886                                 appendStringInfo(buf, " catcache %d", msg->id);
4887                         else if (msg->id == SHAREDINVALCATALOG_ID)
4888                                 appendStringInfo(buf, " catalog %u", msg->cat.catId);
4889                         else if (msg->id == SHAREDINVALRELCACHE_ID)
4890                                 appendStringInfo(buf, " relcache %u", msg->rc.relId);
4891                         /* remaining cases not expected, but print something anyway */
4892                         else if (msg->id == SHAREDINVALSMGR_ID)
4893                                 appendStringInfo(buf, " smgr");
4894                         else if (msg->id == SHAREDINVALRELMAP_ID)
4895                                 appendStringInfo(buf, " relmap");
4896                         else
4897                                 appendStringInfo(buf, " unknown id %d", msg->id);
4898                 }
4899         }
4900 }
4901
4902 static void
4903 xact_desc_commit_compact(StringInfo buf, xl_xact_commit_compact *xlrec)
4904 {
4905         int                     i;
4906
4907         appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4908
4909         if (xlrec->nsubxacts > 0)
4910         {
4911                 appendStringInfo(buf, "; subxacts:");
4912                 for (i = 0; i < xlrec->nsubxacts; i++)
4913                         appendStringInfo(buf, " %u", xlrec->subxacts[i]);
4914         }
4915 }
4916
4917 static void
4918 xact_desc_abort(StringInfo buf, xl_xact_abort *xlrec)
4919 {
4920         int                     i;
4921
4922         appendStringInfoString(buf, timestamptz_to_str(xlrec->xact_time));
4923         if (xlrec->nrels > 0)
4924         {
4925                 appendStringInfo(buf, "; rels:");
4926                 for (i = 0; i < xlrec->nrels; i++)
4927                 {
4928                         char       *path = relpathperm(xlrec->xnodes[i], MAIN_FORKNUM);
4929
4930                         appendStringInfo(buf, " %s", path);
4931                         pfree(path);
4932                 }
4933         }
4934         if (xlrec->nsubxacts > 0)
4935         {
4936                 TransactionId *xacts = (TransactionId *)
4937                 &xlrec->xnodes[xlrec->nrels];
4938
4939                 appendStringInfo(buf, "; subxacts:");
4940                 for (i = 0; i < xlrec->nsubxacts; i++)
4941                         appendStringInfo(buf, " %u", xacts[i]);
4942         }
4943 }
4944
4945 static void
4946 xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
4947 {
4948         int                     i;
4949
4950         appendStringInfo(buf, "subxacts:");
4951
4952         for (i = 0; i < xlrec->nsubxacts; i++)
4953                 appendStringInfo(buf, " %u", xlrec->xsub[i]);
4954 }
4955
4956 void
4957 xact_desc(StringInfo buf, uint8 xl_info, char *rec)
4958 {
4959         uint8           info = xl_info & ~XLR_INFO_MASK;
4960
4961         if (info == XLOG_XACT_COMMIT_COMPACT)
4962         {
4963                 xl_xact_commit_compact *xlrec = (xl_xact_commit_compact *) rec;
4964
4965                 appendStringInfo(buf, "commit: ");
4966                 xact_desc_commit_compact(buf, xlrec);
4967         }
4968         else if (info == XLOG_XACT_COMMIT)
4969         {
4970                 xl_xact_commit *xlrec = (xl_xact_commit *) rec;
4971
4972                 appendStringInfo(buf, "commit: ");
4973                 xact_desc_commit(buf, xlrec);
4974         }
4975         else if (info == XLOG_XACT_ABORT)
4976         {
4977                 xl_xact_abort *xlrec = (xl_xact_abort *) rec;
4978
4979                 appendStringInfo(buf, "abort: ");
4980                 xact_desc_abort(buf, xlrec);
4981         }
4982         else if (info == XLOG_XACT_PREPARE)
4983         {
4984                 appendStringInfo(buf, "prepare");
4985         }
4986         else if (info == XLOG_XACT_COMMIT_PREPARED)
4987         {
4988                 xl_xact_commit_prepared *xlrec = (xl_xact_commit_prepared *) rec;
4989
4990                 appendStringInfo(buf, "commit prepared %u: ", xlrec->xid);
4991                 xact_desc_commit(buf, &xlrec->crec);
4992         }
4993         else if (info == XLOG_XACT_ABORT_PREPARED)
4994         {
4995                 xl_xact_abort_prepared *xlrec = (xl_xact_abort_prepared *) rec;
4996
4997                 appendStringInfo(buf, "abort prepared %u: ", xlrec->xid);
4998                 xact_desc_abort(buf, &xlrec->arec);
4999         }
5000         else if (info == XLOG_XACT_ASSIGNMENT)
5001         {
5002                 xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
5003
5004                 /*
5005                  * Note that we ignore the WAL record's xid, since we're more
5006                  * interested in the top-level xid that issued the record and which
5007                  * xids are being reported here.
5008                  */
5009                 appendStringInfo(buf, "xid assignment xtop %u: ", xlrec->xtop);
5010                 xact_desc_assignment(buf, xlrec);
5011         }
5012         else
5013                 appendStringInfo(buf, "UNKNOWN");
5014 }