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