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