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