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