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