PostgreSQL Source Code git master
xact.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * xact.c
4 * top level transaction system support routines
5 *
6 * See src/backend/access/transam/README for more information.
7 *
8 * Portions Copyright (c) 1996-2025, 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/xlogrecovery.h"
33#include "access/xlogutils.h"
34#include "catalog/index.h"
35#include "catalog/namespace.h"
36#include "catalog/pg_enum.h"
37#include "catalog/storage.h"
38#include "commands/async.h"
39#include "commands/tablecmds.h"
40#include "commands/trigger.h"
41#include "common/pg_prng.h"
42#include "executor/spi.h"
43#include "libpq/be-fsstubs.h"
44#include "libpq/pqsignal.h"
45#include "miscadmin.h"
46#include "pg_trace.h"
47#include "pgstat.h"
48#include "replication/logical.h"
51#include "replication/origin.h"
53#include "replication/syncrep.h"
55#include "storage/fd.h"
56#include "storage/lmgr.h"
57#include "storage/md.h"
58#include "storage/predicate.h"
59#include "storage/proc.h"
60#include "storage/procarray.h"
61#include "storage/sinvaladt.h"
62#include "storage/smgr.h"
63#include "utils/builtins.h"
64#include "utils/combocid.h"
65#include "utils/guc.h"
66#include "utils/inval.h"
67#include "utils/memutils.h"
68#include "utils/relmapper.h"
69#include "utils/snapmgr.h"
70#include "utils/timeout.h"
71#include "utils/timestamp.h"
72#include "utils/typcache.h"
73
74/*
75 * User-tweakable parameters
76 */
79
82
85
87
88/*
89 * CheckXidAlive is a xid value pointing to a possibly ongoing (sub)
90 * transaction. Currently, it is used in logical decoding. It's possible
91 * that such transactions can get aborted while the decoding is ongoing in
92 * which case we skip decoding that particular transaction. To ensure that we
93 * check whether the CheckXidAlive is aborted after fetching the tuple from
94 * system tables. We also ensure that during logical decoding we never
95 * directly access the tableam or heap APIs because we are checking for the
96 * concurrent aborts only in systable_* APIs.
97 */
99bool bsysscan = false;
100
101/*
102 * When running as a parallel worker, we place only a single
103 * TransactionStateData on the parallel worker's state stack, and the XID
104 * reflected there will be that of the *innermost* currently-active
105 * subtransaction in the backend that initiated parallelism. However,
106 * GetTopTransactionId() and TransactionIdIsCurrentTransactionId()
107 * need to return the same answers in the parallel worker as they would have
108 * in the user backend, so we need some additional bookkeeping.
109 *
110 * XactTopFullTransactionId stores the XID of our toplevel transaction, which
111 * will be the same as TopTransactionStateData.fullTransactionId in an
112 * ordinary backend; but in a parallel backend, which does not have the entire
113 * transaction state, it will instead be copied from the backend that started
114 * the parallel operation.
115 *
116 * nParallelCurrentXids will be 0 and ParallelCurrentXids NULL in an ordinary
117 * backend, but in a parallel backend, nParallelCurrentXids will contain the
118 * number of XIDs that need to be considered current, and ParallelCurrentXids
119 * will contain the XIDs themselves. This includes all XIDs that were current
120 * or sub-committed in the parent at the time the parallel operation began.
121 * The XIDs are stored sorted in numerical order (not logical order) to make
122 * lookups as fast as possible.
123 */
125static int nParallelCurrentXids = 0;
127
128/*
129 * Miscellaneous flag bits to record events which occur on the top level
130 * transaction. These flags are only persisted in MyXactFlags and are intended
131 * so we remember to do certain things later on in the transaction. This is
132 * globally accessible, so can be set from anywhere in the code that requires
133 * recording flags.
134 */
136
137/*
138 * transaction states - transaction state from server perspective
139 */
140typedef enum TransState
141{
142 TRANS_DEFAULT, /* idle */
143 TRANS_START, /* transaction starting */
144 TRANS_INPROGRESS, /* inside a valid transaction */
145 TRANS_COMMIT, /* commit in progress */
146 TRANS_ABORT, /* abort in progress */
147 TRANS_PREPARE, /* prepare in progress */
149
150/*
151 * transaction block states - transaction state of client queries
152 *
153 * Note: the subtransaction states are used only for non-topmost
154 * transactions; the others appear only in the topmost transaction.
155 */
156typedef enum TBlockState
157{
158 /* not-in-transaction-block states */
159 TBLOCK_DEFAULT, /* idle */
160 TBLOCK_STARTED, /* running single-query transaction */
161
162 /* transaction block states */
163 TBLOCK_BEGIN, /* starting transaction block */
164 TBLOCK_INPROGRESS, /* live transaction */
165 TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
166 TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
167 TBLOCK_END, /* COMMIT received */
168 TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
169 TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
170 TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
171 TBLOCK_PREPARE, /* live xact, PREPARE received */
172
173 /* subtransaction states */
174 TBLOCK_SUBBEGIN, /* starting a subtransaction */
175 TBLOCK_SUBINPROGRESS, /* live subtransaction */
176 TBLOCK_SUBRELEASE, /* RELEASE received */
177 TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
178 TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
179 TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
180 TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
181 TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
182 TBLOCK_SUBABORT_RESTART, /* failed subxact, ROLLBACK TO received */
184
185/*
186 * transaction state structure
187 *
188 * Note: parallelModeLevel counts the number of unmatched EnterParallelMode
189 * calls done at this transaction level. parallelChildXact is true if any
190 * upper transaction level has nonzero parallelModeLevel.
191 */
193{
194 FullTransactionId fullTransactionId; /* my FullTransactionId */
196 char *name; /* savepoint name, if any */
197 int savepointLevel; /* savepoint level */
198 TransState state; /* low-level state */
199 TBlockState blockState; /* high-level state */
200 int nestingLevel; /* transaction nesting depth */
201 int gucNestLevel; /* GUC context nesting depth */
202 MemoryContext curTransactionContext; /* my xact-lifetime context */
203 ResourceOwner curTransactionOwner; /* my query resources */
204 MemoryContext priorContext; /* CurrentMemoryContext before xact started */
205 TransactionId *childXids; /* subcommitted child XIDs, in XID order */
206 int nChildXids; /* # of subcommitted child XIDs */
207 int maxChildXids; /* allocated size of childXids[] */
208 Oid prevUser; /* previous CurrentUserId setting */
209 int prevSecContext; /* previous SecurityRestrictionContext */
210 bool prevXactReadOnly; /* entry-time xact r/o state */
211 bool startedInRecovery; /* did we start in recovery? */
212 bool didLogXid; /* has xid been included in WAL record? */
213 int parallelModeLevel; /* Enter/ExitParallelMode counter */
214 bool parallelChildXact; /* is any parent transaction parallel? */
215 bool chain; /* start a new block after this one */
216 bool topXidLogged; /* for a subxact: is top-level XID logged? */
217 struct TransactionStateData *parent; /* back link to parent */
219
221
222/*
223 * Serialized representation used to transmit transaction state to parallel
224 * workers through shared memory.
225 */
227{
236
237/* The size of SerializedTransactionState, not including the final array. */
238#define SerializedTransactionStateHeaderSize \
239 offsetof(SerializedTransactionState, parallelCurrentXids)
240
241/*
242 * CurrentTransactionState always points to the current transaction state
243 * block. It will point to TopTransactionStateData when not in a
244 * transaction at all, or when in a top-level transaction.
245 */
248 .blockState = TBLOCK_DEFAULT,
249 .topXidLogged = false,
250};
251
252/*
253 * unreportedXids holds XIDs of all subtransactions that have not yet been
254 * reported in an XLOG_XACT_ASSIGNMENT record.
255 */
258
260
261/*
262 * The subtransaction ID and command ID assignment counters are global
263 * to a whole transaction, so we do not keep them in the state stack.
264 */
268
269/*
270 * xactStartTimestamp is the value of transaction_timestamp().
271 * stmtStartTimestamp is the value of statement_timestamp().
272 * xactStopTimestamp is the time at which we log a commit / abort WAL record,
273 * or if that was skipped, the time of the first subsequent
274 * GetCurrentTransactionStopTimestamp() call.
275 *
276 * These do not change as we enter and exit subtransactions, so we don't
277 * keep them inside the TransactionState stack.
278 */
282
283/*
284 * GID to be used for preparing the current transaction. This is also
285 * global to a whole transaction, so we don't keep it in the state stack.
286 */
287static char *prepareGID;
288
289/*
290 * Some commands want to force synchronous commit.
291 */
292static bool forceSyncCommit = false;
293
294/* Flag for logging statements in a transaction. */
295bool xact_is_sampled = false;
296
297/*
298 * Private context for transaction-abort work --- we reserve space for this
299 * at startup to ensure that AbortTransaction and AbortSubTransaction can work
300 * when we've run out of memory.
301 */
303
304/*
305 * List of add-on start- and end-of-xact callbacks
306 */
307typedef struct XactCallbackItem
308{
311 void *arg;
313
315
316/*
317 * List of add-on start- and end-of-subxact callbacks
318 */
320{
323 void *arg;
325
327
328
329/* local function prototypes */
331static void AbortTransaction(void);
332static void AtAbort_Memory(void);
333static void AtCleanup_Memory(void);
334static void AtAbort_ResourceOwner(void);
335static void AtCCI_LocalCache(void);
336static void AtCommit_Memory(void);
337static void AtStart_Cache(void);
338static void AtStart_Memory(void);
339static void AtStart_ResourceOwner(void);
340static void CallXactCallbacks(XactEvent event);
341static void CallSubXactCallbacks(SubXactEvent event,
342 SubTransactionId mySubid,
343 SubTransactionId parentSubid);
344static void CleanupTransaction(void);
345static void CheckTransactionBlock(bool isTopLevel, bool throwError,
346 const char *stmtType);
347static void CommitTransaction(void);
348static TransactionId RecordTransactionAbort(bool isSubXact);
349static void StartTransaction(void);
350
351static bool CommitTransactionCommandInternal(void);
352static bool AbortCurrentTransactionInternal(void);
353
354static void StartSubTransaction(void);
355static void CommitSubTransaction(void);
356static void AbortSubTransaction(void);
357static void CleanupSubTransaction(void);
358static void PushTransaction(void);
359static void PopTransaction(void);
360
361static void AtSubAbort_Memory(void);
362static void AtSubCleanup_Memory(void);
363static void AtSubAbort_ResourceOwner(void);
364static void AtSubCommit_Memory(void);
365static void AtSubStart_Memory(void);
366static void AtSubStart_ResourceOwner(void);
367
368static void ShowTransactionState(const char *str);
369static void ShowTransactionStateRec(const char *str, TransactionState s);
370static const char *BlockStateAsString(TBlockState blockState);
371static const char *TransStateAsString(TransState state);
372
373
374/* ----------------------------------------------------------------
375 * transaction state accessors
376 * ----------------------------------------------------------------
377 */
378
379/*
380 * IsTransactionState
381 *
382 * This returns true if we are inside a valid transaction; that is,
383 * it is safe to initiate database access, take heavyweight locks, etc.
384 */
385bool
387{
389
390 /*
391 * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
392 * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
393 * TRANS_PREPARE since it might be too soon or too late within those
394 * transition states to do anything interesting. Hence, the only "valid"
395 * state is TRANS_INPROGRESS.
396 */
397 return (s->state == TRANS_INPROGRESS);
398}
399
400/*
401 * IsAbortedTransactionBlockState
402 *
403 * This returns true if we are within an aborted transaction block.
404 */
405bool
407{
409
410 if (s->blockState == TBLOCK_ABORT ||
412 return true;
413
414 return false;
415}
416
417
418/*
419 * GetTopTransactionId
420 *
421 * This will return the XID of the main transaction, assigning one if
422 * it's not yet set. Be careful to call this only inside a valid xact.
423 */
426{
430}
431
432/*
433 * GetTopTransactionIdIfAny
434 *
435 * This will return the XID of the main transaction, if one is assigned.
436 * It will return InvalidTransactionId if we are not currently inside a
437 * transaction, or inside a transaction that hasn't yet been assigned an XID.
438 */
441{
443}
444
445/*
446 * GetCurrentTransactionId
447 *
448 * This will return the XID of the current transaction (main or sub
449 * transaction), assigning one if it's not yet set. Be careful to call this
450 * only inside a valid xact.
451 */
454{
456
460}
461
462/*
463 * GetCurrentTransactionIdIfAny
464 *
465 * This will return the XID of the current sub xact, if one is assigned.
466 * It will return InvalidTransactionId if we are not currently inside a
467 * transaction, or inside a transaction that hasn't been assigned an XID yet.
468 */
471{
473}
474
475/*
476 * GetTopFullTransactionId
477 *
478 * This will return the FullTransactionId of the main transaction, assigning
479 * one if it's not yet set. Be careful to call this only inside a valid xact.
480 */
483{
487}
488
489/*
490 * GetTopFullTransactionIdIfAny
491 *
492 * This will return the FullTransactionId of the main transaction, if one is
493 * assigned. It will return InvalidFullTransactionId if we are not currently
494 * inside a transaction, or inside a transaction that hasn't yet been assigned
495 * one.
496 */
499{
501}
502
503/*
504 * GetCurrentFullTransactionId
505 *
506 * This will return the FullTransactionId of the current transaction (main or
507 * sub transaction), assigning one if it's not yet set. Be careful to call
508 * this only inside a valid xact.
509 */
512{
514
517 return s->fullTransactionId;
518}
519
520/*
521 * GetCurrentFullTransactionIdIfAny
522 *
523 * This will return the FullTransactionId of the current sub xact, if one is
524 * assigned. It will return InvalidFullTransactionId if we are not currently
525 * inside a transaction, or inside a transaction that hasn't been assigned one
526 * yet.
527 */
530{
532}
533
534/*
535 * MarkCurrentTransactionIdLoggedIfAny
536 *
537 * Remember that the current xid - if it is assigned - now has been wal logged.
538 */
539void
541{
544}
545
546/*
547 * IsSubxactTopXidLogPending
548 *
549 * This is used to decide whether we need to WAL log the top-level XID for
550 * operation in a subtransaction. We require that for logical decoding, see
551 * LogicalDecodingProcessRecord.
552 *
553 * This returns true if wal_level >= logical and we are inside a valid
554 * subtransaction, for which the assignment was not yet written to any WAL
555 * record.
556 */
557bool
559{
560 /* check whether it is already logged */
562 return false;
563
564 /* wal_level has to be logical */
566 return false;
567
568 /* we need to be in a transaction state */
569 if (!IsTransactionState())
570 return false;
571
572 /* it has to be a subtransaction */
573 if (!IsSubTransaction())
574 return false;
575
576 /* the subtransaction has to have a XID assigned */
578 return false;
579
580 return true;
581}
582
583/*
584 * MarkSubxactTopXidLogged
585 *
586 * Remember that the top transaction id for the current subtransaction is WAL
587 * logged now.
588 */
589void
591{
593
595}
596
597/*
598 * GetStableLatestTransactionId
599 *
600 * Get the transaction's XID if it has one, else read the next-to-be-assigned
601 * XID. Once we have a value, return that same value for the remainder of the
602 * current transaction. This is meant to provide the reference point for the
603 * age(xid) function, but might be useful for other maintenance tasks as well.
604 */
607{
609 static TransactionId stablexid = InvalidTransactionId;
610
611 if (lxid != MyProc->vxid.lxid)
612 {
613 lxid = MyProc->vxid.lxid;
614 stablexid = GetTopTransactionIdIfAny();
615 if (!TransactionIdIsValid(stablexid))
616 stablexid = ReadNextTransactionId();
617 }
618
619 Assert(TransactionIdIsValid(stablexid));
620
621 return stablexid;
622}
623
624/*
625 * AssignTransactionId
626 *
627 * Assigns a new permanent FullTransactionId to the given TransactionState.
628 * We do not assign XIDs to transactions until/unless this is called.
629 * Also, any parent TransactionStates that don't yet have XIDs are assigned
630 * one; this maintains the invariant that a child transaction has an XID
631 * following its parent's.
632 */
633static void
635{
636 bool isSubXact = (s->parent != NULL);
637 ResourceOwner currentOwner;
638 bool log_unknown_top = false;
639
640 /* Assert that caller didn't screw up */
643
644 /*
645 * Workers synchronize transaction state at the beginning of each parallel
646 * operation, so we can't account for new XIDs at this point.
647 */
650 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
651 errmsg("cannot assign transaction IDs during a parallel operation")));
652
653 /*
654 * Ensure parent(s) have XIDs, so that a child always has an XID later
655 * than its parent. Mustn't recurse here, or we might get a stack
656 * overflow if we're at the bottom of a huge stack of subtransactions none
657 * of which have XIDs yet.
658 */
660 {
662 TransactionState *parents;
663 size_t parentOffset = 0;
664
665 parents = palloc(sizeof(TransactionState) * s->nestingLevel);
666 while (p != NULL && !FullTransactionIdIsValid(p->fullTransactionId))
667 {
668 parents[parentOffset++] = p;
669 p = p->parent;
670 }
671
672 /*
673 * This is technically a recursive call, but the recursion will never
674 * be more than one layer deep.
675 */
676 while (parentOffset != 0)
677 AssignTransactionId(parents[--parentOffset]);
678
679 pfree(parents);
680 }
681
682 /*
683 * When wal_level=logical, guarantee that a subtransaction's xid can only
684 * be seen in the WAL stream if its toplevel xid has been logged before.
685 * If necessary we log an xact_assignment record with fewer than
686 * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
687 * for a transaction even though it appears in a WAL record, we just might
688 * superfluously log something. That can happen when an xid is included
689 * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
690 * xl_standby_locks.
691 */
692 if (isSubXact && XLogLogicalInfoActive() &&
694 log_unknown_top = true;
695
696 /*
697 * Generate a new FullTransactionId and record its xid in PGPROC and
698 * pg_subtrans.
699 *
700 * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
701 * shared storage other than PGPROC; because if there's no room for it in
702 * PGPROC, the subtrans entry is needed to ensure that other backends see
703 * the Xid as "running". See GetNewTransactionId.
704 */
706 if (!isSubXact)
708
709 if (isSubXact)
712
713 /*
714 * If it's a top-level transaction, the predicate locking system needs to
715 * be told about it too.
716 */
717 if (!isSubXact)
719
720 /*
721 * Acquire lock on the transaction XID. (We assume this cannot block.) We
722 * have to ensure that the lock is assigned to the transaction's own
723 * ResourceOwner.
724 */
725 currentOwner = CurrentResourceOwner;
727
729
730 CurrentResourceOwner = currentOwner;
731
732 /*
733 * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
734 * top-level transaction we issue a WAL record for the assignment. We
735 * include the top-level xid and all the subxids that have not yet been
736 * reported using XLOG_XACT_ASSIGNMENT records.
737 *
738 * This is required to limit the amount of shared memory required in a hot
739 * standby server to keep track of in-progress XIDs. See notes for
740 * RecordKnownAssignedTransactionIds().
741 *
742 * We don't keep track of the immediate parent of each subxid, only the
743 * top-level transaction that each subxact belongs to. This is correct in
744 * recovery only because aborted subtransactions are separately WAL
745 * logged.
746 *
747 * This is correct even for the case where several levels above us didn't
748 * have an xid assigned as we recursed up to them beforehand.
749 */
750 if (isSubXact && XLogStandbyInfoActive())
751 {
754
755 /*
756 * ensure this test matches similar one in
757 * RecoverPreparedTransactions()
758 */
760 log_unknown_top)
761 {
762 xl_xact_assignment xlrec;
763
764 /*
765 * xtop is always set by now because we recurse up transaction
766 * stack to the highest unassigned xid and then come back down
767 */
768 xlrec.xtop = GetTopTransactionId();
771
776
777 (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
778
779 nUnreportedXids = 0;
780 /* mark top, not current xact as having been logged */
782 }
783 }
784}
785
786/*
787 * GetCurrentSubTransactionId
788 */
791{
793
794 return s->subTransactionId;
795}
796
797/*
798 * SubTransactionIsActive
799 *
800 * Test if the specified subxact ID is still active. Note caller is
801 * responsible for checking whether this ID is relevant to the current xact.
802 */
803bool
805{
807
808 for (s = CurrentTransactionState; s != NULL; s = s->parent)
809 {
810 if (s->state == TRANS_ABORT)
811 continue;
812 if (s->subTransactionId == subxid)
813 return true;
814 }
815 return false;
816}
817
818
819/*
820 * GetCurrentCommandId
821 *
822 * "used" must be true if the caller intends to use the command ID to mark
823 * inserted/updated/deleted tuples. false means the ID is being fetched
824 * for read-only purposes (ie, as a snapshot validity cutoff). See
825 * CommandCounterIncrement() for discussion.
826 */
829{
830 /* this is global to a transaction, not subtransaction-local */
831 if (used)
832 {
833 /*
834 * Forbid setting currentCommandIdUsed in a parallel worker, because
835 * we have no provision for communicating this back to the leader. We
836 * could relax this restriction when currentCommandIdUsed was already
837 * true at the start of the parallel operation.
838 */
839 if (IsParallelWorker())
841 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
842 errmsg("cannot modify data in a parallel worker")));
843
845 }
846 return currentCommandId;
847}
848
849/*
850 * SetParallelStartTimestamps
851 *
852 * In a parallel worker, we should inherit the parent transaction's
853 * timestamps rather than setting our own. The parallel worker
854 * infrastructure must call this to provide those values before
855 * calling StartTransaction() or SetCurrentStatementStartTimestamp().
856 */
857void
859{
861 xactStartTimestamp = xact_ts;
862 stmtStartTimestamp = stmt_ts;
863}
864
865/*
866 * GetCurrentTransactionStartTimestamp
867 */
870{
871 return xactStartTimestamp;
872}
873
874/*
875 * GetCurrentStatementStartTimestamp
876 */
879{
880 return stmtStartTimestamp;
881}
882
883/*
884 * GetCurrentTransactionStopTimestamp
885 *
886 * If the transaction stop time hasn't already been set, which can happen if
887 * we decided we don't need to log an XLOG record, set xactStopTimestamp.
888 */
891{
893
894 /* should only be called after commit / abort processing */
895 Assert(s->state == TRANS_DEFAULT ||
896 s->state == TRANS_COMMIT ||
897 s->state == TRANS_ABORT ||
898 s->state == TRANS_PREPARE);
899
900 if (xactStopTimestamp == 0)
902
903 return xactStopTimestamp;
904}
905
906/*
907 * SetCurrentStatementStartTimestamp
908 *
909 * In a parallel worker, this should already have been provided by a call
910 * to SetParallelStartTimestamps().
911 */
912void
914{
915 if (!IsParallelWorker())
917 else
919}
920
921/*
922 * GetCurrentTransactionNestLevel
923 *
924 * Note: this will return zero when not inside any transaction, one when
925 * inside a top-level transaction, etc.
926 */
927int
929{
931
932 return s->nestingLevel;
933}
934
935
936/*
937 * TransactionIdIsCurrentTransactionId
938 */
939bool
941{
943
944 /*
945 * We always say that BootstrapTransactionId is "not my transaction ID"
946 * even when it is (ie, during bootstrap). Along with the fact that
947 * transam.c always treats BootstrapTransactionId as already committed,
948 * this causes the heapam_visibility.c routines to see all tuples as
949 * committed, which is what we need during bootstrap. (Bootstrap mode
950 * only inserts tuples, it never updates or deletes them, so all tuples
951 * can be presumed good immediately.)
952 *
953 * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
954 * not my transaction ID, so we can just return "false" immediately for
955 * any non-normal XID.
956 */
957 if (!TransactionIdIsNormal(xid))
958 return false;
959
961 return true;
962
963 /*
964 * In parallel workers, the XIDs we must consider as current are stored in
965 * ParallelCurrentXids rather than the transaction-state stack. Note that
966 * the XIDs in this array are sorted numerically rather than according to
967 * transactionIdPrecedes order.
968 */
969 if (nParallelCurrentXids > 0)
970 {
971 int low,
972 high;
973
974 low = 0;
975 high = nParallelCurrentXids - 1;
976 while (low <= high)
977 {
978 int middle;
979 TransactionId probe;
980
981 middle = low + (high - low) / 2;
982 probe = ParallelCurrentXids[middle];
983 if (probe == xid)
984 return true;
985 else if (probe < xid)
986 low = middle + 1;
987 else
988 high = middle - 1;
989 }
990 return false;
991 }
992
993 /*
994 * We will return true for the Xid of the current subtransaction, any of
995 * its subcommitted children, any of its parents, or any of their
996 * previously subcommitted children. However, a transaction being aborted
997 * is no longer "current", even though it may still have an entry on the
998 * state stack.
999 */
1000 for (s = CurrentTransactionState; s != NULL; s = s->parent)
1001 {
1002 int low,
1003 high;
1004
1005 if (s->state == TRANS_ABORT)
1006 continue;
1008 continue; /* it can't have any child XIDs either */
1010 return true;
1011 /* As the childXids array is ordered, we can use binary search */
1012 low = 0;
1013 high = s->nChildXids - 1;
1014 while (low <= high)
1015 {
1016 int middle;
1017 TransactionId probe;
1018
1019 middle = low + (high - low) / 2;
1020 probe = s->childXids[middle];
1021 if (TransactionIdEquals(probe, xid))
1022 return true;
1023 else if (TransactionIdPrecedes(probe, xid))
1024 low = middle + 1;
1025 else
1026 high = middle - 1;
1027 }
1028 }
1029
1030 return false;
1031}
1032
1033/*
1034 * TransactionStartedDuringRecovery
1035 *
1036 * Returns true if the current transaction started while recovery was still
1037 * in progress. Recovery might have ended since so RecoveryInProgress() might
1038 * return false already.
1039 */
1040bool
1042{
1044}
1045
1046/*
1047 * EnterParallelMode
1048 */
1049void
1051{
1053
1054 Assert(s->parallelModeLevel >= 0);
1055
1056 ++s->parallelModeLevel;
1057}
1058
1059/*
1060 * ExitParallelMode
1061 */
1062void
1064{
1066
1067 Assert(s->parallelModeLevel > 0);
1070
1071 --s->parallelModeLevel;
1072}
1073
1074/*
1075 * IsInParallelMode
1076 *
1077 * Are we in a parallel operation, as either the leader or a worker? Check
1078 * this to prohibit operations that change backend-local state expected to
1079 * match across all workers. Mere caches usually don't require such a
1080 * restriction. State modified in a strict push/pop fashion, such as the
1081 * active snapshot stack, is often fine.
1082 *
1083 * We say we are in parallel mode if we are in a subxact of a transaction
1084 * that's initiated a parallel operation; for most purposes that context
1085 * has all the same restrictions.
1086 */
1087bool
1089{
1091
1092 return s->parallelModeLevel != 0 || s->parallelChildXact;
1093}
1094
1095/*
1096 * CommandCounterIncrement
1097 */
1098void
1100{
1101 /*
1102 * If the current value of the command counter hasn't been "used" to mark
1103 * tuples, we need not increment it, since there's no need to distinguish
1104 * a read-only command from others. This helps postpone command counter
1105 * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1106 */
1108 {
1109 /*
1110 * Workers synchronize transaction state at the beginning of each
1111 * parallel operation, so we can't account for new commands after that
1112 * point.
1113 */
1115 ereport(ERROR,
1116 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
1117 errmsg("cannot start commands during a parallel operation")));
1118
1119 currentCommandId += 1;
1121 {
1122 currentCommandId -= 1;
1123 ereport(ERROR,
1124 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1125 errmsg("cannot have more than 2^32-2 commands in a transaction")));
1126 }
1127 currentCommandIdUsed = false;
1128
1129 /* Propagate new command ID into static snapshots */
1131
1132 /*
1133 * Make any catalog changes done by the just-completed command visible
1134 * in the local syscache. We obviously don't need to do this after a
1135 * read-only command. (But see hacks in inval.c to make real sure we
1136 * don't think a command that queued inval messages was read-only.)
1137 */
1139 }
1140}
1141
1142/*
1143 * ForceSyncCommit
1144 *
1145 * Interface routine to allow commands to force a synchronous commit of the
1146 * current top-level transaction. Currently, two-phase commit does not
1147 * persist and restore this variable. So long as all callers use
1148 * PreventInTransactionBlock(), that omission has no consequences.
1149 */
1150void
1152{
1153 forceSyncCommit = true;
1154}
1155
1156
1157/* ----------------------------------------------------------------
1158 * StartTransaction stuff
1159 * ----------------------------------------------------------------
1160 */
1161
1162/*
1163 * AtStart_Cache
1164 */
1165static void
1167{
1169}
1170
1171/*
1172 * AtStart_Memory
1173 */
1174static void
1176{
1178
1179 /*
1180 * Remember the memory context that was active prior to transaction start.
1181 */
1183
1184 /*
1185 * If this is the first time through, create a private context for
1186 * AbortTransaction to work in. By reserving some space now, we can
1187 * insulate AbortTransaction from out-of-memory scenarios. Like
1188 * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1189 * size, so that space will be reserved immediately.
1190 */
1191 if (TransactionAbortContext == NULL)
1194 "TransactionAbortContext",
1195 32 * 1024,
1196 32 * 1024,
1197 32 * 1024);
1198
1199 /*
1200 * Likewise, if this is the first time through, create a top-level context
1201 * for transaction-local data. This context will be reset at transaction
1202 * end, and then re-used in later transactions.
1203 */
1204 if (TopTransactionContext == NULL)
1207 "TopTransactionContext",
1209
1210 /*
1211 * In a top-level transaction, CurTransactionContext is the same as
1212 * TopTransactionContext.
1213 */
1216
1217 /* Make the CurTransactionContext active. */
1219}
1220
1221/*
1222 * AtStart_ResourceOwner
1223 */
1224static void
1226{
1228
1229 /*
1230 * We shouldn't have a transaction resource owner already.
1231 */
1233
1234 /*
1235 * Create a toplevel resource owner for the transaction.
1236 */
1237 s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1238
1242}
1243
1244/* ----------------------------------------------------------------
1245 * StartSubTransaction stuff
1246 * ----------------------------------------------------------------
1247 */
1248
1249/*
1250 * AtSubStart_Memory
1251 */
1252static void
1254{
1256
1258
1259 /*
1260 * Remember the context that was active prior to subtransaction start.
1261 */
1263
1264 /*
1265 * Create a CurTransactionContext, which will be used to hold data that
1266 * survives subtransaction commit but disappears on subtransaction abort.
1267 * We make it a child of the immediate parent's CurTransactionContext.
1268 */
1270 "CurTransactionContext",
1273
1274 /* Make the CurTransactionContext active. */
1276}
1277
1278/*
1279 * AtSubStart_ResourceOwner
1280 */
1281static void
1283{
1285
1286 Assert(s->parent != NULL);
1287
1288 /*
1289 * Create a resource owner for the subtransaction. We make it a child of
1290 * the immediate parent's resource owner.
1291 */
1294 "SubTransaction");
1295
1298}
1299
1300/* ----------------------------------------------------------------
1301 * CommitTransaction stuff
1302 * ----------------------------------------------------------------
1303 */
1304
1305/*
1306 * RecordTransactionCommit
1307 *
1308 * Returns latest XID among xact and its children, or InvalidTransactionId
1309 * if the xact has no XID. (We compute that here just because it's easier.)
1310 *
1311 * If you change this function, see RecordTransactionCommitPrepared also.
1312 */
1313static TransactionId
1315{
1317 bool markXidCommitted = TransactionIdIsValid(xid);
1319 int nrels;
1320 RelFileLocator *rels;
1321 int nchildren;
1322 TransactionId *children;
1323 int ndroppedstats = 0;
1324 xl_xact_stats_item *droppedstats = NULL;
1325 int nmsgs = 0;
1326 SharedInvalidationMessage *invalMessages = NULL;
1327 bool RelcacheInitFileInval = false;
1328 bool wrote_xlog;
1329
1330 /*
1331 * Log pending invalidations for logical decoding of in-progress
1332 * transactions. Normally for DDLs, we log this at each command end,
1333 * however, for certain cases where we directly update the system table
1334 * without a transaction block, the invalidations are not logged till this
1335 * time.
1336 */
1339
1340 /* Get data needed for commit record */
1341 nrels = smgrGetPendingDeletes(true, &rels);
1342 nchildren = xactGetCommittedChildren(&children);
1343 ndroppedstats = pgstat_get_transactional_drops(true, &droppedstats);
1345 nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1346 &RelcacheInitFileInval);
1347 wrote_xlog = (XactLastRecEnd != 0);
1348
1349 /*
1350 * If we haven't been assigned an XID yet, we neither can, nor do we want
1351 * to write a COMMIT record.
1352 */
1353 if (!markXidCommitted)
1354 {
1355 /*
1356 * We expect that every RelationDropStorage is followed by a catalog
1357 * update, and hence XID assignment, so we shouldn't get here with any
1358 * pending deletes. Same is true for dropping stats.
1359 *
1360 * Use a real test not just an Assert to check this, since it's a bit
1361 * fragile.
1362 */
1363 if (nrels != 0 || ndroppedstats != 0)
1364 elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1365
1366 /* Can't have child XIDs either; AssignTransactionId enforces this */
1367 Assert(nchildren == 0);
1368
1369 /*
1370 * Transactions without an assigned xid can contain invalidation
1371 * messages. While inplace updates do this, this is not known to be
1372 * necessary; see comment at inplace CacheInvalidateHeapTuple().
1373 * Extensions might still rely on this capability, and standbys may
1374 * need to process those invals. We can't emit a commit record
1375 * without an xid, and we don't want to force assigning an xid,
1376 * because that'd be problematic for e.g. vacuum. Hence we emit a
1377 * bespoke record for the invalidations. We don't want to use that in
1378 * case a commit record is emitted, so they happen synchronously with
1379 * commits (besides not wanting to emit more WAL records).
1380 *
1381 * XXX Every known use of this capability is a defect. Since an XID
1382 * isn't controlling visibility of the change that prompted invals,
1383 * other sessions need the inval even if this transactions aborts.
1384 *
1385 * ON COMMIT DELETE ROWS does a nontransactional index_build(), which
1386 * queues a relcache inval, including in transactions without an xid
1387 * that had read the (empty) table. Standbys don't need any ON COMMIT
1388 * DELETE ROWS invals, but we've not done the work to withhold them.
1389 */
1390 if (nmsgs != 0)
1391 {
1392 LogStandbyInvalidations(nmsgs, invalMessages,
1393 RelcacheInitFileInval);
1394 wrote_xlog = true; /* not strictly necessary */
1395 }
1396
1397 /*
1398 * If we didn't create XLOG entries, we're done here; otherwise we
1399 * should trigger flushing those entries the same as a commit record
1400 * would. This will primarily happen for HOT pruning and the like; we
1401 * want these to be flushed to disk in due time.
1402 */
1403 if (!wrote_xlog)
1404 goto cleanup;
1405 }
1406 else
1407 {
1408 bool replorigin;
1409
1410 /*
1411 * Are we using the replication origins feature? Or, in other words,
1412 * are we replaying remote actions?
1413 */
1416
1417 /*
1418 * Mark ourselves as within our "commit critical section". This
1419 * forces any concurrent checkpoint to wait until we've updated
1420 * pg_xact. Without this, it is possible for the checkpoint to set
1421 * REDO after the XLOG record but fail to flush the pg_xact update to
1422 * disk, leading to loss of the transaction commit if the system
1423 * crashes a little later.
1424 *
1425 * Note: we could, but don't bother to, set this flag in
1426 * RecordTransactionAbort. That's because loss of a transaction abort
1427 * is noncritical; the presumption would be that it aborted, anyway.
1428 *
1429 * It's safe to change the delayChkptFlags flag of our own backend
1430 * without holding the ProcArrayLock, since we're the only one
1431 * modifying it. This makes checkpoint's determination of which xacts
1432 * are delaying the checkpoint a bit fuzzy, but it doesn't matter.
1433 */
1437
1438 /*
1439 * Insert the commit XLOG record.
1440 */
1442 nchildren, children, nrels, rels,
1443 ndroppedstats, droppedstats,
1444 nmsgs, invalMessages,
1445 RelcacheInitFileInval,
1447 InvalidTransactionId, NULL /* plain commit */ );
1448
1449 if (replorigin)
1450 /* Move LSNs forward for this replication origin */
1453
1454 /*
1455 * Record commit timestamp. The value comes from plain commit
1456 * timestamp if there's no replication origin; otherwise, the
1457 * timestamp was already set in replorigin_session_origin_timestamp by
1458 * replication.
1459 *
1460 * We don't need to WAL-log anything here, as the commit record
1461 * written above already contains the data.
1462 */
1463
1464 if (!replorigin || replorigin_session_origin_timestamp == 0)
1466
1467 TransactionTreeSetCommitTsData(xid, nchildren, children,
1470 }
1471
1472 /*
1473 * Check if we want to commit asynchronously. We can allow the XLOG flush
1474 * to happen asynchronously if synchronous_commit=off, or if the current
1475 * transaction has not performed any WAL-logged operation or didn't assign
1476 * an xid. The transaction can end up not writing any WAL, even if it has
1477 * an xid, if it only wrote to temporary and/or unlogged tables. It can
1478 * end up having written WAL without an xid if it did HOT pruning. In
1479 * case of a crash, the loss of such a transaction will be irrelevant;
1480 * temp tables will be lost anyway, unlogged tables will be truncated and
1481 * HOT pruning will be done again later. (Given the foregoing, you might
1482 * think that it would be unnecessary to emit the XLOG record at all in
1483 * this case, but we don't currently try to do that. It would certainly
1484 * cause problems at least in Hot Standby mode, where the
1485 * KnownAssignedXids machinery requires tracking every XID assignment. It
1486 * might be OK to skip it only when wal_level < replica, but for now we
1487 * don't.)
1488 *
1489 * However, if we're doing cleanup of any non-temp rels or committing any
1490 * command that wanted to force sync commit, then we must flush XLOG
1491 * immediately. (We must not allow asynchronous commit if there are any
1492 * non-temp tables to be deleted, because we might delete the files before
1493 * the COMMIT record is flushed to disk. We do allow asynchronous commit
1494 * if all to-be-deleted tables are temporary though, since they are lost
1495 * anyway if we crash.)
1496 */
1497 if ((wrote_xlog && markXidCommitted &&
1499 forceSyncCommit || nrels > 0)
1500 {
1502
1503 /*
1504 * Now we may update the CLOG, if we wrote a COMMIT record above
1505 */
1506 if (markXidCommitted)
1507 TransactionIdCommitTree(xid, nchildren, children);
1508 }
1509 else
1510 {
1511 /*
1512 * Asynchronous commit case:
1513 *
1514 * This enables possible committed transaction loss in the case of a
1515 * postmaster crash because WAL buffers are left unwritten. Ideally we
1516 * could issue the WAL write without the fsync, but some
1517 * wal_sync_methods do not allow separate write/fsync.
1518 *
1519 * Report the latest async commit LSN, so that the WAL writer knows to
1520 * flush this commit.
1521 */
1523
1524 /*
1525 * We must not immediately update the CLOG, since we didn't flush the
1526 * XLOG. Instead, we store the LSN up to which the XLOG must be
1527 * flushed before the CLOG may be updated.
1528 */
1529 if (markXidCommitted)
1530 TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1531 }
1532
1533 /*
1534 * If we entered a commit critical section, leave it now, and let
1535 * checkpoints proceed.
1536 */
1537 if (markXidCommitted)
1538 {
1539 MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
1541 }
1542
1543 /* Compute latestXid while we have the child XIDs handy */
1544 latestXid = TransactionIdLatest(xid, nchildren, children);
1545
1546 /*
1547 * Wait for synchronous replication, if required. Similar to the decision
1548 * above about using committing asynchronously we only want to wait if
1549 * this backend assigned an xid and wrote WAL. No need to wait if an xid
1550 * was assigned due to temporary/unlogged tables or due to HOT pruning.
1551 *
1552 * Note that at this stage we have marked clog, but still show as running
1553 * in the procarray and continue to hold locks.
1554 */
1555 if (wrote_xlog && markXidCommitted)
1557
1558 /* remember end of last commit record */
1560
1561 /* Reset XactLastRecEnd until the next transaction writes something */
1562 XactLastRecEnd = 0;
1563cleanup:
1564 /* Clean up local data */
1565 if (rels)
1566 pfree(rels);
1567 if (ndroppedstats)
1568 pfree(droppedstats);
1569
1570 return latestXid;
1571}
1572
1573
1574/*
1575 * AtCCI_LocalCache
1576 */
1577static void
1579{
1580 /*
1581 * Make any pending relation map changes visible. We must do this before
1582 * processing local sinval messages, so that the map changes will get
1583 * reflected into the relcache when relcache invals are processed.
1584 */
1586
1587 /*
1588 * Make catalog changes visible to me for the next command.
1589 */
1591}
1592
1593/*
1594 * AtCommit_Memory
1595 */
1596static void
1598{
1600
1601 /*
1602 * Return to the memory context that was current before we started the
1603 * transaction. (In principle, this could not be any of the contexts we
1604 * are about to delete. If it somehow is, assertions in mcxt.c will
1605 * complain.)
1606 */
1608
1609 /*
1610 * Release all transaction-local memory. TopTransactionContext survives
1611 * but becomes empty; any sub-contexts go away.
1612 */
1615
1616 /*
1617 * Clear these pointers as a pro-forma matter. (Notionally, while
1618 * TopTransactionContext still exists, it's currently not associated with
1619 * this TransactionState struct.)
1620 */
1621 CurTransactionContext = NULL;
1622 s->curTransactionContext = NULL;
1623}
1624
1625/* ----------------------------------------------------------------
1626 * CommitSubTransaction stuff
1627 * ----------------------------------------------------------------
1628 */
1629
1630/*
1631 * AtSubCommit_Memory
1632 */
1633static void
1635{
1637
1638 Assert(s->parent != NULL);
1639
1640 /* Return to parent transaction level's memory context. */
1643
1644 /*
1645 * Ordinarily we cannot throw away the child's CurTransactionContext,
1646 * since the data it contains will be needed at upper commit. However, if
1647 * there isn't actually anything in it, we can throw it away. This avoids
1648 * a small memory leak in the common case of "trivial" subxacts.
1649 */
1651 {
1653 s->curTransactionContext = NULL;
1654 }
1655}
1656
1657/*
1658 * AtSubCommit_childXids
1659 *
1660 * Pass my own XID and my child XIDs up to my parent as committed children.
1661 */
1662static void
1664{
1666 int new_nChildXids;
1667
1668 Assert(s->parent != NULL);
1669
1670 /*
1671 * The parent childXids array will need to hold my XID and all my
1672 * childXids, in addition to the XIDs already there.
1673 */
1674 new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1675
1676 /* Allocate or enlarge the parent array if necessary */
1677 if (s->parent->maxChildXids < new_nChildXids)
1678 {
1679 int new_maxChildXids;
1680 TransactionId *new_childXids;
1681
1682 /*
1683 * Make it 2x what's needed right now, to avoid having to enlarge it
1684 * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1685 * is what ensures that we don't need to worry about integer overflow
1686 * here or in the calculation of new_nChildXids.)
1687 */
1688 new_maxChildXids = Min(new_nChildXids * 2,
1689 (int) (MaxAllocSize / sizeof(TransactionId)));
1690
1691 if (new_maxChildXids < new_nChildXids)
1692 ereport(ERROR,
1693 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1694 errmsg("maximum number of committed subtransactions (%d) exceeded",
1695 (int) (MaxAllocSize / sizeof(TransactionId)))));
1696
1697 /*
1698 * We keep the child-XID arrays in TopTransactionContext; this avoids
1699 * setting up child-transaction contexts for what might be just a few
1700 * bytes of grandchild XIDs.
1701 */
1702 if (s->parent->childXids == NULL)
1703 new_childXids =
1705 new_maxChildXids * sizeof(TransactionId));
1706 else
1707 new_childXids = repalloc(s->parent->childXids,
1708 new_maxChildXids * sizeof(TransactionId));
1709
1710 s->parent->childXids = new_childXids;
1711 s->parent->maxChildXids = new_maxChildXids;
1712 }
1713
1714 /*
1715 * Copy all my XIDs to parent's array.
1716 *
1717 * Note: We rely on the fact that the XID of a child always follows that
1718 * of its parent. By copying the XID of this subtransaction before the
1719 * XIDs of its children, we ensure that the array stays ordered. Likewise,
1720 * all XIDs already in the array belong to subtransactions started and
1721 * subcommitted before us, so their XIDs must precede ours.
1722 */
1724
1725 if (s->nChildXids > 0)
1726 memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1727 s->childXids,
1728 s->nChildXids * sizeof(TransactionId));
1729
1730 s->parent->nChildXids = new_nChildXids;
1731
1732 /* Release child's array to avoid leakage */
1733 if (s->childXids != NULL)
1734 pfree(s->childXids);
1735 /* We must reset these to avoid double-free if fail later in commit */
1736 s->childXids = NULL;
1737 s->nChildXids = 0;
1738 s->maxChildXids = 0;
1739}
1740
1741/* ----------------------------------------------------------------
1742 * AbortTransaction stuff
1743 * ----------------------------------------------------------------
1744 */
1745
1746/*
1747 * RecordTransactionAbort
1748 *
1749 * Returns latest XID among xact and its children, or InvalidTransactionId
1750 * if the xact has no XID. (We compute that here just because it's easier.)
1751 */
1752static TransactionId
1754{
1756 TransactionId latestXid;
1757 int nrels;
1758 RelFileLocator *rels;
1759 int ndroppedstats = 0;
1760 xl_xact_stats_item *droppedstats = NULL;
1761 int nchildren;
1762 TransactionId *children;
1763 TimestampTz xact_time;
1764 bool replorigin;
1765
1766 /*
1767 * If we haven't been assigned an XID, nobody will care whether we aborted
1768 * or not. Hence, we're done in that case. It does not matter if we have
1769 * rels to delete (note that this routine is not responsible for actually
1770 * deleting 'em). We cannot have any child XIDs, either.
1771 */
1772 if (!TransactionIdIsValid(xid))
1773 {
1774 /* Reset XactLastRecEnd until the next transaction writes something */
1775 if (!isSubXact)
1776 XactLastRecEnd = 0;
1777 return InvalidTransactionId;
1778 }
1779
1780 /*
1781 * We have a valid XID, so we should write an ABORT record for it.
1782 *
1783 * We do not flush XLOG to disk here, since the default assumption after a
1784 * crash would be that we aborted, anyway. For the same reason, we don't
1785 * need to worry about interlocking against checkpoint start.
1786 */
1787
1788 /*
1789 * Check that we haven't aborted halfway through RecordTransactionCommit.
1790 */
1791 if (TransactionIdDidCommit(xid))
1792 elog(PANIC, "cannot abort transaction %u, it was already committed",
1793 xid);
1794
1795 /*
1796 * Are we using the replication origins feature? Or, in other words, are
1797 * we replaying remote actions?
1798 */
1801
1802 /* Fetch the data we need for the abort record */
1803 nrels = smgrGetPendingDeletes(false, &rels);
1804 nchildren = xactGetCommittedChildren(&children);
1805 ndroppedstats = pgstat_get_transactional_drops(false, &droppedstats);
1806
1807 /* XXX do we really need a critical section here? */
1809
1810 /* Write the ABORT record */
1811 if (isSubXact)
1812 xact_time = GetCurrentTimestamp();
1813 else
1814 {
1816 }
1817
1818 XactLogAbortRecord(xact_time,
1819 nchildren, children,
1820 nrels, rels,
1821 ndroppedstats, droppedstats,
1823 NULL);
1824
1825 if (replorigin)
1826 /* Move LSNs forward for this replication origin */
1829
1830 /*
1831 * Report the latest async abort LSN, so that the WAL writer knows to
1832 * flush this abort. There's nothing to be gained by delaying this, since
1833 * WALWriter may as well do this when it can. This is important with
1834 * streaming replication because if we don't flush WAL regularly we will
1835 * find that large aborts leave us with a long backlog for when commits
1836 * occur after the abort, increasing our window of data loss should
1837 * problems occur at that point.
1838 */
1839 if (!isSubXact)
1841
1842 /*
1843 * Mark the transaction aborted in clog. This is not absolutely necessary
1844 * but we may as well do it while we are here; also, in the subxact case
1845 * it is helpful because XactLockTableWait makes use of it to avoid
1846 * waiting for already-aborted subtransactions. It is OK to do it without
1847 * having flushed the ABORT record to disk, because in event of a crash
1848 * we'd be assumed to have aborted anyway.
1849 */
1850 TransactionIdAbortTree(xid, nchildren, children);
1851
1853
1854 /* Compute latestXid while we have the child XIDs handy */
1855 latestXid = TransactionIdLatest(xid, nchildren, children);
1856
1857 /*
1858 * If we're aborting a subtransaction, we can immediately remove failed
1859 * XIDs from PGPROC's cache of running child XIDs. We do that here for
1860 * subxacts, because we already have the child XID array at hand. For
1861 * main xacts, the equivalent happens just after this function returns.
1862 */
1863 if (isSubXact)
1864 XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1865
1866 /* Reset XactLastRecEnd until the next transaction writes something */
1867 if (!isSubXact)
1868 XactLastRecEnd = 0;
1869
1870 /* And clean up local data */
1871 if (rels)
1872 pfree(rels);
1873 if (ndroppedstats)
1874 pfree(droppedstats);
1875
1876 return latestXid;
1877}
1878
1879/*
1880 * AtAbort_Memory
1881 */
1882static void
1884{
1885 /*
1886 * Switch into TransactionAbortContext, which should have some free space
1887 * even if nothing else does. We'll work in this context until we've
1888 * finished cleaning up.
1889 *
1890 * It is barely possible to get here when we've not been able to create
1891 * TransactionAbortContext yet; if so use TopMemoryContext.
1892 */
1893 if (TransactionAbortContext != NULL)
1895 else
1897}
1898
1899/*
1900 * AtSubAbort_Memory
1901 */
1902static void
1904{
1906
1908}
1909
1910
1911/*
1912 * AtAbort_ResourceOwner
1913 */
1914static void
1916{
1917 /*
1918 * Make sure we have a valid ResourceOwner, if possible (else it will be
1919 * NULL, which is OK)
1920 */
1922}
1923
1924/*
1925 * AtSubAbort_ResourceOwner
1926 */
1927static void
1929{
1931
1932 /* Make sure we have a valid ResourceOwner */
1934}
1935
1936
1937/*
1938 * AtSubAbort_childXids
1939 */
1940static void
1942{
1944
1945 /*
1946 * We keep the child-XID arrays in TopTransactionContext (see
1947 * AtSubCommit_childXids). This means we'd better free the array
1948 * explicitly at abort to avoid leakage.
1949 */
1950 if (s->childXids != NULL)
1951 pfree(s->childXids);
1952 s->childXids = NULL;
1953 s->nChildXids = 0;
1954 s->maxChildXids = 0;
1955
1956 /*
1957 * We could prune the unreportedXids array here. But we don't bother. That
1958 * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1959 * would likely introduce more CPU time into the more common paths, so we
1960 * choose not to do that.
1961 */
1962}
1963
1964/* ----------------------------------------------------------------
1965 * CleanupTransaction stuff
1966 * ----------------------------------------------------------------
1967 */
1968
1969/*
1970 * AtCleanup_Memory
1971 */
1972static void
1974{
1976
1977 /* Should be at top level */
1978 Assert(s->parent == NULL);
1979
1980 /*
1981 * Return to the memory context that was current before we started the
1982 * transaction. (In principle, this could not be any of the contexts we
1983 * are about to delete. If it somehow is, assertions in mcxt.c will
1984 * complain.)
1985 */
1987
1988 /*
1989 * Clear the special abort context for next time.
1990 */
1991 if (TransactionAbortContext != NULL)
1993
1994 /*
1995 * Release all transaction-local memory, the same as in AtCommit_Memory,
1996 * except we must cope with the possibility that we didn't get as far as
1997 * creating TopTransactionContext.
1998 */
1999 if (TopTransactionContext != NULL)
2001
2002 /*
2003 * Clear these pointers as a pro-forma matter. (Notionally, while
2004 * TopTransactionContext still exists, it's currently not associated with
2005 * this TransactionState struct.)
2006 */
2007 CurTransactionContext = NULL;
2008 s->curTransactionContext = NULL;
2009}
2010
2011
2012/* ----------------------------------------------------------------
2013 * CleanupSubTransaction stuff
2014 * ----------------------------------------------------------------
2015 */
2016
2017/*
2018 * AtSubCleanup_Memory
2019 */
2020static void
2022{
2024
2025 Assert(s->parent != NULL);
2026
2027 /*
2028 * Return to the memory context that was current before we started the
2029 * subtransaction. (In principle, this could not be any of the contexts
2030 * we are about to delete. If it somehow is, assertions in mcxt.c will
2031 * complain.)
2032 */
2034
2035 /* Update CurTransactionContext (might not be same as priorContext) */
2037
2038 /*
2039 * Clear the special abort context for next time.
2040 */
2041 if (TransactionAbortContext != NULL)
2043
2044 /*
2045 * Delete the subxact local memory contexts. Its CurTransactionContext can
2046 * go too (note this also kills CurTransactionContexts from any children
2047 * of the subxact).
2048 */
2049 if (s->curTransactionContext)
2051 s->curTransactionContext = NULL;
2052}
2053
2054/* ----------------------------------------------------------------
2055 * interface routines
2056 * ----------------------------------------------------------------
2057 */
2058
2059/*
2060 * StartTransaction
2061 */
2062static void
2064{
2067
2068 /*
2069 * Let's just make sure the state stack is empty
2070 */
2073
2075
2076 /* check the current transaction state */
2077 Assert(s->state == TRANS_DEFAULT);
2078
2079 /*
2080 * Set the current transaction state information appropriately during
2081 * start processing. Note that once the transaction status is switched
2082 * this process cannot fail until the user ID and the security context
2083 * flags are fetched below.
2084 */
2085 s->state = TRANS_START;
2086 s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
2087
2088 /* Determine if statements are logged in this transaction */
2090 (log_xact_sample_rate == 1 ||
2092
2093 /*
2094 * initialize current transaction state fields
2095 *
2096 * note: prevXactReadOnly is not used at the outermost level
2097 */
2098 s->nestingLevel = 1;
2099 s->gucNestLevel = 1;
2100 s->childXids = NULL;
2101 s->nChildXids = 0;
2102 s->maxChildXids = 0;
2103
2104 /*
2105 * Once the current user ID and the security context flags are fetched,
2106 * both will be properly reset even if transaction startup fails.
2107 */
2109
2110 /* SecurityRestrictionContext should never be set outside a transaction */
2111 Assert(s->prevSecContext == 0);
2112
2113 /*
2114 * Make sure we've reset xact state variables
2115 *
2116 * If recovery is still in progress, mark this transaction as read-only.
2117 * We have lower level defences in XLogInsert and elsewhere to stop us
2118 * from modifying data during recovery, but this gives the normal
2119 * indication to the user that the transaction is read-only.
2120 */
2121 if (RecoveryInProgress())
2122 {
2123 s->startedInRecovery = true;
2124 XactReadOnly = true;
2125 }
2126 else
2127 {
2128 s->startedInRecovery = false;
2130 }
2133 forceSyncCommit = false;
2134 MyXactFlags = 0;
2135
2136 /*
2137 * reinitialize within-transaction counters
2138 */
2142 currentCommandIdUsed = false;
2143
2144 /*
2145 * initialize reported xid accounting
2146 */
2147 nUnreportedXids = 0;
2148 s->didLogXid = false;
2149
2150 /*
2151 * must initialize resource-management stuff first
2152 */
2155
2156 /*
2157 * Assign a new LocalTransactionId, and combine it with the proc number to
2158 * form a virtual transaction id.
2159 */
2160 vxid.procNumber = MyProcNumber;
2162
2163 /*
2164 * Lock the virtual transaction id before we announce it in the proc array
2165 */
2167
2168 /*
2169 * Advertise it in the proc array. We assume assignment of
2170 * localTransactionId is atomic, and the proc number should be set
2171 * already.
2172 */
2175
2176 TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
2177
2178 /*
2179 * set transaction_timestamp() (a/k/a now()). Normally, we want this to
2180 * be the same as the first command's statement_timestamp(), so don't do a
2181 * fresh GetCurrentTimestamp() call (which'd be expensive anyway). But
2182 * for transactions started inside procedures (i.e., nonatomic SPI
2183 * contexts), we do need to advance the timestamp. Also, in a parallel
2184 * worker, the timestamp should already have been provided by a call to
2185 * SetParallelStartTimestamps().
2186 */
2187 if (!IsParallelWorker())
2188 {
2191 else
2193 }
2194 else
2197 /* Mark xactStopTimestamp as unset. */
2199
2200 /*
2201 * initialize other subsystems for new transaction
2202 */
2203 AtStart_GUC();
2204 AtStart_Cache();
2206
2207 /*
2208 * done with start processing, set current transaction state to "in
2209 * progress"
2210 */
2212
2213 /* Schedule transaction timeout */
2214 if (TransactionTimeout > 0)
2216
2217 ShowTransactionState("StartTransaction");
2218}
2219
2220
2221/*
2222 * CommitTransaction
2223 *
2224 * NB: if you change this routine, better look at PrepareTransaction too!
2225 */
2226static void
2228{
2230 TransactionId latestXid;
2231 bool is_parallel_worker;
2232
2233 is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2234
2235 /* Enforce parallel mode restrictions during parallel worker commit. */
2236 if (is_parallel_worker)
2238
2239 ShowTransactionState("CommitTransaction");
2240
2241 /*
2242 * check the current transaction state
2243 */
2244 if (s->state != TRANS_INPROGRESS)
2245 elog(WARNING, "CommitTransaction while in %s state",
2247 Assert(s->parent == NULL);
2248
2249 /*
2250 * Do pre-commit processing that involves calling user-defined code, such
2251 * as triggers. SECURITY_RESTRICTED_OPERATION contexts must not queue an
2252 * action that would run here, because that would bypass the sandbox.
2253 * Since closing cursors could queue trigger actions, triggers could open
2254 * cursors, etc, we have to keep looping until there's nothing left to do.
2255 */
2256 for (;;)
2257 {
2258 /*
2259 * Fire all currently pending deferred triggers.
2260 */
2262
2263 /*
2264 * Close open portals (converting holdable ones into static portals).
2265 * If there weren't any, we are done ... otherwise loop back to check
2266 * if they queued deferred triggers. Lather, rinse, repeat.
2267 */
2268 if (!PreCommit_Portals(false))
2269 break;
2270 }
2271
2272 /*
2273 * The remaining actions cannot call any user-defined code, so it's safe
2274 * to start shutting down within-transaction services. But note that most
2275 * of this stuff could still throw an error, which would switch us into
2276 * the transaction-abort path.
2277 */
2278
2281
2282 /*
2283 * If this xact has started any unfinished parallel operation, clean up
2284 * its workers, warning about leaked resources. (But we don't actually
2285 * reset parallelModeLevel till entering TRANS_COMMIT, a bit below. This
2286 * keeps parallel mode restrictions active as long as possible in a
2287 * parallel worker.)
2288 */
2289 AtEOXact_Parallel(true);
2290 if (is_parallel_worker)
2291 {
2292 if (s->parallelModeLevel != 1)
2293 elog(WARNING, "parallelModeLevel is %d not 1 at end of parallel worker transaction",
2295 }
2296 else
2297 {
2298 if (s->parallelModeLevel != 0)
2299 elog(WARNING, "parallelModeLevel is %d not 0 at end of transaction",
2301 }
2302
2303 /* Shut down the deferred-trigger manager */
2304 AfterTriggerEndXact(true);
2305
2306 /*
2307 * Let ON COMMIT management do its thing (must happen after closing
2308 * cursors, to avoid dangling-reference problems)
2309 */
2311
2312 /*
2313 * Synchronize files that are created and not WAL-logged during this
2314 * transaction. This must happen before AtEOXact_RelationMap(), so that we
2315 * don't see committed-but-broken files after a crash.
2316 */
2317 smgrDoPendingSyncs(true, is_parallel_worker);
2318
2319 /* close large objects before lower-level cleanup */
2321
2322 /*
2323 * Insert notifications sent by NOTIFY commands into the queue. This
2324 * should be late in the pre-commit sequence to minimize time spent
2325 * holding the notify-insertion lock. However, this could result in
2326 * creating a snapshot, so we must do it before serializable cleanup.
2327 */
2329
2330 /*
2331 * Mark serializable transaction as complete for predicate locking
2332 * purposes. This should be done as late as we can put it and still allow
2333 * errors to be raised for failure patterns found at commit. This is not
2334 * appropriate in a parallel worker however, because we aren't committing
2335 * the leader's transaction and its serializable state will live on.
2336 */
2337 if (!is_parallel_worker)
2339
2340 /* Prevent cancel/die interrupt while cleaning up */
2342
2343 /* Commit updates to the relation map --- do this as late as possible */
2344 AtEOXact_RelationMap(true, is_parallel_worker);
2345
2346 /*
2347 * set the current transaction state information appropriately during
2348 * commit processing
2349 */
2350 s->state = TRANS_COMMIT;
2351 s->parallelModeLevel = 0;
2352 s->parallelChildXact = false; /* should be false already */
2353
2354 /* Disable transaction timeout */
2355 if (TransactionTimeout > 0)
2357
2358 if (!is_parallel_worker)
2359 {
2360 /*
2361 * We need to mark our XIDs as committed in pg_xact. This is where we
2362 * durably commit.
2363 */
2364 latestXid = RecordTransactionCommit();
2365 }
2366 else
2367 {
2368 /*
2369 * We must not mark our XID committed; the parallel leader is
2370 * responsible for that.
2371 */
2372 latestXid = InvalidTransactionId;
2373
2374 /*
2375 * Make sure the leader will know about any WAL we wrote before it
2376 * commits.
2377 */
2379 }
2380
2381 TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->vxid.lxid);
2382
2383 /*
2384 * Let others know about no transaction in progress by me. Note that this
2385 * must be done _before_ releasing locks we hold and _after_
2386 * RecordTransactionCommit.
2387 */
2388 ProcArrayEndTransaction(MyProc, latestXid);
2389
2390 /*
2391 * This is all post-commit cleanup. Note that if an error is raised here,
2392 * it's too late to abort the transaction. This should be just
2393 * noncritical resource releasing.
2394 *
2395 * The ordering of operations is not entirely random. The idea is:
2396 * release resources visible to other backends (eg, files, buffer pins);
2397 * then release locks; then release backend-local resources. We want to
2398 * release locks at the point where any backend waiting for us will see
2399 * our transaction as being fully cleaned up.
2400 *
2401 * Resources that can be associated with individual queries are handled by
2402 * the ResourceOwner mechanism. The other calls here are for backend-wide
2403 * state.
2404 */
2405
2408
2409 CurrentResourceOwner = NULL;
2412 true, true);
2413
2414 /* Check we've released all buffer pins */
2415 AtEOXact_Buffers(true);
2416
2417 /* Clean up the relation cache */
2419
2420 /* Clean up the type cache */
2422
2423 /*
2424 * Make catalog changes visible to all backends. This has to happen after
2425 * relcache references are dropped (see comments for
2426 * AtEOXact_RelationCache), but before locks are released (if anyone is
2427 * waiting for lock on a relation we've modified, we want them to know
2428 * about the catalog change before they start using the relation).
2429 */
2430 AtEOXact_Inval(true);
2431
2433
2436 true, true);
2439 true, true);
2440
2441 /*
2442 * Likewise, dropping of files deleted during the transaction is best done
2443 * after releasing relcache and buffer pins. (This is not strictly
2444 * necessary during commit, since such pins should have been released
2445 * already, but this ordering is definitely critical during abort.) Since
2446 * this may take many seconds, also delay until after releasing locks.
2447 * Other backends will observe the attendant catalog changes and not
2448 * attempt to access affected files.
2449 */
2451
2452 /*
2453 * Send out notification signals to other backends (and do other
2454 * post-commit NOTIFY cleanup). This must not happen until after our
2455 * transaction is fully done from the viewpoint of other backends.
2456 */
2458
2459 /*
2460 * Everything after this should be purely internal-to-this-backend
2461 * cleanup.
2462 */
2463 AtEOXact_GUC(true, 1);
2464 AtEOXact_SPI(true);
2465 AtEOXact_Enum();
2467 AtEOXact_Namespace(true, is_parallel_worker);
2468 AtEOXact_SMgr();
2469 AtEOXact_Files(true);
2471 AtEOXact_HashTables(true);
2472 AtEOXact_PgStat(true, is_parallel_worker);
2473 AtEOXact_Snapshot(true, false);
2477
2479 s->curTransactionOwner = NULL;
2482
2484
2487 s->nestingLevel = 0;
2488 s->gucNestLevel = 0;
2489 s->childXids = NULL;
2490 s->nChildXids = 0;
2491 s->maxChildXids = 0;
2492
2495
2496 /*
2497 * done with commit processing, set current transaction state back to
2498 * default
2499 */
2500 s->state = TRANS_DEFAULT;
2501
2503}
2504
2505
2506/*
2507 * PrepareTransaction
2508 *
2509 * NB: if you change this routine, better look at CommitTransaction too!
2510 */
2511static void
2513{
2516 GlobalTransaction gxact;
2517 TimestampTz prepared_at;
2518
2520
2521 ShowTransactionState("PrepareTransaction");
2522
2523 /*
2524 * check the current transaction state
2525 */
2526 if (s->state != TRANS_INPROGRESS)
2527 elog(WARNING, "PrepareTransaction while in %s state",
2529 Assert(s->parent == NULL);
2530
2531 /*
2532 * Do pre-commit processing that involves calling user-defined code, such
2533 * as triggers. Since closing cursors could queue trigger actions,
2534 * triggers could open cursors, etc, we have to keep looping until there's
2535 * nothing left to do.
2536 */
2537 for (;;)
2538 {
2539 /*
2540 * Fire all currently pending deferred triggers.
2541 */
2543
2544 /*
2545 * Close open portals (converting holdable ones into static portals).
2546 * If there weren't any, we are done ... otherwise loop back to check
2547 * if they queued deferred triggers. Lather, rinse, repeat.
2548 */
2549 if (!PreCommit_Portals(true))
2550 break;
2551 }
2552
2554
2555 /*
2556 * The remaining actions cannot call any user-defined code, so it's safe
2557 * to start shutting down within-transaction services. But note that most
2558 * of this stuff could still throw an error, which would switch us into
2559 * the transaction-abort path.
2560 */
2561
2562 /* Shut down the deferred-trigger manager */
2563 AfterTriggerEndXact(true);
2564
2565 /*
2566 * Let ON COMMIT management do its thing (must happen after closing
2567 * cursors, to avoid dangling-reference problems)
2568 */
2570
2571 /*
2572 * Synchronize files that are created and not WAL-logged during this
2573 * transaction. This must happen before EndPrepare(), so that we don't see
2574 * committed-but-broken files after a crash and COMMIT PREPARED.
2575 */
2576 smgrDoPendingSyncs(true, false);
2577
2578 /* close large objects before lower-level cleanup */
2580
2581 /* NOTIFY requires no work at this point */
2582
2583 /*
2584 * Mark serializable transaction as complete for predicate locking
2585 * purposes. This should be done as late as we can put it and still allow
2586 * errors to be raised for failure patterns found at commit.
2587 */
2589
2590 /*
2591 * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2592 * this transaction. Having the prepared xact hold locks on another
2593 * backend's temp table seems a bad idea --- for instance it would prevent
2594 * the backend from exiting. There are other problems too, such as how to
2595 * clean up the source backend's local buffers and ON COMMIT state if the
2596 * prepared xact includes a DROP of a temp table.
2597 *
2598 * Other objects types, like functions, operators or extensions, share the
2599 * same restriction as they should not be created, locked or dropped as
2600 * this can mess up with this session or even a follow-up session trying
2601 * to use the same temporary namespace.
2602 *
2603 * We must check this after executing any ON COMMIT actions, because they
2604 * might still access a temp relation.
2605 *
2606 * XXX In principle this could be relaxed to allow some useful special
2607 * cases, such as a temp table created and dropped all within the
2608 * transaction. That seems to require much more bookkeeping though.
2609 */
2611 ereport(ERROR,
2612 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2613 errmsg("cannot PREPARE a transaction that has operated on temporary objects")));
2614
2615 /*
2616 * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2617 * supported if we added cleanup logic to twophase.c, but for now it
2618 * doesn't seem worth the trouble.
2619 */
2621 ereport(ERROR,
2622 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2623 errmsg("cannot PREPARE a transaction that has exported snapshots")));
2624
2625 /* Prevent cancel/die interrupt while cleaning up */
2627
2628 /*
2629 * set the current transaction state information appropriately during
2630 * prepare processing
2631 */
2632 s->state = TRANS_PREPARE;
2633
2634 /* Disable transaction timeout */
2635 if (TransactionTimeout > 0)
2637
2638 prepared_at = GetCurrentTimestamp();
2639
2640 /*
2641 * Reserve the GID for this transaction. This could fail if the requested
2642 * GID is invalid or already in use.
2643 */
2644 gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2646 prepareGID = NULL;
2647
2648 /*
2649 * Collect data for the 2PC state file. Note that in general, no actual
2650 * state change should happen in the called modules during this step,
2651 * since it's still possible to fail before commit, and in that case we
2652 * want transaction abort to be able to clean up. (In particular, the
2653 * AtPrepare routines may error out if they find cases they cannot
2654 * handle.) State cleanup should happen in the PostPrepare routines
2655 * below. However, some modules can go ahead and clear state here because
2656 * they wouldn't do anything with it during abort anyway.
2657 *
2658 * Note: because the 2PC state file records will be replayed in the same
2659 * order they are made, the order of these calls has to match the order in
2660 * which we want things to happen during COMMIT PREPARED or ROLLBACK
2661 * PREPARED; in particular, pay attention to whether things should happen
2662 * before or after releasing the transaction's locks.
2663 */
2664 StartPrepare(gxact);
2665
2672
2673 /*
2674 * Here is where we really truly prepare.
2675 *
2676 * We have to record transaction prepares even if we didn't make any
2677 * updates, because the transaction manager might get confused if we lose
2678 * a global transaction.
2679 */
2680 EndPrepare(gxact);
2681
2682 /*
2683 * Now we clean up backend-internal state and release internal resources.
2684 */
2685
2686 /* Reset XactLastRecEnd until the next transaction writes something */
2687 XactLastRecEnd = 0;
2688
2689 /*
2690 * Transfer our locks to a dummy PGPROC. This has to be done before
2691 * ProcArrayClearTransaction(). Otherwise, a GetLockConflicts() would
2692 * conclude "xact already committed or aborted" for our locks.
2693 */
2694 PostPrepare_Locks(xid);
2695
2696 /*
2697 * Let others know about no transaction in progress by me. This has to be
2698 * done *after* the prepared transaction has been marked valid, else
2699 * someone may think it is unlocked and recyclable.
2700 */
2702
2703 /*
2704 * In normal commit-processing, this is all non-critical post-transaction
2705 * cleanup. When the transaction is prepared, however, it's important
2706 * that the locks and other per-backend resources are transferred to the
2707 * prepared transaction's PGPROC entry. Note that if an error is raised
2708 * here, it's too late to abort the transaction. XXX: This probably should
2709 * be in a critical section, to force a PANIC if any of this fails, but
2710 * that cure could be worse than the disease.
2711 */
2712
2714
2717 true, true);
2718
2719 /* Check we've released all buffer pins */
2720 AtEOXact_Buffers(true);
2721
2722 /* Clean up the relation cache */
2724
2725 /* Clean up the type cache */
2727
2728 /* notify doesn't need a postprepare call */
2729
2731
2733
2735
2737
2739
2742 true, true);
2745 true, true);
2746
2747 /*
2748 * Allow another backend to finish the transaction. After
2749 * PostPrepare_Twophase(), the transaction is completely detached from our
2750 * backend. The rest is just non-critical cleanup of backend-local state.
2751 */
2753
2754 /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2755 AtEOXact_GUC(true, 1);
2756 AtEOXact_SPI(true);
2757 AtEOXact_Enum();
2759 AtEOXact_Namespace(true, false);
2760 AtEOXact_SMgr();
2761 AtEOXact_Files(true);
2763 AtEOXact_HashTables(true);
2764 /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2765 AtEOXact_Snapshot(true, true);
2766 /* we treat PREPARE as ROLLBACK so far as waking workers goes */
2770
2771 CurrentResourceOwner = NULL;
2773 s->curTransactionOwner = NULL;
2776
2778
2781 s->nestingLevel = 0;
2782 s->gucNestLevel = 0;
2783 s->childXids = NULL;
2784 s->nChildXids = 0;
2785 s->maxChildXids = 0;
2786
2789
2790 /*
2791 * done with 1st phase commit processing, set current transaction state
2792 * back to default
2793 */
2794 s->state = TRANS_DEFAULT;
2795
2797}
2798
2799
2800/*
2801 * AbortTransaction
2802 */
2803static void
2805{
2807 TransactionId latestXid;
2808 bool is_parallel_worker;
2809
2810 /* Prevent cancel/die interrupt while cleaning up */
2812
2813 /* Disable transaction timeout */
2814 if (TransactionTimeout > 0)
2816
2817 /* Make sure we have a valid memory context and resource owner */
2820
2821 /*
2822 * Release any LW locks we might be holding as quickly as possible.
2823 * (Regular locks, however, must be held till we finish aborting.)
2824 * Releasing LW locks is critical since we might try to grab them again
2825 * while cleaning up!
2826 */
2828
2829 /* Clear wait information and command progress indicator */
2832
2833 /* Clean up buffer content locks, too */
2834 UnlockBuffers();
2835
2836 /* Reset WAL record construction state */
2838
2839 /* Cancel condition variable sleep */
2841
2842 /*
2843 * Also clean up any open wait for lock, since the lock manager will choke
2844 * if we try to wait for another lock before doing this.
2845 */
2847
2848 /*
2849 * If any timeout events are still active, make sure the timeout interrupt
2850 * is scheduled. This covers possible loss of a timeout interrupt due to
2851 * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2852 * We delay this till after LockErrorCleanup so that we don't uselessly
2853 * reschedule lock or deadlock check timeouts.
2854 */
2856
2857 /*
2858 * Re-enable signals, in case we got here by longjmp'ing out of a signal
2859 * handler. We do this fairly early in the sequence so that the timeout
2860 * infrastructure will be functional if needed while aborting.
2861 */
2862 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
2863
2864 /*
2865 * check the current transaction state
2866 */
2867 is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2868 if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2869 elog(WARNING, "AbortTransaction while in %s state",
2871 Assert(s->parent == NULL);
2872
2873 /*
2874 * set the current transaction state information appropriately during the
2875 * abort processing
2876 */
2877 s->state = TRANS_ABORT;
2878
2879 /*
2880 * Reset user ID which might have been changed transiently. We need this
2881 * to clean up in case control escaped out of a SECURITY DEFINER function
2882 * or other local change of CurrentUserId; therefore, the prior value of
2883 * SecurityRestrictionContext also needs to be restored.
2884 *
2885 * (Note: it is not necessary to restore session authorization or role
2886 * settings here because those can only be changed via GUC, and GUC will
2887 * take care of rolling them back if need be.)
2888 */
2890
2891 /* Forget about any active REINDEX. */
2893
2894 /* Reset logical streaming state. */
2896
2897 /* Reset snapshot export state. */
2899
2900 /*
2901 * If this xact has started any unfinished parallel operation, clean up
2902 * its workers and exit parallel mode. Don't warn about leaked resources.
2903 */
2904 AtEOXact_Parallel(false);
2905 s->parallelModeLevel = 0;
2906 s->parallelChildXact = false; /* should be false already */
2907
2908 /*
2909 * do abort processing
2910 */
2911 AfterTriggerEndXact(false); /* 'false' means it's abort */
2913 smgrDoPendingSyncs(false, is_parallel_worker);
2914 AtEOXact_LargeObject(false);
2916 AtEOXact_RelationMap(false, is_parallel_worker);
2918
2919 /*
2920 * Advertise the fact that we aborted in pg_xact (assuming that we got as
2921 * far as assigning an XID to advertise). But if we're inside a parallel
2922 * worker, skip this; the user backend must be the one to write the abort
2923 * record.
2924 */
2925 if (!is_parallel_worker)
2926 latestXid = RecordTransactionAbort(false);
2927 else
2928 {
2929 latestXid = InvalidTransactionId;
2930
2931 /*
2932 * Since the parallel leader won't get our value of XactLastRecEnd in
2933 * this case, we nudge WAL-writer ourselves in this case. See related
2934 * comments in RecordTransactionAbort for why this matters.
2935 */
2937 }
2938
2939 TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->vxid.lxid);
2940
2941 /*
2942 * Let others know about no transaction in progress by me. Note that this
2943 * must be done _before_ releasing locks we hold and _after_
2944 * RecordTransactionAbort.
2945 */
2946 ProcArrayEndTransaction(MyProc, latestXid);
2947
2948 /*
2949 * Post-abort cleanup. See notes in CommitTransaction() concerning
2950 * ordering. We can skip all of it if the transaction failed before
2951 * creating a resource owner.
2952 */
2953 if (TopTransactionResourceOwner != NULL)
2954 {
2955 if (is_parallel_worker)
2957 else
2959
2962 false, true);
2963 AtEOXact_Buffers(false);
2966 AtEOXact_Inval(false);
2970 false, true);
2973 false, true);
2974 smgrDoPendingDeletes(false);
2975
2976 AtEOXact_GUC(false, 1);
2977 AtEOXact_SPI(false);
2978 AtEOXact_Enum();
2980 AtEOXact_Namespace(false, is_parallel_worker);
2981 AtEOXact_SMgr();
2982 AtEOXact_Files(false);
2984 AtEOXact_HashTables(false);
2985 AtEOXact_PgStat(false, is_parallel_worker);
2989 }
2990
2991 /*
2992 * State remains TRANS_ABORT until CleanupTransaction().
2993 */
2995}
2996
2997/*
2998 * CleanupTransaction
2999 */
3000static void
3002{
3004
3005 /*
3006 * State should still be TRANS_ABORT from AbortTransaction().
3007 */
3008 if (s->state != TRANS_ABORT)
3009 elog(FATAL, "CleanupTransaction: unexpected state %s",
3011
3012 /*
3013 * do abort cleanup processing
3014 */
3015 AtCleanup_Portals(); /* now safe to release portal memory */
3016 AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
3017
3018 CurrentResourceOwner = NULL; /* and resource owner */
3021 s->curTransactionOwner = NULL;
3024
3025 AtCleanup_Memory(); /* and transaction memory */
3026
3029 s->nestingLevel = 0;
3030 s->gucNestLevel = 0;
3031 s->childXids = NULL;
3032 s->nChildXids = 0;
3033 s->maxChildXids = 0;
3034 s->parallelModeLevel = 0;
3035 s->parallelChildXact = false;
3036
3039
3040 /*
3041 * done with abort processing, set current transaction state back to
3042 * default
3043 */
3044 s->state = TRANS_DEFAULT;
3045}
3046
3047/*
3048 * StartTransactionCommand
3049 */
3050void
3052{
3054
3055 switch (s->blockState)
3056 {
3057 /*
3058 * if we aren't in a transaction block, we just do our usual start
3059 * transaction.
3060 */
3061 case TBLOCK_DEFAULT:
3064 break;
3065
3066 /*
3067 * We are somewhere in a transaction block or subtransaction and
3068 * about to start a new command. For now we do nothing, but
3069 * someday we may do command-local resource initialization. (Note
3070 * that any needed CommandCounterIncrement was done by the
3071 * previous CommitTransactionCommand.)
3072 */
3073 case TBLOCK_INPROGRESS:
3076 break;
3077
3078 /*
3079 * Here we are in a failed transaction block (one of the commands
3080 * caused an abort) so we do nothing but remain in the abort
3081 * state. Eventually we will get a ROLLBACK command which will
3082 * get us out of this state. (It is up to other code to ensure
3083 * that no commands other than ROLLBACK will be processed in these
3084 * states.)
3085 */
3086 case TBLOCK_ABORT:
3087 case TBLOCK_SUBABORT:
3088 break;
3089
3090 /* These cases are invalid. */
3091 case TBLOCK_STARTED:
3092 case TBLOCK_BEGIN:
3094 case TBLOCK_SUBBEGIN:
3095 case TBLOCK_END:
3096 case TBLOCK_SUBRELEASE:
3097 case TBLOCK_SUBCOMMIT:
3098 case TBLOCK_ABORT_END:
3102 case TBLOCK_SUBRESTART:
3104 case TBLOCK_PREPARE:
3105 elog(ERROR, "StartTransactionCommand: unexpected state %s",
3107 break;
3108 }
3109
3110 /*
3111 * We must switch to CurTransactionContext before returning. This is
3112 * already done if we called StartTransaction, otherwise not.
3113 */
3116}
3117
3118
3119/*
3120 * Simple system for saving and restoring transaction characteristics
3121 * (isolation level, read only, deferrable). We need this for transaction
3122 * chaining, so that we can set the characteristics of the new transaction to
3123 * be the same as the previous one. (We need something like this because the
3124 * GUC system resets the characteristics at transaction end, so for example
3125 * just skipping the reset in StartTransaction() won't work.)
3126 */
3127void
3129{
3133}
3134
3135void
3137{
3141}
3142
3143/*
3144 * CommitTransactionCommand -- a wrapper function handling the
3145 * loop over subtransactions to avoid a potentially dangerous recursion
3146 * in CommitTransactionCommandInternal().
3147 */
3148void
3150{
3151 /*
3152 * Repeatedly call CommitTransactionCommandInternal() until all the work
3153 * is done.
3154 */
3156 {
3157 }
3158}
3159
3160/*
3161 * CommitTransactionCommandInternal - a function doing an iteration of work
3162 * regarding handling the commit transaction command. In the case of
3163 * subtransactions more than one iterations could be required. Returns
3164 * true when no more iterations required, false otherwise.
3165 */
3166static bool
3168{
3171
3172 /* Must save in case we need to restore below */
3174
3175 switch (s->blockState)
3176 {
3177 /*
3178 * These shouldn't happen. TBLOCK_DEFAULT means the previous
3179 * StartTransactionCommand didn't set the STARTED state
3180 * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
3181 * by EndParallelWorkerTransaction(), not this function.
3182 */
3183 case TBLOCK_DEFAULT:
3185 elog(FATAL, "CommitTransactionCommand: unexpected state %s",
3187 break;
3188
3189 /*
3190 * If we aren't in a transaction block, just do our usual
3191 * transaction commit, and return to the idle state.
3192 */
3193 case TBLOCK_STARTED:
3196 break;
3197
3198 /*
3199 * We are completing a "BEGIN TRANSACTION" command, so we change
3200 * to the "transaction block in progress" state and return. (We
3201 * assume the BEGIN did nothing to the database, so we need no
3202 * CommandCounterIncrement.)
3203 */
3204 case TBLOCK_BEGIN:
3206 break;
3207
3208 /*
3209 * This is the case when we have finished executing a command
3210 * someplace within a transaction block. We increment the command
3211 * counter and return.
3212 */
3213 case TBLOCK_INPROGRESS:
3217 break;
3218
3219 /*
3220 * We are completing a "COMMIT" command. Do it and return to the
3221 * idle state.
3222 */
3223 case TBLOCK_END:
3226 if (s->chain)
3227 {
3230 s->chain = false;
3232 }
3233 break;
3234
3235 /*
3236 * Here we are in the middle of a transaction block but one of the
3237 * commands caused an abort so we do nothing but remain in the
3238 * abort state. Eventually we will get a ROLLBACK command.
3239 */
3240 case TBLOCK_ABORT:
3241 case TBLOCK_SUBABORT:
3242 break;
3243
3244 /*
3245 * Here we were in an aborted transaction block and we just got
3246 * the ROLLBACK command from the user, so clean up the
3247 * already-aborted transaction and return to the idle state.
3248 */
3249 case TBLOCK_ABORT_END:
3252 if (s->chain)
3253 {
3256 s->chain = false;
3258 }
3259 break;
3260
3261 /*
3262 * Here we were in a perfectly good transaction block but the user
3263 * told us to ROLLBACK anyway. We have to abort the transaction
3264 * and then clean up.
3265 */
3270 if (s->chain)
3271 {
3274 s->chain = false;
3276 }
3277 break;
3278
3279 /*
3280 * We are completing a "PREPARE TRANSACTION" command. Do it and
3281 * return to the idle state.
3282 */
3283 case TBLOCK_PREPARE:
3286 break;
3287
3288 /*
3289 * The user issued a SAVEPOINT inside a transaction block. Start a
3290 * subtransaction. (DefineSavepoint already did PushTransaction,
3291 * so as to have someplace to put the SUBBEGIN state.)
3292 */
3293 case TBLOCK_SUBBEGIN:
3296 break;
3297
3298 /*
3299 * The user issued a RELEASE command, so we end the current
3300 * subtransaction and return to the parent transaction. The parent
3301 * might be ended too, so repeat till we find an INPROGRESS
3302 * transaction or subtransaction.
3303 */
3304 case TBLOCK_SUBRELEASE:
3305 do
3306 {
3308 s = CurrentTransactionState; /* changed by pop */
3309 } while (s->blockState == TBLOCK_SUBRELEASE);
3310
3313 break;
3314
3315 /*
3316 * The user issued a COMMIT, so we end the current subtransaction
3317 * hierarchy and perform final commit. We do this by rolling up
3318 * any subtransactions into their parent, which leads to O(N^2)
3319 * operations with respect to resource owners - this isn't that
3320 * bad until we approach a thousands of savepoints but is
3321 * necessary for correctness should after triggers create new
3322 * resource owners.
3323 */
3324 case TBLOCK_SUBCOMMIT:
3325 do
3326 {
3328 s = CurrentTransactionState; /* changed by pop */
3329 } while (s->blockState == TBLOCK_SUBCOMMIT);
3330 /* If we had a COMMIT command, finish off the main xact too */
3331 if (s->blockState == TBLOCK_END)
3332 {
3333 Assert(s->parent == NULL);
3336 if (s->chain)
3337 {
3340 s->chain = false;
3342 }
3343 }
3344 else if (s->blockState == TBLOCK_PREPARE)
3345 {
3346 Assert(s->parent == NULL);
3349 }
3350 else
3351 elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3353 break;
3354
3355 /*
3356 * The current already-failed subtransaction is ending due to a
3357 * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3358 * examine the parent (which could be in any of several states).
3359 * As we need to examine the parent, return false to request the
3360 * caller to do the next iteration.
3361 */
3364 return false;
3365
3366 /*
3367 * As above, but it's not dead yet, so abort first.
3368 */
3372 return false;
3373
3374 /*
3375 * The current subtransaction is the target of a ROLLBACK TO
3376 * command. Abort and pop it, then start a new subtransaction
3377 * with the same name.
3378 */
3379 case TBLOCK_SUBRESTART:
3380 {
3381 char *name;
3382 int savepointLevel;
3383
3384 /* save name and keep Cleanup from freeing it */
3385 name = s->name;
3386 s->name = NULL;
3387 savepointLevel = s->savepointLevel;
3388
3391
3392 DefineSavepoint(NULL);
3393 s = CurrentTransactionState; /* changed by push */
3394 s->name = name;
3395 s->savepointLevel = savepointLevel;
3396
3397 /* This is the same as TBLOCK_SUBBEGIN case */
3401 }
3402 break;
3403
3404 /*
3405 * Same as above, but the subtransaction had already failed, so we
3406 * don't need AbortSubTransaction.
3407 */
3409 {
3410 char *name;
3411 int savepointLevel;
3412
3413 /* save name and keep Cleanup from freeing it */
3414 name = s->name;
3415 s->name = NULL;
3416 savepointLevel = s->savepointLevel;
3417
3419
3420 DefineSavepoint(NULL);
3421 s = CurrentTransactionState; /* changed by push */
3422 s->name = name;
3423 s->savepointLevel = savepointLevel;
3424
3425 /* This is the same as TBLOCK_SUBBEGIN case */
3429 }
3430 break;
3431 }
3432
3433 /* Done, no more iterations required */
3434 return true;
3435}
3436
3437/*
3438 * AbortCurrentTransaction -- a wrapper function handling the
3439 * loop over subtransactions to avoid potentially dangerous recursion in
3440 * AbortCurrentTransactionInternal().
3441 */
3442void
3444{
3445 /*
3446 * Repeatedly call AbortCurrentTransactionInternal() until all the work is
3447 * done.
3448 */
3450 {
3451 }
3452}
3453
3454/*
3455 * AbortCurrentTransactionInternal - a function doing an iteration of work
3456 * regarding handling the current transaction abort. In the case of
3457 * subtransactions more than one iterations could be required. Returns
3458 * true when no more iterations required, false otherwise.
3459 */
3460static bool
3462{
3464
3465 switch (s->blockState)
3466 {
3467 case TBLOCK_DEFAULT:
3468 if (s->state == TRANS_DEFAULT)
3469 {
3470 /* we are idle, so nothing to do */
3471 }
3472 else
3473 {
3474 /*
3475 * We can get here after an error during transaction start
3476 * (state will be TRANS_START). Need to clean up the
3477 * incompletely started transaction. First, adjust the
3478 * low-level state to suppress warning message from
3479 * AbortTransaction.
3480 */
3481 if (s->state == TRANS_START)
3485 }
3486 break;
3487
3488 /*
3489 * If we aren't in a transaction block, we just do the basic abort
3490 * & cleanup transaction. For this purpose, we treat an implicit
3491 * transaction block as if it were a simple statement.
3492 */
3493 case TBLOCK_STARTED:
3498 break;
3499
3500 /*
3501 * If we are in TBLOCK_BEGIN it means something screwed up right
3502 * after reading "BEGIN TRANSACTION". We assume that the user
3503 * will interpret the error as meaning the BEGIN failed to get him
3504 * into a transaction block, so we should abort and return to idle
3505 * state.
3506 */
3507 case TBLOCK_BEGIN:
3511 break;
3512
3513 /*
3514 * We are somewhere in a transaction block and we've gotten a
3515 * failure, so we abort the transaction and set up the persistent
3516 * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3517 */
3518 case TBLOCK_INPROGRESS:
3522 /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3523 break;
3524
3525 /*
3526 * Here, we failed while trying to COMMIT. Clean up the
3527 * transaction and return to idle state (we do not want to stay in
3528 * the transaction).
3529 */
3530 case TBLOCK_END:
3534 break;
3535
3536 /*
3537 * Here, we are already in an aborted transaction state and are
3538 * waiting for a ROLLBACK, but for some reason we failed again! So
3539 * we just remain in the abort state.
3540 */
3541 case TBLOCK_ABORT:
3542 case TBLOCK_SUBABORT:
3543 break;
3544
3545 /*
3546 * We are in a failed transaction and we got the ROLLBACK command.
3547 * We have already aborted, we just need to cleanup and go to idle
3548 * state.
3549 */
3550 case TBLOCK_ABORT_END:
3553 break;
3554
3555 /*
3556 * We are in a live transaction and we got a ROLLBACK command.
3557 * Abort, cleanup, go to idle state.
3558 */
3563 break;
3564
3565 /*
3566 * Here, we failed while trying to PREPARE. Clean up the
3567 * transaction and return to idle state (we do not want to stay in
3568 * the transaction).
3569 */
3570 case TBLOCK_PREPARE:
3574 break;
3575
3576 /*
3577 * We got an error inside a subtransaction. Abort just the
3578 * subtransaction, and go to the persistent SUBABORT state until
3579 * we get ROLLBACK.
3580 */
3584 break;
3585
3586 /*
3587 * If we failed while trying to create a subtransaction, clean up
3588 * the broken subtransaction and abort the parent. The same
3589 * applies if we get a failure while ending a subtransaction. As
3590 * we need to abort the parent, return false to request the caller
3591 * to do the next iteration.
3592 */
3593 case TBLOCK_SUBBEGIN:
3594 case TBLOCK_SUBRELEASE:
3595 case TBLOCK_SUBCOMMIT:
3597 case TBLOCK_SUBRESTART:
3600 return false;
3601
3602 /*
3603 * Same as above, except the Abort() was already done.
3604 */
3608 return false;
3609 }
3610
3611 /* Done, no more iterations required */
3612 return true;
3613}
3614
3615/*
3616 * PreventInTransactionBlock
3617 *
3618 * This routine is to be called by statements that must not run inside
3619 * a transaction block, typically because they have non-rollback-able
3620 * side effects or do internal commits.
3621 *
3622 * If this routine completes successfully, then the calling statement is
3623 * guaranteed that if it completes without error, its results will be
3624 * committed immediately.
3625 *
3626 * If we have already started a transaction block, issue an error; also issue
3627 * an error if we appear to be running inside a user-defined function (which
3628 * could issue more commands and possibly cause a failure after the statement
3629 * completes). Subtransactions are verboten too.
3630 *
3631 * We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
3632 * that postgres.c follows through by committing after the statement is done.
3633 *
3634 * isTopLevel: passed down from ProcessUtility to determine whether we are
3635 * inside a function. (We will always fail if this is false, but it's
3636 * convenient to centralize the check here instead of making callers do it.)
3637 * stmtType: statement type name, for error messages.
3638 */
3639void
3640PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
3641{
3642 /*
3643 * xact block already started?
3644 */
3645 if (IsTransactionBlock())
3646 ereport(ERROR,
3647 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3648 /* translator: %s represents an SQL statement name */
3649 errmsg("%s cannot run inside a transaction block",
3650 stmtType)));
3651
3652 /*
3653 * subtransaction?
3654 */
3655 if (IsSubTransaction())
3656 ereport(ERROR,
3657 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3658 /* translator: %s represents an SQL statement name */
3659 errmsg("%s cannot run inside a subtransaction",
3660 stmtType)));
3661
3662 /*
3663 * inside a function call?
3664 */
3665 if (!isTopLevel)
3666 ereport(ERROR,
3667 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3668 /* translator: %s represents an SQL statement name */
3669 errmsg("%s cannot be executed from a function", stmtType)));
3670
3671 /* If we got past IsTransactionBlock test, should be in default state */
3674 elog(FATAL, "cannot prevent transaction chain");
3675
3676 /* All okay. Set the flag to make sure the right thing happens later. */
3678}
3679
3680/*
3681 * WarnNoTransactionBlock
3682 * RequireTransactionBlock
3683 *
3684 * These two functions allow for warnings or errors if a command is executed
3685 * outside of a transaction block. This is useful for commands that have no
3686 * effects that persist past transaction end (and so calling them outside a
3687 * transaction block is presumably an error). DECLARE CURSOR is an example.
3688 * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and SET
3689 * that have no effect issue warnings, all other no-effect commands generate
3690 * errors.
3691 *
3692 * If we appear to be running inside a user-defined function, we do not
3693 * issue anything, since the function could issue more commands that make
3694 * use of the current statement's results. Likewise subtransactions.
3695 * Thus these are inverses for PreventInTransactionBlock.
3696 *
3697 * isTopLevel: passed down from ProcessUtility to determine whether we are
3698 * inside a function.
3699 * stmtType: statement type name, for warning or error messages.
3700 */
3701void
3702WarnNoTransactionBlock(bool isTopLevel, const char *stmtType)
3703{
3704 CheckTransactionBlock(isTopLevel, false, stmtType);
3705}
3706
3707void
3708RequireTransactionBlock(bool isTopLevel, const char *stmtType)
3709{
3710 CheckTransactionBlock(isTopLevel, true, stmtType);
3711}
3712
3713/*
3714 * This is the implementation of the above two.
3715 */
3716static void
3717CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
3718{
3719 /*
3720 * xact block already started?
3721 */
3722 if (IsTransactionBlock())
3723 return;
3724
3725 /*
3726 * subtransaction?
3727 */
3728 if (IsSubTransaction())
3729 return;
3730
3731 /*
3732 * inside a function call?
3733 */
3734 if (!isTopLevel)
3735 return;
3736
3737 ereport(throwError ? ERROR : WARNING,
3738 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3739 /* translator: %s represents an SQL statement name */
3740 errmsg("%s can only be used in transaction blocks",
3741 stmtType)));
3742}
3743
3744/*
3745 * IsInTransactionBlock
3746 *
3747 * This routine is for statements that need to behave differently inside
3748 * a transaction block than when running as single commands. ANALYZE is
3749 * currently the only example.
3750 *
3751 * If this routine returns "false", then the calling statement is allowed
3752 * to perform internal transaction-commit-and-start cycles; there is not a
3753 * risk of messing up any transaction already in progress. (Note that this
3754 * is not the identical guarantee provided by PreventInTransactionBlock,
3755 * since we will not force a post-statement commit.)
3756 *
3757 * isTopLevel: passed down from ProcessUtility to determine whether we are
3758 * inside a function.
3759 */
3760bool
3761IsInTransactionBlock(bool isTopLevel)
3762{
3763 /*
3764 * Return true on same conditions that would make
3765 * PreventInTransactionBlock error out
3766 */
3767 if (IsTransactionBlock())
3768 return true;
3769
3770 if (IsSubTransaction())
3771 return true;
3772
3773 if (!isTopLevel)
3774 return true;
3775
3778 return true;
3779
3780 return false;
3781}
3782
3783
3784/*
3785 * Register or deregister callback functions for start- and end-of-xact
3786 * operations.
3787 *
3788 * These functions are intended for use by dynamically loaded modules.
3789 * For built-in modules we generally just hardwire the appropriate calls
3790 * (mainly because it's easier to control the order that way, where needed).
3791 *
3792 * At transaction end, the callback occurs post-commit or post-abort, so the
3793 * callback functions can only do noncritical cleanup.
3794 */
3795void
3797{
3798 XactCallbackItem *item;
3799
3800 item = (XactCallbackItem *)
3802 item->callback = callback;
3803 item->arg = arg;
3804 item->next = Xact_callbacks;
3805 Xact_callbacks = item;
3806}
3807
3808void
3810{
3811 XactCallbackItem *item;
3812 XactCallbackItem *prev;
3813
3814 prev = NULL;
3815 for (item = Xact_callbacks; item; prev = item, item = item->next)
3816 {
3817 if (item->callback == callback && item->arg == arg)
3818 {
3819 if (prev)
3820 prev->next = item->next;
3821 else
3822 Xact_callbacks = item->next;
3823 pfree(item);
3824 break;
3825 }
3826 }
3827}
3828
3829static void
3831{
3832 XactCallbackItem *item;
3834
3835 for (item = Xact_callbacks; item; item = next)
3836 {
3837 /* allow callbacks to unregister themselves when called */
3838 next = item->next;
3839 item->callback(event, item->arg);
3840 }
3841}
3842
3843
3844/*
3845 * Register or deregister callback functions for start- and end-of-subxact
3846 * operations.
3847 *
3848 * Pretty much same as above, but for subtransaction events.
3849 *
3850 * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3851 * so the callback functions can only do noncritical cleanup. At
3852 * subtransaction start, the callback is called when the subtransaction has
3853 * finished initializing.
3854 */
3855void
3857{
3858 SubXactCallbackItem *item;
3859
3860 item = (SubXactCallbackItem *)
3862 item->callback = callback;
3863 item->arg = arg;
3864 item->next = SubXact_callbacks;
3865 SubXact_callbacks = item;
3866}
3867
3868void
3870{
3871 SubXactCallbackItem *item;
3872 SubXactCallbackItem *prev;
3873
3874 prev = NULL;
3875 for (item = SubXact_callbacks; item; prev = item, item = item->next)
3876 {
3877 if (item->callback == callback && item->arg == arg)
3878 {
3879 if (prev)
3880 prev->next = item->next;
3881 else
3882 SubXact_callbacks = item->next;
3883 pfree(item);
3884 break;
3885 }
3886 }
3887}
3888
3889static void
3891 SubTransactionId mySubid,
3892 SubTransactionId parentSubid)
3893{
3894 SubXactCallbackItem *item;
3896
3897 for (item = SubXact_callbacks; item; item = next)
3898 {
3899 /* allow callbacks to unregister themselves when called */
3900 next = item->next;
3901 item->callback(event, mySubid, parentSubid, item->arg);
3902 }
3903}
3904
3905
3906/* ----------------------------------------------------------------
3907 * transaction block support
3908 * ----------------------------------------------------------------
3909 */
3910
3911/*
3912 * BeginTransactionBlock
3913 * This executes a BEGIN command.
3914 */
3915void
3917{
3919
3920 switch (s->blockState)
3921 {
3922 /*
3923 * We are not inside a transaction block, so allow one to begin.
3924 */
3925 case TBLOCK_STARTED:
3927 break;
3928
3929 /*
3930 * BEGIN converts an implicit transaction block to a regular one.
3931 * (Note that we allow this even if we've already done some
3932 * commands, which is a bit odd but matches historical practice.)
3933 */
3936 break;
3937
3938 /*
3939 * Already a transaction block in progress.
3940 */
3941 case TBLOCK_INPROGRESS:
3944 case TBLOCK_ABORT:
3945 case TBLOCK_SUBABORT:
3947 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3948 errmsg("there is already a transaction in progress")));
3949 break;
3950
3951 /* These cases are invalid. */
3952 case TBLOCK_DEFAULT:
3953 case TBLOCK_BEGIN:
3954 case TBLOCK_SUBBEGIN:
3955 case TBLOCK_END:
3956 case TBLOCK_SUBRELEASE:
3957 case TBLOCK_SUBCOMMIT:
3958 case TBLOCK_ABORT_END:
3962 case TBLOCK_SUBRESTART:
3964 case TBLOCK_PREPARE:
3965 elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3967 break;
3968 }
3969}
3970
3971/*
3972 * PrepareTransactionBlock
3973 * This executes a PREPARE command.
3974 *
3975 * Since PREPARE may actually do a ROLLBACK, the result indicates what
3976 * happened: true for PREPARE, false for ROLLBACK.
3977 *
3978 * Note that we don't actually do anything here except change blockState.
3979 * The real work will be done in the upcoming PrepareTransaction().
3980 * We do it this way because it's not convenient to change memory context,
3981 * resource owner, etc while executing inside a Portal.
3982 */
3983bool
3985{
3987 bool result;
3988
3989 /* Set up to commit the current transaction */
3990 result = EndTransactionBlock(false);
3991
3992 /* If successful, change outer tblock state to PREPARE */
3993 if (result)
3994 {
3996
3997 while (s->parent != NULL)
3998 s = s->parent;
3999
4000 if (s->blockState == TBLOCK_END)
4001 {
4002 /* Save GID where PrepareTransaction can find it again */
4004
4006 }
4007 else
4008 {
4009 /*
4010 * ignore case where we are not in a transaction;
4011 * EndTransactionBlock already issued a warning.
4012 */
4015 /* Don't send back a PREPARE result tag... */
4016 result = false;
4017 }
4018 }
4019
4020 return result;
4021}
4022
4023/*
4024 * EndTransactionBlock
4025 * This executes a COMMIT command.
4026 *
4027 * Since COMMIT may actually do a ROLLBACK, the result indicates what
4028 * happened: true for COMMIT, false for ROLLBACK.
4029 *
4030 * Note that we don't actually do anything here except change blockState.
4031 * The real work will be done in the upcoming CommitTransactionCommand().
4032 * We do it this way because it's not convenient to change memory context,
4033 * resource owner, etc while executing inside a Portal.
4034 */
4035bool
4037{
4039 bool result = false;
4040
4041 switch (s->blockState)
4042 {
4043 /*
4044 * We are in a transaction block, so tell CommitTransactionCommand
4045 * to COMMIT.
4046 */
4047 case TBLOCK_INPROGRESS:
4049 result = true;
4050 break;
4051
4052 /*
4053 * We are in an implicit transaction block. If AND CHAIN was
4054 * specified, error. Otherwise commit, but issue a warning
4055 * because there was no explicit BEGIN before this.
4056 */
4058 if (chain)
4059 ereport(ERROR,
4060 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4061 /* translator: %s represents an SQL statement name */
4062 errmsg("%s can only be used in transaction blocks",
4063 "COMMIT AND CHAIN")));
4064 else
4066 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4067 errmsg("there is no transaction in progress")));
4069 result = true;
4070 break;
4071
4072 /*
4073 * We are in a failed transaction block. Tell
4074 * CommitTransactionCommand it's time to exit the block.
4075 */
4076 case TBLOCK_ABORT:
4078 break;
4079
4080 /*
4081 * We are in a live subtransaction block. Set up to subcommit all
4082 * open subtransactions and then commit the main transaction.
4083 */
4085 while (s->parent != NULL)
4086 {
4089 else
4090 elog(FATAL, "EndTransactionBlock: unexpected state %s",
4092 s = s->parent;
4093 }
4094 if (s->blockState == TBLOCK_INPROGRESS)
4096 else
4097 elog(FATAL, "EndTransactionBlock: unexpected state %s",
4099 result = true;
4100 break;
4101
4102 /*
4103 * Here we are inside an aborted subtransaction. Treat the COMMIT
4104 * as ROLLBACK: set up to abort everything and exit the main
4105 * transaction.
4106 */
4107 case TBLOCK_SUBABORT:
4108 while (s->parent != NULL)
4109 {
4112 else if (s->blockState == TBLOCK_SUBABORT)
4114 else
4115 elog(FATAL, "EndTransactionBlock: unexpected state %s",
4117 s = s->parent;
4118 }
4119 if (s->blockState == TBLOCK_INPROGRESS)
4121 else if (s->blockState == TBLOCK_ABORT)
4123 else
4124 elog(FATAL, "EndTransactionBlock: unexpected state %s",
4126 break;
4127
4128 /*
4129 * The user issued COMMIT when not inside a transaction. For
4130 * COMMIT without CHAIN, issue a WARNING, staying in
4131 * TBLOCK_STARTED state. The upcoming call to
4132 * CommitTransactionCommand() will then close the transaction and
4133 * put us back into the default state. For COMMIT AND CHAIN,
4134 * error.
4135 */
4136 case TBLOCK_STARTED:
4137 if (chain)
4138 ereport(ERROR,
4139 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4140 /* translator: %s represents an SQL statement name */
4141 errmsg("%s can only be used in transaction blocks",
4142 "COMMIT AND CHAIN")));
4143 else
4145 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4146 errmsg("there is no transaction in progress")));
4147 result = true;
4148 break;
4149
4150 /*
4151 * The user issued a COMMIT that somehow ran inside a parallel
4152 * worker. We can't cope with that.
4153 */
4155 ereport(FATAL,
4156 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4157 errmsg("cannot commit during a parallel operation")));
4158 break;
4159
4160 /* These cases are invalid. */
4161 case TBLOCK_DEFAULT:
4162 case TBLOCK_BEGIN:
4163 case TBLOCK_SUBBEGIN:
4164 case TBLOCK_END:
4165 case TBLOCK_SUBRELEASE:
4166 case TBLOCK_SUBCOMMIT:
4167 case TBLOCK_ABORT_END:
4171 case TBLOCK_SUBRESTART:
4173 case TBLOCK_PREPARE:
4174 elog(FATAL, "EndTransactionBlock: unexpected state %s",
4176 break;
4177 }
4178
4180 s->blockState == TBLOCK_END ||
4183
4184 s->chain = chain;
4185
4186 return result;
4187}
4188
4189/*
4190 * UserAbortTransactionBlock
4191 * This executes a ROLLBACK command.
4192 *
4193 * As above, we don't actually do anything here except change blockState.
4194 */
4195void
4197{
4199
4200 switch (s->blockState)
4201 {
4202 /*
4203 * We are inside a transaction block and we got a ROLLBACK command
4204 * from the user, so tell CommitTransactionCommand to abort and
4205 * exit the transaction block.
4206 */
4207 case TBLOCK_INPROGRESS:
4209 break;
4210
4211 /*
4212 * We are inside a failed transaction block and we got a ROLLBACK
4213 * command from the user. Abort processing is already done, so
4214 * CommitTransactionCommand just has to cleanup and go back to
4215 * idle state.
4216 */
4217 case TBLOCK_ABORT:
4219 break;
4220
4221 /*
4222 * We are inside a subtransaction. Mark everything up to top
4223 * level as exitable.
4224 */
4226 case TBLOCK_SUBABORT:
4227 while (s->parent != NULL)
4228 {
4231 else if (s->blockState == TBLOCK_SUBABORT)
4233 else
4234 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4236 s = s->parent;
4237 }
4238 if (s->blockState == TBLOCK_INPROGRESS)
4240 else if (s->blockState == TBLOCK_ABORT)
4242 else
4243 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4245 break;
4246
4247 /*
4248 * The user issued ABORT when not inside a transaction. For
4249 * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
4250 * The upcoming call to CommitTransactionCommand() will then put
4251 * us back into the default state. For ROLLBACK AND CHAIN, error.
4252 *
4253 * We do the same thing with ABORT inside an implicit transaction,
4254 * although in this case we might be rolling back actual database
4255 * state changes. (It's debatable whether we should issue a
4256 * WARNING in this case, but we have done so historically.)
4257 */
4258 case TBLOCK_STARTED:
4260 if (chain)
4261 ereport(ERROR,
4262 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4263 /* translator: %s represents an SQL statement name */
4264 errmsg("%s can only be used in transaction blocks",
4265 "ROLLBACK AND CHAIN")));
4266 else
4268 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4269 errmsg("there is no transaction in progress")));
4271 break;
4272
4273 /*
4274 * The user issued an ABORT that somehow ran inside a parallel
4275 * worker. We can't cope with that.
4276 */
4278 ereport(FATAL,
4279 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4280 errmsg("cannot abort during a parallel operation")));
4281 break;
4282
4283 /* These cases are invalid. */
4284 case TBLOCK_DEFAULT:
4285 case TBLOCK_BEGIN:
4286 case TBLOCK_SUBBEGIN:
4287 case TBLOCK_END:
4288 case TBLOCK_SUBRELEASE:
4289 case TBLOCK_SUBCOMMIT:
4290 case TBLOCK_ABORT_END:
4294 case TBLOCK_SUBRESTART:
4296 case TBLOCK_PREPARE:
4297 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4299 break;
4300 }
4301
4304
4305 s->chain = chain;
4306}
4307
4308/*
4309 * BeginImplicitTransactionBlock
4310 * Start an implicit transaction block if we're not already in one.
4311 *
4312 * Unlike BeginTransactionBlock, this is called directly from the main loop
4313 * in postgres.c, not within a Portal. So we can just change blockState
4314 * without a lot of ceremony. We do not expect caller to do
4315 * CommitTransactionCommand/StartTransactionCommand.
4316 */
4317void
4319{
4321
4322 /*
4323 * If we are in STARTED state (that is, no transaction block is open),
4324 * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4325 * block.
4326 *
4327 * For caller convenience, we consider all other transaction states as
4328 * legal here; otherwise the caller would need its own state check, which
4329 * seems rather pointless.
4330 */
4331 if (s->blockState == TBLOCK_STARTED)
4333}
4334
4335/*
4336 * EndImplicitTransactionBlock
4337 * End an implicit transaction block, if we're in one.
4338 *
4339 * Like EndTransactionBlock, we just make any needed blockState change here.
4340 * The real work will be done in the upcoming CommitTransactionCommand().
4341 */
4342void
4344{
4346
4347 /*
4348 * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4349 * allowing CommitTransactionCommand to commit whatever happened during
4350 * the implicit transaction block as though it were a single statement.
4351 *
4352 * For caller convenience, we consider all other transaction states as
4353 * legal here; otherwise the caller would need its own state check, which
4354 * seems rather pointless.
4355 */
4358}
4359
4360/*
4361 * DefineSavepoint
4362 * This executes a SAVEPOINT command.
4363 */
4364void
4366{
4368
4369 /*
4370 * Workers synchronize transaction state at the beginning of each parallel
4371 * operation, so we can't account for new subtransactions after that
4372 * point. (Note that this check will certainly error out if s->blockState
4373 * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4374 * below.)
4375 */
4377 ereport(ERROR,
4378 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4379 errmsg("cannot define savepoints during a parallel operation")));
4380
4381 switch (s->blockState)
4382 {
4383 case TBLOCK_INPROGRESS:
4385 /* Normal subtransaction start */
4387 s = CurrentTransactionState; /* changed by push */
4388
4389 /*
4390 * Savepoint names, like the TransactionState block itself, live
4391 * in TopTransactionContext.
4392 */
4393 if (name)
4395 break;
4396
4397 /*
4398 * We disallow savepoint commands in implicit transaction blocks.
4399 * There would be no great difficulty in allowing them so far as
4400 * this module is concerned, but a savepoint seems inconsistent
4401 * with exec_simple_query's behavior of abandoning the whole query
4402 * string upon error. Also, the point of an implicit transaction
4403 * block (as opposed to a regular one) is to automatically close
4404 * after an error, so it's hard to see how a savepoint would fit
4405 * into that.
4406 *
4407 * The error messages for this are phrased as if there were no
4408 * active transaction block at all, which is historical but
4409 * perhaps could be improved.
4410 */
4412 ereport(ERROR,
4413 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4414 /* translator: %s represents an SQL statement name */
4415 errmsg("%s can only be used in transaction blocks",
4416 "SAVEPOINT")));
4417 break;
4418
4419 /* These cases are invalid. */
4420 case TBLOCK_DEFAULT:
4421 case TBLOCK_STARTED:
4422 case TBLOCK_BEGIN:
4424 case TBLOCK_SUBBEGIN:
4425 case TBLOCK_END:
4426 case TBLOCK_SUBRELEASE:
4427 case TBLOCK_SUBCOMMIT:
4428 case TBLOCK_ABORT:
4429 case TBLOCK_SUBABORT:
4430 case TBLOCK_ABORT_END:
4434 case TBLOCK_SUBRESTART:
4436 case TBLOCK_PREPARE:
4437 elog(FATAL, "DefineSavepoint: unexpected state %s",
4439 break;
4440 }
4441}
4442
4443/*
4444 * ReleaseSavepoint
4445 * This executes a RELEASE command.
4446 *
4447 * As above, we don't actually do anything here except change blockState.
4448 */
4449void
4451{
4453 TransactionState target,
4454 xact;
4455
4456 /*
4457 * Workers synchronize transaction state at the beginning of each parallel
4458 * operation, so we can't account for transaction state change after that
4459 * point. (Note that this check will certainly error out if s->blockState
4460 * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4461 * below.)
4462 */
4464 ereport(ERROR,
4465 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4466 errmsg("cannot release savepoints during a parallel operation")));
4467
4468 switch (s->blockState)
4469 {
4470 /*
4471 * We can't release a savepoint if there is no savepoint defined.
4472 */
4473 case TBLOCK_INPROGRESS:
4474 ereport(ERROR,
4475 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4476 errmsg("savepoint \"%s\" does not exist", name)));
4477 break;
4478
4480 /* See comment about implicit transactions in DefineSavepoint */
4481 ereport(ERROR,
4482 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4483 /* translator: %s represents an SQL statement name */
4484 errmsg("%s can only be used in transaction blocks",
4485 "RELEASE SAVEPOINT")));
4486 break;
4487
4488 /*
4489 * We are in a non-aborted subtransaction. This is the only valid
4490 * case.
4491 */
4493 break;
4494
4495 /* These cases are invalid. */
4496 case TBLOCK_DEFAULT:
4497 case TBLOCK_STARTED:
4498 case TBLOCK_BEGIN:
4500 case TBLOCK_SUBBEGIN:
4501 case TBLOCK_END:
4502 case TBLOCK_SUBRELEASE:
4503 case TBLOCK_SUBCOMMIT:
4504 case TBLOCK_ABORT:
4505 case TBLOCK_SUBABORT:
4506 case TBLOCK_ABORT_END:
4510 case TBLOCK_SUBRESTART:
4512 case TBLOCK_PREPARE:
4513 elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4515 break;
4516 }
4517
4518 for (target = s; PointerIsValid(target); target = target->parent)
4519 {
4520 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4521 break;
4522 }
4523
4524 if (!PointerIsValid(target))
4525 ereport(ERROR,
4526 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4527 errmsg("savepoint \"%s\" does not exist", name)));
4528
4529 /* disallow crossing savepoint level boundaries */
4530 if (target->savepointLevel != s->savepointLevel)
4531 ereport(ERROR,
4532 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4533 errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4534
4535 /*
4536 * Mark "commit pending" all subtransactions up to the target
4537 * subtransaction. The actual commits will happen when control gets to
4538 * CommitTransactionCommand.
4539 */
4541 for (;;)
4542 {
4545 if (xact == target)
4546 break;
4547 xact = xact->parent;
4548 Assert(PointerIsValid(xact));
4549 }
4550}
4551
4552/*
4553 * RollbackToSavepoint
4554 * This executes a ROLLBACK TO <savepoint> command.
4555 *
4556 * As above, we don't actually do anything here except change blockState.
4557 */
4558void
4560{
4562 TransactionState target,
4563 xact;
4564
4565 /*
4566 * Workers synchronize transaction state at the beginning of each parallel
4567 * operation, so we can't account for transaction state change after that
4568 * point. (Note that this check will certainly error out if s->blockState
4569 * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4570 * below.)
4571 */
4573 ereport(ERROR,
4574 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4575 errmsg("cannot rollback to savepoints during a parallel operation")));
4576
4577 switch (s->blockState)
4578 {
4579 /*
4580 * We can't rollback to a savepoint if there is no savepoint
4581 * defined.
4582 */
4583 case TBLOCK_INPROGRESS:
4584 case TBLOCK_ABORT:
4585 ereport(ERROR,
4586 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4587 errmsg("savepoint \"%s\" does not exist", name)));
4588 break;
4589
4591 /* See comment about implicit transactions in DefineSavepoint */
4592 ereport(ERROR,
4593 (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4594 /* translator: %s represents an SQL statement name */
4595 errmsg("%s can only be used in transaction blocks",
4596 "ROLLBACK TO SAVEPOINT")));
4597 break;
4598
4599 /*
4600 * There is at least one savepoint, so proceed.
4601 */
4603 case TBLOCK_SUBABORT:
4604 break;
4605
4606 /* These cases are invalid. */
4607 case TBLOCK_DEFAULT:
4608 case TBLOCK_STARTED:
4609 case TBLOCK_BEGIN:
4611 case TBLOCK_SUBBEGIN:
4612 case TBLOCK_END:
4613 case TBLOCK_SUBRELEASE:
4614 case TBLOCK_SUBCOMMIT:
4615 case TBLOCK_ABORT_END:
4619 case TBLOCK_SUBRESTART:
4621 case TBLOCK_PREPARE:
4622 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4624 break;
4625 }
4626
4627 for (target = s; PointerIsValid(target); target = target->parent)
4628 {
4629 if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4630 break;
4631 }
4632
4633 if (!PointerIsValid(target))
4634 ereport(ERROR,
4635 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4636 errmsg("savepoint \"%s\" does not exist", name)));
4637
4638 /* disallow crossing savepoint level boundaries */
4639 if (target->savepointLevel != s->savepointLevel)
4640 ereport(ERROR,
4641 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4642 errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4643
4644 /*
4645 * Mark "abort pending" all subtransactions up to the target
4646 * subtransaction. The actual aborts will happen when control gets to
4647 * CommitTransactionCommand.
4648 */
4650 for (;;)
4651 {
4652 if (xact == target)
4653 break;
4654 if (xact->blockState == TBLOCK_SUBINPROGRESS)
4656 else if (xact->blockState == TBLOCK_SUBABORT)
4658 else
4659 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4661 xact = xact->parent;
4662 Assert(PointerIsValid(xact));
4663 }
4664
4665 /* And mark the target as "restart pending" */
4666 if (xact->blockState == TBLOCK_SUBINPROGRESS)
4668 else if (xact->blockState == TBLOCK_SUBABORT)
4670 else
4671 elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4673}
4674
4675/*
4676 * BeginInternalSubTransaction
4677 * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4678 * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_END,
4679 * and TBLOCK_PREPARE states, and therefore it can safely be used in
4680 * functions that might be called when not inside a BEGIN block or when
4681 * running deferred triggers at COMMIT/PREPARE time. Also, it
4682 * automatically does CommitTransactionCommand/StartTransactionCommand
4683 * instead of expecting the caller to do it.
4684 */
4685void
4687{
4689 bool save_ExitOnAnyError = ExitOnAnyError;
4690
4691 /*
4692 * Errors within this function are improbable, but if one does happen we
4693 * force a FATAL exit. Callers generally aren't prepared to handle losing
4694 * control, and moreover our transaction state is probably corrupted if we
4695 * fail partway through; so an ordinary ERROR longjmp isn't okay.
4696 */
4697 ExitOnAnyError = true;
4698
4699 /*
4700 * We do not check for parallel mode here. It's permissible to start and
4701 * end "internal" subtransactions while in parallel mode, so long as no
4702 * new XIDs or command IDs are assigned. Enforcement of that occurs in
4703 * AssignTransactionId() and CommandCounterIncrement().
4704 */
4705
4706 switch (s->blockState)
4707 {
4708 case TBLOCK_STARTED:
4709 case TBLOCK_INPROGRESS:
4712 case TBLOCK_END:
4713 case TBLOCK_PREPARE:
4715 /* Normal subtransaction start */
4717 s = CurrentTransactionState; /* changed by push */
4718
4719 /*
4720 * Savepoint names, like the TransactionState block itself, live
4721 * in TopTransactionContext.
4722 */
4723 if (name)
4725 break;
4726
4727 /* These cases are invalid. */
4728 case TBLOCK_DEFAULT:
4729 case TBLOCK_BEGIN:
4730 case TBLOCK_SUBBEGIN:
4731 case TBLOCK_SUBRELEASE:
4732 case TBLOCK_SUBCOMMIT:
4733 case TBLOCK_ABORT:
4734 case TBLOCK_SUBABORT:
4735 case TBLOCK_ABORT_END:
4739 case TBLOCK_SUBRESTART:
4741 elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4743 break;
4744 }
4745
4748
4749 ExitOnAnyError = save_ExitOnAnyError;
4750}
4751
4752/*
4753 * ReleaseCurrentSubTransaction
4754 *
4755 * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4756 * savepoint name (if any).
4757 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4758 */
4759void
4761{
4763
4764 /*
4765 * We do not check for parallel mode here. It's permissible to start and
4766 * end "internal" subtransactions while in parallel mode, so long as no
4767 * new XIDs or command IDs are assigned.
4768 */
4769
4771 elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4776 s = CurrentTransactionState; /* changed by pop */
4778}
4779
4780/*
4781 * RollbackAndReleaseCurrentSubTransaction
4782 *
4783 * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4784 * of its savepoint name (if any).
4785 * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4786 */
4787void
4789{
4791
4792 /*
4793 * We do not check for parallel mode here. It's permissible to start and
4794 * end "internal" subtransactions while in parallel mode, so long as no
4795 * new XIDs or command IDs are assigned.
4796 */
4797
4798 switch (s->blockState)
4799 {
4800 /* Must be in a subtransaction */
4802 case TBLOCK_SUBABORT:
4803 break;
4804
4805 /* These cases are invalid. */
4806 case TBLOCK_DEFAULT:
4807 case TBLOCK_STARTED:
4808 case TBLOCK_BEGIN:
4811 case TBLOCK_SUBBEGIN:
4812 case TBLOCK_INPROGRESS:
4813 case TBLOCK_END:
4814 case TBLOCK_SUBRELEASE:
4815 case TBLOCK_SUBCOMMIT:
4816 case TBLOCK_ABORT:
4817 case TBLOCK_ABORT_END:
4821 case TBLOCK_SUBRESTART:
4823 case TBLOCK_PREPARE:
4824 elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4826 break;
4827 }
4828
4829 /*
4830 * Abort the current subtransaction, if needed.
4831 */
4834
4835 /* And clean it up, too */
4837
4838 s = CurrentTransactionState; /* changed by pop */
4844}
4845
4846/*
4847 * AbortOutOfAnyTransaction
4848 *
4849 * This routine is provided for error recovery purposes. It aborts any
4850 * active transaction or transaction block, leaving the system in a known
4851 * idle state.
4852 */
4853void
4855{
4857
4858 /* Ensure we're not running in a doomed memory context */
4860
4861 /*
4862 * Get out of any transaction or nested transaction
4863 */
4864 do
4865 {
4866 switch (s->blockState)
4867 {
4868 case TBLOCK_DEFAULT:
4869 if (s->state == TRANS_DEFAULT)
4870 {
4871 /* Not in a transaction, do nothing */
4872 }
4873 else
4874 {
4875 /*
4876 * We can get here after an error during transaction start
4877 * (state will be TRANS_START). Need to clean up the
4878 * incompletely started transaction. First, adjust the
4879 * low-level state to suppress warning message from
4880 * AbortTransaction.
4881 */
4882 if (s->state == TRANS_START)
4886 }
4887 break;
4888 case TBLOCK_STARTED:
4889 case TBLOCK_BEGIN:
4890 case TBLOCK_INPROGRESS:
4893 case TBLOCK_END:
4895 case TBLOCK_PREPARE:
4896 /* In a transaction, so clean up */
4900 break;
4901 case TBLOCK_ABORT:
4902 case TBLOCK_ABORT_END:
4903
4904 /*
4905 * AbortTransaction is already done, still need Cleanup.
4906 * However, if we failed partway through running ROLLBACK,
4907 * there will be an active portal running that command, which
4908 * we need to shut down before doing CleanupTransaction.
4909 */
4913 break;
4914
4915 /*
4916 * In a subtransaction, so clean it up and abort parent too
4917 */
4918 case TBLOCK_SUBBEGIN:
4920 case TBLOCK_SUBRELEASE:
4921 case TBLOCK_SUBCOMMIT:
4923 case TBLOCK_SUBRESTART:
4926 s = CurrentTransactionState; /* changed by pop */
4927 break;
4928
4929 case TBLOCK_SUBABORT:
4932 /* As above, but AbortSubTransaction already done */
4933 if (s->curTransactionOwner)
4934 {
4935 /* As in TBLOCK_ABORT, might have a live portal to zap */
4940 }
4942 s = CurrentTransactionState; /* changed by pop */
4943 break;
4944 }
4945 } while (s->blockState != TBLOCK_DEFAULT);
4946
4947 /* Should be out of all subxacts now */
4948 Assert(s->parent == NULL);
4949
4950 /*
4951 * Revert to TopMemoryContext, to ensure we exit in a well-defined state
4952 * whether there were any transactions to close or not. (Callers that
4953 * don't intend to exit soon should switch to some other context to avoid
4954 * long-term memory leaks.)
4955 */
4957}
4958
4959/*
4960 * IsTransactionBlock --- are we within a transaction block?
4961 */
4962bool
4964{
4966
4968 return false;
4969
4970 return true;
4971}
4972
4973/*
4974 * IsTransactionOrTransactionBlock --- are we within either a transaction
4975 * or a transaction block? (The backend is only really "idle" when this
4976 * returns false.)
4977 *
4978 * This should match up with IsTransactionBlock and IsTransactionState.
4979 */
4980bool
4982{
4984
4985 if (s->blockState == TBLOCK_DEFAULT)
4986 return false;
4987
4988 return true;
4989}
4990
4991/*
4992 * TransactionBlockStatusCode - return status code to send in ReadyForQuery
4993 */
4994char
4996{
4998
4999 switch (s->blockState)
5000 {
5001 case TBLOCK_DEFAULT:
5002 case TBLOCK_STARTED:
5003 return 'I'; /* idle --- not in transaction */
5004 case TBLOCK_BEGIN:
5005 case TBLOCK_SUBBEGIN:
5006 case TBLOCK_INPROGRESS:
5010 case TBLOCK_END:
5011 case TBLOCK_SUBRELEASE:
5012 case TBLOCK_SUBCOMMIT:
5013 case TBLOCK_PREPARE:
5014 return 'T'; /* in transaction */
5015 case TBLOCK_ABORT:
5016 case TBLOCK_SUBABORT:
5017 case TBLOCK_ABORT_END:
5021 case TBLOCK_SUBRESTART:
5023 return 'E'; /* in failed transaction */
5024 }
5025
5026 /* should never get here */
5027 elog(FATAL, "invalid transaction block state: %s",
5029 return 0; /* keep compiler quiet */
5030}
5031
5032/*
5033 * IsSubTransaction
5034 */
5035bool
5037{
5039
5040 if (s->nestingLevel >= 2)
5041 return true;
5042
5043 return false;
5044}
5045
5046/*
5047 * StartSubTransaction
5048 *
5049 * If you're wondering why this is separate from PushTransaction: it's because
5050 * we can't conveniently do this stuff right inside DefineSavepoint. The
5051 * SAVEPOINT utility command will be executed inside a Portal, and if we
5052 * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
5053 * the Portal will undo those settings. So we make DefineSavepoint just
5054 * push a dummy transaction block, and when control returns to the main
5055 * idle loop, CommitTransactionCommand will be called, and we'll come here
5056 * to finish starting the subtransaction.
5057 */
5058static void
5060{
5062
5063 if (s->state != TRANS_DEFAULT)
5064 elog(WARNING, "StartSubTransaction while in %s state",
5066
5067 s->state = TRANS_START;
5068
5069 /*
5070 * Initialize subsystems for new subtransaction
5071 *
5072 * must initialize resource-management stuff first
5073 */
5077
5079
5080 /*
5081 * Call start-of-subxact callbacks
5082 */
5085
5086 ShowTransactionState("StartSubTransaction");
5087}
5088
5089/*
5090 * CommitSubTransaction
5091 *
5092 * The caller has to make sure to always reassign CurrentTransactionState
5093 * if it has a local pointer to it after calling this function.
5094 */
5095static void
5097{
5099
5100 ShowTransactionState("CommitSubTransaction");
5101
5102 if (s->state != TRANS_INPROGRESS)
5103 elog(WARNING, "CommitSubTransaction while in %s state",
5105
5106 /* Pre-commit processing goes here */
5107
5110
5111 /*
5112 * If this subxact has started any unfinished parallel operation, clean up
5113 * its workers and exit parallel mode. Warn about leaked resources.
5114 */
5116 if (s->parallelModeLevel != 0)
5117 {
5118 elog(WARNING, "parallelModeLevel is %d not 0 at end of subtransaction",
5120 s->parallelModeLevel = 0;
5121 }
5122
5123 /* Do the actual "commit", such as it is */
5124 s->state = TRANS_COMMIT;
5125
5126 /* Must CCI to ensure commands of subtransaction are seen as done */
5128
5129 /*
5130 * Prior to 8.4 we marked subcommit in clog at this point. We now only
5131 * perform that step, if required, as part of the atomic update of the
5132 * whole transaction tree at top level commit or abort.
5133 */
5134
5135 /* Post-commit cleanup */
5141 s->parent->nestingLevel,
5146
5149
5152 true, false);
5156 AtEOSubXact_Inval(true);
5158
5159 /*
5160 * The only lock we actually release here is the subtransaction XID lock.
5161 */
5165
5166 /*
5167 * Other locks should get transferred to their parent resource owner.
5168 */
5171 true, false);
5174 true, false);
5175
5176 AtEOXact_GUC(true, s->gucNestLevel);
5187
5188 /*
5189 * We need to restore the upper transaction's read-only state, in case the
5190 * upper is read-write while the child is read-only; GUC will incorrectly
5191 * think it should leave the child state in place.
5192 */
5194
5198 s->curTransactionOwner = NULL;
5199
5201
5202 s->state = TRANS_DEFAULT;
5203
5205}
5206
5207/*
5208 * AbortSubTransaction
5209 */
5210static void
5212{
5214
5215 /* Prevent cancel/die interrupt while cleaning up */
5217
5218 /* Make sure we have a valid memory context and resource owner */
5221
5222 /*
5223 * Release any LW locks we might be holding as quickly as possible.
5224 * (Regular locks, however, must be held till we finish aborting.)
5225 * Releasing LW locks is critical since we might try to grab them again
5226 * while cleaning up!
5227 *
5228 * FIXME This may be incorrect --- Are there some locks we should keep?
5229 * Buffer locks, for example? I don't think so but I'm not sure.
5230 */
5232
5235 UnlockBuffers();
5236
5237 /* Reset WAL record construction state */
5239
5240 /* Cancel condition variable sleep */
5242
5243 /*
5244 * Also clean up any open wait for lock, since the lock manager will choke
5245 * if we try to wait for another lock before doing this.
5246 */
5248
5249 /*
5250 * If any timeout events are still active, make sure the timeout interrupt
5251 * is scheduled. This covers possible loss of a timeout interrupt due to
5252 * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
5253 * We delay this till after LockErrorCleanup so that we don't uselessly
5254 * reschedule lock or deadlock check timeouts.
5255 */
5257
5258 /*
5259 * Re-enable signals, in case we got here by longjmp'ing out of a signal
5260 * handler. We do this fairly early in the sequence so that the timeout
5261 * infrastructure will be functional if needed while aborting.
5262 */
5263 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
5264
5265 /*
5266 * check the current transaction state
5267 */
5268 ShowTransactionState("AbortSubTransaction");
5269
5270 if (s->state != TRANS_INPROGRESS)
5271 elog(WARNING, "AbortSubTransaction while in %s state",
5273
5274 s->state = TRANS_ABORT;
5275
5276 /*
5277 * Reset user ID which might have been changed transiently. (See notes in
5278 * AbortTransaction.)
5279 */
5281
5282 /* Forget about any active REINDEX. */
5284
5285 /* Reset logical streaming state. */
5287
5288 /*
5289 * No need for SnapBuildResetExportedSnapshotState() here, snapshot
5290 * exports are not supported in subtransactions.
5291 */
5292
5293 /*
5294 * If this subxact has started any unfinished parallel operation, clean up
5295 * its workers and exit parallel mode. Don't warn about leaked resources.
5296 */
5298 s->parallelModeLevel = 0;
5299
5300 /*
5301 * We can skip all this stuff if the subxact failed before creating a
5302 * ResourceOwner...
5303 */
5304 if (s->curTransactionOwner)
5305 {
5314
5315 /* Advertise the fact that we aborted in pg_xact. */
5316 (void) RecordTransactionAbort(true);
5317
5318 /* Post-abort cleanup */
5321
5324
5327 false, false);
5328
5332 AtEOSubXact_Inval(false);
5335 false, false);
5338 false, false);
5340
5341 AtEOXact_GUC(false, s->gucNestLevel);
5352 }
5353
5354 /*
5355 * Restore the upper transaction's read-only state, too. This should be
5356 * redundant with GUC's cleanup but we may as well do it for consistency
5357 * with the commit case.
5358 */
5360
5362}
5363
5364/*
5365 * CleanupSubTransaction
5366 *
5367 * The caller has to make sure to always reassign CurrentTransactionState
5368 * if it has a local pointer to it after calling this function.
5369 */