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