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-2019, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  * src/backend/access/transam/xact.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #include "postgres.h"
19 
20 #include <time.h>
21 #include <unistd.h>
22 
23 #include "access/commit_ts.h"
24 #include "access/multixact.h"
25 #include "access/parallel.h"
26 #include "access/subtrans.h"
27 #include "access/transam.h"
28 #include "access/twophase.h"
29 #include "access/xact.h"
30 #include "access/xlog.h"
31 #include "access/xloginsert.h"
32 #include "access/xlogutils.h"
33 #include "catalog/namespace.h"
34 #include "catalog/pg_enum.h"
35 #include "catalog/storage.h"
36 #include "commands/async.h"
37 #include "commands/tablecmds.h"
38 #include "commands/trigger.h"
39 #include "executor/spi.h"
40 #include "libpq/be-fsstubs.h"
41 #include "libpq/pqsignal.h"
42 #include "miscadmin.h"
43 #include "pgstat.h"
44 #include "replication/logical.h"
46 #include "replication/origin.h"
47 #include "replication/syncrep.h"
48 #include "replication/walsender.h"
50 #include "storage/fd.h"
51 #include "storage/lmgr.h"
52 #include "storage/md.h"
53 #include "storage/predicate.h"
54 #include "storage/proc.h"
55 #include "storage/procarray.h"
56 #include "storage/sinvaladt.h"
57 #include "storage/smgr.h"
58 #include "utils/builtins.h"
59 #include "utils/catcache.h"
60 #include "utils/combocid.h"
61 #include "utils/guc.h"
62 #include "utils/inval.h"
63 #include "utils/memutils.h"
64 #include "utils/relmapper.h"
65 #include "utils/snapmgr.h"
66 #include "utils/timeout.h"
67 #include "utils/timestamp.h"
68 #include "pg_trace.h"
69 
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 
874  /*
875  * In parallel workers, the XIDs we must consider as current are stored in
876  * ParallelCurrentXids rather than the transaction-state stack. Note that
877  * the XIDs in this array are sorted numerically rather than according to
878  * transactionIdPrecedes order.
879  */
880  if (nParallelCurrentXids > 0)
881  {
882  int low,
883  high;
884 
885  low = 0;
886  high = nParallelCurrentXids - 1;
887  while (low <= high)
888  {
889  int middle;
890  TransactionId probe;
891 
892  middle = low + (high - low) / 2;
893  probe = ParallelCurrentXids[middle];
894  if (probe == xid)
895  return true;
896  else if (probe < xid)
897  low = middle + 1;
898  else
899  high = middle - 1;
900  }
901  return false;
902  }
903 
904  /*
905  * We will return true for the Xid of the current subtransaction, any of
906  * its subcommitted children, any of its parents, or any of their
907  * previously subcommitted children. However, a transaction being aborted
908  * is no longer "current", even though it may still have an entry on the
909  * state stack.
910  */
911  for (s = CurrentTransactionState; s != NULL; s = s->parent)
912  {
913  int low,
914  high;
915 
916  if (s->state == TRANS_ABORT)
917  continue;
919  continue; /* it can't have any child XIDs either */
921  return true;
922  /* As the childXids array is ordered, we can use binary search */
923  low = 0;
924  high = s->nChildXids - 1;
925  while (low <= high)
926  {
927  int middle;
928  TransactionId probe;
929 
930  middle = low + (high - low) / 2;
931  probe = s->childXids[middle];
932  if (TransactionIdEquals(probe, xid))
933  return true;
934  else if (TransactionIdPrecedes(probe, xid))
935  low = middle + 1;
936  else
937  high = middle - 1;
938  }
939  }
940 
941  return false;
942 }
943 
944 /*
945  * TransactionStartedDuringRecovery
946  *
947  * Returns true if the current transaction started while recovery was still
948  * in progress. Recovery might have ended since so RecoveryInProgress() might
949  * return false already.
950  */
951 bool
953 {
954  return CurrentTransactionState->startedInRecovery;
955 }
956 
957 /*
958  * EnterParallelMode
959  */
960 void
962 {
964 
965  Assert(s->parallelModeLevel >= 0);
966 
967  ++s->parallelModeLevel;
968 }
969 
970 /*
971  * ExitParallelMode
972  */
973 void
975 {
977 
978  Assert(s->parallelModeLevel > 0);
980 
981  --s->parallelModeLevel;
982 }
983 
984 /*
985  * IsInParallelMode
986  *
987  * Are we in a parallel operation, as either the master or a worker? Check
988  * this to prohibit operations that change backend-local state expected to
989  * match across all workers. Mere caches usually don't require such a
990  * restriction. State modified in a strict push/pop fashion, such as the
991  * active snapshot stack, is often fine.
992  */
993 bool
995 {
996  return CurrentTransactionState->parallelModeLevel != 0;
997 }
998 
999 /*
1000  * CommandCounterIncrement
1001  */
1002 void
1004 {
1005  /*
1006  * If the current value of the command counter hasn't been "used" to mark
1007  * tuples, we need not increment it, since there's no need to distinguish
1008  * a read-only command from others. This helps postpone command counter
1009  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1010  */
1012  {
1013  /*
1014  * Workers synchronize transaction state at the beginning of each
1015  * parallel operation, so we can't account for new commands after that
1016  * point.
1017  */
1019  elog(ERROR, "cannot start commands during a parallel operation");
1020 
1021  currentCommandId += 1;
1023  {
1024  currentCommandId -= 1;
1025  ereport(ERROR,
1026  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1027  errmsg("cannot have more than 2^32-2 commands in a transaction")));
1028  }
1029  currentCommandIdUsed = false;
1030 
1031  /* Propagate new command ID into static snapshots */
1033 
1034  /*
1035  * Make any catalog changes done by the just-completed command visible
1036  * in the local syscache. We obviously don't need to do this after a
1037  * read-only command. (But see hacks in inval.c to make real sure we
1038  * don't think a command that queued inval messages was read-only.)
1039  */
1040  AtCCI_LocalCache();
1041  }
1042 }
1043 
1044 /*
1045  * ForceSyncCommit
1046  *
1047  * Interface routine to allow commands to force a synchronous commit of the
1048  * current top-level transaction
1049  */
1050 void
1052 {
1053  forceSyncCommit = true;
1054 }
1055 
1056 
1057 /* ----------------------------------------------------------------
1058  * StartTransaction stuff
1059  * ----------------------------------------------------------------
1060  */
1061 
1062 /*
1063  * AtStart_Cache
1064  */
1065 static void
1067 {
1069 }
1070 
1071 /*
1072  * AtStart_Memory
1073  */
1074 static void
1076 {
1078 
1079  /*
1080  * If this is the first time through, create a private context for
1081  * AbortTransaction to work in. By reserving some space now, we can
1082  * insulate AbortTransaction from out-of-memory scenarios. Like
1083  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1084  * size, so that space will be reserved immediately.
1085  */
1086  if (TransactionAbortContext == NULL)
1087  TransactionAbortContext =
1089  "TransactionAbortContext",
1090  32 * 1024,
1091  32 * 1024,
1092  32 * 1024);
1093 
1094  /*
1095  * We shouldn't have a transaction context already.
1096  */
1097  Assert(TopTransactionContext == NULL);
1098 
1099  /*
1100  * Create a toplevel context for the transaction.
1101  */
1104  "TopTransactionContext",
1106 
1107  /*
1108  * In a top-level transaction, CurTransactionContext is the same as
1109  * TopTransactionContext.
1110  */
1113 
1114  /* Make the CurTransactionContext active. */
1116 }
1117 
1118 /*
1119  * AtStart_ResourceOwner
1120  */
1121 static void
1123 {
1125 
1126  /*
1127  * We shouldn't have a transaction resource owner already.
1128  */
1130 
1131  /*
1132  * Create a toplevel resource owner for the transaction.
1133  */
1134  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1135 
1139 }
1140 
1141 /* ----------------------------------------------------------------
1142  * StartSubTransaction stuff
1143  * ----------------------------------------------------------------
1144  */
1145 
1146 /*
1147  * AtSubStart_Memory
1148  */
1149 static void
1151 {
1153 
1154  Assert(CurTransactionContext != NULL);
1155 
1156  /*
1157  * Create a CurTransactionContext, which will be used to hold data that
1158  * survives subtransaction commit but disappears on subtransaction abort.
1159  * We make it a child of the immediate parent's CurTransactionContext.
1160  */
1162  "CurTransactionContext",
1165 
1166  /* Make the CurTransactionContext active. */
1168 }
1169 
1170 /*
1171  * AtSubStart_ResourceOwner
1172  */
1173 static void
1175 {
1177 
1178  Assert(s->parent != NULL);
1179 
1180  /*
1181  * Create a resource owner for the subtransaction. We make it a child of
1182  * the immediate parent's resource owner.
1183  */
1184  s->curTransactionOwner =
1186  "SubTransaction");
1187 
1190 }
1191 
1192 /* ----------------------------------------------------------------
1193  * CommitTransaction stuff
1194  * ----------------------------------------------------------------
1195  */
1196 
1197 /*
1198  * RecordTransactionCommit
1199  *
1200  * Returns latest XID among xact and its children, or InvalidTransactionId
1201  * if the xact has no XID. (We compute that here just because it's easier.)
1202  *
1203  * If you change this function, see RecordTransactionCommitPrepared also.
1204  */
1205 static TransactionId
1207 {
1209  bool markXidCommitted = TransactionIdIsValid(xid);
1210  TransactionId latestXid = InvalidTransactionId;
1211  int nrels;
1212  RelFileNode *rels;
1213  int nchildren;
1214  TransactionId *children;
1215  int nmsgs = 0;
1216  SharedInvalidationMessage *invalMessages = NULL;
1217  bool RelcacheInitFileInval = false;
1218  bool wrote_xlog;
1219 
1220  /* Get data needed for commit record */
1221  nrels = smgrGetPendingDeletes(true, &rels);
1222  nchildren = xactGetCommittedChildren(&children);
1223  if (XLogStandbyInfoActive())
1224  nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1225  &RelcacheInitFileInval);
1226  wrote_xlog = (XactLastRecEnd != 0);
1227 
1228  /*
1229  * If we haven't been assigned an XID yet, we neither can, nor do we want
1230  * to write a COMMIT record.
1231  */
1232  if (!markXidCommitted)
1233  {
1234  /*
1235  * We expect that every RelationDropStorage is followed by a catalog
1236  * update, and hence XID assignment, so we shouldn't get here with any
1237  * pending deletes. Use a real test not just an Assert to check this,
1238  * since it's a bit fragile.
1239  */
1240  if (nrels != 0)
1241  elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1242 
1243  /* Can't have child XIDs either; AssignTransactionId enforces this */
1244  Assert(nchildren == 0);
1245 
1246  /*
1247  * Transactions without an assigned xid can contain invalidation
1248  * messages (e.g. explicit relcache invalidations or catcache
1249  * invalidations for inplace updates); standbys need to process those.
1250  * We can't emit a commit record without an xid, and we don't want to
1251  * force assigning an xid, because that'd be problematic for e.g.
1252  * vacuum. Hence we emit a bespoke record for the invalidations. We
1253  * don't want to use that in case a commit record is emitted, so they
1254  * happen synchronously with commits (besides not wanting to emit more
1255  * WAL records).
1256  */
1257  if (nmsgs != 0)
1258  {
1259  LogStandbyInvalidations(nmsgs, invalMessages,
1260  RelcacheInitFileInval);
1261  wrote_xlog = true; /* not strictly necessary */
1262  }
1263 
1264  /*
1265  * If we didn't create XLOG entries, we're done here; otherwise we
1266  * should trigger flushing those entries the same as a commit record
1267  * would. This will primarily happen for HOT pruning and the like; we
1268  * want these to be flushed to disk in due time.
1269  */
1270  if (!wrote_xlog)
1271  goto cleanup;
1272  }
1273  else
1274  {
1275  bool replorigin;
1276 
1277  /*
1278  * Are we using the replication origins feature? Or, in other words,
1279  * are we replaying remote actions?
1280  */
1281  replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1283 
1284  /*
1285  * Begin commit critical section and insert the commit XLOG record.
1286  */
1287  /* Tell bufmgr and smgr to prepare for commit */
1288  BufmgrCommit();
1289 
1290  /*
1291  * Mark ourselves as within our "commit critical section". This
1292  * forces any concurrent checkpoint to wait until we've updated
1293  * pg_xact. Without this, it is possible for the checkpoint to set
1294  * REDO after the XLOG record but fail to flush the pg_xact update to
1295  * disk, leading to loss of the transaction commit if the system
1296  * crashes a little later.
1297  *
1298  * Note: we could, but don't bother to, set this flag in
1299  * RecordTransactionAbort. That's because loss of a transaction abort
1300  * is noncritical; the presumption would be that it aborted, anyway.
1301  *
1302  * It's safe to change the delayChkpt flag of our own backend without
1303  * holding the ProcArrayLock, since we're the only one modifying it.
1304  * This makes checkpoint's determination of which xacts are delayChkpt
1305  * a bit fuzzy, but it doesn't matter.
1306  */
1308  MyPgXact->delayChkpt = true;
1309 
1311 
1313  nchildren, children, nrels, rels,
1314  nmsgs, invalMessages,
1315  RelcacheInitFileInval, forceSyncCommit,
1316  MyXactFlags,
1317  InvalidTransactionId, NULL /* plain commit */ );
1318 
1319  if (replorigin)
1320  /* Move LSNs forward for this replication origin */
1322  XactLastRecEnd);
1323 
1324  /*
1325  * Record commit timestamp. The value comes from plain commit
1326  * timestamp if there's no replication origin; otherwise, the
1327  * timestamp was already set in replorigin_session_origin_timestamp by
1328  * replication.
1329  *
1330  * We don't need to WAL-log anything here, as the commit record
1331  * written above already contains the data.
1332  */
1333 
1334  if (!replorigin || replorigin_session_origin_timestamp == 0)
1336 
1337  TransactionTreeSetCommitTsData(xid, nchildren, children,
1339  replorigin_session_origin, false);
1340  }
1341 
1342  /*
1343  * Check if we want to commit asynchronously. We can allow the XLOG flush
1344  * to happen asynchronously if synchronous_commit=off, or if the current
1345  * transaction has not performed any WAL-logged operation or didn't assign
1346  * an xid. The transaction can end up not writing any WAL, even if it has
1347  * an xid, if it only wrote to temporary and/or unlogged tables. It can
1348  * end up having written WAL without an xid if it did HOT pruning. In
1349  * case of a crash, the loss of such a transaction will be irrelevant;
1350  * temp tables will be lost anyway, unlogged tables will be truncated and
1351  * HOT pruning will be done again later. (Given the foregoing, you might
1352  * think that it would be unnecessary to emit the XLOG record at all in
1353  * this case, but we don't currently try to do that. It would certainly
1354  * cause problems at least in Hot Standby mode, where the
1355  * KnownAssignedXids machinery requires tracking every XID assignment. It
1356  * might be OK to skip it only when wal_level < replica, but for now we
1357  * don't.)
1358  *
1359  * However, if we're doing cleanup of any non-temp rels or committing any
1360  * command that wanted to force sync commit, then we must flush XLOG
1361  * immediately. (We must not allow asynchronous commit if there are any
1362  * non-temp tables to be deleted, because we might delete the files before
1363  * the COMMIT record is flushed to disk. We do allow asynchronous commit
1364  * if all to-be-deleted tables are temporary though, since they are lost
1365  * anyway if we crash.)
1366  */
1367  if ((wrote_xlog && markXidCommitted &&
1369  forceSyncCommit || nrels > 0)
1370  {
1372 
1373  /*
1374  * Now we may update the CLOG, if we wrote a COMMIT record above
1375  */
1376  if (markXidCommitted)
1377  TransactionIdCommitTree(xid, nchildren, children);
1378  }
1379  else
1380  {
1381  /*
1382  * Asynchronous commit case:
1383  *
1384  * This enables possible committed transaction loss in the case of a
1385  * postmaster crash because WAL buffers are left unwritten. Ideally we
1386  * could issue the WAL write without the fsync, but some
1387  * wal_sync_methods do not allow separate write/fsync.
1388  *
1389  * Report the latest async commit LSN, so that the WAL writer knows to
1390  * flush this commit.
1391  */
1393 
1394  /*
1395  * We must not immediately update the CLOG, since we didn't flush the
1396  * XLOG. Instead, we store the LSN up to which the XLOG must be
1397  * flushed before the CLOG may be updated.
1398  */
1399  if (markXidCommitted)
1400  TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1401  }
1402 
1403  /*
1404  * If we entered a commit critical section, leave it now, and let
1405  * checkpoints proceed.
1406  */
1407  if (markXidCommitted)
1408  {
1409  MyPgXact->delayChkpt = false;
1410  END_CRIT_SECTION();
1411  }
1412 
1413  /* Compute latestXid while we have the child XIDs handy */
1414  latestXid = TransactionIdLatest(xid, nchildren, children);
1415 
1416  /*
1417  * Wait for synchronous replication, if required. Similar to the decision
1418  * above about using committing asynchronously we only want to wait if
1419  * this backend assigned an xid and wrote WAL. No need to wait if an xid
1420  * was assigned due to temporary/unlogged tables or due to HOT pruning.
1421  *
1422  * Note that at this stage we have marked clog, but still show as running
1423  * in the procarray and continue to hold locks.
1424  */
1425  if (wrote_xlog && markXidCommitted)
1427 
1428  /* remember end of last commit record */
1430 
1431  /* Reset XactLastRecEnd until the next transaction writes something */
1432  XactLastRecEnd = 0;
1433 cleanup:
1434  /* Clean up local data */
1435  if (rels)
1436  pfree(rels);
1437 
1438  return latestXid;
1439 }
1440 
1441 
1442 /*
1443  * AtCCI_LocalCache
1444  */
1445 static void
1447 {
1448  /*
1449  * Make any pending relation map changes visible. We must do this before
1450  * processing local sinval messages, so that the map changes will get
1451  * reflected into the relcache when relcache invals are processed.
1452  */
1454 
1455  /*
1456  * Make catalog changes visible to me for the next command.
1457  */
1459 }
1460 
1461 /*
1462  * AtCommit_Memory
1463  */
1464 static void
1466 {
1467  /*
1468  * Now that we're "out" of a transaction, have the system allocate things
1469  * in the top memory context instead of per-transaction contexts.
1470  */
1472 
1473  /*
1474  * Release all transaction-local memory.
1475  */
1476  Assert(TopTransactionContext != NULL);
1478  TopTransactionContext = NULL;
1479  CurTransactionContext = NULL;
1480  CurrentTransactionState->curTransactionContext = NULL;
1481 }
1482 
1483 /* ----------------------------------------------------------------
1484  * CommitSubTransaction stuff
1485  * ----------------------------------------------------------------
1486  */
1487 
1488 /*
1489  * AtSubCommit_Memory
1490  */
1491 static void
1493 {
1495 
1496  Assert(s->parent != NULL);
1497 
1498  /* Return to parent transaction level's memory context. */
1501 
1502  /*
1503  * Ordinarily we cannot throw away the child's CurTransactionContext,
1504  * since the data it contains will be needed at upper commit. However, if
1505  * there isn't actually anything in it, we can throw it away. This avoids
1506  * a small memory leak in the common case of "trivial" subxacts.
1507  */
1509  {
1511  s->curTransactionContext = NULL;
1512  }
1513 }
1514 
1515 /*
1516  * AtSubCommit_childXids
1517  *
1518  * Pass my own XID and my child XIDs up to my parent as committed children.
1519  */
1520 static void
1522 {
1524  int new_nChildXids;
1525 
1526  Assert(s->parent != NULL);
1527 
1528  /*
1529  * The parent childXids array will need to hold my XID and all my
1530  * childXids, in addition to the XIDs already there.
1531  */
1532  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1533 
1534  /* Allocate or enlarge the parent array if necessary */
1535  if (s->parent->maxChildXids < new_nChildXids)
1536  {
1537  int new_maxChildXids;
1538  TransactionId *new_childXids;
1539 
1540  /*
1541  * Make it 2x what's needed right now, to avoid having to enlarge it
1542  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1543  * is what ensures that we don't need to worry about integer overflow
1544  * here or in the calculation of new_nChildXids.)
1545  */
1546  new_maxChildXids = Min(new_nChildXids * 2,
1547  (int) (MaxAllocSize / sizeof(TransactionId)));
1548 
1549  if (new_maxChildXids < new_nChildXids)
1550  ereport(ERROR,
1551  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1552  errmsg("maximum number of committed subtransactions (%d) exceeded",
1553  (int) (MaxAllocSize / sizeof(TransactionId)))));
1554 
1555  /*
1556  * We keep the child-XID arrays in TopTransactionContext; this avoids
1557  * setting up child-transaction contexts for what might be just a few
1558  * bytes of grandchild XIDs.
1559  */
1560  if (s->parent->childXids == NULL)
1561  new_childXids =
1563  new_maxChildXids * sizeof(TransactionId));
1564  else
1565  new_childXids = repalloc(s->parent->childXids,
1566  new_maxChildXids * sizeof(TransactionId));
1567 
1568  s->parent->childXids = new_childXids;
1569  s->parent->maxChildXids = new_maxChildXids;
1570  }
1571 
1572  /*
1573  * Copy all my XIDs to parent's array.
1574  *
1575  * Note: We rely on the fact that the XID of a child always follows that
1576  * of its parent. By copying the XID of this subtransaction before the
1577  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1578  * all XIDs already in the array belong to subtransactions started and
1579  * subcommitted before us, so their XIDs must precede ours.
1580  */
1582 
1583  if (s->nChildXids > 0)
1584  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1585  s->childXids,
1586  s->nChildXids * sizeof(TransactionId));
1587 
1588  s->parent->nChildXids = new_nChildXids;
1589 
1590  /* Release child's array to avoid leakage */
1591  if (s->childXids != NULL)
1592  pfree(s->childXids);
1593  /* We must reset these to avoid double-free if fail later in commit */
1594  s->childXids = NULL;
1595  s->nChildXids = 0;
1596  s->maxChildXids = 0;
1597 }
1598 
1599 /* ----------------------------------------------------------------
1600  * AbortTransaction stuff
1601  * ----------------------------------------------------------------
1602  */
1603 
1604 /*
1605  * RecordTransactionAbort
1606  *
1607  * Returns latest XID among xact and its children, or InvalidTransactionId
1608  * if the xact has no XID. (We compute that here just because it's easier.)
1609  */
1610 static TransactionId
1611 RecordTransactionAbort(bool isSubXact)
1612 {
1614  TransactionId latestXid;
1615  int nrels;
1616  RelFileNode *rels;
1617  int nchildren;
1618  TransactionId *children;
1619  TimestampTz xact_time;
1620 
1621  /*
1622  * If we haven't been assigned an XID, nobody will care whether we aborted
1623  * or not. Hence, we're done in that case. It does not matter if we have
1624  * rels to delete (note that this routine is not responsible for actually
1625  * deleting 'em). We cannot have any child XIDs, either.
1626  */
1627  if (!TransactionIdIsValid(xid))
1628  {
1629  /* Reset XactLastRecEnd until the next transaction writes something */
1630  if (!isSubXact)
1631  XactLastRecEnd = 0;
1632  return InvalidTransactionId;
1633  }
1634 
1635  /*
1636  * We have a valid XID, so we should write an ABORT record for it.
1637  *
1638  * We do not flush XLOG to disk here, since the default assumption after a
1639  * crash would be that we aborted, anyway. For the same reason, we don't
1640  * need to worry about interlocking against checkpoint start.
1641  */
1642 
1643  /*
1644  * Check that we haven't aborted halfway through RecordTransactionCommit.
1645  */
1646  if (TransactionIdDidCommit(xid))
1647  elog(PANIC, "cannot abort transaction %u, it was already committed",
1648  xid);
1649 
1650  /* Fetch the data we need for the abort record */
1651  nrels = smgrGetPendingDeletes(false, &rels);
1652  nchildren = xactGetCommittedChildren(&children);
1653 
1654  /* XXX do we really need a critical section here? */
1656 
1657  /* Write the ABORT record */
1658  if (isSubXact)
1659  xact_time = GetCurrentTimestamp();
1660  else
1661  {
1663  xact_time = xactStopTimestamp;
1664  }
1665 
1666  XactLogAbortRecord(xact_time,
1667  nchildren, children,
1668  nrels, rels,
1670  NULL);
1671 
1672  /*
1673  * Report the latest async abort LSN, so that the WAL writer knows to
1674  * flush this abort. There's nothing to be gained by delaying this, since
1675  * WALWriter may as well do this when it can. This is important with
1676  * streaming replication because if we don't flush WAL regularly we will
1677  * find that large aborts leave us with a long backlog for when commits
1678  * occur after the abort, increasing our window of data loss should
1679  * problems occur at that point.
1680  */
1681  if (!isSubXact)
1683 
1684  /*
1685  * Mark the transaction aborted in clog. This is not absolutely necessary
1686  * but we may as well do it while we are here; also, in the subxact case
1687  * it is helpful because XactLockTableWait makes use of it to avoid
1688  * waiting for already-aborted subtransactions. It is OK to do it without
1689  * having flushed the ABORT record to disk, because in event of a crash
1690  * we'd be assumed to have aborted anyway.
1691  */
1692  TransactionIdAbortTree(xid, nchildren, children);
1693 
1694  END_CRIT_SECTION();
1695 
1696  /* Compute latestXid while we have the child XIDs handy */
1697  latestXid = TransactionIdLatest(xid, nchildren, children);
1698 
1699  /*
1700  * If we're aborting a subtransaction, we can immediately remove failed
1701  * XIDs from PGPROC's cache of running child XIDs. We do that here for
1702  * subxacts, because we already have the child XID array at hand. For
1703  * main xacts, the equivalent happens just after this function returns.
1704  */
1705  if (isSubXact)
1706  XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1707 
1708  /* Reset XactLastRecEnd until the next transaction writes something */
1709  if (!isSubXact)
1710  XactLastRecEnd = 0;
1711 
1712  /* And clean up local data */
1713  if (rels)
1714  pfree(rels);
1715 
1716  return latestXid;
1717 }
1718 
1719 /*
1720  * AtAbort_Memory
1721  */
1722 static void
1724 {
1725  /*
1726  * Switch into TransactionAbortContext, which should have some free space
1727  * even if nothing else does. We'll work in this context until we've
1728  * finished cleaning up.
1729  *
1730  * It is barely possible to get here when we've not been able to create
1731  * TransactionAbortContext yet; if so use TopMemoryContext.
1732  */
1733  if (TransactionAbortContext != NULL)
1734  MemoryContextSwitchTo(TransactionAbortContext);
1735  else
1737 }
1738 
1739 /*
1740  * AtSubAbort_Memory
1741  */
1742 static void
1744 {
1745  Assert(TransactionAbortContext != NULL);
1746 
1747  MemoryContextSwitchTo(TransactionAbortContext);
1748 }
1749 
1750 
1751 /*
1752  * AtAbort_ResourceOwner
1753  */
1754 static void
1756 {
1757  /*
1758  * Make sure we have a valid ResourceOwner, if possible (else it will be
1759  * NULL, which is OK)
1760  */
1762 }
1763 
1764 /*
1765  * AtSubAbort_ResourceOwner
1766  */
1767 static void
1769 {
1771 
1772  /* Make sure we have a valid ResourceOwner */
1774 }
1775 
1776 
1777 /*
1778  * AtSubAbort_childXids
1779  */
1780 static void
1782 {
1784 
1785  /*
1786  * We keep the child-XID arrays in TopTransactionContext (see
1787  * AtSubCommit_childXids). This means we'd better free the array
1788  * explicitly at abort to avoid leakage.
1789  */
1790  if (s->childXids != NULL)
1791  pfree(s->childXids);
1792  s->childXids = NULL;
1793  s->nChildXids = 0;
1794  s->maxChildXids = 0;
1795 
1796  /*
1797  * We could prune the unreportedXids array here. But we don't bother. That
1798  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1799  * would likely introduce more CPU time into the more common paths, so we
1800  * choose not to do that.
1801  */
1802 }
1803 
1804 /* ----------------------------------------------------------------
1805  * CleanupTransaction stuff
1806  * ----------------------------------------------------------------
1807  */
1808 
1809 /*
1810  * AtCleanup_Memory
1811  */
1812 static void
1814 {
1815  Assert(CurrentTransactionState->parent == NULL);
1816 
1817  /*
1818  * Now that we're "out" of a transaction, have the system allocate things
1819  * in the top memory context instead of per-transaction contexts.
1820  */
1822 
1823  /*
1824  * Clear the special abort context for next time.
1825  */
1826  if (TransactionAbortContext != NULL)
1827  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1828 
1829  /*
1830  * Release all transaction-local memory.
1831  */
1832  if (TopTransactionContext != NULL)
1834  TopTransactionContext = NULL;
1835  CurTransactionContext = NULL;
1836  CurrentTransactionState->curTransactionContext = NULL;
1837 }
1838 
1839 
1840 /* ----------------------------------------------------------------
1841  * CleanupSubTransaction stuff
1842  * ----------------------------------------------------------------
1843  */
1844 
1845 /*
1846  * AtSubCleanup_Memory
1847  */
1848 static void
1850 {
1852 
1853  Assert(s->parent != NULL);
1854 
1855  /* Make sure we're not in an about-to-be-deleted context */
1858 
1859  /*
1860  * Clear the special abort context for next time.
1861  */
1862  if (TransactionAbortContext != NULL)
1863  MemoryContextResetAndDeleteChildren(TransactionAbortContext);
1864 
1865  /*
1866  * Delete the subxact local memory contexts. Its CurTransactionContext can
1867  * go too (note this also kills CurTransactionContexts from any children
1868  * of the subxact).
1869  */
1870  if (s->curTransactionContext)
1872  s->curTransactionContext = NULL;
1873 }
1874 
1875 /* ----------------------------------------------------------------
1876  * interface routines
1877  * ----------------------------------------------------------------
1878  */
1879 
1880 /*
1881  * StartTransaction
1882  */
1883 static void
1885 {
1886  TransactionState s;
1887  VirtualTransactionId vxid;
1888 
1889  /*
1890  * Let's just make sure the state stack is empty
1891  */
1893  CurrentTransactionState = s;
1894 
1895  Assert(!FullTransactionIdIsValid(XactTopFullTransactionId));
1896 
1897  /* check the current transaction state */
1898  Assert(s->state == TRANS_DEFAULT);
1899 
1900  /*
1901  * Set the current transaction state information appropriately during
1902  * start processing. Note that once the transaction status is switched
1903  * this process cannot fail until the user ID and the security context
1904  * flags are fetched below.
1905  */
1906  s->state = TRANS_START;
1907  s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
1908 
1909  /* Determine if statements are logged in this transaction */
1911  (log_xact_sample_rate == 1 ||
1913 
1914  /*
1915  * initialize current transaction state fields
1916  *
1917  * note: prevXactReadOnly is not used at the outermost level
1918  */
1919  s->nestingLevel = 1;
1920  s->gucNestLevel = 1;
1921  s->childXids = NULL;
1922  s->nChildXids = 0;
1923  s->maxChildXids = 0;
1924 
1925  /*
1926  * Once the current user ID and the security context flags are fetched,
1927  * both will be properly reset even if transaction startup fails.
1928  */
1930 
1931  /* SecurityRestrictionContext should never be set outside a transaction */
1932  Assert(s->prevSecContext == 0);
1933 
1934  /*
1935  * Make sure we've reset xact state variables
1936  *
1937  * If recovery is still in progress, mark this transaction as read-only.
1938  * We have lower level defences in XLogInsert and elsewhere to stop us
1939  * from modifying data during recovery, but this gives the normal
1940  * indication to the user that the transaction is read-only.
1941  */
1942  if (RecoveryInProgress())
1943  {
1944  s->startedInRecovery = true;
1945  XactReadOnly = true;
1946  }
1947  else
1948  {
1949  s->startedInRecovery = false;
1951  }
1954  forceSyncCommit = false;
1955  MyXactFlags = 0;
1956 
1957  /*
1958  * reinitialize within-transaction counters
1959  */
1963  currentCommandIdUsed = false;
1964 
1965  /*
1966  * initialize reported xid accounting
1967  */
1968  nUnreportedXids = 0;
1969  s->didLogXid = false;
1970 
1971  /*
1972  * must initialize resource-management stuff first
1973  */
1974  AtStart_Memory();
1976 
1977  /*
1978  * Assign a new LocalTransactionId, and combine it with the backendId to
1979  * form a virtual transaction id.
1980  */
1981  vxid.backendId = MyBackendId;
1983 
1984  /*
1985  * Lock the virtual transaction id before we announce it in the proc array
1986  */
1988 
1989  /*
1990  * Advertise it in the proc array. We assume assignment of
1991  * localTransactionId is atomic, and the backendId should be set already.
1992  */
1993  Assert(MyProc->backendId == vxid.backendId);
1994  MyProc->lxid = vxid.localTransactionId;
1995 
1996  TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
1997 
1998  /*
1999  * set transaction_timestamp() (a/k/a now()). Normally, we want this to
2000  * be the same as the first command's statement_timestamp(), so don't do a
2001  * fresh GetCurrentTimestamp() call (which'd be expensive anyway). But
2002  * for transactions started inside procedures (i.e., nonatomic SPI
2003  * contexts), we do need to advance the timestamp. Also, in a parallel
2004  * worker, the timestamp should already have been provided by a call to
2005  * SetParallelStartTimestamps().
2006  */
2007  if (!IsParallelWorker())
2008  {
2011  else
2013  }
2014  else
2015  Assert(xactStartTimestamp != 0);
2017  /* Mark xactStopTimestamp as unset. */
2018  xactStopTimestamp = 0;
2019 
2020  /*
2021  * initialize other subsystems for new transaction
2022  */
2023  AtStart_GUC();
2024  AtStart_Cache();
2026 
2027  /*
2028  * done with start processing, set current transaction state to "in
2029  * progress"
2030  */
2031  s->state = TRANS_INPROGRESS;
2032 
2033  ShowTransactionState("StartTransaction");
2034 }
2035 
2036 
2037 /*
2038  * CommitTransaction
2039  *
2040  * NB: if you change this routine, better look at PrepareTransaction too!
2041  */
2042 static void
2044 {
2046  TransactionId latestXid;
2047  bool is_parallel_worker;
2048 
2049  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2050 
2051  /* Enforce parallel mode restrictions during parallel worker commit. */
2052  if (is_parallel_worker)
2054 
2055  ShowTransactionState("CommitTransaction");
2056 
2057  /*
2058  * check the current transaction state
2059  */
2060  if (s->state != TRANS_INPROGRESS)
2061  elog(WARNING, "CommitTransaction while in %s state",
2063  Assert(s->parent == NULL);
2064 
2065  /*
2066  * Do pre-commit processing that involves calling user-defined code, such
2067  * as triggers. Since closing cursors could queue trigger actions,
2068  * triggers could open cursors, etc, we have to keep looping until there's
2069  * nothing left to do.
2070  */
2071  for (;;)
2072  {
2073  /*
2074  * Fire all currently pending deferred triggers.
2075  */
2077 
2078  /*
2079  * Close open portals (converting holdable ones into static portals).
2080  * If there weren't any, we are done ... otherwise loop back to check
2081  * if they queued deferred triggers. Lather, rinse, repeat.
2082  */
2083  if (!PreCommit_Portals(false))
2084  break;
2085  }
2086 
2089 
2090  /*
2091  * The remaining actions cannot call any user-defined code, so it's safe
2092  * to start shutting down within-transaction services. But note that most
2093  * of this stuff could still throw an error, which would switch us into
2094  * the transaction-abort path.
2095  */
2096 
2097  /* If we might have parallel workers, clean them up now. */
2098  if (IsInParallelMode())
2099  AtEOXact_Parallel(true);
2100 
2101  /* Shut down the deferred-trigger manager */
2102  AfterTriggerEndXact(true);
2103 
2104  /*
2105  * Let ON COMMIT management do its thing (must happen after closing
2106  * cursors, to avoid dangling-reference problems)
2107  */
2109 
2110  /* close large objects before lower-level cleanup */
2111  AtEOXact_LargeObject(true);
2112 
2113  /*
2114  * Mark serializable transaction as complete for predicate locking
2115  * purposes. This should be done as late as we can put it and still allow
2116  * errors to be raised for failure patterns found at commit. This is not
2117  * appropriate in a parallel worker however, because we aren't committing
2118  * the leader's transaction and its serializable state will live on.
2119  */
2120  if (!is_parallel_worker)
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.
2127  */
2128  PreCommit_Notify();
2129 
2130  /* Prevent cancel/die interrupt while cleaning up */
2131  HOLD_INTERRUPTS();
2132 
2133  /* Commit updates to the relation map --- do this as late as possible */
2134  AtEOXact_RelationMap(true, is_parallel_worker);
2135 
2136  /*
2137  * set the current transaction state information appropriately during
2138  * commit processing
2139  */
2140  s->state = TRANS_COMMIT;
2141  s->parallelModeLevel = 0;
2142 
2143  if (!is_parallel_worker)
2144  {
2145  /*
2146  * We need to mark our XIDs as committed in pg_xact. This is where we
2147  * durably commit.
2148  */
2149  latestXid = RecordTransactionCommit();
2150  }
2151  else
2152  {
2153  /*
2154  * We must not mark our XID committed; the parallel master is
2155  * responsible for that.
2156  */
2157  latestXid = InvalidTransactionId;
2158 
2159  /*
2160  * Make sure the master will know about any WAL we wrote before it
2161  * commits.
2162  */
2164  }
2165 
2166  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2167 
2168  /*
2169  * Let others know about no transaction in progress by me. Note that this
2170  * must be done _before_ releasing locks we hold and _after_
2171  * RecordTransactionCommit.
2172  */
2173  ProcArrayEndTransaction(MyProc, latestXid);
2174 
2175  /*
2176  * This is all post-commit cleanup. Note that if an error is raised here,
2177  * it's too late to abort the transaction. This should be just
2178  * noncritical resource releasing.
2179  *
2180  * The ordering of operations is not entirely random. The idea is:
2181  * release resources visible to other backends (eg, files, buffer pins);
2182  * then release locks; then release backend-local resources. We want to
2183  * release locks at the point where any backend waiting for us will see
2184  * our transaction as being fully cleaned up.
2185  *
2186  * Resources that can be associated with individual queries are handled by
2187  * the ResourceOwner mechanism. The other calls here are for backend-wide
2188  * state.
2189  */
2190 
2191  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2192  : XACT_EVENT_COMMIT);
2193 
2196  true, true);
2197 
2198  /* Check we've released all buffer pins */
2199  AtEOXact_Buffers(true);
2200 
2201  /* Clean up the relation cache */
2202  AtEOXact_RelationCache(true);
2203 
2204  /*
2205  * Make catalog changes visible to all backends. This has to happen after
2206  * relcache references are dropped (see comments for
2207  * AtEOXact_RelationCache), but before locks are released (if anyone is
2208  * waiting for lock on a relation we've modified, we want them to know
2209  * about the catalog change before they start using the relation).
2210  */
2211  AtEOXact_Inval(true);
2212 
2214 
2217  true, true);
2220  true, true);
2221 
2222  /*
2223  * Likewise, dropping of files deleted during the transaction is best done
2224  * after releasing relcache and buffer pins. (This is not strictly
2225  * necessary during commit, since such pins should have been released
2226  * already, but this ordering is definitely critical during abort.) Since
2227  * this may take many seconds, also delay until after releasing locks.
2228  * Other backends will observe the attendant catalog changes and not
2229  * attempt to access affected files.
2230  */
2231  smgrDoPendingDeletes(true);
2232 
2233  AtCommit_Notify();
2234  AtEOXact_GUC(true, 1);
2235  AtEOXact_SPI(true);
2236  AtEOXact_Enum();
2238  AtEOXact_Namespace(true, is_parallel_worker);
2239  AtEOXact_SMgr();
2240  AtEOXact_Files(true);
2242  AtEOXact_HashTables(true);
2243  AtEOXact_PgStat(true, is_parallel_worker);
2244  AtEOXact_Snapshot(true, false);
2245  AtEOXact_ApplyLauncher(true);
2247 
2248  CurrentResourceOwner = NULL;
2250  s->curTransactionOwner = NULL;
2253 
2254  AtCommit_Memory();
2255 
2258  s->nestingLevel = 0;
2259  s->gucNestLevel = 0;
2260  s->childXids = NULL;
2261  s->nChildXids = 0;
2262  s->maxChildXids = 0;
2263 
2264  XactTopFullTransactionId = InvalidFullTransactionId;
2266 
2267  /*
2268  * done with commit processing, set current transaction state back to
2269  * default
2270  */
2271  s->state = TRANS_DEFAULT;
2272 
2274 }
2275 
2276 
2277 /*
2278  * PrepareTransaction
2279  *
2280  * NB: if you change this routine, better look at CommitTransaction too!
2281  */
2282 static void
2284 {
2287  GlobalTransaction gxact;
2288  TimestampTz prepared_at;
2289 
2291 
2292  ShowTransactionState("PrepareTransaction");
2293 
2294  /*
2295  * check the current transaction state
2296  */
2297  if (s->state != TRANS_INPROGRESS)
2298  elog(WARNING, "PrepareTransaction while in %s state",
2300  Assert(s->parent == NULL);
2301 
2302  /*
2303  * Do pre-commit processing that involves calling user-defined code, such
2304  * as triggers. Since closing cursors could queue trigger actions,
2305  * triggers could open cursors, etc, we have to keep looping until there's
2306  * nothing left to do.
2307  */
2308  for (;;)
2309  {
2310  /*
2311  * Fire all currently pending deferred triggers.
2312  */
2314 
2315  /*
2316  * Close open portals (converting holdable ones into static portals).
2317  * If there weren't any, we are done ... otherwise loop back to check
2318  * if they queued deferred triggers. Lather, rinse, repeat.
2319  */
2320  if (!PreCommit_Portals(true))
2321  break;
2322  }
2323 
2325 
2326  /*
2327  * The remaining actions cannot call any user-defined code, so it's safe
2328  * to start shutting down within-transaction services. But note that most
2329  * of this stuff could still throw an error, which would switch us into
2330  * the transaction-abort path.
2331  */
2332 
2333  /* Shut down the deferred-trigger manager */
2334  AfterTriggerEndXact(true);
2335 
2336  /*
2337  * Let ON COMMIT management do its thing (must happen after closing
2338  * cursors, to avoid dangling-reference problems)
2339  */
2341 
2342  /* close large objects before lower-level cleanup */
2343  AtEOXact_LargeObject(true);
2344 
2345  /*
2346  * Mark serializable transaction as complete for predicate locking
2347  * purposes. This should be done as late as we can put it and still allow
2348  * errors to be raised for failure patterns found at commit.
2349  */
2351 
2352  /* NOTIFY will be handled below */
2353 
2354  /*
2355  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2356  * this transaction. Having the prepared xact hold locks on another
2357  * backend's temp table seems a bad idea --- for instance it would prevent
2358  * the backend from exiting. There are other problems too, such as how to
2359  * clean up the source backend's local buffers and ON COMMIT state if the
2360  * prepared xact includes a DROP of a temp table.
2361  *
2362  * Other objects types, like functions, operators or extensions, share the
2363  * same restriction as they should not be created, locked or dropped as
2364  * this can mess up with this session or even a follow-up session trying
2365  * to use the same temporary namespace.
2366  *
2367  * We must check this after executing any ON COMMIT actions, because they
2368  * might still access a temp relation.
2369  *
2370  * XXX In principle this could be relaxed to allow some useful special
2371  * cases, such as a temp table created and dropped all within the
2372  * transaction. That seems to require much more bookkeeping though.
2373  */
2375  ereport(ERROR,
2376  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2377  errmsg("cannot PREPARE a transaction that has operated on temporary objects")));
2378 
2379  /*
2380  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2381  * supported if we added cleanup logic to twophase.c, but for now it
2382  * doesn't seem worth the trouble.
2383  */
2385  ereport(ERROR,
2386  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2387  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2388 
2389  /*
2390  * Don't allow PREPARE but for transaction that has/might kill logical
2391  * replication workers.
2392  */
2394  ereport(ERROR,
2395  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2396  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
2397 
2398  /* Prevent cancel/die interrupt while cleaning up */
2399  HOLD_INTERRUPTS();
2400 
2401  /*
2402  * set the current transaction state information appropriately during
2403  * prepare processing
2404  */
2405  s->state = TRANS_PREPARE;
2406 
2407  prepared_at = GetCurrentTimestamp();
2408 
2409  /* Tell bufmgr and smgr to prepare for commit */
2410  BufmgrCommit();
2411 
2412  /*
2413  * Reserve the GID for this transaction. This could fail if the requested
2414  * GID is invalid or already in use.
2415  */
2416  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2417  GetUserId(), MyDatabaseId);
2418  prepareGID = NULL;
2419 
2420  /*
2421  * Collect data for the 2PC state file. Note that in general, no actual
2422  * state change should happen in the called modules during this step,
2423  * since it's still possible to fail before commit, and in that case we
2424  * want transaction abort to be able to clean up. (In particular, the
2425  * AtPrepare routines may error out if they find cases they cannot
2426  * handle.) State cleanup should happen in the PostPrepare routines
2427  * below. However, some modules can go ahead and clear state here because
2428  * they wouldn't do anything with it during abort anyway.
2429  *
2430  * Note: because the 2PC state file records will be replayed in the same
2431  * order they are made, the order of these calls has to match the order in
2432  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2433  * PREPARED; in particular, pay attention to whether things should happen
2434  * before or after releasing the transaction's locks.
2435  */
2436  StartPrepare(gxact);
2437 
2438  AtPrepare_Notify();
2439  AtPrepare_Locks();
2441  AtPrepare_PgStat();
2444 
2445  /*
2446  * Here is where we really truly prepare.
2447  *
2448  * We have to record transaction prepares even if we didn't make any
2449  * updates, because the transaction manager might get confused if we lose
2450  * a global transaction.
2451  */
2452  EndPrepare(gxact);
2453 
2454  /*
2455  * Now we clean up backend-internal state and release internal resources.
2456  */
2457 
2458  /* Reset XactLastRecEnd until the next transaction writes something */
2459  XactLastRecEnd = 0;
2460 
2461  /*
2462  * Let others know about no transaction in progress by me. This has to be
2463  * done *after* the prepared transaction has been marked valid, else
2464  * someone may think it is unlocked and recyclable.
2465  */
2467 
2468  /*
2469  * In normal commit-processing, this is all non-critical post-transaction
2470  * cleanup. When the transaction is prepared, however, it's important
2471  * that the locks and other per-backend resources are transferred to the
2472  * prepared transaction's PGPROC entry. Note that if an error is raised
2473  * here, it's too late to abort the transaction. XXX: This probably should
2474  * be in a critical section, to force a PANIC if any of this fails, but
2475  * that cure could be worse than the disease.
2476  */
2477 
2479 
2482  true, true);
2483 
2484  /* Check we've released all buffer pins */
2485  AtEOXact_Buffers(true);
2486 
2487  /* Clean up the relation cache */
2488  AtEOXact_RelationCache(true);
2489 
2490  /* notify doesn't need a postprepare call */
2491 
2493 
2495 
2496  PostPrepare_smgr();
2497 
2498  PostPrepare_MultiXact(xid);
2499 
2500  PostPrepare_Locks(xid);
2502 
2505  true, true);
2508  true, true);
2509 
2510  /*
2511  * Allow another backend to finish the transaction. After
2512  * PostPrepare_Twophase(), the transaction is completely detached from our
2513  * backend. The rest is just non-critical cleanup of backend-local state.
2514  */
2516 
2517  /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2518  AtEOXact_GUC(true, 1);
2519  AtEOXact_SPI(true);
2520  AtEOXact_Enum();
2522  AtEOXact_Namespace(true, false);
2523  AtEOXact_SMgr();
2524  AtEOXact_Files(true);
2526  AtEOXact_HashTables(true);
2527  /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2528  AtEOXact_Snapshot(true, true);
2530 
2531  CurrentResourceOwner = NULL;
2533  s->curTransactionOwner = NULL;
2536 
2537  AtCommit_Memory();
2538 
2541  s->nestingLevel = 0;
2542  s->gucNestLevel = 0;
2543  s->childXids = NULL;
2544  s->nChildXids = 0;
2545  s->maxChildXids = 0;
2546 
2547  XactTopFullTransactionId = InvalidFullTransactionId;
2549 
2550  /*
2551  * done with 1st phase commit processing, set current transaction state
2552  * back to default
2553  */
2554  s->state = TRANS_DEFAULT;
2555 
2557 }
2558 
2559 
2560 /*
2561  * AbortTransaction
2562  */
2563 static void
2565 {
2567  TransactionId latestXid;
2568  bool is_parallel_worker;
2569 
2570  /* Prevent cancel/die interrupt while cleaning up */
2571  HOLD_INTERRUPTS();
2572 
2573  /* Make sure we have a valid memory context and resource owner */
2574  AtAbort_Memory();
2576 
2577  /*
2578  * Release any LW locks we might be holding as quickly as possible.
2579  * (Regular locks, however, must be held till we finish aborting.)
2580  * Releasing LW locks is critical since we might try to grab them again
2581  * while cleaning up!
2582  */
2583  LWLockReleaseAll();
2584 
2585  /* Clear wait information and command progress indicator */
2588 
2589  /* Clean up buffer I/O and buffer context locks, too */
2590  AbortBufferIO();
2591  UnlockBuffers();
2592 
2593  /* Reset WAL record construction state */
2595 
2596  /* Cancel condition variable sleep */
2598 
2599  /*
2600  * Also clean up any open wait for lock, since the lock manager will choke
2601  * if we try to wait for another lock before doing this.
2602  */
2603  LockErrorCleanup();
2604 
2605  /*
2606  * If any timeout events are still active, make sure the timeout interrupt
2607  * is scheduled. This covers possible loss of a timeout interrupt due to
2608  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2609  * We delay this till after LockErrorCleanup so that we don't uselessly
2610  * reschedule lock or deadlock check timeouts.
2611  */
2613 
2614  /*
2615  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2616  * handler. We do this fairly early in the sequence so that the timeout
2617  * infrastructure will be functional if needed while aborting.
2618  */
2620 
2621  /*
2622  * check the current transaction state
2623  */
2624  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2625  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2626  elog(WARNING, "AbortTransaction while in %s state",
2628  Assert(s->parent == NULL);
2629 
2630  /*
2631  * set the current transaction state information appropriately during the
2632  * abort processing
2633  */
2634  s->state = TRANS_ABORT;
2635 
2636  /*
2637  * Reset user ID which might have been changed transiently. We need this
2638  * to clean up in case control escaped out of a SECURITY DEFINER function
2639  * or other local change of CurrentUserId; therefore, the prior value of
2640  * SecurityRestrictionContext also needs to be restored.
2641  *
2642  * (Note: it is not necessary to restore session authorization or role
2643  * settings here because those can only be changed via GUC, and GUC will
2644  * take care of rolling them back if need be.)
2645  */
2647 
2648  /* If in parallel mode, clean up workers and exit parallel mode. */
2649  if (IsInParallelMode())
2650  {
2651  AtEOXact_Parallel(false);
2652  s->parallelModeLevel = 0;
2653  }
2654 
2655  /*
2656  * do abort processing
2657  */
2658  AfterTriggerEndXact(false); /* 'false' means it's abort */
2659  AtAbort_Portals();
2660  AtEOXact_LargeObject(false);
2661  AtAbort_Notify();
2662  AtEOXact_RelationMap(false, is_parallel_worker);
2663  AtAbort_Twophase();
2664 
2665  /*
2666  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2667  * far as assigning an XID to advertise). But if we're inside a parallel
2668  * worker, skip this; the user backend must be the one to write the abort
2669  * record.
2670  */
2671  if (!is_parallel_worker)
2672  latestXid = RecordTransactionAbort(false);
2673  else
2674  {
2675  latestXid = InvalidTransactionId;
2676 
2677  /*
2678  * Since the parallel master won't get our value of XactLastRecEnd in
2679  * this case, we nudge WAL-writer ourselves in this case. See related
2680  * comments in RecordTransactionAbort for why this matters.
2681  */
2683  }
2684 
2685  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2686 
2687  /*
2688  * Let others know about no transaction in progress by me. Note that this
2689  * must be done _before_ releasing locks we hold and _after_
2690  * RecordTransactionAbort.
2691  */
2692  ProcArrayEndTransaction(MyProc, latestXid);
2693 
2694  /*
2695  * Post-abort cleanup. See notes in CommitTransaction() concerning
2696  * ordering. We can skip all of it if the transaction failed before
2697  * creating a resource owner.
2698  */
2699  if (TopTransactionResourceOwner != NULL)
2700  {
2701  if (is_parallel_worker)
2703  else
2705 
2708  false, true);
2709  AtEOXact_Buffers(false);
2710  AtEOXact_RelationCache(false);
2711  AtEOXact_Inval(false);
2715  false, true);
2718  false, true);
2719  smgrDoPendingDeletes(false);
2720 
2721  AtEOXact_GUC(false, 1);
2722  AtEOXact_SPI(false);
2723  AtEOXact_Enum();
2725  AtEOXact_Namespace(false, is_parallel_worker);
2726  AtEOXact_SMgr();
2727  AtEOXact_Files(false);
2729  AtEOXact_HashTables(false);
2730  AtEOXact_PgStat(false, is_parallel_worker);
2731  AtEOXact_ApplyLauncher(false);
2733  }
2734 
2735  /*
2736  * State remains TRANS_ABORT until CleanupTransaction().
2737  */
2739 }
2740 
2741 /*
2742  * CleanupTransaction
2743  */
2744 static void
2746 {
2748 
2749  /*
2750  * State should still be TRANS_ABORT from AbortTransaction().
2751  */
2752  if (s->state != TRANS_ABORT)
2753  elog(FATAL, "CleanupTransaction: unexpected state %s",
2755 
2756  /*
2757  * do abort cleanup processing
2758  */
2759  AtCleanup_Portals(); /* now safe to release portal memory */
2760  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2761 
2762  CurrentResourceOwner = NULL; /* and resource owner */
2765  s->curTransactionOwner = NULL;
2768 
2769  AtCleanup_Memory(); /* and transaction memory */
2770 
2773  s->nestingLevel = 0;
2774  s->gucNestLevel = 0;
2775  s->childXids = NULL;
2776  s->nChildXids = 0;
2777  s->maxChildXids = 0;
2778  s->parallelModeLevel = 0;
2779 
2780  XactTopFullTransactionId = InvalidFullTransactionId;
2782 
2783  /*
2784  * done with abort processing, set current transaction state back to
2785  * default
2786  */
2787  s->state = TRANS_DEFAULT;
2788 }
2789 
2790 /*
2791  * StartTransactionCommand
2792  */
2793 void
2795 {
2797 
2798  switch (s->blockState)
2799  {
2800  /*
2801  * if we aren't in a transaction block, we just do our usual start
2802  * transaction.
2803  */
2804  case TBLOCK_DEFAULT:
2805  StartTransaction();
2807  break;
2808 
2809  /*
2810  * We are somewhere in a transaction block or subtransaction and
2811  * about to start a new command. For now we do nothing, but
2812  * someday we may do command-local resource initialization. (Note
2813  * that any needed CommandCounterIncrement was done by the
2814  * previous CommitTransactionCommand.)
2815  */
2816  case TBLOCK_INPROGRESS:
2818  case TBLOCK_SUBINPROGRESS:
2819  break;
2820 
2821  /*
2822  * Here we are in a failed transaction block (one of the commands
2823  * caused an abort) so we do nothing but remain in the abort
2824  * state. Eventually we will get a ROLLBACK command which will
2825  * get us out of this state. (It is up to other code to ensure
2826  * that no commands other than ROLLBACK will be processed in these
2827  * states.)
2828  */
2829  case TBLOCK_ABORT:
2830  case TBLOCK_SUBABORT:
2831  break;
2832 
2833  /* These cases are invalid. */
2834  case TBLOCK_STARTED:
2835  case TBLOCK_BEGIN:
2837  case TBLOCK_SUBBEGIN:
2838  case TBLOCK_END:
2839  case TBLOCK_SUBRELEASE:
2840  case TBLOCK_SUBCOMMIT:
2841  case TBLOCK_ABORT_END:
2842  case TBLOCK_SUBABORT_END:
2843  case TBLOCK_ABORT_PENDING:
2845  case TBLOCK_SUBRESTART:
2847  case TBLOCK_PREPARE:
2848  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2850  break;
2851  }
2852 
2853  /*
2854  * We must switch to CurTransactionContext before returning. This is
2855  * already done if we called StartTransaction, otherwise not.
2856  */
2857  Assert(CurTransactionContext != NULL);
2859 }
2860 
2861 
2862 /*
2863  * Simple system for saving and restoring transaction characteristics
2864  * (isolation level, read only, deferrable). We need this for transaction
2865  * chaining, so that we can set the characteristics of the new transaction to
2866  * be the same as the previous one. (We need something like this because the
2867  * GUC system resets the characteristics at transaction end, so for example
2868  * just skipping the reset in StartTransaction() won't work.)
2869  */
2871 static bool save_XactReadOnly;
2873 
2874 void
2876 {
2880 }
2881 
2882 void
2884 {
2888 }
2889 
2890 
2891 /*
2892  * CommitTransactionCommand
2893  */
2894 void
2896 {
2898 
2899  if (s->chain)
2901 
2902  switch (s->blockState)
2903  {
2904  /*
2905  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2906  * StartTransactionCommand didn't set the STARTED state
2907  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2908  * by EndParallelWorkerTransaction(), not this function.
2909  */
2910  case TBLOCK_DEFAULT:
2912  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2914  break;
2915 
2916  /*
2917  * If we aren't in a transaction block, just do our usual
2918  * transaction commit, and return to the idle state.
2919  */
2920  case TBLOCK_STARTED:
2923  break;
2924 
2925  /*
2926  * We are completing a "BEGIN TRANSACTION" command, so we change
2927  * to the "transaction block in progress" state and return. (We
2928  * assume the BEGIN did nothing to the database, so we need no
2929  * CommandCounterIncrement.)
2930  */
2931  case TBLOCK_BEGIN:
2933  break;
2934 
2935  /*
2936  * This is the case when we have finished executing a command
2937  * someplace within a transaction block. We increment the command
2938  * counter and return.
2939  */
2940  case TBLOCK_INPROGRESS:
2942  case TBLOCK_SUBINPROGRESS:
2944  break;
2945 
2946  /*
2947  * We are completing a "COMMIT" command. Do it and return to the
2948  * idle state.
2949  */
2950  case TBLOCK_END:
2953  if (s->chain)
2954  {
2955  StartTransaction();
2957  s->chain = false;
2959  }
2960  break;
2961 
2962  /*
2963  * Here we are in the middle of a transaction block but one of the
2964  * commands caused an abort so we do nothing but remain in the
2965  * abort state. Eventually we will get a ROLLBACK command.
2966  */
2967  case TBLOCK_ABORT:
2968  case TBLOCK_SUBABORT:
2969  break;
2970 
2971  /*
2972  * Here we were in an aborted transaction block and we just got
2973  * the ROLLBACK command from the user, so clean up the
2974  * already-aborted transaction and return to the idle state.
2975  */
2976  case TBLOCK_ABORT_END:
2979  if (s->chain)
2980  {
2981  StartTransaction();
2983  s->chain = false;
2985  }
2986  break;
2987 
2988  /*
2989  * Here we were in a perfectly good transaction block but the user
2990  * told us to ROLLBACK anyway. We have to abort the transaction
2991  * and then clean up.
2992  */
2993  case TBLOCK_ABORT_PENDING:
2994  AbortTransaction();
2997  if (s->chain)
2998  {
2999  StartTransaction();
3001  s->chain = false;
3003  }
3004  break;
3005 
3006  /*
3007  * We are completing a "PREPARE TRANSACTION" command. Do it and
3008  * return to the idle state.
3009  */
3010  case TBLOCK_PREPARE:
3013  break;
3014 
3015  /*
3016  * We were just issued a SAVEPOINT inside a transaction block.
3017  * Start a subtransaction. (DefineSavepoint already did
3018  * PushTransaction, so as to have someplace to put the SUBBEGIN
3019  * state.)
3020  */
3021  case TBLOCK_SUBBEGIN:
3024  break;
3025 
3026  /*
3027  * We were issued a RELEASE command, so we end the current
3028  * subtransaction and return to the parent transaction. The parent
3029  * might be ended too, so repeat till we find an INPROGRESS
3030  * transaction or subtransaction.
3031  */
3032  case TBLOCK_SUBRELEASE:
3033  do
3034  {
3036  s = CurrentTransactionState; /* changed by pop */
3037  } while (s->blockState == TBLOCK_SUBRELEASE);
3038 
3041  break;
3042 
3043  /*
3044  * We were issued a COMMIT, so we end the current subtransaction
3045  * hierarchy and perform final commit. We do this by rolling up
3046  * any subtransactions into their parent, which leads to O(N^2)
3047  * operations with respect to resource owners - this isn't that
3048  * bad until we approach a thousands of savepoints but is
3049  * necessary for correctness should after triggers create new
3050  * resource owners.
3051  */
3052  case TBLOCK_SUBCOMMIT:
3053  do
3054  {
3056  s = CurrentTransactionState; /* changed by pop */
3057  } while (s->blockState == TBLOCK_SUBCOMMIT);
3058  /* If we had a COMMIT command, finish off the main xact too */
3059  if (s->blockState == TBLOCK_END)
3060  {
3061  Assert(s->parent == NULL);
3064  }
3065  else if (s->blockState == TBLOCK_PREPARE)
3066  {
3067  Assert(s->parent == NULL);
3070  }
3071  else
3072  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3074  break;
3075 
3076  /*
3077  * The current already-failed subtransaction is ending due to a
3078  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3079  * examine the parent (which could be in any of several states).
3080  */
3081  case TBLOCK_SUBABORT_END:
3084  break;
3085 
3086  /*
3087  * As above, but it's not dead yet, so abort first.
3088  */
3093  break;
3094 
3095  /*
3096  * The current subtransaction is the target of a ROLLBACK TO
3097  * command. Abort and pop it, then start a new subtransaction
3098  * with the same name.
3099  */
3100  case TBLOCK_SUBRESTART:
3101  {
3102  char *name;
3103  int savepointLevel;
3104 
3105  /* save name and keep Cleanup from freeing it */
3106  name = s->name;
3107  s->name = NULL;
3108  savepointLevel = s->savepointLevel;
3109 
3112 
3113  DefineSavepoint(NULL);
3114  s = CurrentTransactionState; /* changed by push */
3115  s->name = name;
3117 
3118  /* This is the same as TBLOCK_SUBBEGIN case */
3122  }
3123  break;
3124 
3125  /*
3126  * Same as above, but the subtransaction had already failed, so we
3127  * don't need AbortSubTransaction.
3128  */
3130  {
3131  char *name;
3132  int savepointLevel;
3133 
3134  /* save name and keep Cleanup from freeing it */
3135  name = s->name;
3136  s->name = NULL;
3137  savepointLevel = s->savepointLevel;
3138 
3140 
3141  DefineSavepoint(NULL);
3142  s = CurrentTransactionState; /* changed by push */
3143  s->name = name;
3145 
3146  /* This is the same as TBLOCK_SUBBEGIN case */
3150  }
3151  break;
3152  }
3153 }
3154 
3155 /*
3156  * AbortCurrentTransaction
3157  */
3158 void
3160 {
3162 
3163  switch (s->blockState)
3164  {
3165  case TBLOCK_DEFAULT:
3166  if (s->state == TRANS_DEFAULT)
3167  {
3168  /* we are idle, so nothing to do */
3169  }
3170  else
3171  {
3172  /*
3173  * We can get here after an error during transaction start
3174  * (state will be TRANS_START). Need to clean up the
3175  * incompletely started transaction. First, adjust the
3176  * low-level state to suppress warning message from
3177  * AbortTransaction.
3178  */
3179  if (s->state == TRANS_START)
3180  s->state = TRANS_INPROGRESS;
3181  AbortTransaction();
3183  }
3184  break;
3185 
3186  /*
3187  * If we aren't in a transaction block, we just do the basic abort
3188  * & cleanup transaction. For this purpose, we treat an implicit
3189  * transaction block as if it were a simple statement.
3190  */
3191  case TBLOCK_STARTED:
3193  AbortTransaction();
3196  break;
3197 
3198  /*
3199  * If we are in TBLOCK_BEGIN it means something screwed up right
3200  * after reading "BEGIN TRANSACTION". We assume that the user
3201  * will interpret the error as meaning the BEGIN failed to get him
3202  * into a transaction block, so we should abort and return to idle
3203  * state.
3204  */
3205  case TBLOCK_BEGIN:
3206  AbortTransaction();
3209  break;
3210 
3211  /*
3212  * We are somewhere in a transaction block and we've gotten a
3213  * failure, so we abort the transaction and set up the persistent
3214  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3215  */
3216  case TBLOCK_INPROGRESS:
3218  AbortTransaction();
3219  s->blockState = TBLOCK_ABORT;
3220  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3221  break;
3222 
3223  /*
3224  * Here, we failed while trying to COMMIT. Clean up the
3225  * transaction and return to idle state (we do not want to stay in
3226  * the transaction).
3227  */
3228  case TBLOCK_END:
3229  AbortTransaction();
3232  break;
3233 
3234  /*
3235  * Here, we are already in an aborted transaction state and are
3236  * waiting for a ROLLBACK, but for some reason we failed again! So
3237  * we just remain in the abort state.
3238  */
3239  case TBLOCK_ABORT:
3240  case TBLOCK_SUBABORT:
3241  break;
3242 
3243  /*
3244  * We are in a failed transaction and we got the ROLLBACK command.
3245  * We have already aborted, we just need to cleanup and go to idle
3246  * state.
3247  */
3248  case TBLOCK_ABORT_END:
3251  break;
3252 
3253  /*
3254  * We are in a live transaction and we got a ROLLBACK command.
3255  * Abort, cleanup, go to idle state.
3256  */
3257  case TBLOCK_ABORT_PENDING:
3258  AbortTransaction();
3261  break;
3262 
3263  /*
3264  * Here, we failed while trying to PREPARE. Clean up the
3265  * transaction and return to idle state (we do not want to stay in
3266  * the transaction).
3267  */
3268  case TBLOCK_PREPARE:
3269  AbortTransaction();
3272  break;
3273 
3274  /*
3275  * We got an error inside a subtransaction. Abort just the
3276  * subtransaction, and go to the persistent SUBABORT state until
3277  * we get ROLLBACK.
3278  */
3279  case TBLOCK_SUBINPROGRESS:
3282  break;
3283 
3284  /*
3285  * If we failed while trying to create a subtransaction, clean up
3286  * the broken subtransaction and abort the parent. The same
3287  * applies if we get a failure while ending a subtransaction.
3288  */
3289  case TBLOCK_SUBBEGIN:
3290  case TBLOCK_SUBRELEASE:
3291  case TBLOCK_SUBCOMMIT:
3293  case TBLOCK_SUBRESTART:
3297  break;
3298 
3299  /*
3300  * Same as above, except the Abort() was already done.
3301  */
3302  case TBLOCK_SUBABORT_END:
3306  break;
3307  }
3308 }
3309 
3310 /*
3311  * PreventInTransactionBlock
3312  *
3313  * This routine is to be called by statements that must not run inside
3314  * a transaction block, typically because they have non-rollback-able
3315  * side effects or do internal commits.
3316  *
3317  * If we have already started a transaction block, issue an error; also issue
3318  * an error if we appear to be running inside a user-defined function (which
3319  * could issue more commands and possibly cause a failure after the statement
3320  * completes). Subtransactions are verboten too.
3321  *
3322  * isTopLevel: passed down from ProcessUtility to determine whether we are
3323  * inside a function. (We will always fail if this is false, but it's
3324  * convenient to centralize the check here instead of making callers do it.)
3325  * stmtType: statement type name, for error messages.
3326  */
3327 void
3328 PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
3329 {
3330  /*
3331  * xact block already started?
3332  */
3333  if (IsTransactionBlock())
3334  ereport(ERROR,
3335  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3336  /* translator: %s represents an SQL statement name */
3337  errmsg("%s cannot run inside a transaction block",
3338  stmtType)));
3339 
3340  /*
3341  * subtransaction?
3342  */
3343  if (IsSubTransaction())
3344  ereport(ERROR,
3345  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3346  /* translator: %s represents an SQL statement name */
3347  errmsg("%s cannot run inside a subtransaction",
3348  stmtType)));
3349 
3350  /*
3351  * inside a function call?
3352  */
3353  if (!isTopLevel)
3354  ereport(ERROR,
3355  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3356  /* translator: %s represents an SQL statement name */
3357  errmsg("%s cannot be executed from a function", stmtType)));
3358 
3359  /* If we got past IsTransactionBlock test, should be in default state */
3360  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3361  CurrentTransactionState->blockState != TBLOCK_STARTED)
3362  elog(FATAL, "cannot prevent transaction chain");
3363  /* all okay */
3364 }
3365 
3366 /*
3367  * WarnNoTransactionBlock
3368  * RequireTransactionBlock
3369  *
3370  * These two functions allow for warnings or errors if a command is executed
3371  * outside of a transaction block. This is useful for commands that have no
3372  * effects that persist past transaction end (and so calling them outside a
3373  * transaction block is presumably an error). DECLARE CURSOR is an example.
3374  * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and SET
3375  * that have no effect issue warnings, all other no-effect commands generate
3376  * errors.
3377  *
3378  * If we appear to be running inside a user-defined function, we do not
3379  * issue anything, since the function could issue more commands that make
3380  * use of the current statement's results. Likewise subtransactions.
3381  * Thus these are inverses for PreventInTransactionBlock.
3382  *
3383  * isTopLevel: passed down from ProcessUtility to determine whether we are
3384  * inside a function.
3385  * stmtType: statement type name, for warning or error messages.
3386  */
3387 void
3388 WarnNoTransactionBlock(bool isTopLevel, const char *stmtType)
3389 {
3390  CheckTransactionBlock(isTopLevel, false, stmtType);
3391 }
3392 
3393 void
3394 RequireTransactionBlock(bool isTopLevel, const char *stmtType)
3395 {
3396  CheckTransactionBlock(isTopLevel, true, stmtType);
3397 }
3398 
3399 /*
3400  * This is the implementation of the above two.
3401  */
3402 static void
3403 CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
3404 {
3405  /*
3406  * xact block already started?
3407  */
3408  if (IsTransactionBlock())
3409  return;
3410 
3411  /*
3412  * subtransaction?
3413  */
3414  if (IsSubTransaction())
3415  return;
3416 
3417  /*
3418  * inside a function call?
3419  */
3420  if (!isTopLevel)
3421  return;
3422 
3423  ereport(throwError ? ERROR : WARNING,
3424  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3425  /* translator: %s represents an SQL statement name */
3426  errmsg("%s can only be used in transaction blocks",
3427  stmtType)));
3428  return;
3429 }
3430 
3431 /*
3432  * IsInTransactionBlock
3433  *
3434  * This routine is for statements that need to behave differently inside
3435  * a transaction block than when running as single commands. ANALYZE is
3436  * currently the only example.
3437  *
3438  * isTopLevel: passed down from ProcessUtility to determine whether we are
3439  * inside a function.
3440  */
3441 bool
3442 IsInTransactionBlock(bool isTopLevel)
3443 {
3444  /*
3445  * Return true on same conditions that would make
3446  * PreventInTransactionBlock error out
3447  */
3448  if (IsTransactionBlock())
3449  return true;
3450 
3451  if (IsSubTransaction())
3452  return true;
3453 
3454  if (!isTopLevel)
3455  return true;
3456 
3457  if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3458  CurrentTransactionState->blockState != TBLOCK_STARTED)
3459  return true;
3460 
3461  return false;
3462 }
3463 
3464 
3465 /*
3466  * Register or deregister callback functions for start- and end-of-xact
3467  * operations.
3468  *
3469  * These functions are intended for use by dynamically loaded modules.
3470  * For built-in modules we generally just hardwire the appropriate calls
3471  * (mainly because it's easier to control the order that way, where needed).
3472  *
3473  * At transaction end, the callback occurs post-commit or post-abort, so the
3474  * callback functions can only do noncritical cleanup.
3475  */
3476 void
3478 {
3479  XactCallbackItem *item;
3480 
3481  item = (XactCallbackItem *)
3483  item->callback = callback;
3484  item->arg = arg;
3485  item->next = Xact_callbacks;
3486  Xact_callbacks = item;
3487 }
3488 
3489 void
3491 {
3492  XactCallbackItem *item;
3493  XactCallbackItem *prev;
3494 
3495  prev = NULL;
3496  for (item = Xact_callbacks; item; prev = item, item = item->next)
3497  {
3498  if (item->callback == callback && item->arg == arg)
3499  {
3500  if (prev)
3501  prev->next = item->next;
3502  else
3503  Xact_callbacks = item->next;
3504  pfree(item);
3505  break;
3506  }
3507  }
3508 }
3509 
3510 static void
3512 {
3513  XactCallbackItem *item;
3514 
3515  for (item = Xact_callbacks; item; item = item->next)
3516  item->callback(event, item->arg);
3517 }
3518 
3519 
3520 /*
3521  * Register or deregister callback functions for start- and end-of-subxact
3522  * operations.
3523  *
3524  * Pretty much same as above, but for subtransaction events.
3525  *
3526  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3527  * so the callback functions can only do noncritical cleanup. At
3528  * subtransaction start, the callback is called when the subtransaction has
3529  * finished initializing.
3530  */
3531 void
3533 {
3534  SubXactCallbackItem *item;
3535 
3536  item = (SubXactCallbackItem *)
3538  item->callback = callback;
3539  item->arg = arg;
3540  item->next = SubXact_callbacks;
3541  SubXact_callbacks = item;
3542 }
3543 
3544 void
3546 {
3547  SubXactCallbackItem *item;
3548  SubXactCallbackItem *prev;
3549 
3550  prev = NULL;
3551  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3552  {
3553  if (item->callback == callback && item->arg == arg)
3554  {
3555  if (prev)
3556  prev->next = item->next;
3557  else
3558  SubXact_callbacks = item->next;
3559  pfree(item);
3560  break;
3561  }
3562  }
3563 }
3564 
3565 static void
3567  SubTransactionId mySubid,
3568  SubTransactionId parentSubid)
3569 {
3570  SubXactCallbackItem *item;
3571 
3572  for (item = SubXact_callbacks; item; item = item->next)
3573  item->callback(event, mySubid, parentSubid, item->arg);
3574 }
3575 
3576 
3577 /* ----------------------------------------------------------------
3578  * transaction block support
3579  * ----------------------------------------------------------------
3580  */
3581 
3582 /*
3583  * BeginTransactionBlock
3584  * This executes a BEGIN command.
3585  */
3586 void
3588 {
3590 
3591  switch (s->blockState)
3592  {
3593  /*
3594  * We are not inside a transaction block, so allow one to begin.
3595  */
3596  case TBLOCK_STARTED:
3597  s->blockState = TBLOCK_BEGIN;
3598  break;
3599 
3600  /*
3601  * BEGIN converts an implicit transaction block to a regular one.
3602  * (Note that we allow this even if we've already done some
3603  * commands, which is a bit odd but matches historical practice.)
3604  */
3606  s->blockState = TBLOCK_BEGIN;
3607  break;
3608 
3609  /*
3610  * Already a transaction block in progress.
3611  */
3612  case TBLOCK_INPROGRESS:
3614  case TBLOCK_SUBINPROGRESS:
3615  case TBLOCK_ABORT:
3616  case TBLOCK_SUBABORT:
3617  ereport(WARNING,
3618  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3619  errmsg("there is already a transaction in progress")));
3620  break;
3621 
3622  /* These cases are invalid. */
3623  case TBLOCK_DEFAULT:
3624  case TBLOCK_BEGIN:
3625  case TBLOCK_SUBBEGIN:
3626  case TBLOCK_END:
3627  case TBLOCK_SUBRELEASE:
3628  case TBLOCK_SUBCOMMIT:
3629  case TBLOCK_ABORT_END:
3630  case TBLOCK_SUBABORT_END:
3631  case TBLOCK_ABORT_PENDING:
3633  case TBLOCK_SUBRESTART:
3635  case TBLOCK_PREPARE:
3636  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3638  break;
3639  }
3640 }
3641 
3642 /*
3643  * PrepareTransactionBlock
3644  * This executes a PREPARE command.
3645  *
3646  * Since PREPARE may actually do a ROLLBACK, the result indicates what
3647  * happened: true for PREPARE, false for ROLLBACK.
3648  *
3649  * Note that we don't actually do anything here except change blockState.
3650  * The real work will be done in the upcoming PrepareTransaction().
3651  * We do it this way because it's not convenient to change memory context,
3652  * resource owner, etc while executing inside a Portal.
3653  */
3654 bool
3655 PrepareTransactionBlock(const char *gid)
3656 {
3657  TransactionState s;
3658  bool result;
3659 
3660  /* Set up to commit the current transaction */
3661  result = EndTransactionBlock(false);
3662 
3663  /* If successful, change outer tblock state to PREPARE */
3664  if (result)
3665  {
3667 
3668  while (s->parent != NULL)
3669  s = s->parent;
3670 
3671  if (s->blockState == TBLOCK_END)
3672  {
3673  /* Save GID where PrepareTransaction can find it again */
3675 
3677  }
3678  else
3679  {
3680  /*
3681  * ignore case where we are not in a transaction;
3682  * EndTransactionBlock already issued a warning.
3683  */
3686  /* Don't send back a PREPARE result tag... */
3687  result = false;
3688  }
3689  }
3690 
3691  return result;
3692 }
3693 
3694 /*
3695  * EndTransactionBlock
3696  * This executes a COMMIT command.
3697  *
3698  * Since COMMIT may actually do a ROLLBACK, the result indicates what
3699  * happened: true for COMMIT, false for ROLLBACK.
3700  *
3701  * Note that we don't actually do anything here except change blockState.
3702  * The real work will be done in the upcoming CommitTransactionCommand().
3703  * We do it this way because it's not convenient to change memory context,
3704  * resource owner, etc while executing inside a Portal.
3705  */
3706 bool
3708 {
3710  bool result = false;
3711 
3712  switch (s->blockState)
3713  {
3714  /*
3715  * We are in a transaction block, so tell CommitTransactionCommand
3716  * to COMMIT.
3717  */
3718  case TBLOCK_INPROGRESS:
3719  s->blockState = TBLOCK_END;
3720  result = true;
3721  break;
3722 
3723  /*
3724  * In an implicit transaction block, commit, but issue a warning
3725  * because there was no explicit BEGIN before this.
3726  */
3728  ereport(WARNING,
3729  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3730  errmsg("there is no transaction in progress")));
3731  s->blockState = TBLOCK_END;
3732  result = true;
3733  break;
3734 
3735  /*
3736  * We are in a failed transaction block. Tell
3737  * CommitTransactionCommand it's time to exit the block.
3738  */
3739  case TBLOCK_ABORT:
3741  break;
3742 
3743  /*
3744  * We are in a live subtransaction block. Set up to subcommit all
3745  * open subtransactions and then commit the main transaction.
3746  */
3747  case TBLOCK_SUBINPROGRESS:
3748  while (s->parent != NULL)
3749  {
3750  if (s->blockState == TBLOCK_SUBINPROGRESS)
3752  else
3753  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3755  s = s->parent;
3756  }
3757  if (s->blockState == TBLOCK_INPROGRESS)
3758  s->blockState = TBLOCK_END;
3759  else
3760  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3762  result = true;
3763  break;
3764 
3765  /*
3766  * Here we are inside an aborted subtransaction. Treat the COMMIT
3767  * as ROLLBACK: set up to abort everything and exit the main
3768  * transaction.
3769  */
3770  case TBLOCK_SUBABORT:
3771  while (s->parent != NULL)
3772  {
3773  if (s->blockState == TBLOCK_SUBINPROGRESS)
3775  else if (s->blockState == TBLOCK_SUBABORT)
3777  else
3778  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3780  s = s->parent;
3781  }
3782  if (s->blockState == TBLOCK_INPROGRESS)
3784  else if (s->blockState == TBLOCK_ABORT)
3786  else
3787  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3789  break;
3790 
3791  /*
3792  * The user issued COMMIT when not inside a transaction. Issue a
3793  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3794  * CommitTransactionCommand() will then close the transaction and
3795  * put us back into the default state.
3796  */
3797  case TBLOCK_STARTED:
3798  ereport(WARNING,
3799  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3800  errmsg("there is no transaction in progress")));
3801  result = true;
3802  break;
3803 
3804  /*
3805  * The user issued a COMMIT that somehow ran inside a parallel
3806  * worker. We can't cope with that.
3807  */
3809  ereport(FATAL,
3810  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3811  errmsg("cannot commit during a parallel operation")));
3812  break;
3813 
3814  /* These cases are invalid. */
3815  case TBLOCK_DEFAULT:
3816  case TBLOCK_BEGIN:
3817  case TBLOCK_SUBBEGIN:
3818  case TBLOCK_END:
3819  case TBLOCK_SUBRELEASE:
3820  case TBLOCK_SUBCOMMIT:
3821  case TBLOCK_ABORT_END:
3822  case TBLOCK_SUBABORT_END:
3823  case TBLOCK_ABORT_PENDING:
3825  case TBLOCK_SUBRESTART:
3827  case TBLOCK_PREPARE:
3828  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3830  break;
3831  }
3832 
3834  s->blockState == TBLOCK_END ||
3835  s->blockState == TBLOCK_ABORT_END ||
3837 
3838  s->chain = chain;
3839 
3840  return result;
3841 }
3842 
3843 /*
3844  * UserAbortTransactionBlock
3845  * This executes a ROLLBACK command.
3846  *
3847  * As above, we don't actually do anything here except change blockState.
3848  */
3849 void
3851 {
3853 
3854  switch (s->blockState)
3855  {
3856  /*
3857  * We are inside a transaction block and we got a ROLLBACK command
3858  * from the user, so tell CommitTransactionCommand to abort and
3859  * exit the transaction block.
3860  */
3861  case TBLOCK_INPROGRESS:
3863  break;
3864 
3865  /*
3866  * We are inside a failed transaction block and we got a ROLLBACK
3867  * command from the user. Abort processing is already done, so
3868  * CommitTransactionCommand just has to cleanup and go back to
3869  * idle state.
3870  */
3871  case TBLOCK_ABORT:
3873  break;
3874 
3875  /*
3876  * We are inside a subtransaction. Mark everything up to top
3877  * level as exitable.
3878  */
3879  case TBLOCK_SUBINPROGRESS:
3880  case TBLOCK_SUBABORT:
3881  while (s->parent != NULL)
3882  {
3883  if (s->blockState == TBLOCK_SUBINPROGRESS)
3885  else if (s->blockState == TBLOCK_SUBABORT)
3887  else
3888  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3890  s = s->parent;
3891  }
3892  if (s->blockState == TBLOCK_INPROGRESS)
3894  else if (s->blockState == TBLOCK_ABORT)
3896  else
3897  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3899  break;
3900 
3901  /*
3902  * The user issued ABORT when not inside a transaction. Issue a
3903  * WARNING and go to abort state. The upcoming call to
3904  * CommitTransactionCommand() will then put us back into the
3905  * default state.
3906  *
3907  * We do the same thing with ABORT inside an implicit transaction,
3908  * although in this case we might be rolling back actual database
3909  * state changes. (It's debatable whether we should issue a
3910  * WARNING in this case, but we have done so historically.)
3911  */
3912  case TBLOCK_STARTED:
3914  ereport(WARNING,
3915  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3916  errmsg("there is no transaction in progress")));
3918  break;
3919 
3920  /*
3921  * The user issued an ABORT that somehow ran inside a parallel
3922  * worker. We can't cope with that.
3923  */
3925  ereport(FATAL,
3926  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3927  errmsg("cannot abort during a parallel operation")));
3928  break;
3929 
3930  /* These cases are invalid. */
3931  case TBLOCK_DEFAULT:
3932  case TBLOCK_BEGIN:
3933  case TBLOCK_SUBBEGIN:
3934  case TBLOCK_END:
3935  case TBLOCK_SUBRELEASE:
3936  case TBLOCK_SUBCOMMIT:
3937  case TBLOCK_ABORT_END:
3938  case TBLOCK_SUBABORT_END:
3939  case TBLOCK_ABORT_PENDING:
3941  case TBLOCK_SUBRESTART:
3943  case TBLOCK_PREPARE:
3944  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3946  break;
3947  }
3948 
3951 
3952  s->chain = chain;
3953 }
3954 
3955 /*
3956  * BeginImplicitTransactionBlock
3957  * Start an implicit transaction block if we're not already in one.
3958  *
3959  * Unlike BeginTransactionBlock, this is called directly from the main loop
3960  * in postgres.c, not within a Portal. So we can just change blockState
3961  * without a lot of ceremony. We do not expect caller to do
3962  * CommitTransactionCommand/StartTransactionCommand.
3963  */
3964 void
3966 {
3968 
3969  /*
3970  * If we are in STARTED state (that is, no transaction block is open),
3971  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3972  * block.
3973  *
3974  * For caller convenience, we consider all other transaction states as
3975  * legal here; otherwise the caller would need its own state check, which
3976  * seems rather pointless.
3977  */
3978  if (s->blockState == TBLOCK_STARTED)
3980 }
3981 
3982 /*
3983  * EndImplicitTransactionBlock
3984  * End an implicit transaction block, if we're in one.
3985  *
3986  * Like EndTransactionBlock, we just make any needed blockState change here.
3987  * The real work will be done in the upcoming CommitTransactionCommand().
3988  */
3989 void
3991 {
3993 
3994  /*
3995  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3996  * allowing CommitTransactionCommand to commit whatever happened during
3997  * the implicit transaction block as though it were a single statement.
3998  *
3999  * For caller convenience, we consider all other transaction states as
4000  * legal here; otherwise the caller would need its own state check, which
4001  * seems rather pointless.
4002  */
4005 }
4006 
4007 /*
4008  * DefineSavepoint
4009  * This executes a SAVEPOINT command.
4010  */
4011 void
4012 DefineSavepoint(const char *name)
4013 {
4015 
4016  /*
4017  * Workers synchronize transaction state at the beginning of each parallel
4018  * operation, so we can't account for new subtransactions after that
4019  * point. (Note that this check will certainly error out if s->blockState
4020  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4021  * below.)
4022  */
4023  if (IsInParallelMode())
4024  ereport(ERROR,
4025  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4026  errmsg("cannot define savepoints during a parallel operation")));
4027 
4028  switch (s->blockState)
4029  {
4030  case TBLOCK_INPROGRESS:
4031  case TBLOCK_SUBINPROGRESS:
4032  /* Normal subtransaction start */
4033  PushTransaction();
4034  s = CurrentTransactionState; /* changed by push */
4035 
4036  /*
4037  * Savepoint names, like the TransactionState block itself, live
4038  * in TopTransactionContext.
4039  */
4040  if (name)
4042  break;
4043 
4044  /*
4045  * We disallow savepoint commands in implicit transaction blocks.
4046  * There would be no great difficulty in allowing them so far as
4047  * this module is concerned, but a savepoint seems inconsistent
4048  * with exec_simple_query's behavior of abandoning the whole query
4049  * string upon error. Also, the point of an implicit transaction
4050  * block (as opposed to a regular one) is to automatically close
4051  * after an error, so it's hard to see how a savepoint would fit
4052  * into that.
4053  *
4054  * The error messages for this are phrased as if there were no
4055  * active transaction block at all, which is historical but
4056  * perhaps could be improved.
4057  */
4059  ereport(ERROR,
4060  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4061  /* translator: %s represents an SQL statement name */
4062  errmsg("%s can only be used in transaction blocks",
4063  "SAVEPOINT")));
4064  break;
4065 
4066  /* These cases are invalid. */
4067  case TBLOCK_DEFAULT:
4068  case TBLOCK_STARTED:
4069  case TBLOCK_BEGIN:
4071  case TBLOCK_SUBBEGIN:
4072  case TBLOCK_END:
4073  case TBLOCK_SUBRELEASE:
4074  case TBLOCK_SUBCOMMIT:
4075  case TBLOCK_ABORT:
4076  case TBLOCK_SUBABORT:
4077  case TBLOCK_ABORT_END:
4078  case TBLOCK_SUBABORT_END:
4079  case TBLOCK_ABORT_PENDING:
4081  case TBLOCK_SUBRESTART:
4083  case TBLOCK_PREPARE:
4084  elog(FATAL, "DefineSavepoint: unexpected state %s",
4086  break;
4087  }
4088 }
4089 
4090 /*
4091  * ReleaseSavepoint
4092  * This executes a RELEASE command.
4093  *
4094  * As above, we don't actually do anything here except change blockState.
4095  */
4096 void
4098 {
4100  TransactionState target,
4101  xact;
4102 
4103  /*
4104  * Workers synchronize transaction state at the beginning of each parallel
4105  * operation, so we can't account for transaction state change after that
4106  * point. (Note that this check will certainly error out if s->blockState
4107  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4108  * below.)
4109  */
4110  if (IsInParallelMode())
4111  ereport(ERROR,
4112  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4113  errmsg("cannot release savepoints during a parallel operation")));
4114 
4115  switch (s->blockState)
4116  {
4117  /*
4118  * We can't release a savepoint if there is no savepoint defined.
4119  */
4120  case TBLOCK_INPROGRESS:
4121  ereport(ERROR,
4122  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4123  errmsg("savepoint \"%s\" does not exist", name)));
4124  break;
4125 
4127  /* See comment about implicit transactions in DefineSavepoint */
4128  ereport(ERROR,
4129  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4130  /* translator: %s represents an SQL statement name */
4131  errmsg("%s can only be used in transaction blocks",
4132  "RELEASE SAVEPOINT")));
4133  break;
4134 
4135  /*
4136  * We are in a non-aborted subtransaction. This is the only valid
4137  * case.
4138  */
4139  case TBLOCK_SUBINPROGRESS:
4140  break;
4141 
4142  /* These cases are invalid. */
4143  case TBLOCK_DEFAULT:
4144  case TBLOCK_STARTED:
4145  case TBLOCK_BEGIN:
4147  case TBLOCK_SUBBEGIN:
4148  case TBLOCK_END:
4149  case TBLOCK_SUBRELEASE:
4150  case TBLOCK_SUBCOMMIT:
4151  case TBLOCK_ABORT:
4152  case TBLOCK_SUBABORT:
4153  case TBLOCK_ABORT_END:
4154  case TBLOCK_SUBABORT_END:
4155  case TBLOCK_ABORT_PENDING:
4157  case TBLOCK_SUBRESTART:
4159  case TBLOCK_PREPARE:
4160  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4162  break;
4163  }
4164 
4165  for (target = s; PointerIsValid(target); target = target->parent)
4166  {
4167  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4168  break;
4169  }
4170 
4171  if (!PointerIsValid(target))
4172  ereport(ERROR,
4173  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4174  errmsg("savepoint \"%s\" does not exist", name)));
4175 
4176  /* disallow crossing savepoint level boundaries */
4177  if (target->savepointLevel != s->savepointLevel)
4178  ereport(ERROR,
4179  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4180  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4181 
4182  /*
4183  * Mark "commit pending" all subtransactions up to the target
4184  * subtransaction. The actual commits will happen when control gets to
4185  * CommitTransactionCommand.
4186  */
4187  xact = CurrentTransactionState;
4188  for (;;)
4189  {
4191  xact->blockState = TBLOCK_SUBRELEASE;
4192  if (xact == target)
4193  break;
4194  xact = xact->parent;
4195  Assert(PointerIsValid(xact));
4196  }
4197 }
4198 
4199 /*
4200  * RollbackToSavepoint
4201  * This executes a ROLLBACK TO <savepoint> command.
4202  *
4203  * As above, we don't actually do anything here except change blockState.
4204  */
4205 void
4207 {
4209  TransactionState target,
4210  xact;
4211 
4212  /*
4213  * Workers synchronize transaction state at the beginning of each parallel
4214  * operation, so we can't account for transaction state change after that
4215  * point. (Note that this check will certainly error out if s->blockState
4216  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4217  * below.)
4218  */
4219  if (IsInParallelMode())
4220  ereport(ERROR,
4221  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4222  errmsg("cannot rollback to savepoints during a parallel operation")));
4223 
4224  switch (s->blockState)
4225  {
4226  /*
4227  * We can't rollback to a savepoint if there is no savepoint
4228  * defined.
4229  */
4230  case TBLOCK_INPROGRESS:
4231  case TBLOCK_ABORT:
4232  ereport(ERROR,
4233  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4234  errmsg("savepoint \"%s\" does not exist", name)));
4235  break;
4236 
4238  /* See comment about implicit transactions in DefineSavepoint */
4239  ereport(ERROR,
4240  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4241  /* translator: %s represents an SQL statement name */
4242  errmsg("%s can only be used in transaction blocks",
4243  "ROLLBACK TO SAVEPOINT")));
4244  break;
4245 
4246  /*
4247  * There is at least one savepoint, so proceed.
4248  */
4249  case TBLOCK_SUBINPROGRESS:
4250  case TBLOCK_SUBABORT:
4251  break;
4252 
4253  /* These cases are invalid. */
4254  case TBLOCK_DEFAULT:
4255  case TBLOCK_STARTED:
4256  case TBLOCK_BEGIN:
4258  case TBLOCK_SUBBEGIN:
4259  case TBLOCK_END:
4260  case TBLOCK_SUBRELEASE:
4261  case TBLOCK_SUBCOMMIT:
4262  case TBLOCK_ABORT_END:
4263  case TBLOCK_SUBABORT_END:
4264  case TBLOCK_ABORT_PENDING:
4266  case TBLOCK_SUBRESTART:
4268  case TBLOCK_PREPARE:
4269  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4271  break;
4272  }
4273 
4274  for (target = s; PointerIsValid(target); target = target->parent)
4275  {
4276  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4277  break;
4278  }
4279 
4280  if (!PointerIsValid(target))
4281  ereport(ERROR,
4282  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4283  errmsg("savepoint \"%s\" does not exist", name)));
4284 
4285  /* disallow crossing savepoint level boundaries */
4286  if (target->savepointLevel != s->savepointLevel)
4287  ereport(ERROR,
4288  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4289  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4290 
4291  /*
4292  * Mark "abort pending" all subtransactions up to the target
4293  * subtransaction. The actual aborts will happen when control gets to
4294  * CommitTransactionCommand.
4295  */
4296  xact = CurrentTransactionState;
4297  for (;;)
4298  {
4299  if (xact == target)
4300  break;
4301  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4303  else if (xact->blockState == TBLOCK_SUBABORT)
4305  else
4306  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4308  xact = xact->parent;
4309  Assert(PointerIsValid(xact));
4310  }
4311 
4312  /* And mark the target as "restart pending" */
4313  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4314  xact->blockState = TBLOCK_SUBRESTART;
4315  else if (xact->blockState == TBLOCK_SUBABORT)
4317  else
4318  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4320 }
4321 
4322 /*
4323  * BeginInternalSubTransaction
4324  * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4325  * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_END, and TBLOCK_PREPARE states,
4326  * and therefore it can safely be used in functions that might be called
4327  * when not inside a BEGIN block or when running deferred triggers at
4328  * COMMIT/PREPARE time. Also, it automatically does
4329  * CommitTransactionCommand/StartTransactionCommand instead of expecting
4330  * the caller to do it.
4331  */
4332 void
4334 {
4336 
4337  /*
4338  * Workers synchronize transaction state at the beginning of each parallel
4339  * operation, so we can't account for new subtransactions after that
4340  * point. We might be able to make an exception for the type of
4341  * subtransaction established by this function, which is typically used in
4342  * contexts where we're going to release or roll back the subtransaction
4343  * before proceeding further, so that no enduring change to the
4344  * transaction state occurs. For now, however, we prohibit this case along
4345  * with all the others.
4346  */
4347  if (IsInParallelMode())
4348  ereport(ERROR,
4349  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4350  errmsg("cannot start subtransactions during a parallel operation")));
4351 
4352  switch (s->blockState)
4353  {
4354  case TBLOCK_STARTED:
4355  case TBLOCK_INPROGRESS:
4357  case TBLOCK_END:
4358  case TBLOCK_PREPARE:
4359  case TBLOCK_SUBINPROGRESS:
4360  /* Normal subtransaction start */
4361  PushTransaction();
4362  s = CurrentTransactionState; /* changed by push */
4363 
4364  /*
4365  * Savepoint names, like the TransactionState block itself, live
4366  * in TopTransactionContext.
4367  */
4368  if (name)
4370  break;
4371 
4372  /* These cases are invalid. */
4373  case TBLOCK_DEFAULT:
4374  case TBLOCK_BEGIN:
4376  case TBLOCK_SUBBEGIN:
4377  case TBLOCK_SUBRELEASE:
4378  case TBLOCK_SUBCOMMIT:
4379  case TBLOCK_ABORT:
4380  case TBLOCK_SUBABORT:
4381  case TBLOCK_ABORT_END:
4382  case TBLOCK_SUBABORT_END:
4383  case TBLOCK_ABORT_PENDING:
4385  case TBLOCK_SUBRESTART:
4387  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4389  break;
4390  }
4391 
4394 }
4395 
4396 /*
4397  * ReleaseCurrentSubTransaction
4398  *
4399  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4400  * savepoint name (if any).
4401  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4402  */
4403 void
4405 {
4407 
4408  /*
4409  * Workers synchronize transaction state at the beginning of each parallel
4410  * operation, so we can't account for commit of subtransactions after that
4411  * point. This should not happen anyway. Code calling this would
4412  * typically have called BeginInternalSubTransaction() first, failing
4413  * there.
4414  */
4415  if (IsInParallelMode())
4416  ereport(ERROR,
4417  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4418  errmsg("cannot commit subtransactions during a parallel operation")));
4419 
4420  if (s->blockState != TBLOCK_SUBINPROGRESS)
4421  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4423  Assert(s->state == TRANS_INPROGRESS);
4426  s = CurrentTransactionState; /* changed by pop */
4427  Assert(s->state == TRANS_INPROGRESS);
4428 }
4429 
4430 /*
4431  * RollbackAndReleaseCurrentSubTransaction
4432  *
4433  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4434  * of its savepoint name (if any).
4435  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4436  */
4437 void
4439 {
4441 
4442  /*
4443  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4444  * during parallel operations. That's because we may be in the master,
4445  * recovering from an error thrown while we were in parallel mode. We
4446  * won't reach here in a worker, because BeginInternalSubTransaction()
4447  * will have failed.
4448  */
4449 
4450  switch (s->blockState)
4451  {
4452  /* Must be in a subtransaction */
4453  case TBLOCK_SUBINPROGRESS:
4454  case TBLOCK_SUBABORT:
4455  break;
4456 
4457  /* These cases are invalid. */
4458  case TBLOCK_DEFAULT:
4459  case TBLOCK_STARTED:
4460  case TBLOCK_BEGIN:
4463  case TBLOCK_SUBBEGIN:
4464  case TBLOCK_INPROGRESS:
4465  case TBLOCK_END:
4466  case TBLOCK_SUBRELEASE:
4467  case TBLOCK_SUBCOMMIT:
4468  case TBLOCK_ABORT:
4469  case TBLOCK_ABORT_END:
4470  case TBLOCK_SUBABORT_END:
4471  case TBLOCK_ABORT_PENDING:
4473  case TBLOCK_SUBRESTART:
4475  case TBLOCK_PREPARE:
4476  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4478  break;
4479  }
4480 
4481  /*
4482  * Abort the current subtransaction, if needed.
4483  */
4484  if (s->blockState == TBLOCK_SUBINPROGRESS)
4486 
4487  /* And clean it up, too */
4489 
4490  s = CurrentTransactionState; /* changed by pop */
4492  s->blockState == TBLOCK_INPROGRESS ||
4494  s->blockState == TBLOCK_STARTED);
4495 }
4496 
4497 /*
4498  * AbortOutOfAnyTransaction
4499  *
4500  * This routine is provided for error recovery purposes. It aborts any
4501  * active transaction or transaction block, leaving the system in a known
4502  * idle state.
4503  */
4504 void
4506 {
4508 
4509  /* Ensure we're not running in a doomed memory context */
4510  AtAbort_Memory();
4511 
4512  /*
4513  * Get out of any transaction or nested transaction
4514  */
4515  do
4516  {
4517  switch (s->blockState)
4518  {
4519  case TBLOCK_DEFAULT:
4520  if (s->state == TRANS_DEFAULT)
4521  {
4522  /* Not in a transaction, do nothing */
4523  }
4524  else
4525  {
4526  /*
4527  * We can get here after an error during transaction start
4528  * (state will be TRANS_START). Need to clean up the
4529  * incompletely started transaction. First, adjust the
4530  * low-level state to suppress warning message from
4531  * AbortTransaction.
4532  */
4533  if (s->state == TRANS_START)
4534  s->state = TRANS_INPROGRESS;
4535  AbortTransaction();
4537  }
4538  break;
4539  case TBLOCK_STARTED:
4540  case TBLOCK_BEGIN:
4541  case TBLOCK_INPROGRESS:
4544  case TBLOCK_END:
4545  case TBLOCK_ABORT_PENDING:
4546  case TBLOCK_PREPARE:
4547  /* In a transaction, so clean up */
4548  AbortTransaction();
4551  break;
4552  case TBLOCK_ABORT:
4553  case TBLOCK_ABORT_END:
4554 
4555  /*
4556  * AbortTransaction is already done, still need Cleanup.
4557  * However, if we failed partway through running ROLLBACK,
4558  * there will be an active portal running that command, which
4559  * we need to shut down before doing CleanupTransaction.
4560  */
4561  AtAbort_Portals();
4564  break;
4565 
4566  /*
4567  * In a subtransaction, so clean it up and abort parent too
4568  */
4569  case TBLOCK_SUBBEGIN:
4570  case TBLOCK_SUBINPROGRESS:
4571  case TBLOCK_SUBRELEASE:
4572  case TBLOCK_SUBCOMMIT:
4574  case TBLOCK_SUBRESTART:
4577  s = CurrentTransactionState; /* changed by pop */
4578  break;
4579 
4580  case TBLOCK_SUBABORT:
4581  case TBLOCK_SUBABORT_END:
4583  /* As above, but AbortSubTransaction already done */
4584  if (s->curTransactionOwner)
4585  {
4586  /* As in TBLOCK_ABORT, might have a live portal to zap */
4591  }
4593  s = CurrentTransactionState; /* changed by pop */
4594  break;
4595  }
4596  } while (s->blockState != TBLOCK_DEFAULT);
4597 
4598  /* Should be out of all subxacts now */
4599  Assert(s->parent == NULL);
4600 
4601  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4602  AtCleanup_Memory();
4603 }
4604 
4605 /*
4606  * IsTransactionBlock --- are we within a transaction block?
4607  */
4608 bool
4610 {
4612 
4614  return false;
4615 
4616  return true;
4617 }
4618 
4619 /*
4620  * IsTransactionOrTransactionBlock --- are we within either a transaction
4621  * or a transaction block? (The backend is only really "idle" when this
4622  * returns false.)
4623  *
4624  * This should match up with IsTransactionBlock and IsTransactionState.
4625  */
4626 bool
4628 {
4630 
4631  if (s->blockState == TBLOCK_DEFAULT)
4632  return false;
4633 
4634  return true;
4635 }
4636 
4637 /*
4638  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
4639  */
4640 char
4642 {
4644 
4645  switch (s->blockState)
4646  {
4647  case TBLOCK_DEFAULT:
4648  case TBLOCK_STARTED:
4649  return 'I'; /* idle --- not in transaction */
4650  case TBLOCK_BEGIN:
4651  case TBLOCK_SUBBEGIN:
4652  case TBLOCK_INPROGRESS:
4655  case TBLOCK_SUBINPROGRESS:
4656  case TBLOCK_END:
4657  case TBLOCK_SUBRELEASE:
4658  case TBLOCK_SUBCOMMIT:
4659  case TBLOCK_PREPARE:
4660  return 'T'; /* in transaction */
4661  case TBLOCK_ABORT:
4662  case TBLOCK_SUBABORT:
4663  case TBLOCK_ABORT_END:
4664  case TBLOCK_SUBABORT_END:
4665  case TBLOCK_ABORT_PENDING:
4667  case TBLOCK_SUBRESTART:
4669  return 'E'; /* in failed transaction */
4670  }
4671 
4672  /* should never get here */
4673  elog(FATAL, "invalid transaction block state: %s",
4675  return 0; /* keep compiler quiet */
4676 }
4677 
4678 /*
4679  * IsSubTransaction
4680  */
4681 bool
4683 {
4685 
4686  if (s->nestingLevel >= 2)
4687  return true;
4688 
4689  return false;
4690 }
4691 
4692 /*
4693  * StartSubTransaction
4694  *
4695  * If you're wondering why this is separate from PushTransaction: it's because
4696  * we can't conveniently do this stuff right inside DefineSavepoint. The
4697  * SAVEPOINT utility command will be executed inside a Portal, and if we
4698  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
4699  * the Portal will undo those settings. So we make DefineSavepoint just
4700  * push a dummy transaction block, and when control returns to the main
4701  * idle loop, CommitTransactionCommand will be called, and we'll come here
4702  * to finish starting the subtransaction.
4703  */
4704 static void
4706 {
4708 
4709  if (s->state != TRANS_DEFAULT)
4710  elog(WARNING, "StartSubTransaction while in %s state",
4712 
4713  s->state = TRANS_START;
4714 
4715  /*
4716  * Initialize subsystems for new subtransaction
4717  *
4718  * must initialize resource-management stuff first
4719  */
4724 
4725  s->state = TRANS_INPROGRESS;
4726 
4727  /*
4728  * Call start-of-subxact callbacks
4729  */
4731  s->parent->subTransactionId);
4732 
4733  ShowTransactionState("StartSubTransaction");
4734 }
4735 
4736 /*
4737  * CommitSubTransaction
4738  *
4739  * The caller has to make sure to always reassign CurrentTransactionState
4740  * if it has a local pointer to it after calling this function.
4741  */
4742 static void
4744 {
4746 
4747  ShowTransactionState("CommitSubTransaction");
4748 
4749  if (s->state != TRANS_INPROGRESS)
4750  elog(WARNING, "CommitSubTransaction while in %s state",
4752 
4753  /* Pre-commit processing goes here */
4754 
4756  s->parent->subTransactionId);
4757 
4758  /* If in parallel mode, clean up workers and exit parallel mode. */
4759  if (IsInParallelMode())
4760  {
4762  s->parallelModeLevel = 0;
4763  }
4764 
4765  /* Do the actual "commit", such as it is */
4766  s->state = TRANS_COMMIT;
4767 
4768  /* Must CCI to ensure commands of subtransaction are seen as done */
4770 
4771  /*
4772  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4773  * perform that step, if required, as part of the atomic update of the
4774  * whole transaction tree at top level commit or abort.
4775  */
4776 
4777  /* Post-commit cleanup */
4780  AfterTriggerEndSubXact(true);
4785  s->parent->subTransactionId);
4787 
4789  s->parent->subTransactionId);
4790 
4793  true, false);
4795  s->parent->subTransactionId);
4796  AtEOSubXact_Inval(true);
4797  AtSubCommit_smgr();
4798 
4799  /*
4800  * The only lock we actually release here is the subtransaction XID lock.
4801  */
4805 
4806  /*
4807  * Other locks should get transferred to their parent resource owner.
4808  */
4811  true, false);
4814  true, false);
4815 
4816  AtEOXact_GUC(true, s->gucNestLevel);
4819  s->parent->subTransactionId);
4821  s->parent->subTransactionId);
4823  s->parent->subTransactionId);
4825  AtEOSubXact_PgStat(true, s->nestingLevel);
4828 
4829  /*
4830  * We need to restore the upper transaction's read-only state, in case the
4831  * upper is read-write while the child is read-only; GUC will incorrectly
4832  * think it should leave the child state in place.
4833  */
4835 
4839  s->curTransactionOwner = NULL;
4840 
4842 
4843  s->state = TRANS_DEFAULT;
4844 
4845  PopTransaction();
4846 }
4847 
4848 /*
4849  * AbortSubTransaction
4850  */
4851 static void
4853 {
4855 
4856  /* Prevent cancel/die interrupt while cleaning up */
4857  HOLD_INTERRUPTS();
4858 
4859  /* Make sure we have a valid memory context and resource owner */
4862 
4863  /*
4864  * Release any LW locks we might be holding as quickly as possible.
4865  * (Regular locks, however, must be held till we finish aborting.)
4866  * Releasing LW locks is critical since we might try to grab them again
4867  * while cleaning up!
4868  *
4869  * FIXME This may be incorrect --- Are there some locks we should keep?
4870  * Buffer locks, for example? I don't think so but I'm not sure.
4871  */
4872  LWLockReleaseAll();
4873 
4876  AbortBufferIO();
4877  UnlockBuffers();
4878 
4879  /* Reset WAL record construction state */
4881 
4882  /* Cancel condition variable sleep */
4884 
4885  /*
4886  * Also clean up any open wait for lock, since the lock manager will choke
4887  * if we try to wait for another lock before doing this.
4888  */
4889  LockErrorCleanup();
4890 
4891  /*
4892  * If any timeout events are still active, make sure the timeout interrupt
4893  * is scheduled. This covers possible loss of a timeout interrupt due to
4894  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4895  * We delay this till after LockErrorCleanup so that we don't uselessly
4896  * reschedule lock or deadlock check timeouts.
4897  */
4899 
4900  /*
4901  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4902  * handler. We do this fairly early in the sequence so that the timeout
4903  * infrastructure will be functional if needed while aborting.
4904  */
4906 
4907  /*
4908  * check the current transaction state
4909  */
4910  ShowTransactionState("AbortSubTransaction");
4911 
4912  if (s->state != TRANS_INPROGRESS)
4913  elog(WARNING, "AbortSubTransaction while in %s state",
4915 
4916  s->state = TRANS_ABORT;
4917 
4918  /*
4919  * Reset user ID which might have been changed transiently. (See notes in
4920  * AbortTransaction.)
4921  */
4923 
4924  /* Exit from parallel mode, if necessary. */
4925  if (IsInParallelMode())
4926  {
4928  s->parallelModeLevel = 0;
4929  }
4930 
4931  /*
4932  * We can skip all this stuff if the subxact failed before creating a
4933  * ResourceOwner...
4934  */
4935  if (s->curTransactionOwner)
4936  {
4937  AfterTriggerEndSubXact(false);
4943  s->parent->subTransactionId);
4945 
4946  /* Advertise the fact that we aborted in pg_xact. */
4947  (void) RecordTransactionAbort(true);
4948 
4949  /* Post-abort cleanup */
4952 
4954  s->parent->subTransactionId);
4955 
4958  false, false);
4960  s->parent->subTransactionId);
4961  AtEOSubXact_Inval(false);
4964  false, false);
4967  false, false);
4968  AtSubAbort_smgr();
4969 
4970  AtEOXact_GUC(false, s->gucNestLevel);
4971  AtEOSubXact_SPI(false, s->subTransactionId);
4973  s->parent->subTransactionId);
4975  s->parent->subTransactionId);
4977  s->parent->subTransactionId);
4979  AtEOSubXact_PgStat(false, s->nestingLevel);
4982  }
4983 
4984  /*
4985  * Restore the upper transaction's read-only state, too. This should be
4986  * redundant with GUC's cleanup but we may as well do it for consistency
4987  * with the commit case.
4988  */
4990 
4992 }
4993 
4994 /*
4995  * CleanupSubTransaction
4996  *
4997  * The caller has to make sure to always reassign CurrentTransactionState
4998  * if it has a local pointer to it after calling this function.
4999  */
5000 static void
5002 {
5004 
5005  ShowTransactionState("CleanupSubTransaction");
5006 
5007  if (s->state != TRANS_ABORT)
5008  elog(WARNING, "CleanupSubTransaction while in %s state",
5010 
5012 
5015  if (s->curTransactionOwner)
5017  s->curTransactionOwner = NULL;
5018 
5020 
5021  s->state = TRANS_DEFAULT;
5022 
5023  PopTransaction();
5024 }
5025 
5026 /*
5027  * PushTransaction
5028  * Create transaction state stack entry for a subtransaction
5029  *
5030  * The caller has to make sure to always reassign CurrentTransactionState
5031  * if it has a local pointer to it after calling this function.
5032  */
5033 static void
5035 {
5037  TransactionState s;
5038 
5039  /*
5040  * We keep subtransaction state nodes in TopTransactionContext.
5041  */
5042  s = (TransactionState)
5044  sizeof(TransactionStateData));
5045 
5046  /*
5047  * Assign a subtransaction ID, watching out for counter wraparound.
5048  */
5051  {
5053  pfree(s);
5054  ereport(ERROR,
5055  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5056  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
5057  }
5058 
5059  /*
5060  * We can now stack a minimally valid subtransaction without fear of
5061  * failure.
5062  */
5063  s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
5065  s->parent = p;
5066  s->nestingLevel = p->nestingLevel + 1;
5069  s->state = TRANS_DEFAULT;
5073  s->parallelModeLevel = 0;
5074 
5075  CurrentTransactionState = s;
5076 
5077  /*
5078  * AbortSubTransaction and CleanupSubTransaction have to be able to cope
5079  * with the subtransaction from here on out; in particular they should not
5080  * assume that it necessarily has a transaction context, resource owner,
5081  * or XID.
5082  */
5083 }
5084 
5085 /*
5086  * PopTransaction
5087  * Pop back to parent transaction state
5088  *
5089  * The caller has to make sure to always reassign CurrentTransactionState
5090  * if it has a local pointer to it after calling this function.
5091  */
5092 static void
5094 {
5096 
5097  if (s->state != TRANS_DEFAULT)
5098  elog(WARNING, "PopTransaction while in %s state",
5100 
5101  if (s->parent == NULL)
5102  elog(FATAL, "PopTransaction with no parent");
5103 
5104  CurrentTransactionState = s->parent;
5105 
5106  /* Let's just make sure CurTransactionContext is good */
5109 
5110  /* Ditto for ResourceOwner links */
5113 
5114  /* Free the old child structure */
5115  if (s->name)
5116  pfree(s->name);
5117  pfree(s);
5118 }
5119 
5120 /*
5121  * EstimateTransactionStateSpace
5122  * Estimate the amount of space that will be needed by
5123  * SerializeTransactionState. It would be OK to overestimate slightly,
5124  * but it's simple for us to work out the precise value, so we do.
5125  */
5126 Size
5128 {
5129  TransactionState s;
5130  Size nxids = 0;
5132 
5133  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5134  {
5136  nxids = add_size(nxids, 1);
5137  nxids = add_size(nxids, s->nChildXids);
5138  }
5139 
5140  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5141 }
5142 
5143 /*
5144  * SerializeTransactionState
5145  * Write out relevant details of our transaction state that will be
5146  * needed by a parallel worker.
5147  *
5148  * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
5149  * associated with this transaction. These are serialized into a
5150  * caller-supplied buffer big enough to hold the number of bytes reported by
5151  * EstimateTransactionStateSpace(). We emit the XIDs in sorted order for the
5152  * convenience of the receiving process.
5153  */
5154 void
5155 SerializeTransactionState(Size maxsize, char *start_address)
5156 {
5157  TransactionState s;
5158  Size nxids = 0;
5159  Size i = 0;
5160  TransactionId *workspace;
5162 
5163  result = (SerializedTransactionState *) start_address;
5164 
5165  result->xactIsoLevel = XactIsoLevel;
5166  result->xactDeferrable = XactDeferrable;
5168  result->currentFullTransactionId =
5169  CurrentTransactionState->fullTransactionId;
5171 
5172  /*
5173  * If we're running in a parallel worker and launching a parallel worker
5174  * of our own, we can just pass along the information that was passed to
5175  * us.
5176  */
5177  if (nParallelCurrentXids > 0)
5178  {
5180  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5182  return;
5183  }
5184 
5185  /*
5186  * OK, we need to generate a sorted list of XIDs that our workers should
5187  * view as current. First, figure out how many there are.
5188  */
5189  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5190  {
5192  nxids = add_size(nxids, 1);
5193  nxids = add_size(nxids, s->nChildXids);
5194  }
5196  <= maxsize);
5197 
5198  /* Copy them to our scratch space. */
5199  workspace = palloc(nxids * sizeof(TransactionId));
5200  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5201  {
5203  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5204  memcpy(&workspace[i], s->childXids,
5205  s->nChildXids * sizeof(TransactionId));
5206  i += s->nChildXids;
5207  }
5208  Assert(i == nxids);
5209 
5210  /* Sort them. */
5211  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5212 
5213  /* Copy data into output area. */
5214  result->nParallelCurrentXids = nxids;
5215  memcpy(&result->parallelCurrentXids[0], workspace,
5216  nxids * sizeof(TransactionId));
5217 }
5218 
5219 /*
5220  * StartParallelWorkerTransaction
5221  * Start a parallel worker transaction, restoring the relevant
5222  * transaction state serialized by SerializeTransactionState.
5223  */
5224 void
5226 {
5228 
5229  Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
5230  StartTransaction();
5231 
5232  tstate = (SerializedTransactionState *) tstatespace;
5233  XactIsoLevel = tstate->xactIsoLevel;
5234  XactDeferrable = tstate->xactDeferrable;
5235  XactTopFullTransactionId = tstate->topFullTransactionId;
5236  CurrentTransactionState->fullTransactionId =
5237  tstate->currentFullTransactionId;
5241 
5242  CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
5243 }
5244 
5245 /*
5246  * EndParallelWorkerTransaction
5247  * End a parallel worker transaction.
5248  */
5249 void
5251 {
5252  Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
5254  CurrentTransactionState->blockState = TBLOCK_DEFAULT;
5255 }
5256 
5257 /*
5258  * ShowTransactionState
5259  * Debug support
5260  */
5261 static void
5263 {
5264  /* skip work if message will definitely not be printed */
5266  ShowTransactionStateRec(str, CurrentTransactionState);
5267 }
5268 
5269 /*
5270  * ShowTransactionStateRec
5271  * Recursive subroutine for ShowTransactionState
5272  */
5273 static void
5275 {
5277 
5278  initStringInfo(&buf);
5279 
5280  if (s->nChildXids > 0)
5281  {
5282  int i;
5283 
5284  appendStringInfo(&buf, ", children: %u", s->childXids[0]);
5285  for (i = 1; i < s->nChildXids; i++)
5286  appendStringInfo(&buf, " %u", s->childXids[i]);
5287  }
5288 
5289  if (s->parent)
5291 
5292  /* use ereport to suppress computation if msg will not be printed */
5293  ereport(DEBUG5,
5294  (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
5295  str, s->nestingLevel,
5296  PointerIsValid(s->name) ? s->name : "unnamed",
5299  (unsigned int) XidFromFullTransactionId(s->fullTransactionId),
5300  (unsigned int) s->subTransactionId,
5301  (unsigned int) currentCommandId,
5302  currentCommandIdUsed ? " (used)" : "",
5303  buf.data)));
5304 
5305  pfree(buf.data);
5306 }
5307 
5308 /*
5309  * BlockStateAsString
5310  * Debug support
5311  */
5312 static const char *
5314 {
5315  switch (blockState)
5316  {
5317  case TBLOCK_DEFAULT:
5318  return "DEFAULT";
5319  case TBLOCK_STARTED:
5320  return "STARTED";
5321  case TBLOCK_BEGIN:
5322  return "BEGIN";
5323  case TBLOCK_INPROGRESS:
5324  return "INPROGRESS";
5326  return "IMPLICIT_INPROGRESS";
5328  return "PARALLEL_INPROGRESS";
5329  case TBLOCK_END:
5330  return "END";
5331  case TBLOCK_ABORT:
5332  return "ABORT";
5333  case TBLOCK_ABORT_END:
5334  return "ABORT_END";
5335  case TBLOCK_ABORT_PENDING:
5336  return "ABORT_PENDING";
5337  case TBLOCK_PREPARE:
5338  return "PREPARE";
5339  case TBLOCK_SUBBEGIN:
5340  return "SUBBEGIN";
5341  case TBLOCK_SUBINPROGRESS:
5342  return "SUBINPROGRESS";
5343  case TBLOCK_SUBRELEASE:
5344  return "SUBRELEASE";
5345  case TBLOCK_SUBCOMMIT:
5346  return "SUBCOMMIT";
5347  case TBLOCK_SUBABORT:
5348  return "SUBABORT";
5349  case TBLOCK_SUBABORT_END:
5350  return "SUBABORT_END";
5352  return "SUBABORT_PENDING";
5353  case TBLOCK_SUBRESTART:
5354  return "SUBRESTART";
5356  return "SUBABORT_RESTART";
5357  }
5358  return "UNRECOGNIZED";
5359 }
5360 
5361 /*
5362  * TransStateAsString
5363  * Debug support
5364  */
5365 static const char *
5367 {
5368  switch (state)
5369  {
5370  case TRANS_DEFAULT:
5371  return "DEFAULT";
5372  case TRANS_START:
5373  return "START";
5374  case TRANS_INPROGRESS:
5375  return "INPROGRESS";
5376  case TRANS_COMMIT:
5377  return "COMMIT";
5378  case TRANS_ABORT:
5379  return "ABORT";
5380  case TRANS_PREPARE:
5381  return "PREPARE";
5382  }
5383  return "UNRECOGNIZED";
5384 }
5385 
5386 /*
5387  * xactGetCommittedChildren
5388  *
5389  * Gets the list of committed children of the current transaction. The return
5390  * value is the number of child transactions. *ptr is set to point to an
5391  * array of TransactionIds. The array is allocated in TopTransactionContext;
5392  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
5393  * If there are no subxacts, *ptr is set to NULL.
5394  */
5395 int
5397 {
5399 
5400  if (s->nChildXids == 0)
5401  *ptr = NULL;
5402  else
5403  *ptr = s->childXids;
5404 
5405  return s->nChildXids;
5406 }
5407 
5408 /*
5409  * XLOG support routines
5410  */
5411 
5412 
5413 /*
5414  * Log the commit record for a plain or twophase transaction commit.
5415  *
5416  * A 2pc commit will be emitted when twophase_xid is valid, a plain one
5417  * otherwise.
5418  */
5419 XLogRecPtr
5421  int nsubxacts, TransactionId *subxacts,
5422  int nrels, RelFileNode *rels,
5423  int nmsgs, SharedInvalidationMessage *msgs,
5424  bool relcacheInval, bool forceSync,
5425  int xactflags, TransactionId twophase_xid,
5426  const char *twophase_gid)
5427 {
5428  xl_xact_commit xlrec;
5429  xl_xact_xinfo xl_xinfo;
5430  xl_xact_dbinfo xl_dbinfo;
5431  xl_xact_subxacts xl_subxacts;
5432  xl_xact_relfilenodes xl_relfilenodes;
5433  xl_xact_invals xl_invals;
5434  xl_xact_twophase xl_twophase;
5435  xl_xact_origin xl_origin;
5436  uint8 info;
5437 
5438  Assert(CritSectionCount > 0);
5439 
5440  xl_xinfo.xinfo = 0;
5441 
5442  /* decide between a plain and 2pc commit */
5443  if (!TransactionIdIsValid(twophase_xid))
5444  info = XLOG_XACT_COMMIT;
5445  else
5447 
5448  /* First figure out and collect all the information needed */
5449 
5450  xlrec.xact_time = commit_time;
5451 
5452  if (relcacheInval)
5454  if (forceSyncCommit)
5456  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5457  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5458 
5459  /*
5460  * Check if the caller would like to ask standbys for immediate feedback
5461  * once this commit is applied.
5462  */
5465 
5466  /*
5467  * Relcache invalidations requires information about the current database
5468  * and so does logical decoding.
5469  */
5470  if (nmsgs > 0 || XLogLogicalInfoActive())
5471  {
5472  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5473  xl_dbinfo.dbId = MyDatabaseId;
5474  xl_dbinfo.tsId = MyDatabaseTableSpace;
5475  }
5476 
5477  if (nsubxacts > 0)
5478  {
5479  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5480  xl_subxacts.nsubxacts = nsubxacts;
5481  }
5482 
5483  if (nrels > 0)
5484  {
5485  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5486  xl_relfilenodes.nrels = nrels;
5487  }
5488 
5489  if (nmsgs > 0)
5490  {
5491  xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5492  xl_invals.nmsgs = nmsgs;
5493  }
5494 
5495  if (TransactionIdIsValid(twophase_xid))
5496  {
5497  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5498  xl_twophase.xid = twophase_xid;
5499  Assert(twophase_gid != NULL);
5500 
5501  if (XLogLogicalInfoActive())
5502  xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
5503  }
5504 
5505  /* dump transaction origin information */
5507  {
5508  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5509 
5512  }
5513 
5514  if (xl_xinfo.xinfo != 0)
5515  info |= XLOG_XACT_HAS_INFO;
5516 
5517  /* Then include all the collected data into the commit record. */
5518 
5519  XLogBeginInsert();
5520 
5521  XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
5522 
5523  if (xl_xinfo.xinfo != 0)
5524  XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
5525 
5526  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5527  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5528 
5529  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5530  {
5531  XLogRegisterData((char *) (&xl_subxacts),
5533  XLogRegisterData((char *) subxacts,
5534  nsubxacts * sizeof(TransactionId));
5535  }
5536 
5537  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5538  {
5539  XLogRegisterData((char *) (&xl_relfilenodes),
5541  XLogRegisterData((char *) rels,
5542  nrels * sizeof(RelFileNode));
5543  }
5544 
5545  if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5546  {
5547  XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
5548  XLogRegisterData((char *) msgs,
5549  nmsgs * sizeof(SharedInvalidationMessage));
5550  }
5551 
5552  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5553  {
5554  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5555  if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
5556  XLogRegisterData(unconstify(char *, twophase_gid), strlen(twophase_gid) + 1);
5557  }
5558 
5559  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5560  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5561 
5562  /* we allow filtering by xacts */
5564 
5565  return XLogInsert(RM_XACT_ID, info);
5566 }
5567 
5568 /*
5569  * Log the commit record for a plain or twophase transaction abort.
5570  *
5571  * A 2pc abort will be emitted when twophase_xid is valid, a plain one
5572  * otherwise.
5573  */
5574 XLogRecPtr
5576  int nsubxacts, TransactionId *subxacts,
5577  int nrels, RelFileNode *rels,
5578  int xactflags, TransactionId twophase_xid,
5579  const char *twophase_gid)
5580 {
5581  xl_xact_abort xlrec;
5582  xl_xact_xinfo xl_xinfo;
5583  xl_xact_subxacts xl_subxacts;
5584  xl_xact_relfilenodes xl_relfilenodes;
5585  xl_xact_twophase xl_twophase;
5586  xl_xact_dbinfo xl_dbinfo;
5587  xl_xact_origin xl_origin;
5588 
5589  uint8 info;
5590 
5591  Assert(CritSectionCount > 0);
5592 
5593  xl_xinfo.xinfo = 0;
5594 
5595  /* decide between a plain and 2pc abort */
5596  if (!TransactionIdIsValid(twophase_xid))
5597  info = XLOG_XACT_ABORT;
5598  else
5599  info = XLOG_XACT_ABORT_PREPARED;
5600 
5601 
5602  /* First figure out and collect all the information needed */
5603 
5604  xlrec.xact_time = abort_time;
5605 
5606  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5607  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5608 
5609  if (nsubxacts > 0)
5610  {
5611  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5612  xl_subxacts.nsubxacts = nsubxacts;
5613  }
5614 
5615  if (nrels > 0)
5616  {
5617  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5618  xl_relfilenodes.nrels = nrels;
5619  }
5620 
5621  if (TransactionIdIsValid(twophase_xid))
5622  {
5623  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5624  xl_twophase.xid = twophase_xid;
5625  Assert(twophase_gid != NULL);
5626 
5627  if (XLogLogicalInfoActive())
5628  xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
5629  }
5630 
5631  if (TransactionIdIsValid(twophase_xid) && XLogLogicalInfoActive())
5632  {
5633  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5634  xl_dbinfo.dbId = MyDatabaseId;
5635  xl_dbinfo.tsId = MyDatabaseTableSpace;
5636  }
5637 
5638  /* dump transaction origin information only for abort prepared */
5640  TransactionIdIsValid(twophase_xid) &&
5642  {
5643  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5644 
5647  }
5648 
5649  if (xl_xinfo.xinfo != 0)
5650  info |= XLOG_XACT_HAS_INFO;
5651 
5652  /* Then include all the collected data into the abort record. */
5653 
5654  XLogBeginInsert();
5655 
5656  XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
5657 
5658  if (xl_xinfo.xinfo != 0)
5659  XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
5660 
5661  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5662  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5663 
5664  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5665  {
5666  XLogRegisterData((char *) (&xl_subxacts),
5668  XLogRegisterData((char *) subxacts,
5669  nsubxacts * sizeof(TransactionId));
5670  }
5671 
5672  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5673  {
5674  XLogRegisterData((char *) (&xl_relfilenodes),
5676  XLogRegisterData((char *) rels,
5677  nrels * sizeof(RelFileNode));
5678  }
5679 
5680  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5681  {
5682  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5683  if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
5684  XLogRegisterData(unconstify(char *, twophase_gid), strlen(twophase_gid) + 1);
5685  }
5686 
5687  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5688  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5689 
5690  if (TransactionIdIsValid(twophase_xid))
5692 
5693  return XLogInsert(RM_XACT_ID, info);
5694 }
5695 
5696 /*
5697  * Before 9.0 this was a fairly short function, but now it performs many
5698  * actions for which the order of execution is critical.
5699  */
5700 static void
5702  TransactionId xid,
5703  XLogRecPtr lsn,
5704  RepOriginId origin_id)
5705 {
5706  TransactionId max_xid;
5707  TimestampTz commit_time;
5708 
5710 
5711  max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
5712 
5713  /* Make sure nextFullXid is beyond any XID mentioned in the record. */
5715 
5716  Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
5717  (origin_id == InvalidRepOriginId));
5718 
5719  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
5720  commit_time = parsed->origin_timestamp;
5721  else
5722  commit_time = parsed->xact_time;
5723 
5724  /* Set the transaction commit timestamp and metadata */
5725  TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
5726  commit_time, origin_id, false);
5727 
5729  {
5730  /*
5731  * Mark the transaction committed in pg_xact.
5732  */
5733  TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
5734  }
5735  else
5736  {
5737  /*
5738  * If a transaction completion record arrives that has as-yet
5739  * unobserved subtransactions then this will not have been fully