PostgreSQL Source Code  git master
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;
579 
581 
582  CurrentResourceOwner = currentOwner;
583 
584  /*
585  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
586  * top-level transaction we issue a WAL record for the assignment. We
587  * include the top-level xid and all the subxids that have not yet been
588  * reported using XLOG_XACT_ASSIGNMENT records.
589  *
590  * This is required to limit the amount of shared memory required in a hot
591  * standby server to keep track of in-progress XIDs. See notes for
592  * RecordKnownAssignedTransactionIds().
593  *
594  * We don't keep track of the immediate parent of each subxid, only the
595  * top-level transaction that each subxact belongs to. This is correct in
596  * recovery only because aborted subtransactions are separately WAL
597  * logged.
598  *
599  * This is correct even for the case where several levels above us didn't
600  * have an xid assigned as we recursed up to them beforehand.
601  */
602  if (isSubXact && XLogStandbyInfoActive())
603  {
605  nUnreportedXids++;
606 
607  /*
608  * ensure this test matches similar one in
609  * RecoverPreparedTransactions()
610  */
612  log_unknown_top)
613  {
614  xl_xact_assignment xlrec;
615 
616  /*
617  * xtop is always set by now because we recurse up transaction
618  * stack to the highest unassigned xid and then come back down
619  */
620  xlrec.xtop = GetTopTransactionId();
622  xlrec.nsubxacts = nUnreportedXids;
623 
624  XLogBeginInsert();
625  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
627  nUnreportedXids * sizeof(TransactionId));
628 
629  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
630 
631  nUnreportedXids = 0;
632  /* mark top, not current xact as having been logged */
633  TopTransactionStateData.didLogXid = true;
634  }
635  }
636 }
637 
638 /*
639  * GetCurrentSubTransactionId
640  */
643 {
645 
646  return s->subTransactionId;
647 }
648 
649 /*
650  * SubTransactionIsActive
651  *
652  * Test if the specified subxact ID is still active. Note caller is
653  * responsible for checking whether this ID is relevant to the current xact.
654  */
655 bool
657 {
659 
660  for (s = CurrentTransactionState; s != NULL; s = s->parent)
661  {
662  if (s->state == TRANS_ABORT)
663  continue;
664  if (s->subTransactionId == subxid)
665  return true;
666  }
667  return false;
668 }
669 
670 
671 /*
672  * GetCurrentCommandId
673  *
674  * "used" must be true if the caller intends to use the command ID to mark
675  * inserted/updated/deleted tuples. false means the ID is being fetched
676  * for read-only purposes (ie, as a snapshot validity cutoff). See
677  * CommandCounterIncrement() for discussion.
678  */
679 CommandId
681 {
682  /* this is global to a transaction, not subtransaction-local */
683  if (used)
684  {
685  /*
686  * Forbid setting currentCommandIdUsed in parallel mode, because we
687  * have no provision for communicating this back to the master. We
688  * could relax this restriction when currentCommandIdUsed was already
689  * true at the start of the parallel operation.
690  */
691  Assert(CurrentTransactionState->parallelModeLevel == 0);
692  currentCommandIdUsed = true;
693  }
694  return currentCommandId;
695 }
696 
697 /*
698  * GetCurrentTransactionStartTimestamp
699  */
702 {
703  return xactStartTimestamp;
704 }
705 
706 /*
707  * GetCurrentStatementStartTimestamp
708  */
711 {
712  return stmtStartTimestamp;
713 }
714 
715 /*
716  * GetCurrentTransactionStopTimestamp
717  *
718  * We return current time if the transaction stop time hasn't been set
719  * (which can happen if we decide we don't need to log an XLOG record).
720  */
723 {
724  if (xactStopTimestamp != 0)
725  return xactStopTimestamp;
726  return GetCurrentTimestamp();
727 }
728 
729 /*
730  * SetCurrentStatementStartTimestamp
731  */
732 void
734 {
736 }
737 
738 /*
739  * SetCurrentTransactionStopTimestamp
740  */
741 static inline void
743 {
745 }
746 
747 /*
748  * GetCurrentTransactionNestLevel
749  *
750  * Note: this will return zero when not inside any transaction, one when
751  * inside a top-level transaction, etc.
752  */
753 int
755 {
757 
758  return s->nestingLevel;
759 }
760 
761 
762 /*
763  * TransactionIdIsCurrentTransactionId
764  */
765 bool
767 {
769 
770  /*
771  * We always say that BootstrapTransactionId is "not my transaction ID"
772  * even when it is (ie, during bootstrap). Along with the fact that
773  * transam.c always treats BootstrapTransactionId as already committed,
774  * this causes the tqual.c routines to see all tuples as committed, which
775  * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
776  * it never updates or deletes them, so all tuples can be presumed good
777  * immediately.)
778  *
779  * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
780  * not my transaction ID, so we can just return "false" immediately for
781  * any non-normal XID.
782  */
783  if (!TransactionIdIsNormal(xid))
784  return false;
785 
786  /*
787  * In parallel workers, the XIDs we must consider as current are stored in
788  * ParallelCurrentXids rather than the transaction-state stack. Note that
789  * the XIDs in this array are sorted numerically rather than according to
790  * transactionIdPrecedes order.
791  */
792  if (nParallelCurrentXids > 0)
793  {
794  int low,
795  high;
796 
797  low = 0;
798  high = nParallelCurrentXids - 1;
799  while (low <= high)
800  {
801  int middle;
802  TransactionId probe;
803 
804  middle = low + (high - low) / 2;
805  probe = ParallelCurrentXids[middle];
806  if (probe == xid)
807  return true;
808  else if (probe < xid)
809  low = middle + 1;
810  else
811  high = middle - 1;
812  }
813  return false;
814  }
815 
816  /*
817  * We will return true for the Xid of the current subtransaction, any of
818  * its subcommitted children, any of its parents, or any of their
819  * previously subcommitted children. However, a transaction being aborted
820  * is no longer "current", even though it may still have an entry on the
821  * state stack.
822  */
823  for (s = CurrentTransactionState; s != NULL; s = s->parent)
824  {
825  int low,
826  high;
827 
828  if (s->state == TRANS_ABORT)
829  continue;
831  continue; /* it can't have any child XIDs either */
832  if (TransactionIdEquals(xid, s->transactionId))
833  return true;
834  /* As the childXids array is ordered, we can use binary search */
835  low = 0;
836  high = s->nChildXids - 1;
837  while (low <= high)
838  {
839  int middle;
840  TransactionId probe;
841 
842  middle = low + (high - low) / 2;
843  probe = s->childXids[middle];
844  if (TransactionIdEquals(probe, xid))
845  return true;
846  else if (TransactionIdPrecedes(probe, xid))
847  low = middle + 1;
848  else
849  high = middle - 1;
850  }
851  }
852 
853  return false;
854 }
855 
856 /*
857  * TransactionStartedDuringRecovery
858  *
859  * Returns true if the current transaction started while recovery was still
860  * in progress. Recovery might have ended since so RecoveryInProgress() might
861  * return false already.
862  */
863 bool
865 {
866  return CurrentTransactionState->startedInRecovery;
867 }
868 
869 /*
870  * EnterParallelMode
871  */
872 void
874 {
876 
877  Assert(s->parallelModeLevel >= 0);
878 
879  ++s->parallelModeLevel;
880 }
881 
882 /*
883  * ExitParallelMode
884  */
885 void
887 {
889 
890  Assert(s->parallelModeLevel > 0);
892 
893  --s->parallelModeLevel;
894 }
895 
896 /*
897  * IsInParallelMode
898  *
899  * Are we in a parallel operation, as either the master or a worker? Check
900  * this to prohibit operations that change backend-local state expected to
901  * match across all workers. Mere caches usually don't require such a
902  * restriction. State modified in a strict push/pop fashion, such as the
903  * active snapshot stack, is often fine.
904  */
905 bool
907 {
908  return CurrentTransactionState->parallelModeLevel != 0;
909 }
910 
911 /*
912  * CommandCounterIncrement
913  */
914 void
916 {
917  /*
918  * If the current value of the command counter hasn't been "used" to mark
919  * tuples, we need not increment it, since there's no need to distinguish
920  * a read-only command from others. This helps postpone command counter
921  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
922  */
924  {
925  /*
926  * Workers synchronize transaction state at the beginning of each
927  * parallel operation, so we can't account for new commands after that
928  * point.
929  */
931  elog(ERROR, "cannot start commands during a parallel operation");
932 
933  currentCommandId += 1;
935  {
936  currentCommandId -= 1;
937  ereport(ERROR,
938  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
939  errmsg("cannot have more than 2^32-2 commands in a transaction")));
940  }
941  currentCommandIdUsed = false;
942 
943  /* Propagate new command ID into static snapshots */
945 
946  /*
947  * Make any catalog changes done by the just-completed command visible
948  * in the local syscache. We obviously don't need to do this after a
949  * read-only command. (But see hacks in inval.c to make real sure we
950  * don't think a command that queued inval messages was read-only.)
951  */
953  }
954 }
955 
956 /*
957  * ForceSyncCommit
958  *
959  * Interface routine to allow commands to force a synchronous commit of the
960  * current top-level transaction
961  */
962 void
964 {
965  forceSyncCommit = true;
966 }
967 
968 
969 /* ----------------------------------------------------------------
970  * StartTransaction stuff
971  * ----------------------------------------------------------------
972  */
973 
974 /*
975  * AtStart_Cache
976  */
977 static void
979 {
981 }
982 
983 /*
984  * AtStart_Memory
985  */
986 static void
988 {
990 
991  /*
992  * If this is the first time through, create a private context for
993  * AbortTransaction to work in. By reserving some space now, we can
994  * insulate AbortTransaction from out-of-memory scenarios. Like
995  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
996  * size, so that space will be reserved immediately.
997  */
998  if (TransactionAbortContext == NULL)
999  TransactionAbortContext =
1001  "TransactionAbortContext",
1002  32 * 1024,
1003  32 * 1024,
1004  32 * 1024);
1005 
1006  /*
1007  * We shouldn't have a transaction context already.
1008  */
1009  Assert(TopTransactionContext == NULL);
1010 
1011  /*
1012  * Create a toplevel context for the transaction.
1013  */
1016  "TopTransactionContext",
1018 
1019  /*
1020  * In a top-level transaction, CurTransactionContext is the same as
1021  * TopTransactionContext.
1022  */
1025 
1026  /* Make the CurTransactionContext active. */
1028 }
1029 
1030 /*
1031  * AtStart_ResourceOwner
1032  */
1033 static void
1035 {
1037 
1038  /*
1039  * We shouldn't have a transaction resource owner already.
1040  */
1042 
1043  /*
1044  * Create a toplevel resource owner for the transaction.
1045  */
1046  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1047 
1051 }
1052 
1053 /* ----------------------------------------------------------------
1054  * StartSubTransaction stuff
1055  * ----------------------------------------------------------------
1056  */
1057 
1058 /*
1059  * AtSubStart_Memory
1060  */
1061 static void
1063 {
1065 
1066  Assert(CurTransactionContext != NULL);
1067 
1068  /*
1069  * Create a CurTransactionContext, which will be used to hold data that
1070  * survives subtransaction commit but disappears on subtransaction abort.
1071  * We make it a child of the immediate parent's CurTransactionContext.
1072  */
1074  "CurTransactionContext",
1077 
1078  /* Make the CurTransactionContext active. */
1080 }
1081 
1082 /*
1083  * AtSubStart_ResourceOwner
1084  */
1085 static void
1087 {
1089 
1090  Assert(s->parent != NULL);
1091 
1092  /*
1093  * Create a resource owner for the subtransaction. We make it a child of
1094  * the immediate parent's resource owner.
1095  */
1096  s->curTransactionOwner =
1098  "SubTransaction");
1099 
1102 }
1103 
1104 /* ----------------------------------------------------------------
1105  * CommitTransaction stuff
1106  * ----------------------------------------------------------------
1107  */
1108 
1109 /*
1110  * RecordTransactionCommit
1111  *
1112  * Returns latest XID among xact and its children, or InvalidTransactionId
1113  * if the xact has no XID. (We compute that here just because it's easier.)
1114  *
1115  * If you change this function, see RecordTransactionCommitPrepared also.
1116  */
1117 static TransactionId
1119 {
1121  bool markXidCommitted = TransactionIdIsValid(xid);
1122  TransactionId latestXid = InvalidTransactionId;
1123  int nrels;
1124  RelFileNode *rels;
1125  int nchildren;
1126  TransactionId *children;
1127  int nmsgs = 0;
1128  SharedInvalidationMessage *invalMessages = NULL;
1129  bool RelcacheInitFileInval = false;
1130  bool wrote_xlog;
1131 
1132  /* Get data needed for commit record */
1133  nrels = smgrGetPendingDeletes(true, &rels);
1134  nchildren = xactGetCommittedChildren(&children);
1135  if (XLogStandbyInfoActive())
1136  nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1137  &RelcacheInitFileInval);
1138  wrote_xlog = (XactLastRecEnd != 0);
1139 
1140  /*
1141  * If we haven't been assigned an XID yet, we neither can, nor do we want
1142  * to write a COMMIT record.
1143  */
1144  if (!markXidCommitted)
1145  {
1146  /*
1147  * We expect that every smgrscheduleunlink is followed by a catalog
1148  * update, and hence XID assignment, so we shouldn't get here with any
1149  * pending deletes. Use a real test not just an Assert to check this,
1150  * since it's a bit fragile.
1151  */
1152  if (nrels != 0)
1153  elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1154 
1155  /* Can't have child XIDs either; AssignTransactionId enforces this */
1156  Assert(nchildren == 0);
1157 
1158  /*
1159  * Transactions without an assigned xid can contain invalidation
1160  * messages (e.g. explicit relcache invalidations or catcache
1161  * invalidations for inplace updates); standbys need to process those.
1162  * We can't emit a commit record without an xid, and we don't want to
1163  * force assigning an xid, because that'd be problematic for e.g.
1164  * vacuum. Hence we emit a bespoke record for the invalidations. We
1165  * don't want to use that in case a commit record is emitted, so they
1166  * happen synchronously with commits (besides not wanting to emit more
1167  * WAL recoreds).
1168  */
1169  if (nmsgs != 0)
1170  {
1171  LogStandbyInvalidations(nmsgs, invalMessages,
1172  RelcacheInitFileInval);
1173  wrote_xlog = true; /* not strictly necessary */
1174  }
1175 
1176  /*
1177  * If we didn't create XLOG entries, we're done here; otherwise we
1178  * should trigger flushing those entries the same as a commit record
1179  * would. This will primarily happen for HOT pruning and the like; we
1180  * want these to be flushed to disk in due time.
1181  */
1182  if (!wrote_xlog)
1183  goto cleanup;
1184  }
1185  else
1186  {
1187  bool replorigin;
1188 
1189  /*
1190  * Are we using the replication origins feature? Or, in other words,
1191  * are we replaying remote actions?
1192  */
1193  replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1195 
1196  /*
1197  * Begin commit critical section and insert the commit XLOG record.
1198  */
1199  /* Tell bufmgr and smgr to prepare for commit */
1200  BufmgrCommit();
1201 
1202  /*
1203  * Mark ourselves as within our "commit critical section". This
1204  * forces any concurrent checkpoint to wait until we've updated
1205  * pg_xact. Without this, it is possible for the checkpoint to set
1206  * REDO after the XLOG record but fail to flush the pg_xact update to
1207  * disk, leading to loss of the transaction commit if the system
1208  * crashes a little later.
1209  *
1210  * Note: we could, but don't bother to, set this flag in
1211  * RecordTransactionAbort. That's because loss of a transaction abort
1212  * is noncritical; the presumption would be that it aborted, anyway.
1213  *
1214  * It's safe to change the delayChkpt flag of our own backend without
1215  * holding the ProcArrayLock, since we're the only one modifying it.
1216  * This makes checkpoint's determination of which xacts are delayChkpt
1217  * a bit fuzzy, but it doesn't matter.
1218  */
1220  MyPgXact->delayChkpt = true;
1221 
1223 
1225  nchildren, children, nrels, rels,
1226  nmsgs, invalMessages,
1227  RelcacheInitFileInval, forceSyncCommit,
1228  MyXactFlags,
1229  InvalidTransactionId /* plain commit */ );
1230 
1231  if (replorigin)
1232  /* Move LSNs forward for this replication origin */
1234  XactLastRecEnd);
1235 
1236  /*
1237  * Record commit timestamp. The value comes from plain commit
1238  * timestamp if there's no replication origin; otherwise, the
1239  * timestamp was already set in replorigin_session_origin_timestamp by
1240  * replication.
1241  *
1242  * We don't need to WAL-log anything here, as the commit record
1243  * written above already contains the data.
1244  */
1245 
1246  if (!replorigin || replorigin_session_origin_timestamp == 0)
1248 
1249  TransactionTreeSetCommitTsData(xid, nchildren, children,
1251  replorigin_session_origin, false);
1252  }
1253 
1254  /*
1255  * Check if we want to commit asynchronously. We can allow the XLOG flush
1256  * to happen asynchronously if synchronous_commit=off, or if the current
1257  * transaction has not performed any WAL-logged operation or didn't assign
1258  * an xid. The transaction can end up not writing any WAL, even if it has
1259  * an xid, if it only wrote to temporary and/or unlogged tables. It can
1260  * end up having written WAL without an xid if it did HOT pruning. In
1261  * case of a crash, the loss of such a transaction will be irrelevant;
1262  * temp tables will be lost anyway, unlogged tables will be truncated and
1263  * HOT pruning will be done again later. (Given the foregoing, you might
1264  * think that it would be unnecessary to emit the XLOG record at all in
1265  * this case, but we don't currently try to do that. It would certainly
1266  * cause problems at least in Hot Standby mode, where the
1267  * KnownAssignedXids machinery requires tracking every XID assignment. It
1268  * might be OK to skip it only when wal_level < replica, but for now we
1269  * don't.)
1270  *
1271  * However, if we're doing cleanup of any non-temp rels or committing any
1272  * command that wanted to force sync commit, then we must flush XLOG
1273  * immediately. (We must not allow asynchronous commit if there are any
1274  * non-temp tables to be deleted, because we might delete the files before
1275  * the COMMIT record is flushed to disk. We do allow asynchronous commit
1276  * if all to-be-deleted tables are temporary though, since they are lost
1277  * anyway if we crash.)
1278  */
1279  if ((wrote_xlog && markXidCommitted &&
1281  forceSyncCommit || nrels > 0)
1282  {
1284 
1285  /*
1286  * Now we may update the CLOG, if we wrote a COMMIT record above
1287  */
1288  if (markXidCommitted)
1289  TransactionIdCommitTree(xid, nchildren, children);
1290  }
1291  else
1292  {
1293  /*
1294  * Asynchronous commit case:
1295  *
1296  * This enables possible committed transaction loss in the case of a
1297  * postmaster crash because WAL buffers are left unwritten. Ideally we
1298  * could issue the WAL write without the fsync, but some
1299  * wal_sync_methods do not allow separate write/fsync.
1300  *
1301  * Report the latest async commit LSN, so that the WAL writer knows to
1302  * flush this commit.
1303  */
1305 
1306  /*
1307  * We must not immediately update the CLOG, since we didn't flush the
1308  * XLOG. Instead, we store the LSN up to which the XLOG must be
1309  * flushed before the CLOG may be updated.
1310  */
1311  if (markXidCommitted)
1312  TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1313  }
1314 
1315  /*
1316  * If we entered a commit critical section, leave it now, and let
1317  * checkpoints proceed.
1318  */
1319  if (markXidCommitted)
1320  {
1321  MyPgXact->delayChkpt = false;
1322  END_CRIT_SECTION();
1323  }
1324 
1325  /* Compute latestXid while we have the child XIDs handy */
1326  latestXid = TransactionIdLatest(xid, nchildren, children);
1327 
1328  /*
1329  * Wait for synchronous replication, if required. Similar to the decision
1330  * above about using committing asynchronously we only want to wait if
1331  * this backend assigned an xid and wrote WAL. No need to wait if an xid
1332  * was assigned due to temporary/unlogged tables or due to HOT pruning.
1333  *
1334  * Note that at this stage we have marked clog, but still show as running
1335  * in the procarray and continue to hold locks.
1336  */
1337  if (wrote_xlog && markXidCommitted)
1339 
1340  /* remember end of last commit record */
1342 
1343  /* Reset XactLastRecEnd until the next transaction writes something */
1344  XactLastRecEnd = 0;
1345 cleanup:
1346  /* Clean up local data */
1347  if (rels)
1348  pfree(rels);
1349 
1350  return latestXid;
1351 }
1352 
1353 
1354 /*
1355  * AtCCI_LocalCache
1356  */
1357 static void
1359 {
1360  /*
1361  * Make any pending relation map changes visible. We must do this before
1362  * processing local sinval messages, so that the map changes will get
1363  * reflected into the relcache when relcache invals are processed.
1364  */
1366 
1367  /*
1368  * Make catalog changes visible to me for the next command.
1369  */
1371 }
1372 
1373 /*
1374  * AtCommit_Memory
1375  */
1376 static void
1378 {
1379  /*
1380  * Now that we're "out" of a transaction, have the system allocate things
1381  * in the top memory context instead of per-transaction contexts.
1382  */
1384 
1385  /*
1386  * Release all transaction-local memory.
1387  */
1388  Assert(TopTransactionContext != NULL);
1390  TopTransactionContext = NULL;
1391  CurTransactionContext = NULL;
1392  CurrentTransactionState->curTransactionContext = NULL;
1393 }
1394 
1395 /* ----------------------------------------------------------------
1396  * CommitSubTransaction stuff
1397  * ----------------------------------------------------------------
1398  */
1399 
1400 /*
1401  * AtSubCommit_Memory
1402  */
1403 static void
1405 {
1407 
1408  Assert(s->parent != NULL);
1409 
1410  /* Return to parent transaction level's memory context. */
1413 
1414  /*
1415  * Ordinarily we cannot throw away the child's CurTransactionContext,
1416  * since the data it contains will be needed at upper commit. However, if
1417  * there isn't actually anything in it, we can throw it away. This avoids
1418  * a small memory leak in the common case of "trivial" subxacts.
1419  */
1421  {
1423  s->curTransactionContext = NULL;
1424  }
1425 }
1426 
1427 /*
1428  * AtSubCommit_childXids
1429  *
1430  * Pass my own XID and my child XIDs up to my parent as committed children.
1431  */
1432 static void
1434 {
1436  int new_nChildXids;
1437 
1438  Assert(s->parent != NULL);
1439 
1440  /*
1441  * The parent childXids array will need to hold my XID and all my
1442  * childXids, in addition to the XIDs already there.
1443  */
1444  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1445 
1446  /* Allocate or enlarge the parent array if necessary */
1447  if (s->parent->maxChildXids < new_nChildXids)
1448  {
1449  int new_maxChildXids;
1450  TransactionId *new_childXids;
1451 
1452  /*
1453  * Make it 2x what's needed right now, to avoid having to enlarge it
1454  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1455  * is what ensures that we don't need to worry about integer overflow
1456  * here or in the calculation of new_nChildXids.)
1457  */
1458  new_maxChildXids = Min(new_nChildXids * 2,
1459  (int) (MaxAllocSize / sizeof(TransactionId)));
1460 
1461  if (new_maxChildXids < new_nChildXids)
1462  ereport(ERROR,
1463  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1464  errmsg("maximum number of committed subtransactions (%d) exceeded",
1465  (int) (MaxAllocSize / sizeof(TransactionId)))));
1466 
1467  /*
1468  * We keep the child-XID arrays in TopTransactionContext; this avoids
1469  * setting up child-transaction contexts for what might be just a few
1470  * bytes of grandchild XIDs.
1471  */
1472  if (s->parent->childXids == NULL)
1473  new_childXids =
1475  new_maxChildXids * sizeof(TransactionId));
1476  else
1477  new_childXids = repalloc(s->parent->childXids,
1478  new_maxChildXids * sizeof(TransactionId));
1479 
1480  s->parent->childXids = new_childXids;
1481  s->parent->maxChildXids = new_maxChildXids;
1482  }
1483 
1484  /*
1485  * Copy all my XIDs to parent's array.
1486  *
1487  * Note: We rely on the fact that the XID of a child always follows that
1488  * of its parent. By copying the XID of this subtransaction before the
1489  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1490  * all XIDs already in the array belong to subtransactions started and
1491  * subcommitted before us, so their XIDs must precede ours.
1492  */
1494 
1495  if (s->nChildXids > 0)
1496  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1497  s->childXids,
1498  s->nChildXids * sizeof(TransactionId));
1499 
1500  s->parent->nChildXids = new_nChildXids;
1501 
1502  /* Release child's array to avoid leakage */
1503  if (s->childXids != NULL)
1504  pfree(s->childXids);
1505  /* We must reset these to avoid double-free if fail later in commit */
1506  s->childXids = NULL;
1507  s->nChildXids = 0;
1508  s->maxChildXids = 0;
1509 }
1510 
1511 /* ----------------------------------------------------------------
1512  * AbortTransaction stuff
1513  * ----------------------------------------------------------------
1514  */
1515 
1516 /*
1517  * RecordTransactionAbort
1518  *
1519  * Returns latest XID among xact and its children, or InvalidTransactionId
1520  * if the xact has no XID. (We compute that here just because it's easier.)
1521  */
1522 static TransactionId
1523 RecordTransactionAbort(bool isSubXact)
1524 {
1526  TransactionId latestXid;
1527  int nrels;
1528  RelFileNode *rels;
1529  int nchildren;
1530  TransactionId *children;
1531  TimestampTz xact_time;
1532 
1533  /*
1534  * If we haven't been assigned an XID, nobody will care whether we aborted
1535  * or not. Hence, we're done in that case. It does not matter if we have
1536  * rels to delete (note that this routine is not responsible for actually
1537  * deleting 'em). We cannot have any child XIDs, either.
1538  */
1539  if (!TransactionIdIsValid(xid))
1540  {
1541  /* Reset XactLastRecEnd until the next transaction writes something */
1542  if (!isSubXact)
1543  XactLastRecEnd = 0;
1544  return InvalidTransactionId;
1545  }
1546 
1547  /*
1548  * We have a valid XID, so we should write an ABORT record for it.
1549  *
1550  * We do not flush XLOG to disk here, since the default assumption after a
1551  * crash would be that we aborted, anyway. For the same reason, we don't
1552  * need to worry about interlocking against checkpoint start.
1553  */
1554 
1555  /*
1556  * Check that we haven't aborted halfway through RecordTransactionCommit.
1557  */
1558  if (TransactionIdDidCommit(xid))
1559  elog(PANIC, "cannot abort transaction %u, it was already committed",
1560  xid);
1561 
1562  /* Fetch the data we need for the abort record */
1563  nrels = smgrGetPendingDeletes(false, &rels);
1564  nchildren = xactGetCommittedChildren(&children);
1565 
1566  /* XXX do we really need a critical section here? */
1568 
1569  /* Write the ABORT record */
1570  if (isSubXact)
1571  xact_time = GetCurrentTimestamp();
1572  else
1573  {
1575  xact_time = xactStopTimestamp;
1576  }
1577 
1578  XactLogAbortRecord(xact_time,
1579  nchildren, children,
1580  nrels, rels,
1582 
1583  /*
1584  * Report the latest async abort LSN, so that the WAL writer knows to
1585  * flush this abort. There's nothing to be gained by delaying this, since
1586  * WALWriter may as well do this when it can. This is important with
1587  * streaming replication because if we don't flush WAL regularly we will
1588  * find that large aborts leave us with a long backlog for when commits
1589  * occur after the abort, increasing our window of data loss should
1590  * problems occur at that point.
1591  */
1592  if (!isSubXact)
1594 
1595  /*
1596  * Mark the transaction aborted in clog. This is not absolutely necessary
1597  * but we may as well do it while we are here; also, in the subxact case
1598  * it is helpful because XactLockTableWait makes use of it to avoid
1599  * waiting for already-aborted subtransactions. It is OK to do it without
1600  * having flushed the ABORT record to disk, because in event of a crash
1601  * we'd be assumed to have aborted anyway.
1602  */
1603  TransactionIdAbortTree(xid, nchildren, children);
1604 
1605  END_CRIT_SECTION();
1606 
1607  /* Compute latestXid while we have the child XIDs handy */
1608  latestXid = TransactionIdLatest(xid, nchildren, children);
1609 
1610  /*
1611  * If we're aborting a subtransaction, we can immediately remove failed
1612  * XIDs from PGPROC's cache of running child XIDs. We do that here for
1613  * subxacts, because we already have the child XID array at hand. For
1614  * main xacts, the equivalent happens just after this function returns.
1615  */
1616  if (isSubXact)
1617  XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1618 
1619  /* Reset XactLastRecEnd until the next transaction writes something */
1620  if (!isSubXact)
1621  XactLastRecEnd = 0;
1622 
1623  /* And clean up local data */
1624  if (rels)
1625  pfree(rels);
1626 
1627  return latestXid;
1628 }
1629 
1630 /*
1631  * AtAbort_Memory
1632  */
1633 static void
1635 {
1636  /*
1637  * Switch into TransactionAbortContext, which should have some free space
1638  * even if nothing else does. We'll work in this context until we've
1639  * finished cleaning up.
1640  *
1641  * It is barely possible to get here when we've not been able to create
1642  * TransactionAbortContext yet; if so use TopMemoryContext.
1643  */
1644  if (TransactionAbortContext != NULL)
1645  MemoryContextSwitchTo(TransactionAbortContext);
1646  else
1648 }
1649 
1650 /*
1651  * AtSubAbort_Memory
1652  */
1653 static void
1655 {
1656  Assert(TransactionAbortContext != NULL);
1657 
1658  MemoryContextSwitchTo(TransactionAbortContext);
1659 }
1660 
1661 
1662 /*
1663  * AtAbort_ResourceOwner
1664  */
1665 static void
1667 {
1668  /*
1669  * Make sure we have a valid ResourceOwner, if possible (else it will be
1670  * NULL, which is OK)
1671  */
1673 }
1674 
1675 /*
1676  * AtSubAbort_ResourceOwner
1677  */
1678 static void
1680 {
1682 
1683  /* Make sure we have a valid ResourceOwner */
1685 }
1686 
1687 
1688 /*
1689  * AtSubAbort_childXids
1690  */
1691 static void
1693 {
1695 
1696  /*
1697  * We keep the child-XID arrays in TopTransactionContext (see
1698  * AtSubCommit_childXids). This means we'd better free the array
1699  * explicitly at abort to avoid leakage.
1700  */
1701  if (s->childXids != NULL)
1702  pfree(s->childXids);
1703  s->childXids = NULL;
1704  s->nChildXids = 0;
1705  s->maxChildXids = 0;
1706 
1707  /*
1708  * We could prune the unreportedXids array here. But we don't bother. That
1709  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1710  * would likely introduce more CPU time into the more common paths, so we
1711  * choose not to do that.
1712  */
1713 }
1714 
1715 /* ----------------------------------------------------------------
1716  * CleanupTransaction stuff
1717  * ----------------------------------------------------------------
1718  */
1719 
1720 /*
1721  * AtCleanup_Memory
1722  */
1723 static void
1725 {
1726  Assert(CurrentTransactionState->parent == NULL);
1727 
1728  /*
1729  * Now that we're "out" of a transaction, have the system allocate things
1730  * in the top memory context instead of per-transaction contexts.
1731  */
1733 
1734  /*
1735  * Clear the special abort context for next time.
1736  */
1737  if (TransactionAbortContext != NULL)
1738  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1739 
1740  /*
1741  * Release all transaction-local memory.
1742  */
1743  if (TopTransactionContext != NULL)
1745  TopTransactionContext = NULL;
1746  CurTransactionContext = NULL;
1747  CurrentTransactionState->curTransactionContext = NULL;
1748 }
1749 
1750 
1751 /* ----------------------------------------------------------------
1752  * CleanupSubTransaction stuff
1753  * ----------------------------------------------------------------
1754  */
1755 
1756 /*
1757  * AtSubCleanup_Memory
1758  */
1759 static void
1761 {
1763 
1764  Assert(s->parent != NULL);
1765 
1766  /* Make sure we're not in an about-to-be-deleted context */
1769 
1770  /*
1771  * Clear the special abort context for next time.
1772  */
1773  if (TransactionAbortContext != NULL)
1774  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1775 
1776  /*
1777  * Delete the subxact local memory contexts. Its CurTransactionContext can
1778  * go too (note this also kills CurTransactionContexts from any children
1779  * of the subxact).
1780  */
1781  if (s->curTransactionContext)
1783  s->curTransactionContext = NULL;
1784 }
1785 
1786 /* ----------------------------------------------------------------
1787  * interface routines
1788  * ----------------------------------------------------------------
1789  */
1790 
1791 /*
1792  * StartTransaction
1793  */
1794 static void
1796 {
1797  TransactionState s;
1798  VirtualTransactionId vxid;
1799 
1800  /*
1801  * Let's just make sure the state stack is empty
1802  */
1804  CurrentTransactionState = s;
1805 
1807 
1808  /*
1809  * check the current transaction state
1810  */
1811  if (s->state != TRANS_DEFAULT)
1812  elog(WARNING, "StartTransaction while in %s state",
1814 
1815  /*
1816  * set the current transaction state information appropriately during
1817  * start processing
1818  */
1819  s->state = TRANS_START;
1820  s->transactionId = InvalidTransactionId; /* until assigned */
1821 
1822  /*
1823  * Make sure we've reset xact state variables
1824  *
1825  * If recovery is still in progress, mark this transaction as read-only.
1826  * We have lower level defences in XLogInsert and elsewhere to stop us
1827  * from modifying data during recovery, but this gives the normal
1828  * indication to the user that the transaction is read-only.
1829  */
1830  if (RecoveryInProgress())
1831  {
1832  s->startedInRecovery = true;
1833  XactReadOnly = true;
1834  }
1835  else
1836  {
1837  s->startedInRecovery = false;
1839  }
1842  forceSyncCommit = false;
1843  MyXactFlags = 0;
1844 
1845  /*
1846  * reinitialize within-transaction counters
1847  */
1851  currentCommandIdUsed = false;
1852 
1853  /*
1854  * initialize reported xid accounting
1855  */
1856  nUnreportedXids = 0;
1857  s->didLogXid = false;
1858 
1859  /*
1860  * must initialize resource-management stuff first
1861  */
1862  AtStart_Memory();
1864 
1865  /*
1866  * Assign a new LocalTransactionId, and combine it with the backendId to
1867  * form a virtual transaction id.
1868  */
1869  vxid.backendId = MyBackendId;
1871 
1872  /*
1873  * Lock the virtual transaction id before we announce it in the proc array
1874  */
1876 
1877  /*
1878  * Advertise it in the proc array. We assume assignment of
1879  * LocalTransactionID is atomic, and the backendId should be set already.
1880  */
1881  Assert(MyProc->backendId == vxid.backendId);
1882  MyProc->lxid = vxid.localTransactionId;
1883 
1884  TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1885 
1886  /*
1887  * set transaction_timestamp() (a/k/a now()). We want this to be the same
1888  * as the first command's statement_timestamp(), so don't do a fresh
1889  * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1890  * xactStopTimestamp as unset.
1891  */
1893  xactStopTimestamp = 0;
1895 
1896  /*
1897  * initialize current transaction state fields
1898  *
1899  * note: prevXactReadOnly is not used at the outermost level
1900  */
1901  s->nestingLevel = 1;
1902  s->gucNestLevel = 1;
1903  s->childXids = NULL;
1904  s->nChildXids = 0;
1905  s->maxChildXids = 0;
1907  /* SecurityRestrictionContext should never be set outside a transaction */
1908  Assert(s->prevSecContext == 0);
1909 
1910  /*
1911  * initialize other subsystems for new transaction
1912  */
1913  AtStart_GUC();
1914  AtStart_Cache();
1916 
1917  /*
1918  * done with start processing, set current transaction state to "in
1919  * progress"
1920  */
1921  s->state = TRANS_INPROGRESS;
1922 
1923  ShowTransactionState("StartTransaction");
1924 }
1925 
1926 
1927 /*
1928  * CommitTransaction
1929  *
1930  * NB: if you change this routine, better look at PrepareTransaction too!
1931  */
1932 static void
1934 {
1936  TransactionId latestXid;
1937  bool is_parallel_worker;
1938 
1939  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
1940 
1941  /* Enforce parallel mode restrictions during parallel worker commit. */
1942  if (is_parallel_worker)
1944 
1945  ShowTransactionState("CommitTransaction");
1946 
1947  /*
1948  * check the current transaction state
1949  */
1950  if (s->state != TRANS_INPROGRESS)
1951  elog(WARNING, "CommitTransaction while in %s state",
1953  Assert(s->parent == NULL);
1954 
1955  /*
1956  * Do pre-commit processing that involves calling user-defined code, such
1957  * as triggers. Since closing cursors could queue trigger actions,
1958  * triggers could open cursors, etc, we have to keep looping until there's
1959  * nothing left to do.
1960  */
1961  for (;;)
1962  {
1963  /*
1964  * Fire all currently pending deferred triggers.
1965  */
1967 
1968  /*
1969  * Close open portals (converting holdable ones into static portals).
1970  * If there weren't any, we are done ... otherwise loop back to check
1971  * if they queued deferred triggers. Lather, rinse, repeat.
1972  */
1973  if (!PreCommit_Portals(false))
1974  break;
1975  }
1976 
1979 
1980  /*
1981  * The remaining actions cannot call any user-defined code, so it's safe
1982  * to start shutting down within-transaction services. But note that most
1983  * of this stuff could still throw an error, which would switch us into
1984  * the transaction-abort path.
1985  */
1986 
1987  /* If we might have parallel workers, clean them up now. */
1988  if (IsInParallelMode())
1989  AtEOXact_Parallel(true);
1990 
1991  /* Shut down the deferred-trigger manager */
1992  AfterTriggerEndXact(true);
1993 
1994  /*
1995  * Let ON COMMIT management do its thing (must happen after closing
1996  * cursors, to avoid dangling-reference problems)
1997  */
1999 
2000  /* close large objects before lower-level cleanup */
2001  AtEOXact_LargeObject(true);
2002 
2003  /*
2004  * Mark serializable transaction as complete for predicate locking
2005  * purposes. This should be done as late as we can put it and still allow
2006  * errors to be raised for failure patterns found at commit.
2007  */
2009 
2010  /*
2011  * Insert notifications sent by NOTIFY commands into the queue. This
2012  * should be late in the pre-commit sequence to minimize time spent
2013  * holding the notify-insertion lock.
2014  */
2015  PreCommit_Notify();
2016 
2017  /* Prevent cancel/die interrupt while cleaning up */
2018  HOLD_INTERRUPTS();
2019 
2020  /* Commit updates to the relation map --- do this as late as possible */
2021  AtEOXact_RelationMap(true);
2022 
2023  /*
2024  * set the current transaction state information appropriately during
2025  * commit processing
2026  */
2027  s->state = TRANS_COMMIT;
2028  s->parallelModeLevel = 0;
2029 
2030  if (!is_parallel_worker)
2031  {
2032  /*
2033  * We need to mark our XIDs as committed in pg_xact. This is where we
2034  * durably commit.
2035  */
2036  latestXid = RecordTransactionCommit();
2037  }
2038  else
2039  {
2040  /*
2041  * We must not mark our XID committed; the parallel master is
2042  * responsible for that.
2043  */
2044  latestXid = InvalidTransactionId;
2045 
2046  /*
2047  * Make sure the master will know about any WAL we wrote before it
2048  * commits.
2049  */
2051  }
2052 
2053  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2054 
2055  /*
2056  * Let others know about no transaction in progress by me. Note that this
2057  * must be done _before_ releasing locks we hold and _after_
2058  * RecordTransactionCommit.
2059  */
2060  ProcArrayEndTransaction(MyProc, latestXid);
2061 
2062  /*
2063  * This is all post-commit cleanup. Note that if an error is raised here,
2064  * it's too late to abort the transaction. This should be just
2065  * noncritical resource releasing.
2066  *
2067  * The ordering of operations is not entirely random. The idea is:
2068  * release resources visible to other backends (eg, files, buffer pins);
2069  * then release locks; then release backend-local resources. We want to
2070  * release locks at the point where any backend waiting for us will see
2071  * our transaction as being fully cleaned up.
2072  *
2073  * Resources that can be associated with individual queries are handled by
2074  * the ResourceOwner mechanism. The other calls here are for backend-wide
2075  * state.
2076  */
2077 
2078  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2079  : XACT_EVENT_COMMIT);
2080 
2083  true, true);
2084 
2085  /* Check we've released all buffer pins */
2086  AtEOXact_Buffers(true);
2087 
2088  /* Clean up the relation cache */
2089  AtEOXact_RelationCache(true);
2090 
2091  /*
2092  * Make catalog changes visible to all backends. This has to happen after
2093  * relcache references are dropped (see comments for
2094  * AtEOXact_RelationCache), but before locks are released (if anyone is
2095  * waiting for lock on a relation we've modified, we want them to know
2096  * about the catalog change before they start using the relation).
2097  */
2098  AtEOXact_Inval(true);
2099 
2101 
2104  true, true);
2107  true, true);
2108 
2109  /*
2110  * Likewise, dropping of files deleted during the transaction is best done
2111  * after releasing relcache and buffer pins. (This is not strictly
2112  * necessary during commit, since such pins should have been released
2113  * already, but this ordering is definitely critical during abort.) Since
2114  * this may take many seconds, also delay until after releasing locks.
2115  * Other backends will observe the attendant catalog changes and not
2116  * attempt to access affected files.
2117  */
2118  smgrDoPendingDeletes(true);
2119 
2120  AtCommit_Notify();
2121  AtEOXact_GUC(true, 1);
2122  AtEOXact_SPI(true);
2124  AtEOXact_Namespace(true, is_parallel_worker);
2125  AtEOXact_SMgr();
2126  AtEOXact_Files();
2128  AtEOXact_HashTables(true);
2129  AtEOXact_PgStat(true);
2130  AtEOXact_Snapshot(true, false);
2131  AtEOXact_ApplyLauncher(true);
2133 
2134  CurrentResourceOwner = NULL;
2136  s->curTransactionOwner = NULL;
2139 
2140  AtCommit_Memory();
2141 
2144  s->nestingLevel = 0;
2145  s->gucNestLevel = 0;
2146  s->childXids = NULL;
2147  s->nChildXids = 0;
2148  s->maxChildXids = 0;
2149 
2152 
2153  /*
2154  * done with commit processing, set current transaction state back to
2155  * default
2156  */
2157  s->state = TRANS_DEFAULT;
2158 
2160 }
2161 
2162 
2163 /*
2164  * PrepareTransaction
2165  *
2166  * NB: if you change this routine, better look at CommitTransaction too!
2167  */
2168 static void
2170 {
2173  GlobalTransaction gxact;
2174  TimestampTz prepared_at;
2175 
2177 
2178  ShowTransactionState("PrepareTransaction");
2179 
2180  /*
2181  * check the current transaction state
2182  */
2183  if (s->state != TRANS_INPROGRESS)
2184  elog(WARNING, "PrepareTransaction while in %s state",
2186  Assert(s->parent == NULL);
2187 
2188  /*
2189  * Do pre-commit processing that involves calling user-defined code, such
2190  * as triggers. Since closing cursors could queue trigger actions,
2191  * triggers could open cursors, etc, we have to keep looping until there's
2192  * nothing left to do.
2193  */
2194  for (;;)
2195  {
2196  /*
2197  * Fire all currently pending deferred triggers.
2198  */
2200 
2201  /*
2202  * Close open portals (converting holdable ones into static portals).
2203  * If there weren't any, we are done ... otherwise loop back to check
2204  * if they queued deferred triggers. Lather, rinse, repeat.
2205  */
2206  if (!PreCommit_Portals(true))
2207  break;
2208  }
2209 
2211 
2212  /*
2213  * The remaining actions cannot call any user-defined code, so it's safe
2214  * to start shutting down within-transaction services. But note that most
2215  * of this stuff could still throw an error, which would switch us into
2216  * the transaction-abort path.
2217  */
2218 
2219  /* Shut down the deferred-trigger manager */
2220  AfterTriggerEndXact(true);
2221 
2222  /*
2223  * Let ON COMMIT management do its thing (must happen after closing
2224  * cursors, to avoid dangling-reference problems)
2225  */
2227 
2228  /* close large objects before lower-level cleanup */
2229  AtEOXact_LargeObject(true);
2230 
2231  /*
2232  * Mark serializable transaction as complete for predicate locking
2233  * purposes. This should be done as late as we can put it and still allow
2234  * errors to be raised for failure patterns found at commit.
2235  */
2237 
2238  /* NOTIFY will be handled below */
2239 
2240  /*
2241  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2242  * this transaction. Having the prepared xact hold locks on another
2243  * backend's temp table seems a bad idea --- for instance it would prevent
2244  * the backend from exiting. There are other problems too, such as how to
2245  * clean up the source backend's local buffers and ON COMMIT state if the
2246  * prepared xact includes a DROP of a temp table.
2247  *
2248  * We must check this after executing any ON COMMIT actions, because they
2249  * might still access a temp relation.
2250  *
2251  * XXX In principle this could be relaxed to allow some useful special
2252  * cases, such as a temp table created and dropped all within the
2253  * transaction. That seems to require much more bookkeeping though.
2254  */
2256  ereport(ERROR,
2257  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2258  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2259 
2260  /*
2261  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2262  * supported if we added cleanup logic to twophase.c, but for now it
2263  * doesn't seem worth the trouble.
2264  */
2266  ereport(ERROR,
2267  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2268  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2269 
2270  /*
2271  * Don't allow PREPARE but for transaction that has/might kill logical
2272  * replication workers.
2273  */
2275  ereport(ERROR,
2276  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2277  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
2278 
2279  /* Prevent cancel/die interrupt while cleaning up */
2280  HOLD_INTERRUPTS();
2281 
2282  /*
2283  * set the current transaction state information appropriately during
2284  * prepare processing
2285  */
2286  s->state = TRANS_PREPARE;
2287 
2288  prepared_at = GetCurrentTimestamp();
2289 
2290  /* Tell bufmgr and smgr to prepare for commit */
2291  BufmgrCommit();
2292 
2293  /*
2294  * Reserve the GID for this transaction. This could fail if the requested
2295  * GID is invalid or already in use.
2296  */
2297  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2298  GetUserId(), MyDatabaseId);
2299  prepareGID = NULL;
2300 
2301  /*
2302  * Collect data for the 2PC state file. Note that in general, no actual
2303  * state change should happen in the called modules during this step,
2304  * since it's still possible to fail before commit, and in that case we
2305  * want transaction abort to be able to clean up. (In particular, the
2306  * AtPrepare routines may error out if they find cases they cannot
2307  * handle.) State cleanup should happen in the PostPrepare routines
2308  * below. However, some modules can go ahead and clear state here because
2309  * they wouldn't do anything with it during abort anyway.
2310  *
2311  * Note: because the 2PC state file records will be replayed in the same
2312  * order they are made, the order of these calls has to match the order in
2313  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2314  * PREPARED; in particular, pay attention to whether things should happen
2315  * before or after releasing the transaction's locks.
2316  */
2317  StartPrepare(gxact);
2318 
2319  AtPrepare_Notify();
2320  AtPrepare_Locks();
2322  AtPrepare_PgStat();
2325 
2326  /*
2327  * Here is where we really truly prepare.
2328  *
2329  * We have to record transaction prepares even if we didn't make any
2330  * updates, because the transaction manager might get confused if we lose
2331  * a global transaction.
2332  */
2333  EndPrepare(gxact);
2334 
2335  /*
2336  * Now we clean up backend-internal state and release internal resources.
2337  */
2338 
2339  /* Reset XactLastRecEnd until the next transaction writes something */
2340  XactLastRecEnd = 0;
2341 
2342  /*
2343  * Let others know about no transaction in progress by me. This has to be
2344  * done *after* the prepared transaction has been marked valid, else
2345  * someone may think it is unlocked and recyclable.
2346  */
2348 
2349  /*
2350  * In normal commit-processing, this is all non-critical post-transaction
2351  * cleanup. When the transaction is prepared, however, it's important
2352  * that the locks and other per-backend resources are transferred to the
2353  * prepared transaction's PGPROC entry. Note that if an error is raised
2354  * here, it's too late to abort the transaction. XXX: This probably should
2355  * be in a critical section, to force a PANIC if any of this fails, but
2356  * that cure could be worse than the disease.
2357  */
2358 
2360 
2363  true, true);
2364 
2365  /* Check we've released all buffer pins */
2366  AtEOXact_Buffers(true);
2367 
2368  /* Clean up the relation cache */
2369  AtEOXact_RelationCache(true);
2370 
2371  /* notify doesn't need a postprepare call */
2372 
2374 
2376 
2377  PostPrepare_smgr();
2378 
2379  PostPrepare_MultiXact(xid);
2380 
2381  PostPrepare_Locks(xid);
2383 
2386  true, true);
2389  true, true);
2390 
2391  /*
2392  * Allow another backend to finish the transaction. After
2393  * PostPrepare_Twophase(), the transaction is completely detached from our
2394  * backend. The rest is just non-critical cleanup of backend-local state.
2395  */
2397 
2398  /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2399  AtEOXact_GUC(true, 1);
2400  AtEOXact_SPI(true);
2402  AtEOXact_Namespace(true, false);
2403  AtEOXact_SMgr();
2404  AtEOXact_Files();
2406  AtEOXact_HashTables(true);
2407  /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2408  AtEOXact_Snapshot(true, true);
2410 
2411  CurrentResourceOwner = NULL;
2413  s->curTransactionOwner = NULL;
2416 
2417  AtCommit_Memory();
2418 
2421  s->nestingLevel = 0;
2422  s->gucNestLevel = 0;
2423  s->childXids = NULL;
2424  s->nChildXids = 0;
2425  s->maxChildXids = 0;
2426 
2429 
2430  /*
2431  * done with 1st phase commit processing, set current transaction state
2432  * back to default
2433  */
2434  s->state = TRANS_DEFAULT;
2435 
2437 }
2438 
2439 
2440 /*
2441  * AbortTransaction
2442  */
2443 static void
2445 {
2447  TransactionId latestXid;
2448  bool is_parallel_worker;
2449 
2450  /* Prevent cancel/die interrupt while cleaning up */
2451  HOLD_INTERRUPTS();
2452 
2453  /* Make sure we have a valid memory context and resource owner */
2454  AtAbort_Memory();
2456 
2457  /*
2458  * Release any LW locks we might be holding as quickly as possible.
2459  * (Regular locks, however, must be held till we finish aborting.)
2460  * Releasing LW locks is critical since we might try to grab them again
2461  * while cleaning up!
2462  */
2463  LWLockReleaseAll();
2464 
2465  /* Clear wait information and command progress indicator */
2468 
2469  /* Clean up buffer I/O and buffer context locks, too */
2470  AbortBufferIO();
2471  UnlockBuffers();
2472 
2473  /* Reset WAL record construction state */
2475 
2476  /* Cancel condition variable sleep */
2478 
2479  /*
2480  * Also clean up any open wait for lock, since the lock manager will choke
2481  * if we try to wait for another lock before doing this.
2482  */
2483  LockErrorCleanup();
2484 
2485  /*
2486  * If any timeout events are still active, make sure the timeout interrupt
2487  * is scheduled. This covers possible loss of a timeout interrupt due to
2488  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2489  * We delay this till after LockErrorCleanup so that we don't uselessly
2490  * reschedule lock or deadlock check timeouts.
2491  */
2493 
2494  /*
2495  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2496  * handler. We do this fairly early in the sequence so that the timeout
2497  * infrastructure will be functional if needed while aborting.
2498  */
2500 
2501  /*
2502  * check the current transaction state
2503  */
2504  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2505  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2506  elog(WARNING, "AbortTransaction while in %s state",
2508  Assert(s->parent == NULL);
2509 
2510  /*
2511  * set the current transaction state information appropriately during the
2512  * abort processing
2513  */
2514  s->state = TRANS_ABORT;
2515 
2516  /*
2517  * Reset user ID which might have been changed transiently. We need this
2518  * to clean up in case control escaped out of a SECURITY DEFINER function
2519  * or other local change of CurrentUserId; therefore, the prior value of
2520  * SecurityRestrictionContext also needs to be restored.
2521  *
2522  * (Note: it is not necessary to restore session authorization or role
2523  * settings here because those can only be changed via GUC, and GUC will
2524  * take care of rolling them back if need be.)
2525  */
2527 
2528  /* If in parallel mode, clean up workers and exit parallel mode. */
2529  if (IsInParallelMode())
2530  {
2531  AtEOXact_Parallel(false);
2532  s->parallelModeLevel = 0;
2533  }
2534 
2535  /*
2536  * do abort processing
2537  */
2538  AfterTriggerEndXact(false); /* 'false' means it's abort */
2539  AtAbort_Portals();
2540  AtEOXact_LargeObject(false);
2541  AtAbort_Notify();
2542  AtEOXact_RelationMap(false);
2543  AtAbort_Twophase();
2544 
2545  /*
2546  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2547  * far as assigning an XID to advertise). But if we're inside a parallel
2548  * worker, skip this; the user backend must be the one to write the abort
2549  * record.
2550  */
2551  if (!is_parallel_worker)
2552  latestXid = RecordTransactionAbort(false);
2553  else
2554  {
2555  latestXid = InvalidTransactionId;
2556 
2557  /*
2558  * Since the parallel master won't get our value of XactLastRecEnd in
2559  * this case, we nudge WAL-writer ourselves in this case. See related
2560  * comments in RecordTransactionAbort for why this matters.
2561  */
2563  }
2564 
2565  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2566 
2567  /*
2568  * Let others know about no transaction in progress by me. Note that this
2569  * must be done _before_ releasing locks we hold and _after_
2570  * RecordTransactionAbort.
2571  */
2572  ProcArrayEndTransaction(MyProc, latestXid);
2573 
2574  /*
2575  * Post-abort cleanup. See notes in CommitTransaction() concerning
2576  * ordering. We can skip all of it if the transaction failed before
2577  * creating a resource owner.
2578  */
2579  if (TopTransactionResourceOwner != NULL)
2580  {
2581  if (is_parallel_worker)
2583  else
2585 
2588  false, true);
2589  AtEOXact_Buffers(false);
2590  AtEOXact_RelationCache(false);
2591  AtEOXact_Inval(false);
2595  false, true);
2598  false, true);
2599  smgrDoPendingDeletes(false);
2600 
2601  AtEOXact_GUC(false, 1);
2602  AtEOXact_SPI(false);
2604  AtEOXact_Namespace(false, is_parallel_worker);
2605  AtEOXact_SMgr();
2606  AtEOXact_Files();
2608  AtEOXact_HashTables(false);
2609  AtEOXact_PgStat(false);
2610  AtEOXact_ApplyLauncher(false);
2612  }
2613 
2614  /*
2615  * State remains TRANS_ABORT until CleanupTransaction().
2616  */
2618 }
2619 
2620 /*
2621  * CleanupTransaction
2622  */
2623 static void
2625 {
2627 
2628  /*
2629  * State should still be TRANS_ABORT from AbortTransaction().
2630  */
2631  if (s->state != TRANS_ABORT)
2632  elog(FATAL, "CleanupTransaction: unexpected state %s",
2634 
2635  /*
2636  * do abort cleanup processing
2637  */
2638  AtCleanup_Portals(); /* now safe to release portal memory */
2639  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2640 
2641  CurrentResourceOwner = NULL; /* and resource owner */
2644  s->curTransactionOwner = NULL;
2647 
2648  AtCleanup_Memory(); /* and transaction memory */
2649 
2652  s->nestingLevel = 0;
2653  s->gucNestLevel = 0;
2654  s->childXids = NULL;
2655  s->nChildXids = 0;
2656  s->maxChildXids = 0;
2657  s->parallelModeLevel = 0;
2658 
2661 
2662  /*
2663  * done with abort processing, set current transaction state back to
2664  * default
2665  */
2666  s->state = TRANS_DEFAULT;
2667 }
2668 
2669 /*
2670  * StartTransactionCommand
2671  */
2672 void
2674 {
2676 
2677  switch (s->blockState)
2678  {
2679  /*
2680  * if we aren't in a transaction block, we just do our usual start
2681  * transaction.
2682  */
2683  case TBLOCK_DEFAULT:
2684  StartTransaction();
2686  break;
2687 
2688  /*
2689  * We are somewhere in a transaction block or subtransaction and
2690  * about to start a new command. For now we do nothing, but
2691  * someday we may do command-local resource initialization. (Note
2692  * that any needed CommandCounterIncrement was done by the
2693  * previous CommitTransactionCommand.)
2694  */
2695  case TBLOCK_INPROGRESS:
2697  case TBLOCK_SUBINPROGRESS:
2698  break;
2699 
2700  /*
2701  * Here we are in a failed transaction block (one of the commands
2702  * caused an abort) so we do nothing but remain in the abort
2703  * state. Eventually we will get a ROLLBACK command which will
2704  * get us out of this state. (It is up to other code to ensure
2705  * that no commands other than ROLLBACK will be processed in these
2706  * states.)
2707  */
2708  case TBLOCK_ABORT:
2709  case TBLOCK_SUBABORT:
2710  break;
2711 
2712  /* These cases are invalid. */
2713  case TBLOCK_STARTED:
2714  case TBLOCK_BEGIN:
2716  case TBLOCK_SUBBEGIN:
2717  case TBLOCK_END:
2718  case TBLOCK_SUBRELEASE:
2719  case TBLOCK_SUBCOMMIT:
2720  case TBLOCK_ABORT_END:
2721  case TBLOCK_SUBABORT_END:
2722  case TBLOCK_ABORT_PENDING:
2724  case TBLOCK_SUBRESTART:
2726  case TBLOCK_PREPARE:
2727  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2729  break;
2730  }
2731 
2732  /*
2733  * We must switch to CurTransactionContext before returning. This is
2734  * already done if we called StartTransaction, otherwise not.
2735  */
2736  Assert(CurTransactionContext != NULL);
2738 }
2739 
2740 /*
2741  * CommitTransactionCommand
2742  */
2743 void
2745 {
2747 
2748  switch (s->blockState)
2749  {
2750  /*
2751  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2752  * StartTransactionCommand didn't set the STARTED state
2753  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2754  * by EndParallelWorkerTransaction(), not this function.
2755  */
2756  case TBLOCK_DEFAULT:
2758  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2760  break;
2761 
2762  /*
2763  * If we aren't in a transaction block, just do our usual
2764  * transaction commit, and return to the idle state.
2765  */
2766  case TBLOCK_STARTED:
2769  break;
2770 
2771  /*
2772  * We are completing a "BEGIN TRANSACTION" command, so we change
2773  * to the "transaction block in progress" state and return. (We
2774  * assume the BEGIN did nothing to the database, so we need no
2775  * CommandCounterIncrement.)
2776  */
2777  case TBLOCK_BEGIN:
2779  break;
2780 
2781  /*
2782  * This is the case when we have finished executing a command
2783  * someplace within a transaction block. We increment the command
2784  * counter and return.
2785  */
2786  case TBLOCK_INPROGRESS:
2788  case TBLOCK_SUBINPROGRESS:
2790  break;
2791 
2792  /*
2793  * We are completing a "COMMIT" command. Do it and return to the
2794  * idle state.
2795  */
2796  case TBLOCK_END:
2799  break;
2800 
2801  /*
2802  * Here we are in the middle of a transaction block but one of the
2803  * commands caused an abort so we do nothing but remain in the
2804  * abort state. Eventually we will get a ROLLBACK command.
2805  */
2806  case TBLOCK_ABORT:
2807  case TBLOCK_SUBABORT:
2808  break;
2809 
2810  /*
2811  * Here we were in an aborted transaction block and we just got
2812  * the ROLLBACK command from the user, so clean up the
2813  * already-aborted transaction and return to the idle state.
2814  */
2815  case TBLOCK_ABORT_END:
2818  break;
2819 
2820  /*
2821  * Here we were in a perfectly good transaction block but the user
2822  * told us to ROLLBACK anyway. We have to abort the transaction
2823  * and then clean up.
2824  */
2825  case TBLOCK_ABORT_PENDING:
2826  AbortTransaction();
2829  break;
2830 
2831  /*
2832  * We are completing a "PREPARE TRANSACTION" command. Do it and
2833  * return to the idle state.
2834  */
2835  case TBLOCK_PREPARE:
2838  break;
2839 
2840  /*
2841  * We were just issued a SAVEPOINT inside a transaction block.
2842  * Start a subtransaction. (DefineSavepoint already did
2843  * PushTransaction, so as to have someplace to put the SUBBEGIN
2844  * state.)
2845  */
2846  case TBLOCK_SUBBEGIN:
2849  break;
2850 
2851  /*
2852  * We were issued a RELEASE command, so we end the current
2853  * subtransaction and return to the parent transaction. The parent
2854  * might be ended too, so repeat till we find an INPROGRESS
2855  * transaction or subtransaction.
2856  */
2857  case TBLOCK_SUBRELEASE:
2858  do
2859  {
2861  s = CurrentTransactionState; /* changed by pop */
2862  } while (s->blockState == TBLOCK_SUBRELEASE);
2863 
2866  break;
2867 
2868  /*
2869  * We were issued a COMMIT, so we end the current subtransaction
2870  * hierarchy and perform final commit. We do this by rolling up
2871  * any subtransactions into their parent, which leads to O(N^2)
2872  * operations with respect to resource owners - this isn't that
2873  * bad until we approach a thousands of savepoints but is
2874  * necessary for correctness should after triggers create new
2875  * resource owners.
2876  */
2877  case TBLOCK_SUBCOMMIT:
2878  do
2879  {
2881  s = CurrentTransactionState; /* changed by pop */
2882  } while (s->blockState == TBLOCK_SUBCOMMIT);
2883  /* If we had a COMMIT command, finish off the main xact too */
2884  if (s->blockState == TBLOCK_END)
2885  {
2886  Assert(s->parent == NULL);
2889  }
2890  else if (s->blockState == TBLOCK_PREPARE)
2891  {
2892  Assert(s->parent == NULL);
2895  }
2896  else
2897  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2899  break;
2900 
2901  /*
2902  * The current already-failed subtransaction is ending due to a
2903  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2904  * examine the parent (which could be in any of several states).
2905  */
2906  case TBLOCK_SUBABORT_END:
2909  break;
2910 
2911  /*
2912  * As above, but it's not dead yet, so abort first.
2913  */
2918  break;
2919 
2920  /*
2921  * The current subtransaction is the target of a ROLLBACK TO
2922  * command. Abort and pop it, then start a new subtransaction
2923  * with the same name.
2924  */
2925  case TBLOCK_SUBRESTART:
2926  {
2927  char *name;
2928  int savepointLevel;
2929 
2930  /* save name and keep Cleanup from freeing it */
2931  name = s->name;
2932  s->name = NULL;
2933  savepointLevel = s->savepointLevel;
2934 
2937 
2938  DefineSavepoint(NULL);
2939  s = CurrentTransactionState; /* changed by push */
2940  s->name = name;
2942 
2943  /* This is the same as TBLOCK_SUBBEGIN case */
2947  }
2948  break;
2949 
2950  /*
2951  * Same as above, but the subtransaction had already failed, so we
2952  * don't need AbortSubTransaction.
2953  */
2955  {
2956  char *name;
2957  int savepointLevel;
2958 
2959  /* save name and keep Cleanup from freeing it */
2960  name = s->name;
2961  s->name = NULL;
2962  savepointLevel = s->savepointLevel;
2963 
2965 
2966  DefineSavepoint(NULL);
2967  s = CurrentTransactionState; /* changed by push */
2968  s->name = name;
2970 
2971  /* This is the same as TBLOCK_SUBBEGIN case */
2975  }
2976  break;
2977  }
2978 }
2979 
2980 /*
2981  * AbortCurrentTransaction
2982  */
2983 void
2985 {
2987 
2988  switch (s->blockState)
2989  {
2990  case TBLOCK_DEFAULT:
2991  if (s->state == TRANS_DEFAULT)
2992  {
2993  /* we are idle, so nothing to do */
2994  }
2995  else
2996  {
2997  /*
2998  * We can get here after an error during transaction start
2999  * (state will be TRANS_START). Need to clean up the
3000  * incompletely started transaction. First, adjust the
3001  * low-level state to suppress warning message from
3002  * AbortTransaction.
3003  */
3004  if (s->state == TRANS_START)
3005  s->state = TRANS_INPROGRESS;
3006  AbortTransaction();
3008  }
3009  break;
3010 
3011  /*
3012  * If we aren't in a transaction block, we just do the basic abort
3013  * & cleanup transaction. For this purpose, we treat an implicit
3014  * transaction block as if it were a simple statement.
3015  */
3016  case TBLOCK_STARTED:
3018  AbortTransaction();
3021  break;
3022 
3023  /*
3024  * If we are in TBLOCK_BEGIN it means something screwed up right
3025  * after reading "BEGIN TRANSACTION". We assume that the user
3026  * will interpret the error as meaning the BEGIN failed to get him
3027  * into a transaction block, so we should abort and return to idle
3028  * state.
3029  */
3030  case TBLOCK_BEGIN:
3031  AbortTransaction();
3034  break;
3035 
3036  /*
3037  * We are somewhere in a transaction block and we've gotten a
3038  * failure, so we abort the transaction and set up the persistent
3039  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3040  */
3041  case TBLOCK_INPROGRESS:
3043  AbortTransaction();
3044  s->blockState = TBLOCK_ABORT;
3045  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3046  break;
3047 
3048  /*
3049  * Here, we failed while trying to COMMIT. Clean up the
3050  * transaction and return to idle state (we do not want to stay in
3051  * the transaction).
3052  */
3053  case TBLOCK_END:
3054  AbortTransaction();
3057  break;
3058 
3059  /*
3060  * Here, we are already in an aborted transaction state and are
3061  * waiting for a ROLLBACK, but for some reason we failed again! So
3062  * we just remain in the abort state.
3063  */
3064  case TBLOCK_ABORT:
3065  case TBLOCK_SUBABORT:
3066  break;
3067 
3068  /*
3069  * We are in a failed transaction and we got the ROLLBACK command.
3070  * We have already aborted, we just need to cleanup and go to idle
3071  * state.
3072  */
3073  case TBLOCK_ABORT_END:
3076  break;
3077 
3078  /*
3079  * We are in a live transaction and we got a ROLLBACK command.
3080  * Abort, cleanup, go to idle state.
3081  */
3082  case TBLOCK_ABORT_PENDING:
3083  AbortTransaction();
3086  break;
3087 
3088  /*
3089  * Here, we failed while trying to PREPARE. Clean up the
3090  * transaction and return to idle state (we do not want to stay in
3091  * the transaction).
3092  */
3093  case TBLOCK_PREPARE:
3094  AbortTransaction();
3097  break;
3098 
3099  /*
3100  * We got an error inside a subtransaction. Abort just the
3101  * subtransaction, and go to the persistent SUBABORT state until
3102  * we get ROLLBACK.
3103  */
3104  case TBLOCK_SUBINPROGRESS:
3107  break;
3108 
3109  /*
3110  * If we failed while trying to create a subtransaction, clean up
3111  * the broken subtransaction and abort the parent. The same
3112  * applies if we get a failure while ending a subtransaction.
3113  */
3114  case TBLOCK_SUBBEGIN:
3115  case TBLOCK_SUBRELEASE:
3116  case TBLOCK_SUBCOMMIT:
3118  case TBLOCK_SUBRESTART:
3122  break;
3123 
3124  /*
3125  * Same as above, except the Abort() was already done.
3126  */
3127  case TBLOCK_SUBABORT_END:
3131  break;
3132  }
3133 }
3134 
3135 /*
3136  * PreventTransactionChain
3137  *
3138  * This routine is to be called by statements that must not run inside
3139  * a transaction block, typically because they have non-rollback-able
3140  * side effects or do internal commits.
3141  *
3142  * If we have already started a transaction block, issue an error; also issue
3143  * an error if we appear to be running inside a user-defined function (which
3144  * could issue more commands and possibly cause a failure after the statement
3145  * completes). Subtransactions are verboten too.
3146  *
3147  * isTopLevel: passed down from ProcessUtility to determine whether we are
3148  * inside a function. (We will always fail if this is false, but it's
3149  * convenient to centralize the check here instead of making callers do it.)
3150  * stmtType: statement type name, for error messages.
3151  */
3152 void
3153 PreventTransactionChain(bool isTopLevel, const char *stmtType)
3154 {
3155  /*
3156  * xact block already started?
3157  */
3158  if (IsTransactionBlock())
3159  ereport(ERROR,
3160  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3161  /* translator: %s represents an SQL statement name */
3162  errmsg("%s cannot run inside a transaction block",
3163  stmtType)));
3164 
3165  /*
3166  * subtransaction?
3167  */
3168  if (IsSubTransaction())
3169  ereport(ERROR,
3170  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3171  /* translator: %s represents an SQL statement name */
3172  errmsg("%s cannot run inside a subtransaction",
3173  stmtType)));
3174 
3175  /*
3176  * inside a function call?
3177  */
3178  if (!isTopLevel)
3179  ereport(ERROR,
3180  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3181  /* translator: %s represents an SQL statement name */
3182  errmsg("%s cannot be executed from a function", stmtType)));
3183 
3184  /* If we got past IsTransactionBlock test, should be in default state */
3185  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3186  CurrentTransactionState->blockState != TBLOCK_STARTED)
3187  elog(FATAL, "cannot prevent transaction chain");
3188  /* all okay */
3189 }
3190 
3191 /*
3192  * These two functions allow for warnings or errors if a command is
3193  * executed outside of a transaction block.
3194  *
3195  * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and
3196  * SET that have no effect issue warnings, all other no-effect commands
3197  * generate errors.
3198  */
3199 void
3200 WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
3201 {
3202  CheckTransactionChain(isTopLevel, false, stmtType);
3203 }
3204 
3205 void
3206 RequireTransactionChain(bool isTopLevel, const char *stmtType)
3207 {
3208  CheckTransactionChain(isTopLevel, true, stmtType);
3209 }
3210 
3211 /*
3212  * RequireTransactionChain
3213  *
3214  * This routine is to be called by statements that must run inside
3215  * a transaction block, because they have no effects that persist past
3216  * transaction end (and so calling them outside a transaction block
3217  * is presumably an error). DECLARE CURSOR is an example.
3218  *
3219  * If we appear to be running inside a user-defined function, we do not
3220  * issue anything, since the function could issue more commands that make
3221  * use of the current statement's results. Likewise subtransactions.
3222  * Thus this is an inverse for PreventTransactionChain.
3223  *
3224  * isTopLevel: passed down from ProcessUtility to determine whether we are
3225  * inside a function.
3226  * stmtType: statement type name, for warning or error messages.
3227  */
3228 static void
3229 CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
3230 {
3231  /*
3232  * xact block already started?
3233  */
3234  if (IsTransactionBlock())
3235  return;
3236 
3237  /*
3238  * subtransaction?
3239  */
3240  if (IsSubTransaction())
3241  return;
3242 
3243  /*
3244  * inside a function call?
3245  */
3246  if (!isTopLevel)
3247  return;
3248 
3249  ereport(throwError ? ERROR : WARNING,
3250  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3251  /* translator: %s represents an SQL statement name */
3252  errmsg("%s can only be used in transaction blocks",
3253  stmtType)));
3254  return;
3255 }
3256 
3257 /*
3258  * IsInTransactionChain
3259  *
3260  * This routine is for statements that need to behave differently inside
3261  * a transaction block than when running as single commands. ANALYZE is
3262  * currently the only example.
3263  *
3264  * isTopLevel: passed down from ProcessUtility to determine whether we are
3265  * inside a function.
3266  */
3267 bool
3268 IsInTransactionChain(bool isTopLevel)
3269 {
3270  /*
3271  * Return true on same conditions that would make PreventTransactionChain
3272  * error out
3273  */
3274  if (IsTransactionBlock())
3275  return true;
3276 
3277  if (IsSubTransaction())
3278  return true;
3279 
3280  if (!isTopLevel)
3281  return true;
3282 
3283  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3284  CurrentTransactionState->blockState != TBLOCK_STARTED)
3285  return true;
3286 
3287  return false;
3288 }
3289 
3290 
3291 /*
3292  * Register or deregister callback functions for start- and end-of-xact
3293  * operations.
3294  *
3295  * These functions are intended for use by dynamically loaded modules.
3296  * For built-in modules we generally just hardwire the appropriate calls
3297  * (mainly because it's easier to control the order that way, where needed).
3298  *
3299  * At transaction end, the callback occurs post-commit or post-abort, so the
3300  * callback functions can only do noncritical cleanup.
3301  */
3302 void
3304 {
3305  XactCallbackItem *item;
3306 
3307  item = (XactCallbackItem *)
3309  item->callback = callback;
3310  item->arg = arg;
3311  item->next = Xact_callbacks;
3312  Xact_callbacks = item;
3313 }
3314 
3315 void
3317 {
3318  XactCallbackItem *item;
3319  XactCallbackItem *prev;
3320 
3321  prev = NULL;
3322  for (item = Xact_callbacks; item; prev = item, item = item->next)
3323  {
3324  if (item->callback == callback && item->arg == arg)
3325  {
3326  if (prev)
3327  prev->next = item->next;
3328  else
3329  Xact_callbacks = item->next;
3330  pfree(item);
3331  break;
3332  }
3333  }
3334 }
3335 
3336 static void
3338 {
3339  XactCallbackItem *item;
3340 
3341  for (item = Xact_callbacks; item; item = item->next)
3342  item->callback(event, item->arg);
3343 }
3344 
3345 
3346 /*
3347  * Register or deregister callback functions for start- and end-of-subxact
3348  * operations.
3349  *
3350  * Pretty much same as above, but for subtransaction events.
3351  *
3352  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3353  * so the callback functions can only do noncritical cleanup. At
3354  * subtransaction start, the callback is called when the subtransaction has
3355  * finished initializing.
3356  */
3357 void
3359 {
3360  SubXactCallbackItem *item;
3361 
3362  item = (SubXactCallbackItem *)
3364  item->callback = callback;
3365  item->arg = arg;
3366  item->next = SubXact_callbacks;
3367  SubXact_callbacks = item;
3368 }
3369 
3370 void
3372 {
3373  SubXactCallbackItem *item;
3374  SubXactCallbackItem *prev;
3375 
3376  prev = NULL;
3377  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3378  {
3379  if (item->callback == callback && item->arg == arg)
3380  {
3381  if (prev)
3382  prev->next = item->next;
3383  else
3384  SubXact_callbacks = item->next;
3385  pfree(item);
3386  break;
3387  }
3388  }
3389 }
3390 
3391 static void
3393  SubTransactionId mySubid,
3394  SubTransactionId parentSubid)
3395 {
3396  SubXactCallbackItem *item;
3397 
3398  for (item = SubXact_callbacks; item; item = item->next)
3399  item->callback(event, mySubid, parentSubid, item->arg);
3400 }
3401 
3402 
3403 /* ----------------------------------------------------------------
3404  * transaction block support
3405  * ----------------------------------------------------------------
3406  */
3407 
3408 /*
3409  * BeginTransactionBlock
3410  * This executes a BEGIN command.
3411  */
3412 void
3414 {
3416 
3417  switch (s->blockState)
3418  {
3419  /*
3420  * We are not inside a transaction block, so allow one to begin.
3421  */
3422  case TBLOCK_STARTED:
3423  s->blockState = TBLOCK_BEGIN;
3424  break;
3425 
3426  /*
3427  * BEGIN converts an implicit transaction block to a regular one.
3428  * (Note that we allow this even if we've already done some
3429  * commands, which is a bit odd but matches historical practice.)
3430  */
3432  s->blockState = TBLOCK_BEGIN;
3433  break;
3434 
3435  /*
3436  * Already a transaction block in progress.
3437  */
3438  case TBLOCK_INPROGRESS:
3440  case TBLOCK_SUBINPROGRESS:
3441  case TBLOCK_ABORT:
3442  case TBLOCK_SUBABORT:
3443  ereport(WARNING,
3444  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3445  errmsg("there is already a transaction in progress")));
3446  break;
3447 
3448  /* These cases are invalid. */
3449  case TBLOCK_DEFAULT:
3450  case TBLOCK_BEGIN:
3451  case TBLOCK_SUBBEGIN:
3452  case TBLOCK_END:
3453  case TBLOCK_SUBRELEASE:
3454  case TBLOCK_SUBCOMMIT:
3455  case TBLOCK_ABORT_END:
3456  case TBLOCK_SUBABORT_END:
3457  case TBLOCK_ABORT_PENDING:
3459  case TBLOCK_SUBRESTART:
3461  case TBLOCK_PREPARE:
3462  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3464  break;
3465  }
3466 }
3467 
3468 /*
3469  * PrepareTransactionBlock
3470  * This executes a PREPARE command.
3471  *
3472  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3473  * happened: true for PREPARE, false for ROLLBACK.
3474  *
3475  * Note that we don't actually do anything here except change blockState.
3476  * The real work will be done in the upcoming PrepareTransaction().
3477  * We do it this way because it's not convenient to change memory context,
3478  * resource owner, etc while executing inside a Portal.
3479  */
3480 bool
3481 PrepareTransactionBlock(const char *gid)
3482 {
3483  TransactionState s;
3484  bool result;
3485 
3486  /* Set up to commit the current transaction */
3487  result = EndTransactionBlock();
3488 
3489  /* If successful, change outer tblock state to PREPARE */
3490  if (result)
3491  {
3493 
3494  while (s->parent != NULL)
3495  s = s->parent;
3496 
3497  if (s->blockState == TBLOCK_END)
3498  {
3499  /* Save GID where PrepareTransaction can find it again */
3501 
3503  }
3504  else
3505  {
3506  /*
3507  * ignore case where we are not in a transaction;
3508  * EndTransactionBlock already issued a warning.
3509  */
3512  /* Don't send back a PREPARE result tag... */
3513  result = false;
3514  }
3515  }
3516 
3517  return result;
3518 }
3519 
3520 /*
3521  * EndTransactionBlock
3522  * This executes a COMMIT command.
3523  *
3524  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3525  * happened: true for COMMIT, false for ROLLBACK.
3526  *
3527  * Note that we don't actually do anything here except change blockState.
3528  * The real work will be done in the upcoming CommitTransactionCommand().
3529  * We do it this way because it's not convenient to change memory context,
3530  * resource owner, etc while executing inside a Portal.
3531  */
3532 bool
3534 {
3536  bool result = false;
3537 
3538  switch (s->blockState)
3539  {
3540  /*
3541  * We are in a transaction block, so tell CommitTransactionCommand
3542  * to COMMIT.
3543  */
3544  case TBLOCK_INPROGRESS:
3545  s->blockState = TBLOCK_END;
3546  result = true;
3547  break;
3548 
3549  /*
3550  * In an implicit transaction block, commit, but issue a warning
3551  * because there was no explicit BEGIN before this.
3552  */
3554  ereport(WARNING,
3555  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3556  errmsg("there is no transaction in progress")));
3557  s->blockState = TBLOCK_END;
3558  result = true;
3559  break;
3560 
3561  /*
3562  * We are in a failed transaction block. Tell
3563  * CommitTransactionCommand it's time to exit the block.
3564  */
3565  case TBLOCK_ABORT:
3567  break;
3568 
3569  /*
3570  * We are in a live subtransaction block. Set up to subcommit all
3571  * open subtransactions and then commit the main transaction.
3572  */
3573  case TBLOCK_SUBINPROGRESS:
3574  while (s->parent != NULL)
3575  {
3576  if (s->blockState == TBLOCK_SUBINPROGRESS)
3578  else
3579  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3581  s = s->parent;
3582  }
3583  if (s->blockState == TBLOCK_INPROGRESS)
3584  s->blockState = TBLOCK_END;
3585  else
3586  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3588  result = true;
3589  break;
3590 
3591  /*
3592  * Here we are inside an aborted subtransaction. Treat the COMMIT
3593  * as ROLLBACK: set up to abort everything and exit the main
3594  * transaction.
3595  */
3596  case TBLOCK_SUBABORT:
3597  while (s->parent != NULL)
3598  {
3599  if (s->blockState == TBLOCK_SUBINPROGRESS)
3601  else if (s->blockState == TBLOCK_SUBABORT)
3603  else
3604  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3606  s = s->parent;
3607  }
3608  if (s->blockState == TBLOCK_INPROGRESS)
3610  else if (s->blockState == TBLOCK_ABORT)
3612  else
3613  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3615  break;
3616 
3617  /*
3618  * The user issued COMMIT when not inside a transaction. Issue a
3619  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3620  * CommitTransactionCommand() will then close the transaction and
3621  * put us back into the default state.
3622  */
3623  case TBLOCK_STARTED:
3624  ereport(WARNING,
3625  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3626  errmsg("there is no transaction in progress")));
3627  result = true;
3628  break;
3629 
3630  /*
3631  * The user issued a COMMIT that somehow ran inside a parallel
3632  * worker. We can't cope with that.
3633  */
3635  ereport(FATAL,
3636  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3637  errmsg("cannot commit during a parallel operation")));
3638  break;
3639 
3640  /* These cases are invalid. */
3641  case TBLOCK_DEFAULT:
3642  case TBLOCK_BEGIN:
3643  case TBLOCK_SUBBEGIN:
3644  case TBLOCK_END:
3645  case TBLOCK_SUBRELEASE:
3646  case TBLOCK_SUBCOMMIT:
3647  case TBLOCK_ABORT_END:
3648  case TBLOCK_SUBABORT_END:
3649  case TBLOCK_ABORT_PENDING:
3651  case TBLOCK_SUBRESTART:
3653  case TBLOCK_PREPARE:
3654  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3656  break;
3657  }
3658 
3659  return result;
3660 }
3661 
3662 /*
3663  * UserAbortTransactionBlock
3664  * This executes a ROLLBACK command.
3665  *
3666  * As above, we don't actually do anything here except change blockState.
3667  */
3668 void
3670 {
3672 
3673  switch (s->blockState)
3674  {
3675  /*
3676  * We are inside a transaction block and we got a ROLLBACK command
3677  * from the user, so tell CommitTransactionCommand to abort and
3678  * exit the transaction block.
3679  */
3680  case TBLOCK_INPROGRESS:
3682  break;
3683 
3684  /*
3685  * We are inside a failed transaction block and we got a ROLLBACK
3686  * command from the user. Abort processing is already done, so
3687  * CommitTransactionCommand just has to cleanup and go back to
3688  * idle state.
3689  */
3690  case TBLOCK_ABORT:
3692  break;
3693 
3694  /*
3695  * We are inside a subtransaction. Mark everything up to top
3696  * level as exitable.
3697  */
3698  case TBLOCK_SUBINPROGRESS:
3699  case TBLOCK_SUBABORT:
3700  while (s->parent != NULL)
3701  {
3702  if (s->blockState == TBLOCK_SUBINPROGRESS)
3704  else if (s->blockState == TBLOCK_SUBABORT)
3706  else
3707  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3709  s = s->parent;
3710  }
3711  if (s->blockState == TBLOCK_INPROGRESS)
3713  else if (s->blockState == TBLOCK_ABORT)
3715  else
3716  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3718  break;
3719 
3720  /*
3721  * The user issued ABORT when not inside a transaction. Issue a
3722  * WARNING and go to abort state. The upcoming call to
3723  * CommitTransactionCommand() will then put us back into the
3724  * default state.
3725  *
3726  * We do the same thing with ABORT inside an implicit transaction,
3727  * although in this case we might be rolling back actual database
3728  * state changes. (It's debatable whether we should issue a
3729  * WARNING in this case, but we have done so historically.)
3730  */
3731  case TBLOCK_STARTED:
3733  ereport(WARNING,
3734  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3735  errmsg("there is no transaction in progress")));
3737  break;
3738 
3739  /*
3740  * The user issued an ABORT that somehow ran inside a parallel
3741  * worker. We can't cope with that.
3742  */
3744  ereport(FATAL,
3745  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3746  errmsg("cannot abort during a parallel operation")));
3747  break;
3748 
3749  /* These cases are invalid. */
3750  case TBLOCK_DEFAULT:
3751  case TBLOCK_BEGIN:
3752  case TBLOCK_SUBBEGIN:
3753  case TBLOCK_END:
3754  case TBLOCK_SUBRELEASE:
3755  case TBLOCK_SUBCOMMIT:
3756  case TBLOCK_ABORT_END:
3757  case TBLOCK_SUBABORT_END:
3758  case TBLOCK_ABORT_PENDING:
3760  case TBLOCK_SUBRESTART:
3762  case TBLOCK_PREPARE:
3763  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3765  break;
3766  }
3767 }
3768 
3769 /*
3770  * BeginImplicitTransactionBlock
3771  * Start an implicit transaction block if we're not already in one.
3772  *
3773  * Unlike BeginTransactionBlock, this is called directly from the main loop
3774  * in postgres.c, not within a Portal. So we can just change blockState
3775  * without a lot of ceremony. We do not expect caller to do
3776  * CommitTransactionCommand/StartTransactionCommand.
3777  */
3778 void
3780 {
3782 
3783  /*
3784  * If we are in STARTED state (that is, no transaction block is open),
3785  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3786  * block.
3787  *
3788  * For caller convenience, we consider all other transaction states as
3789  * legal here; otherwise the caller would need its own state check, which
3790  * seems rather pointless.
3791  */
3792  if (s->blockState == TBLOCK_STARTED)
3794 }
3795 
3796 /*
3797  * EndImplicitTransactionBlock
3798  * End an implicit transaction block, if we're in one.
3799  *
3800  * Like EndTransactionBlock, we just make any needed blockState change here.
3801  * The real work will be done in the upcoming CommitTransactionCommand().
3802  */
3803 void
3805 {
3807 
3808  /*
3809  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3810  * allowing CommitTransactionCommand to commit whatever happened during
3811  * the implicit transaction block as though it were a single statement.
3812  *
3813  * For caller convenience, we consider all other transaction states as
3814  * legal here; otherwise the caller would need its own state check, which
3815  * seems rather pointless.
3816  */
3819 }
3820 
3821 /*
3822  * DefineSavepoint
3823  * This executes a SAVEPOINT command.
3824  */
3825 void
3826 DefineSavepoint(const char *name)
3827 {
3829 
3830  /*
3831  * Workers synchronize transaction state at the beginning of each parallel
3832  * operation, so we can't account for new subtransactions after that
3833  * point. (Note that this check will certainly error out if s->blockState
3834  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3835  * below.)
3836  */
3837  if (IsInParallelMode())
3838  ereport(ERROR,
3839  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3840  errmsg("cannot define savepoints during a parallel operation")));
3841 
3842  switch (s->blockState)
3843  {
3844  case TBLOCK_INPROGRESS:
3845  case TBLOCK_SUBINPROGRESS:
3846  /* Normal subtransaction start */
3847  PushTransaction();
3848  s = CurrentTransactionState; /* changed by push */
3849 
3850  /*
3851  * Savepoint names, like the TransactionState block itself, live
3852  * in TopTransactionContext.
3853  */
3854  if (name)
3856  break;
3857 
3858  /*
3859  * We disallow savepoint commands in implicit transaction blocks.
3860  * There would be no great difficulty in allowing them so far as
3861  * this module is concerned, but a savepoint seems inconsistent
3862  * with exec_simple_query's behavior of abandoning the whole query
3863  * string upon error. Also, the point of an implicit transaction
3864  * block (as opposed to a regular one) is to automatically close
3865  * after an error, so it's hard to see how a savepoint would fit
3866  * into that.
3867  *
3868  * The error messages for this are phrased as if there were no
3869  * active transaction block at all, which is historical but
3870  * perhaps could be improved.
3871  */
3873  ereport(ERROR,
3874  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3875  /* translator: %s represents an SQL statement name */
3876  errmsg("%s can only be used in transaction blocks",
3877  "SAVEPOINT")));
3878  break;
3879 
3880  /* These cases are invalid. */
3881  case TBLOCK_DEFAULT:
3882  case TBLOCK_STARTED:
3883  case TBLOCK_BEGIN:
3885  case TBLOCK_SUBBEGIN:
3886  case TBLOCK_END:
3887  case TBLOCK_SUBRELEASE:
3888  case TBLOCK_SUBCOMMIT:
3889  case TBLOCK_ABORT:
3890  case TBLOCK_SUBABORT:
3891  case TBLOCK_ABORT_END:
3892  case TBLOCK_SUBABORT_END:
3893  case TBLOCK_ABORT_PENDING:
3895  case TBLOCK_SUBRESTART:
3897  case TBLOCK_PREPARE:
3898  elog(FATAL, "DefineSavepoint: unexpected state %s",
3900  break;
3901  }
3902 }
3903 
3904 /*
3905  * ReleaseSavepoint
3906  * This executes a RELEASE command.
3907  *
3908  * As above, we don't actually do anything here except change blockState.
3909  */
3910 void
3912 {
3914  TransactionState target,
3915  xact;
3916  ListCell *cell;
3917  char *name = NULL;
3918 
3919  /*
3920  * Workers synchronize transaction state at the beginning of each parallel
3921  * operation, so we can't account for transaction state change after that
3922  * point. (Note that this check will certainly error out if s->blockState
3923  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3924  * below.)
3925  */
3926  if (IsInParallelMode())
3927  ereport(ERROR,
3928  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3929  errmsg("cannot release savepoints during a parallel operation")));
3930 
3931  switch (s->blockState)
3932  {
3933  /*
3934  * We can't release a savepoint if there is no savepoint defined.
3935  */
3936  case TBLOCK_INPROGRESS:
3937  ereport(ERROR,
3938  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3939  errmsg("no such savepoint")));
3940  break;
3941 
3943  /* See comment about implicit transactions in DefineSavepoint */
3944  ereport(ERROR,
3945  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3946  /* translator: %s represents an SQL statement name */
3947  errmsg("%s can only be used in transaction blocks",
3948  "RELEASE SAVEPOINT")));
3949  break;
3950 
3951  /*
3952  * We are in a non-aborted subtransaction. This is the only valid
3953  * case.
3954  */
3955  case TBLOCK_SUBINPROGRESS:
3956  break;
3957 
3958  /* These cases are invalid. */
3959  case TBLOCK_DEFAULT:
3960  case TBLOCK_STARTED:
3961  case TBLOCK_BEGIN:
3963  case TBLOCK_SUBBEGIN:
3964  case TBLOCK_END:
3965  case TBLOCK_SUBRELEASE:
3966  case TBLOCK_SUBCOMMIT:
3967  case TBLOCK_ABORT:
3968  case TBLOCK_SUBABORT:
3969  case TBLOCK_ABORT_END:
3970  case TBLOCK_SUBABORT_END:
3971  case TBLOCK_ABORT_PENDING:
3973  case TBLOCK_SUBRESTART:
3975  case TBLOCK_PREPARE:
3976  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3978  break;
3979  }
3980 
3981  foreach(cell, options)
3982  {
3983  DefElem *elem = lfirst(cell);
3984 
3985  if (strcmp(elem->defname, "savepoint_name") == 0)
3986  name = strVal(elem->arg);
3987  }
3988 
3989  Assert(PointerIsValid(name));
3990 
3991  for (target = s; PointerIsValid(target); target = target->parent)
3992  {
3993  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3994  break;
3995  }
3996 
3997  if (!PointerIsValid(target))
3998  ereport(ERROR,
3999  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4000  errmsg("no such savepoint")));
4001 
4002  /* disallow crossing savepoint level boundaries */
4003  if (target->savepointLevel != s->savepointLevel)
4004  ereport(ERROR,
4005  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4006  errmsg("no such savepoint")));
4007 
4008  /*
4009  * Mark "commit pending" all subtransactions up to the target
4010  * subtransaction. The actual commits will happen when control gets to
4011  * CommitTransactionCommand.
4012  */
4013  xact = CurrentTransactionState;
4014  for (;;)
4015  {
4017  xact->blockState = TBLOCK_SUBRELEASE;
4018  if (xact == target)
4019  break;
4020  xact = xact->parent;
4021  Assert(PointerIsValid(xact));
4022  }
4023 }
4024 
4025 /*
4026  * RollbackToSavepoint
4027  * This executes a ROLLBACK TO <savepoint> command.
4028  *
4029  * As above, we don't actually do anything here except change blockState.
4030  */
4031 void
4033 {
4035  TransactionState target,
4036  xact;
4037  ListCell *cell;
4038  char *name = NULL;
4039 
4040  /*
4041  * Workers synchronize transaction state at the beginning of each parallel
4042  * operation, so we can't account for transaction state change after that
4043  * point. (Note that this check will certainly error out if s->blockState
4044  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4045  * below.)
4046  */
4047  if (IsInParallelMode())
4048  ereport(ERROR,
4049  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4050  errmsg("cannot rollback to savepoints during a parallel operation")));
4051 
4052  switch (s->blockState)
4053  {
4054  /*
4055  * We can't rollback to a savepoint if there is no savepoint
4056  * defined.
4057  */
4058  case TBLOCK_INPROGRESS:
4059  case TBLOCK_ABORT:
4060  ereport(ERROR,
4061  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4062  errmsg("no such savepoint")));
4063  break;
4064 
4066  /* See comment about implicit transactions in DefineSavepoint */
4067  ereport(ERROR,
4068  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4069  /* translator: %s represents an SQL statement name */
4070  errmsg("%s can only be used in transaction blocks",
4071  "ROLLBACK TO SAVEPOINT")));
4072  break;
4073 
4074  /*
4075  * There is at least one savepoint, so proceed.
4076  */
4077  case TBLOCK_SUBINPROGRESS:
4078  case TBLOCK_SUBABORT:
4079  break;
4080 
4081  /* These cases are invalid. */
4082  case TBLOCK_DEFAULT:
4083  case TBLOCK_STARTED:
4084  case TBLOCK_BEGIN:
4086  case TBLOCK_SUBBEGIN:
4087  case TBLOCK_END:
4088  case TBLOCK_SUBRELEASE:
4089  case TBLOCK_SUBCOMMIT:
4090  case TBLOCK_ABORT_END:
4091  case TBLOCK_SUBABORT_END:
4092  case TBLOCK_ABORT_PENDING:
4094  case TBLOCK_SUBRESTART:
4096  case TBLOCK_PREPARE:
4097  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4099  break;
4100  }
4101 
4102  foreach(cell, options)
4103  {
4104  DefElem *elem = lfirst(cell);
4105 
4106  if (strcmp(elem->defname, "savepoint_name") == 0)
4107  name = strVal(elem->arg);
4108  }
4109 
4110  Assert(PointerIsValid(name));
4111 
4112  for (target = s; PointerIsValid(target); target = target->parent)
4113  {
4114  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4115  break;
4116  }
4117 
4118  if (!PointerIsValid(target))
4119  ereport(ERROR,
4120  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4121  errmsg("no such savepoint")));
4122 
4123  /* disallow crossing savepoint level boundaries */
4124  if (target->savepointLevel != s->savepointLevel)
4125  ereport(ERROR,
4126  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4127  errmsg("no such savepoint")));
4128 
4129  /*
4130  * Mark "abort pending" all subtransactions up to the target
4131  * subtransaction. The actual aborts will happen when control gets to
4132  * CommitTransactionCommand.
4133  */
4134  xact = CurrentTransactionState;
4135  for (;;)
4136  {
4137  if (xact == target)
4138  break;
4139  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4141  else if (xact->blockState == TBLOCK_SUBABORT)
4143  else
4144  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4146  xact = xact->parent;
4147  Assert(PointerIsValid(xact));
4148  }
4149 
4150  /* And mark the target as "restart pending" */
4151  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4152  xact->blockState = TBLOCK_SUBRESTART;
4153  else if (xact->blockState == TBLOCK_SUBABORT)
4155  else
4156  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4158 }
4159 
4160 /*
4161  * BeginInternalSubTransaction
4162  * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4163  * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_END, and TBLOCK_PREPARE states,
4164  * and therefore it can safely be used in functions that might be called
4165  * when not inside a BEGIN block or when running deferred triggers at
4166  * COMMIT/PREPARE time. Also, it automatically does
4167  * CommitTransactionCommand/StartTransactionCommand instead of expecting
4168  * the caller to do it.
4169  */
4170 void
4172 {
4174 
4175  /*
4176  * Workers synchronize transaction state at the beginning of each parallel
4177  * operation, so we can't account for new subtransactions after that
4178  * point. We might be able to make an exception for the type of
4179  * subtransaction established by this function, which is typically used in
4180  * contexts where we're going to release or roll back the subtransaction
4181  * before proceeding further, so that no enduring change to the
4182  * transaction state occurs. For now, however, we prohibit this case along
4183  * with all the others.
4184  */
4185  if (IsInParallelMode())
4186  ereport(ERROR,
4187  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4188  errmsg("cannot start subtransactions during a parallel operation")));
4189 
4190  switch (s->blockState)
4191  {
4192  case TBLOCK_STARTED:
4193  case TBLOCK_INPROGRESS:
4195  case TBLOCK_END:
4196  case TBLOCK_PREPARE:
4197  case TBLOCK_SUBINPROGRESS:
4198  /* Normal subtransaction start */
4199  PushTransaction();
4200  s = CurrentTransactionState; /* changed by push */
4201 
4202  /*
4203  * Savepoint names, like the TransactionState block itself, live
4204  * in TopTransactionContext.
4205  */
4206  if (name)
4208  break;
4209 
4210  /* These cases are invalid. */
4211  case TBLOCK_DEFAULT:
4212  case TBLOCK_BEGIN:
4214  case TBLOCK_SUBBEGIN:
4215  case TBLOCK_SUBRELEASE:
4216  case TBLOCK_SUBCOMMIT:
4217  case TBLOCK_ABORT:
4218  case TBLOCK_SUBABORT:
4219  case TBLOCK_ABORT_END:
4220  case TBLOCK_SUBABORT_END:
4221  case TBLOCK_ABORT_PENDING:
4223  case TBLOCK_SUBRESTART:
4225  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4227  break;
4228  }
4229 
4232 }
4233 
4234 /*
4235  * ReleaseCurrentSubTransaction
4236  *
4237  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4238  * savepoint name (if any).
4239  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4240  */
4241 void
4243 {
4245 
4246  /*
4247  * Workers synchronize transaction state at the beginning of each parallel
4248  * operation, so we can't account for commit of subtransactions after that
4249  * point. This should not happen anyway. Code calling this would
4250  * typically have called BeginInternalSubTransaction() first, failing
4251  * there.
4252  */
4253  if (IsInParallelMode())
4254  ereport(ERROR,
4255  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4256  errmsg("cannot commit subtransactions during a parallel operation")));
4257 
4258  if (s->blockState != TBLOCK_SUBINPROGRESS)
4259  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4261  Assert(s->state == TRANS_INPROGRESS);
4264  s = CurrentTransactionState; /* changed by pop */
4265  Assert(s->state == TRANS_INPROGRESS);
4266 }
4267 
4268 /*
4269  * RollbackAndReleaseCurrentSubTransaction
4270  *
4271  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4272  * of its savepoint name (if any).
4273  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4274  */
4275 void
4277 {
4279 
4280  /*
4281  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4282  * during parallel operations. That's because we may be in the master,
4283  * recovering from an error thrown while we were in parallel mode. We
4284  * won't reach here in a worker, because BeginInternalSubTransaction()
4285  * will have failed.
4286  */
4287 
4288  switch (s->blockState)
4289  {
4290  /* Must be in a subtransaction */
4291  case TBLOCK_SUBINPROGRESS:
4292  case TBLOCK_SUBABORT:
4293  break;
4294 
4295  /* These cases are invalid. */
4296  case TBLOCK_DEFAULT:
4297  case TBLOCK_STARTED:
4298  case TBLOCK_BEGIN:
4301  case TBLOCK_SUBBEGIN:
4302  case TBLOCK_INPROGRESS:
4303  case TBLOCK_END:
4304  case TBLOCK_SUBRELEASE:
4305  case TBLOCK_SUBCOMMIT:
4306  case TBLOCK_ABORT:
4307  case TBLOCK_ABORT_END:
4308  case TBLOCK_SUBABORT_END:
4309  case TBLOCK_ABORT_PENDING:
4311  case TBLOCK_SUBRESTART:
4313  case TBLOCK_PREPARE:
4314  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4316  break;
4317  }
4318 
4319  /*
4320  * Abort the current subtransaction, if needed.
4321  */
4322  if (s->blockState == TBLOCK_SUBINPROGRESS)
4324 
4325  /* And clean it up, too */
4327 
4328  s = CurrentTransactionState; /* changed by pop */
4330  s->blockState == TBLOCK_INPROGRESS ||
4332  s->blockState == TBLOCK_STARTED);
4333 }
4334 
4335 /*
4336  * AbortOutOfAnyTransaction
4337  *
4338  * This routine is provided for error recovery purposes. It aborts any
4339  * active transaction or transaction block, leaving the system in a known
4340  * idle state.
4341  */
4342 void
4344 {
4346 
4347  /* Ensure we're not running in a doomed memory context */
4348  AtAbort_Memory();
4349 
4350  /*
4351  * Get out of any transaction or nested transaction
4352  */
4353  do
4354  {
4355  switch (s->blockState)
4356  {
4357  case TBLOCK_DEFAULT:
4358  if (s->state == TRANS_DEFAULT)
4359  {
4360  /* Not in a transaction, do nothing */
4361  }
4362  else
4363  {
4364  /*
4365  * We can get here after an error during transaction start
4366  * (state will be TRANS_START). Need to clean up the
4367  * incompletely started transaction. First, adjust the
4368  * low-level state to suppress warning message from
4369  * AbortTransaction.
4370  */
4371  if (s->state == TRANS_START)
4372  s->state = TRANS_INPROGRESS;
4373  AbortTransaction();
4375  }
4376  break;
4377  case TBLOCK_STARTED:
4378  case TBLOCK_BEGIN:
4379  case TBLOCK_INPROGRESS:
4382  case TBLOCK_END:
4383  case TBLOCK_ABORT_PENDING:
4384  case TBLOCK_PREPARE:
4385  /* In a transaction, so clean up */
4386  AbortTransaction();
4389  break;
4390  case TBLOCK_ABORT:
4391  case TBLOCK_ABORT_END:
4392 
4393  /*
4394  * AbortTransaction is already done, still need Cleanup.
4395  * However, if we failed partway through running ROLLBACK,
4396  * there will be an active portal running that command, which
4397  * we need to shut down before doing CleanupTransaction.
4398  */
4399  AtAbort_Portals();
4402  break;
4403 
4404  /*
4405  * In a subtransaction, so clean it up and abort parent too
4406  */
4407  case TBLOCK_SUBBEGIN:
4408  case TBLOCK_SUBINPROGRESS:
4409  case TBLOCK_SUBRELEASE:
4410  case TBLOCK_SUBCOMMIT:
4412  case TBLOCK_SUBRESTART:
4415  s = CurrentTransactionState; /* changed by pop */
4416  break;
4417 
4418  case TBLOCK_SUBABORT:
4419  case TBLOCK_SUBABORT_END:
4421  /* As above, but AbortSubTransaction already done */
4422  if (s->curTransactionOwner)
4423  {
4424  /* As in TBLOCK_ABORT, might have a live portal to zap */
4429  }
4431  s = CurrentTransactionState; /* changed by pop */
4432  break;
4433  }
4434  } while (s->blockState != TBLOCK_DEFAULT);
4435 
4436  /* Should be out of all subxacts now */
4437  Assert(s->parent == NULL);
4438 
4439  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4440  AtCleanup_Memory();
4441 }
4442 
4443 /*
4444  * IsTransactionBlock --- are we within a transaction block?
4445  */
4446 bool
4448 {
4450 
4452  return false;
4453 
4454  return true;
4455 }
4456 
4457 /*
4458  * IsTransactionOrTransactionBlock --- are we within either a transaction
4459  * or a transaction block? (The backend is only really "idle" when this
4460  * returns false.)
4461  *
4462  * This should match up with IsTransactionBlock and IsTransactionState.
4463  */
4464 bool
4466 {
4468 
4469  if (s->blockState == TBLOCK_DEFAULT)
4470  return false;
4471 
4472  return true;
4473 }
4474 
4475 /*
4476  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
4477  */
4478 char
4480 {
4482 
4483  switch (s->blockState)
4484  {
4485  case TBLOCK_DEFAULT:
4486  case TBLOCK_STARTED:
4487  return 'I'; /* idle --- not in transaction */
4488  case TBLOCK_BEGIN:
4489  case TBLOCK_SUBBEGIN:
4490  case TBLOCK_INPROGRESS:
4493  case TBLOCK_SUBINPROGRESS:
4494  case TBLOCK_END:
4495  case TBLOCK_SUBRELEASE:
4496  case TBLOCK_SUBCOMMIT:
4497  case TBLOCK_PREPARE:
4498  return 'T'; /* in transaction */
4499  case TBLOCK_ABORT:
4500  case TBLOCK_SUBABORT:
4501  case TBLOCK_ABORT_END:
4502  case TBLOCK_SUBABORT_END:
4503  case TBLOCK_ABORT_PENDING:
4505  case TBLOCK_SUBRESTART:
4507  return 'E'; /* in failed transaction */
4508  }
4509 
4510  /* should never get here */
4511  elog(FATAL, "invalid transaction block state: %s",
4513  return 0; /* keep compiler quiet */
4514 }
4515 
4516 /*
4517  * IsSubTransaction
4518  */
4519 bool
4521 {
4523 
4524  if (s->nestingLevel >= 2)
4525  return true;
4526 
4527  return false;
4528 }
4529 
4530 /*
4531  * StartSubTransaction
4532  *
4533  * If you're wondering why this is separate from PushTransaction: it's because
4534  * we can't conveniently do this stuff right inside DefineSavepoint. The
4535  * SAVEPOINT utility command will be executed inside a Portal, and if we
4536  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4537  * the Portal will undo those settings. So we make DefineSavepoint just
4538  * push a dummy transaction block, and when control returns to the main
4539  * idle loop, CommitTransactionCommand will be called, and we'll come here
4540  * to finish starting the subtransaction.
4541  */
4542 static void
4544 {
4546 
4547  if (s->state != TRANS_DEFAULT)
4548  elog(WARNING, "StartSubTransaction while in %s state",
4550 
4551  s->state = TRANS_START;
4552 
4553  /*
4554  * Initialize subsystems for new subtransaction
4555  *
4556  * must initialize resource-management stuff first
4557  */
4562 
4563  s->state = TRANS_INPROGRESS;
4564 
4565  /*
4566  * Call start-of-subxact callbacks
4567  */
4569  s->parent->subTransactionId);
4570 
4571  ShowTransactionState("StartSubTransaction");
4572 }
4573 
4574 /*
4575  * CommitSubTransaction
4576  *
4577  * The caller has to make sure to always reassign CurrentTransactionState
4578  * if it has a local pointer to it after calling this function.
4579  */
4580 static void
4582 {
4584 
4585  ShowTransactionState("CommitSubTransaction");
4586 
4587  if (s->state != TRANS_INPROGRESS)
4588  elog(WARNING, "CommitSubTransaction while in %s state",
4590 
4591  /* Pre-commit processing goes here */
4592 
4594  s->parent->subTransactionId);
4595 
4596  /* If in parallel mode, clean up workers and exit parallel mode. */
4597  if (IsInParallelMode())
4598  {
4600  s->parallelModeLevel = 0;
4601  }
4602 
4603  /* Do the actual "commit", such as it is */
4604  s->state = TRANS_COMMIT;
4605 
4606  /* Must CCI to ensure commands of subtransaction are seen as done */
4608 
4609  /*
4610  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4611  * perform that step, if required, as part of the atomic update of the
4612  * whole transaction tree at top level commit or abort.
4613  */
4614 
4615  /* Post-commit cleanup */
4618  AfterTriggerEndSubXact(true);
4623  s->parent->subTransactionId);
4625 
4627  s->parent->subTransactionId);
4628 
4631  true, false);
4633  s->parent->subTransactionId);
4634  AtEOSubXact_Inval(true);
4635  AtSubCommit_smgr();
4636 
4637  /*
4638  * The only lock we actually release here is the subtransaction XID lock.
4639  */
4643 
4644  /*
4645  * Other locks should get transferred to their parent resource owner.
4646  */
4649  true, false);
4652  true, false);
4653 
4654  AtEOXact_GUC(true, s->gucNestLevel);
4657  s->parent->subTransactionId);
4659  s->parent->subTransactionId);
4661  s->parent->subTransactionId);
4663  AtEOSubXact_PgStat(true, s->nestingLevel);
4665 
4666  /*
4667  * We need to restore the upper transaction's read-only state, in case the
4668  * upper is read-write while the child is read-only; GUC will incorrectly
4669  * think it should leave the child state in place.
4670  */
4672 
4676  s->curTransactionOwner = NULL;
4677 
4679 
4680  s->state = TRANS_DEFAULT;
4681 
4682  PopTransaction();
4683 }
4684 
4685 /*
4686  * AbortSubTransaction
4687  */
4688 static void
4690 {
4692 
4693  /* Prevent cancel/die interrupt while cleaning up */
4694  HOLD_INTERRUPTS();
4695 
4696  /* Make sure we have a valid memory context and resource owner */
4699 
4700  /*
4701  * Release any LW locks we might be holding as quickly as possible.
4702  * (Regular locks, however, must be held till we finish aborting.)
4703  * Releasing LW locks is critical since we might try to grab them again
4704  * while cleaning up!
4705  *
4706  * FIXME This may be incorrect --- Are there some locks we should keep?
4707  * Buffer locks, for example? I don't think so but I'm not sure.
4708  */
4709  LWLockReleaseAll();
4710 
4713  AbortBufferIO();
4714  UnlockBuffers();
4715 
4716  /* Reset WAL record construction state */
4718 
4719  /*
4720  * Also clean up any open wait for lock, since the lock manager will choke
4721  * if we try to wait for another lock before doing this.
4722  */
4723  LockErrorCleanup();
4724 
4725  /*
4726  * If any timeout events are still active, make sure the timeout interrupt
4727  * is scheduled. This covers possible loss of a timeout interrupt due to
4728  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4729  * We delay this till after LockErrorCleanup so that we don't uselessly
4730  * reschedule lock or deadlock check timeouts.
4731  */
4733 
4734  /*
4735  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4736  * handler. We do this fairly early in the sequence so that the timeout
4737  * infrastructure will be functional if needed while aborting.
4738  */
4740 
4741  /*
4742  * check the current transaction state
4743  */
4744  ShowTransactionState("AbortSubTransaction");
4745 
4746  if (s->state != TRANS_INPROGRESS)
4747  elog(WARNING, "AbortSubTransaction while in %s state",
4749 
4750  s->state = TRANS_ABORT;
4751 
4752  /*
4753  * Reset user ID which might have been changed transiently. (See notes in
4754  * AbortTransaction.)
4755  */
4757 
4758  /* Exit from parallel mode, if necessary. */
4759  if (IsInParallelMode())
4760  {
4762  s->parallelModeLevel = 0;
4763  }
4764 
4765  /*
4766  * We can skip all this stuff if the subxact failed before creating a
4767  * ResourceOwner...
4768  */
4769  if (s->curTransactionOwner)
4770  {
4771  AfterTriggerEndSubXact(false);
4777  s->parent->subTransactionId);
4779 
4780  /* Advertise the fact that we aborted in pg_xact. */
4781  (void) RecordTransactionAbort(true);
4782 
4783  /* Post-abort cleanup */
4786 
4788  s->parent->subTransactionId);
4789 
4792  false, false);
4794  s->parent->subTransactionId);
4795  AtEOSubXact_Inval(false);
4798  false, false);
4801  false, false);
4802  AtSubAbort_smgr();
4803 
4804  AtEOXact_GUC(false, s->gucNestLevel);
4805  AtEOSubXact_SPI(false, s->subTransactionId);
4807  s->parent->subTransactionId);
4809  s->parent->subTransactionId);
4811  s->parent->subTransactionId);
4813  AtEOSubXact_PgStat(false, s->nestingLevel);
4815  }
4816 
4817  /*
4818  * Restore the upper transaction's read-only state, too. This should be
4819  * redundant with GUC's cleanup but we may as well do it for consistency
4820  * with the commit case.
4821  */
4823 
4825 }
4826 
4827 /*
4828  * CleanupSubTransaction
4829  *
4830  * The caller has to make sure to always reassign CurrentTransactionState
4831  * if it has a local pointer to it after calling this function.
4832  */
4833 static void
4835 {
4837 
4838  ShowTransactionState("CleanupSubTransaction");
4839 
4840  if (s->state != TRANS_ABORT)
4841  elog(WARNING, "CleanupSubTransaction while in %s state",
4843 
4845 
4848  if (s->curTransactionOwner)
4850  s->curTransactionOwner = NULL;
4851 
4853 
4854  s->state = TRANS_DEFAULT;
4855 
4856  PopTransaction();
4857 }
4858 
4859 /*
4860  * PushTransaction
4861  * Create transaction state stack entry for a subtransaction
4862  *
4863  * The caller has to make sure to always reassign CurrentTransactionState
4864  * if it has a local pointer to it after calling this function.
4865  */
4866 static void
4868 {
4870  TransactionState s;
4871 
4872  /*
4873  * We keep subtransaction state nodes in TopTransactionContext.
4874  */
4875  s = (TransactionState)
4877  sizeof(TransactionStateData));
4878 
4879  /*
4880  * Assign a subtransaction ID, watching out for counter wraparound.
4881  */
4884  {
4886  pfree(s);
4887  ereport(ERROR,
4888  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4889  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4890  }
4891 
4892  /*
4893  * We can now stack a minimally valid subtransaction without fear of
4894  * failure.
4895  */
4896  s->transactionId = InvalidTransactionId; /* until assigned */
4898  s->parent = p;
4899  s->nestingLevel = p->nestingLevel + 1;
4902  s->state = TRANS_DEFAULT;
4906  s->parallelModeLevel = 0;
4907 
4908  CurrentTransactionState = s;
4909 
4910  /*
4911  * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4912  * with the subtransaction from here on out; in particular they should not
4913  * assume that it necessarily has a transaction context, resource owner,
4914  * or XID.
4915  */
4916 }
4917 
4918 /*
4919  * PopTransaction
4920  * Pop back to parent transaction state
4921  *
4922  * The caller has to make sure to always reassign CurrentTransactionState
4923  * if it has a local pointer to it after calling this function.
4924  */
4925 static void
4927 {
4929 
4930  if (s->state != TRANS_DEFAULT)
4931  elog(WARNING, "PopTransaction while in %s state",
4933 
4934  if (s->parent == NULL)
4935  elog(FATAL, "PopTransaction with no parent");
4936 
4937  CurrentTransactionState = s->parent;
4938 
4939  /* Let's just make sure CurTransactionContext is good */
4942 
4943  /* Ditto for ResourceOwner links */
4946 
4947  /* Free the old child structure */
4948  if (s->name)
4949  pfree(s->name);
4950  pfree(s);
4951 }
4952 
4953 /*
4954  * EstimateTransactionStateSpace
4955  * Estimate the amount of space that will be needed by
4956  * SerializeTransactionState. It would be OK to overestimate slightly,
4957  * but it's simple for us to work out the precise value, so we do.
4958  */
4959 Size
4961 {
4962  TransactionState s;
4963  Size nxids = 6; /* iso level, deferrable, top & current XID,
4964  * command counter, XID count */
4965 
4966  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4967  {
4969  nxids = add_size(nxids, 1);
4970  nxids = add_size(nxids, s->nChildXids);
4971  }
4972 
4973  nxids = add_size(nxids, nParallelCurrentXids);
4974  return mul_size(nxids, sizeof(TransactionId));
4975 }
4976 
4977 /*
4978  * SerializeTransactionState
4979  * Write out relevant details of our transaction state that will be
4980  * needed by a parallel worker.
4981  *
4982  * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
4983  * associated with this transaction. The first eight bytes of the result
4984  * contain XactDeferrable and XactIsoLevel; the next twelve bytes contain the
4985  * XID of the top-level transaction, the XID of the current transaction
4986  * (or, in each case, InvalidTransactionId if none), and the current command
4987  * counter. After that, the next 4 bytes contain a count of how many
4988  * additional XIDs follow; this is followed by all of those XIDs one after
4989  * another. We emit the XIDs in sorted order for the convenience of the
4990  * receiving process.
4991  */
4992 void
4993 SerializeTransactionState(Size maxsize, char *start_address)
4994 {
4995  TransactionState s;
4996  Size nxids = 0;
4997  Size i = 0;
4998  Size c = 0;
4999  TransactionId *workspace;
5000  TransactionId *result = (TransactionId *) start_address;
5001 
5002  result[c++] = (TransactionId) XactIsoLevel;
5003  result[c++] = (TransactionId) XactDeferrable;
5004  result[c++] = XactTopTransactionId;
5005  result[c++] = CurrentTransactionState->transactionId;
5006  result[c++] = (TransactionId) currentCommandId;
5007  Assert(maxsize >= c * sizeof(TransactionId));
5008 
5009  /*
5010  * If we're running in a parallel worker and launching a parallel worker
5011  * of our own, we can just pass along the information that was passed to
5012  * us.
5013  */
5014  if (nParallelCurrentXids > 0)
5015  {
5016  result[c++] = nParallelCurrentXids;
5017  Assert(maxsize >= (nParallelCurrentXids + c) * sizeof(TransactionId));
5018  memcpy(&result[c], ParallelCurrentXids,
5020  return;
5021  }
5022 
5023  /*
5024  * OK, we need to generate a sorted list of XIDs that our workers should
5025  * view as current. First, figure out how many there are.
5026  */
5027  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5028  {
5030  nxids = add_size(nxids, 1);
5031  nxids = add_size(nxids, s->nChildXids);
5032  }
5033  Assert((c + 1 + nxids) * sizeof(TransactionId) <= maxsize);
5034 
5035  /* Copy them to our scratch space. */
5036  workspace = palloc(nxids * sizeof(TransactionId));
5037  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5038  {
5040  workspace[i++] = s->transactionId;
5041  memcpy(&workspace[i], s->childXids,
5042  s->nChildXids * sizeof(TransactionId));
5043  i += s->nChildXids;
5044  }
5045  Assert(i == nxids);
5046 
5047  /* Sort them. */
5048  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5049 
5050  /* Copy data into output area. */
5051  result[c++] = (TransactionId) nxids;
5052  memcpy(&result[c], workspace, nxids * sizeof(TransactionId));
5053 }
5054 
5055 /*
5056  * StartParallelWorkerTransaction
5057  * Start a parallel worker transaction, restoring the relevant
5058  * transaction state serialized by SerializeTransactionState.
5059  */
5060 void
5062 {
5063  TransactionId *tstate = (TransactionId *) tstatespace;
5064 
5065  Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
5066  StartTransaction();
5067 
5068  XactIsoLevel = (int) tstate[0];
5069  XactDeferrable = (bool) tstate[1];
5070  XactTopTransactionId = tstate[2];
5071  CurrentTransactionState->transactionId = tstate[3];
5072  currentCommandId = tstate[4];
5073  nParallelCurrentXids = (int) tstate[5];
5074  ParallelCurrentXids = &tstate[6];
5075 
5076  CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
5077 }
5078 
5079 /*
5080  * EndParallelWorkerTransaction
5081  * End a parallel worker transaction.
5082  */
5083 void
5085 {
5086  Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
5088  CurrentTransactionState->blockState = TBLOCK_DEFAULT;
5089 }
5090 
5091 /*
5092  * ShowTransactionState
5093  * Debug support
5094  */
5095 static void
5097 {
5098  /* skip work if message will definitely not be printed */
5100  ShowTransactionStateRec(str, CurrentTransactionState);
5101 }
5102 
5103 /*
5104  * ShowTransactionStateRec
5105  * Recursive subroutine for ShowTransactionState
5106  */
5107 static void
5109 {
5111 
5112  initStringInfo(&buf);
5113 
5114  if (s->nChildXids > 0)
5115  {
5116  int i;
5117 
5118  appendStringInfo(&buf, ", children: %u", s->childXids[0]);
5119  for (i = 1; i < s->nChildXids; i++)
5120  appendStringInfo(&buf, " %u", s->childXids[i]);
5121  }
5122 
5123  if (s->parent)
5125 
5126  /* use ereport to suppress computation if msg will not be printed */
5127  ereport(DEBUG5,
5128  (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
5129  str, s->nestingLevel,
5130  PointerIsValid(s->name) ? s->name : "unnamed",
5133  (unsigned int) s->transactionId,
5134  (unsigned int) s->subTransactionId,
5135  (unsigned int) currentCommandId,
5136  currentCommandIdUsed ? " (used)" : "",
5137  buf.data)));
5138 
5139  pfree(buf.data);
5140 }
5141 
5142 /*
5143  * BlockStateAsString
5144  * Debug support
5145  */
5146 static const char *
5148 {
5149  switch (blockState)
5150  {
5151  case TBLOCK_DEFAULT:
5152  return "DEFAULT";
5153  case TBLOCK_STARTED:
5154  return "STARTED";
5155  case TBLOCK_BEGIN:
5156  return "BEGIN";
5157  case TBLOCK_INPROGRESS:
5158  return "INPROGRESS";
5160  return "IMPLICIT_INPROGRESS";
5162  return "PARALLEL_INPROGRESS";
5163  case TBLOCK_END:
5164  return "END";
5165  case TBLOCK_ABORT:
5166  return "ABORT";
5167  case TBLOCK_ABORT_END:
5168  return "ABORT END";
5169  case TBLOCK_ABORT_PENDING:
5170  return "ABORT PEND";
5171  case TBLOCK_PREPARE:
5172  return "PREPARE";
5173  case TBLOCK_SUBBEGIN:
5174  return "SUB BEGIN";
5175  case TBLOCK_SUBINPROGRESS:
5176  return "SUB INPROGRS";
5177  case TBLOCK_SUBRELEASE:
5178  return "SUB RELEASE";
5179  case TBLOCK_SUBCOMMIT:
5180  return "SUB COMMIT";
5181  case TBLOCK_SUBABORT:
5182  return "SUB ABORT";
5183  case TBLOCK_SUBABORT_END:
5184  return "SUB ABORT END";
5186  return "SUB ABRT PEND";
5187  case TBLOCK_SUBRESTART:
5188  return "SUB RESTART";
5190  return "SUB AB RESTRT";
5191  }
5192  return "UNRECOGNIZED";
5193 }
5194 
5195 /*
5196  * TransStateAsString
5197  * Debug support
5198  */
5199 static const char *
5201 {
5202  switch (state)
5203  {
5204  case TRANS_DEFAULT:
5205  return "DEFAULT";
5206  case TRANS_START:
5207  return "START";
5208  case TRANS_INPROGRESS:
5209  return "INPROGR";
5210  case TRANS_COMMIT:
5211  return "COMMIT";
5212  case TRANS_ABORT:
5213  return "ABORT";
5214  case TRANS_PREPARE:
5215  return "PREPARE";
5216  }
5217  return "UNRECOGNIZED";
5218 }
5219 
5220 /*
5221  * xactGetCommittedChildren
5222  *
5223  * Gets the list of committed children of the current transaction. The return
5224  * value is the number of child transactions. *ptr is set to point to an
5225  * array of TransactionIds. The array is allocated in TopTransactionContext;
5226  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
5227  * If there are no subxacts, *ptr is set to NULL.
5228  */
5229 int
5231 {
5233 
5234  if (s->nChildXids == 0)
5235  *ptr = NULL;
5236  else
5237  *ptr = s->childXids;
5238 
5239  return s->nChildXids;
5240 }
5241 
5242 /*
5243  * XLOG support routines
5244  */
5245 
5246 
5247 /*
5248  * Log the commit record for a plain or twophase transaction commit.
5249  *
5250  * A 2pc commit will be emitted when twophase_xid is valid, a plain one
5251  * otherwise.
5252  */
5253 XLogRecPtr
5255  int nsubxacts, TransactionId *subxacts,
5256  int nrels, RelFileNode *rels,
5257  int nmsgs, SharedInvalidationMessage *msgs,
5258  bool relcacheInval, bool forceSync,
5259  int xactflags, TransactionId twophase_xid)
5260 {
5261  xl_xact_commit xlrec;
5262  xl_xact_xinfo xl_xinfo;
5263  xl_xact_dbinfo xl_dbinfo;
5264  xl_xact_subxacts xl_subxacts;
5265  xl_xact_relfilenodes xl_relfilenodes;
5266  xl_xact_invals xl_invals;
5267  xl_xact_twophase xl_twophase;
5268  xl_xact_origin xl_origin;
5269 
5270  uint8 info;
5271 
5272  Assert(CritSectionCount > 0);
5273 
5274  xl_xinfo.xinfo = 0;
5275 
5276  /* decide between a plain and 2pc commit */
5277  if (!TransactionIdIsValid(twophase_xid))
5278  info = XLOG_XACT_COMMIT;
5279  else
5281 
5282  /* First figure out and collect all the information needed */
5283 
5284  xlrec.xact_time = commit_time;
5285 
5286  if (relcacheInval)
5288  if (forceSyncCommit)
5290  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5291  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5292 
5293  /*
5294  * Check if the caller would like to ask standbys for immediate feedback
5295  * once this commit is applied.
5296  */
5299 
5300  /*
5301  * Relcache invalidations requires information about the current database
5302  * and so does logical decoding.
5303  */
5304  if (nmsgs > 0 || XLogLogicalInfoActive())
5305  {
5306  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5307  xl_dbinfo.dbId = MyDatabaseId;
5308  xl_dbinfo.tsId = MyDatabaseTableSpace;
5309  }
5310 
5311  if (nsubxacts > 0)
5312  {
5313  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5314  xl_subxacts.nsubxacts = nsubxacts;
5315  }
5316 
5317  if (nrels > 0)
5318  {
5319  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5320  xl_relfilenodes.nrels = nrels;
5321  }
5322 
5323  if (nmsgs > 0)
5324  {
5325  xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5326  xl_invals.nmsgs = nmsgs;
5327  }
5328 
5329  if (TransactionIdIsValid(twophase_xid))
5330  {
5331  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5332  xl_twophase.xid = twophase_xid;
5333  }
5334 
5335  /* dump transaction origin information */
5337  {
5338  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5339 
5342  }
5343 
5344  if (xl_xinfo.xinfo != 0)
5345  info |= XLOG_XACT_HAS_INFO;
5346 
5347  /* Then include all the collected data into the commit record. */
5348 
5349  XLogBeginInsert();
5350 
5351  XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
5352 
5353  if (xl_xinfo.xinfo != 0)
5354  XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
5355 
5356  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5357  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5358 
5359  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5360  {
5361  XLogRegisterData((char *) (&xl_subxacts),
5363  XLogRegisterData((char *) subxacts,
5364  nsubxacts * sizeof(TransactionId));
5365  }
5366 
5367  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5368  {
5369  XLogRegisterData((char *) (&xl_relfilenodes),
5371  XLogRegisterData((char *) rels,
5372  nrels * sizeof(RelFileNode));
5373  }
5374 
5375  if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5376  {
5377  XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
5378  XLogRegisterData((char *) msgs,
5379  nmsgs * sizeof(SharedInvalidationMessage));
5380  }
5381 
5382  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5383  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5384 
5385  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5386  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5387 
5388  /* we allow filtering by xacts */
5390 
5391  return XLogInsert(RM_XACT_ID, info);
5392 }
5393 
5394 /*
5395  * Log the commit record for a plain or twophase transaction abort.
5396  *
5397  * A 2pc abort will be emitted when twophase_xid is valid, a plain one
5398  * otherwise.
5399  */
5400 XLogRecPtr
5402  int nsubxacts, TransactionId *subxacts,
5403  int nrels, RelFileNode *rels,
5404  int xactflags, TransactionId twophase_xid)
5405 {
5406  xl_xact_abort xlrec;
5407  xl_xact_xinfo xl_xinfo;
5408  xl_xact_subxacts xl_subxacts;
5409  xl_xact_relfilenodes xl_relfilenodes;
5410  xl_xact_twophase xl_twophase;
5411 
5412  uint8 info;
5413 
5414  Assert(CritSectionCount > 0);
5415 
5416  xl_xinfo.xinfo = 0;
5417 
5418  /* decide between a plain and 2pc abort */
5419  if (!TransactionIdIsValid(twophase_xid))
5420  info = XLOG_XACT_ABORT;
5421  else
5422  info = XLOG_XACT_ABORT_PREPARED;
5423 
5424 
5425  /* First figure out and collect all the information needed */
5426 
5427  xlrec.xact_time = abort_time;
5428 
5429  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5430  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5431 
5432  if (nsubxacts > 0)
5433  {
5434  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5435  xl_subxacts.nsubxacts = nsubxacts;
5436  }
5437 
5438  if (nrels > 0)
5439  {
5440  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5441  xl_relfilenodes.nrels = nrels;
5442  }
5443 
5444  if (TransactionIdIsValid(twophase_xid))
5445  {
5446  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5447  xl_twophase.xid = twophase_xid;
5448  }
5449 
5450  if (xl_xinfo.xinfo != 0)
5451  info |= XLOG_XACT_HAS_INFO;
5452 
5453  /* Then include all the collected data into the abort record. */
5454 
5455  XLogBeginInsert();
5456 
5457  XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
5458 
5459  if (xl_xinfo.xinfo != 0)
5460  XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
5461 
5462  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5463  {
5464  XLogRegisterData((char *) (&xl_subxacts),
5466  XLogRegisterData((char *) subxacts,
5467  nsubxacts * sizeof(TransactionId));
5468  }
5469 
5470  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5471  {
5472  XLogRegisterData((char *) (&xl_relfilenodes),
5474  XLogRegisterData((char *) rels,
5475  nrels * sizeof(RelFileNode));
5476  }
5477 
5478  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5479  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5480 
5481  return XLogInsert(RM_XACT_ID, info);
5482 }
5483 
5484 /*
5485  * Before 9.0 this was a fairly short function, but now it performs many
5486  * actions for which the order of execution is critical.
5487  */
5488 static void
5490  TransactionId xid,
5491  XLogRecPtr lsn,
5492  RepOriginId origin_id)
5493 {
5494  TransactionId max_xid;
5495  int i;
5496  TimestampTz commit_time;
5497 
5499 
5500  max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
5501 
5502  /*
5503  * Make sure nextXid is beyond any XID mentioned in the record.
5504  *
5505  * We don't expect anyone else to modify nextXid, hence we don't need to
5506  * hold a lock while checking this. We still acquire the lock to modify
5507  * it, though.
5508  */
5509  if (TransactionIdFollowsOrEquals(max_xid,
5511  {
5512  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5513  ShmemVariableCache->nextXid = max_xid;
5515  LWLockRelease(XidGenLock);
5516  }
5517 
5518  Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
5519  (origin_id == InvalidRepOriginId));
5520 
5521  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5522  commit_time = parsed->origin_timestamp;
5523  else
5524  commit_time = parsed->xact_time;
5525 
5526  /* Set the transaction commit timestamp and metadata */
5527  TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
5528  commit_time, origin_id, false);
5529 
5531  {
5532  /*
5533  * Mark the transaction committed in pg_xact.
5534  */
5535  TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
5536  }
5537  else
5538  {
5539  /*
5540  * If a transaction completion record arrives that has as-yet
5541  * unobserved subtransactions then this will not have been fully
5542  * handled by the call to RecordKnownAssignedTransactionIds() in the
5543  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5544  * cover that case. This is confusing and it is easy to think this
5545  * call is irrelevant, which has happened three times in development
5546  * already. Leave it in.
5547  */
5549 
5550  /*
5551  * Mark the transaction committed in pg_xact. We use async commit
5552  * protocol during recovery to provide information on database
5553  * consistency for when users try to set hint bits. It is important
5554  * that we do not set hint bits until the minRecoveryPoint is past
5555  * this commit record. This ensures that if we crash we don't see hint
5556  * bits set on changes made by transactions that haven't yet
5557  * recovered. It's unlikely but it's good to be safe.
5558  */
5560  xid, parsed->nsubxacts, parsed->subxacts, lsn);
5561 
5562  /*
5563  * We must mark clog before we update the ProcArray.
5564  */
5566  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5567 
5568  /*
5569  * Send any cache invalidations attached to the commit. We must
5570  * maintain the same order of invalidation then release locks as
5571  * occurs in CommitTransaction().
5572  */
5574  parsed->msgs, parsed->nmsgs,
5576  parsed->dbId, parsed->tsId);
5577 
5578  /*
5579  * Release locks, if any. We do this for both two phase and normal one
5580  * phase transactions. In effect we are ignoring the prepare phase and
5581  * just going straight to lock release. At commit we release all locks
5582  * via their top-level xid only, so no need to provide subxact list,
5583  * which will save time when replaying commits.
5584  */
5585  if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
5586  StandbyReleaseLockTree(xid, 0, NULL);
5587  }
5588 
5589  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5590  {
5591  /* recover apply progress */
5592  replorigin_advance(origin_id, parsed->origin_lsn, lsn,
5593  false /* backward */ , false /* WAL */ );
5594  }
5595 
5596  /* Make sure files supposed to be dropped are dropped */
5597  if (parsed->nrels > 0)
5598  {
5599  /*
5600  * First update minimum recovery point to cover this WAL record. Once
5601  * a relation is deleted, there's no going back. The buffer manager
5602  * enforces the WAL-first rule for normal updates to relation files,
5603  * so that the minimum recovery point is always updated before the
5604  * corresponding change in the data file is flushed to disk, but we
5605  * have to do the same here since we're bypassing the buffer manager.
5606  *
5607  * Doing this before deleting the files means that if a deletion fails
5608  * for some reason, you cannot start up the system even after restart,
5609  * until you fix the underlying situation so that the deletion will
5610  * succeed. Alternatively, we could update the minimum recovery point
5611  * after deletion, but that would leave a small window where the
5612  * WAL-first rule would be violated.
5613  */
5614  XLogFlush(lsn);
5615 
5616  for (i = 0; i < parsed->nrels; i++)
5617  {
5618  SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId);
5619  ForkNumber fork;
5620 
5621  for (fork = 0; fork <= MAX_FORKNUM; fork++)
5622  XLogDropRelation(parsed->xnodes[i], fork);
5623  smgrdounlink(srel, true);
5624  smgrclose(srel);
5625  }
5626  }
5627 
5628  /*
5629  * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
5630  * in normal operation. For example, in CREATE DATABASE, we copy all files
5631  * from the template database, and then commit the transaction. If we
5632  * crash after all the files have been copied but before the commit, you
5633  * have files in the data directory without an entry in pg_database. To
5634  * minimize the window for that, we use ForceSyncCommit() to rush the
5635  * commit record to disk as quick as possible. We have the same window
5636  * during recovery, and forcing an XLogFlush() (which updates
5637  * minRecoveryPoint during recovery) helps to reduce that problem window,
5638  * for any user that requested ForceSyncCommit().
5639  */
5640  if (XactCompletionForceSyncCommit(parsed->xinfo))
5641  XLogFlush(lsn);
5642 
5643  /*
5644  * If asked by the primary (because someone is waiting for a synchronous
5645  * commit = remote_apply), we will need to ask walreceiver to send a reply
5646  * immediately.
5647  */
5648  if (XactCompletionApplyFeedback(parsed->xinfo))
5650 }
5651 
5652 /*
5653  * Be careful with the order of execution, as with xact_redo_commit().
5654  * The two functions are similar but differ in key places.
5655  *
5656  * Note also that an abort can be for a subtransaction and its children,
5657  * not just for a top level abort. That means we have to consider
5658  * topxid != xid, whereas in commit we would find topxid == xid always
5659  * because subtransaction commit is never WAL logged.
5660  */
5661 static void
5663 {
5664  int i;
5665  TransactionId max_xid;
5666 
5668 
5669  /*
5670  * Make sure nextXid is beyond any XID mentioned in the record.
5671  *
5672  * We don't expect anyone else to modify nextXid, hence we don't need to
5673  * hold a lock while checking this. We still acquire the lock to modify
5674  * it, though.
5675  */
5676  max_xid = TransactionIdLatest(xid,
5677  parsed->nsubxacts,
5678  parsed->subxacts);
5679 
5680  if (TransactionIdFollowsOrEquals(max_xid,
5682  {
5683  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5684  ShmemVariableCache->nextXid = max_xid;
5686  LWLockRelease(XidGenLock);
5687  }
5688 
5690  {
5691  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5692  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5693  }
5694  else
5695  {
5696  /*
5697  * If a transaction completion record arrives that has as-yet
5698  * unobserved subtransactions then this will not have been fully
5699  * handled by the call to RecordKnownAssignedTransactionIds() in the
5700  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5701  * cover that case. This is confusing and it is easy to think this
5702  * call is irrelevant, which has happened three times in development
5703  * already. Leave it in.
5704  */
5706 
5707  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5708  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5709 
5710  /*
5711  * We must update the ProcArray after we have marked clog.
5712  */
5714  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5715 
5716  /*
5717  * There are no flat files that need updating, nor invalidation
5718  * messages to send or undo.
5719  */
5720 
5721  /*
5722  * Release locks, if any. There are no invalidations to send.
5723  */
5724  if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
5725  StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
5726  }
5727 
5728  /* Make sure files supposed to be dropped are dropped */
5729  for (i = 0; i < parsed->nrels; i++)
5730  {
5731  SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId);
5732  ForkNumber fork;
5733 
5734  for (fork = 0; fork <= MAX_FORKNUM; fork++)
5735  XLogDropRelation(parsed->xnodes[i], fork);
5736  smgrdounlink(srel, true);
5737  smgrclose(srel);
5738  }
5739 }
5740 
5741 void
5743 {
5744  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
5745 
5746  /* Backup blocks are not used in xact records */
5747  Assert(!XLogRecHasAnyBlockRefs(record));
5748 
5749  if (info == XLOG_XACT_COMMIT)
5750  {
5751  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5752  xl_xact_parsed_commit parsed;
5753 
5754  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5755  xact_redo_commit(&parsed, XLogRecGetXid(record),
5756  record->EndRecPtr, XLogRecGetOrigin(record));
5757  }
5758  else if (info == XLOG_XACT_COMMIT_PREPARED)
5759  {
5760  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5761  xl_xact_parsed_commit parsed;
5762