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