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-2018, 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, /* parallelModeLevel */
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 a parallel worker, because
687  * we 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  */
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  0,
1003  32 * 1024,
1004  32 * 1024,
1005  32 * 1024);
1006 
1007  /*
1008  * We shouldn't have a transaction context already.
1009  */
1010  Assert(TopTransactionContext == NULL);
1011 
1012  /*
1013  * Create a toplevel context for the transaction.
1014  */
1017  "TopTransactionContext",
1019 
1020  /*
1021  * In a top-level transaction, CurTransactionContext is the same as
1022  * TopTransactionContext.
1023  */
1026 
1027  /* Make the CurTransactionContext active. */
1029 }
1030 
1031 /*
1032  * AtStart_ResourceOwner
1033  */
1034 static void
1036 {
1038 
1039  /*
1040  * We shouldn't have a transaction resource owner already.
1041  */
1043 
1044  /*
1045  * Create a toplevel resource owner for the transaction.
1046  */
1047  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1048 
1052 }
1053 
1054 /* ----------------------------------------------------------------
1055  * StartSubTransaction stuff
1056  * ----------------------------------------------------------------
1057  */
1058 
1059 /*
1060  * AtSubStart_Memory
1061  */
1062 static void
1064 {
1066 
1067  Assert(CurTransactionContext != NULL);
1068 
1069  /*
1070  * Create a CurTransactionContext, which will be used to hold data that
1071  * survives subtransaction commit but disappears on subtransaction abort.
1072  * We make it a child of the immediate parent's CurTransactionContext.
1073  */
1075  "CurTransactionContext",
1078 
1079  /* Make the CurTransactionContext active. */
1081 }
1082 
1083 /*
1084  * AtSubStart_ResourceOwner
1085  */
1086 static void
1088 {
1090 
1091  Assert(s->parent != NULL);
1092 
1093  /*
1094  * Create a resource owner for the subtransaction. We make it a child of
1095  * the immediate parent's resource owner.
1096  */
1097  s->curTransactionOwner =
1099  "SubTransaction");
1100 
1103 }
1104 
1105 /* ----------------------------------------------------------------
1106  * CommitTransaction stuff
1107  * ----------------------------------------------------------------
1108  */
1109 
1110 /*
1111  * RecordTransactionCommit
1112  *
1113  * Returns latest XID among xact and its children, or InvalidTransactionId
1114  * if the xact has no XID. (We compute that here just because it's easier.)
1115  *
1116  * If you change this function, see RecordTransactionCommitPrepared also.
1117  */
1118 static TransactionId
1120 {
1122  bool markXidCommitted = TransactionIdIsValid(xid);
1123  TransactionId latestXid = InvalidTransactionId;
1124  int nrels;
1125  RelFileNode *rels;
1126  int nchildren;
1127  TransactionId *children;
1128  int nmsgs = 0;
1129  SharedInvalidationMessage *invalMessages = NULL;
1130  bool RelcacheInitFileInval = false;
1131  bool wrote_xlog;
1132 
1133  /* Get data needed for commit record */
1134  nrels = smgrGetPendingDeletes(true, &rels);
1135  nchildren = xactGetCommittedChildren(&children);
1136  if (XLogStandbyInfoActive())
1137  nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1138  &RelcacheInitFileInval);
1139  wrote_xlog = (XactLastRecEnd != 0);
1140 
1141  /*
1142  * If we haven't been assigned an XID yet, we neither can, nor do we want
1143  * to write a COMMIT record.
1144  */
1145  if (!markXidCommitted)
1146  {
1147  /*
1148  * We expect that every smgrscheduleunlink is followed by a catalog
1149  * update, and hence XID assignment, so we shouldn't get here with any
1150  * pending deletes. Use a real test not just an Assert to check this,
1151  * since it's a bit fragile.
1152  */
1153  if (nrels != 0)
1154  elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1155 
1156  /* Can't have child XIDs either; AssignTransactionId enforces this */
1157  Assert(nchildren == 0);
1158 
1159  /*
1160  * Transactions without an assigned xid can contain invalidation
1161  * messages (e.g. explicit relcache invalidations or catcache
1162  * invalidations for inplace updates); standbys need to process those.
1163  * We can't emit a commit record without an xid, and we don't want to
1164  * force assigning an xid, because that'd be problematic for e.g.
1165  * vacuum. Hence we emit a bespoke record for the invalidations. We
1166  * don't want to use that in case a commit record is emitted, so they
1167  * happen synchronously with commits (besides not wanting to emit more
1168  * WAL records).
1169  */
1170  if (nmsgs != 0)
1171  {
1172  LogStandbyInvalidations(nmsgs, invalMessages,
1173  RelcacheInitFileInval);
1174  wrote_xlog = true; /* not strictly necessary */
1175  }
1176 
1177  /*
1178  * If we didn't create XLOG entries, we're done here; otherwise we
1179  * should trigger flushing those entries the same as a commit record
1180  * would. This will primarily happen for HOT pruning and the like; we
1181  * want these to be flushed to disk in due time.
1182  */
1183  if (!wrote_xlog)
1184  goto cleanup;
1185  }
1186  else
1187  {
1188  bool replorigin;
1189 
1190  /*
1191  * Are we using the replication origins feature? Or, in other words,
1192  * are we replaying remote actions?
1193  */
1194  replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1196 
1197  /*
1198  * Begin commit critical section and insert the commit XLOG record.
1199  */
1200  /* Tell bufmgr and smgr to prepare for commit */
1201  BufmgrCommit();
1202 
1203  /*
1204  * Mark ourselves as within our "commit critical section". This
1205  * forces any concurrent checkpoint to wait until we've updated
1206  * pg_xact. Without this, it is possible for the checkpoint to set
1207  * REDO after the XLOG record but fail to flush the pg_xact update to
1208  * disk, leading to loss of the transaction commit if the system
1209  * crashes a little later.
1210  *
1211  * Note: we could, but don't bother to, set this flag in
1212  * RecordTransactionAbort. That's because loss of a transaction abort
1213  * is noncritical; the presumption would be that it aborted, anyway.
1214  *
1215  * It's safe to change the delayChkpt flag of our own backend without
1216  * holding the ProcArrayLock, since we're the only one modifying it.
1217  * This makes checkpoint's determination of which xacts are delayChkpt
1218  * a bit fuzzy, but it doesn't matter.
1219  */
1221  MyPgXact->delayChkpt = true;
1222 
1224 
1226  nchildren, children, nrels, rels,
1227  nmsgs, invalMessages,
1228  RelcacheInitFileInval, forceSyncCommit,
1229  MyXactFlags,
1230  InvalidTransactionId /* plain commit */ );
1231 
1232  if (replorigin)
1233  /* Move LSNs forward for this replication origin */
1235  XactLastRecEnd);
1236 
1237  /*
1238  * Record commit timestamp. The value comes from plain commit
1239  * timestamp if there's no replication origin; otherwise, the
1240  * timestamp was already set in replorigin_session_origin_timestamp by
1241  * replication.
1242  *
1243  * We don't need to WAL-log anything here, as the commit record
1244  * written above already contains the data.
1245  */
1246 
1247  if (!replorigin || replorigin_session_origin_timestamp == 0)
1249 
1250  TransactionTreeSetCommitTsData(xid, nchildren, children,
1252  replorigin_session_origin, false);
1253  }
1254 
1255  /*
1256  * Check if we want to commit asynchronously. We can allow the XLOG flush
1257  * to happen asynchronously if synchronous_commit=off, or if the current
1258  * transaction has not performed any WAL-logged operation or didn't assign
1259  * an xid. The transaction can end up not writing any WAL, even if it has
1260  * an xid, if it only wrote to temporary and/or unlogged tables. It can
1261  * end up having written WAL without an xid if it did HOT pruning. In
1262  * case of a crash, the loss of such a transaction will be irrelevant;
1263  * temp tables will be lost anyway, unlogged tables will be truncated and
1264  * HOT pruning will be done again later. (Given the foregoing, you might
1265  * think that it would be unnecessary to emit the XLOG record at all in
1266  * this case, but we don't currently try to do that. It would certainly
1267  * cause problems at least in Hot Standby mode, where the
1268  * KnownAssignedXids machinery requires tracking every XID assignment. It
1269  * might be OK to skip it only when wal_level < replica, but for now we
1270  * don't.)
1271  *
1272  * However, if we're doing cleanup of any non-temp rels or committing any
1273  * command that wanted to force sync commit, then we must flush XLOG
1274  * immediately. (We must not allow asynchronous commit if there are any
1275  * non-temp tables to be deleted, because we might delete the files before
1276  * the COMMIT record is flushed to disk. We do allow asynchronous commit
1277  * if all to-be-deleted tables are temporary though, since they are lost
1278  * anyway if we crash.)
1279  */
1280  if ((wrote_xlog && markXidCommitted &&
1282  forceSyncCommit || nrels > 0)
1283  {
1285 
1286  /*
1287  * Now we may update the CLOG, if we wrote a COMMIT record above
1288  */
1289  if (markXidCommitted)
1290  TransactionIdCommitTree(xid, nchildren, children);
1291  }
1292  else
1293  {
1294  /*
1295  * Asynchronous commit case:
1296  *
1297  * This enables possible committed transaction loss in the case of a
1298  * postmaster crash because WAL buffers are left unwritten. Ideally we
1299  * could issue the WAL write without the fsync, but some
1300  * wal_sync_methods do not allow separate write/fsync.
1301  *
1302  * Report the latest async commit LSN, so that the WAL writer knows to
1303  * flush this commit.
1304  */
1306 
1307  /*
1308  * We must not immediately update the CLOG, since we didn't flush the
1309  * XLOG. Instead, we store the LSN up to which the XLOG must be
1310  * flushed before the CLOG may be updated.
1311  */
1312  if (markXidCommitted)
1313  TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1314  }
1315 
1316  /*
1317  * If we entered a commit critical section, leave it now, and let
1318  * checkpoints proceed.
1319  */
1320  if (markXidCommitted)
1321  {
1322  MyPgXact->delayChkpt = false;
1323  END_CRIT_SECTION();
1324  }
1325 
1326  /* Compute latestXid while we have the child XIDs handy */
1327  latestXid = TransactionIdLatest(xid, nchildren, children);
1328 
1329  /*
1330  * Wait for synchronous replication, if required. Similar to the decision
1331  * above about using committing asynchronously we only want to wait if
1332  * this backend assigned an xid and wrote WAL. No need to wait if an xid
1333  * was assigned due to temporary/unlogged tables or due to HOT pruning.
1334  *
1335  * Note that at this stage we have marked clog, but still show as running
1336  * in the procarray and continue to hold locks.
1337  */
1338  if (wrote_xlog && markXidCommitted)
1340 
1341  /* remember end of last commit record */
1343 
1344  /* Reset XactLastRecEnd until the next transaction writes something */
1345  XactLastRecEnd = 0;
1346 cleanup:
1347  /* Clean up local data */
1348  if (rels)
1349  pfree(rels);
1350 
1351  return latestXid;
1352 }
1353 
1354 
1355 /*
1356  * AtCCI_LocalCache
1357  */
1358 static void
1360 {
1361  /*
1362  * Make any pending relation map changes visible. We must do this before
1363  * processing local sinval messages, so that the map changes will get
1364  * reflected into the relcache when relcache invals are processed.
1365  */
1367 
1368  /*
1369  * Make catalog changes visible to me for the next command.
1370  */
1372 }
1373 
1374 /*
1375  * AtCommit_Memory
1376  */
1377 static void
1379 {
1380  /*
1381  * Now that we're "out" of a transaction, have the system allocate things
1382  * in the top memory context instead of per-transaction contexts.
1383  */
1385 
1386  /*
1387  * Release all transaction-local memory.
1388  */
1389  Assert(TopTransactionContext != NULL);
1391  TopTransactionContext = NULL;
1392  CurTransactionContext = NULL;
1393  CurrentTransactionState->curTransactionContext = NULL;
1394 }
1395 
1396 /* ----------------------------------------------------------------
1397  * CommitSubTransaction stuff
1398  * ----------------------------------------------------------------
1399  */
1400 
1401 /*
1402  * AtSubCommit_Memory
1403  */
1404 static void
1406 {
1408 
1409  Assert(s->parent != NULL);
1410 
1411  /* Return to parent transaction level's memory context. */
1414 
1415  /*
1416  * Ordinarily we cannot throw away the child's CurTransactionContext,
1417  * since the data it contains will be needed at upper commit. However, if
1418  * there isn't actually anything in it, we can throw it away. This avoids
1419  * a small memory leak in the common case of "trivial" subxacts.
1420  */
1422  {
1424  s->curTransactionContext = NULL;
1425  }
1426 }
1427 
1428 /*
1429  * AtSubCommit_childXids
1430  *
1431  * Pass my own XID and my child XIDs up to my parent as committed children.
1432  */
1433 static void
1435 {
1437  int new_nChildXids;
1438 
1439  Assert(s->parent != NULL);
1440 
1441  /*
1442  * The parent childXids array will need to hold my XID and all my
1443  * childXids, in addition to the XIDs already there.
1444  */
1445  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1446 
1447  /* Allocate or enlarge the parent array if necessary */
1448  if (s->parent->maxChildXids < new_nChildXids)
1449  {
1450  int new_maxChildXids;
1451  TransactionId *new_childXids;
1452 
1453  /*
1454  * Make it 2x what's needed right now, to avoid having to enlarge it
1455  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1456  * is what ensures that we don't need to worry about integer overflow
1457  * here or in the calculation of new_nChildXids.)
1458  */
1459  new_maxChildXids = Min(new_nChildXids * 2,
1460  (int) (MaxAllocSize / sizeof(TransactionId)));
1461 
1462  if (new_maxChildXids < new_nChildXids)
1463  ereport(ERROR,
1464  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1465  errmsg("maximum number of committed subtransactions (%d) exceeded",
1466  (int) (MaxAllocSize / sizeof(TransactionId)))));
1467 
1468  /*
1469  * We keep the child-XID arrays in TopTransactionContext; this avoids
1470  * setting up child-transaction contexts for what might be just a few
1471  * bytes of grandchild XIDs.
1472  */
1473  if (s->parent->childXids == NULL)
1474  new_childXids =
1476  new_maxChildXids * sizeof(TransactionId));
1477  else
1478  new_childXids = repalloc(s->parent->childXids,
1479  new_maxChildXids * sizeof(TransactionId));
1480 
1481  s->parent->childXids = new_childXids;
1482  s->parent->maxChildXids = new_maxChildXids;
1483  }
1484 
1485  /*
1486  * Copy all my XIDs to parent's array.
1487  *
1488  * Note: We rely on the fact that the XID of a child always follows that
1489  * of its parent. By copying the XID of this subtransaction before the
1490  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1491  * all XIDs already in the array belong to subtransactions started and
1492  * subcommitted before us, so their XIDs must precede ours.
1493  */
1495 
1496  if (s->nChildXids > 0)
1497  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1498  s->childXids,
1499  s->nChildXids * sizeof(TransactionId));
1500 
1501  s->parent->nChildXids = new_nChildXids;
1502 
1503  /* Release child's array to avoid leakage */
1504  if (s->childXids != NULL)
1505  pfree(s->childXids);
1506  /* We must reset these to avoid double-free if fail later in commit */
1507  s->childXids = NULL;
1508  s->nChildXids = 0;
1509  s->maxChildXids = 0;
1510 }
1511 
1512 /* ----------------------------------------------------------------
1513  * AbortTransaction stuff
1514  * ----------------------------------------------------------------
1515  */
1516 
1517 /*
1518  * RecordTransactionAbort
1519  *
1520  * Returns latest XID among xact and its children, or InvalidTransactionId
1521  * if the xact has no XID. (We compute that here just because it's easier.)
1522  */
1523 static TransactionId
1524 RecordTransactionAbort(bool isSubXact)
1525 {
1527  TransactionId latestXid;
1528  int nrels;
1529  RelFileNode *rels;
1530  int nchildren;
1531  TransactionId *children;
1532  TimestampTz xact_time;
1533 
1534  /*
1535  * If we haven't been assigned an XID, nobody will care whether we aborted
1536  * or not. Hence, we're done in that case. It does not matter if we have
1537  * rels to delete (note that this routine is not responsible for actually
1538  * deleting 'em). We cannot have any child XIDs, either.
1539  */
1540  if (!TransactionIdIsValid(xid))
1541  {
1542  /* Reset XactLastRecEnd until the next transaction writes something */
1543  if (!isSubXact)
1544  XactLastRecEnd = 0;
1545  return InvalidTransactionId;
1546  }
1547 
1548  /*
1549  * We have a valid XID, so we should write an ABORT record for it.
1550  *
1551  * We do not flush XLOG to disk here, since the default assumption after a
1552  * crash would be that we aborted, anyway. For the same reason, we don't
1553  * need to worry about interlocking against checkpoint start.
1554  */
1555 
1556  /*
1557  * Check that we haven't aborted halfway through RecordTransactionCommit.
1558  */
1559  if (TransactionIdDidCommit(xid))
1560  elog(PANIC, "cannot abort transaction %u, it was already committed",
1561  xid);
1562 
1563  /* Fetch the data we need for the abort record */
1564  nrels = smgrGetPendingDeletes(false, &rels);
1565  nchildren = xactGetCommittedChildren(&children);
1566 
1567  /* XXX do we really need a critical section here? */
1569 
1570  /* Write the ABORT record */
1571  if (isSubXact)
1572  xact_time = GetCurrentTimestamp();
1573  else
1574  {
1576  xact_time = xactStopTimestamp;
1577  }
1578 
1579  XactLogAbortRecord(xact_time,
1580  nchildren, children,
1581  nrels, rels,
1583 
1584  /*
1585  * Report the latest async abort LSN, so that the WAL writer knows to
1586  * flush this abort. There's nothing to be gained by delaying this, since
1587  * WALWriter may as well do this when it can. This is important with
1588  * streaming replication because if we don't flush WAL regularly we will
1589  * find that large aborts leave us with a long backlog for when commits
1590  * occur after the abort, increasing our window of data loss should
1591  * problems occur at that point.
1592  */
1593  if (!isSubXact)
1595 
1596  /*
1597  * Mark the transaction aborted in clog. This is not absolutely necessary
1598  * but we may as well do it while we are here; also, in the subxact case
1599  * it is helpful because XactLockTableWait makes use of it to avoid
1600  * waiting for already-aborted subtransactions. It is OK to do it without
1601  * having flushed the ABORT record to disk, because in event of a crash
1602  * we'd be assumed to have aborted anyway.
1603  */
1604  TransactionIdAbortTree(xid, nchildren, children);
1605 
1606  END_CRIT_SECTION();
1607 
1608  /* Compute latestXid while we have the child XIDs handy */
1609  latestXid = TransactionIdLatest(xid, nchildren, children);
1610 
1611  /*
1612  * If we're aborting a subtransaction, we can immediately remove failed
1613  * XIDs from PGPROC's cache of running child XIDs. We do that here for
1614  * subxacts, because we already have the child XID array at hand. For
1615  * main xacts, the equivalent happens just after this function returns.
1616  */
1617  if (isSubXact)
1618  XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1619 
1620  /* Reset XactLastRecEnd until the next transaction writes something */
1621  if (!isSubXact)
1622  XactLastRecEnd = 0;
1623 
1624  /* And clean up local data */
1625  if (rels)
1626  pfree(rels);
1627 
1628  return latestXid;
1629 }
1630 
1631 /*
1632  * AtAbort_Memory
1633  */
1634 static void
1636 {
1637  /*
1638  * Switch into TransactionAbortContext, which should have some free space
1639  * even if nothing else does. We'll work in this context until we've
1640  * finished cleaning up.
1641  *
1642  * It is barely possible to get here when we've not been able to create
1643  * TransactionAbortContext yet; if so use TopMemoryContext.
1644  */
1645  if (TransactionAbortContext != NULL)
1646  MemoryContextSwitchTo(TransactionAbortContext);
1647  else
1649 }
1650 
1651 /*
1652  * AtSubAbort_Memory
1653  */
1654 static void
1656 {
1657  Assert(TransactionAbortContext != NULL);
1658 
1659  MemoryContextSwitchTo(TransactionAbortContext);
1660 }
1661 
1662 
1663 /*
1664  * AtAbort_ResourceOwner
1665  */
1666 static void
1668 {
1669  /*
1670  * Make sure we have a valid ResourceOwner, if possible (else it will be
1671  * NULL, which is OK)
1672  */
1674 }
1675 
1676 /*
1677  * AtSubAbort_ResourceOwner
1678  */
1679 static void
1681 {
1683 
1684  /* Make sure we have a valid ResourceOwner */
1686 }
1687 
1688 
1689 /*
1690  * AtSubAbort_childXids
1691  */
1692 static void
1694 {
1696 
1697  /*
1698  * We keep the child-XID arrays in TopTransactionContext (see
1699  * AtSubCommit_childXids). This means we'd better free the array
1700  * explicitly at abort to avoid leakage.
1701  */
1702  if (s->childXids != NULL)
1703  pfree(s->childXids);
1704  s->childXids = NULL;
1705  s->nChildXids = 0;
1706  s->maxChildXids = 0;
1707 
1708  /*
1709  * We could prune the unreportedXids array here. But we don't bother. That
1710  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1711  * would likely introduce more CPU time into the more common paths, so we
1712  * choose not to do that.
1713  */
1714 }
1715 
1716 /* ----------------------------------------------------------------
1717  * CleanupTransaction stuff
1718  * ----------------------------------------------------------------
1719  */
1720 
1721 /*
1722  * AtCleanup_Memory
1723  */
1724 static void
1726 {
1727  Assert(CurrentTransactionState->parent == NULL);
1728 
1729  /*
1730  * Now that we're "out" of a transaction, have the system allocate things
1731  * in the top memory context instead of per-transaction contexts.
1732  */
1734 
1735  /*
1736  * Clear the special abort context for next time.
1737  */
1738  if (TransactionAbortContext != NULL)
1739  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1740 
1741  /*
1742  * Release all transaction-local memory.
1743  */
1744  if (TopTransactionContext != NULL)
1746  TopTransactionContext = NULL;
1747  CurTransactionContext = NULL;
1748  CurrentTransactionState->curTransactionContext = NULL;
1749 }
1750 
1751 
1752 /* ----------------------------------------------------------------
1753  * CleanupSubTransaction stuff
1754  * ----------------------------------------------------------------
1755  */
1756 
1757 /*
1758  * AtSubCleanup_Memory
1759  */
1760 static void
1762 {
1764 
1765  Assert(s->parent != NULL);
1766 
1767  /* Make sure we're not in an about-to-be-deleted context */
1770 
1771  /*
1772  * Clear the special abort context for next time.
1773  */
1774  if (TransactionAbortContext != NULL)
1775  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1776 
1777  /*
1778  * Delete the subxact local memory contexts. Its CurTransactionContext can
1779  * go too (note this also kills CurTransactionContexts from any children
1780  * of the subxact).
1781  */
1782  if (s->curTransactionContext)
1784  s->curTransactionContext = NULL;
1785 }
1786 
1787 /* ----------------------------------------------------------------
1788  * interface routines
1789  * ----------------------------------------------------------------
1790  */
1791 
1792 /*
1793  * StartTransaction
1794  */
1795 static void
1797 {
1798  TransactionState s;
1799  VirtualTransactionId vxid;
1800 
1801  /*
1802  * Let's just make sure the state stack is empty
1803  */
1805  CurrentTransactionState = s;
1806 
1808 
1809  /*
1810  * check the current transaction state
1811  */
1812  if (s->state != TRANS_DEFAULT)
1813  elog(WARNING, "StartTransaction while in %s state",
1815 
1816  /*
1817  * set the current transaction state information appropriately during
1818  * start processing
1819  */
1820  s->state = TRANS_START;
1821  s->transactionId = InvalidTransactionId; /* until assigned */
1822 
1823  /*
1824  * Make sure we've reset xact state variables
1825  *
1826  * If recovery is still in progress, mark this transaction as read-only.
1827  * We have lower level defences in XLogInsert and elsewhere to stop us
1828  * from modifying data during recovery, but this gives the normal
1829  * indication to the user that the transaction is read-only.
1830  */
1831  if (RecoveryInProgress())
1832  {
1833  s->startedInRecovery = true;
1834  XactReadOnly = true;
1835  }
1836  else
1837  {
1838  s->startedInRecovery = false;
1840  }
1843  forceSyncCommit = false;
1844  MyXactFlags = 0;
1845 
1846  /*
1847  * reinitialize within-transaction counters
1848  */
1852  currentCommandIdUsed = false;
1853 
1854  /*
1855  * initialize reported xid accounting
1856  */
1857  nUnreportedXids = 0;
1858  s->didLogXid = false;
1859 
1860  /*
1861  * must initialize resource-management stuff first
1862  */
1863  AtStart_Memory();
1865 
1866  /*
1867  * Assign a new LocalTransactionId, and combine it with the backendId to
1868  * form a virtual transaction id.
1869  */
1870  vxid.backendId = MyBackendId;
1872 
1873  /*
1874  * Lock the virtual transaction id before we announce it in the proc array
1875  */
1877 
1878  /*
1879  * Advertise it in the proc array. We assume assignment of
1880  * LocalTransactionID is atomic, and the backendId should be set already.
1881  */
1882  Assert(MyProc->backendId == vxid.backendId);
1883  MyProc->lxid = vxid.localTransactionId;
1884 
1885  TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1886 
1887  /*
1888  * set transaction_timestamp() (a/k/a now()). We want this to be the same
1889  * as the first command's statement_timestamp(), so don't do a fresh
1890  * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1891  * xactStopTimestamp as unset.
1892  */
1894  xactStopTimestamp = 0;
1896 
1897  /*
1898  * initialize current transaction state fields
1899  *
1900  * note: prevXactReadOnly is not used at the outermost level
1901  */
1902  s->nestingLevel = 1;
1903  s->gucNestLevel = 1;
1904  s->childXids = NULL;
1905  s->nChildXids = 0;
1906  s->maxChildXids = 0;
1908  /* SecurityRestrictionContext should never be set outside a transaction */
1909  Assert(s->prevSecContext == 0);
1910 
1911  /*
1912  * initialize other subsystems for new transaction
1913  */
1914  AtStart_GUC();
1915  AtStart_Cache();
1917 
1918  /*
1919  * done with start processing, set current transaction state to "in
1920  * progress"
1921  */
1922  s->state = TRANS_INPROGRESS;
1923 
1924  ShowTransactionState("StartTransaction");
1925 }
1926 
1927 
1928 /*
1929  * CommitTransaction
1930  *
1931  * NB: if you change this routine, better look at PrepareTransaction too!
1932  */
1933 static void
1935 {
1937  TransactionId latestXid;
1938  bool is_parallel_worker;
1939 
1940  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
1941 
1942  /* Enforce parallel mode restrictions during parallel worker commit. */
1943  if (is_parallel_worker)
1945 
1946  ShowTransactionState("CommitTransaction");
1947 
1948  /*
1949  * check the current transaction state
1950  */
1951  if (s->state != TRANS_INPROGRESS)
1952  elog(WARNING, "CommitTransaction while in %s state",
1954  Assert(s->parent == NULL);
1955 
1956  /*
1957  * Do pre-commit processing that involves calling user-defined code, such
1958  * as triggers. Since closing cursors could queue trigger actions,
1959  * triggers could open cursors, etc, we have to keep looping until there's
1960  * nothing left to do.
1961  */
1962  for (;;)
1963  {
1964  /*
1965  * Fire all currently pending deferred triggers.
1966  */
1968 
1969  /*
1970  * Close open portals (converting holdable ones into static portals).
1971  * If there weren't any, we are done ... otherwise loop back to check
1972  * if they queued deferred triggers. Lather, rinse, repeat.
1973  */
1974  if (!PreCommit_Portals(false))
1975  break;
1976  }
1977 
1980 
1981  /*
1982  * The remaining actions cannot call any user-defined code, so it's safe
1983  * to start shutting down within-transaction services. But note that most
1984  * of this stuff could still throw an error, which would switch us into
1985  * the transaction-abort path.
1986  */
1987 
1988  /* If we might have parallel workers, clean them up now. */
1989  if (IsInParallelMode())
1990  AtEOXact_Parallel(true);
1991 
1992  /* Shut down the deferred-trigger manager */
1993  AfterTriggerEndXact(true);
1994 
1995  /*
1996  * Let ON COMMIT management do its thing (must happen after closing
1997  * cursors, to avoid dangling-reference problems)
1998  */
2000 
2001  /* close large objects before lower-level cleanup */
2002  AtEOXact_LargeObject(true);
2003 
2004  /*
2005  * Mark serializable transaction as complete for predicate locking
2006  * purposes. This should be done as late as we can put it and still allow
2007  * errors to be raised for failure patterns found at commit.
2008  */
2010 
2011  /*
2012  * Insert notifications sent by NOTIFY commands into the queue. This
2013  * should be late in the pre-commit sequence to minimize time spent
2014  * holding the notify-insertion lock.
2015  */
2016  PreCommit_Notify();
2017 
2018  /* Prevent cancel/die interrupt while cleaning up */
2019  HOLD_INTERRUPTS();
2020 
2021  /* Commit updates to the relation map --- do this as late as possible */
2022  AtEOXact_RelationMap(true);
2023 
2024  /*
2025  * set the current transaction state information appropriately during
2026  * commit processing
2027  */
2028  s->state = TRANS_COMMIT;
2029  s->parallelModeLevel = 0;
2030 
2031  if (!is_parallel_worker)
2032  {
2033  /*
2034  * We need to mark our XIDs as committed in pg_xact. This is where we
2035  * durably commit.
2036  */
2037  latestXid = RecordTransactionCommit();
2038  }
2039  else
2040  {
2041  /*
2042  * We must not mark our XID committed; the parallel master is
2043  * responsible for that.
2044  */
2045  latestXid = InvalidTransactionId;
2046 
2047  /*
2048  * Make sure the master will know about any WAL we wrote before it
2049  * commits.
2050  */
2052  }
2053 
2054  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2055 
2056  /*
2057  * Let others know about no transaction in progress by me. Note that this
2058  * must be done _before_ releasing locks we hold and _after_
2059  * RecordTransactionCommit.
2060  */
2061  ProcArrayEndTransaction(MyProc, latestXid);
2062 
2063  /*
2064  * This is all post-commit cleanup. Note that if an error is raised here,
2065  * it's too late to abort the transaction. This should be just
2066  * noncritical resource releasing.
2067  *
2068  * The ordering of operations is not entirely random. The idea is:
2069  * release resources visible to other backends (eg, files, buffer pins);
2070  * then release locks; then release backend-local resources. We want to
2071  * release locks at the point where any backend waiting for us will see
2072  * our transaction as being fully cleaned up.
2073  *
2074  * Resources that can be associated with individual queries are handled by
2075  * the ResourceOwner mechanism. The other calls here are for backend-wide
2076  * state.
2077  */
2078 
2079  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2080  : XACT_EVENT_COMMIT);
2081 
2084  true, true);
2085 
2086  /* Check we've released all buffer pins */
2087  AtEOXact_Buffers(true);
2088 
2089  /* Clean up the relation cache */
2090  AtEOXact_RelationCache(true);
2091 
2092  /*
2093  * Make catalog changes visible to all backends. This has to happen after
2094  * relcache references are dropped (see comments for
2095  * AtEOXact_RelationCache), but before locks are released (if anyone is
2096  * waiting for lock on a relation we've modified, we want them to know
2097  * about the catalog change before they start using the relation).
2098  */
2099  AtEOXact_Inval(true);
2100 
2102 
2105  true, true);
2108  true, true);
2109 
2110  /*
2111  * Likewise, dropping of files deleted during the transaction is best done
2112  * after releasing relcache and buffer pins. (This is not strictly
2113  * necessary during commit, since such pins should have been released
2114  * already, but this ordering is definitely critical during abort.) Since
2115  * this may take many seconds, also delay until after releasing locks.
2116  * Other backends will observe the attendant catalog changes and not
2117  * attempt to access affected files.
2118  */
2119  smgrDoPendingDeletes(true);
2120 
2121  AtCommit_Notify();
2122  AtEOXact_GUC(true, 1);
2123  AtEOXact_SPI(true);
2125  AtEOXact_Namespace(true, is_parallel_worker);
2126  AtEOXact_SMgr();
2127  AtEOXact_Files();
2129  AtEOXact_HashTables(true);
2130  AtEOXact_PgStat(true);
2131  AtEOXact_Snapshot(true, false);
2132  AtEOXact_ApplyLauncher(true);
2134 
2135  CurrentResourceOwner = NULL;
2137  s->curTransactionOwner = NULL;
2140 
2141  AtCommit_Memory();
2142 
2145  s->nestingLevel = 0;
2146  s->gucNestLevel = 0;
2147  s->childXids = NULL;
2148  s->nChildXids = 0;
2149  s->maxChildXids = 0;
2150 
2153 
2154  /*
2155  * done with commit processing, set current transaction state back to
2156  * default
2157  */
2158  s->state = TRANS_DEFAULT;
2159 
2161 }
2162 
2163 
2164 /*
2165  * PrepareTransaction
2166  *
2167  * NB: if you change this routine, better look at CommitTransaction too!
2168  */
2169 static void
2171 {
2174  GlobalTransaction gxact;
2175  TimestampTz prepared_at;
2176 
2178 
2179  ShowTransactionState("PrepareTransaction");
2180 
2181  /*
2182  * check the current transaction state
2183  */
2184  if (s->state != TRANS_INPROGRESS)
2185  elog(WARNING, "PrepareTransaction while in %s state",
2187  Assert(s->parent == NULL);
2188 
2189  /*
2190  * Do pre-commit processing that involves calling user-defined code, such
2191  * as triggers. Since closing cursors could queue trigger actions,
2192  * triggers could open cursors, etc, we have to keep looping until there's
2193  * nothing left to do.
2194  */
2195  for (;;)
2196  {
2197  /*
2198  * Fire all currently pending deferred triggers.
2199  */
2201 
2202  /*
2203  * Close open portals (converting holdable ones into static portals).
2204  * If there weren't any, we are done ... otherwise loop back to check
2205  * if they queued deferred triggers. Lather, rinse, repeat.
2206  */
2207  if (!PreCommit_Portals(true))
2208  break;
2209  }
2210 
2212 
2213  /*
2214  * The remaining actions cannot call any user-defined code, so it's safe
2215  * to start shutting down within-transaction services. But note that most
2216  * of this stuff could still throw an error, which would switch us into
2217  * the transaction-abort path.
2218  */
2219 
2220  /* Shut down the deferred-trigger manager */
2221  AfterTriggerEndXact(true);
2222 
2223  /*
2224  * Let ON COMMIT management do its thing (must happen after closing
2225  * cursors, to avoid dangling-reference problems)
2226  */
2228 
2229  /* close large objects before lower-level cleanup */
2230  AtEOXact_LargeObject(true);
2231 
2232  /*
2233  * Mark serializable transaction as complete for predicate locking
2234  * purposes. This should be done as late as we can put it and still allow
2235  * errors to be raised for failure patterns found at commit.
2236  */
2238 
2239  /* NOTIFY will be handled below */
2240 
2241  /*
2242  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2243  * this transaction. Having the prepared xact hold locks on another
2244  * backend's temp table seems a bad idea --- for instance it would prevent
2245  * the backend from exiting. There are other problems too, such as how to
2246  * clean up the source backend's local buffers and ON COMMIT state if the
2247  * prepared xact includes a DROP of a temp table.
2248  *
2249  * We must check this after executing any ON COMMIT actions, because they
2250  * might still access a temp relation.
2251  *
2252  * XXX In principle this could be relaxed to allow some useful special
2253  * cases, such as a temp table created and dropped all within the
2254  * transaction. That seems to require much more bookkeeping though.
2255  */
2257  ereport(ERROR,
2258  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2259  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2260 
2261  /*
2262  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2263  * supported if we added cleanup logic to twophase.c, but for now it
2264  * doesn't seem worth the trouble.
2265  */
2267  ereport(ERROR,
2268  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2269  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2270 
2271  /*
2272  * Don't allow PREPARE but for transaction that has/might kill logical
2273  * replication workers.
2274  */
2276  ereport(ERROR,
2277  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2278  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
2279 
2280  /* Prevent cancel/die interrupt while cleaning up */
2281  HOLD_INTERRUPTS();
2282 
2283  /*
2284  * set the current transaction state information appropriately during
2285  * prepare processing
2286  */
2287  s->state = TRANS_PREPARE;
2288 
2289  prepared_at = GetCurrentTimestamp();
2290 
2291  /* Tell bufmgr and smgr to prepare for commit */
2292  BufmgrCommit();
2293 
2294  /*
2295  * Reserve the GID for this transaction. This could fail if the requested
2296  * GID is invalid or already in use.
2297  */
2298  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2299  GetUserId(), MyDatabaseId);
2300  prepareGID = NULL;
2301 
2302  /*
2303  * Collect data for the 2PC state file. Note that in general, no actual
2304  * state change should happen in the called modules during this step,
2305  * since it's still possible to fail before commit, and in that case we
2306  * want transaction abort to be able to clean up. (In particular, the
2307  * AtPrepare routines may error out if they find cases they cannot
2308  * handle.) State cleanup should happen in the PostPrepare routines
2309  * below. However, some modules can go ahead and clear state here because
2310  * they wouldn't do anything with it during abort anyway.
2311  *
2312  * Note: because the 2PC state file records will be replayed in the same
2313  * order they are made, the order of these calls has to match the order in
2314  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2315  * PREPARED; in particular, pay attention to whether things should happen
2316  * before or after releasing the transaction's locks.
2317  */
2318  StartPrepare(gxact);
2319 
2320  AtPrepare_Notify();
2321  AtPrepare_Locks();
2323  AtPrepare_PgStat();
2326 
2327  /*
2328  * Here is where we really truly prepare.
2329  *
2330  * We have to record transaction prepares even if we didn't make any
2331  * updates, because the transaction manager might get confused if we lose
2332  * a global transaction.
2333  */
2334  EndPrepare(gxact);
2335 
2336  /*
2337  * Now we clean up backend-internal state and release internal resources.
2338  */
2339 
2340  /* Reset XactLastRecEnd until the next transaction writes something */
2341  XactLastRecEnd = 0;
2342 
2343  /*
2344  * Let others know about no transaction in progress by me. This has to be
2345  * done *after* the prepared transaction has been marked valid, else
2346  * someone may think it is unlocked and recyclable.
2347  */
2349 
2350  /*
2351  * In normal commit-processing, this is all non-critical post-transaction
2352  * cleanup. When the transaction is prepared, however, it's important
2353  * that the locks and other per-backend resources are transferred to the
2354  * prepared transaction's PGPROC entry. Note that if an error is raised
2355  * here, it's too late to abort the transaction. XXX: This probably should
2356  * be in a critical section, to force a PANIC if any of this fails, but
2357  * that cure could be worse than the disease.
2358  */
2359 
2361 
2364  true, true);
2365 
2366  /* Check we've released all buffer pins */
2367  AtEOXact_Buffers(true);
2368 
2369  /* Clean up the relation cache */
2370  AtEOXact_RelationCache(true);
2371 
2372  /* notify doesn't need a postprepare call */
2373 
2375 
2377 
2378  PostPrepare_smgr();
2379 
2380  PostPrepare_MultiXact(xid);
2381 
2382  PostPrepare_Locks(xid);
2384 
2387  true, true);
2390  true, true);
2391 
2392  /*
2393  * Allow another backend to finish the transaction. After
2394  * PostPrepare_Twophase(), the transaction is completely detached from our
2395  * backend. The rest is just non-critical cleanup of backend-local state.
2396  */
2398 
2399  /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2400  AtEOXact_GUC(true, 1);
2401  AtEOXact_SPI(true);
2403  AtEOXact_Namespace(true, false);
2404  AtEOXact_SMgr();
2405  AtEOXact_Files();
2407  AtEOXact_HashTables(true);
2408  /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2409  AtEOXact_Snapshot(true, true);
2411 
2412  CurrentResourceOwner = NULL;
2414  s->curTransactionOwner = NULL;
2417 
2418  AtCommit_Memory();
2419 
2422  s->nestingLevel = 0;
2423  s->gucNestLevel = 0;
2424  s->childXids = NULL;
2425  s->nChildXids = 0;
2426  s->maxChildXids = 0;
2427 
2430 
2431  /*
2432  * done with 1st phase commit processing, set current transaction state
2433  * back to default
2434  */
2435  s->state = TRANS_DEFAULT;
2436 
2438 }
2439 
2440 
2441 /*
2442  * AbortTransaction
2443  */
2444 static void
2446 {
2448  TransactionId latestXid;
2449  bool is_parallel_worker;
2450 
2451  /* Prevent cancel/die interrupt while cleaning up */
2452  HOLD_INTERRUPTS();
2453 
2454  /* Make sure we have a valid memory context and resource owner */
2455  AtAbort_Memory();
2457 
2458  /*
2459  * Release any LW locks we might be holding as quickly as possible.
2460  * (Regular locks, however, must be held till we finish aborting.)
2461  * Releasing LW locks is critical since we might try to grab them again
2462  * while cleaning up!
2463  */
2464  LWLockReleaseAll();
2465 
2466  /* Clear wait information and command progress indicator */
2469 
2470  /* Clean up buffer I/O and buffer context locks, too */
2471  AbortBufferIO();
2472  UnlockBuffers();
2473 
2474  /* Reset WAL record construction state */
2476 
2477  /* Cancel condition variable sleep */
2479 
2480  /*
2481  * Also clean up any open wait for lock, since the lock manager will choke
2482  * if we try to wait for another lock before doing this.
2483  */
2484  LockErrorCleanup();
2485 
2486  /*
2487  * If any timeout events are still active, make sure the timeout interrupt
2488  * is scheduled. This covers possible loss of a timeout interrupt due to
2489  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2490  * We delay this till after LockErrorCleanup so that we don't uselessly
2491  * reschedule lock or deadlock check timeouts.
2492  */
2494 
2495  /*
2496  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2497  * handler. We do this fairly early in the sequence so that the timeout
2498  * infrastructure will be functional if needed while aborting.
2499  */
2501 
2502  /*
2503  * check the current transaction state
2504  */
2505  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2506  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2507  elog(WARNING, "AbortTransaction while in %s state",
2509  Assert(s->parent == NULL);
2510 
2511  /*
2512  * set the current transaction state information appropriately during the
2513  * abort processing
2514  */
2515  s->state = TRANS_ABORT;
2516 
2517  /*
2518  * Reset user ID which might have been changed transiently. We need this
2519  * to clean up in case control escaped out of a SECURITY DEFINER function
2520  * or other local change of CurrentUserId; therefore, the prior value of
2521  * SecurityRestrictionContext also needs to be restored.
2522  *
2523  * (Note: it is not necessary to restore session authorization or role
2524  * settings here because those can only be changed via GUC, and GUC will
2525  * take care of rolling them back if need be.)
2526  */
2528 
2529  /* If in parallel mode, clean up workers and exit parallel mode. */
2530  if (IsInParallelMode())
2531  {
2532  AtEOXact_Parallel(false);
2533  s->parallelModeLevel = 0;
2534  }
2535 
2536  /*
2537  * do abort processing
2538  */
2539  AfterTriggerEndXact(false); /* 'false' means it's abort */
2540  AtAbort_Portals();
2541  AtEOXact_LargeObject(false);
2542  AtAbort_Notify();
2543  AtEOXact_RelationMap(false);
2544  AtAbort_Twophase();
2545 
2546  /*
2547  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2548  * far as assigning an XID to advertise). But if we're inside a parallel
2549  * worker, skip this; the user backend must be the one to write the abort
2550  * record.
2551  */
2552  if (!is_parallel_worker)
2553  latestXid = RecordTransactionAbort(false);
2554  else
2555  {
2556  latestXid = InvalidTransactionId;
2557 
2558  /*
2559  * Since the parallel master won't get our value of XactLastRecEnd in
2560  * this case, we nudge WAL-writer ourselves in this case. See related
2561  * comments in RecordTransactionAbort for why this matters.
2562  */
2564  }
2565 
2566  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2567 
2568  /*
2569  * Let others know about no transaction in progress by me. Note that this
2570  * must be done _before_ releasing locks we hold and _after_
2571  * RecordTransactionAbort.
2572  */
2573  ProcArrayEndTransaction(MyProc, latestXid);
2574 
2575  /*
2576  * Post-abort cleanup. See notes in CommitTransaction() concerning
2577  * ordering. We can skip all of it if the transaction failed before
2578  * creating a resource owner.
2579  */
2580  if (TopTransactionResourceOwner != NULL)
2581  {
2582  if (is_parallel_worker)
2584  else
2586 
2589  false, true);
2590  AtEOXact_Buffers(false);
2591  AtEOXact_RelationCache(false);
2592  AtEOXact_Inval(false);
2596  false, true);
2599  false, true);
2600  smgrDoPendingDeletes(false);
2601 
2602  AtEOXact_GUC(false, 1);
2603  AtEOXact_SPI(false);
2605  AtEOXact_Namespace(false, is_parallel_worker);
2606  AtEOXact_SMgr();
2607  AtEOXact_Files();
2609  AtEOXact_HashTables(false);
2610  AtEOXact_PgStat(false);
2611  AtEOXact_ApplyLauncher(false);
2613  }
2614 
2615  /*
2616  * State remains TRANS_ABORT until CleanupTransaction().
2617  */
2619 }
2620 
2621 /*
2622  * CleanupTransaction
2623  */
2624 static void
2626 {
2628 
2629  /*
2630  * State should still be TRANS_ABORT from AbortTransaction().
2631  */
2632  if (s->state != TRANS_ABORT)
2633  elog(FATAL, "CleanupTransaction: unexpected state %s",
2635 
2636  /*
2637  * do abort cleanup processing
2638  */
2639  AtCleanup_Portals(); /* now safe to release portal memory */
2640  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2641 
2642  CurrentResourceOwner = NULL; /* and resource owner */
2645  s->curTransactionOwner = NULL;
2648 
2649  AtCleanup_Memory(); /* and transaction memory */
2650 
2653  s->nestingLevel = 0;
2654  s->gucNestLevel = 0;
2655  s->childXids = NULL;
2656  s->nChildXids = 0;
2657  s->maxChildXids = 0;
2658  s->parallelModeLevel = 0;
2659 
2662 
2663  /*
2664  * done with abort processing, set current transaction state back to
2665  * default
2666  */
2667  s->state = TRANS_DEFAULT;
2668 }
2669 
2670 /*
2671  * StartTransactionCommand
2672  */
2673 void
2675 {
2677 
2678  switch (s->blockState)
2679  {
2680  /*
2681  * if we aren't in a transaction block, we just do our usual start
2682  * transaction.
2683  */
2684  case TBLOCK_DEFAULT:
2685  StartTransaction();
2687  break;
2688 
2689  /*
2690  * We are somewhere in a transaction block or subtransaction and
2691  * about to start a new command. For now we do nothing, but
2692  * someday we may do command-local resource initialization. (Note
2693  * that any needed CommandCounterIncrement was done by the
2694  * previous CommitTransactionCommand.)
2695  */
2696  case TBLOCK_INPROGRESS:
2698  case TBLOCK_SUBINPROGRESS:
2699  break;
2700 
2701  /*
2702  * Here we are in a failed transaction block (one of the commands
2703  * caused an abort) so we do nothing but remain in the abort
2704  * state. Eventually we will get a ROLLBACK command which will
2705  * get us out of this state. (It is up to other code to ensure
2706  * that no commands other than ROLLBACK will be processed in these
2707  * states.)
2708  */
2709  case TBLOCK_ABORT:
2710  case TBLOCK_SUBABORT:
2711  break;
2712 
2713  /* These cases are invalid. */
2714  case TBLOCK_STARTED:
2715  case TBLOCK_BEGIN:
2717  case TBLOCK_SUBBEGIN:
2718  case TBLOCK_END:
2719  case TBLOCK_SUBRELEASE:
2720  case TBLOCK_SUBCOMMIT:
2721  case TBLOCK_ABORT_END:
2722  case TBLOCK_SUBABORT_END:
2723  case TBLOCK_ABORT_PENDING:
2725  case TBLOCK_SUBRESTART:
2727  case TBLOCK_PREPARE:
2728  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2730  break;
2731  }
2732 
2733  /*
2734  * We must switch to CurTransactionContext before returning. This is
2735  * already done if we called StartTransaction, otherwise not.
2736  */
2737  Assert(CurTransactionContext != NULL);
2739 }
2740 
2741 /*
2742  * CommitTransactionCommand
2743  */
2744 void
2746 {
2748 
2749  switch (s->blockState)
2750  {
2751  /*
2752  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2753  * StartTransactionCommand didn't set the STARTED state
2754  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2755  * by EndParallelWorkerTransaction(), not this function.
2756  */
2757  case TBLOCK_DEFAULT:
2759  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2761  break;
2762 
2763  /*
2764  * If we aren't in a transaction block, just do our usual
2765  * transaction commit, and return to the idle state.
2766  */
2767  case TBLOCK_STARTED:
2770  break;
2771 
2772  /*
2773  * We are completing a "BEGIN TRANSACTION" command, so we change
2774  * to the "transaction block in progress" state and return. (We
2775  * assume the BEGIN did nothing to the database, so we need no
2776  * CommandCounterIncrement.)
2777  */
2778  case TBLOCK_BEGIN:
2780  break;
2781 
2782  /*
2783  * This is the case when we have finished executing a command
2784  * someplace within a transaction block. We increment the command
2785  * counter and return.
2786  */
2787  case TBLOCK_INPROGRESS:
2789  case TBLOCK_SUBINPROGRESS:
2791  break;
2792 
2793  /*
2794  * We are completing a "COMMIT" command. Do it and return to the
2795  * idle state.
2796  */
2797  case TBLOCK_END:
2800  break;
2801 
2802  /*
2803  * Here we are in the middle of a transaction block but one of the
2804  * commands caused an abort so we do nothing but remain in the
2805  * abort state. Eventually we will get a ROLLBACK command.
2806  */
2807  case TBLOCK_ABORT:
2808  case TBLOCK_SUBABORT:
2809  break;
2810 
2811  /*
2812  * Here we were in an aborted transaction block and we just got
2813  * the ROLLBACK command from the user, so clean up the
2814  * already-aborted transaction and return to the idle state.
2815  */
2816  case TBLOCK_ABORT_END:
2819  break;
2820 
2821  /*
2822  * Here we were in a perfectly good transaction block but the user
2823  * told us to ROLLBACK anyway. We have to abort the transaction
2824  * and then clean up.
2825  */
2826  case TBLOCK_ABORT_PENDING:
2827  AbortTransaction();
2830  break;
2831 
2832  /*
2833  * We are completing a "PREPARE TRANSACTION" command. Do it and
2834  * return to the idle state.
2835  */
2836  case TBLOCK_PREPARE:
2839  break;
2840 
2841  /*
2842  * We were just issued a SAVEPOINT inside a transaction block.
2843  * Start a subtransaction. (DefineSavepoint already did
2844  * PushTransaction, so as to have someplace to put the SUBBEGIN
2845  * state.)
2846  */
2847  case TBLOCK_SUBBEGIN:
2850  break;
2851 
2852  /*
2853  * We were issued a RELEASE command, so we end the current
2854  * subtransaction and return to the parent transaction. The parent
2855  * might be ended too, so repeat till we find an INPROGRESS
2856  * transaction or subtransaction.
2857  */
2858  case TBLOCK_SUBRELEASE:
2859  do
2860  {
2862  s = CurrentTransactionState; /* changed by pop */
2863  } while (s->blockState == TBLOCK_SUBRELEASE);
2864 
2867  break;
2868 
2869  /*
2870  * We were issued a COMMIT, so we end the current subtransaction
2871  * hierarchy and perform final commit. We do this by rolling up
2872  * any subtransactions into their parent, which leads to O(N^2)
2873  * operations with respect to resource owners - this isn't that
2874  * bad until we approach a thousands of savepoints but is
2875  * necessary for correctness should after triggers create new
2876  * resource owners.
2877  */
2878  case TBLOCK_SUBCOMMIT:
2879  do
2880  {
2882  s = CurrentTransactionState; /* changed by pop */
2883  } while (s->blockState == TBLOCK_SUBCOMMIT);
2884  /* If we had a COMMIT command, finish off the main xact too */
2885  if (s->blockState == TBLOCK_END)
2886  {
2887  Assert(s->parent == NULL);
2890  }
2891  else if (s->blockState == TBLOCK_PREPARE)
2892  {
2893  Assert(s->parent == NULL);
2896  }
2897  else
2898  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2900  break;
2901 
2902  /*
2903  * The current already-failed subtransaction is ending due to a
2904  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2905  * examine the parent (which could be in any of several states).
2906  */
2907  case TBLOCK_SUBABORT_END:
2910  break;
2911 
2912  /*
2913  * As above, but it's not dead yet, so abort first.
2914  */
2919  break;
2920 
2921  /*
2922  * The current subtransaction is the target of a ROLLBACK TO
2923  * command. Abort and pop it, then start a new subtransaction
2924  * with the same name.
2925  */
2926  case TBLOCK_SUBRESTART:
2927  {
2928  char *name;
2929  int savepointLevel;
2930 
2931  /* save name and keep Cleanup from freeing it */
2932  name = s->name;
2933  s->name = NULL;
2934  savepointLevel = s->savepointLevel;
2935 
2938 
2939  DefineSavepoint(NULL);
2940  s = CurrentTransactionState; /* changed by push */
2941  s->name = name;
2943 
2944  /* This is the same as TBLOCK_SUBBEGIN case */
2948  }
2949  break;
2950 
2951  /*
2952  * Same as above, but the subtransaction had already failed, so we
2953  * don't need AbortSubTransaction.
2954  */
2956  {
2957  char *name;
2958  int savepointLevel;
2959 
2960  /* save name and keep Cleanup from freeing it */
2961  name = s->name;
2962  s->name = NULL;
2963  savepointLevel = s->savepointLevel;
2964 
2966 
2967  DefineSavepoint(NULL);
2968  s = CurrentTransactionState; /* changed by push */
2969  s->name = name;
2971 
2972  /* This is the same as TBLOCK_SUBBEGIN case */
2976  }
2977  break;
2978  }
2979 }
2980 
2981 /*
2982  * AbortCurrentTransaction
2983  */
2984 void
2986 {
2988 
2989  switch (s->blockState)
2990  {
2991  case TBLOCK_DEFAULT:
2992  if (s->state == TRANS_DEFAULT)
2993  {
2994  /* we are idle, so nothing to do */
2995  }
2996  else
2997  {
2998  /*
2999  * We can get here after an error during transaction start
3000  * (state will be TRANS_START). Need to clean up the
3001  * incompletely started transaction. First, adjust the
3002  * low-level state to suppress warning message from
3003  * AbortTransaction.
3004  */
3005  if (s->state == TRANS_START)
3006  s->state = TRANS_INPROGRESS;
3007  AbortTransaction();
3009  }
3010  break;
3011 
3012  /*
3013  * If we aren't in a transaction block, we just do the basic abort
3014  * & cleanup transaction. For this purpose, we treat an implicit
3015  * transaction block as if it were a simple statement.
3016  */
3017  case TBLOCK_STARTED:
3019  AbortTransaction();
3022  break;
3023 
3024  /*
3025  * If we are in TBLOCK_BEGIN it means something screwed up right
3026  * after reading "BEGIN TRANSACTION". We assume that the user
3027  * will interpret the error as meaning the BEGIN failed to get him
3028  * into a transaction block, so we should abort and return to idle
3029  * state.
3030  */
3031  case TBLOCK_BEGIN:
3032  AbortTransaction();
3035  break;
3036 
3037  /*
3038  * We are somewhere in a transaction block and we've gotten a
3039  * failure, so we abort the transaction and set up the persistent
3040  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3041  */
3042  case TBLOCK_INPROGRESS:
3044  AbortTransaction();
3045  s->blockState = TBLOCK_ABORT;
3046  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3047  break;
3048 
3049  /*
3050  * Here, we failed while trying to COMMIT. Clean up the
3051  * transaction and return to idle state (we do not want to stay in
3052  * the transaction).
3053  */
3054  case TBLOCK_END:
3055  AbortTransaction();
3058  break;
3059 
3060  /*
3061  * Here, we are already in an aborted transaction state and are
3062  * waiting for a ROLLBACK, but for some reason we failed again! So
3063  * we just remain in the abort state.
3064  */
3065  case TBLOCK_ABORT:
3066  case TBLOCK_SUBABORT:
3067  break;
3068 
3069  /*
3070  * We are in a failed transaction and we got the ROLLBACK command.
3071  * We have already aborted, we just need to cleanup and go to idle
3072  * state.
3073  */
3074  case TBLOCK_ABORT_END:
3077  break;
3078 
3079  /*
3080  * We are in a live transaction and we got a ROLLBACK command.
3081  * Abort, cleanup, go to idle state.
3082  */
3083  case TBLOCK_ABORT_PENDING:
3084  AbortTransaction();
3087  break;
3088 
3089  /*
3090  * Here, we failed while trying to PREPARE. Clean up the
3091  * transaction and return to idle state (we do not want to stay in
3092  * the transaction).
3093  */
3094  case TBLOCK_PREPARE:
3095  AbortTransaction();
3098  break;
3099 
3100  /*
3101  * We got an error inside a subtransaction. Abort just the
3102  * subtransaction, and go to the persistent SUBABORT state until
3103  * we get ROLLBACK.
3104  */
3105  case TBLOCK_SUBINPROGRESS:
3108  break;
3109 
3110  /*
3111  * If we failed while trying to create a subtransaction, clean up
3112  * the broken subtransaction and abort the parent. The same
3113  * applies if we get a failure while ending a subtransaction.
3114  */
3115  case TBLOCK_SUBBEGIN:
3116  case TBLOCK_SUBRELEASE:
3117  case TBLOCK_SUBCOMMIT:
3119  case TBLOCK_SUBRESTART:
3123  break;
3124 
3125  /*
3126  * Same as above, except the Abort() was already done.
3127  */
3128  case TBLOCK_SUBABORT_END:
3132  break;
3133  }
3134 }
3135 
3136 /*
3137  * PreventTransactionChain
3138  *
3139  * This routine is to be called by statements that must not run inside
3140  * a transaction block, typically because they have non-rollback-able
3141  * side effects or do internal commits.
3142  *
3143  * If we have already started a transaction block, issue an error; also issue
3144  * an error if we appear to be running inside a user-defined function (which
3145  * could issue more commands and possibly cause a failure after the statement
3146  * completes). Subtransactions are verboten too.
3147  *
3148  * isTopLevel: passed down from ProcessUtility to determine whether we are
3149  * inside a function. (We will always fail if this is false, but it's
3150  * convenient to centralize the check here instead of making callers do it.)
3151  * stmtType: statement type name, for error messages.
3152  */
3153 void
3154 PreventTransactionChain(bool isTopLevel, const char *stmtType)
3155 {
3156  /*
3157  * xact block already started?
3158  */
3159  if (IsTransactionBlock())
3160  ereport(ERROR,
3161  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3162  /* translator: %s represents an SQL statement name */
3163  errmsg("%s cannot run inside a transaction block",
3164  stmtType)));
3165 
3166  /*
3167  * subtransaction?
3168  */
3169  if (IsSubTransaction())
3170  ereport(ERROR,
3171  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3172  /* translator: %s represents an SQL statement name */
3173  errmsg("%s cannot run inside a subtransaction",
3174  stmtType)));
3175 
3176  /*
3177  * inside a function call?
3178  */
3179  if (!isTopLevel)
3180  ereport(ERROR,
3181  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3182  /* translator: %s represents an SQL statement name */
3183  errmsg("%s cannot be executed from a function", stmtType)));
3184 
3185  /* If we got past IsTransactionBlock test, should be in default state */
3186  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3187  CurrentTransactionState->blockState != TBLOCK_STARTED)
3188  elog(FATAL, "cannot prevent transaction chain");
3189  /* all okay */
3190 }
3191 
3192 /*
3193  * These two functions allow for warnings or errors if a command is
3194  * executed outside of a transaction block.
3195  *
3196  * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and
3197  * SET that have no effect issue warnings, all other no-effect commands
3198  * generate errors.
3199  */
3200 void
3201 WarnNoTransactionChain(bool isTopLevel, const char *stmtType)
3202 {
3203  CheckTransactionChain(isTopLevel, false, stmtType);
3204 }
3205 
3206 void
3207 RequireTransactionChain(bool isTopLevel, const char *stmtType)
3208 {
3209  CheckTransactionChain(isTopLevel, true, stmtType);
3210 }
3211 
3212 /*
3213  * RequireTransactionChain
3214  *
3215  * This routine is to be called by statements that must run inside
3216  * a transaction block, because they have no effects that persist past
3217  * transaction end (and so calling them outside a transaction block
3218  * is presumably an error). DECLARE CURSOR is an example.
3219  *
3220  * If we appear to be running inside a user-defined function, we do not
3221  * issue anything, since the function could issue more commands that make
3222  * use of the current statement's results. Likewise subtransactions.
3223  * Thus this is an inverse for PreventTransactionChain.
3224  *
3225  * isTopLevel: passed down from ProcessUtility to determine whether we are
3226  * inside a function.
3227  * stmtType: statement type name, for warning or error messages.
3228  */
3229 static void
3230 CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
3231 {
3232  /*
3233  * xact block already started?
3234  */
3235  if (IsTransactionBlock())
3236  return;
3237 
3238  /*
3239  * subtransaction?
3240  */
3241  if (IsSubTransaction())
3242  return;
3243 
3244  /*
3245  * inside a function call?
3246  */
3247  if (!isTopLevel)
3248  return;
3249 
3250  ereport(throwError ? ERROR : WARNING,
3251  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3252  /* translator: %s represents an SQL statement name */
3253  errmsg("%s can only be used in transaction blocks",
3254  stmtType)));
3255  return;
3256 }
3257 
3258 /*
3259  * IsInTransactionChain
3260  *
3261  * This routine is for statements that need to behave differently inside
3262  * a transaction block than when running as single commands. ANALYZE is
3263  * currently the only example.
3264  *
3265  * isTopLevel: passed down from ProcessUtility to determine whether we are
3266  * inside a function.
3267  */
3268 bool
3269 IsInTransactionChain(bool isTopLevel)
3270 {
3271  /*
3272  * Return true on same conditions that would make PreventTransactionChain
3273  * error out
3274  */
3275  if (IsTransactionBlock())
3276  return true;
3277 
3278  if (IsSubTransaction())
3279  return true;
3280 
3281  if (!isTopLevel)
3282  return true;
3283 
3284  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3285  CurrentTransactionState->blockState != TBLOCK_STARTED)
3286  return true;
3287 
3288  return false;
3289 }
3290 
3291 
3292 /*
3293  * Register or deregister callback functions for start- and end-of-xact
3294  * operations.
3295  *
3296  * These functions are intended for use by dynamically loaded modules.
3297  * For built-in modules we generally just hardwire the appropriate calls
3298  * (mainly because it's easier to control the order that way, where needed).
3299  *
3300  * At transaction end, the callback occurs post-commit or post-abort, so the
3301  * callback functions can only do noncritical cleanup.
3302  */
3303 void
3305 {
3306  XactCallbackItem *item;
3307 
3308  item = (XactCallbackItem *)
3310  item->callback = callback;
3311  item->arg = arg;
3312  item->next = Xact_callbacks;
3313  Xact_callbacks = item;
3314 }
3315 
3316 void
3318 {
3319  XactCallbackItem *item;
3320  XactCallbackItem *prev;
3321 
3322  prev = NULL;
3323  for (item = Xact_callbacks; item; prev = item, item = item->next)
3324  {
3325  if (item->callback == callback && item->arg == arg)
3326  {
3327  if (prev)
3328  prev->next = item->next;
3329  else
3330  Xact_callbacks = item->next;
3331  pfree(item);
3332  break;
3333  }
3334  }
3335 }
3336 
3337 static void
3339 {
3340  XactCallbackItem *item;
3341 
3342  for (item = Xact_callbacks; item; item = item->next)
3343  item->callback(event, item->arg);
3344 }
3345 
3346 
3347 /*
3348  * Register or deregister callback functions for start- and end-of-subxact
3349  * operations.
3350  *
3351  * Pretty much same as above, but for subtransaction events.
3352  *
3353  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3354  * so the callback functions can only do noncritical cleanup. At
3355  * subtransaction start, the callback is called when the subtransaction has
3356  * finished initializing.
3357  */
3358 void
3360 {
3361  SubXactCallbackItem *item;
3362 
3363  item = (SubXactCallbackItem *)
3365  item->callback = callback;
3366  item->arg = arg;
3367  item->next = SubXact_callbacks;
3368  SubXact_callbacks = item;
3369 }
3370 
3371 void
3373 {
3374  SubXactCallbackItem *item;
3375  SubXactCallbackItem *prev;
3376 
3377  prev = NULL;
3378  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3379  {
3380  if (item->callback == callback && item->arg == arg)
3381  {
3382  if (prev)
3383  prev->next = item->next;
3384  else
3385  SubXact_callbacks = item->next;
3386  pfree(item);
3387  break;
3388  }
3389  }
3390 }
3391 
3392 static void
3394  SubTransactionId mySubid,
3395  SubTransactionId parentSubid)
3396 {
3397  SubXactCallbackItem *item;
3398 
3399  for (item = SubXact_callbacks; item; item = item->next)
3400  item->callback(event, mySubid, parentSubid, item->arg);
3401 }
3402 
3403 
3404 /* ----------------------------------------------------------------
3405  * transaction block support
3406  * ----------------------------------------------------------------
3407  */
3408 
3409 /*
3410  * BeginTransactionBlock
3411  * This executes a BEGIN command.
3412  */
3413 void
3415 {
3417 
3418  switch (s->blockState)
3419  {
3420  /*
3421  * We are not inside a transaction block, so allow one to begin.
3422  */
3423  case TBLOCK_STARTED:
3424  s->blockState = TBLOCK_BEGIN;
3425  break;
3426 
3427  /*
3428  * BEGIN converts an implicit transaction block to a regular one.
3429  * (Note that we allow this even if we've already done some
3430  * commands, which is a bit odd but matches historical practice.)
3431  */
3433  s->blockState = TBLOCK_BEGIN;
3434  break;
3435 
3436  /*
3437  * Already a transaction block in progress.
3438  */
3439  case TBLOCK_INPROGRESS:
3441  case TBLOCK_SUBINPROGRESS:
3442  case TBLOCK_ABORT:
3443  case TBLOCK_SUBABORT:
3444  ereport(WARNING,
3445  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3446  errmsg("there is already a transaction in progress")));
3447  break;
3448 
3449  /* These cases are invalid. */
3450  case TBLOCK_DEFAULT:
3451  case TBLOCK_BEGIN:
3452  case TBLOCK_SUBBEGIN:
3453  case TBLOCK_END:
3454  case TBLOCK_SUBRELEASE:
3455  case TBLOCK_SUBCOMMIT:
3456  case TBLOCK_ABORT_END:
3457  case TBLOCK_SUBABORT_END:
3458  case TBLOCK_ABORT_PENDING:
3460  case TBLOCK_SUBRESTART:
3462  case TBLOCK_PREPARE:
3463  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3465  break;
3466  }
3467 }
3468 
3469 /*
3470  * PrepareTransactionBlock
3471  * This executes a PREPARE command.
3472  *
3473  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3474  * happened: true for PREPARE, false for ROLLBACK.
3475  *
3476  * Note that we don't actually do anything here except change blockState.
3477  * The real work will be done in the upcoming PrepareTransaction().
3478  * We do it this way because it's not convenient to change memory context,
3479  * resource owner, etc while executing inside a Portal.
3480  */
3481 bool
3482 PrepareTransactionBlock(const char *gid)
3483 {
3484  TransactionState s;
3485  bool result;
3486 
3487  /* Set up to commit the current transaction */
3488  result = EndTransactionBlock();
3489 
3490  /* If successful, change outer tblock state to PREPARE */
3491  if (result)
3492  {
3494 
3495  while (s->parent != NULL)
3496  s = s->parent;
3497 
3498  if (s->blockState == TBLOCK_END)
3499  {
3500  /* Save GID where PrepareTransaction can find it again */
3502 
3504  }
3505  else
3506  {
3507  /*
3508  * ignore case where we are not in a transaction;
3509  * EndTransactionBlock already issued a warning.
3510  */
3513  /* Don't send back a PREPARE result tag... */
3514  result = false;
3515  }
3516  }
3517 
3518  return result;
3519 }
3520 
3521 /*
3522  * EndTransactionBlock
3523  * This executes a COMMIT command.
3524  *
3525  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3526  * happened: true for COMMIT, false for ROLLBACK.
3527  *
3528  * Note that we don't actually do anything here except change blockState.
3529  * The real work will be done in the upcoming CommitTransactionCommand().
3530  * We do it this way because it's not convenient to change memory context,
3531  * resource owner, etc while executing inside a Portal.
3532  */
3533 bool
3535 {
3537  bool result = false;
3538 
3539  switch (s->blockState)
3540  {
3541  /*
3542  * We are in a transaction block, so tell CommitTransactionCommand
3543  * to COMMIT.
3544  */
3545  case TBLOCK_INPROGRESS:
3546  s->blockState = TBLOCK_END;
3547  result = true;
3548  break;
3549 
3550  /*
3551  * In an implicit transaction block, commit, but issue a warning
3552  * because there was no explicit BEGIN before this.
3553  */
3555  ereport(WARNING,
3556  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3557  errmsg("there is no transaction in progress")));
3558  s->blockState = TBLOCK_END;
3559  result = true;
3560  break;
3561 
3562  /*
3563  * We are in a failed transaction block. Tell
3564  * CommitTransactionCommand it's time to exit the block.
3565  */
3566  case TBLOCK_ABORT:
3568  break;
3569 
3570  /*
3571  * We are in a live subtransaction block. Set up to subcommit all
3572  * open subtransactions and then commit the main transaction.
3573  */
3574  case TBLOCK_SUBINPROGRESS:
3575  while (s->parent != NULL)
3576  {
3577  if (s->blockState == TBLOCK_SUBINPROGRESS)
3579  else
3580  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3582  s = s->parent;
3583  }
3584  if (s->blockState == TBLOCK_INPROGRESS)
3585  s->blockState = TBLOCK_END;
3586  else
3587  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3589  result = true;
3590  break;
3591 
3592  /*
3593  * Here we are inside an aborted subtransaction. Treat the COMMIT
3594  * as ROLLBACK: set up to abort everything and exit the main
3595  * transaction.
3596  */
3597  case TBLOCK_SUBABORT:
3598  while (s->parent != NULL)
3599  {
3600  if (s->blockState == TBLOCK_SUBINPROGRESS)
3602  else if (s->blockState == TBLOCK_SUBABORT)
3604  else
3605  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3607  s = s->parent;
3608  }
3609  if (s->blockState == TBLOCK_INPROGRESS)
3611  else if (s->blockState == TBLOCK_ABORT)
3613  else
3614  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3616  break;
3617 
3618  /*
3619  * The user issued COMMIT when not inside a transaction. Issue a
3620  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3621  * CommitTransactionCommand() will then close the transaction and
3622  * put us back into the default state.
3623  */
3624  case TBLOCK_STARTED:
3625  ereport(WARNING,
3626  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3627  errmsg("there is no transaction in progress")));
3628  result = true;
3629  break;
3630 
3631  /*
3632  * The user issued a COMMIT that somehow ran inside a parallel
3633  * worker. We can't cope with that.
3634  */
3636  ereport(FATAL,
3637  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3638  errmsg("cannot commit during a parallel operation")));
3639  break;
3640 
3641  /* These cases are invalid. */
3642  case TBLOCK_DEFAULT:
3643  case TBLOCK_BEGIN:
3644  case TBLOCK_SUBBEGIN:
3645  case TBLOCK_END:
3646  case TBLOCK_SUBRELEASE:
3647  case TBLOCK_SUBCOMMIT:
3648  case TBLOCK_ABORT_END:
3649  case TBLOCK_SUBABORT_END:
3650  case TBLOCK_ABORT_PENDING:
3652  case TBLOCK_SUBRESTART:
3654  case TBLOCK_PREPARE:
3655  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3657  break;
3658  }
3659 
3660  return result;
3661 }
3662 
3663 /*
3664  * UserAbortTransactionBlock
3665  * This executes a ROLLBACK command.
3666  *
3667  * As above, we don't actually do anything here except change blockState.
3668  */
3669 void
3671 {
3673 
3674  switch (s->blockState)
3675  {
3676  /*
3677  * We are inside a transaction block and we got a ROLLBACK command
3678  * from the user, so tell CommitTransactionCommand to abort and
3679  * exit the transaction block.
3680  */
3681  case TBLOCK_INPROGRESS:
3683  break;
3684 
3685  /*
3686  * We are inside a failed transaction block and we got a ROLLBACK
3687  * command from the user. Abort processing is already done, so
3688  * CommitTransactionCommand just has to cleanup and go back to
3689  * idle state.
3690  */
3691  case TBLOCK_ABORT:
3693  break;
3694 
3695  /*
3696  * We are inside a subtransaction. Mark everything up to top
3697  * level as exitable.
3698  */
3699  case TBLOCK_SUBINPROGRESS:
3700  case TBLOCK_SUBABORT:
3701  while (s->parent != NULL)
3702  {
3703  if (s->blockState == TBLOCK_SUBINPROGRESS)
3705  else if (s->blockState == TBLOCK_SUBABORT)
3707  else
3708  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3710  s = s->parent;
3711  }
3712  if (s->blockState == TBLOCK_INPROGRESS)
3714  else if (s->blockState == TBLOCK_ABORT)
3716  else
3717  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3719  break;
3720 
3721  /*
3722  * The user issued ABORT when not inside a transaction. Issue a
3723  * WARNING and go to abort state. The upcoming call to
3724  * CommitTransactionCommand() will then put us back into the
3725  * default state.
3726  *
3727  * We do the same thing with ABORT inside an implicit transaction,
3728  * although in this case we might be rolling back actual database
3729  * state changes. (It's debatable whether we should issue a
3730  * WARNING in this case, but we have done so historically.)
3731  */
3732  case TBLOCK_STARTED:
3734  ereport(WARNING,
3735  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3736  errmsg("there is no transaction in progress")));
3738  break;
3739 
3740  /*
3741  * The user issued an ABORT that somehow ran inside a parallel
3742  * worker. We can't cope with that.
3743  */
3745  ereport(FATAL,
3746  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3747  errmsg("cannot abort during a parallel operation")));
3748  break;
3749 
3750  /* These cases are invalid. */
3751  case TBLOCK_DEFAULT:
3752  case TBLOCK_BEGIN:
3753  case TBLOCK_SUBBEGIN:
3754  case TBLOCK_END:
3755  case TBLOCK_SUBRELEASE:
3756  case TBLOCK_SUBCOMMIT:
3757  case TBLOCK_ABORT_END:
3758  case TBLOCK_SUBABORT_END:
3759  case TBLOCK_ABORT_PENDING:
3761  case TBLOCK_SUBRESTART:
3763  case TBLOCK_PREPARE:
3764  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3766  break;
3767  }
3768 }
3769 
3770 /*
3771  * BeginImplicitTransactionBlock
3772  * Start an implicit transaction block if we're not already in one.
3773  *
3774  * Unlike BeginTransactionBlock, this is called directly from the main loop
3775  * in postgres.c, not within a Portal. So we can just change blockState
3776  * without a lot of ceremony. We do not expect caller to do
3777  * CommitTransactionCommand/StartTransactionCommand.
3778  */
3779 void
3781 {
3783 
3784  /*
3785  * If we are in STARTED state (that is, no transaction block is open),
3786  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3787  * block.
3788  *
3789  * For caller convenience, we consider all other transaction states as
3790  * legal here; otherwise the caller would need its own state check, which
3791  * seems rather pointless.
3792  */
3793  if (s->blockState == TBLOCK_STARTED)
3795 }
3796 
3797 /*
3798  * EndImplicitTransactionBlock
3799  * End an implicit transaction block, if we're in one.
3800  *
3801  * Like EndTransactionBlock, we just make any needed blockState change here.
3802  * The real work will be done in the upcoming CommitTransactionCommand().
3803  */
3804 void
3806 {
3808 
3809  /*
3810  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3811  * allowing CommitTransactionCommand to commit whatever happened during
3812  * the implicit transaction block as though it were a single statement.
3813  *
3814  * For caller convenience, we consider all other transaction states as
3815  * legal here; otherwise the caller would need its own state check, which
3816  * seems rather pointless.
3817  */
3820 }
3821 
3822 /*
3823  * DefineSavepoint
3824  * This executes a SAVEPOINT command.
3825  */
3826 void
3827 DefineSavepoint(const char *name)
3828 {
3830 
3831  /*
3832  * Workers synchronize transaction state at the beginning of each parallel
3833  * operation, so we can't account for new subtransactions after that
3834  * point. (Note that this check will certainly error out if s->blockState
3835  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3836  * below.)
3837  */
3838  if (IsInParallelMode())
3839  ereport(ERROR,
3840  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3841  errmsg("cannot define savepoints during a parallel operation")));
3842 
3843  switch (s->blockState)
3844  {
3845  case TBLOCK_INPROGRESS:
3846  case TBLOCK_SUBINPROGRESS:
3847  /* Normal subtransaction start */
3848  PushTransaction();
3849  s = CurrentTransactionState; /* changed by push */
3850 
3851  /*
3852  * Savepoint names, like the TransactionState block itself, live
3853  * in TopTransactionContext.
3854  */
3855  if (name)
3857  break;
3858 
3859  /*
3860  * We disallow savepoint commands in implicit transaction blocks.
3861  * There would be no great difficulty in allowing them so far as
3862  * this module is concerned, but a savepoint seems inconsistent
3863  * with exec_simple_query's behavior of abandoning the whole query
3864  * string upon error. Also, the point of an implicit transaction
3865  * block (as opposed to a regular one) is to automatically close
3866  * after an error, so it's hard to see how a savepoint would fit
3867  * into that.
3868  *
3869  * The error messages for this are phrased as if there were no
3870  * active transaction block at all, which is historical but
3871  * perhaps could be improved.
3872  */
3874  ereport(ERROR,
3875  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3876  /* translator: %s represents an SQL statement name */
3877  errmsg("%s can only be used in transaction blocks",
3878  "SAVEPOINT")));
3879  break;
3880 
3881  /* These cases are invalid. */
3882  case TBLOCK_DEFAULT:
3883  case TBLOCK_STARTED:
3884  case TBLOCK_BEGIN:
3886  case TBLOCK_SUBBEGIN:
3887  case TBLOCK_END:
3888  case TBLOCK_SUBRELEASE:
3889  case TBLOCK_SUBCOMMIT:
3890  case TBLOCK_ABORT:
3891  case TBLOCK_SUBABORT:
3892  case TBLOCK_ABORT_END:
3893  case TBLOCK_SUBABORT_END:
3894  case TBLOCK_ABORT_PENDING:
3896  case TBLOCK_SUBRESTART:
3898  case TBLOCK_PREPARE:
3899  elog(FATAL, "DefineSavepoint: unexpected state %s",
3901  break;
3902  }
3903 }
3904 
3905 /*
3906  * ReleaseSavepoint
3907  * This executes a RELEASE command.
3908  *
3909  * As above, we don't actually do anything here except change blockState.
3910  */
3911 void
3913 {
3915  TransactionState target,
3916  xact;
3917  ListCell *cell;
3918  char *name = NULL;
3919 
3920  /*
3921  * Workers synchronize transaction state at the beginning of each parallel
3922  * operation, so we can't account for transaction state change after that
3923  * point. (Note that this check will certainly error out if s->blockState
3924  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3925  * below.)
3926  */
3927  if (IsInParallelMode())
3928  ereport(ERROR,
3929  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3930  errmsg("cannot release savepoints during a parallel operation")));
3931 
3932  switch (s->blockState)
3933  {
3934  /*
3935  * We can't release a savepoint if there is no savepoint defined.
3936  */
3937  case TBLOCK_INPROGRESS:
3938  ereport(ERROR,
3939  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3940  errmsg("no such savepoint")));
3941  break;
3942 
3944  /* See comment about implicit transactions in DefineSavepoint */
3945  ereport(ERROR,
3946  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3947  /* translator: %s represents an SQL statement name */
3948  errmsg("%s can only be used in transaction blocks",
3949  "RELEASE SAVEPOINT")));
3950  break;
3951 
3952  /*
3953  * We are in a non-aborted subtransaction. This is the only valid
3954  * case.
3955  */
3956  case TBLOCK_SUBINPROGRESS:
3957  break;
3958 
3959  /* These cases are invalid. */
3960  case TBLOCK_DEFAULT:
3961  case TBLOCK_STARTED:
3962  case TBLOCK_BEGIN:
3964  case TBLOCK_SUBBEGIN:
3965  case TBLOCK_END:
3966  case TBLOCK_SUBRELEASE:
3967  case TBLOCK_SUBCOMMIT:
3968  case TBLOCK_ABORT:
3969  case TBLOCK_SUBABORT:
3970  case TBLOCK_ABORT_END:
3971  case TBLOCK_SUBABORT_END:
3972  case TBLOCK_ABORT_PENDING:
3974  case TBLOCK_SUBRESTART:
3976  case TBLOCK_PREPARE:
3977  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3979  break;
3980  }
3981 
3982  foreach(cell, options)
3983  {
3984  DefElem *elem = lfirst(cell);
3985 
3986  if (strcmp(elem->defname, "savepoint_name") == 0)
3987  name = strVal(elem->arg);
3988  }
3989 
3990  Assert(PointerIsValid(name));
3991 
3992  for (target = s; PointerIsValid(target); target = target->parent)
3993  {
3994  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3995  break;
3996  }
3997 
3998  if (!PointerIsValid(target))
3999  ereport(ERROR,
4000  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4001  errmsg("no such savepoint")));
4002 
4003  /* disallow crossing savepoint level boundaries */
4004  if (target->savepointLevel != s->savepointLevel)
4005  ereport(ERROR,
4006  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4007  errmsg("no such savepoint")));
4008 
4009  /*
4010  * Mark "commit pending" all subtransactions up to the target
4011  * subtransaction. The actual commits will happen when control gets to
4012  * CommitTransactionCommand.
4013  */
4014  xact = CurrentTransactionState;
4015  for (;;)
4016  {
4018  xact->blockState = TBLOCK_SUBRELEASE;
4019  if (xact == target)
4020  break;
4021  xact = xact->parent;
4022  Assert(PointerIsValid(xact));
4023  }
4024 }
4025 
4026 /*
4027  * RollbackToSavepoint
4028  * This executes a ROLLBACK TO <savepoint> command.
4029  *
4030  * As above, we don't actually do anything here except change blockState.
4031  */
4032 void
4034 {
4036  TransactionState target,
4037  xact;
4038  ListCell *cell;
4039  char *name = NULL;
4040 
4041  /*
4042  * Workers synchronize transaction state at the beginning of each parallel
4043  * operation, so we can't account for transaction state change after that
4044  * point. (Note that this check will certainly error out if s->blockState
4045  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4046  * below.)
4047  */
4048  if (IsInParallelMode())
4049  ereport(ERROR,
4050  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4051  errmsg("cannot rollback to savepoints during a parallel operation")));
4052 
4053  switch (s->blockState)
4054  {
4055  /*
4056  * We can't rollback to a savepoint if there is no savepoint
4057  * defined.
4058  */
4059  case TBLOCK_INPROGRESS:
4060  case TBLOCK_ABORT:
4061  ereport(ERROR,
4062  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4063  errmsg("no such savepoint")));
4064  break;
4065 
4067  /* See comment about implicit transactions in DefineSavepoint */
4068  ereport(ERROR,
4069  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4070  /* translator: %s represents an SQL statement name */
4071  errmsg("%s can only be used in transaction blocks",
4072  "ROLLBACK TO SAVEPOINT")));
4073  break;
4074 
4075  /*
4076  * There is at least one savepoint, so proceed.
4077  */
4078  case TBLOCK_SUBINPROGRESS:
4079  case TBLOCK_SUBABORT:
4080  break;
4081 
4082  /* These cases are invalid. */
4083  case TBLOCK_DEFAULT:
4084  case TBLOCK_STARTED:
4085  case TBLOCK_BEGIN:
4087  case TBLOCK_SUBBEGIN:
4088  case TBLOCK_END:
4089  case TBLOCK_SUBRELEASE:
4090  case TBLOCK_SUBCOMMIT:
4091  case TBLOCK_ABORT_END:
4092  case TBLOCK_SUBABORT_END:
4093  case TBLOCK_ABORT_PENDING:
4095  case TBLOCK_SUBRESTART:
4097  case TBLOCK_PREPARE:
4098  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4100  break;
4101  }
4102 
4103  foreach(cell, options)
4104  {
4105  DefElem *elem = lfirst(cell);
4106 
4107  if (strcmp(elem->defname, "savepoint_name") == 0)
4108  name = strVal(elem->arg);
4109  }
4110 
4111  Assert(PointerIsValid(name));
4112 
4113  for (target = s; PointerIsValid(target); target = target->parent)
4114  {
4115  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4116  break;
4117  }
4118 
4119  if (!PointerIsValid(target))
4120  ereport(ERROR,
4121  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4122  errmsg("no such savepoint")));
4123 
4124  /* disallow crossing savepoint level boundaries */
4125  if (target->savepointLevel != s->savepointLevel)
4126  ereport(ERROR,
4127  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4128  errmsg("no such savepoint")));
4129 
4130  /*
4131  * Mark "abort pending" all subtransactions up to the target
4132  * subtransaction. The actual aborts will happen when control gets to
4133  * CommitTransactionCommand.
4134  */
4135  xact = CurrentTransactionState;
4136  for (;;)
4137  {
4138  if (xact == target)
4139  break;
4140  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4142  else if (xact->blockState == TBLOCK_SUBABORT)
4144  else
4145  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4147  xact = xact->parent;
4148  Assert(PointerIsValid(xact));
4149  }
4150 
4151  /* And mark the target as "restart pending" */
4152  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4153  xact->blockState = TBLOCK_SUBRESTART;
4154  else if (xact->blockState == TBLOCK_SUBABORT)
4156  else
4157  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4159 }
4160 
4161 /*
4162  * BeginInternalSubTransaction
4163  * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4164  * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_END, and TBLOCK_PREPARE states,
4165  * and therefore it can safely be used in functions that might be called
4166  * when not inside a BEGIN block or when running deferred triggers at
4167  * COMMIT/PREPARE time. Also, it automatically does
4168  * CommitTransactionCommand/StartTransactionCommand instead of expecting
4169  * the caller to do it.
4170  */
4171 void
4173 {
4175 
4176  /*
4177  * Workers synchronize transaction state at the beginning of each parallel
4178  * operation, so we can't account for new subtransactions after that
4179  * point. We might be able to make an exception for the type of
4180  * subtransaction established by this function, which is typically used in
4181  * contexts where we're going to release or roll back the subtransaction
4182  * before proceeding further, so that no enduring change to the
4183  * transaction state occurs. For now, however, we prohibit this case along
4184  * with all the others.
4185  */
4186  if (IsInParallelMode())
4187  ereport(ERROR,
4188  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4189  errmsg("cannot start subtransactions during a parallel operation")));
4190 
4191  switch (s->blockState)
4192  {
4193  case TBLOCK_STARTED:
4194  case TBLOCK_INPROGRESS:
4196  case TBLOCK_END:
4197  case TBLOCK_PREPARE:
4198  case TBLOCK_SUBINPROGRESS:
4199  /* Normal subtransaction start */
4200  PushTransaction();
4201  s = CurrentTransactionState; /* changed by push */
4202 
4203  /*
4204  * Savepoint names, like the TransactionState block itself, live
4205  * in TopTransactionContext.
4206  */
4207  if (name)
4209  break;
4210 
4211  /* These cases are invalid. */
4212  case TBLOCK_DEFAULT:
4213  case TBLOCK_BEGIN:
4215  case TBLOCK_SUBBEGIN:
4216  case TBLOCK_SUBRELEASE:
4217  case TBLOCK_SUBCOMMIT:
4218  case TBLOCK_ABORT:
4219  case TBLOCK_SUBABORT:
4220  case TBLOCK_ABORT_END:
4221  case TBLOCK_SUBABORT_END:
4222  case TBLOCK_ABORT_PENDING:
4224  case TBLOCK_SUBRESTART:
4226  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4228  break;
4229  }
4230 
4233 }
4234 
4235 /*
4236  * ReleaseCurrentSubTransaction
4237  *
4238  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4239  * savepoint name (if any).
4240  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4241  */
4242 void
4244 {
4246 
4247  /*
4248  * Workers synchronize transaction state at the beginning of each parallel
4249  * operation, so we can't account for commit of subtransactions after that
4250  * point. This should not happen anyway. Code calling this would
4251  * typically have called BeginInternalSubTransaction() first, failing
4252  * there.
4253  */
4254  if (IsInParallelMode())
4255  ereport(ERROR,
4256  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4257  errmsg("cannot commit subtransactions during a parallel operation")));
4258 
4259  if (s->blockState != TBLOCK_SUBINPROGRESS)
4260  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4262  Assert(s->state == TRANS_INPROGRESS);
4265  s = CurrentTransactionState; /* changed by pop */
4266  Assert(s->state == TRANS_INPROGRESS);
4267 }
4268 
4269 /*
4270  * RollbackAndReleaseCurrentSubTransaction
4271  *
4272  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4273  * of its savepoint name (if any).
4274  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4275  */
4276 void
4278 {
4280 
4281  /*
4282  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4283  * during parallel operations. That's because we may be in the master,
4284  * recovering from an error thrown while we were in parallel mode. We
4285  * won't reach here in a worker, because BeginInternalSubTransaction()
4286  * will have failed.
4287  */
4288 
4289  switch (s->blockState)
4290  {
4291  /* Must be in a subtransaction */
4292  case TBLOCK_SUBINPROGRESS:
4293  case TBLOCK_SUBABORT:
4294  break;
4295 
4296  /* These cases are invalid. */
4297  case TBLOCK_DEFAULT:
4298  case TBLOCK_STARTED:
4299  case TBLOCK_BEGIN:
4302  case TBLOCK_SUBBEGIN:
4303  case TBLOCK_INPROGRESS:
4304  case TBLOCK_END:
4305  case TBLOCK_SUBRELEASE:
4306  case TBLOCK_SUBCOMMIT:
4307  case TBLOCK_ABORT:
4308  case TBLOCK_ABORT_END:
4309  case TBLOCK_SUBABORT_END:
4310  case TBLOCK_ABORT_PENDING:
4312  case TBLOCK_SUBRESTART:
4314  case TBLOCK_PREPARE:
4315  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4317  break;
4318  }
4319 
4320  /*
4321  * Abort the current subtransaction, if needed.
4322  */
4323  if (s->blockState == TBLOCK_SUBINPROGRESS)
4325 
4326  /* And clean it up, too */
4328 
4329  s = CurrentTransactionState; /* changed by pop */
4331  s->blockState == TBLOCK_INPROGRESS ||
4333  s->blockState == TBLOCK_STARTED);
4334 }
4335 
4336 /*
4337  * AbortOutOfAnyTransaction
4338  *
4339  * This routine is provided for error recovery purposes. It aborts any
4340  * active transaction or transaction block, leaving the system in a known
4341  * idle state.
4342  */
4343 void
4345 {
4347 
4348  /* Ensure we're not running in a doomed memory context */
4349  AtAbort_Memory();
4350 
4351  /*
4352  * Get out of any transaction or nested transaction
4353  */
4354  do
4355  {
4356  switch (s->blockState)
4357  {
4358  case TBLOCK_DEFAULT:
4359  if (s->state == TRANS_DEFAULT)
4360  {
4361  /* Not in a transaction, do nothing */
4362  }
4363  else
4364  {
4365  /*
4366  * We can get here after an error during transaction start
4367  * (state will be TRANS_START). Need to clean up the
4368  * incompletely started transaction. First, adjust the
4369  * low-level state to suppress warning message from
4370  * AbortTransaction.
4371  */
4372  if (s->state == TRANS_START)
4373  s->state = TRANS_INPROGRESS;
4374  AbortTransaction();
4376  }
4377  break;
4378  case TBLOCK_STARTED:
4379  case TBLOCK_BEGIN:
4380  case TBLOCK_INPROGRESS:
4383  case TBLOCK_END:
4384  case TBLOCK_ABORT_PENDING:
4385  case TBLOCK_PREPARE:
4386  /* In a transaction, so clean up */
4387  AbortTransaction();
4390  break;
4391  case TBLOCK_ABORT:
4392  case TBLOCK_ABORT_END:
4393 
4394  /*
4395  * AbortTransaction is already done, still need Cleanup.
4396  * However, if we failed partway through running ROLLBACK,
4397  * there will be an active portal running that command, which
4398  * we need to shut down before doing CleanupTransaction.
4399  */
4400  AtAbort_Portals();
4403  break;
4404 
4405  /*
4406  * In a subtransaction, so clean it up and abort parent too
4407  */
4408  case TBLOCK_SUBBEGIN:
4409  case TBLOCK_SUBINPROGRESS:
4410  case TBLOCK_SUBRELEASE:
4411  case TBLOCK_SUBCOMMIT:
4413  case TBLOCK_SUBRESTART:
4416  s = CurrentTransactionState; /* changed by pop */
4417  break;
4418 
4419  case TBLOCK_SUBABORT:
4420  case TBLOCK_SUBABORT_END:
4422  /* As above, but AbortSubTransaction already done */
4423  if (s->curTransactionOwner)
4424  {
4425  /* As in TBLOCK_ABORT, might have a live portal to zap */
4430  }
4432  s = CurrentTransactionState; /* changed by pop */
4433  break;
4434  }
4435  } while (s->blockState != TBLOCK_DEFAULT);
4436 
4437  /* Should be out of all subxacts now */
4438  Assert(s->parent == NULL);
4439 
4440  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4441  AtCleanup_Memory();
4442 }
4443 
4444 /*
4445  * IsTransactionBlock --- are we within a transaction block?
4446  */
4447 bool
4449 {
4451 
4453  return false;
4454 
4455  return true;
4456 }
4457 
4458 /*
4459  * IsTransactionOrTransactionBlock --- are we within either a transaction
4460  * or a transaction block? (The backend is only really "idle" when this
4461  * returns false.)
4462  *
4463  * This should match up with IsTransactionBlock and IsTransactionState.
4464  */
4465 bool
4467 {
4469 
4470  if (s->blockState == TBLOCK_DEFAULT)
4471  return false;
4472 
4473  return true;
4474 }
4475 
4476 /*
4477  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
4478  */
4479 char
4481 {
4483 
4484  switch (s->blockState)
4485  {
4486  case TBLOCK_DEFAULT:
4487  case TBLOCK_STARTED:
4488  return 'I'; /* idle --- not in transaction */
4489  case TBLOCK_BEGIN:
4490  case TBLOCK_SUBBEGIN:
4491  case TBLOCK_INPROGRESS:
4494  case TBLOCK_SUBINPROGRESS:
4495  case TBLOCK_END:
4496  case TBLOCK_SUBRELEASE:
4497  case TBLOCK_SUBCOMMIT:
4498  case TBLOCK_PREPARE:
4499  return 'T'; /* in transaction */
4500  case TBLOCK_ABORT:
4501  case TBLOCK_SUBABORT:
4502  case TBLOCK_ABORT_END:
4503  case TBLOCK_SUBABORT_END:
4504  case TBLOCK_ABORT_PENDING:
4506  case TBLOCK_SUBRESTART:
4508  return 'E'; /* in failed transaction */
4509  }
4510 
4511  /* should never get here */
4512  elog(FATAL, "invalid transaction block state: %s",
4514  return 0; /* keep compiler quiet */
4515 }
4516 
4517 /*
4518  * IsSubTransaction
4519  */
4520 bool
4522 {
4524 
4525  if (s->nestingLevel >= 2)
4526  return true;
4527 
4528  return false;
4529 }
4530 
4531 /*
4532  * StartSubTransaction
4533  *
4534  * If you're wondering why this is separate from PushTransaction: it's because
4535  * we can't conveniently do this stuff right inside DefineSavepoint. The
4536  * SAVEPOINT utility command will be executed inside a Portal, and if we
4537  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4538  * the Portal will undo those settings. So we make DefineSavepoint just
4539  * push a dummy transaction block, and when control returns to the main
4540  * idle loop, CommitTransactionCommand will be called, and we'll come here
4541  * to finish starting the subtransaction.
4542  */
4543 static void
4545 {
4547 
4548  if (s->state != TRANS_DEFAULT)
4549  elog(WARNING, "StartSubTransaction while in %s state",
4551 
4552  s->state = TRANS_START;
4553 
4554  /*
4555  * Initialize subsystems for new subtransaction
4556  *
4557  * must initialize resource-management stuff first
4558  */
4563 
4564  s->state = TRANS_INPROGRESS;
4565 
4566  /*
4567  * Call start-of-subxact callbacks
4568  */
4570  s->parent->subTransactionId);
4571 
4572  ShowTransactionState("StartSubTransaction");
4573 }
4574 
4575 /*
4576  * CommitSubTransaction
4577  *
4578  * The caller has to make sure to always reassign CurrentTransactionState
4579  * if it has a local pointer to it after calling this function.
4580  */
4581 static void
4583 {
4585 
4586  ShowTransactionState("CommitSubTransaction");
4587 
4588  if (s->state != TRANS_INPROGRESS)
4589  elog(WARNING, "CommitSubTransaction while in %s state",
4591 
4592  /* Pre-commit processing goes here */
4593 
4595  s->parent->subTransactionId);
4596 
4597  /* If in parallel mode, clean up workers and exit parallel mode. */
4598  if (IsInParallelMode())
4599  {
4601  s->parallelModeLevel = 0;
4602  }
4603 
4604  /* Do the actual "commit", such as it is */
4605  s->state = TRANS_COMMIT;
4606 
4607  /* Must CCI to ensure commands of subtransaction are seen as done */
4609 
4610  /*
4611  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4612  * perform that step, if required, as part of the atomic update of the
4613  * whole transaction tree at top level commit or abort.
4614  */
4615 
4616  /* Post-commit cleanup */
4619  AfterTriggerEndSubXact(true);
4624  s->parent->subTransactionId);
4626 
4628  s->parent->subTransactionId);
4629 
4632  true, false);
4634  s->parent->subTransactionId);
4635  AtEOSubXact_Inval(true);
4636  AtSubCommit_smgr();
4637 
4638  /*
4639  * The only lock we actually release here is the subtransaction XID lock.
4640  */
4644 
4645  /*
4646  * Other locks should get transferred to their parent resource owner.
4647  */
4650  true, false);
4653  true, false);
4654 
4655  AtEOXact_GUC(true, s->gucNestLevel);
4658  s->parent->subTransactionId);
4660  s->parent->subTransactionId);
4662  s->parent->subTransactionId);
4664  AtEOSubXact_PgStat(true, s->nestingLevel);
4666 
4667  /*
4668  * We need to restore the upper transaction's read-only state, in case the
4669  * upper is read-write while the child is read-only; GUC will incorrectly
4670  * think it should leave the child state in place.
4671  */
4673 
4677  s->curTransactionOwner = NULL;
4678 
4680 
4681  s->state = TRANS_DEFAULT;
4682 
4683  PopTransaction();
4684 }
4685 
4686 /*
4687  * AbortSubTransaction
4688  */
4689 static void
4691 {
4693 
4694  /* Prevent cancel/die interrupt while cleaning up */
4695  HOLD_INTERRUPTS();
4696 
4697  /* Make sure we have a valid memory context and resource owner */
4700 
4701  /*
4702  * Release any LW locks we might be holding as quickly as possible.
4703  * (Regular locks, however, must be held till we finish aborting.)
4704  * Releasing LW locks is critical since we might try to grab them again
4705  * while cleaning up!
4706  *
4707  * FIXME This may be incorrect --- Are there some locks we should keep?
4708  * Buffer locks, for example? I don't think so but I'm not sure.
4709  */
4710  LWLockReleaseAll();
4711 
4714  AbortBufferIO();
4715  UnlockBuffers();
4716 
4717  /* Reset WAL record construction state */
4719 
4720  /* Cancel condition variable sleep */
4722 
4723  /*
4724  * Also clean up any open wait for lock, since the lock manager will choke
4725  * if we try to wait for another lock before doing this.
4726  */
4727  LockErrorCleanup();
4728 
4729  /*
4730  * If any timeout events are still active, make sure the timeout interrupt
4731  * is scheduled. This covers possible loss of a timeout interrupt due to
4732  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4733  * We delay this till after LockErrorCleanup so that we don't uselessly
4734  * reschedule lock or deadlock check timeouts.
4735  */
4737 
4738  /*
4739  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4740  * handler. We do this fairly early in the sequence so that the timeout
4741  * infrastructure will be functional if needed while aborting.
4742  */
4744 
4745  /*
4746  * check the current transaction state
4747  */
4748  ShowTransactionState("AbortSubTransaction");
4749 
4750  if (s->state != TRANS_INPROGRESS)
4751  elog(WARNING, "AbortSubTransaction while in %s state",
4753 
4754  s->state = TRANS_ABORT;
4755 
4756  /*
4757  * Reset user ID which might have been changed transiently. (See notes in
4758  * AbortTransaction.)
4759  */
4761 
4762  /* Exit from parallel mode, if necessary. */
4763  if (IsInParallelMode())
4764  {
4766  s->parallelModeLevel = 0;
4767  }
4768 
4769  /*
4770  * We can skip all this stuff if the subxact failed before creating a
4771  * ResourceOwner...
4772  */
4773  if (s->curTransactionOwner)
4774  {
4775  AfterTriggerEndSubXact(false);
4781  s->parent->subTransactionId);
4783 
4784  /* Advertise the fact that we aborted in pg_xact. */
4785  (void) RecordTransactionAbort(true);
4786 
4787  /* Post-abort cleanup */
4790 
4792  s->parent->subTransactionId);
4793 
4796  false, false);
4798  s->parent->subTransactionId);
4799  AtEOSubXact_Inval(false);
4802  false, false);
4805  false, false);
4806  AtSubAbort_smgr();
4807 
4808  AtEOXact_GUC(false, s->gucNestLevel);
4809  AtEOSubXact_SPI(false, s->subTransactionId);
4811  s->parent->subTransactionId);
4813  s->parent->subTransactionId);
4815  s->parent->subTransactionId);
4817  AtEOSubXact_PgStat(false, s->nestingLevel);
4819  }
4820 
4821  /*
4822  * Restore the upper transaction's read-only state, too. This should be
4823  * redundant with GUC's cleanup but we may as well do it for consistency
4824  * with the commit case.
4825  */
4827 
4829 }
4830 
4831 /*
4832  * CleanupSubTransaction
4833  *
4834  * The caller has to make sure to always reassign CurrentTransactionState
4835  * if it has a local pointer to it after calling this function.
4836  */
4837 static void
4839 {
4841 
4842  ShowTransactionState("CleanupSubTransaction");
4843 
4844  if (s->state != TRANS_ABORT)
4845  elog(WARNING, "CleanupSubTransaction while in %s state",
4847 
4849 
4852  if (s->curTransactionOwner)
4854  s->curTransactionOwner = NULL;
4855 
4857 
4858  s->state = TRANS_DEFAULT;
4859 
4860  PopTransaction();
4861 }
4862 
4863 /*
4864  * PushTransaction
4865  * Create transaction state stack entry for a subtransaction
4866  *
4867  * The caller has to make sure to always reassign CurrentTransactionState
4868  * if it has a local pointer to it after calling this function.
4869  */
4870 static void
4872 {
4874  TransactionState s;
4875 
4876  /*
4877  * We keep subtransaction state nodes in TopTransactionContext.
4878  */
4879  s = (TransactionState)
4881  sizeof(TransactionStateData));
4882 
4883  /*
4884  * Assign a subtransaction ID, watching out for counter wraparound.
4885  */
4888  {
4890  pfree(s);
4891  ereport(ERROR,
4892  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4893  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
4894  }
4895 
4896  /*
4897  * We can now stack a minimally valid subtransaction without fear of
4898  * failure.
4899  */
4900  s->transactionId = InvalidTransactionId; /* until assigned */
4902  s->parent = p;
4903  s->nestingLevel = p->nestingLevel + 1;
4906  s->state = TRANS_DEFAULT;
4910  s->parallelModeLevel = 0;
4911 
4912  CurrentTransactionState = s;
4913 
4914  /*
4915  * AbortSubTransaction and CleanupSubTransaction have to be able to cope
4916  * with the subtransaction from here on out; in particular they should not
4917  * assume that it necessarily has a transaction context, resource owner,
4918  * or XID.
4919  */
4920 }
4921 
4922 /*
4923  * PopTransaction
4924  * Pop back to parent transaction state
4925  *
4926  * The caller has to make sure to always reassign CurrentTransactionState
4927  * if it has a local pointer to it after calling this function.
4928  */
4929 static void
4931 {
4933 
4934  if (s->state != TRANS_DEFAULT)
4935  elog(WARNING, "PopTransaction while in %s state",
4937 
4938  if (s->parent == NULL)
4939  elog(FATAL, "PopTransaction with no parent");
4940 
4941  CurrentTransactionState = s->parent;
4942 
4943  /* Let's just make sure CurTransactionContext is good */
4946 
4947  /* Ditto for ResourceOwner links */
4950 
4951  /* Free the old child structure */
4952  if (s->name)
4953  pfree(s->name);
4954  pfree(s);
4955 }
4956 
4957 /*
4958  * EstimateTransactionStateSpace
4959  * Estimate the amount of space that will be needed by
4960  * SerializeTransactionState. It would be OK to overestimate slightly,
4961  * but it's simple for us to work out the precise value, so we do.
4962  */
4963 Size
4965 {
4966  TransactionState s;
4967  Size nxids = 6; /* iso level, deferrable, top & current XID,
4968  * command counter, XID count */
4969 
4970  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4971  {
4973  nxids = add_size(nxids, 1);
4974  nxids = add_size(nxids, s->nChildXids);
4975  }
4976 
4977  nxids = add_size(nxids, nParallelCurrentXids);
4978  return mul_size(nxids, sizeof(TransactionId));
4979 }
4980 
4981 /*
4982  * SerializeTransactionState
4983  * Write out relevant details of our transaction state that will be
4984  * needed by a parallel worker.
4985  *
4986  * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
4987  * associated with this transaction. The first eight bytes of the result
4988  * contain XactDeferrable and XactIsoLevel; the next twelve bytes contain the
4989  * XID of the top-level transaction, the XID of the current transaction
4990  * (or, in each case, InvalidTransactionId if none), and the current command
4991  * counter. After that, the next 4 bytes contain a count of how many
4992  * additional XIDs follow; this is followed by all of those XIDs one after
4993  * another. We emit the XIDs in sorted order for the convenience of the
4994  * receiving process.
4995  */
4996 void
4997 SerializeTransactionState(Size maxsize, char *start_address)
4998 {
4999  TransactionState s;
5000  Size nxids = 0;
5001  Size i = 0;
5002  Size c = 0;
5003  TransactionId *workspace;
5004  TransactionId *result = (TransactionId *) start_address;
5005 
5006  result[c++] = (TransactionId) XactIsoLevel;
5007  result[c++] = (TransactionId) XactDeferrable;
5008  result[c++] = XactTopTransactionId;
5009  result[c++] = CurrentTransactionState->transactionId;
5010  result[c++] = (TransactionId) currentCommandId;
5011  Assert(maxsize >= c * sizeof(TransactionId));
5012 
5013  /*
5014  * If we're running in a parallel worker and launching a parallel worker
5015  * of our own, we can just pass along the information that was passed to
5016  * us.
5017  */
5018  if (nParallelCurrentXids > 0)
5019  {
5020  result[c++] = nParallelCurrentXids;
5021  Assert(maxsize >= (nParallelCurrentXids + c) * sizeof(TransactionId));
5022  memcpy(&result[c], ParallelCurrentXids,
5024  return;
5025  }
5026 
5027  /*
5028  * OK, we need to generate a sorted list of XIDs that our workers should
5029  * view as current. First, figure out how many there are.
5030  */
5031  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5032  {
5034  nxids = add_size(nxids, 1);
5035  nxids = add_size(nxids, s->nChildXids);
5036  }
5037  Assert((c + 1 + nxids) * sizeof(TransactionId) <= maxsize);
5038 
5039  /* Copy them to our scratch space. */
5040  workspace = palloc(nxids * sizeof(TransactionId));
5041  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5042  {
5044  workspace[i++] = s->transactionId;
5045  memcpy(&workspace[i], s->childXids,
5046  s->nChildXids * sizeof(TransactionId));
5047  i += s->nChildXids;
5048  }
5049  Assert(i == nxids);
5050 
5051  /* Sort them. */
5052  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5053 
5054  /* Copy data into output area. */
5055  result[c++] = (TransactionId) nxids;
5056  memcpy(&result[c], workspace, nxids * sizeof(TransactionId));
5057 }
5058 
5059 /*
5060  * StartParallelWorkerTransaction
5061  * Start a parallel worker transaction, restoring the relevant
5062  * transaction state serialized by SerializeTransactionState.
5063  */
5064 void
5066 {
5067  TransactionId *tstate = (TransactionId *) tstatespace;
5068 
5069  Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
5070  StartTransaction();
5071 
5072  XactIsoLevel = (int) tstate[0];
5073  XactDeferrable = (bool) tstate[1];
5074  XactTopTransactionId = tstate[2];
5075  CurrentTransactionState->transactionId = tstate[3];
5076  currentCommandId = tstate[4];
5077  nParallelCurrentXids = (int) tstate[5];
5078  ParallelCurrentXids = &tstate[6];
5079 
5080  CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
5081 }
5082 
5083 /*
5084  * EndParallelWorkerTransaction
5085  * End a parallel worker transaction.
5086  */
5087 void
5089 {
5090  Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
5092  CurrentTransactionState->blockState = TBLOCK_DEFAULT;
5093 }
5094 
5095 /*
5096  * ShowTransactionState
5097  * Debug support
5098  */
5099 static void
5101 {
5102  /* skip work if message will definitely not be printed */
5104  ShowTransactionStateRec(str, CurrentTransactionState);
5105 }
5106 
5107 /*
5108  * ShowTransactionStateRec
5109  * Recursive subroutine for ShowTransactionState
5110  */
5111 static void
5113 {
5115 
5116  initStringInfo(&buf);
5117 
5118  if (s->nChildXids > 0)
5119  {
5120  int i;
5121 
5122  appendStringInfo(&buf, ", children: %u", s->childXids[0]);
5123  for (i = 1; i < s->nChildXids; i++)
5124  appendStringInfo(&buf, " %u", s->childXids[i]);
5125  }
5126 
5127  if (s->parent)
5129 
5130  /* use ereport to suppress computation if msg will not be printed */
5131  ereport(DEBUG5,
5132  (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
5133  str, s->nestingLevel,
5134  PointerIsValid(s->name) ? s->name : "unnamed",
5137  (unsigned int) s->transactionId,
5138  (unsigned int) s->subTransactionId,
5139  (unsigned int) currentCommandId,
5140  currentCommandIdUsed ? " (used)" : "",
5141  buf.data)));
5142 
5143  pfree(buf.data);
5144 }
5145 
5146 /*
5147  * BlockStateAsString
5148  * Debug support
5149  */
5150 static const char *
5152 {
5153  switch (blockState)
5154  {
5155  case TBLOCK_DEFAULT:
5156  return "DEFAULT";
5157  case TBLOCK_STARTED:
5158  return "STARTED";
5159  case TBLOCK_BEGIN:
5160  return "BEGIN";
5161  case TBLOCK_INPROGRESS:
5162  return "INPROGRESS";
5164  return "IMPLICIT_INPROGRESS";
5166  return "PARALLEL_INPROGRESS";
5167  case TBLOCK_END:
5168  return "END";
5169  case TBLOCK_ABORT:
5170  return "ABORT";
5171  case TBLOCK_ABORT_END:
5172  return "ABORT END";
5173  case TBLOCK_ABORT_PENDING:
5174  return "ABORT PEND";
5175  case TBLOCK_PREPARE:
5176  return "PREPARE";
5177  case TBLOCK_SUBBEGIN:
5178  return "SUB BEGIN";
5179  case TBLOCK_SUBINPROGRESS:
5180  return "SUB INPROGRS";
5181  case TBLOCK_SUBRELEASE:
5182  return "SUB RELEASE";
5183  case TBLOCK_SUBCOMMIT:
5184  return "SUB COMMIT";
5185  case TBLOCK_SUBABORT:
5186  return "SUB ABORT";
5187  case TBLOCK_SUBABORT_END:
5188  return "SUB ABORT END";
5190  return "SUB ABRT PEND";
5191  case TBLOCK_SUBRESTART:
5192  return "SUB RESTART";
5194  return "SUB AB RESTRT";
5195  }
5196  return "UNRECOGNIZED";
5197 }
5198 
5199 /*
5200  * TransStateAsString
5201  * Debug support
5202  */
5203 static const char *
5205 {
5206  switch (state)
5207  {
5208  case TRANS_DEFAULT:
5209  return "DEFAULT";
5210  case TRANS_START:
5211  return "START";
5212  case TRANS_INPROGRESS:
5213  return "INPROGR";
5214  case TRANS_COMMIT:
5215  return "COMMIT";
5216  case TRANS_ABORT:
5217  return "ABORT";
5218  case TRANS_PREPARE:
5219  return "PREPARE";
5220  }
5221  return "UNRECOGNIZED";
5222 }
5223 
5224 /*
5225  * xactGetCommittedChildren
5226  *
5227  * Gets the list of committed children of the current transaction. The return
5228  * value is the number of child transactions. *ptr is set to point to an
5229  * array of TransactionIds. The array is allocated in TopTransactionContext;
5230  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
5231  * If there are no subxacts, *ptr is set to NULL.
5232  */
5233 int
5235 {
5237 
5238  if (s->nChildXids == 0)
5239  *ptr = NULL;
5240  else
5241  *ptr = s->childXids;
5242 
5243  return s->nChildXids;
5244 }
5245 
5246 /*
5247  * XLOG support routines
5248  */
5249 
5250 
5251 /*
5252  * Log the commit record for a plain or twophase transaction commit.
5253  *
5254  * A 2pc commit will be emitted when twophase_xid is valid, a plain one
5255  * otherwise.
5256  */
5257 XLogRecPtr
5259  int nsubxacts, TransactionId *subxacts,
5260  int nrels, RelFileNode *rels,
5261  int nmsgs, SharedInvalidationMessage *msgs,
5262  bool relcacheInval, bool forceSync,
5263  int xactflags, TransactionId twophase_xid)
5264 {
5265  xl_xact_commit xlrec;
5266  xl_xact_xinfo xl_xinfo;
5267  xl_xact_dbinfo xl_dbinfo;
5268  xl_xact_subxacts xl_subxacts;
5269  xl_xact_relfilenodes xl_relfilenodes;
5270  xl_xact_invals xl_invals;
5271  xl_xact_twophase xl_twophase;
5272  xl_xact_origin xl_origin;
5273 
5274  uint8 info;
5275 
5276  Assert(CritSectionCount > 0);
5277 
5278  xl_xinfo.xinfo = 0;
5279 
5280  /* decide between a plain and 2pc commit */
5281  if (!TransactionIdIsValid(twophase_xid))
5282  info = XLOG_XACT_COMMIT;
5283  else
5285 
5286  /* First figure out and collect all the information needed */
5287 
5288  xlrec.xact_time = commit_time;
5289 
5290  if (relcacheInval)
5292  if (forceSyncCommit)
5294  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5295  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5296 
5297  /*
5298  * Check if the caller would like to ask standbys for immediate feedback
5299  * once this commit is applied.
5300  */
5303 
5304  /*
5305  * Relcache invalidations requires information about the current database
5306  * and so does logical decoding.
5307  */
5308  if (nmsgs > 0 || XLogLogicalInfoActive())
5309  {
5310  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5311  xl_dbinfo.dbId = MyDatabaseId;
5312  xl_dbinfo.tsId = MyDatabaseTableSpace;
5313  }
5314 
5315  if (nsubxacts > 0)
5316  {
5317  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5318  xl_subxacts.nsubxacts = nsubxacts;
5319  }
5320 
5321  if (nrels > 0)
5322  {
5323  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5324  xl_relfilenodes.nrels = nrels;
5325  }
5326 
5327  if (nmsgs > 0)
5328  {
5329  xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5330  xl_invals.nmsgs = nmsgs;
5331  }
5332 
5333  if (TransactionIdIsValid(twophase_xid))
5334  {
5335  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5336  xl_twophase.xid = twophase_xid;
5337  }
5338 
5339  /* dump transaction origin information */
5341  {
5342  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5343 
5346  }
5347 
5348  if (xl_xinfo.xinfo != 0)
5349  info |= XLOG_XACT_HAS_INFO;
5350 
5351  /* Then include all the collected data into the commit record. */
5352 
5353  XLogBeginInsert();
5354 
5355  XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
5356 
5357  if (xl_xinfo.xinfo != 0)
5358  XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
5359 
5360  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5361  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5362 
5363  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5364  {
5365  XLogRegisterData((char *) (&xl_subxacts),
5367  XLogRegisterData((char *) subxacts,
5368  nsubxacts * sizeof(TransactionId));
5369  }
5370 
5371  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5372  {
5373  XLogRegisterData((char *) (&xl_relfilenodes),
5375  XLogRegisterData((char *) rels,
5376  nrels * sizeof(RelFileNode));
5377  }
5378 
5379  if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5380  {
5381  XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
5382  XLogRegisterData((char *) msgs,
5383  nmsgs * sizeof(SharedInvalidationMessage));
5384  }
5385 
5386  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5387  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5388 
5389  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5390  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5391 
5392  /* we allow filtering by xacts */
5394 
5395  return XLogInsert(RM_XACT_ID, info);
5396 }
5397 
5398 /*
5399  * Log the commit record for a plain or twophase transaction abort.
5400  *
5401  * A 2pc abort will be emitted when twophase_xid is valid, a plain one
5402  * otherwise.
5403  */
5404 XLogRecPtr
5406  int nsubxacts, TransactionId *subxacts,
5407  int nrels, RelFileNode *rels,
5408  int xactflags, TransactionId twophase_xid)
5409 {
5410  xl_xact_abort xlrec;
5411  xl_xact_xinfo xl_xinfo;
5412  xl_xact_subxacts xl_subxacts;
5413  xl_xact_relfilenodes xl_relfilenodes;
5414  xl_xact_twophase xl_twophase;
5415 
5416  uint8 info;
5417 
5418  Assert(CritSectionCount > 0);
5419 
5420  xl_xinfo.xinfo = 0;
5421 
5422  /* decide between a plain and 2pc abort */
5423  if (!TransactionIdIsValid(twophase_xid))
5424  info = XLOG_XACT_ABORT;
5425  else
5426  info = XLOG_XACT_ABORT_PREPARED;
5427 
5428 
5429  /* First figure out and collect all the information needed */
5430 
5431  xlrec.xact_time = abort_time;
5432 
5433  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5434  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5435 
5436  if (nsubxacts > 0)
5437  {
5438  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5439  xl_subxacts.nsubxacts = nsubxacts;
5440  }
5441 
5442  if (nrels > 0)
5443  {
5444  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5445  xl_relfilenodes.nrels = nrels;
5446  }
5447 
5448  if (TransactionIdIsValid(twophase_xid))
5449  {
5450  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5451  xl_twophase.xid = twophase_xid;
5452  }
5453 
5454  if (xl_xinfo.xinfo != 0)
5455  info |= XLOG_XACT_HAS_INFO;
5456 
5457  /* Then include all the collected data into the abort record. */
5458 
5459  XLogBeginInsert();
5460 
5461  XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
5462 
5463  if (xl_xinfo.xinfo != 0)
5464  XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
5465 
5466  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5467  {
5468  XLogRegisterData((char *) (&xl_subxacts),
5470  XLogRegisterData((char *) subxacts,
5471  nsubxacts * sizeof(TransactionId));
5472  }
5473 
5474  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5475  {
5476  XLogRegisterData((char *) (&xl_relfilenodes),
5478  XLogRegisterData((char *) rels,
5479  nrels * sizeof(RelFileNode));
5480  }
5481 
5482  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5483  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5484 
5485  return XLogInsert(RM_XACT_ID, info);
5486 }
5487 
5488 /*
5489  * Before 9.0 this was a fairly short function, but now it performs many
5490  * actions for which the order of execution is critical.
5491  */
5492 static void
5494  TransactionId xid,
5495  XLogRecPtr lsn,
5496  RepOriginId origin_id)
5497 {
5498  TransactionId max_xid;
5499  int i;
5500  TimestampTz commit_time;
5501 
5503 
5504  max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
5505 
5506  /*
5507  * Make sure nextXid is beyond any XID mentioned in the record.
5508  *
5509  * We don't expect anyone else to modify nextXid, hence we don't need to
5510  * hold a lock while checking this. We still acquire the lock to modify
5511  * it, though.
5512  */
5513  if (TransactionIdFollowsOrEquals(max_xid,
5515  {
5516  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5517  ShmemVariableCache->nextXid = max_xid;
5519  LWLockRelease(XidGenLock);
5520  }
5521 
5522  Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
5523  (origin_id == InvalidRepOriginId));
5524 
5525  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5526  commit_time = parsed->origin_timestamp;
5527  else
5528  commit_time = parsed->xact_time;
5529 
5530  /* Set the transaction commit timestamp and metadata */
5531  TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
5532  commit_time, origin_id, false);
5533 
5535  {
5536  /*
5537  * Mark the transaction committed in pg_xact.
5538  */
5539  TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
5540  }
5541  else
5542  {
5543  /*
5544  * If a transaction completion record arrives that has as-yet
5545  * unobserved subtransactions then this will not have been fully
5546  * handled by the call to RecordKnownAssignedTransactionIds() in the
5547  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5548  * cover that case. This is confusing and it is easy to think this
5549  * call is irrelevant, which has happened three times in development
5550  * already. Leave it in.
5551  */
5553 
5554  /*
5555  * Mark the transaction committed in pg_xact. We use async commit
5556  * protocol during recovery to provide information on database
5557  * consistency for when users try to set hint bits. It is important
5558  * that we do not set hint bits until the minRecoveryPoint is past
5559  * this commit record. This ensures that if we crash we don't see hint
5560  * bits set on changes made by transactions that haven't yet
5561  * recovered. It's unlikely but it's good to be safe.
5562  */
5564  xid, parsed->nsubxacts, parsed->subxacts, lsn);
5565 
5566  /*
5567  * We must mark clog before we update the ProcArray.
5568  */
5570  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5571 
5572  /*
5573  * Send any cache invalidations attached to the commit. We must
5574  * maintain the same order of invalidation then release locks as
5575  * occurs in CommitTransaction().
5576  */
5578  parsed->msgs, parsed->nmsgs,
5580  parsed->dbId, parsed->tsId);
5581 
5582  /*
5583  * Release locks, if any. We do this for both two phase and normal one
5584  * phase transactions. In effect we are ignoring the prepare phase and
5585  * just going straight to lock release. At commit we release all locks
5586  * via their top-level xid only, so no need to provide subxact list,
5587  * which will save time when replaying commits.
5588  */
5589  if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
5590  StandbyReleaseLockTree(xid, 0, NULL);
5591  }
5592 
5593  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5594  {
5595  /* recover apply progress */
5596  replorigin_advance(origin_id, parsed->origin_lsn, lsn,
5597  false /* backward */ , false /* WAL */ );
5598  }
5599 
5600  /* Make sure files supposed to be dropped are dropped */
5601  if (parsed->nrels > 0)
5602  {
5603  /*
5604  * First update minimum recovery point to cover this WAL record. Once
5605  * a relation is deleted, there's no going back. The buffer manager
5606  * enforces the WAL-first rule for normal updates to relation files,
5607  * so that the minimum recovery point is always updated before the
5608  * corresponding change in the data file is flushed to disk, but we
5609  * have to do the same here since we're bypassing the buffer manager.
5610  *
5611  * Doing this before deleting the files means that if a deletion fails
5612  * for some reason, you cannot start up the system even after restart,
5613  * until you fix the underlying situation so that the deletion will
5614  * succeed. Alternatively, we could update the minimum recovery point
5615  * after deletion, but that would leave a small window where the
5616  * WAL-first rule would be violated.
5617  */
5618  XLogFlush(lsn);
5619 
5620  for (i = 0; i < parsed->nrels; i++)
5621  {
5622  SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId);
5623  ForkNumber fork;
5624 
5625  for (fork = 0; fork <= MAX_FORKNUM; fork++)
5626  XLogDropRelation(parsed->xnodes[i], fork);
5627  smgrdounlink(srel, true);
5628  smgrclose(srel);
5629  }
5630  }
5631 
5632  /*
5633  * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
5634  * in normal operation. For example, in CREATE DATABASE, we copy all files
5635  * from the template database, and then commit the transaction. If we
5636  * crash after all the files have been copied but before the commit, you
5637  * have files in the data directory without an entry in pg_database. To
5638  * minimize the window for that, we use ForceSyncCommit() to rush the
5639  * commit record to disk as quick as possible. We have the same window
5640  * during recovery, and forcing an XLogFlush() (which updates
5641  * minRecoveryPoint during recovery) helps to reduce that problem window,
5642  * for any user that requested ForceSyncCommit().
5643  */
5644  if (XactCompletionForceSyncCommit(parsed->xinfo))
5645  XLogFlush(lsn);
5646 
5647  /*
5648  * If asked by the primary (because someone is waiting for a synchronous
5649  * commit = remote_apply), we will need to ask walreceiver to send a reply
5650  * immediately.
5651  */
5652  if (XactCompletionApplyFeedback(parsed->xinfo))
5654 }
5655 
5656 /*
5657  * Be careful with the order of execution, as with xact_redo_commit().
5658  * The two functions are similar but differ in key places.
5659  *
5660  * Note also that an abort can be for a subtransaction and its children,
5661  * not just for a top level abort. That means we have to consider
5662  * topxid != xid, whereas in commit we would find topxid == xid always
5663  * because subtransaction commit is never WAL logged.
5664  */
5665 static void
5667 {
5668  int i;
5669  TransactionId max_xid;
5670 
5672 
5673  /*
5674  * Make sure nextXid is beyond any XID mentioned in the record.
5675  *
5676  * We don't expect anyone else to modify nextXid, hence we don't need to
5677  * hold a lock while checking this. We still acquire the lock to modify
5678  * it, though.
5679  */
5680  max_xid = TransactionIdLatest(xid,
5681  parsed->nsubxacts,
5682  parsed->subxacts);
5683 
5684  if (TransactionIdFollowsOrEquals(max_xid,
5686  {
5687  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
5688  ShmemVariableCache->nextXid = max_xid;
5690  LWLockRelease(XidGenLock);
5691  }
5692 
5694  {
5695  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5696  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5697  }
5698  else
5699  {
5700  /*
5701  * If a transaction completion record arrives that has as-yet
5702  * unobserved subtransactions then this will not have been fully
5703  * handled by the call to RecordKnownAssignedTransactionIds() in the
5704  * main recovery loop in xlog.c. So we need to do bookkeeping again to
5705  * cover that case. This is confusing and it is easy to think this
5706  * call is irrelevant, which has happened three times in development
5707  * already. Leave it in.
5708  */
5710 
5711  /* Mark the transaction aborted in pg_xact, no need for async stuff */
5712  TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
5713 
5714  /*
5715  * We must update the ProcArray after we have marked clog.
5716  */
5718  xid, parsed->nsubxacts, parsed->subxacts, max_xid);
5719 
5720  /*
5721  * There are no flat files that need updating, nor invalidation
5722  * messages to send or undo.
5723  */
5724 
5725  /*
5726  * Release locks, if any. There are no invalidations to send.
5727  */
5728  if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
5729  StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
5730  }
5731 
5732  /* Make sure files supposed to be dropped are dropped */
5733  for (i = 0; i < parsed->nrels; i++)
5734  {
5735  SMgrRelation srel = smgropen(parsed->xnodes[i], InvalidBackendId);
5736  ForkNumber fork;
5737 
5738  for (fork = 0; fork <= MAX_FORKNUM; fork++)
5739  XLogDropRelation(parsed->xnodes[i], fork);
5740  smgrdounlink(srel, true);
5741  smgrclose(srel);
5742  }
5743 }
5744 
5745 void
5747 {
5748  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
5749 
5750  /* Backup blocks are not used in xact records */
5751  Assert(!XLogRecHasAnyBlockRefs(record));
5752 
5753  if (info == XLOG_XACT_COMMIT)
5754  {
5755  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5756  xl_xact_parsed_commit parsed;
5757 
5758  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5759  xact_redo_commit(&parsed, XLogRecGetXid(record),
5760  record->EndRecPtr, XLogRecGetOrigin(record));
5761  }
5762  else if (info == XLOG_XACT_COMMIT_PREPARED)
5763  {
5764  xl_xact_commit *xlrec = (xl_xact_commit *)