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