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