PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xact.c
Go to the documentation of this file.
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-2017, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  * src/backend/access/transam/xact.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include <time.h>
21 #include <unistd.h>
22 
23 #include "access/commit_ts.h"
24 #include "access/multixact.h"
25 #include "access/parallel.h"
26 #include "access/subtrans.h"
27 #include "access/transam.h"
28 #include "access/twophase.h"
29 #include "access/xact.h"
30 #include "access/xlog.h"
31 #include "access/xloginsert.h"
32 #include "access/xlogutils.h"
33 #include "catalog/catalog.h"
34 #include "catalog/namespace.h"
35 #include "catalog/storage.h"
36 #include "commands/async.h"
37 #include "commands/tablecmds.h"
38 #include "commands/trigger.h"
39 #include "executor/spi.h"
40 #include "libpq/be-fsstubs.h"
41 #include "libpq/pqsignal.h"
42 #include "miscadmin.h"
43 #include "pgstat.h"
44 #include "replication/logical.h"
46 #include "replication/origin.h"
47 #include "replication/syncrep.h"
48 #include "replication/walsender.h"
50 #include "storage/fd.h"
51 #include "storage/lmgr.h"
52 #include "storage/predicate.h"
53 #include "storage/proc.h"
54 #include "storage/procarray.h"
55 #include "storage/sinvaladt.h"
56 #include "storage/smgr.h"
57 #include "utils/builtins.h"
58 #include "utils/catcache.h"
59 #include "utils/combocid.h"
60 #include "utils/guc.h"
61 #include "utils/inval.h"
62 #include "utils/memutils.h"
63 #include "utils/relmapper.h"
64 #include "utils/snapmgr.h"
65 #include "utils/timeout.h"
66 #include "utils/timestamp.h"
67 #include "pg_trace.h"
68 
69 
70 /*
71  * User-tweakable parameters
72  */
75 
76 bool DefaultXactReadOnly = false;
78 
79 bool DefaultXactDeferrable = false;
81 
83 
84 /*
85  * When running as a parallel worker, we place only a single
86  * TransactionStateData on the parallel worker's state stack, and the XID
87  * reflected there will be that of the *innermost* currently-active
88  * subtransaction in the backend that initiated parallelism. However,
89  * GetTopTransactionId() and TransactionIdIsCurrentTransactionId()
90  * need to return the same answers in the parallel worker as they would have
91  * in the user backend, so we need some additional bookkeeping.
92  *
93  * XactTopTransactionId stores the XID of our toplevel transaction, which
94  * will be the same as TopTransactionState.transactionId in an ordinary
95  * backend; but in a parallel backend, which does not have the entire
96  * transaction state, it will instead be copied from the backend that started
97  * the parallel operation.
98  *
99  * nParallelCurrentXids will be 0 and ParallelCurrentXids NULL in an ordinary
100  * backend, but in a parallel backend, nParallelCurrentXids will contain the
101  * number of XIDs that need to be considered current, and ParallelCurrentXids
102  * will contain the XIDs themselves. This includes all XIDs that were current
103  * or sub-committed in the parent at the time the parallel operation began.
104  * The XIDs are stored sorted in numerical order (not logical order) to make
105  * lookups as fast as possible.
106  */
110 
111 /*
112  * Miscellaneous flag bits to record events which occur on the top level
113  * transaction. These flags are only persisted in MyXactFlags and are intended
114  * so we remember to do certain things later on in the transaction. This is
115  * globally accessible, so can be set from anywhere in the code that requires
116  * recording flags.
117  */
119 
120 /*
121  * transaction states - transaction state from server perspective
122  */
123 typedef enum TransState
124 {
125  TRANS_DEFAULT, /* idle */
126  TRANS_START, /* transaction starting */
127  TRANS_INPROGRESS, /* inside a valid transaction */
128  TRANS_COMMIT, /* commit in progress */
129  TRANS_ABORT, /* abort in progress */
130  TRANS_PREPARE /* prepare in progress */
131 } TransState;
132 
133 /*
134  * transaction block states - transaction state of client queries
135  *
136  * Note: the subtransaction states are used only for non-topmost
137  * transactions; the others appear only in the topmost transaction.
138  */
139 typedef enum TBlockState
140 {
141  /* not-in-transaction-block states */
142  TBLOCK_DEFAULT, /* idle */
143  TBLOCK_STARTED, /* running single-query transaction */
144 
145  /* transaction block states */
146  TBLOCK_BEGIN, /* starting transaction block */
147  TBLOCK_INPROGRESS, /* live transaction */
148  TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
149  TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
150  TBLOCK_END, /* COMMIT received */
151  TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
152  TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
153  TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
154  TBLOCK_PREPARE, /* live xact, PREPARE received */
155 
156  /* subtransaction states */
157  TBLOCK_SUBBEGIN, /* starting a subtransaction */
158  TBLOCK_SUBINPROGRESS, /* live subtransaction */
159  TBLOCK_SUBRELEASE, /* RELEASE received */
160  TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
161  TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
162  TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
163  TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
164  TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
165  TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
166 } TBlockState;
167 
168 /*
169  * transaction state structure
170  */
171 typedef struct TransactionStateData
172 {
173  TransactionId transactionId; /* my XID, or Invalid if none */
174  SubTransactionId subTransactionId; /* my subxact ID */
175  char *name; /* savepoint name, if any */
176  int savepointLevel; /* savepoint level */
177  TransState state; /* low-level state */
178  TBlockState blockState; /* high-level state */
179  int nestingLevel; /* transaction nesting depth */
180  int gucNestLevel; /* GUC context nesting depth */
181  MemoryContext curTransactionContext; /* my xact-lifetime context */
182  ResourceOwner curTransactionOwner; /* my query resources */
183  TransactionId *childXids; /* subcommitted child XIDs, in XID order */
184  int nChildXids; /* # of subcommitted child XIDs */
185  int maxChildXids; /* allocated size of childXids[] */
186  Oid prevUser; /* previous CurrentUserId setting */
187  int prevSecContext; /* previous SecurityRestrictionContext */
188  bool prevXactReadOnly; /* entry-time xact r/o state */
189  bool startedInRecovery; /* did we start in recovery? */
190  bool didLogXid; /* has xid been included in WAL record? */
191  int parallelModeLevel; /* Enter/ExitParallelMode counter */
192  struct TransactionStateData *parent; /* back link to parent */
194 
196 
197 /*
198  * CurrentTransactionState always points to the current transaction state
199  * block. It will point to TopTransactionStateData when not in a
200  * transaction at all, or when in a top-level transaction.
201  */
203  0, /* transaction id */
204  0, /* subtransaction id */
205  NULL, /* savepoint name */
206  0, /* savepoint level */
207  TRANS_DEFAULT, /* transaction state */
208  TBLOCK_DEFAULT, /* transaction block state from the client
209  * perspective */
210  0, /* transaction nesting depth */
211  0, /* GUC context nesting depth */
212  NULL, /* cur transaction context */
213  NULL, /* cur transaction resource owner */
214  NULL, /* subcommitted child Xids */
215  0, /* # of subcommitted child Xids */
216  0, /* allocated size of childXids[] */
217  InvalidOid, /* previous CurrentUserId setting */
218  0, /* previous SecurityRestrictionContext */
219  false, /* entry-time xact r/o state */
220  false, /* startedInRecovery */
221  false, /* didLogXid */
222  0, /* parallelMode */
223  NULL /* link to parent state block */
224 };
225 
226 /*
227  * unreportedXids holds XIDs of all subtransactions that have not yet been
228  * reported in an XLOG_XACT_ASSIGNMENT record.
229  */
230 static int nUnreportedXids;
232 
234 
235 /*
236  * The subtransaction ID and command ID assignment counters are global
237  * to a whole transaction, so we do not keep them in the state stack.
238  */
242 
243 /*
244  * xactStartTimestamp is the value of transaction_timestamp().
245  * stmtStartTimestamp is the value of statement_timestamp().
246  * xactStopTimestamp is the time at which we log a commit or abort WAL record.
247  * These do not change as we enter and exit subtransactions, so we don't
248  * keep them inside the TransactionState stack.
249  */
253 
254 /*
255  * GID to be used for preparing the current transaction. This is also
256  * global to a whole transaction, so we don't keep it in the state stack.
257  */
258 static char *prepareGID;
259 
260 /*
261  * Some commands want to force synchronous commit.
262  */
263 static bool forceSyncCommit = false;
264 
265 /*
266  * Private context for transaction-abort work --- we reserve space for this
267  * at startup to ensure that AbortTransaction and AbortSubTransaction can work
268  * when we've run out of memory.
269  */
271 
272 /*
273  * List of add-on start- and end-of-xact callbacks
274  */
275 typedef struct XactCallbackItem
276 {
279  void *arg;
281 
283 
284 /*
285  * List of add-on start- and end-of-subxact callbacks
286  */
287 typedef struct SubXactCallbackItem
288 {
291  void *arg;
293 
295 
296 
297 /* local function prototypes */
299 static void AbortTransaction(void);
300 static void AtAbort_Memory(void);
301 static void AtCleanup_Memory(void);
302 static void AtAbort_ResourceOwner(void);
303 static void AtCCI_LocalCache(void);
304 static void AtCommit_Memory(void);
305 static void AtStart_Cache(void);
306 static void AtStart_Memory(void);
307 static void AtStart_ResourceOwner(void);
308 static void CallXactCallbacks(XactEvent event);
309 static void CallSubXactCallbacks(SubXactEvent event,
310  SubTransactionId mySubid,
311  SubTransactionId parentSubid);
312 static void CleanupTransaction(void);
313 static void CheckTransactionChain(bool isTopLevel, bool throwError,
314  const char *stmtType);
315 static void CommitTransaction(void);
316 static TransactionId RecordTransactionAbort(bool isSubXact);
317 static void StartTransaction(void);
318 
319 static void StartSubTransaction(void);
320 static void CommitSubTransaction(void);
321 static void AbortSubTransaction(void);
322 static void CleanupSubTransaction(void);
323 static void PushTransaction(void);
324 static void PopTransaction(void);
325 
326 static void AtSubAbort_Memory(void);
327 static void AtSubCleanup_Memory(void);
328 static void AtSubAbort_ResourceOwner(void);
329 static void AtSubCommit_Memory(void);
330 static void AtSubStart_Memory(void);
331 static void AtSubStart_ResourceOwner(void);
332 
333 static void ShowTransactionState(const char *str);
334 static void ShowTransactionStateRec(const char *str, TransactionState state);
335 static const char *BlockStateAsString(TBlockState blockState);
336 static const char *TransStateAsString(TransState state);
337 
338 
339 /* ----------------------------------------------------------------
340  * transaction state accessors
341  * ----------------------------------------------------------------
342  */
343 
344 /*
345  * IsTransactionState
346  *
347  * This returns true if we are inside a valid transaction; that is,
348  * it is safe to initiate database access, take heavyweight locks, etc.
349  */
350 bool
352 {
354 
355  /*
356  * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
357  * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
358  * TRANS_PREPARE since it might be too soon or too late within those
359  * transition states to do anything interesting. Hence, the only "valid"
360  * state is TRANS_INPROGRESS.
361  */
362  return (s->state == TRANS_INPROGRESS);
363 }
364 
365 /*
366  * IsAbortedTransactionBlockState
367  *
368  * This returns true if we are within an aborted transaction block.
369  */
370 bool
372 {
374 
375  if (s->blockState == TBLOCK_ABORT ||
377  return true;
378 
379  return false;
380 }
381 
382 
383 /*
384  * GetTopTransactionId
385  *
386  * This will return the XID of the main transaction, assigning one if
387  * it's not yet set. Be careful to call this only inside a valid xact.
388  */
391 {
393  AssignTransactionId(&TopTransactionStateData);
394  return XactTopTransactionId;
395 }
396 
397 /*
398  * GetTopTransactionIdIfAny
399  *
400  * This will return the XID of the main transaction, if one is assigned.
401  * It will return InvalidTransactionId if we are not currently inside a
402  * transaction, or inside a transaction that hasn't yet been assigned an XID.
403  */
406 {
407  return XactTopTransactionId;
408 }
409 
410 /*
411  * GetCurrentTransactionId
412  *
413  * This will return the XID of the current transaction (main or sub
414  * transaction), assigning one if it's not yet set. Be careful to call this
415  * only inside a valid xact.
416  */
419 {
421 
424  return s->transactionId;
425 }
426 
427 /*
428  * GetCurrentTransactionIdIfAny
429  *
430  * This will return the XID of the current sub xact, if one is assigned.
431  * It will return InvalidTransactionId if we are not currently inside a
432  * transaction, or inside a transaction that hasn't been assigned an XID yet.
433  */
436 {
437  return CurrentTransactionState->transactionId;
438 }
439 
440 /*
441  * MarkCurrentTransactionIdLoggedIfAny
442  *
443  * Remember that the current xid - if it is assigned - now has been wal logged.
444  */
445 void
447 {
448  if (TransactionIdIsValid(CurrentTransactionState->transactionId))
449  CurrentTransactionState->didLogXid = true;
450 }
451 
452 
453 /*
454  * GetStableLatestTransactionId
455  *
456  * Get the transaction's XID if it has one, else read the next-to-be-assigned
457  * XID. Once we have a value, return that same value for the remainder of the
458  * current transaction. This is meant to provide the reference point for the
459  * age(xid) function, but might be useful for other maintenance tasks as well.
460  */
463 {
465  static TransactionId stablexid = InvalidTransactionId;
466 
467  if (lxid != MyProc->lxid)
468  {
469  lxid = MyProc->lxid;
470  stablexid = GetTopTransactionIdIfAny();
471  if (!TransactionIdIsValid(stablexid))
472  stablexid = ReadNewTransactionId();
473  }
474 
475  Assert(TransactionIdIsValid(stablexid));
476 
477  return stablexid;
478 }
479 
480 /*
481  * AssignTransactionId
482  *
483  * Assigns a new permanent XID to the given TransactionState.
484  * We do not assign XIDs to transactions until/unless this is called.
485  * Also, any parent TransactionStates that don't yet have XIDs are assigned
486  * one; this maintains the invariant that a child transaction has an XID
487  * following its parent's.
488  */
489 static void
491 {
492  bool isSubXact = (s->parent != NULL);
493  ResourceOwner currentOwner;
494  bool log_unknown_top = false;
495 
496  /* Assert that caller didn't screw up */
499 
500  /*
501  * Workers synchronize transaction state at the beginning of each parallel
502  * operation, so we can't account for new XIDs at this point.
503  */
505  elog(ERROR, "cannot assign XIDs during a parallel operation");
506 
507  /*
508  * Ensure parent(s) have XIDs, so that a child always has an XID later
509  * than its parent. Musn't recurse here, or we might get a stack overflow
510  * if we're at the bottom of a huge stack of subtransactions none of which
511  * have XIDs yet.
512  */
513  if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
514  {
515  TransactionState p = s->parent;
516  TransactionState *parents;
517  size_t parentOffset = 0;
518 
519  parents = palloc(sizeof(TransactionState) * s->nestingLevel);
520  while (p != NULL && !TransactionIdIsValid(p->transactionId))
521  {
522  parents[parentOffset++] = p;
523  p = p->parent;
524  }
525 
526  /*
527  * This is technically a recursive call, but the recursion will never
528  * be more than one layer deep.
529  */
530  while (parentOffset != 0)
531  AssignTransactionId(parents[--parentOffset]);
532 
533  pfree(parents);
534  }
535 
536  /*
537  * When wal_level=logical, guarantee that a subtransaction's xid can only
538  * be seen in the WAL stream if its toplevel xid has been logged before.
539  * If necessary we log an xact_assignment record with fewer than
540  * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
541  * for a transaction even though it appears in a WAL record, we just might
542  * superfluously log something. That can happen when an xid is included
543  * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
544  * xl_standby_locks.
545  */
546  if (isSubXact && XLogLogicalInfoActive() &&
547  !TopTransactionStateData.didLogXid)
548  log_unknown_top = true;
549 
550  /*
551  * Generate a new Xid and record it in PG_PROC and pg_subtrans.
552  *
553  * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
554  * shared storage other than PG_PROC; because if there's no room for it in
555  * PG_PROC, the subtrans entry is needed to ensure that other backends see
556  * the Xid as "running". See GetNewTransactionId.
557  */
558  s->transactionId = GetNewTransactionId(isSubXact);
559  if (!isSubXact)
561 
562  if (isSubXact)
564 
565  /*
566  * If it's a top-level transaction, the predicate locking system needs to
567  * be told about it too.
568  */
569  if (!isSubXact)
571 
572  /*
573  * Acquire lock on the transaction XID. (We assume this cannot block.) We
574  * have to ensure that the lock is assigned to the transaction's own
575  * ResourceOwner.
576  */
577  currentOwner = CurrentResourceOwner;
578  PG_TRY();
579  {
582  }
583  PG_CATCH();
584  {
585  /* Ensure CurrentResourceOwner is restored on error */
586  CurrentResourceOwner = currentOwner;
587  PG_RE_THROW();
588  }
589  PG_END_TRY();
590  CurrentResourceOwner = currentOwner;
591 
592  /*
593  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
594  * top-level transaction we issue a WAL record for the assignment. We
595  * include the top-level xid and all the subxids that have not yet been
596  * reported using XLOG_XACT_ASSIGNMENT records.
597  *
598  * This is required to limit the amount of shared memory required in a hot
599  * standby server to keep track of in-progress XIDs. See notes for
600  * RecordKnownAssignedTransactionIds().
601  *
602  * We don't keep track of the immediate parent of each subxid, only the
603  * top-level transaction that each subxact belongs to. This is correct in
604  * recovery only because aborted subtransactions are separately WAL
605  * logged.
606  *
607  * This is correct even for the case where several levels above us didn't
608  * have an xid assigned as we recursed up to them beforehand.
609  */
610  if (isSubXact && XLogStandbyInfoActive())
611  {
613  nUnreportedXids++;
614 
615  /*
616  * ensure this test matches similar one in
617  * RecoverPreparedTransactions()
618  */
620  log_unknown_top)
621  {
622  xl_xact_assignment xlrec;
623 
624  /*
625  * xtop is always set by now because we recurse up transaction
626  * stack to the highest unassigned xid and then come back down
627  */
628  xlrec.xtop = GetTopTransactionId();
630  xlrec.nsubxacts = nUnreportedXids;
631 
632  XLogBeginInsert();
633  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
635  nUnreportedXids * sizeof(TransactionId));
636 
637  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
638 
639  nUnreportedXids = 0;
640  /* mark top, not current xact as having been logged */
641  TopTransactionStateData.didLogXid = true;
642  }
643  }
644 }
645 
646 /*
647  * GetCurrentSubTransactionId
648  */
651 {
653 
654  return s->subTransactionId;
655 }
656 
657 /*
658  * SubTransactionIsActive
659  *
660  * Test if the specified subxact ID is still active. Note caller is
661  * responsible for checking whether this ID is relevant to the current xact.
662  */
663 bool
665 {
667 
668  for (s = CurrentTransactionState; s != NULL; s = s->parent)
669  {
670  if (s->state == TRANS_ABORT)
671  continue;
672  if (s->subTransactionId == subxid)
673  return true;
674  }
675  return false;
676 }
677 
678 
679 /*
680  * GetCurrentCommandId
681  *
682  * "used" must be TRUE if the caller intends to use the command ID to mark
683  * inserted/updated/deleted tuples. FALSE means the ID is being fetched
684  * for read-only purposes (ie, as a snapshot validity cutoff). See
685  * CommandCounterIncrement() for discussion.
686  */
687 CommandId
689 {
690  /* this is global to a transaction, not subtransaction-local */
691  if (used)
692  {
693  /*
694  * Forbid setting currentCommandIdUsed in parallel mode, because we
695  * have no provision for communicating this back to the master. We
696  * could relax this restriction when currentCommandIdUsed was already
697  * true at the start of the parallel operation.
698  */
699  Assert(CurrentTransactionState->parallelModeLevel == 0);
700  currentCommandIdUsed = true;
701  }
702  return currentCommandId;
703 }
704 
705 /*
706  * GetCurrentTransactionStartTimestamp
707  */
710 {
711  return xactStartTimestamp;
712 }
713 
714 /*
715  * GetCurrentStatementStartTimestamp
716  */
719 {
720  return stmtStartTimestamp;
721 }
722 
723 /*
724  * GetCurrentTransactionStopTimestamp
725  *
726  * We return current time if the transaction stop time hasn't been set
727  * (which can happen if we decide we don't need to log an XLOG record).
728  */
731 {
732  if (xactStopTimestamp != 0)
733  return xactStopTimestamp;
734  return GetCurrentTimestamp();
735 }
736 
737 /*
738  * SetCurrentStatementStartTimestamp
739  */
740 void
742 {
744 }
745 
746 /*
747  * SetCurrentTransactionStopTimestamp
748  */
749 static inline void
751 {
753 }
754 
755 /*
756  * GetCurrentTransactionNestLevel
757  *
758  * Note: this will return zero when not inside any transaction, one when
759  * inside a top-level transaction, etc.
760  */
761 int
763 {
765 
766  return s->nestingLevel;
767 }
768 
769 
770 /*
771  * TransactionIdIsCurrentTransactionId
772  */
773 bool
775 {
777 
778  /*
779  * We always say that BootstrapTransactionId is "not my transaction ID"
780  * even when it is (ie, during bootstrap). Along with the fact that
781  * transam.c always treats BootstrapTransactionId as already committed,
782  * this causes the tqual.c routines to see all tuples as committed, which
783  * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
784  * it never updates or deletes them, so all tuples can be presumed good
785  * immediately.)
786  *
787  * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
788  * not my transaction ID, so we can just return "false" immediately for
789  * any non-normal XID.
790  */
791  if (!TransactionIdIsNormal(xid))
792  return false;
793 
794  /*
795  * In parallel workers, the XIDs we must consider as current are stored in
796  * ParallelCurrentXids rather than the transaction-state stack. Note that
797  * the XIDs in this array are sorted numerically rather than according to
798  * transactionIdPrecedes order.
799  */
800  if (nParallelCurrentXids > 0)
801  {
802  int low,
803  high;
804 
805  low = 0;
806  high = nParallelCurrentXids - 1;
807  while (low <= high)
808  {
809  int middle;
810  TransactionId probe;
811 
812  middle = low + (high - low) / 2;
813  probe = ParallelCurrentXids[middle];
814  if (probe == xid)
815  return true;
816  else if (probe < xid)
817  low = middle + 1;
818  else
819  high = middle - 1;
820  }
821  return false;
822  }
823 
824  /*
825  * We will return true for the Xid of the current subtransaction, any of
826  * its subcommitted children, any of its parents, or any of their
827  * previously subcommitted children. However, a transaction being aborted
828  * is no longer "current", even though it may still have an entry on the
829  * state stack.
830  */
831  for (s = CurrentTransactionState; s != NULL; s = s->parent)
832  {
833  int low,
834  high;
835 
836  if (s->state == TRANS_ABORT)
837  continue;
839  continue; /* it can't have any child XIDs either */
840  if (TransactionIdEquals(xid, s->transactionId))
841  return true;
842  /* As the childXids array is ordered, we can use binary search */
843  low = 0;
844  high = s->nChildXids - 1;
845  while (low <= high)
846  {
847  int middle;
848  TransactionId probe;
849 
850  middle = low + (high - low) / 2;
851  probe = s->childXids[middle];
852  if (TransactionIdEquals(probe, xid))
853  return true;
854  else if (TransactionIdPrecedes(probe, xid))
855  low = middle + 1;
856  else
857  high = middle - 1;
858  }
859  }
860 
861  return false;
862 }
863 
864 /*
865  * TransactionStartedDuringRecovery
866  *
867  * Returns true if the current transaction started while recovery was still
868  * in progress. Recovery might have ended since so RecoveryInProgress() might
869  * return false already.
870  */
871 bool
873 {
874  return CurrentTransactionState->startedInRecovery;
875 }
876 
877 /*
878  * EnterParallelMode
879  */
880 void
882 {
884 
885  Assert(s->parallelModeLevel >= 0);
886 
887  ++s->parallelModeLevel;
888 }
889 
890 /*
891  * ExitParallelMode
892  */
893 void
895 {
897 
898  Assert(s->parallelModeLevel > 0);
900 
901  --s->parallelModeLevel;
902 }
903 
904 /*
905  * IsInParallelMode
906  *
907  * Are we in a parallel operation, as either the master or a worker? Check
908  * this to prohibit operations that change backend-local state expected to
909  * match across all workers. Mere caches usually don't require such a
910  * restriction. State modified in a strict push/pop fashion, such as the
911  * active snapshot stack, is often fine.
912  */
913 bool
915 {
916  return CurrentTransactionState->parallelModeLevel != 0;
917 }
918 
919 /*
920  * CommandCounterIncrement
921  */
922 void
924 {
925  /*
926  * If the current value of the command counter hasn't been "used" to mark
927  * tuples, we need not increment it, since there's no need to distinguish
928  * a read-only command from others. This helps postpone command counter
929  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
930  */
932  {
933  /*
934  * Workers synchronize transaction state at the beginning of each
935  * parallel operation, so we can't account for new commands after that
936  * point.
937  */
939  elog(ERROR, "cannot start commands during a parallel operation");
940 
941  currentCommandId += 1;
943  {
944  currentCommandId -= 1;
945  ereport(ERROR,
946  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
947  errmsg("cannot have more than 2^32-2 commands in a transaction")));
948  }
949  currentCommandIdUsed = false;
950 
951  /* Propagate new command ID into static snapshots */
953 
954  /*
955  * Make any catalog changes done by the just-completed command visible
956  * in the local syscache. We obviously don't need to do this after a
957  * read-only command. (But see hacks in inval.c to make real sure we
958  * don't think a command that queued inval messages was read-only.)
959  */
961  }
962 }
963 
964 /*
965  * ForceSyncCommit
966  *
967  * Interface routine to allow commands to force a synchronous commit of the
968  * current top-level transaction
969  */
970 void
972 {
973  forceSyncCommit = true;
974 }
975 
976 
977 /* ----------------------------------------------------------------
978  * StartTransaction stuff
979  * ----------------------------------------------------------------
980  */
981 
982 /*
983  * AtStart_Cache
984  */
985 static void
987 {
989 }
990 
991 /*
992  * AtStart_Memory
993  */
994 static void
996 {
998 
999  /*
1000  * If this is the first time through, create a private context for
1001  * AbortTransaction to work in. By reserving some space now, we can
1002  * insulate AbortTransaction from out-of-memory scenarios. Like
1003  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1004  * size, so that space will be reserved immediately.
1005  */
1006  if (TransactionAbortContext == NULL)
1007  TransactionAbortContext =
1009  "TransactionAbortContext",
1010  32 * 1024,
1011  32 * 1024,
1012  32 * 1024);
1013 
1014  /*
1015  * We shouldn't have a transaction context already.
1016  */
1017  Assert(TopTransactionContext == NULL);
1018 
1019  /*
1020  * Create a toplevel context for the transaction.
1021  */
1024  "TopTransactionContext",
1026 
1027  /*
1028  * In a top-level transaction, CurTransactionContext is the same as
1029  * TopTransactionContext.
1030  */
1033 
1034  /* Make the CurTransactionContext active. */
1036 }
1037 
1038 /*
1039  * AtStart_ResourceOwner
1040  */
1041 static void
1043 {
1045 
1046  /*
1047  * We shouldn't have a transaction resource owner already.
1048  */
1050 
1051  /*
1052  * Create a toplevel resource owner for the transaction.
1053  */
1054  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1055 
1059 }
1060 
1061 /* ----------------------------------------------------------------
1062  * StartSubTransaction stuff
1063  * ----------------------------------------------------------------
1064  */
1065 
1066 /*
1067  * AtSubStart_Memory
1068  */
1069 static void
1071 {
1073 
1074  Assert(CurTransactionContext != NULL);
1075 
1076  /*
1077  * Create a CurTransactionContext, which will be used to hold data that
1078  * survives subtransaction commit but disappears on subtransaction abort.
1079  * We make it a child of the immediate parent's CurTransactionContext.
1080  */
1082  "CurTransactionContext",
1085 
1086  /* Make the CurTransactionContext active. */
1088 }
1089 
1090 /*
1091  * AtSubStart_ResourceOwner
1092  */
1093 static void
1095 {
1097 
1098  Assert(s->parent != NULL);
1099 
1100  /*
1101  * Create a resource owner for the subtransaction. We make it a child of
1102  * the immediate parent's resource owner.
1103  */
1104  s->curTransactionOwner =
1106  "SubTransaction");
1107 
1110 }
1111 
1112 /* ----------------------------------------------------------------
1113  * CommitTransaction stuff
1114  * ----------------------------------------------------------------
1115  */
1116 
1117 /*
1118  * RecordTransactionCommit
1119  *
1120  * Returns latest XID among xact and its children, or InvalidTransactionId
1121  * if the xact has no XID. (We compute that here just because it's easier.)
1122  *
1123  * If you change this function, see RecordTransactionCommitPrepared also.
1124  */
1125 static TransactionId
1127 {
1129  bool markXidCommitted = TransactionIdIsValid(xid);
1130  TransactionId latestXid = InvalidTransactionId;
1131  int nrels;
1132  RelFileNode *rels;
1133  int nchildren;
1134  TransactionId *children;
1135  int nmsgs = 0;
1136  SharedInvalidationMessage *invalMessages = NULL;
1137  bool RelcacheInitFileInval = false;
1138  bool wrote_xlog;
1139 
1140  /* Get data needed for commit record */
1141  nrels = smgrGetPendingDeletes(true, &rels);
1142  nchildren = xactGetCommittedChildren(&children);
1143  if (XLogStandbyInfoActive())
1144  nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1145  &RelcacheInitFileInval);
1146  wrote_xlog = (XactLastRecEnd != 0);
1147 
1148  /*
1149  * If we haven't been assigned an XID yet, we neither can, nor do we want
1150  * to write a COMMIT record.
1151  */
1152  if (!markXidCommitted)
1153  {
1154  /*
1155  * We expect that every smgrscheduleunlink is followed by a catalog
1156  * update, and hence XID assignment, so we shouldn't get here with any
1157  * pending deletes. Use a real test not just an Assert to check this,
1158  * since it's a bit fragile.
1159  */
1160  if (nrels != 0)
1161  elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1162 
1163  /* Can't have child XIDs either; AssignTransactionId enforces this */
1164  Assert(nchildren == 0);
1165 
1166  /*
1167  * Transactions without an assigned xid can contain invalidation
1168  * messages (e.g. explicit relcache invalidations or catcache
1169  * invalidations for inplace updates); standbys need to process those.
1170  * We can't emit a commit record without an xid, and we don't want to
1171  * force assigning an xid, because that'd be problematic for e.g.
1172  * vacuum. Hence we emit a bespoke record for the invalidations. We
1173  * don't want to use that in case a commit record is emitted, so they
1174  * happen synchronously with commits (besides not wanting to emit more
1175  * WAL recoreds).
1176  */
1177  if (nmsgs != 0)
1178  {
1179  LogStandbyInvalidations(nmsgs, invalMessages,
1180  RelcacheInitFileInval);
1181  wrote_xlog = true; /* not strictly necessary */
1182  }
1183 
1184  /*
1185  * If we didn't create XLOG entries, we're done here; otherwise we
1186  * should trigger flushing those entries the same as a commit record
1187  * would. This will primarily happen for HOT pruning and the like; we
1188  * want these to be flushed to disk in due time.
1189  */
1190  if (!wrote_xlog)
1191  goto cleanup;
1192  }
1193  else
1194  {
1195  bool replorigin;
1196 
1197  /*
1198  * Are we using the replication origins feature? Or, in other words,
1199  * are we replaying remote actions?
1200  */
1201  replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1203 
1204  /*
1205  * Begin commit critical section and insert the commit XLOG record.
1206  */
1207  /* Tell bufmgr and smgr to prepare for commit */
1208  BufmgrCommit();
1209 
1210  /*
1211  * Mark ourselves as within our "commit critical section". This
1212  * forces any concurrent checkpoint to wait until we've updated
1213  * pg_xact. Without this, it is possible for the checkpoint to set
1214  * REDO after the XLOG record but fail to flush the pg_xact update to
1215  * disk, leading to loss of the transaction commit if the system
1216  * crashes a little later.
1217  *
1218  * Note: we could, but don't bother to, set this flag in
1219  * RecordTransactionAbort. That's because loss of a transaction abort
1220  * is noncritical; the presumption would be that it aborted, anyway.
1221  *
1222  * It's safe to change the delayChkpt flag of our own backend without
1223  * holding the ProcArrayLock, since we're the only one modifying it.
1224  * This makes checkpoint's determination of which xacts are delayChkpt
1225  * a bit fuzzy, but it doesn't matter.
1226  */
1228  MyPgXact->delayChkpt = true;
1229 
1231 
1233  nchildren, children, nrels, rels,
1234  nmsgs, invalMessages,
1235  RelcacheInitFileInval, forceSyncCommit,
1236  MyXactFlags,
1237  InvalidTransactionId /* plain commit */ );
1238 
1239  if (replorigin)
1240  /* Move LSNs forward for this replication origin */
1242  XactLastRecEnd);
1243 
1244  /*
1245  * Record commit timestamp. The value comes from plain commit
1246  * timestamp if there's no replication origin; otherwise, the
1247  * timestamp was already set in replorigin_session_origin_timestamp by
1248  * replication.
1249  *
1250  * We don't need to WAL-log anything here, as the commit record
1251  * written above already contains the data.
1252  */
1253 
1254  if (!replorigin || replorigin_session_origin_timestamp == 0)
1256 
1257  TransactionTreeSetCommitTsData(xid, nchildren, children,
1259  replorigin_session_origin, false);
1260  }
1261 
1262  /*
1263  * Check if we want to commit asynchronously. We can allow the XLOG flush
1264  * to happen asynchronously if synchronous_commit=off, or if the current
1265  * transaction has not performed any WAL-logged operation or didn't assign
1266  * an xid. The transaction can end up not writing any WAL, even if it has
1267  * an xid, if it only wrote to temporary and/or unlogged tables. It can
1268  * end up having written WAL without an xid if it did HOT pruning. In
1269  * case of a crash, the loss of such a transaction will be irrelevant;
1270  * temp tables will be lost anyway, unlogged tables will be truncated and
1271  * HOT pruning will be done again later. (Given the foregoing, you might
1272  * think that it would be unnecessary to emit the XLOG record at all in
1273  * this case, but we don't currently try to do that. It would certainly
1274  * cause problems at least in Hot Standby mode, where the
1275  * KnownAssignedXids machinery requires tracking every XID assignment. It
1276  * might be OK to skip it only when wal_level < replica, but for now we
1277  * don't.)
1278  *
1279  * However, if we're doing cleanup of any non-temp rels or committing any
1280  * command that wanted to force sync commit, then we must flush XLOG
1281  * immediately. (We must not allow asynchronous commit if there are any
1282  * non-temp tables to be deleted, because we might delete the files before
1283  * the COMMIT record is flushed to disk. We do allow asynchronous commit
1284  * if all to-be-deleted tables are temporary though, since they are lost
1285  * anyway if we crash.)
1286  */
1287  if ((wrote_xlog && markXidCommitted &&
1289  forceSyncCommit || nrels > 0)
1290  {
1292 
1293  /*
1294  * Now we may update the CLOG, if we wrote a COMMIT record above
1295  */
1296  if (markXidCommitted)
1297  TransactionIdCommitTree(xid, nchildren, children);
1298  }
1299  else
1300  {
1301  /*
1302  * Asynchronous commit case:
1303  *
1304  * This enables possible committed transaction loss in the case of a
1305  * postmaster crash because WAL buffers are left unwritten. Ideally we
1306  * could issue the WAL write without the fsync, but some
1307  * wal_sync_methods do not allow separate write/fsync.
1308  *
1309  * Report the latest async commit LSN, so that the WAL writer knows to
1310  * flush this commit.
1311  */
1313 
1314  /*
1315  * We must not immediately update the CLOG, since we didn't flush the
1316  * XLOG. Instead, we store the LSN up to which the XLOG must be
1317  * flushed before the CLOG may be updated.
1318  */
1319  if (markXidCommitted)
1320  TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1321  }
1322 
1323  /*
1324  * If we entered a commit critical section, leave it now, and let
1325  * checkpoints proceed.
1326  */
1327  if (markXidCommitted)
1328  {
1329  MyPgXact->delayChkpt = false;
1330  END_CRIT_SECTION();
1331  }
1332 
1333  /* Compute latestXid while we have the child XIDs handy */
1334  latestXid = TransactionIdLatest(xid, nchildren, children);
1335 
1336  /*
1337  * Wait for synchronous replication, if required. Similar to the decision
1338  * above about using committing asynchronously we only want to wait if
1339  * this backend assigned an xid and wrote WAL. No need to wait if an xid
1340  * was assigned due to temporary/unlogged tables or due to HOT pruning.
1341  *
1342  * Note that at this stage we have marked clog, but still show as running
1343  * in the procarray and continue to hold locks.
1344  */
1345  if (wrote_xlog && markXidCommitted)
1347 
1348  /* remember end of last commit record */
1350 
1351  /* Reset XactLastRecEnd until the next transaction writes something */
1352  XactLastRecEnd = 0;
1353 cleanup:
1354  /* Clean up local data */
1355  if (rels)
1356  pfree(rels);
1357 
1358  return latestXid;
1359 }
1360 
1361 
1362 /*
1363  * AtCCI_LocalCache
1364  */
1365 static void
1367 {
1368  /*
1369  * Make any pending relation map changes visible. We must do this before
1370  * processing local sinval messages, so that the map changes will get
1371  * reflected into the relcache when relcache invals are processed.
1372  */
1374 
1375  /*
1376  * Make catalog changes visible to me for the next command.
1377  */
1379 }
1380 
1381 /*
1382  * AtCommit_Memory
1383  */
1384 static void
1386 {
1387  /*
1388  * Now that we're "out" of a transaction, have the system allocate things
1389  * in the top memory context instead of per-transaction contexts.
1390  */
1392 
1393  /*
1394  * Release all transaction-local memory.
1395  */
1396  Assert(TopTransactionContext != NULL);
1398  TopTransactionContext = NULL;
1399  CurTransactionContext = NULL;
1400  CurrentTransactionState->curTransactionContext = NULL;
1401 }
1402 
1403 /* ----------------------------------------------------------------
1404  * CommitSubTransaction stuff
1405  * ----------------------------------------------------------------
1406  */
1407 
1408 /*
1409  * AtSubCommit_Memory
1410  */
1411 static void
1413 {
1415 
1416  Assert(s->parent != NULL);
1417 
1418  /* Return to parent transaction level's memory context. */
1421 
1422  /*
1423  * Ordinarily we cannot throw away the child's CurTransactionContext,
1424  * since the data it contains will be needed at upper commit. However, if
1425  * there isn't actually anything in it, we can throw it away. This avoids
1426  * a small memory leak in the common case of "trivial" subxacts.
1427  */
1429  {
1431  s->curTransactionContext = NULL;
1432  }
1433 }
1434 
1435 /*
1436  * AtSubCommit_childXids
1437  *
1438  * Pass my own XID and my child XIDs up to my parent as committed children.
1439  */
1440 static void
1442 {
1444  int new_nChildXids;
1445 
1446  Assert(s->parent != NULL);
1447 
1448  /*
1449  * The parent childXids array will need to hold my XID and all my
1450  * childXids, in addition to the XIDs already there.
1451  */
1452  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1453 
1454  /* Allocate or enlarge the parent array if necessary */
1455  if (s->parent->maxChildXids < new_nChildXids)
1456  {
1457  int new_maxChildXids;
1458  TransactionId *new_childXids;
1459 
1460  /*
1461  * Make it 2x what's needed right now, to avoid having to enlarge it
1462  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1463  * is what ensures that we don't need to worry about integer overflow
1464  * here or in the calculation of new_nChildXids.)
1465  */
1466  new_maxChildXids = Min(new_nChildXids * 2,
1467  (int) (MaxAllocSize / sizeof(TransactionId)));
1468 
1469  if (new_maxChildXids < new_nChildXids)
1470  ereport(ERROR,
1471  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1472  errmsg("maximum number of committed subtransactions (%d) exceeded",
1473  (int) (MaxAllocSize / sizeof(TransactionId)))));
1474 
1475  /*
1476  * We keep the child-XID arrays in TopTransactionContext; this avoids
1477  * setting up child-transaction contexts for what might be just a few
1478  * bytes of grandchild XIDs.
1479  */
1480  if (s->parent->childXids == NULL)
1481  new_childXids =
1483  new_maxChildXids * sizeof(TransactionId));
1484  else
1485  new_childXids = repalloc(s->parent->childXids,
1486  new_maxChildXids * sizeof(TransactionId));
1487 
1488  s->parent->childXids = new_childXids;
1489  s->parent->maxChildXids = new_maxChildXids;
1490  }
1491 
1492  /*
1493  * Copy all my XIDs to parent's array.
1494  *
1495  * Note: We rely on the fact that the XID of a child always follows that
1496  * of its parent. By copying the XID of this subtransaction before the
1497  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1498  * all XIDs already in the array belong to subtransactions started and
1499  * subcommitted before us, so their XIDs must precede ours.
1500  */
1502 
1503  if (s->nChildXids > 0)
1504  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1505  s->childXids,
1506  s->nChildXids * sizeof(TransactionId));
1507 
1508  s->parent->nChildXids = new_nChildXids;
1509 
1510  /* Release child's array to avoid leakage */
1511  if (s->childXids != NULL)
1512  pfree(s->childXids);
1513  /* We must reset these to avoid double-free if fail later in commit */
1514  s->childXids = NULL;
1515  s->nChildXids = 0;
1516  s->maxChildXids = 0;
1517 }
1518 
1519 /* ----------------------------------------------------------------
1520  * AbortTransaction stuff
1521  * ----------------------------------------------------------------
1522  */
1523 
1524 /*
1525  * RecordTransactionAbort
1526  *
1527  * Returns latest XID among xact and its children, or InvalidTransactionId
1528  * if the xact has no XID. (We compute that here just because it's easier.)
1529  */
1530 static TransactionId
1531 RecordTransactionAbort(bool isSubXact)
1532 {
1534  TransactionId latestXid;
1535  int nrels;
1536  RelFileNode *rels;
1537  int nchildren;
1538  TransactionId *children;
1539  TimestampTz xact_time;
1540 
1541  /*
1542  * If we haven't been assigned an XID, nobody will care whether we aborted
1543  * or not. Hence, we're done in that case. It does not matter if we have
1544  * rels to delete (note that this routine is not responsible for actually
1545  * deleting 'em). We cannot have any child XIDs, either.
1546  */
1547  if (!TransactionIdIsValid(xid))
1548  {
1549  /* Reset XactLastRecEnd until the next transaction writes something */
1550  if (!isSubXact)
1551  XactLastRecEnd = 0;
1552  return InvalidTransactionId;
1553  }
1554 
1555  /*
1556  * We have a valid XID, so we should write an ABORT record for it.
1557  *
1558  * We do not flush XLOG to disk here, since the default assumption after a
1559  * crash would be that we aborted, anyway. For the same reason, we don't
1560  * need to worry about interlocking against checkpoint start.
1561  */
1562 
1563  /*
1564  * Check that we haven't aborted halfway through RecordTransactionCommit.
1565  */
1566  if (TransactionIdDidCommit(xid))
1567  elog(PANIC, "cannot abort transaction %u, it was already committed",
1568  xid);
1569 
1570  /* Fetch the data we need for the abort record */
1571  nrels = smgrGetPendingDeletes(false, &rels);
1572  nchildren = xactGetCommittedChildren(&children);
1573 
1574  /* XXX do we really need a critical section here? */
1576 
1577  /* Write the ABORT record */
1578  if (isSubXact)
1579  xact_time = GetCurrentTimestamp();
1580  else
1581  {
1583  xact_time = xactStopTimestamp;
1584  }
1585 
1586  XactLogAbortRecord(xact_time,
1587  nchildren, children,
1588  nrels, rels,
1590 
1591  /*
1592  * Report the latest async abort LSN, so that the WAL writer knows to
1593  * flush this abort. There's nothing to be gained by delaying this, since
1594  * WALWriter may as well do this when it can. This is important with
1595  * streaming replication because if we don't flush WAL regularly we will
1596  * find that large aborts leave us with a long backlog for when commits
1597  * occur after the abort, increasing our window of data loss should
1598  * problems occur at that point.
1599  */
1600  if (!isSubXact)
1602 
1603  /*
1604  * Mark the transaction aborted in clog. This is not absolutely necessary
1605  * but we may as well do it while we are here; also, in the subxact case
1606  * it is helpful because XactLockTableWait makes use of it to avoid
1607  * waiting for already-aborted subtransactions. It is OK to do it without
1608  * having flushed the ABORT record to disk, because in event of a crash
1609  * we'd be assumed to have aborted anyway.
1610  */
1611  TransactionIdAbortTree(xid, nchildren, children);
1612 
1613  END_CRIT_SECTION();
1614 
1615  /* Compute latestXid while we have the child XIDs handy */
1616  latestXid = TransactionIdLatest(xid, nchildren, children);
1617 
1618  /*
1619  * If we're aborting a subtransaction, we can immediately remove failed
1620  * XIDs from PGPROC's cache of running child XIDs. We do that here for
1621  * subxacts, because we already have the child XID array at hand. For
1622  * main xacts, the equivalent happens just after this function returns.
1623  */
1624  if (isSubXact)
1625  XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1626 
1627  /* Reset XactLastRecEnd until the next transaction writes something */
1628  if (!isSubXact)
1629  XactLastRecEnd = 0;
1630 
1631  /* And clean up local data */
1632  if (rels)
1633  pfree(rels);
1634 
1635  return latestXid;
1636 }
1637 
1638 /*
1639  * AtAbort_Memory
1640  */
1641 static void
1643 {
1644  /*
1645  * Switch into TransactionAbortContext, which should have some free space
1646  * even if nothing else does. We'll work in this context until we've
1647  * finished cleaning up.
1648  *
1649  * It is barely possible to get here when we've not been able to create
1650  * TransactionAbortContext yet; if so use TopMemoryContext.
1651  */
1652  if (TransactionAbortContext != NULL)
1653  MemoryContextSwitchTo(TransactionAbortContext);
1654  else
1656 }
1657 
1658 /*
1659  * AtSubAbort_Memory
1660  */
1661 static void
1663 {
1664  Assert(TransactionAbortContext != NULL);
1665 
1666  MemoryContextSwitchTo(TransactionAbortContext);
1667 }
1668 
1669 
1670 /*
1671  * AtAbort_ResourceOwner
1672  */
1673 static void
1675 {
1676  /*
1677  * Make sure we have a valid ResourceOwner, if possible (else it will be
1678  * NULL, which is OK)
1679  */
1681 }
1682 
1683 /*
1684  * AtSubAbort_ResourceOwner
1685  */
1686 static void
1688 {
1690 
1691  /* Make sure we have a valid ResourceOwner */
1693 }
1694 
1695 
1696 /*
1697  * AtSubAbort_childXids
1698  */
1699 static void
1701 {
1703 
1704  /*
1705  * We keep the child-XID arrays in TopTransactionContext (see
1706  * AtSubCommit_childXids). This means we'd better free the array
1707  * explicitly at abort to avoid leakage.
1708  */
1709  if (s->childXids != NULL)
1710  pfree(s->childXids);
1711  s->childXids = NULL;
1712  s->nChildXids = 0;
1713  s->maxChildXids = 0;
1714 
1715  /*
1716  * We could prune the unreportedXids array here. But we don't bother. That
1717  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1718  * would likely introduce more CPU time into the more common paths, so we
1719  * choose not to do that.
1720  */
1721 }
1722 
1723 /* ----------------------------------------------------------------
1724  * CleanupTransaction stuff
1725  * ----------------------------------------------------------------
1726  */
1727 
1728 /*
1729  * AtCleanup_Memory
1730  */
1731 static void
1733 {
1734  Assert(CurrentTransactionState->parent == NULL);
1735 
1736  /*
1737  * Now that we're "out" of a transaction, have the system allocate things
1738  * in the top memory context instead of per-transaction contexts.
1739  */
1741 
1742  /*
1743  * Clear the special abort context for next time.
1744  */
1745  if (TransactionAbortContext != NULL)
1746  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1747 
1748  /*
1749  * Release all transaction-local memory.
1750  */
1751  if (TopTransactionContext != NULL)
1753  TopTransactionContext = NULL;
1754  CurTransactionContext = NULL;
1755  CurrentTransactionState->curTransactionContext = NULL;
1756 }
1757 
1758 
1759 /* ----------------------------------------------------------------
1760  * CleanupSubTransaction stuff
1761  * ----------------------------------------------------------------
1762  */
1763 
1764 /*
1765  * AtSubCleanup_Memory
1766  */
1767 static void
1769 {
1771 
1772  Assert(s->parent != NULL);
1773 
1774  /* Make sure we're not in an about-to-be-deleted context */
1777 
1778  /*
1779  * Clear the special abort context for next time.
1780  */
1781  if (TransactionAbortContext != NULL)
1782  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1783 
1784  /*
1785  * Delete the subxact local memory contexts. Its CurTransactionContext can
1786  * go too (note this also kills CurTransactionContexts from any children
1787  * of the subxact).
1788  */
1789  if (s->curTransactionContext)
1791  s->curTransactionContext = NULL;
1792 }
1793 
1794 /* ----------------------------------------------------------------
1795  * interface routines
1796  * ----------------------------------------------------------------
1797  */
1798 
1799 /*
1800  * StartTransaction
1801  */
1802 static void
1804 {
1805  TransactionState s;
1806  VirtualTransactionId vxid;
1807 
1808  /*
1809  * Let's just make sure the state stack is empty
1810  */
1812  CurrentTransactionState = s;
1813 
1815 
1816  /*
1817  * check the current transaction state
1818  */
1819  if (s->state != TRANS_DEFAULT)
1820  elog(WARNING, "StartTransaction while in %s state",
1822 
1823  /*
1824  * set the current transaction state information appropriately during
1825  * start processing
1826  */
1827  s->state = TRANS_START;
1828  s->transactionId = InvalidTransactionId; /* until assigned */
1829 
1830  /*
1831  * Make sure we've reset xact state variables
1832  *
1833  * If recovery is still in progress, mark this transaction as read-only.
1834  * We have lower level defences in XLogInsert and elsewhere to stop us
1835  * from modifying data during recovery, but this gives the normal
1836  * indication to the user that the transaction is read-only.
1837  */
1838  if (RecoveryInProgress())
1839  {
1840  s->startedInRecovery = true;
1841  XactReadOnly = true;
1842  }
1843  else
1844  {
1845  s->startedInRecovery = false;
1847  }
1850  forceSyncCommit = false;
1851  MyXactFlags = 0;
1852 
1853  /*
1854  * reinitialize within-transaction counters
1855  */
1859  currentCommandIdUsed = false;
1860 
1861  /*
1862  * initialize reported xid accounting
1863  */
1864  nUnreportedXids = 0;
1865  s->didLogXid = false;
1866 
1867  /*
1868  * must initialize resource-management stuff first
1869  */
1870  AtStart_Memory();
1872 
1873  /*
1874  * Assign a new LocalTransactionId, and combine it with the backendId to
1875  * form a virtual transaction id.
1876  */
1877  vxid.backendId = MyBackendId;
1879 
1880  /*
1881  * Lock the virtual transaction id before we announce it in the proc array
1882  */
1884 
1885  /*
1886  * Advertise it in the proc array. We assume assignment of
1887  * LocalTransactionID is atomic, and the backendId should be set already.
1888  */
1889  Assert(MyProc->backendId == vxid.backendId);
1890  MyProc->lxid = vxid.localTransactionId;
1891 
1892  TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1893 
1894  /*
1895  * set transaction_timestamp() (a/k/a now()). We want this to be the same
1896  * as the first command's statement_timestamp(), so don't do a fresh
1897  * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1898  * xactStopTimestamp as unset.
1899  */
1901  xactStopTimestamp = 0;
1903 
1904  /*
1905  * initialize current transaction state fields
1906  *
1907  * note: prevXactReadOnly is not used at the outermost level
1908  */
1909  s->nestingLevel = 1;
1910  s->gucNestLevel = 1;
1911  s->childXids = NULL;
1912  s->nChildXids = 0;
1913  s->maxChildXids = 0;
1915  /* SecurityRestrictionContext should never be set outside a transaction */
1916  Assert(s->prevSecContext == 0);
1917 
1918  /*
1919  * initialize other subsystems for new transaction
1920  */
1921  AtStart_GUC();
1922  AtStart_Cache();
1924 
1925  /*
1926  * done with start processing, set current transaction state to "in
1927  * progress"
1928  */
1929  s->state = TRANS_INPROGRESS;
1930 
1931  ShowTransactionState("StartTransaction");
1932 }
1933 
1934 
1935 /*
1936  * CommitTransaction
1937  *
1938  * NB: if you change this routine, better look at PrepareTransaction too!
1939  */
1940 static void
1942 {
1944  TransactionId latestXid;
1945  bool is_parallel_worker;
1946 
1947  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
1948 
1949  /* Enforce parallel mode restrictions during parallel worker commit. */
1950  if (is_parallel_worker)
1952 
1953  ShowTransactionState("CommitTransaction");
1954 
1955  /*
1956  * check the current transaction state
1957  */
1958  if (s->state != TRANS_INPROGRESS)
1959  elog(WARNING, "CommitTransaction while in %s state",
1961  Assert(s->parent == NULL);
1962 
1963  /*
1964  * Do pre-commit processing that involves calling user-defined code, such
1965  * as triggers. Since closing cursors could queue trigger actions,
1966  * triggers could open cursors, etc, we have to keep looping until there's
1967  * nothing left to do.
1968  */
1969  for (;;)
1970  {
1971  /*
1972  * Fire all currently pending deferred triggers.
1973  */
1975 
1976  /*
1977  * Close open portals (converting holdable ones into static portals).
1978  * If there weren't any, we are done ... otherwise loop back to check
1979  * if they queued deferred triggers. Lather, rinse, repeat.
1980  */
1981  if (!PreCommit_Portals(false))
1982  break;
1983  }
1984 
1987 
1988  /*
1989  * The remaining actions cannot call any user-defined code, so it's safe
1990  * to start shutting down within-transaction services. But note that most
1991  * of this stuff could still throw an error, which would switch us into
1992  * the transaction-abort path.
1993  */
1994 
1995  /* If we might have parallel workers, clean them up now. */
1996  if (IsInParallelMode())
1997  AtEOXact_Parallel(true);
1998 
1999  /* Shut down the deferred-trigger manager */
2000  AfterTriggerEndXact(true);
2001 
2002  /*
2003  * Let ON COMMIT management do its thing (must happen after closing
2004  * cursors, to avoid dangling-reference problems)
2005  */
2007 
2008  /* close large objects before lower-level cleanup */
2009  AtEOXact_LargeObject(true);
2010 
2011  /*
2012  * Mark serializable transaction as complete for predicate locking
2013  * purposes. This should be done as late as we can put it and still allow
2014  * errors to be raised for failure patterns found at commit.
2015  */
2017 
2018  /*
2019  * Insert notifications sent by NOTIFY commands into the queue. This
2020  * should be late in the pre-commit sequence to minimize time spent
2021  * holding the notify-insertion lock.
2022  */
2023  PreCommit_Notify();
2024 
2025  /* Prevent cancel/die interrupt while cleaning up */
2026  HOLD_INTERRUPTS();
2027 
2028  /* Commit updates to the relation map --- do this as late as possible */
2029  AtEOXact_RelationMap(true);
2030 
2031  /*
2032  * set the current transaction state information appropriately during
2033  * commit processing
2034  */
2035  s->state = TRANS_COMMIT;
2036  s->parallelModeLevel = 0;
2037 
2038  if (!is_parallel_worker)
2039  {
2040  /*
2041  * We need to mark our XIDs as committed in pg_xact. This is where we
2042  * durably commit.
2043  */
2044  latestXid = RecordTransactionCommit();
2045  }
2046  else
2047  {
2048  /*
2049  * We must not mark our XID committed; the parallel master is
2050  * responsible for that.
2051  */
2052  latestXid = InvalidTransactionId;
2053 
2054  /*
2055  * Make sure the master will know about any WAL we wrote before it
2056  * commits.
2057  */
2059  }
2060 
2061  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2062 
2063  /*
2064  * Let others know about no transaction in progress by me. Note that this
2065  * must be done _before_ releasing locks we hold and _after_
2066  * RecordTransactionCommit.
2067  */
2068  ProcArrayEndTransaction(MyProc, latestXid);
2069 
2070  /*
2071  * This is all post-commit cleanup. Note that if an error is raised here,
2072  * it's too late to abort the transaction. This should be just
2073  * noncritical resource releasing.
2074  *
2075  * The ordering of operations is not entirely random. The idea is:
2076  * release resources visible to other backends (eg, files, buffer pins);
2077  * then release locks; then release backend-local resources. We want to
2078  * release locks at the point where any backend waiting for us will see
2079  * our transaction as being fully cleaned up.
2080  *
2081  * Resources that can be associated with individual queries are handled by
2082  * the ResourceOwner mechanism. The other calls here are for backend-wide
2083  * state.
2084  */
2085 
2086  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2087  : XACT_EVENT_COMMIT);
2088 
2091  true, true);
2092 
2093  /* Check we've released all buffer pins */
2094  AtEOXact_Buffers(true);
2095 
2096  /* Clean up the relation cache */
2097  AtEOXact_RelationCache(true);
2098 
2099  /*
2100  * Make catalog changes visible to all backends. This has to happen after
2101  * relcache references are dropped (see comments for
2102  * AtEOXact_RelationCache), but before locks are released (if anyone is
2103  * waiting for lock on a relation we've modified, we want them to know
2104  * about the catalog change before they start using the relation).
2105  */
2106  AtEOXact_Inval(true);
2107 
2109 
2112  true, true);
2115  true, true);
2116 
2117  /*
2118  * Likewise, dropping of files deleted during the transaction is best done
2119  * after releasing relcache and buffer pins. (This is not strictly
2120  * necessary during commit, since such pins should have been released
2121  * already, but this ordering is definitely critical during abort.) Since
2122  * this may take many seconds, also delay until after releasing locks.
2123  * Other backends will observe the attendant catalog changes and not
2124  * attempt to access affected files.
2125  */
2126  smgrDoPendingDeletes(true);
2127 
2128  AtCommit_Notify();
2129  AtEOXact_GUC(true, 1);
2130  AtEOXact_SPI(true);
2132  AtEOXact_Namespace(true, is_parallel_worker);
2133  AtEOXact_SMgr();
2134  AtEOXact_Files();
2136  AtEOXact_HashTables(true);
2137  AtEOXact_PgStat(true);
2138  AtEOXact_Snapshot(true, false);
2139  AtEOXact_ApplyLauncher(true);
2141 
2142  CurrentResourceOwner = NULL;
2144  s->curTransactionOwner = NULL;
2147 
2148  AtCommit_Memory();
2149 
2152  s->nestingLevel = 0;
2153  s->gucNestLevel = 0;
2154  s->childXids = NULL;
2155  s->nChildXids = 0;
2156  s->maxChildXids = 0;
2157 
2160 
2161  /*
2162  * done with commit processing, set current transaction state back to
2163  * default
2164  */
2165  s->state = TRANS_DEFAULT;
2166 
2168 }
2169 
2170 
2171 /*
2172  * PrepareTransaction
2173  *
2174  * NB: if you change this routine, better look at CommitTransaction too!
2175  */
2176 static void
2178 {
2181  GlobalTransaction gxact;
2182  TimestampTz prepared_at;
2183 
2185 
2186  ShowTransactionState("PrepareTransaction");
2187 
2188  /*
2189  * check the current transaction state
2190  */
2191  if (s->state != TRANS_INPROGRESS)
2192  elog(WARNING, "PrepareTransaction while in %s state",
2194  Assert(s->parent == NULL);
2195 
2196  /*
2197  * Do pre-commit processing that involves calling user-defined code, such
2198  * as triggers. Since closing cursors could queue trigger actions,
2199  * triggers could open cursors, etc, we have to keep looping until there's
2200  * nothing left to do.
2201  */
2202  for (;;)
2203  {
2204  /*
2205  * Fire all currently pending deferred triggers.
2206  */
2208 
2209  /*
2210  * Close open portals (converting holdable ones into static portals).
2211  * If there weren't any, we are done ... otherwise loop back to check
2212  * if they queued deferred triggers. Lather, rinse, repeat.
2213  */
2214  if (!PreCommit_Portals(true))
2215  break;
2216  }
2217 
2219 
2220  /*
2221  * The remaining actions cannot call any user-defined code, so it's safe
2222  * to start shutting down within-transaction services. But note that most
2223  * of this stuff could still throw an error, which would switch us into
2224  * the transaction-abort path.
2225  */
2226 
2227  /* Shut down the deferred-trigger manager */
2228  AfterTriggerEndXact(true);
2229 
2230  /*
2231  * Let ON COMMIT management do its thing (must happen after closing
2232  * cursors, to avoid dangling-reference problems)
2233  */
2235 
2236  /* close large objects before lower-level cleanup */
2237  AtEOXact_LargeObject(true);
2238 
2239  /*
2240  * Mark serializable transaction as complete for predicate locking
2241  * purposes. This should be done as late as we can put it and still allow
2242  * errors to be raised for failure patterns found at commit.
2243  */
2245 
2246  /* NOTIFY will be handled below */
2247 
2248  /*
2249  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2250  * this transaction. Having the prepared xact hold locks on another
2251  * backend's temp table seems a bad idea --- for instance it would prevent
2252  * the backend from exiting. There are other problems too, such as how to
2253  * clean up the source backend's local buffers and ON COMMIT state if the
2254  * prepared xact includes a DROP of a temp table.
2255  *
2256  * We must check this after executing any ON COMMIT actions, because they
2257  * might still access a temp relation.
2258  *
2259  * XXX In principle this could be relaxed to allow some useful special
2260  * cases, such as a temp table created and dropped all within the
2261  * transaction. That seems to require much more bookkeeping though.
2262  */
2264  ereport(ERROR,
2265  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2266  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2267 
2268  /*
2269  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2270  * supported if we added cleanup logic to twophase.c, but for now it
2271  * doesn't seem worth the trouble.
2272  */
2274  ereport(ERROR,
2275  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2276  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2277 
2278  /*
2279  * Don't allow PREPARE but for transaction that has/might kill logical
2280  * replication workers.
2281  */
2283  ereport(ERROR,
2284  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2285  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
2286 
2287  /* Prevent cancel/die interrupt while cleaning up */
2288  HOLD_INTERRUPTS();
2289 
2290  /*
2291  * set the current transaction state information appropriately during
2292  * prepare processing
2293  */
2294  s->state = TRANS_PREPARE;
2295 
2296  prepared_at = GetCurrentTimestamp();
2297 
2298  /* Tell bufmgr and smgr to prepare for commit */
2299  BufmgrCommit();
2300 
2301  /*
2302  * Reserve the GID for this transaction. This could fail if the requested
2303  * GID is invalid or already in use.
2304  */
2305  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2306  GetUserId(), MyDatabaseId);
2307  prepareGID = NULL;
2308 
2309  /*
2310  * Collect data for the 2PC state file. Note that in general, no actual
2311  * state change should happen in the called modules during this step,
2312  * since it's still possible to fail before commit, and in that case we
2313  * want transaction abort to be able to clean up. (In particular, the
2314  * AtPrepare routines may error out if they find cases they cannot
2315  * handle.) State cleanup should happen in the PostPrepare routines
2316  * below. However, some modules can go ahead and clear state here because
2317  * they wouldn't do anything with it during abort anyway.
2318  *
2319  * Note: because the 2PC state file records will be replayed in the same
2320  * order they are made, the order of these calls has to match the order in
2321  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2322  * PREPARED; in particular, pay attention to whether things should happen
2323  * before or after releasing the transaction's locks.
2324  */
2325  StartPrepare(gxact);
2326 
2327  AtPrepare_Notify();
2328  AtPrepare_Locks();
2330  AtPrepare_PgStat();
2333 
2334  /*
2335  * Here is where we really truly prepare.
2336  *
2337  * We have to record transaction prepares even if we didn't make any
2338  * updates, because the transaction manager might get confused if we lose
2339  * a global transaction.
2340  */
2341  EndPrepare(gxact);
2342 
2343  /*
2344  * Now we clean up backend-internal state and release internal resources.
2345  */
2346 
2347  /* Reset XactLastRecEnd until the next transaction writes something */
2348  XactLastRecEnd = 0;
2349 
2350  /*
2351  * Let others know about no transaction in progress by me. This has to be
2352  * done *after* the prepared transaction has been marked valid, else
2353  * someone may think it is unlocked and recyclable.
2354  */
2356 
2357  /*
2358  * In normal commit-processing, this is all non-critical post-transaction
2359  * cleanup. When the transaction is prepared, however, it's important
2360  * that the locks and other per-backend resources are transferred to the
2361  * prepared transaction's PGPROC entry. Note that if an error is raised
2362  * here, it's too late to abort the transaction. XXX: This probably should
2363  * be in a critical section, to force a PANIC if any of this fails, but
2364  * that cure could be worse than the disease.
2365  */
2366 
2368 
2371  true, true);
2372 
2373  /* Check we've released all buffer pins */
2374  AtEOXact_Buffers(true);
2375 
2376  /* Clean up the relation cache */
2377  AtEOXact_RelationCache(true);
2378 
2379  /* notify doesn't need a postprepare call */
2380 
2382 
2384 
2385  PostPrepare_smgr();
2386 
2387  PostPrepare_MultiXact(xid);
2388 
2389  PostPrepare_Locks(xid);
2391 
2394  true, true);
2397  true, true);
2398 
2399  /*
2400  * Allow another backend to finish the transaction. After
2401  * PostPrepare_Twophase(), the transaction is completely detached from our
2402  * backend. The rest is just non-critical cleanup of backend-local state.
2403  */
2405 
2406  /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2407  AtEOXact_GUC(true, 1);
2408  AtEOXact_SPI(true);
2410  AtEOXact_Namespace(true, false);
2411  AtEOXact_SMgr();
2412  AtEOXact_Files();
2414  AtEOXact_HashTables(true);
2415  /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2416  AtEOXact_Snapshot(true, true);
2418 
2419  CurrentResourceOwner = NULL;
2421  s->curTransactionOwner = NULL;
2424 
2425  AtCommit_Memory();
2426 
2429  s->nestingLevel = 0;
2430  s->gucNestLevel = 0;
2431  s->childXids = NULL;
2432  s->nChildXids = 0;
2433  s->maxChildXids = 0;
2434 
2437 
2438  /*
2439  * done with 1st phase commit processing, set current transaction state
2440  * back to default
2441  */
2442  s->state = TRANS_DEFAULT;
2443 
2445 }
2446 
2447 
2448 /*
2449  * AbortTransaction
2450  */
2451 static void
2453 {
2455  TransactionId latestXid;
2456  bool is_parallel_worker;
2457 
2458  /* Prevent cancel/die interrupt while cleaning up */
2459  HOLD_INTERRUPTS();
2460 
2461  /* Make sure we have a valid memory context and resource owner */
2462  AtAbort_Memory();
2464 
2465  /*
2466  * Release any LW locks we might be holding as quickly as possible.
2467  * (Regular locks, however, must be held till we finish aborting.)
2468  * Releasing LW locks is critical since we might try to grab them again
2469  * while cleaning up!
2470  */
2471  LWLockReleaseAll();
2472 
2473  /* Clear wait information and command progress indicator */
2476 
2477  /* Clean up buffer I/O and buffer context locks, too */
2478  AbortBufferIO();
2479  UnlockBuffers();
2480 
2481  /* Reset WAL record construction state */
2483 
2484  /* Cancel condition variable sleep */
2486 
2487  /*
2488  * Also clean up any open wait for lock, since the lock manager will choke
2489  * if we try to wait for another lock before doing this.
2490  */
2491  LockErrorCleanup();
2492 
2493  /*
2494  * If any timeout events are still active, make sure the timeout interrupt
2495  * is scheduled. This covers possible loss of a timeout interrupt due to
2496  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2497  * We delay this till after LockErrorCleanup so that we don't uselessly
2498  * reschedule lock or deadlock check timeouts.
2499  */
2501 
2502  /*
2503  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2504  * handler. We do this fairly early in the sequence so that the timeout
2505  * infrastructure will be functional if needed while aborting.
2506  */
2508 
2509  /*
2510  * check the current transaction state
2511  */
2512  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2513  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2514  elog(WARNING, "AbortTransaction while in %s state",
2516  Assert(s->parent == NULL);
2517 
2518  /*
2519  * set the current transaction state information appropriately during the
2520  * abort processing
2521  */
2522  s->state = TRANS_ABORT;
2523 
2524  /*
2525  * Reset user ID which might have been changed transiently. We need this
2526  * to clean up in case control escaped out of a SECURITY DEFINER function
2527  * or other local change of CurrentUserId; therefore, the prior value of
2528  * SecurityRestrictionContext also needs to be restored.
2529  *
2530  * (Note: it is not necessary to restore session authorization or role
2531  * settings here because those can only be changed via GUC, and GUC will
2532  * take care of rolling them back if need be.)
2533  */
2535 
2536  /* If in parallel mode, clean up workers and exit parallel mode. */
2537  if (IsInParallelMode())
2538  {
2539  AtEOXact_Parallel(false);
2540  s->parallelModeLevel = 0;
2541  }
2542 
2543  /*
2544  * do abort processing
2545  */
2546  AfterTriggerEndXact(false); /* 'false' means it's abort */
2547  AtAbort_Portals();
2548  AtEOXact_LargeObject(false);
2549  AtAbort_Notify();
2550  AtEOXact_RelationMap(false);
2551  AtAbort_Twophase();
2552 
2553  /*
2554  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2555  * far as assigning an XID to advertise). But if we're inside a parallel
2556  * worker, skip this; the user backend must be the one to write the abort
2557  * record.
2558  */
2559  if (!is_parallel_worker)
2560  latestXid = RecordTransactionAbort(false);
2561  else
2562  {
2563  latestXid = InvalidTransactionId;
2564 
2565  /*
2566  * Since the parallel master won't get our value of XactLastRecEnd in
2567  * this case, we nudge WAL-writer ourselves in this case. See related
2568  * comments in RecordTransactionAbort for why this matters.
2569  */
2571  }
2572 
2573  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2574 
2575  /*
2576  * Let others know about no transaction in progress by me. Note that this
2577  * must be done _before_ releasing locks we hold and _after_
2578  * RecordTransactionAbort.
2579  */
2580  ProcArrayEndTransaction(MyProc, latestXid);
2581 
2582  /*
2583  * Post-abort cleanup. See notes in CommitTransaction() concerning
2584  * ordering. We can skip all of it if the transaction failed before
2585  * creating a resource owner.
2586  */
2587  if (TopTransactionResourceOwner != NULL)
2588  {
2589  if (is_parallel_worker)
2591  else
2593 
2596  false, true);
2597  AtEOXact_Buffers(false);
2598  AtEOXact_RelationCache(false);
2599  AtEOXact_Inval(false);
2603  false, true);
2606  false, true);
2607  smgrDoPendingDeletes(false);
2608 
2609  AtEOXact_GUC(false, 1);
2610  AtEOXact_SPI(false);
2612  AtEOXact_Namespace(false, is_parallel_worker);
2613  AtEOXact_SMgr();
2614  AtEOXact_Files();
2616  AtEOXact_HashTables(false);
2617  AtEOXact_PgStat(false);
2618  AtEOXact_ApplyLauncher(false);
2620  }
2621 
2622  /*
2623  * State remains TRANS_ABORT until CleanupTransaction().
2624  */
2626 }
2627 
2628 /*
2629  * CleanupTransaction
2630  */
2631 static void
2633 {
2635 
2636  /*
2637  * State should still be TRANS_ABORT from AbortTransaction().
2638  */
2639  if (s->state != TRANS_ABORT)
2640  elog(FATAL, "CleanupTransaction: unexpected state %s",
2642 
2643  /*
2644  * do abort cleanup processing
2645  */
2646  AtCleanup_Portals(); /* now safe to release portal memory */
2647  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2648 
2649  CurrentResourceOwner = NULL; /* and resource owner */
2652  s->curTransactionOwner = NULL;
2655 
2656  AtCleanup_Memory(); /* and transaction memory */
2657 
2660  s->nestingLevel = 0;
2661  s->gucNestLevel = 0;
2662  s->childXids = NULL;
2663  s->nChildXids = 0;
2664  s->maxChildXids = 0;
2665  s->parallelModeLevel = 0;
2666 
2669 
2670  /*
2671  * done with abort processing, set current transaction state back to
2672  * default
2673  */
2674  s->state = TRANS_DEFAULT;
2675 }
2676 
2677 /*
2678  * StartTransactionCommand
2679  */
2680 void
2682 {
2684 
2685  switch (s->blockState)
2686  {
2687  /*
2688  * if we aren't in a transaction block, we just do our usual start
2689  * transaction.
2690  */
2691  case TBLOCK_DEFAULT:
2692  StartTransaction();
2694  break;
2695 
2696  /*
2697  * We are somewhere in a transaction block or subtransaction and
2698  * about to start a new command. For now we do nothing, but
2699  * someday we may do command-local resource initialization. (Note
2700  * that any needed CommandCounterIncrement was done by the
2701  * previous CommitTransactionCommand.)
2702  */
2703  case TBLOCK_INPROGRESS:
2705  case TBLOCK_SUBINPROGRESS:
2706  break;
2707 
2708  /*
2709  * Here we are in a failed transaction block (one of the commands
2710  * caused an abort) so we do nothing but remain in the abort
2711  * state. Eventually we will get a ROLLBACK command which will
2712  * get us out of this state. (It is up to other code to ensure
2713  * that no commands other than ROLLBACK will be processed in these
2714  * states.)
2715  */
2716  case TBLOCK_ABORT:
2717  case TBLOCK_SUBABORT:
2718  break;
2719 
2720  /* These cases are invalid. */
2721  case TBLOCK_STARTED:
2722  case TBLOCK_BEGIN:
2724  case TBLOCK_SUBBEGIN:
2725  case TBLOCK_END:
2726  case TBLOCK_SUBRELEASE:
2727  case TBLOCK_SUBCOMMIT:
2728  case TBLOCK_ABORT_END:
2729  case TBLOCK_SUBABORT_END:
2730  case TBLOCK_ABORT_PENDING:
2732  case TBLOCK_SUBRESTART:
2734  case TBLOCK_PREPARE:
2735  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2737  break;
2738  }
2739 
2740  /*
2741  * We must switch to CurTransactionContext before returning. This is
2742  * already done if we called StartTransaction, otherwise not.
2743  */
2744  Assert(CurTransactionContext != NULL);
2746 }
2747 
2748 /*
2749  * CommitTransactionCommand
2750  */
2751 void
2753 {
2755 
2756  switch (s->blockState)
2757  {
2758  /*
2759  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2760  * StartTransactionCommand didn't set the STARTED state
2761  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2762  * by EndParallelWorkerTransaction(), not this function.
2763  */
2764  case TBLOCK_DEFAULT:
2766  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2768  break;
2769 
2770  /*
2771  * If we aren't in a transaction block, just do our usual
2772  * transaction commit, and return to the idle state.
2773  */
2774  case TBLOCK_STARTED:
2777  break;
2778 
2779  /*
2780  * We are completing a "BEGIN TRANSACTION" command, so we change
2781  * to the "transaction block in progress" state and return. (We
2782  * assume the BEGIN did nothing to the database, so we need no
2783  * CommandCounterIncrement.)
2784  */
2785  case TBLOCK_BEGIN:
2787  break;
2788 
2789  /*
2790  * This is the case when we have finished executing a command
2791  * someplace within a transaction block. We increment the command
2792  * counter and return.
2793  */
2794  case TBLOCK_INPROGRESS:
2796  case TBLOCK_SUBINPROGRESS:
2798  break;
2799 
2800  /*
2801  * We are completing a "COMMIT" command. Do it and return to the
2802  * idle state.
2803  */
2804  case TBLOCK_END:
2807  break;
2808 
2809  /*
2810  * Here we are in the middle of a transaction block but one of the
2811  * commands caused an abort so we do nothing but remain in the
2812  * abort state. Eventually we will get a ROLLBACK command.
2813  */
2814  case TBLOCK_ABORT:
2815  case TBLOCK_SUBABORT:
2816  break;
2817 
2818  /*
2819  * Here we were in an aborted transaction block and we just got
2820  * the ROLLBACK command from the user, so clean up the
2821  * already-aborted transaction and return to the idle state.
2822  */
2823  case TBLOCK_ABORT_END:
2826  break;
2827 
2828  /*
2829  * Here we were in a perfectly good transaction block but the user
2830  * told us to ROLLBACK anyway. We have to abort the transaction
2831  * and then clean up.
2832  */
2833  case TBLOCK_ABORT_PENDING:
2834  AbortTransaction();
2837  break;
2838 
2839  /*
2840  * We are completing a "PREPARE TRANSACTION" command. Do it and
2841  * return to the idle state.
2842  */
2843  case TBLOCK_PREPARE:
2846  break;
2847 
2848  /*
2849  * We were just issued a SAVEPOINT inside a transaction block.
2850  * Start a subtransaction. (DefineSavepoint already did
2851  * PushTransaction, so as to have someplace to put the SUBBEGIN
2852  * state.)
2853  */
2854  case TBLOCK_SUBBEGIN:
2857  break;
2858 
2859  /*
2860  * We were issued a RELEASE command, so we end the current
2861  * subtransaction and return to the parent transaction. The parent
2862  * might be ended too, so repeat till we find an INPROGRESS
2863  * transaction or subtransaction.
2864  */
2865  case TBLOCK_SUBRELEASE:
2866  do
2867  {
2869  s = CurrentTransactionState; /* changed by pop */
2870  } while (s->blockState == TBLOCK_SUBRELEASE);
2871 
2874  break;
2875 
2876  /*
2877  * We were issued a COMMIT, so we end the current subtransaction
2878  * hierarchy and perform final commit. We do this by rolling up
2879  * any subtransactions into their parent, which leads to O(N^2)
2880  * operations with respect to resource owners - this isn't that
2881  * bad until we approach a thousands of savepoints but is
2882  * necessary for correctness should after triggers create new
2883  * resource owners.
2884  */
2885  case TBLOCK_SUBCOMMIT:
2886  do
2887  {
2889  s = CurrentTransactionState; /* changed by pop */
2890  } while (s->blockState == TBLOCK_SUBCOMMIT);
2891  /* If we had a COMMIT command, finish off the main xact too */
2892  if (s->blockState == TBLOCK_END)
2893  {
2894  Assert(s->parent == NULL);
2897  }
2898  else if (s->blockState == TBLOCK_PREPARE)
2899  {
2900  Assert(s->parent == NULL);
2903  }
2904  else
2905  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2907  break;
2908 
2909  /*
2910  * The current already-failed subtransaction is ending due to a
2911  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2912  * examine the parent (which could be in any of several states).
2913  */
2914  case TBLOCK_SUBABORT_END:
2917  break;
2918 
2919  /*
2920  * As above, but it's not dead yet, so abort first.
2921  */
2926  break;
2927 
2928  /*
2929  * The current subtransaction is the target of a ROLLBACK TO
2930  * command. Abort and pop it, then start a new subtransaction
2931  * with the same name.
2932  */
2933  case TBLOCK_SUBRESTART:
2934  {
2935  char *name;
2936  int savepointLevel;
2937 
2938  /* save name and keep Cleanup from freeing it */
2939  name = s->name;
2940  s->name = NULL;
2941  savepointLevel = s->savepointLevel;
2942 
2945 
2946  DefineSavepoint(NULL);
2947  s = CurrentTransactionState; /* changed by push */
2948  s->name = name;
2949  s->savepointLevel = savepointLevel;
2950 
2951  /* This is the same as TBLOCK_SUBBEGIN case */
2955  }
2956  break;
2957 
2958  /*
2959  * Same as above, but the subtransaction had already failed, so we
2960  * don't need AbortSubTransaction.
2961  */
2963  {
2964  char *name;
2965  int savepointLevel;
2966 
2967  /* save name and keep Cleanup from freeing it */
2968  name = s->name;
2969  s->name = NULL;
2970  savepointLevel = s->savepointLevel;
2971 
2973 
2974  DefineSavepoint(NULL);
2975  s = CurrentTransactionState; /* changed by push */
2976  s->name = name;
2977  s->savepointLevel = savepointLevel;
2978 
2979  /* This is the same as TBLOCK_SUBBEGIN case */
2983  }
2984  break;
2985  }
2986 }
2987 
2988 /*
2989  * AbortCurrentTransaction
2990  */
2991 void
2993 {
2995 
2996  switch (s->blockState)
2997  {
2998  case TBLOCK_DEFAULT:
2999  if (s->state == TRANS_DEFAULT)
3000  {
3001  /* we are idle, so nothing to do */
3002  }
3003  else
3004  {
3005  /*
3006  * We can get here after an error during transaction start
3007  * (state will be TRANS_START). Need to clean up the
3008  * incompletely started transaction. First, adjust the
3009  * low-level state to suppress warning message from
3010  * AbortTransaction.
3011  */
3012  if (s->state == TRANS_START)
3013  s->state = TRANS_INPROGRESS;
3014  AbortTransaction();
3016  }
3017  break;
3018 
3019  /*
3020  * If we aren't in a transaction block, we just do the basic abort
3021  * & cleanup transaction. For this purpose, we treat an implicit
3022  * transaction block as if it were a simple statement.
3023  */
3024  case TBLOCK_STARTED:
3026  AbortTransaction();
3029  break;
3030 
3031  /*
3032  * If we are in TBLOCK_BEGIN it means something screwed up right
3033  * after reading "BEGIN TRANSACTION". We assume that the user
3034  * will interpret the error as meaning the BEGIN failed to get him
3035  * into a transaction block, so we should abort and return to idle
3036  * state.
3037  */
3038  case TBLOCK_BEGIN:
3039  AbortTransaction();
3042  break;
3043 
3044  /*
3045  * We are somewhere in a transaction block and we've gotten a
3046  * failure, so we abort the transaction and set up the persistent
3047  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3048  */
3049  case TBLOCK_INPROGRESS:
3051  AbortTransaction();
3052  s->blockState = TBLOCK_ABORT;
3053  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3054  break;
3055 
3056  /*
3057  * Here, we failed while trying to COMMIT. Clean up the
3058  * transaction and return to idle state (we do not want to stay in
3059  * the transaction).
3060  */
3061  case TBLOCK_END:
3062  AbortTransaction();
3065  break;
3066 
3067  /*
3068  * Here, we are already in an aborted transaction state and are
3069  * waiting for a ROLLBACK, but for some reason we failed again! So
3070  * we just remain in the abort state.
3071  */
3072  case TBLOCK_ABORT:
3073  case TBLOCK_SUBABORT:
3074  break;
3075 
3076  /*
3077  * We are in a failed transaction and we got the ROLLBACK command.
3078  * We have already aborted, we just need to cleanup and go to idle
3079  * state.
3080  */
3081  case TBLOCK_ABORT_END:
3084  break;
3085 
3086  /*
3087  * We are in a live transaction and we got a ROLLBACK command.
3088  * Abort, cleanup, go to idle state.
3089  */
3090  case TBLOCK_ABORT_PENDING:
3091  AbortTransaction();
3094  break;
3095 
3096  /*
3097  * Here, we failed while trying to PREPARE. Clean up the
3098  * transaction and return to idle state (we do not want to stay in
3099  * the transaction).
3100  */
3101  case TBLOCK_PREPARE:
3102  AbortTransaction();
3105  break;
3106 
3107  /*
3108  * We got an error inside a subtransaction. Abort just the
3109  * subtransaction, and go to the persistent SUBABORT state until
3110  * we get ROLLBACK.
3111  */
3112  case TBLOCK_SUBINPROGRESS:
3115  break;
3116 
3117  /*
3118  * If we failed while trying to create a subtransaction, clean up
3119  * the broken subtransaction and abort the parent. The same
3120  * applies if we get a failure while ending a subtransaction.
3121  */
3122  case TBLOCK_SUBBEGIN:
3123  case TBLOCK_SUBRELEASE:
3124  case TBLOCK_SUBCOMMIT:
3126  case TBLOCK_SUBRESTART:
3130  break;
3131 
3132  /*
3133  * Same as above, except the Abort() was already done.
3134  */
3135  case TBLOCK_SUBABORT_END:
3139  break;
3140  }
3141 }
3142 
3143 /*
3144  * PreventTransactionChain
3145  *
3146  * This routine is to be called by statements that must not run inside
3147  * a transaction block, typically because they have non-rollback-able
3148  * side effects or do internal commits.
3149  *
3150  * If we have already started a transaction block, issue an error; also issue
3151  * an error if we appear to be running inside a user-defined function (which
3152  * could issue more commands and possibly cause a failure after the statement
3153  * completes). Subtransactions are verboten too.
3154  *
3155  * isTopLevel: passed down from ProcessUtility to determine whether we are
3156  * inside a function. (We will always fail if this is false, but it's
3157  * convenient to centralize the check here instead of making callers do it.)
3158  * stmtType: statement type name, for error messages.
3159  */
3160 void
3161 PreventTransactionChain(bool isTopLevel, const char *stmtType)
3162 {
3163  /*
3164  * xact block already started?
3165  */
3166  if (IsTransactionBlock())
3167  ereport(ERROR,
3168  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3169  /* translator: %s represents an SQL statement name */
3170  errmsg("%s cannot run inside a transaction block",
3171  stmtType)));
3172 
3173  /*
3174  * subtransaction?
3175  */
3176  if (IsSubTransaction())
3177  ereport(ERROR,
3178  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3179  /* translator: %s represents an SQL statement name */
3180  errmsg("%s cannot run inside a subtransaction",
3181  stmtType)));
3182 
3183  /*
3184  * inside a function call?
3185  */
3186  if (!isTopLevel)
3187  ereport(ERROR,
3188  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3189  /* translator: %s represents an SQL statement name */
3190  errmsg("%s cannot be executed from a function", stmtType)));
3191 
3192  /* If we got past IsTransactionBlock test, should be in default state */
3193  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3194  CurrentTransactionState->blockState != TBLOCK_STARTED)
3195  elog(FATAL, "cannot prevent transaction chain");
3196  /* all okay */
3197 }
3198 
3199 /*
3200  * These two functions allow for warnings or errors if a command is
3201  * executed outside of a transaction block.
3202  *
3203  * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and
3204  * SET that have no effect issue warnings, all other no-effect commands
3205  * generate errors.
3206  */
3207 void
3208 WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
3209 {
3210  CheckTransactionChain(isTopLevel, false, stmtType);
3211 }
3212 
3213 void
3214 RequireTransactionChain(bool isTopLevel, const char *stmtType)
3215 {
3216  CheckTransactionChain(isTopLevel, true, stmtType);
3217 }
3218 
3219 /*
3220  * RequireTransactionChain
3221  *
3222  * This routine is to be called by statements that must run inside
3223  * a transaction block, because they have no effects that persist past
3224  * transaction end (and so calling them outside a transaction block
3225  * is presumably an error). DECLARE CURSOR is an example.
3226  *
3227  * If we appear to be running inside a user-defined function, we do not
3228  * issue anything, since the function could issue more commands that make
3229  * use of the current statement's results. Likewise subtransactions.
3230  * Thus this is an inverse for PreventTransactionChain.
3231  *
3232  * isTopLevel: passed down from ProcessUtility to determine whether we are
3233  * inside a function.
3234  * stmtType: statement type name, for warning or error messages.
3235  */
3236 static void
3237 CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
3238 {
3239  /*
3240  * xact block already started?
3241  */
3242  if (IsTransactionBlock())
3243  return;
3244 
3245  /*
3246  * subtransaction?
3247  */
3248  if (IsSubTransaction())
3249  return;
3250 
3251  /*
3252  * inside a function call?
3253  */
3254  if (!isTopLevel)
3255  return;
3256 
3257  ereport(throwError ? ERROR : WARNING,
3258  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3259  /* translator: %s represents an SQL statement name */
3260  errmsg("%s can only be used in transaction blocks",
3261  stmtType)));
3262  return;
3263 }
3264 
3265 /*
3266  * IsInTransactionChain
3267  *
3268  * This routine is for statements that need to behave differently inside
3269  * a transaction block than when running as single commands. ANALYZE is
3270  * currently the only example.
3271  *
3272  * isTopLevel: passed down from ProcessUtility to determine whether we are
3273  * inside a function.
3274  */
3275 bool
3276 IsInTransactionChain(bool isTopLevel)
3277 {
3278  /*
3279  * Return true on same conditions that would make PreventTransactionChain
3280  * error out
3281  */
3282  if (IsTransactionBlock())
3283  return true;
3284 
3285  if (IsSubTransaction())
3286  return true;
3287 
3288  if (!isTopLevel)
3289  return true;
3290 
3291  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3292  CurrentTransactionState->blockState != TBLOCK_STARTED)
3293  return true;
3294 
3295  return false;
3296 }
3297 
3298 
3299 /*
3300  * Register or deregister callback functions for start- and end-of-xact
3301  * operations.
3302  *
3303  * These functions are intended for use by dynamically loaded modules.
3304  * For built-in modules we generally just hardwire the appropriate calls
3305  * (mainly because it's easier to control the order that way, where needed).
3306  *
3307  * At transaction end, the callback occurs post-commit or post-abort, so the
3308  * callback functions can only do noncritical cleanup.
3309  */
3310 void
3312 {
3313  XactCallbackItem *item;
3314 
3315  item = (XactCallbackItem *)
3317  item->callback = callback;
3318  item->arg = arg;
3319  item->next = Xact_callbacks;
3320  Xact_callbacks = item;
3321 }
3322 
3323 void
3325 {
3326  XactCallbackItem *item;
3327  XactCallbackItem *prev;
3328 
3329  prev = NULL;
3330  for (item = Xact_callbacks; item; prev = item, item = item->next)
3331  {
3332  if (item->callback == callback && item->arg == arg)
3333  {
3334  if (prev)
3335  prev->next = item->next;
3336  else
3337  Xact_callbacks = item->next;
3338  pfree(item);
3339  break;
3340  }
3341  }
3342 }
3343 
3344 static void
3346 {
3347  XactCallbackItem *item;
3348 
3349  for (item = Xact_callbacks; item; item = item->next)
3350  item->callback(event, item->arg);
3351 }
3352 
3353 
3354 /*
3355  * Register or deregister callback functions for start- and end-of-subxact
3356  * operations.
3357  *
3358  * Pretty much same as above, but for subtransaction events.
3359  *
3360  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3361  * so the callback functions can only do noncritical cleanup. At
3362  * subtransaction start, the callback is called when the subtransaction has
3363  * finished initializing.
3364  */
3365 void
3367 {
3368  SubXactCallbackItem *item;
3369 
3370  item = (SubXactCallbackItem *)
3372  item->callback = callback;
3373  item->arg = arg;
3374  item->next = SubXact_callbacks;
3375  SubXact_callbacks = item;
3376 }
3377 
3378 void
3380 {
3381  SubXactCallbackItem *item;
3382  SubXactCallbackItem *prev;
3383 
3384  prev = NULL;
3385  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3386  {
3387  if (item->callback == callback && item->arg == arg)
3388  {
3389  if (prev)
3390  prev->next = item->next;
3391  else
3392  SubXact_callbacks = item->next;
3393  pfree(item);
3394  break;
3395  }
3396  }
3397 }
3398 
3399 static void
3401  SubTransactionId mySubid,
3402  SubTransactionId parentSubid)
3403 {
3404  SubXactCallbackItem *item;
3405 
3406  for (item = SubXact_callbacks; item; item = item->next)
3407  item->callback(event, mySubid, parentSubid, item->arg);
3408 }
3409 
3410 
3411 /* ----------------------------------------------------------------
3412  * transaction block support
3413  * ----------------------------------------------------------------
3414  */
3415 
3416 /*
3417  * BeginTransactionBlock
3418  * This executes a BEGIN command.
3419  */
3420 void
3422 {
3424 
3425  switch (s->blockState)
3426  {
3427  /*
3428  * We are not inside a transaction block, so allow one to begin.
3429  */
3430  case TBLOCK_STARTED:
3431  s->blockState = TBLOCK_BEGIN;
3432  break;
3433 
3434  /*
3435  * BEGIN converts an implicit transaction block to a regular one.
3436  * (Note that we allow this even if we've already done some
3437  * commands, which is a bit odd but matches historical practice.)
3438  */
3440  s->blockState = TBLOCK_BEGIN;
3441  break;
3442 
3443  /*
3444  * Already a transaction block in progress.
3445  */
3446  case TBLOCK_INPROGRESS:
3448  case TBLOCK_SUBINPROGRESS:
3449  case TBLOCK_ABORT:
3450  case TBLOCK_SUBABORT:
3451  ereport(WARNING,
3452  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3453  errmsg("there is already a transaction in progress")));
3454  break;
3455 
3456  /* These cases are invalid. */
3457  case TBLOCK_DEFAULT:
3458  case TBLOCK_BEGIN:
3459  case TBLOCK_SUBBEGIN:
3460  case TBLOCK_END:
3461  case TBLOCK_SUBRELEASE:
3462  case TBLOCK_SUBCOMMIT:
3463  case TBLOCK_ABORT_END:
3464  case TBLOCK_SUBABORT_END:
3465  case TBLOCK_ABORT_PENDING:
3467  case TBLOCK_SUBRESTART:
3469  case TBLOCK_PREPARE:
3470  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3472  break;
3473  }
3474 }
3475 
3476 /*
3477  * PrepareTransactionBlock
3478  * This executes a PREPARE command.
3479  *
3480  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3481  * happened: TRUE for PREPARE, FALSE for ROLLBACK.
3482  *
3483  * Note that we don't actually do anything here except change blockState.
3484  * The real work will be done in the upcoming PrepareTransaction().
3485  * We do it this way because it's not convenient to change memory context,
3486  * resource owner, etc while executing inside a Portal.
3487  */
3488 bool
3490 {
3491  TransactionState s;
3492  bool result;
3493 
3494  /* Set up to commit the current transaction */
3495  result = EndTransactionBlock();
3496 
3497  /* If successful, change outer tblock state to PREPARE */
3498  if (result)
3499  {
3501 
3502  while (s->parent != NULL)
3503  s = s->parent;
3504 
3505  if (s->blockState == TBLOCK_END)
3506  {
3507  /* Save GID where PrepareTransaction can find it again */
3509 
3511  }
3512  else
3513  {
3514  /*
3515  * ignore case where we are not in a transaction;
3516  * EndTransactionBlock already issued a warning.
3517  */
3520  /* Don't send back a PREPARE result tag... */
3521  result = false;
3522  }
3523  }
3524 
3525  return result;
3526 }
3527 
3528 /*
3529  * EndTransactionBlock
3530  * This executes a COMMIT command.
3531  *
3532  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3533  * happened: TRUE for COMMIT, FALSE for ROLLBACK.
3534  *
3535  * Note that we don't actually do anything here except change blockState.
3536  * The real work will be done in the upcoming CommitTransactionCommand().
3537  * We do it this way because it's not convenient to change memory context,
3538  * resource owner, etc while executing inside a Portal.
3539  */
3540 bool
3542 {
3544  bool result = false;
3545 
3546  switch (s->blockState)
3547  {
3548  /*
3549  * We are in a transaction block, so tell CommitTransactionCommand
3550  * to COMMIT.
3551  */
3552  case TBLOCK_INPROGRESS:
3553  s->blockState = TBLOCK_END;
3554  result = true;
3555  break;
3556 
3557  /*
3558  * In an implicit transaction block, commit, but issue a warning
3559  * because there was no explicit BEGIN before this.
3560  */
3562  ereport(WARNING,
3563  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3564  errmsg("there is no transaction in progress")));
3565  s->blockState = TBLOCK_END;
3566  result = true;
3567  break;
3568 
3569  /*
3570  * We are in a failed transaction block. Tell
3571  * CommitTransactionCommand it's time to exit the block.
3572  */
3573  case TBLOCK_ABORT:
3575  break;
3576 
3577  /*
3578  * We are in a live subtransaction block. Set up to subcommit all
3579  * open subtransactions and then commit the main transaction.
3580  */
3581  case TBLOCK_SUBINPROGRESS:
3582  while (s->parent != NULL)
3583  {
3584  if (s->blockState == TBLOCK_SUBINPROGRESS)
3586  else
3587  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3589  s = s->parent;
3590  }
3591  if (s->blockState == TBLOCK_INPROGRESS)
3592  s->blockState = TBLOCK_END;
3593  else
3594  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3596  result = true;
3597  break;
3598 
3599  /*
3600  * Here we are inside an aborted subtransaction. Treat the COMMIT
3601  * as ROLLBACK: set up to abort everything and exit the main
3602  * transaction.
3603  */
3604  case TBLOCK_SUBABORT:
3605  while (s->parent != NULL)
3606  {
3607  if (s->blockState == TBLOCK_SUBINPROGRESS)
3609  else if (s->blockState == TBLOCK_SUBABORT)
3611  else
3612  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3614  s = s->parent;
3615  }
3616  if (s->blockState == TBLOCK_INPROGRESS)
3618  else if (s->blockState == TBLOCK_ABORT)
3620  else
3621  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3623  break;
3624 
3625  /*
3626  * The user issued COMMIT when not inside a transaction. Issue a
3627  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3628  * CommitTransactionCommand() will then close the transaction and
3629  * put us back into the default state.
3630  */
3631  case TBLOCK_STARTED:
3632  ereport(WARNING,
3633  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3634  errmsg("there is no transaction in progress")));
3635  result = true;
3636  break;
3637 
3638  /*
3639  * The user issued a COMMIT that somehow ran inside a parallel
3640  * worker. We can't cope with that.
3641  */
3643  ereport(FATAL,
3644  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3645  errmsg("cannot commit during a parallel operation")));
3646  break;
3647 
3648  /* These cases are invalid. */
3649  case TBLOCK_DEFAULT:
3650  case TBLOCK_BEGIN:
3651  case TBLOCK_SUBBEGIN:
3652  case TBLOCK_END:
3653  case TBLOCK_SUBRELEASE:
3654  case TBLOCK_SUBCOMMIT:
3655  case TBLOCK_ABORT_END:
3656  case TBLOCK_SUBABORT_END:
3657  case TBLOCK_ABORT_PENDING:
3659  case TBLOCK_SUBRESTART:
3661  case TBLOCK_PREPARE:
3662  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3664  break;
3665  }
3666 
3667  return result;
3668 }
3669 
3670 /*
3671  * UserAbortTransactionBlock
3672  * This executes a ROLLBACK command.
3673  *
3674  * As above, we don't actually do anything here except change blockState.
3675  */
3676 void
3678 {
3680 
3681  switch (s->blockState)
3682  {
3683  /*
3684  * We are inside a transaction block and we got a ROLLBACK command
3685  * from the user, so tell CommitTransactionCommand to abort and
3686  * exit the transaction block.
3687  */
3688  case TBLOCK_INPROGRESS:
3690  break;
3691 
3692  /*
3693  * We are inside a failed transaction block and we got a ROLLBACK
3694  * command from the user. Abort processing is already done, so
3695  * CommitTransactionCommand just has to cleanup and go back to
3696  * idle state.
3697  */
3698  case TBLOCK_ABORT:
3700  break;
3701 
3702  /*
3703  * We are inside a subtransaction. Mark everything up to top
3704  * level as exitable.
3705  */
3706  case TBLOCK_SUBINPROGRESS:
3707  case TBLOCK_SUBABORT:
3708  while (s->parent != NULL)
3709  {
3710  if (s->blockState == TBLOCK_SUBINPROGRESS)
3712  else if (s->blockState == TBLOCK_SUBABORT)
3714  else
3715  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3717  s = s->parent;
3718  }
3719  if (s->blockState == TBLOCK_INPROGRESS)
3721  else if (s->blockState == TBLOCK_ABORT)
3723  else
3724  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3726  break;
3727 
3728  /*
3729  * The user issued ABORT when not inside a transaction. Issue a
3730  * WARNING and go to abort state. The upcoming call to
3731  * CommitTransactionCommand() will then put us back into the
3732  * default state.
3733  *
3734  * We do the same thing with ABORT inside an implicit transaction,
3735  * although in this case we might be rolling back actual database
3736  * state changes. (It's debatable whether we should issue a
3737  * WARNING in this case, but we have done so historically.)
3738  */
3739  case TBLOCK_STARTED:
3741  ereport(WARNING,
3742  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3743  errmsg("there is no transaction in progress")));
3745  break;
3746 
3747  /*
3748  * The user issued an ABORT that somehow ran inside a parallel
3749  * worker. We can't cope with that.
3750  */
3752  ereport(FATAL,
3753  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3754  errmsg("cannot abort during a parallel operation")));
3755  break;
3756 
3757  /* These cases are invalid. */
3758  case TBLOCK_DEFAULT:
3759  case TBLOCK_BEGIN:
3760  case TBLOCK_SUBBEGIN:
3761  case TBLOCK_END:
3762  case TBLOCK_SUBRELEASE:
3763  case TBLOCK_SUBCOMMIT:
3764  case TBLOCK_ABORT_END:
3765  case TBLOCK_SUBABORT_END:
3766  case TBLOCK_ABORT_PENDING:
3768  case TBLOCK_SUBRESTART:
3770  case TBLOCK_PREPARE:
3771  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3773  break;
3774  }
3775 }
3776 
3777 /*
3778  * BeginImplicitTransactionBlock
3779  * Start an implicit transaction block if we're not already in one.
3780  *
3781  * Unlike BeginTransactionBlock, this is called directly from the main loop
3782  * in postgres.c, not within a Portal. So we can just change blockState
3783  * without a lot of ceremony. We do not expect caller to do
3784  * CommitTransactionCommand/StartTransactionCommand.
3785  */
3786 void
3788 {
3790 
3791  /*
3792  * If we are in STARTED state (that is, no transaction block is open),
3793  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3794  * block.
3795  *
3796  * For caller convenience, we consider all other transaction states as
3797  * legal here; otherwise the caller would need its own state check, which
3798  * seems rather pointless.
3799  */
3800  if (s->blockState == TBLOCK_STARTED)
3802 }
3803 
3804 /*
3805  * EndImplicitTransactionBlock
3806  * End an implicit transaction block, if we're in one.
3807  *
3808  * Like EndTransactionBlock, we just make any needed blockState change here.
3809  * The real work will be done in the upcoming CommitTransactionCommand().
3810  */
3811 void
3813 {
3815 
3816  /*
3817  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3818  * allowing CommitTransactionCommand to commit whatever happened during
3819  * the implicit transaction block as though it were a single statement.
3820  *
3821  * For caller convenience, we consider all other transaction states as
3822  * legal here; otherwise the caller would need its own state check, which
3823  * seems rather pointless.
3824  */
3827 }
3828 
3829 /*
3830  * DefineSavepoint
3831  * This executes a SAVEPOINT command.
3832  */
3833 void
3835 {
3837 
3838  /*
3839  * Workers synchronize transaction state at the beginning of each parallel
3840  * operation, so we can't account for new subtransactions after that
3841  * point. (Note that this check will certainly error out if s->blockState
3842  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3843  * below.)
3844  */
3845  if (IsInParallelMode())
3846  ereport(ERROR,
3847  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3848  errmsg("cannot define savepoints during a parallel operation")));
3849 
3850  switch (s->blockState)
3851  {
3852  case TBLOCK_INPROGRESS:
3853  case TBLOCK_SUBINPROGRESS:
3854  /* Normal subtransaction start */
3855  PushTransaction();
3856  s = CurrentTransactionState; /* changed by push */
3857 
3858  /*
3859  * Savepoint names, like the TransactionState block itself, live
3860  * in TopTransactionContext.
3861  */
3862  if (name)
3864  break;
3865 
3866  /*
3867  * We disallow savepoint commands in implicit transaction blocks.
3868  * There would be no great difficulty in allowing them so far as
3869  * this module is concerned, but a savepoint seems inconsistent
3870  * with exec_simple_query's behavior of abandoning the whole query
3871  * string upon error. Also, the point of an implicit transaction
3872  * block (as opposed to a regular one) is to automatically close
3873  * after an error, so it's hard to see how a savepoint would fit
3874  * into that.
3875  *
3876  * The error messages for this are phrased as if there were no
3877  * active transaction block at all, which is historical but
3878  * perhaps could be improved.
3879  */
3881  ereport(ERROR,
3882  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3883  /* translator: %s represents an SQL statement name */
3884  errmsg("%s can only be used in transaction blocks",
3885  "SAVEPOINT")));
3886  break;
3887 
3888  /* These cases are invalid. */
3889  case TBLOCK_DEFAULT:
3890  case TBLOCK_STARTED:
3891  case TBLOCK_BEGIN:
3893  case TBLOCK_SUBBEGIN:
3894  case TBLOCK_END:
3895  case TBLOCK_SUBRELEASE:
3896  case TBLOCK_SUBCOMMIT:
3897  case TBLOCK_ABORT:
3898  case TBLOCK_SUBABORT:
3899  case TBLOCK_ABORT_END:
3900  case TBLOCK_SUBABORT_END:
3901  case TBLOCK_ABORT_PENDING:
3903  case TBLOCK_SUBRESTART:
3905  case TBLOCK_PREPARE:
3906  elog(FATAL, "DefineSavepoint: unexpected state %s",
3908  break;
3909  }
3910 }
3911 
3912 /*
3913  * ReleaseSavepoint
3914  * This executes a RELEASE command.
3915  *
3916  * As above, we don't actually do anything here except change blockState.
3917  */
3918 void
3920 {
3922  TransactionState target,
3923  xact;
3924  ListCell *cell;
3925  char *name = NULL;
3926 
3927  /*
3928  * Workers synchronize transaction state at the beginning of each parallel
3929  * operation, so we can't account for transaction state change after that
3930  * point. (Note that this check will certainly error out if s->blockState
3931  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3932  * below.)
3933  */
3934  if (IsInParallelMode())
3935  ereport(ERROR,
3936  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3937  errmsg("cannot release savepoints during a parallel operation")));
3938 
3939  switch (s->blockState)
3940  {
3941  /*
3942  * We can't release a savepoint if there is no savepoint defined.
3943  */
3944  case TBLOCK_INPROGRESS:
3945  ereport(ERROR,
3946  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3947  errmsg("no such savepoint")));
3948  break;
3949 
3951  /* See comment about implicit transactions in DefineSavepoint */
3952  ereport(ERROR,
3953  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3954  /* translator: %s represents an SQL statement name */
3955  errmsg("%s can only be used in transaction blocks",
3956  "RELEASE SAVEPOINT")));
3957  break;
3958 
3959  /*
3960  * We are in a non-aborted subtransaction. This is the only valid
3961  * case.
3962  */
3963  case TBLOCK_SUBINPROGRESS:
3964  break;
3965 
3966  /* These cases are invalid. */
3967  case TBLOCK_DEFAULT:
3968  case TBLOCK_STARTED:
3969  case TBLOCK_BEGIN:
3971  case TBLOCK_SUBBEGIN:
3972  case TBLOCK_END:
3973  case TBLOCK_SUBRELEASE:
3974  case TBLOCK_SUBCOMMIT:
3975  case TBLOCK_ABORT:
3976  case TBLOCK_SUBABORT:
3977  case TBLOCK_ABORT_END:
3978  case TBLOCK_SUBABORT_END:
3979  case TBLOCK_ABORT_PENDING:
3981  case TBLOCK_SUBRESTART:
3983  case TBLOCK_PREPARE:
3984  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3986  break;
3987  }
3988 
3989  foreach(cell, options)
3990  {
3991  DefElem *elem = lfirst(cell);
3992 
3993  if (strcmp(elem->defname, "savepoint_name") == 0)
3994  name = strVal(elem->arg);
3995  }
3996 
3997  Assert(PointerIsValid(name));
3998 
3999  for (target = s; PointerIsValid(target); target = target->parent)
4000  {
4001  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4002  break;
4003  }
4004 
4005  if (!PointerIsValid(target))
4006  ereport(ERROR,
4007  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4008  errmsg("no such savepoint")));
4009 
4010  /* disallow crossing savepoint level boundaries */
4011  if (target->savepointLevel != s->savepointLevel)
4012  ereport(ERROR,
4013  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4014  errmsg("no such savepoint")));
4015 
4016  /*
4017  * Mark "commit pending" all subtransactions up to the target
4018  * subtransaction. The actual commits will happen when control gets to
4019  * CommitTransactionCommand.
4020  */
4021  xact = CurrentTransactionState;
4022  for (;;)
4023  {
4025  xact->blockState = TBLOCK_SUBRELEASE;
4026  if (xact == target)
4027  break;
4028  xact = xact->parent;
4029  Assert(PointerIsValid(xact));
4030  }
4031 }
4032 
4033 /*
4034  * RollbackToSavepoint
4035  * This executes a ROLLBACK TO <savepoint> command.
4036  *
4037  * As above, we don't actually do anything here except change blockState.
4038  */
4039 void
4041 {
4043  TransactionState target,
4044  xact;
4045  ListCell *cell;
4046  char *name = NULL;
4047 
4048  /*
4049  * Workers synchronize transaction state at the beginning of each parallel
4050  * operation, so we can't account for transaction state change after that
4051  * point. (Note that this check will certainly error out if s->blockState
4052  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4053  * below.)
4054  */
4055  if (IsInParallelMode())
4056  ereport(ERROR,
4057  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4058  errmsg("cannot rollback to savepoints during a parallel operation")));
4059 
4060  switch (s->blockState)
4061  {
4062  /*
4063  * We can't rollback to a savepoint if there is no savepoint
4064  * defined.
4065  */
4066  case TBLOCK_INPROGRESS:
4067  case TBLOCK_ABORT:
4068  ereport(ERROR,
4069  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4070  errmsg("no such savepoint")));
4071  break;
4072 
4074  /* See comment about implicit transactions in DefineSavepoint */
4075  ereport(ERROR,
4076  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4077  /* translator: %s represents an SQL statement name */
4078  errmsg("%s can only be used in transaction blocks",
4079  "ROLLBACK TO SAVEPOINT")));
4080  break;
4081 
4082  /*
4083  * There is at least one savepoint, so proceed.
4084  */
4085  case TBLOCK_SUBINPROGRESS:
4086  case TBLOCK_SUBABORT:
4087  break;
4088 
4089  /* These cases are invalid. */
4090  case TBLOCK_DEFAULT:
4091  case TBLOCK_STARTED:
4092  case TBLOCK_BEGIN:
4094  case TBLOCK_SUBBEGIN:
4095  case TBLOCK_END:
4096  case TBLOCK_SUBRELEASE:
4097  case TBLOCK_SUBCOMMIT:
4098  case TBLOCK_ABORT_END:
4099  case TBLOCK_SUBABORT_END:
4100  case TBLOCK_ABORT_PENDING:
4102  case TBLOCK_SUBRESTART:
4104  case TBLOCK_PREPARE:
4105  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4107  break;
4108  }
4109 
4110  foreach(cell, options)
4111  {
4112  DefElem *elem = lfirst(cell);
4113 
4114  if (strcmp(elem->defname, "savepoint_name") == 0)
4115  name = strVal(elem->arg);
4116  }
4117 
4118  Assert(PointerIsValid(name));
4119 
4120  for (target = s; PointerIsValid(target); target = target->parent)
4121  {
4122  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4123  break;
4124  }
4125 
4126  if (!PointerIsValid(target))
4127  ereport(ERROR,
4128  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4129  errmsg("no such savepoint")));
4130 
4131  /* disallow crossing savepoint level boundaries */
4132  if (target->savepointLevel != s->savepointLevel)
4133  ereport(ERROR,
4134  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4135  errmsg("no such savepoint")));
4136 
4137  /*
4138  * Mark "abort pending" all subtransactions up to the target
4139  * subtransaction. The actual aborts will happen when control gets to
4140  * CommitTransactionCommand.
4141  */
4142  xact = CurrentTransactionState;
4143  for (;;)
4144  {
4145  if (xact == target)
4146  break;
4147  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4149  else if (xact->blockState == TBLOCK_SUBABORT)
4151  else
4152  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4154  xact = xact->parent;
4155  Assert(PointerIsValid(xact));
4156  }
4157 
4158  /* And mark the target as "restart pending" */
4159  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4160  xact->blockState = TBLOCK_SUBRESTART;
4161  else if (xact->blockState == TBLOCK_SUBABORT)
4163  else
4164  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4166 }
4167 
4168 /*
4169  * BeginInternalSubTransaction
4170  * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4171  * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_END, and TBLOCK_PREPARE states,
4172  * and therefore it can safely be used in functions that might be called
4173  * when not inside a BEGIN block or when running deferred triggers at
4174  * COMMIT/PREPARE time. Also, it automatically does
4175  * CommitTransactionCommand/StartTransactionCommand instead of expecting
4176  * the caller to do it.
4177  */
4178 void
4180 {
4182 
4183  /*
4184  * Workers synchronize transaction state at the beginning of each parallel
4185  * operation, so we can't account for new subtransactions after that
4186  * point. We might be able to make an exception for the type of
4187  * subtransaction established by this function, which is typically used in
4188  * contexts where we're going to release or roll back the subtransaction
4189  * before proceeding further, so that no enduring change to the
4190  * transaction state occurs. For now, however, we prohibit this case along
4191  * with all the others.
4192  */
4193  if (IsInParallelMode())
4194  ereport(ERROR,
4195  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4196  errmsg("cannot start subtransactions during a parallel operation")));
4197 
4198  switch (s->blockState)
4199  {
4200  case TBLOCK_STARTED:
4201  case TBLOCK_INPROGRESS:
4203  case TBLOCK_END:
4204  case TBLOCK_PREPARE:
4205  case TBLOCK_SUBINPROGRESS:
4206  /* Normal subtransaction start */
4207  PushTransaction();
4208  s = CurrentTransactionState; /* changed by push */
4209 
4210  /*
4211  * Savepoint names, like the TransactionState block itself, live
4212  * in TopTransactionContext.
4213  */
4214  if (name)
4216  break;
4217 
4218  /* These cases are invalid. */
4219  case TBLOCK_DEFAULT:
4220  case TBLOCK_BEGIN:
4222  case TBLOCK_SUBBEGIN:
4223  case TBLOCK_SUBRELEASE:
4224  case TBLOCK_SUBCOMMIT:
4225  case TBLOCK_ABORT:
4226  case TBLOCK_SUBABORT:
4227  case TBLOCK_ABORT_END:
4228  case TBLOCK_SUBABORT_END:
4229  case TBLOCK_ABORT_PENDING:
4231  case TBLOCK_SUBRESTART:
4233  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4235  break;
4236  }
4237 
4240 }
4241 
4242 /*
4243  * ReleaseCurrentSubTransaction
4244  *
4245  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4246  * savepoint name (if any).
4247  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4248  */
4249 void
4251 {
4253 
4254  /*
4255  * Workers synchronize transaction state at the beginning of each parallel
4256  * operation, so we can't account for commit of subtransactions after that
4257  * point. This should not happen anyway. Code calling this would
4258  * typically have called BeginInternalSubTransaction() first, failing
4259  * there.
4260  */
4261  if (IsInParallelMode())
4262  ereport(ERROR,
4263  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4264  errmsg("cannot commit subtransactions during a parallel operation")));
4265 
4266  if (s->blockState != TBLOCK_SUBINPROGRESS)
4267  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4269  Assert(s->state == TRANS_INPROGRESS);
4272  s = CurrentTransactionState; /* changed by pop */
4273  Assert(s->state == TRANS_INPROGRESS);
4274 }
4275 
4276 /*
4277  * RollbackAndReleaseCurrentSubTransaction
4278  *
4279  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4280  * of its savepoint name (if any).
4281  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4282  */
4283 void
4285 {
4287 
4288  /*
4289  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4290  * during parallel operations. That's because we may be in the master,
4291  * recovering from an error thrown while we were in parallel mode. We
4292  * won't reach here in a worker, because BeginInternalSubTransaction()
4293  * will have failed.
4294  */
4295 
4296  switch (s->blockState)
4297  {
4298  /* Must be in a subtransaction */
4299  case TBLOCK_SUBINPROGRESS:
4300  case TBLOCK_SUBABORT:
4301  break;
4302 
4303  /* These cases are invalid. */
4304  case TBLOCK_DEFAULT:
4305  case TBLOCK_STARTED:
4306  case TBLOCK_BEGIN:
4309  case TBLOCK_SUBBEGIN:
4310  case TBLOCK_INPROGRESS:
4311  case TBLOCK_END:
4312  case TBLOCK_SUBRELEASE:
4313  case TBLOCK_SUBCOMMIT:
4314  case TBLOCK_ABORT:
4315  case TBLOCK_ABORT_END:
4316  case TBLOCK_SUBABORT_END:
4317  case TBLOCK_ABORT_PENDING:
4319  case TBLOCK_SUBRESTART:
4321  case TBLOCK_PREPARE:
4322  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4324  break;
4325  }
4326 
4327  /*
4328  * Abort the current subtransaction, if needed.
4329  */
4330  if (s->blockState == TBLOCK_SUBINPROGRESS)
4332 
4333  /* And clean it up, too */
4335 
4336  s = CurrentTransactionState; /* changed by pop */
4338  s->blockState == TBLOCK_INPROGRESS ||
4340  s->blockState == TBLOCK_STARTED);
4341 }
4342 
4343 /*
4344  * AbortOutOfAnyTransaction
4345  *
4346  * This routine is provided for error recovery purposes. It aborts any
4347  * active transaction or transaction block, leaving the system in a known
4348  * idle state.
4349  */
4350 void
4352 {
4354 
4355  /* Ensure we're not running in a doomed memory context */
4356  AtAbort_Memory();
4357 
4358  /*
4359  * Get out of any transaction or nested transaction
4360  */
4361  do
4362  {
4363  switch (s->blockState)
4364  {
4365  case TBLOCK_DEFAULT:
4366  if (s->state == TRANS_DEFAULT)
4367  {
4368  /* Not in a transaction, do nothing */
4369  }
4370  else
4371  {
4372  /*
4373  * We can get here after an error during transaction start
4374  * (state will be TRANS_START). Need to clean up the
4375  * incompletely started transaction. First, adjust the
4376  * low-level state to suppress warning message from
4377  * AbortTransaction.
4378  */
4379  if (s->state == TRANS_START)
4380  s->state = TRANS_INPROGRESS;
4381  AbortTransaction();
4383  }
4384  break;
4385  case TBLOCK_STARTED:
4386  case TBLOCK_BEGIN:
4387  case TBLOCK_INPROGRESS:
4390  case TBLOCK_END:
4391  case TBLOCK_ABORT_PENDING:
4392  case TBLOCK_PREPARE:
4393  /* In a transaction, so clean up */
4394  AbortTransaction();
4397  break;
4398  case TBLOCK_ABORT:
4399  case TBLOCK_ABORT_END:
4400 
4401  /*
4402  * AbortTransaction is already done, still need Cleanup.
4403  * However, if we failed partway through running ROLLBACK,
4404  * there will be an active portal running that command, which
4405  * we need to shut down before doing CleanupTransaction.
4406  */
4407  AtAbort_Portals();
4410  break;
4411 
4412  /*
4413  * In a subtransaction, so clean it up and abort parent too
4414  */
4415  case TBLOCK_SUBBEGIN:
4416  case TBLOCK_SUBINPROGRESS:
4417  case TBLOCK_SUBRELEASE:
4418  case TBLOCK_SUBCOMMIT:
4420  case TBLOCK_SUBRESTART:
4423  s = CurrentTransactionState; /* changed by pop */
4424  break;
4425 
4426  case TBLOCK_SUBABORT:
4427  case TBLOCK_SUBABORT_END:
4429  /* As above, but AbortSubTransaction already done */
4430  if (s->curTransactionOwner)
4431  {
4432  /* As in TBLOCK_ABORT, might have a live portal to zap */
4437  }
4439  s = CurrentTransactionState; /* changed by pop */
4440  break;
4441  }
4442  } while (s->blockState != TBLOCK_DEFAULT);
4443 
4444  /* Should be out of all subxacts now */
4445  Assert(s->parent == NULL);
4446 
4447  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4448  AtCleanup_Memory();
4449 }
4450 
4451 /*
4452  * IsTransactionBlock --- are we within a transaction block?
4453  */
4454 bool
4456 {
4458 
4460  return false;
4461 
4462  return true;
4463 }
4464 
4465 /*
4466  * IsTransactionOrTransactionBlock --- are we within either a transaction
4467  * or a transaction block? (The backend is only really "idle" when this
4468  * returns false.)
4469  *
4470  * This should match up with IsTransactionBlock and IsTransactionState.
4471  */
4472 bool
4474 {
4476 
4477  if (s->blockState == TBLOCK_DEFAULT)
4478  return false;
4479 
4480  return true;
4481 }
4482 
4483 /*
4484  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
4485  */
4486 char
4488 {
4490 
4491  switch (s->blockState)
4492  {
4493  case TBLOCK_DEFAULT:
4494  case TBLOCK_STARTED:
4495  return 'I'; /* idle --- not in transaction */
4496  case TBLOCK_BEGIN:
4497  case TBLOCK_SUBBEGIN:
4498  case TBLOCK_INPROGRESS:
4501  case TBLOCK_SUBINPROGRESS:
4502  case TBLOCK_END:
4503  case TBLOCK_SUBRELEASE:
4504  case TBLOCK_SUBCOMMIT:
4505  case TBLOCK_PREPARE:
4506  return 'T'; /* in transaction */
4507  case TBLOCK_ABORT:
4508  case TBLOCK_SUBABORT:
4509  case TBLOCK_ABORT_END:
4510  case TBLOCK_SUBABORT_END:
4511  case TBLOCK_ABORT_PENDING:
4513  case TBLOCK_SUBRESTART:
4515  return 'E'; /* in failed transaction */
4516  }
4517 
4518  /* should never get here */
4519  elog(FATAL, "invalid transaction block state: %s",
4521  return 0; /* keep compiler quiet */
4522 }
4523 
4524 /*
4525  * IsSubTransaction
4526  */
4527 bool
4529 {
4531 
4532  if (s->nestingLevel >= 2)
4533  return true;
4534 
4535  return false;
4536 }
4537 
4538 /*
4539  * StartSubTransaction
4540  *
4541  * If you're wondering why this is separate from PushTransaction: it's because
4542  * we can't conveniently do this stuff right inside DefineSavepoint. The
4543  * SAVEPOINT utility command will be executed inside a Portal, and if we
4544  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4545  * the Portal will undo those settings. So we make DefineSavepoint just
4546  * push a dummy transaction block, and when control returns to the main
4547  * idle loop, CommitTransactionCommand will be called, and we'll come here
4548  * to finish starting the subtransaction.
4549  */
4550 static void
4552 {
4554 
4555  if (s->state != TRANS_DEFAULT)
4556  elog(WARNING, "StartSubTransaction while in %s state",
4558 
4559  s->state = TRANS_START;
4560 
4561  /*
4562  * Initialize subsystems for new subtransaction
4563  *
4564  * must initialize resource-management stuff first
4565  */
4570 
4571  s->state = TRANS_INPROGRESS;
4572 
4573  /*
4574  * Call start-of-subxact callbacks
4575  */
4577  s->parent->subTransactionId);
4578 
4579  ShowTransactionState("StartSubTransaction");
4580 }
4581 
4582 /*
4583  * CommitSubTransaction
4584  *
4585  * The caller has to make sure to always reassign CurrentTransactionState
4586  * if it has a local pointer to it after calling this function.
4587  */
4588 static void
4590 {
4592 
4593  ShowTransactionState("CommitSubTransaction");
4594 
4595  if (s->state != TRANS_INPROGRESS)
4596  elog(WARNING, "CommitSubTransaction while in %s state",
4598 
4599  /* Pre-commit processing goes here */
4600 
4602  s->parent->subTransactionId);
4603 
4604  /* If in parallel mode, clean up workers and exit parallel mode. */
4605  if (IsInParallelMode())
4606  {
4608  s->parallelModeLevel = 0;
4609  }
4610 
4611  /* Do the actual "commit", such as it is */
4612  s->state = TRANS_COMMIT;
4613 
4614  /* Must CCI to ensure commands of subtransaction are seen as done */
4616 
4617  /*
4618  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4619  * perform that step, if required, as part of the atomic update of the
4620  * whole transaction tree at top level commit or abort.
4621  */
4622 
4623  /* Post-commit cleanup */
4626  AfterTriggerEndSubXact(true);
4631  s->parent->subTransactionId);
4633 
4635  s->parent->subTransactionId);
4636 
4639  true, false);
4641  s->parent->subTransactionId);
4642  AtEOSubXact_Inval(true);
4643  AtSubCommit_smgr();
4644 
4645  /*
4646  * The only lock we actually release here is the subtransaction XID lock.
4647  */
4651 
4652  /*
4653  * Other locks should get transferred to their parent resource owner.
4654  */
4657  true, false);
4660  true, false);
4661 
4662  AtEOXact_GUC(true, s->gucNestLevel);
4665  s->parent->subTransactionId);
4667  s->parent->subTransactionId);
4669  s->parent->subTransactionId);
4671  AtEOSubXact_PgStat(true, s->nestingLevel);
4673 
4674  /*
4675  * We need to restore the upper transaction's read-only state, in case the
4676  * upper is read-write while the child is read-only; GUC will incorrectly
4677  * think it should leave the child state in place.
4678  */
4680 
4684  s->curTransactionOwner = NULL;
4685 
4687 
4688  s->state = TRANS_DEFAULT;
4689 
4690  PopTransaction();
4691 }
4692 
4693 /*
4694  * AbortSubTransaction
4695  */
4696 static void
4698 {
4700 
4701  /* Prevent cancel/die interrupt while cleaning up */
4702  HOLD_INTERRUPTS();
4703 
4704  /* Make sure we have a valid memory context and resource owner */
4707 
4708  /*
4709  * Release any LW locks we might be holding as quickly as possible.
4710  * (Regular locks, however, must be held till we finish aborting.)
4711  * Releasing LW locks is critical since we might try to grab them again
4712  * while cleaning up!
4713  *
4714  * FIXME This may be incorrect --- Are there some locks we should keep?
4715  * Buffer locks, for example? I don't think so but I'm not sure.
4716  */
4717  LWLockReleaseAll();
4718 
4721  AbortBufferIO();
4722  UnlockBuffers();
4723 
4724  /* Reset WAL record construction state */
4726 
4727  /*
4728  * Also clean up any open wait for lock, since the lock manager will choke
4729  * if we try to wait for another lock before doing this.
4730  */
4731  LockErrorCleanup();
4732 
4733  /*
4734  * If any timeout events are still active, make sure the timeout interrupt
4735  * is scheduled. This covers possible loss of a timeout interrupt due to
4736  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4737  * We delay this till after LockErrorCleanup so that we don't uselessly
4738  * reschedule lock or deadlock check timeouts.
4739  */
4741 
4742  /*
4743  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4744  * handler. We do this fairly early in the sequence so that the timeout
4745  * infrastructure will be functional if needed while aborting.
4746  */
4748 
4749  /*
4750  * check the current transaction state
4751  */
4752  ShowTransactionState("AbortSubTransaction");
4753 
4754  if (s->state != TRANS_INPROGRESS)
4755  elog(WARNING, "AbortSubTransaction while in %s state",
4757 
4758  s->state = TRANS_ABORT;
4759 
4760  /*
4761  * Reset user ID which might have been changed transiently. (See notes in
4762  * AbortTransaction.)
4763  */
4765 
4766  /* Exit from parallel mode, if necessary. */
4767  if (IsInParallelMode())
4768  {
4770  s->parallelModeLevel = 0;
4771  }
4772 
4773  /*
4774  * We can skip all this stuff if the subxact failed before creating a
4775  * ResourceOwner...
4776  */
4777  if (s->curTransactionOwner)
4778  {
4779  AfterTriggerEndSubXact(false);
4785  s->parent->subTransactionId);
4787 
4788  /* Advertise the fact that we aborted in pg_xact. */
4789  (void) RecordTransactionAbort(true);
4790 
4791  /* Post-abort cleanup */
4794 
4796  s->parent->subTransactionId);
4797 
4800  false, false);
4802  s->parent->subTransactionId);
4803  AtEOSubXact_Inval(false);
4806  false, false);
4809  false, false);
4810  AtSubAbort_smgr();
4811 
4812  AtEOXact_GUC(false, s->gucNestLevel);
4813  AtEOSubXact_SPI(false, s->subTransactionId);
4815  s->parent->subTransactionId);
4817  s->parent->subTransactionId);
4819  s->parent->subTransactionId);
4821  AtEOSubXact_PgStat(false, s->nestingLevel);
4823  }
4824 
4825  /*
4826  * Restore the upper transaction's read-only state, too. This should be
4827  * redundant with GUC's cleanup but we may as well do it for consistency
4828  * with the commit case.
4829  */
4831 
4833 }
4834 
4835 /*
4836  * CleanupSubTransaction
4837  *
4838  * The caller has to make sure to always reassign CurrentTransactionState
4839  * if it has a local pointer to it after calling this function.
4840  */
4841 static void
4843 {
4845 
4846  ShowTransactionState("CleanupSubTransaction");
4847 
4848  if (s->state != TRANS_ABORT)
4849  elog(WARNING, "CleanupSubTransaction while in %s state",
4851 
4853 
4856  if (s->curTransactionOwner)
4858  s->curTransactionOwner = NULL;
4859 
4861 
4862  s->state = TRANS_DEFAULT;
4863 
4864  PopTransaction();
4865 }
4866 
4867 /*
4868  * PushTransaction
4869  * Create transaction state stack entry for a subtransaction
4870  *
4871  * The caller has to make sure to always reassign CurrentTransactionState
4872  * if it has a local pointer to it after calling this function.
4873  */
4874 static void
4876 {
4878  TransactionState s;
4879 
4880  /*
4881  * We keep subtransaction state nodes in TopTransactionContext.
4882  */
4883  s = (TransactionState)
4885  sizeof(TransactionStateData));
4886 
4887  /*
4888  * Assign a subtransaction ID, watching out for counter wraparound.
4889  */
4892  {
4894  pfree(s);
4895  ereport(ERROR,
4896  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4897  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4898  }
4899 
4900  /*
4901  * We can now stack a minimally valid subtransaction without fear of
4902  * failure.
4903  */
4904  s->transactionId = InvalidTransactionId; /* until assigned */
4906  s->parent = p;
4907  s->nestingLevel = p->nestingLevel + 1;
4910  s->state = TRANS_DEFAULT;
4914  s->parallelModeLevel = 0;
4915 
4916  CurrentTransactionState = s;
4917 
4918  /*
4919  * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4920  * with the subtransaction from here on out; in particular they should not
4921  * assume that it necessarily has a transaction context, resource owner,
4922  * or XID.
4923  */
4924 }
4925 
4926 /*
4927  * PopTransaction
4928  * Pop back to parent transaction state
4929  *
4930  * The caller has to make sure to always reassign CurrentTransactionState
4931  * if it has a local pointer to it after calling this function.
4932  */
4933 static void
4935 {
4937 
4938  if (s->state != TRANS_DEFAULT)
4939  elog(WARNING, "PopTransaction while in %s state",
4941 
4942  if (s->parent == NULL)
4943  elog(FATAL, "PopTransaction with no parent");
4944 
4945  CurrentTransactionState = s->parent;
4946 
4947  /* Let's just make sure CurTransactionContext is good */
4950 
4951  /* Ditto for ResourceOwner links */
4954 
4955  /* Free the old child structure */
4956  if (s->name)
4957  pfree(s->name);
4958  pfree(s);
4959 }
4960 
4961 /*
4962  * EstimateTransactionStateSpace
4963  * Estimate the amount of space that will be needed by
4964  * SerializeTransactionState. It would be OK to overestimate slightly,
4965  * but it's simple for us to work out the precise value, so we do.
4966  */
4967 Size
4969 {
4970  TransactionState s;
4971  Size nxids = 6; /* iso level, deferrable, top & current XID,
4972  * command counter, XID count */
4973 
4974  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4975  {
4977  nxids = add_size(nxids, 1);
4978  nxids = add_size(nxids, s->nChildXids);
4979  }
4980 
4981  nxids = add_size(nxids, nParallelCurrentXids);
4982  return mul_size(nxids, sizeof(TransactionId));
4983 }
4984 
4985 /*
4986  * SerializeTransactionState
4987  * Write out relevant details of our transaction state that will be
4988  * needed by a parallel worker.
4989  *
4990  * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
4991  * associated with this transaction. The first eight bytes of the result
4992  * contain XactDeferrable and XactIsoLevel; the next twelve bytes contain the
4993  * XID of the top-level transaction, the XID of the current transaction
4994  * (or, in each case, InvalidTransactionId if none), and the current command
4995  * counter. After that, the next 4 bytes contain a count of how many
4996  * additional XIDs follow; this is followed by all of those XIDs one after
4997  * another. We emit the XIDs in sorted order for the convenience of the
4998  * receiving process.
4999  */
5000 void
5001 SerializeTransactionState(Size maxsize, char *start_address)
5002 {
5003  TransactionState s;
5004  Size nxids = 0;
5005  Size i = 0;
5006  Size c = 0;
5007  TransactionId *workspace;
5008  TransactionId *result = (TransactionId *) start_address;
5009 
5010  result[c++] = (TransactionId) XactIsoLevel;
5011  result[c++] = (TransactionId) XactDeferrable;
5012  result[c++] = XactTopTransactionId;
5013  result[c++] = CurrentTransactionState->transactionId;
5014  result[c++] = (TransactionId) currentCommandId;
5015  Assert(maxsize >= c * sizeof(TransactionId));
5016 
5017  /*
5018  * If we're running in a parallel worker and launching a parallel worker
5019  * of our own, we can just pass along the information that was passed to
5020  * us.
5021  */
5022  if (nParallelCurrentXids > 0)
5023  {
5024  result[c++] = nParallelCurrentXids;
5025  Assert(maxsize >= (nParallelCurrentXids + c) * sizeof(TransactionId));
5026  memcpy(&result[c], ParallelCurrentXids,
5028  return;
5029  }
5030 
5031  /*
5032  * OK, we need to generate a sorted list of XIDs that our workers should
5033  * view as current. First, figure out how many there are.
5034  */
5035  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5036  {
5038  nxids = add_size(nxids, 1);
5039  nxids = add_size(nxids, s->nChildXids);
5040  }
5041  Assert((c + 1 + nxids) * sizeof(TransactionId) <= maxsize);
5042 
5043  /* Copy them to our scratch space. */
5044  workspace = palloc(nxids * sizeof(TransactionId));
5045  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5046  {
5048  workspace[i++] = s->transactionId;
5049  memcpy(&workspace[i], s->childXids,
5050  s->nChildXids * sizeof(TransactionId));
5051  i += s->nChildXids;
5052  }
5053  Assert(i == nxids);
5054 
5055  /* Sort them. */
5056  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5057 
5058  /* Copy data into output area. */
5059  result[c++] = (TransactionId) nxids;
5060  memcpy(&result[c], workspace, nxids * sizeof(TransactionId));
5061 }
5062 
5063 /*
5064  * StartParallelWorkerTransaction
5065  * Start a parallel worker transaction, restoring the relevant
5066  * transaction state serialized by SerializeTransactionState.
5067  */
5068 void
5070 {
5071  TransactionId *tstate = (TransactionId *) tstatespace;
5072 
5073  Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
5074  StartTransaction();
5075 
5076  XactIsoLevel = (int) tstate[0];
5077  XactDeferrable = (bool) tstate[1];
5078  XactTopTransactionId = tstate[2];
5079  CurrentTransactionState->transactionId = tstate[3];
5080  currentCommandId = tstate[4];
5081  nParallelCurrentXids = (int) tstate[5];
5082  ParallelCurrentXids = &tstate[6];
5083 
5084  CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
5085 }
5086 
5087 /*
5088  * EndParallelWorkerTransaction
5089  * End a parallel worker transaction.
5090  */
5091 void
5093 {
5094  Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
5096  CurrentTransactionState->blockState = TBLOCK_DEFAULT;
5097 }
5098 
5099 /*
5100  * ShowTransactionState
5101  * Debug support
5102  */
5103 static void
5104 ShowTransactionState(const char *str)
5105 {
5106  /* skip work if message will definitely not be printed */
5108  ShowTransactionStateRec(str, CurrentTransactionState);
5109 }
5110 
5111 /*
5112  * ShowTransactionStateRec
5113  * Recursive subroutine for ShowTransactionState
5114  */
5115 static void
5117 {
5119 
5120  initStringInfo(&buf);
5121 
5122  if (s->nChildXids > 0)
5123  {
5124  int i;
5125 
5126  appendStringInfo(&buf, ", children: %u", s->childXids[0]);
5127  for (i = 1; i < s->nChildXids; i++)
5128  appendStringInfo(&buf, " %u", s->childXids[i]);
5129  }
5130 
5131  if (s->parent)
5133 
5134  /* use ereport to suppress computation if msg will not be printed */
5135  ereport(DEBUG5,
5136  (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
5137  str, s->nestingLevel,
5138  PointerIsValid(s->name) ? s->name : "unnamed",
5141  (unsigned int) s->transactionId,
5142  (unsigned int) s->subTransactionId,
5143  (unsigned int) currentCommandId,
5144  currentCommandIdUsed ? " (used)" : "",
5145  buf.data)));
5146 
5147  pfree(buf.data);
5148 }
5149 
5150 /*
5151  * BlockStateAsString
5152  * Debug support
5153  */
5154 static const char *
5156 {
5157  switch (blockState)
5158  {
5159  case TBLOCK_DEFAULT:
5160  return "DEFAULT";
5161  case TBLOCK_STARTED:
5162  return "STARTED";
5163  case TBLOCK_BEGIN:
5164  return "BEGIN";
5165  case TBLOCK_INPROGRESS:
5166  return "INPROGRESS";
5168  return "IMPLICIT_INPROGRESS";
5170  return "PARALLEL_INPROGRESS";
5171  case TBLOCK_END:
5172  return "END";
5173  case TBLOCK_ABORT:
5174  return "ABORT";
5175  case TBLOCK_ABORT_END:
5176  return "ABORT END";
5177  case TBLOCK_ABORT_PENDING:
5178  return "ABORT PEND";
5179  case TBLOCK_PREPARE:
5180  return "PREPARE";
5181  case TBLOCK_SUBBEGIN:
5182  return "SUB BEGIN";
5183  case TBLOCK_SUBINPROGRESS:
5184  return "SUB INPROGRS";
5185  case TBLOCK_SUBRELEASE:
5186  return "SUB RELEASE";
5187  case TBLOCK_SUBCOMMIT:
5188  return "SUB COMMIT";
5189  case TBLOCK_SUBABORT:
5190  return "SUB ABORT";
5191  case TBLOCK_SUBABORT_END:
5192  return "SUB ABORT END";
5194  return "SUB ABRT PEND";
5195  case TBLOCK_SUBRESTART:
5196  return "SUB RESTART";
5198  return "SUB AB RESTRT";
5199  }
5200  return "UNRECOGNIZED";
5201 }
5202 
5203 /*
5204  * TransStateAsString
5205  * Debug support
5206  */
5207 static const char *
5209 {
5210  switch (state)
5211  {
5212  case TRANS_DEFAULT:
5213  return "DEFAULT";
5214  case TRANS_START:
5215  return "START";
5216  case TRANS_INPROGRESS:
5217  return "INPROGR";
5218  case TRANS_COMMIT:
5219  return "COMMIT";
5220  case TRANS_ABORT:
5221  return "ABORT";
5222  case TRANS_PREPARE:
5223  return "PREPARE";
5224  }
5225  return "UNRECOGNIZED";
5226 }
5227 
5228 /*
5229  * xactGetCommittedChildren
5230  *
5231  * Gets the list of committed children of the current transaction. The return
5232  * value is the number of child transactions. *ptr is set to point to an
5233  * array of TransactionIds. The array is allocated in TopTransactionContext;
5234  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
5235  * If there are no subxacts, *ptr is set to NULL.
5236  */
5237 int
5239 {
5241 
5242  if (s->nChildXids == 0)
5243  *ptr = NULL;
5244  else
5245  *ptr = s->childXids;
5246 
5247  return s->nChildXids;
5248 }
5249 
5250 /*
5251  * XLOG support routines
5252  */
5253 
5254 
5255 /*
5256  * Log the commit record for a plain or twophase transaction commit.
5257  *
5258  * A 2pc commit will be emitted when twophase_xid is valid, a plain one
5259  * otherwise.
5260  */
5261 XLogRecPtr
5263  int nsubxacts, TransactionId *subxacts,
5264  int nrels, RelFileNode *rels,
5265  int nmsgs, SharedInvalidationMessage *msgs,
5266  bool relcacheInval, bool forceSync,
5267  int xactflags, TransactionId twophase_xid)
5268 {
5269  xl_xact_commit xlrec;
5270  xl_xact_xinfo xl_xinfo;
5271  xl_xact_dbinfo xl_dbinfo;
5272  xl_xact_subxacts xl_subxacts;
5273  xl_xact_relfilenodes xl_relfilenodes;
5274  xl_xact_invals xl_invals;
5275  xl_xact_twophase xl_twophase;
5276  xl_xact_origin xl_origin;
5277 
5278  uint8 info;
5279 
5280  Assert(CritSectionCount > 0);
5281 
5282  xl_xinfo.xinfo = 0;
5283 
5284  /* decide between a plain and 2pc commit */
5285  if (!TransactionIdIsValid(twophase_xid))
5286  info = XLOG_XACT_COMMIT;
5287  else
5289 
5290  /* First figure out and collect all the information needed */
5291 
5292  xlrec.xact_time = commit_time;
5293 
5294  if (relcacheInval)
5296  if (forceSyncCommit)
5298  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5299  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5300 
5301  /*
5302  * Check if the caller would like to ask standbys for immediate feedback
5303  * once this commit is applied.
5304  */
5307 
5308  /*
5309  * Relcache invalidations requires information about the current database
5310  * and so does logical decoding.
5311  */
5312  if (nmsgs > 0 || XLogLogicalInfoActive())
5313  {
5314  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5315  xl_dbinfo.dbId = MyDatabaseId;
5316  xl_dbinfo.tsId = MyDatabaseTableSpace;
5317  }
5318 
5319  if (nsubxacts > 0)
5320  {
5321  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5322  xl_subxacts.nsubxacts = nsubxacts;
5323  }
5324 
5325  if (nrels > 0)
5326  {
5327  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5328  xl_relfilenodes.nrels = nrels;
5329  }
5330 
5331  if (nmsgs > 0)
5332  {
5333  xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5334  xl_invals.nmsgs = nmsgs;
5335  }
5336 
5337  if (TransactionIdIsValid(twophase_xid))
5338  {
5339  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5340  xl_twophase.xid = twophase_xid;
5341  }
5342 
5343  /* dump transaction origin information */
5345  {
5346  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5347 
5350  }
5351 
5352  if (xl_xinfo.xinfo != 0)
5353  info |= XLOG_XACT_HAS_INFO;
5354 
5355  /* Then include all the collected data into the commit record. */
5356 
5357  XLogBeginInsert();
5358 
5359  XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
5360 
5361  if (xl_xinfo.xinfo != 0)
5362  XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
5363 
5364  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5365  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5366 
5367  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5368  {
5369  XLogRegisterData((char *) (&xl_subxacts),
5371  XLogRegisterData((char *) subxacts,
5372  nsubxacts * sizeof(TransactionId));
5373  }
5374 
5375  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5376  {
5377  XLogRegisterData((char *) (&xl_relfilenodes),
5379  XLogRegisterData((char *) rels,
5380  nrels * sizeof(RelFileNode));
5381  }
5382 
5383  if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5384  {
5385  XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
5386  XLogRegisterData((char *) msgs,
5387  nmsgs * sizeof(SharedInvalidationMessage));
5388  }
5389 
5390  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5391  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5392 
5393  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5394  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5395 
5396  /* we allow filtering by xacts */
5398 
5399  return XLogInsert(RM_XACT_ID, info);
5400 }
5401 
5402 /*
5403  * Log the commit record for a plain or twophase transaction abort.
5404  *
5405  * A 2pc abort will be emitted when twophase_xid is valid, a plain one
5406  * otherwise.
5407  */
5408 XLogRecPtr
5410  int nsubxacts, TransactionId *subxacts,
5411  int nrels, RelFileNode *rels,
5412  int xactflags, TransactionId twophase_xid)
5413 {
5414  xl_xact_abort xlrec;
5415  xl_xact_xinfo xl_xinfo;
5416  xl_xact_subxacts xl_subxacts;
5417  xl_xact_relfilenodes xl_relfilenodes;
5418  xl_xact_twophase xl_twophase;
5419 
5420  uint8 info;
5421 
5422  Assert(CritSectionCount > 0);
5423 
5424  xl_xinfo.xinfo = 0;
5425 
5426  /* decide between a plain and 2pc abort */
5427  if (!TransactionIdIsValid(twophase_xid))
5428  info = XLOG_XACT_ABORT;
5429  else
5430  info = XLOG_XACT_ABORT_PREPARED;
5431 
5432 
5433  /* First figure out and collect all the information needed */
5434 
5435  xlrec.xact_time = abort_time;
5436 
5437  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5438  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5439 
5440  if (nsubxacts > 0)
5441  {
5442  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5443  xl_subxacts.nsubxacts = nsubxacts;
5444  }
5445 
5446  if (nrels > 0)
5447  {
5448  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5449  xl_relfilenodes.nrels = nrels;
5450  }
5451 
5452  if (TransactionIdIsValid(twophase_xid))
5453  {
5454  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5455  xl_twophase.xid = twophase_xid;
5456  }
5457 
5458  if (xl_xinfo.xinfo != 0)
5459  info |= XLOG_XACT_HAS_INFO;
5460 
5461  /* Then include all the collected data into the abort record. */
5462 
5463  XLogBeginInsert();
5464 
5465  XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
5466 
5467  if (xl_xinfo.xinfo != 0)
5468  XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
5469 
5470  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5471  {
5472  XLogRegisterData((char *) (&xl_subxacts),
5474  XLogRegisterData((char *) subxacts,
5475  nsubxacts * sizeof(TransactionId));
5476  }
5477 
5478  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5479  {
5480  XLogRegisterData((char *) (&xl_relfilenodes),
5482  XLogRegisterData((char *) rels,
5483  nrels * sizeof(RelFileNode));
5484  }
5485 
5486  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5487  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5488 
5489  return XLogInsert(RM_XACT_ID, info);
5490 }
5491 
5492 /*
5493  * Before 9.0 this was a fairly short function, but now it performs many
5494  * actions for which the order of execution is critical.
5495  */
5496 static void
5498  TransactionId xid,
5499  XLogRecPtr lsn,
5500  RepOriginId origin_id)
5501 {
5502  TransactionId max_xid;
5503  int i;
5504  TimestampTz commit_time;
5505 
5507 
5508  max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
5509 
5510  /*
5511  * Make sure nextXid is beyond any XID mentioned in the record.
5512  *
5513  * We don't expect anyone else to modify nextXid, hence we don't need to
5514  * hold a lock while checking this. We still acquire the lock to modify
5515  * it, though.
5516  */
5517  if (TransactionIdFollowsOrEquals(max_xid,
5519  {
5520  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5521  ShmemVariableCache->nextXid = max_xid;
5523  LWLockRelease(XidGenLock);
5524  }
5525 
5526  Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
5527  (origin_id == InvalidRepOriginId));
5528 
5529  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5530  commit_time = parsed->origin_timestamp;
5531  else
5532  commit_time = parsed->xact_time;
5533 
5534  /* Set the transaction commit timestamp and metadata */
5535  TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
5536  commit_time, origin_id, false);
5537 
5539  {
5540  /*
5541  * Mark the transaction committed in pg_xact.
5542  */
5543  TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
5544  }
5545  else
5546  {
5547  /*
5548  * If a transaction completion record arrives that has as-yet
5549  * unobserved subtransactions then this will not have been fully
5550  * handled by the call to RecordKnownAssignedTransactionIds() in the
5551  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5552  * cover that case. This is confusing and it is easy to think this
5553  * call is irrelevant, which has happened three times in development
5554  * already. Leave it in.
5555  */
5557 
5558  /*
5559  * Mark the transaction committed in pg_xact. We use async commit
5560  * protocol during recovery to provide information on database
5561  * consistency for when users try to set hint bits. It is important
5562  * that we do not set hint bits until the minRecoveryPoint is past
5563  * this commit record. This ensures that if we crash we don't see hint
5564  * bits set on changes made by transactions that haven't yet
5565  * recovered. It's unlikely but it's good to be safe.
5566  */
5568  xid, parsed->nsubxacts, parsed->subxacts, lsn);
5569 
5570  /*
5571  * We must mark clog before we update the ProcArray.
5572  */
5574  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5575 
5576  /*
5577  * Send any cache invalidations attached to the commit. We must
5578  * maintain the same order of invalidation then release locks as
5579  * occurs in CommitTransaction().
5580  */
5582  parsed->msgs, parsed->nmsgs,
5584  parsed->dbId, parsed->tsId);
5585 
5586  /*
5587  * Release locks, if any. We do this for both two phase and normal one
5588  * phase transactions. In effect we are ignoring the prepare phase and
5589  * just going straight to lock release. At commit we release all locks
5590  * via their top-level xid only, so no need to provide subxact list,
5591  * which will save time when replaying commits.
5592  */
5593  if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
5594  StandbyReleaseLockTree(xid, 0, NULL);
5595  }
5596 
5597  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5598  {
5599  /* recover apply progress */
5600  replorigin_advance(origin_id, parsed->origin_lsn, lsn,
5601  false /* backward */ , false /* WAL */ );
5602  }
5603 
5604  /* Make sure files supposed to be dropped are dropped */
5605  if (parsed->nrels > 0)
5606  {
5607  /*
5608  * First update minimum recovery point to cover this WAL record. Once
5609  * a relation is deleted, there's no going back. The buffer manager
5610  * enforces the WAL-first rule for normal updates to relation files,
5611  * so that the minimum recovery point is always updated before the
5612  * corresponding change in the data file is flushed to disk, but we
5613  * have to do the same here since we're bypassing the buffer manager.
5614  *
5615  * Doing this before deleting the files means that if a deletion fails
5616  * for some reason, you cannot start up the system even after restart,
5617  * until you fix the underlying situation so that the deletion will
5618  * succeed. Alternatively, we could update the minimum recovery point
5619  * after deletion, but that would leave a small window where the
5620  * WAL-first rule would be violated.
5621  */
5622  XLogFlush(lsn);
5623 
5624  for (i = 0; i < parsed->nrels; i++)
5625  {
5626  SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId);
5627  ForkNumber fork;
5628 
5629  for (fork = 0; fork <= MAX_FORKNUM; fork++)
5630  XLogDropRelation(parsed->xnodes[i], fork);
5631  smgrdounlink(srel, true);
5632  smgrclose(srel);
5633  }
5634  }
5635 
5636  /*
5637  * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
5638  * in normal operation. For example, in CREATE DATABASE, we copy all files
5639  * from the template database, and then commit the transaction. If we
5640  * crash after all the files have been copied but before the commit, you
5641  * have files in the data directory without an entry in pg_database. To
5642  * minimize the window for that, we use ForceSyncCommit() to rush the
5643  * commit record to disk as quick as possible. We have the same window
5644  * during recovery, and forcing an XLogFlush() (which updates
5645  * minRecoveryPoint during recovery) helps to reduce that problem window,
5646  * for any user that requested ForceSyncCommit().
5647  */
5648  if (XactCompletionForceSyncCommit(parsed->xinfo))
5649  XLogFlush(lsn);
5650 
5651  /*
5652  * If asked by the primary (because someone is waiting for a synchronous
5653  * commit = remote_apply), we will need to ask walreceiver to send a reply
5654  * immediately.
5655  */
5656  if (XactCompletionApplyFeedback(parsed->xinfo))
5658 }
5659 
5660 /*
5661  * Be careful with the order of execution, as with xact_redo_commit().
5662  * The two functions are similar but differ in key places.
5663  *
5664  * Note also that an abort can be for a subtransaction and its children,
5665  * not just for a top level abort. That means we have to consider
5666  * topxid != xid, whereas in commit we would find topxid == xid always
5667  * because subtransaction commit is never WAL logged.
5668  */
5669 static void
5671 {
5672  int i;
5673  TransactionId max_xid;
5674 
5676 
5677  /*
5678  * Make sure nextXid is beyond any XID mentioned in the record.
5679  *
5680  * We don't expect anyone else to modify nextXid, hence we don't need to
5681  * hold a lock while checking this. We still acquire the lock to modify
5682  * it, though.
5683  */
5684  max_xid = TransactionIdLatest(xid,
5685  parsed->nsubxacts,
5686  parsed->subxacts);
5687 
5688  if (TransactionIdFollowsOrEquals(max_xid,
5690  {
5691  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5692  ShmemVariableCache->nextXid = max_xid;
5694  LWLockRelease(XidGenLock);
5695  }
5696 
5698  {
5699  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5700  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5701  }
5702  else
5703  {
5704  /*
5705  * If a transaction completion record arrives that has as-yet
5706  * unobserved subtransactions then this will not have been fully
5707  * handled by the call to RecordKnownAssignedTransactionIds() in the
5708  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5709  * cover that case. This is confusing and it is easy to think this
5710  * call is irrelevant, which has happened three times in development
5711  * already. Leave it in.
5712  */
5714 
5715  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5716  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5717 
5718  /*
5719  * We must update the ProcArray after we have marked clog.
5720  */
5722  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5723 
5724  /*
5725  * There are no flat files that need updating, nor invalidation
5726  * messages to send or undo.
5727  */
5728</