Line data Source code
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"
49 : #include "replication/logicallauncher.h"
50 : #include "replication/logicalworker.h"
51 : #include "replication/origin.h"
52 : #include "replication/snapbuild.h"
53 : #include "replication/syncrep.h"
54 : #include "storage/aio_subsys.h"
55 : #include "storage/condition_variable.h"
56 : #include "storage/fd.h"
57 : #include "storage/lmgr.h"
58 : #include "storage/md.h"
59 : #include "storage/predicate.h"
60 : #include "storage/proc.h"
61 : #include "storage/procarray.h"
62 : #include "storage/sinvaladt.h"
63 : #include "storage/smgr.h"
64 : #include "utils/builtins.h"
65 : #include "utils/combocid.h"
66 : #include "utils/guc.h"
67 : #include "utils/inval.h"
68 : #include "utils/memutils.h"
69 : #include "utils/relmapper.h"
70 : #include "utils/snapmgr.h"
71 : #include "utils/timeout.h"
72 : #include "utils/timestamp.h"
73 : #include "utils/typcache.h"
74 :
75 : /*
76 : * User-tweakable parameters
77 : */
78 : int DefaultXactIsoLevel = XACT_READ_COMMITTED;
79 : int XactIsoLevel = XACT_READ_COMMITTED;
80 :
81 : bool DefaultXactReadOnly = false;
82 : bool XactReadOnly;
83 :
84 : bool DefaultXactDeferrable = false;
85 : bool XactDeferrable;
86 :
87 : int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
88 :
89 : /*
90 : * CheckXidAlive is a xid value pointing to a possibly ongoing (sub)
91 : * transaction. Currently, it is used in logical decoding. It's possible
92 : * that such transactions can get aborted while the decoding is ongoing in
93 : * which case we skip decoding that particular transaction. To ensure that we
94 : * check whether the CheckXidAlive is aborted after fetching the tuple from
95 : * system tables. We also ensure that during logical decoding we never
96 : * directly access the tableam or heap APIs because we are checking for the
97 : * concurrent aborts only in systable_* APIs.
98 : */
99 : TransactionId CheckXidAlive = InvalidTransactionId;
100 : bool bsysscan = false;
101 :
102 : /*
103 : * When running as a parallel worker, we place only a single
104 : * TransactionStateData on the parallel worker's state stack, and the XID
105 : * reflected there will be that of the *innermost* currently-active
106 : * subtransaction in the backend that initiated parallelism. However,
107 : * GetTopTransactionId() and TransactionIdIsCurrentTransactionId()
108 : * need to return the same answers in the parallel worker as they would have
109 : * in the user backend, so we need some additional bookkeeping.
110 : *
111 : * XactTopFullTransactionId stores the XID of our toplevel transaction, which
112 : * will be the same as TopTransactionStateData.fullTransactionId in an
113 : * ordinary backend; but in a parallel backend, which does not have the entire
114 : * transaction state, it will instead be copied from the backend that started
115 : * the parallel operation.
116 : *
117 : * nParallelCurrentXids will be 0 and ParallelCurrentXids NULL in an ordinary
118 : * backend, but in a parallel backend, nParallelCurrentXids will contain the
119 : * number of XIDs that need to be considered current, and ParallelCurrentXids
120 : * will contain the XIDs themselves. This includes all XIDs that were current
121 : * or sub-committed in the parent at the time the parallel operation began.
122 : * The XIDs are stored sorted in numerical order (not logical order) to make
123 : * lookups as fast as possible.
124 : */
125 : static FullTransactionId XactTopFullTransactionId = {InvalidTransactionId};
126 : static int nParallelCurrentXids = 0;
127 : static TransactionId *ParallelCurrentXids;
128 :
129 : /*
130 : * Miscellaneous flag bits to record events which occur on the top level
131 : * transaction. These flags are only persisted in MyXactFlags and are intended
132 : * so we remember to do certain things later on in the transaction. This is
133 : * globally accessible, so can be set from anywhere in the code that requires
134 : * recording flags.
135 : */
136 : int MyXactFlags;
137 :
138 : /*
139 : * transaction states - transaction state from server perspective
140 : */
141 : typedef enum TransState
142 : {
143 : TRANS_DEFAULT, /* idle */
144 : TRANS_START, /* transaction starting */
145 : TRANS_INPROGRESS, /* inside a valid transaction */
146 : TRANS_COMMIT, /* commit in progress */
147 : TRANS_ABORT, /* abort in progress */
148 : TRANS_PREPARE, /* prepare in progress */
149 : } TransState;
150 :
151 : /*
152 : * transaction block states - transaction state of client queries
153 : *
154 : * Note: the subtransaction states are used only for non-topmost
155 : * transactions; the others appear only in the topmost transaction.
156 : */
157 : typedef enum TBlockState
158 : {
159 : /* not-in-transaction-block states */
160 : TBLOCK_DEFAULT, /* idle */
161 : TBLOCK_STARTED, /* running single-query transaction */
162 :
163 : /* transaction block states */
164 : TBLOCK_BEGIN, /* starting transaction block */
165 : TBLOCK_INPROGRESS, /* live transaction */
166 : TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
167 : TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
168 : TBLOCK_END, /* COMMIT received */
169 : TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
170 : TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
171 : TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
172 : TBLOCK_PREPARE, /* live xact, PREPARE received */
173 :
174 : /* subtransaction states */
175 : TBLOCK_SUBBEGIN, /* starting a subtransaction */
176 : TBLOCK_SUBINPROGRESS, /* live subtransaction */
177 : TBLOCK_SUBRELEASE, /* RELEASE received */
178 : TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
179 : TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
180 : TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
181 : TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
182 : TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
183 : TBLOCK_SUBABORT_RESTART, /* failed subxact, ROLLBACK TO received */
184 : } TBlockState;
185 :
186 : /*
187 : * transaction state structure
188 : *
189 : * Note: parallelModeLevel counts the number of unmatched EnterParallelMode
190 : * calls done at this transaction level. parallelChildXact is true if any
191 : * upper transaction level has nonzero parallelModeLevel.
192 : */
193 : typedef struct TransactionStateData
194 : {
195 : FullTransactionId fullTransactionId; /* my FullTransactionId */
196 : SubTransactionId subTransactionId; /* my subxact ID */
197 : char *name; /* savepoint name, if any */
198 : int savepointLevel; /* savepoint level */
199 : TransState state; /* low-level state */
200 : TBlockState blockState; /* high-level state */
201 : int nestingLevel; /* transaction nesting depth */
202 : int gucNestLevel; /* GUC context nesting depth */
203 : MemoryContext curTransactionContext; /* my xact-lifetime context */
204 : ResourceOwner curTransactionOwner; /* my query resources */
205 : MemoryContext priorContext; /* CurrentMemoryContext before xact started */
206 : TransactionId *childXids; /* subcommitted child XIDs, in XID order */
207 : int nChildXids; /* # of subcommitted child XIDs */
208 : int maxChildXids; /* allocated size of childXids[] */
209 : Oid prevUser; /* previous CurrentUserId setting */
210 : int prevSecContext; /* previous SecurityRestrictionContext */
211 : bool prevXactReadOnly; /* entry-time xact r/o state */
212 : bool startedInRecovery; /* did we start in recovery? */
213 : bool didLogXid; /* has xid been included in WAL record? */
214 : int parallelModeLevel; /* Enter/ExitParallelMode counter */
215 : bool parallelChildXact; /* is any parent transaction parallel? */
216 : bool chain; /* start a new block after this one */
217 : bool topXidLogged; /* for a subxact: is top-level XID logged? */
218 : struct TransactionStateData *parent; /* back link to parent */
219 : } TransactionStateData;
220 :
221 : typedef TransactionStateData *TransactionState;
222 :
223 : /*
224 : * Serialized representation used to transmit transaction state to parallel
225 : * workers through shared memory.
226 : */
227 : typedef struct SerializedTransactionState
228 : {
229 : int xactIsoLevel;
230 : bool xactDeferrable;
231 : FullTransactionId topFullTransactionId;
232 : FullTransactionId currentFullTransactionId;
233 : CommandId currentCommandId;
234 : int nParallelCurrentXids;
235 : TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER];
236 : } SerializedTransactionState;
237 :
238 : /* The size of SerializedTransactionState, not including the final array. */
239 : #define SerializedTransactionStateHeaderSize \
240 : offsetof(SerializedTransactionState, parallelCurrentXids)
241 :
242 : /*
243 : * CurrentTransactionState always points to the current transaction state
244 : * block. It will point to TopTransactionStateData when not in a
245 : * transaction at all, or when in a top-level transaction.
246 : */
247 : static TransactionStateData TopTransactionStateData = {
248 : .state = TRANS_DEFAULT,
249 : .blockState = TBLOCK_DEFAULT,
250 : .topXidLogged = false,
251 : };
252 :
253 : /*
254 : * unreportedXids holds XIDs of all subtransactions that have not yet been
255 : * reported in an XLOG_XACT_ASSIGNMENT record.
256 : */
257 : static int nUnreportedXids;
258 : static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS];
259 :
260 : static TransactionState CurrentTransactionState = &TopTransactionStateData;
261 :
262 : /*
263 : * The subtransaction ID and command ID assignment counters are global
264 : * to a whole transaction, so we do not keep them in the state stack.
265 : */
266 : static SubTransactionId currentSubTransactionId;
267 : static CommandId currentCommandId;
268 : static bool currentCommandIdUsed;
269 :
270 : /*
271 : * xactStartTimestamp is the value of transaction_timestamp().
272 : * stmtStartTimestamp is the value of statement_timestamp().
273 : * xactStopTimestamp is the time at which we log a commit / abort WAL record,
274 : * or if that was skipped, the time of the first subsequent
275 : * GetCurrentTransactionStopTimestamp() call.
276 : *
277 : * These do not change as we enter and exit subtransactions, so we don't
278 : * keep them inside the TransactionState stack.
279 : */
280 : static TimestampTz xactStartTimestamp;
281 : static TimestampTz stmtStartTimestamp;
282 : static TimestampTz xactStopTimestamp;
283 :
284 : /*
285 : * GID to be used for preparing the current transaction. This is also
286 : * global to a whole transaction, so we don't keep it in the state stack.
287 : */
288 : static char *prepareGID;
289 :
290 : /*
291 : * Some commands want to force synchronous commit.
292 : */
293 : static bool forceSyncCommit = false;
294 :
295 : /* Flag for logging statements in a transaction. */
296 : bool xact_is_sampled = false;
297 :
298 : /*
299 : * Private context for transaction-abort work --- we reserve space for this
300 : * at startup to ensure that AbortTransaction and AbortSubTransaction can work
301 : * when we've run out of memory.
302 : */
303 : static MemoryContext TransactionAbortContext = NULL;
304 :
305 : /*
306 : * List of add-on start- and end-of-xact callbacks
307 : */
308 : typedef struct XactCallbackItem
309 : {
310 : struct XactCallbackItem *next;
311 : XactCallback callback;
312 : void *arg;
313 : } XactCallbackItem;
314 :
315 : static XactCallbackItem *Xact_callbacks = NULL;
316 :
317 : /*
318 : * List of add-on start- and end-of-subxact callbacks
319 : */
320 : typedef struct SubXactCallbackItem
321 : {
322 : struct SubXactCallbackItem *next;
323 : SubXactCallback callback;
324 : void *arg;
325 : } SubXactCallbackItem;
326 :
327 : static SubXactCallbackItem *SubXact_callbacks = NULL;
328 :
329 :
330 : /* local function prototypes */
331 : static void AssignTransactionId(TransactionState s);
332 : static void AbortTransaction(void);
333 : static void AtAbort_Memory(void);
334 : static void AtCleanup_Memory(void);
335 : static void AtAbort_ResourceOwner(void);
336 : static void AtCCI_LocalCache(void);
337 : static void AtCommit_Memory(void);
338 : static void AtStart_Cache(void);
339 : static void AtStart_Memory(void);
340 : static void AtStart_ResourceOwner(void);
341 : static void CallXactCallbacks(XactEvent event);
342 : static void CallSubXactCallbacks(SubXactEvent event,
343 : SubTransactionId mySubid,
344 : SubTransactionId parentSubid);
345 : static void CleanupTransaction(void);
346 : static void CheckTransactionBlock(bool isTopLevel, bool throwError,
347 : const char *stmtType);
348 : static void CommitTransaction(void);
349 : static TransactionId RecordTransactionAbort(bool isSubXact);
350 : static void StartTransaction(void);
351 :
352 : static bool CommitTransactionCommandInternal(void);
353 : static bool AbortCurrentTransactionInternal(void);
354 :
355 : static void StartSubTransaction(void);
356 : static void CommitSubTransaction(void);
357 : static void AbortSubTransaction(void);
358 : static void CleanupSubTransaction(void);
359 : static void PushTransaction(void);
360 : static void PopTransaction(void);
361 :
362 : static void AtSubAbort_Memory(void);
363 : static void AtSubCleanup_Memory(void);
364 : static void AtSubAbort_ResourceOwner(void);
365 : static void AtSubCommit_Memory(void);
366 : static void AtSubStart_Memory(void);
367 : static void AtSubStart_ResourceOwner(void);
368 :
369 : static void ShowTransactionState(const char *str);
370 : static void ShowTransactionStateRec(const char *str, TransactionState s);
371 : static const char *BlockStateAsString(TBlockState blockState);
372 : static const char *TransStateAsString(TransState state);
373 :
374 :
375 : /* ----------------------------------------------------------------
376 : * transaction state accessors
377 : * ----------------------------------------------------------------
378 : */
379 :
380 : /*
381 : * IsTransactionState
382 : *
383 : * This returns true if we are inside a valid transaction; that is,
384 : * it is safe to initiate database access, take heavyweight locks, etc.
385 : */
386 : bool
387 2107190 : IsTransactionState(void)
388 : {
389 2107190 : TransactionState s = CurrentTransactionState;
390 :
391 : /*
392 : * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
393 : * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
394 : * TRANS_PREPARE since it might be too soon or too late within those
395 : * transition states to do anything interesting. Hence, the only "valid"
396 : * state is TRANS_INPROGRESS.
397 : */
398 2107190 : return (s->state == TRANS_INPROGRESS);
399 : }
400 :
401 : /*
402 : * IsAbortedTransactionBlockState
403 : *
404 : * This returns true if we are within an aborted transaction block.
405 : */
406 : bool
407 1541820 : IsAbortedTransactionBlockState(void)
408 : {
409 1541820 : TransactionState s = CurrentTransactionState;
410 :
411 1541820 : if (s->blockState == TBLOCK_ABORT ||
412 1538842 : s->blockState == TBLOCK_SUBABORT)
413 3562 : return true;
414 :
415 1538258 : return false;
416 : }
417 :
418 :
419 : /*
420 : * GetTopTransactionId
421 : *
422 : * This will return the XID of the main transaction, assigning one if
423 : * it's not yet set. Be careful to call this only inside a valid xact.
424 : */
425 : TransactionId
426 59336 : GetTopTransactionId(void)
427 : {
428 59336 : if (!FullTransactionIdIsValid(XactTopFullTransactionId))
429 1222 : AssignTransactionId(&TopTransactionStateData);
430 59336 : return XidFromFullTransactionId(XactTopFullTransactionId);
431 : }
432 :
433 : /*
434 : * GetTopTransactionIdIfAny
435 : *
436 : * This will return the XID of the main transaction, if one is assigned.
437 : * It will return InvalidTransactionId if we are not currently inside a
438 : * transaction, or inside a transaction that hasn't yet been assigned an XID.
439 : */
440 : TransactionId
441 87658852 : GetTopTransactionIdIfAny(void)
442 : {
443 87658852 : return XidFromFullTransactionId(XactTopFullTransactionId);
444 : }
445 :
446 : /*
447 : * GetCurrentTransactionId
448 : *
449 : * This will return the XID of the current transaction (main or sub
450 : * transaction), assigning one if it's not yet set. Be careful to call this
451 : * only inside a valid xact.
452 : */
453 : TransactionId
454 21589714 : GetCurrentTransactionId(void)
455 : {
456 21589714 : TransactionState s = CurrentTransactionState;
457 :
458 21589714 : if (!FullTransactionIdIsValid(s->fullTransactionId))
459 265392 : AssignTransactionId(s);
460 21589700 : return XidFromFullTransactionId(s->fullTransactionId);
461 : }
462 :
463 : /*
464 : * GetCurrentTransactionIdIfAny
465 : *
466 : * This will return the XID of the current sub xact, if one is assigned.
467 : * It will return InvalidTransactionId if we are not currently inside a
468 : * transaction, or inside a transaction that hasn't been assigned an XID yet.
469 : */
470 : TransactionId
471 29316682 : GetCurrentTransactionIdIfAny(void)
472 : {
473 29316682 : return XidFromFullTransactionId(CurrentTransactionState->fullTransactionId);
474 : }
475 :
476 : /*
477 : * GetTopFullTransactionId
478 : *
479 : * This will return the FullTransactionId of the main transaction, assigning
480 : * one if it's not yet set. Be careful to call this only inside a valid xact.
481 : */
482 : FullTransactionId
483 5732 : GetTopFullTransactionId(void)
484 : {
485 5732 : if (!FullTransactionIdIsValid(XactTopFullTransactionId))
486 4166 : AssignTransactionId(&TopTransactionStateData);
487 5732 : return XactTopFullTransactionId;
488 : }
489 :
490 : /*
491 : * GetTopFullTransactionIdIfAny
492 : *
493 : * This will return the FullTransactionId of the main transaction, if one is
494 : * assigned. It will return InvalidFullTransactionId if we are not currently
495 : * inside a transaction, or inside a transaction that hasn't yet been assigned
496 : * one.
497 : */
498 : FullTransactionId
499 24 : GetTopFullTransactionIdIfAny(void)
500 : {
501 24 : return XactTopFullTransactionId;
502 : }
503 :
504 : /*
505 : * GetCurrentFullTransactionId
506 : *
507 : * This will return the FullTransactionId of the current transaction (main or
508 : * sub transaction), assigning one if it's not yet set. Be careful to call
509 : * this only inside a valid xact.
510 : */
511 : FullTransactionId
512 668 : GetCurrentFullTransactionId(void)
513 : {
514 668 : TransactionState s = CurrentTransactionState;
515 :
516 668 : if (!FullTransactionIdIsValid(s->fullTransactionId))
517 34 : AssignTransactionId(s);
518 668 : return s->fullTransactionId;
519 : }
520 :
521 : /*
522 : * GetCurrentFullTransactionIdIfAny
523 : *
524 : * This will return the FullTransactionId of the current sub xact, if one is
525 : * assigned. It will return InvalidFullTransactionId if we are not currently
526 : * inside a transaction, or inside a transaction that hasn't been assigned one
527 : * yet.
528 : */
529 : FullTransactionId
530 0 : GetCurrentFullTransactionIdIfAny(void)
531 : {
532 0 : return CurrentTransactionState->fullTransactionId;
533 : }
534 :
535 : /*
536 : * MarkCurrentTransactionIdLoggedIfAny
537 : *
538 : * Remember that the current xid - if it is assigned - now has been wal logged.
539 : */
540 : void
541 29238612 : MarkCurrentTransactionIdLoggedIfAny(void)
542 : {
543 29238612 : if (FullTransactionIdIsValid(CurrentTransactionState->fullTransactionId))
544 28686978 : CurrentTransactionState->didLogXid = true;
545 29238612 : }
546 :
547 : /*
548 : * IsSubxactTopXidLogPending
549 : *
550 : * This is used to decide whether we need to WAL log the top-level XID for
551 : * operation in a subtransaction. We require that for logical decoding, see
552 : * LogicalDecodingProcessRecord.
553 : *
554 : * This returns true if wal_level >= logical and we are inside a valid
555 : * subtransaction, for which the assignment was not yet written to any WAL
556 : * record.
557 : */
558 : bool
559 29253636 : IsSubxactTopXidLogPending(void)
560 : {
561 : /* check whether it is already logged */
562 29253636 : if (CurrentTransactionState->topXidLogged)
563 204248 : return false;
564 :
565 : /* wal_level has to be logical */
566 29049388 : if (!XLogLogicalInfoActive())
567 27983264 : return false;
568 :
569 : /* we need to be in a transaction state */
570 1066124 : if (!IsTransactionState())
571 7534 : return false;
572 :
573 : /* it has to be a subtransaction */
574 1058590 : if (!IsSubTransaction())
575 1058134 : return false;
576 :
577 : /* the subtransaction has to have a XID assigned */
578 456 : if (!TransactionIdIsValid(GetCurrentTransactionIdIfAny()))
579 14 : return false;
580 :
581 442 : return true;
582 : }
583 :
584 : /*
585 : * MarkSubxactTopXidLogged
586 : *
587 : * Remember that the top transaction id for the current subtransaction is WAL
588 : * logged now.
589 : */
590 : void
591 438 : MarkSubxactTopXidLogged(void)
592 : {
593 : Assert(IsSubxactTopXidLogPending());
594 :
595 438 : CurrentTransactionState->topXidLogged = true;
596 438 : }
597 :
598 : /*
599 : * GetStableLatestTransactionId
600 : *
601 : * Get the transaction's XID if it has one, else read the next-to-be-assigned
602 : * XID. Once we have a value, return that same value for the remainder of the
603 : * current transaction. This is meant to provide the reference point for the
604 : * age(xid) function, but might be useful for other maintenance tasks as well.
605 : */
606 : TransactionId
607 308 : GetStableLatestTransactionId(void)
608 : {
609 : static LocalTransactionId lxid = InvalidLocalTransactionId;
610 : static TransactionId stablexid = InvalidTransactionId;
611 :
612 308 : if (lxid != MyProc->vxid.lxid)
613 : {
614 32 : lxid = MyProc->vxid.lxid;
615 32 : stablexid = GetTopTransactionIdIfAny();
616 32 : if (!TransactionIdIsValid(stablexid))
617 32 : stablexid = ReadNextTransactionId();
618 : }
619 :
620 : Assert(TransactionIdIsValid(stablexid));
621 :
622 308 : return stablexid;
623 : }
624 :
625 : /*
626 : * AssignTransactionId
627 : *
628 : * Assigns a new permanent FullTransactionId to the given TransactionState.
629 : * We do not assign XIDs to transactions until/unless this is called.
630 : * Also, any parent TransactionStates that don't yet have XIDs are assigned
631 : * one; this maintains the invariant that a child transaction has an XID
632 : * following its parent's.
633 : */
634 : static void
635 273122 : AssignTransactionId(TransactionState s)
636 : {
637 273122 : bool isSubXact = (s->parent != NULL);
638 : ResourceOwner currentOwner;
639 273122 : bool log_unknown_top = false;
640 :
641 : /* Assert that caller didn't screw up */
642 : Assert(!FullTransactionIdIsValid(s->fullTransactionId));
643 : Assert(s->state == TRANS_INPROGRESS);
644 :
645 : /*
646 : * Workers synchronize transaction state at the beginning of each parallel
647 : * operation, so we can't account for new XIDs at this point.
648 : */
649 273122 : if (IsInParallelMode() || IsParallelWorker())
650 0 : ereport(ERROR,
651 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
652 : errmsg("cannot assign transaction IDs during a parallel operation")));
653 :
654 : /*
655 : * Ensure parent(s) have XIDs, so that a child always has an XID later
656 : * than its parent. Mustn't recurse here, or we might get a stack
657 : * overflow if we're at the bottom of a huge stack of subtransactions none
658 : * of which have XIDs yet.
659 : */
660 273122 : if (isSubXact && !FullTransactionIdIsValid(s->parent->fullTransactionId))
661 : {
662 1170 : TransactionState p = s->parent;
663 : TransactionState *parents;
664 1170 : size_t parentOffset = 0;
665 :
666 1170 : parents = palloc(sizeof(TransactionState) * s->nestingLevel);
667 3478 : while (p != NULL && !FullTransactionIdIsValid(p->fullTransactionId))
668 : {
669 2308 : parents[parentOffset++] = p;
670 2308 : p = p->parent;
671 : }
672 :
673 : /*
674 : * This is technically a recursive call, but the recursion will never
675 : * be more than one layer deep.
676 : */
677 3478 : while (parentOffset != 0)
678 2308 : AssignTransactionId(parents[--parentOffset]);
679 :
680 1170 : pfree(parents);
681 : }
682 :
683 : /*
684 : * When wal_level=logical, guarantee that a subtransaction's xid can only
685 : * be seen in the WAL stream if its toplevel xid has been logged before.
686 : * If necessary we log an xact_assignment record with fewer than
687 : * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
688 : * for a transaction even though it appears in a WAL record, we just might
689 : * superfluously log something. That can happen when an xid is included
690 : * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
691 : * xl_standby_locks.
692 : */
693 273122 : if (isSubXact && XLogLogicalInfoActive() &&
694 600 : !TopTransactionStateData.didLogXid)
695 48 : log_unknown_top = true;
696 :
697 : /*
698 : * Generate a new FullTransactionId and record its xid in PGPROC and
699 : * pg_subtrans.
700 : *
701 : * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
702 : * shared storage other than PGPROC; because if there's no room for it in
703 : * PGPROC, the subtrans entry is needed to ensure that other backends see
704 : * the Xid as "running". See GetNewTransactionId.
705 : */
706 273122 : s->fullTransactionId = GetNewTransactionId(isSubXact);
707 273108 : if (!isSubXact)
708 264444 : XactTopFullTransactionId = s->fullTransactionId;
709 :
710 273108 : if (isSubXact)
711 8664 : SubTransSetParent(XidFromFullTransactionId(s->fullTransactionId),
712 8664 : XidFromFullTransactionId(s->parent->fullTransactionId));
713 :
714 : /*
715 : * If it's a top-level transaction, the predicate locking system needs to
716 : * be told about it too.
717 : */
718 273108 : if (!isSubXact)
719 264444 : RegisterPredicateLockingXid(XidFromFullTransactionId(s->fullTransactionId));
720 :
721 : /*
722 : * Acquire lock on the transaction XID. (We assume this cannot block.) We
723 : * have to ensure that the lock is assigned to the transaction's own
724 : * ResourceOwner.
725 : */
726 273108 : currentOwner = CurrentResourceOwner;
727 273108 : CurrentResourceOwner = s->curTransactionOwner;
728 :
729 273108 : XactLockTableInsert(XidFromFullTransactionId(s->fullTransactionId));
730 :
731 273108 : CurrentResourceOwner = currentOwner;
732 :
733 : /*
734 : * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
735 : * top-level transaction we issue a WAL record for the assignment. We
736 : * include the top-level xid and all the subxids that have not yet been
737 : * reported using XLOG_XACT_ASSIGNMENT records.
738 : *
739 : * This is required to limit the amount of shared memory required in a hot
740 : * standby server to keep track of in-progress XIDs. See notes for
741 : * RecordKnownAssignedTransactionIds().
742 : *
743 : * We don't keep track of the immediate parent of each subxid, only the
744 : * top-level transaction that each subxact belongs to. This is correct in
745 : * recovery only because aborted subtransactions are separately WAL
746 : * logged.
747 : *
748 : * This is correct even for the case where several levels above us didn't
749 : * have an xid assigned as we recursed up to them beforehand.
750 : */
751 273108 : if (isSubXact && XLogStandbyInfoActive())
752 : {
753 7656 : unreportedXids[nUnreportedXids] = XidFromFullTransactionId(s->fullTransactionId);
754 7656 : nUnreportedXids++;
755 :
756 : /*
757 : * ensure this test matches similar one in
758 : * RecoverPreparedTransactions()
759 : */
760 7656 : if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS ||
761 : log_unknown_top)
762 : {
763 : xl_xact_assignment xlrec;
764 :
765 : /*
766 : * xtop is always set by now because we recurse up transaction
767 : * stack to the highest unassigned xid and then come back down
768 : */
769 132 : xlrec.xtop = GetTopTransactionId();
770 : Assert(TransactionIdIsValid(xlrec.xtop));
771 132 : xlrec.nsubxacts = nUnreportedXids;
772 :
773 132 : XLogBeginInsert();
774 132 : XLogRegisterData(&xlrec, MinSizeOfXactAssignment);
775 132 : XLogRegisterData(unreportedXids,
776 : nUnreportedXids * sizeof(TransactionId));
777 :
778 132 : (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
779 :
780 132 : nUnreportedXids = 0;
781 : /* mark top, not current xact as having been logged */
782 132 : TopTransactionStateData.didLogXid = true;
783 : }
784 : }
785 273108 : }
786 :
787 : /*
788 : * GetCurrentSubTransactionId
789 : */
790 : SubTransactionId
791 17914898 : GetCurrentSubTransactionId(void)
792 : {
793 17914898 : TransactionState s = CurrentTransactionState;
794 :
795 17914898 : return s->subTransactionId;
796 : }
797 :
798 : /*
799 : * SubTransactionIsActive
800 : *
801 : * Test if the specified subxact ID is still active. Note caller is
802 : * responsible for checking whether this ID is relevant to the current xact.
803 : */
804 : bool
805 0 : SubTransactionIsActive(SubTransactionId subxid)
806 : {
807 : TransactionState s;
808 :
809 0 : for (s = CurrentTransactionState; s != NULL; s = s->parent)
810 : {
811 0 : if (s->state == TRANS_ABORT)
812 0 : continue;
813 0 : if (s->subTransactionId == subxid)
814 0 : return true;
815 : }
816 0 : return false;
817 : }
818 :
819 :
820 : /*
821 : * GetCurrentCommandId
822 : *
823 : * "used" must be true if the caller intends to use the command ID to mark
824 : * inserted/updated/deleted tuples. false means the ID is being fetched
825 : * for read-only purposes (ie, as a snapshot validity cutoff). See
826 : * CommandCounterIncrement() for discussion.
827 : */
828 : CommandId
829 11895678 : GetCurrentCommandId(bool used)
830 : {
831 : /* this is global to a transaction, not subtransaction-local */
832 11895678 : if (used)
833 : {
834 : /*
835 : * Forbid setting currentCommandIdUsed in a parallel worker, because
836 : * we have no provision for communicating this back to the leader. We
837 : * could relax this restriction when currentCommandIdUsed was already
838 : * true at the start of the parallel operation.
839 : */
840 6990822 : if (IsParallelWorker())
841 0 : ereport(ERROR,
842 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
843 : errmsg("cannot modify data in a parallel worker")));
844 :
845 6990822 : currentCommandIdUsed = true;
846 : }
847 11895678 : return currentCommandId;
848 : }
849 :
850 : /*
851 : * SetParallelStartTimestamps
852 : *
853 : * In a parallel worker, we should inherit the parent transaction's
854 : * timestamps rather than setting our own. The parallel worker
855 : * infrastructure must call this to provide those values before
856 : * calling StartTransaction() or SetCurrentStatementStartTimestamp().
857 : */
858 : void
859 2746 : SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts)
860 : {
861 : Assert(IsParallelWorker());
862 2746 : xactStartTimestamp = xact_ts;
863 2746 : stmtStartTimestamp = stmt_ts;
864 2746 : }
865 :
866 : /*
867 : * GetCurrentTransactionStartTimestamp
868 : */
869 : TimestampTz
870 86614 : GetCurrentTransactionStartTimestamp(void)
871 : {
872 86614 : return xactStartTimestamp;
873 : }
874 :
875 : /*
876 : * GetCurrentStatementStartTimestamp
877 : */
878 : TimestampTz
879 2452556 : GetCurrentStatementStartTimestamp(void)
880 : {
881 2452556 : return stmtStartTimestamp;
882 : }
883 :
884 : /*
885 : * GetCurrentTransactionStopTimestamp
886 : *
887 : * If the transaction stop time hasn't already been set, which can happen if
888 : * we decided we don't need to log an XLOG record, set xactStopTimestamp.
889 : */
890 : TimestampTz
891 2062910 : GetCurrentTransactionStopTimestamp(void)
892 : {
893 2062910 : TransactionState s PG_USED_FOR_ASSERTS_ONLY = CurrentTransactionState;
894 :
895 : /* should only be called after commit / abort processing */
896 : Assert(s->state == TRANS_DEFAULT ||
897 : s->state == TRANS_COMMIT ||
898 : s->state == TRANS_ABORT ||
899 : s->state == TRANS_PREPARE);
900 :
901 2062910 : if (xactStopTimestamp == 0)
902 588444 : xactStopTimestamp = GetCurrentTimestamp();
903 :
904 2062910 : return xactStopTimestamp;
905 : }
906 :
907 : /*
908 : * SetCurrentStatementStartTimestamp
909 : *
910 : * In a parallel worker, this should already have been provided by a call
911 : * to SetParallelStartTimestamps().
912 : */
913 : void
914 1289102 : SetCurrentStatementStartTimestamp(void)
915 : {
916 1289102 : if (!IsParallelWorker())
917 1286356 : stmtStartTimestamp = GetCurrentTimestamp();
918 : else
919 : Assert(stmtStartTimestamp != 0);
920 1289102 : }
921 :
922 : /*
923 : * GetCurrentTransactionNestLevel
924 : *
925 : * Note: this will return zero when not inside any transaction, one when
926 : * inside a top-level transaction, etc.
927 : */
928 : int
929 35524586 : GetCurrentTransactionNestLevel(void)
930 : {
931 35524586 : TransactionState s = CurrentTransactionState;
932 :
933 35524586 : return s->nestingLevel;
934 : }
935 :
936 :
937 : /*
938 : * TransactionIdIsCurrentTransactionId
939 : */
940 : bool
941 87707420 : TransactionIdIsCurrentTransactionId(TransactionId xid)
942 : {
943 : TransactionState s;
944 :
945 : /*
946 : * We always say that BootstrapTransactionId is "not my transaction ID"
947 : * even when it is (ie, during bootstrap). Along with the fact that
948 : * transam.c always treats BootstrapTransactionId as already committed,
949 : * this causes the heapam_visibility.c routines to see all tuples as
950 : * committed, which is what we need during bootstrap. (Bootstrap mode
951 : * only inserts tuples, it never updates or deletes them, so all tuples
952 : * can be presumed good immediately.)
953 : *
954 : * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
955 : * not my transaction ID, so we can just return "false" immediately for
956 : * any non-normal XID.
957 : */
958 87707420 : if (!TransactionIdIsNormal(xid))
959 1303606 : return false;
960 :
961 86403814 : if (TransactionIdEquals(xid, GetTopTransactionIdIfAny()))
962 56473104 : return true;
963 :
964 : /*
965 : * In parallel workers, the XIDs we must consider as current are stored in
966 : * ParallelCurrentXids rather than the transaction-state stack. Note that
967 : * the XIDs in this array are sorted numerically rather than according to
968 : * transactionIdPrecedes order.
969 : */
970 29930710 : if (nParallelCurrentXids > 0)
971 : {
972 : int low,
973 : high;
974 :
975 3784276 : low = 0;
976 3784276 : high = nParallelCurrentXids - 1;
977 14779022 : while (low <= high)
978 : {
979 : int middle;
980 : TransactionId probe;
981 :
982 14600068 : middle = low + (high - low) / 2;
983 14600068 : probe = ParallelCurrentXids[middle];
984 14600068 : if (probe == xid)
985 3605322 : return true;
986 10994746 : else if (probe < xid)
987 10815800 : low = middle + 1;
988 : else
989 178946 : high = middle - 1;
990 : }
991 178954 : return false;
992 : }
993 :
994 : /*
995 : * We will return true for the Xid of the current subtransaction, any of
996 : * its subcommitted children, any of its parents, or any of their
997 : * previously subcommitted children. However, a transaction being aborted
998 : * is no longer "current", even though it may still have an entry on the
999 : * state stack.
1000 : */
1001 52344632 : for (s = CurrentTransactionState; s != NULL; s = s->parent)
1002 : {
1003 : int low,
1004 : high;
1005 :
1006 26270188 : if (s->state == TRANS_ABORT)
1007 0 : continue;
1008 26270188 : if (!FullTransactionIdIsValid(s->fullTransactionId))
1009 19096994 : continue; /* it can't have any child XIDs either */
1010 7173194 : if (TransactionIdEquals(xid, XidFromFullTransactionId(s->fullTransactionId)))
1011 68912 : return true;
1012 : /* As the childXids array is ordered, we can use binary search */
1013 7104282 : low = 0;
1014 7104282 : high = s->nChildXids - 1;
1015 7106026 : while (low <= high)
1016 : {
1017 : int middle;
1018 : TransactionId probe;
1019 :
1020 4822 : middle = low + (high - low) / 2;
1021 4822 : probe = s->childXids[middle];
1022 4822 : if (TransactionIdEquals(probe, xid))
1023 3078 : return true;
1024 1744 : else if (TransactionIdPrecedes(probe, xid))
1025 1606 : low = middle + 1;
1026 : else
1027 138 : high = middle - 1;
1028 : }
1029 : }
1030 :
1031 26074444 : return false;
1032 : }
1033 :
1034 : /*
1035 : * TransactionStartedDuringRecovery
1036 : *
1037 : * Returns true if the current transaction started while recovery was still
1038 : * in progress. Recovery might have ended since so RecoveryInProgress() might
1039 : * return false already.
1040 : */
1041 : bool
1042 15536556 : TransactionStartedDuringRecovery(void)
1043 : {
1044 15536556 : return CurrentTransactionState->startedInRecovery;
1045 : }
1046 :
1047 : /*
1048 : * EnterParallelMode
1049 : */
1050 : void
1051 6366 : EnterParallelMode(void)
1052 : {
1053 6366 : TransactionState s = CurrentTransactionState;
1054 :
1055 : Assert(s->parallelModeLevel >= 0);
1056 :
1057 6366 : ++s->parallelModeLevel;
1058 6366 : }
1059 :
1060 : /*
1061 : * ExitParallelMode
1062 : */
1063 : void
1064 3608 : ExitParallelMode(void)
1065 : {
1066 3608 : TransactionState s = CurrentTransactionState;
1067 :
1068 : Assert(s->parallelModeLevel > 0);
1069 : Assert(s->parallelModeLevel > 1 || s->parallelChildXact ||
1070 : !ParallelContextActive());
1071 :
1072 3608 : --s->parallelModeLevel;
1073 3608 : }
1074 :
1075 : /*
1076 : * IsInParallelMode
1077 : *
1078 : * Are we in a parallel operation, as either the leader or a worker? Check
1079 : * this to prohibit operations that change backend-local state expected to
1080 : * match across all workers. Mere caches usually don't require such a
1081 : * restriction. State modified in a strict push/pop fashion, such as the
1082 : * active snapshot stack, is often fine.
1083 : *
1084 : * We say we are in parallel mode if we are in a subxact of a transaction
1085 : * that's initiated a parallel operation; for most purposes that context
1086 : * has all the same restrictions.
1087 : */
1088 : bool
1089 57828252 : IsInParallelMode(void)
1090 : {
1091 57828252 : TransactionState s = CurrentTransactionState;
1092 :
1093 57828252 : return s->parallelModeLevel != 0 || s->parallelChildXact;
1094 : }
1095 :
1096 : /*
1097 : * CommandCounterIncrement
1098 : */
1099 : void
1100 2192604 : CommandCounterIncrement(void)
1101 : {
1102 : /*
1103 : * If the current value of the command counter hasn't been "used" to mark
1104 : * tuples, we need not increment it, since there's no need to distinguish
1105 : * a read-only command from others. This helps postpone command counter
1106 : * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1107 : */
1108 2192604 : if (currentCommandIdUsed)
1109 : {
1110 : /*
1111 : * Workers synchronize transaction state at the beginning of each
1112 : * parallel operation, so we can't account for new commands after that
1113 : * point.
1114 : */
1115 1150314 : if (IsInParallelMode() || IsParallelWorker())
1116 0 : ereport(ERROR,
1117 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
1118 : errmsg("cannot start commands during a parallel operation")));
1119 :
1120 1150314 : currentCommandId += 1;
1121 1150314 : if (currentCommandId == InvalidCommandId)
1122 : {
1123 0 : currentCommandId -= 1;
1124 0 : ereport(ERROR,
1125 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1126 : errmsg("cannot have more than 2^32-2 commands in a transaction")));
1127 : }
1128 1150314 : currentCommandIdUsed = false;
1129 :
1130 : /* Propagate new command ID into static snapshots */
1131 1150314 : SnapshotSetCommandId(currentCommandId);
1132 :
1133 : /*
1134 : * Make any catalog changes done by the just-completed command visible
1135 : * in the local syscache. We obviously don't need to do this after a
1136 : * read-only command. (But see hacks in inval.c to make real sure we
1137 : * don't think a command that queued inval messages was read-only.)
1138 : */
1139 1150314 : AtCCI_LocalCache();
1140 : }
1141 2192598 : }
1142 :
1143 : /*
1144 : * ForceSyncCommit
1145 : *
1146 : * Interface routine to allow commands to force a synchronous commit of the
1147 : * current top-level transaction. Currently, two-phase commit does not
1148 : * persist and restore this variable. So long as all callers use
1149 : * PreventInTransactionBlock(), that omission has no consequences.
1150 : */
1151 : void
1152 990 : ForceSyncCommit(void)
1153 : {
1154 990 : forceSyncCommit = true;
1155 990 : }
1156 :
1157 :
1158 : /* ----------------------------------------------------------------
1159 : * StartTransaction stuff
1160 : * ----------------------------------------------------------------
1161 : */
1162 :
1163 : /*
1164 : * AtStart_Cache
1165 : */
1166 : static void
1167 1056948 : AtStart_Cache(void)
1168 : {
1169 1056948 : AcceptInvalidationMessages();
1170 1056948 : }
1171 :
1172 : /*
1173 : * AtStart_Memory
1174 : */
1175 : static void
1176 1056948 : AtStart_Memory(void)
1177 : {
1178 1056948 : TransactionState s = CurrentTransactionState;
1179 :
1180 : /*
1181 : * Remember the memory context that was active prior to transaction start.
1182 : */
1183 1056948 : s->priorContext = CurrentMemoryContext;
1184 :
1185 : /*
1186 : * If this is the first time through, create a private context for
1187 : * AbortTransaction to work in. By reserving some space now, we can
1188 : * insulate AbortTransaction from out-of-memory scenarios. Like
1189 : * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1190 : * size, so that space will be reserved immediately.
1191 : */
1192 1056948 : if (TransactionAbortContext == NULL)
1193 34158 : TransactionAbortContext =
1194 34158 : AllocSetContextCreate(TopMemoryContext,
1195 : "TransactionAbortContext",
1196 : 32 * 1024,
1197 : 32 * 1024,
1198 : 32 * 1024);
1199 :
1200 : /*
1201 : * Likewise, if this is the first time through, create a top-level context
1202 : * for transaction-local data. This context will be reset at transaction
1203 : * end, and then re-used in later transactions.
1204 : */
1205 1056948 : if (TopTransactionContext == NULL)
1206 34158 : TopTransactionContext =
1207 34158 : AllocSetContextCreate(TopMemoryContext,
1208 : "TopTransactionContext",
1209 : ALLOCSET_DEFAULT_SIZES);
1210 :
1211 : /*
1212 : * In a top-level transaction, CurTransactionContext is the same as
1213 : * TopTransactionContext.
1214 : */
1215 1056948 : CurTransactionContext = TopTransactionContext;
1216 1056948 : s->curTransactionContext = CurTransactionContext;
1217 :
1218 : /* Make the CurTransactionContext active. */
1219 1056948 : MemoryContextSwitchTo(CurTransactionContext);
1220 1056948 : }
1221 :
1222 : /*
1223 : * AtStart_ResourceOwner
1224 : */
1225 : static void
1226 1056948 : AtStart_ResourceOwner(void)
1227 : {
1228 1056948 : TransactionState s = CurrentTransactionState;
1229 :
1230 : /*
1231 : * We shouldn't have a transaction resource owner already.
1232 : */
1233 : Assert(TopTransactionResourceOwner == NULL);
1234 :
1235 : /*
1236 : * Create a toplevel resource owner for the transaction.
1237 : */
1238 1056948 : s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1239 :
1240 1056948 : TopTransactionResourceOwner = s->curTransactionOwner;
1241 1056948 : CurTransactionResourceOwner = s->curTransactionOwner;
1242 1056948 : CurrentResourceOwner = s->curTransactionOwner;
1243 1056948 : }
1244 :
1245 : /* ----------------------------------------------------------------
1246 : * StartSubTransaction stuff
1247 : * ----------------------------------------------------------------
1248 : */
1249 :
1250 : /*
1251 : * AtSubStart_Memory
1252 : */
1253 : static void
1254 20052 : AtSubStart_Memory(void)
1255 : {
1256 20052 : TransactionState s = CurrentTransactionState;
1257 :
1258 : Assert(CurTransactionContext != NULL);
1259 :
1260 : /*
1261 : * Remember the context that was active prior to subtransaction start.
1262 : */
1263 20052 : s->priorContext = CurrentMemoryContext;
1264 :
1265 : /*
1266 : * Create a CurTransactionContext, which will be used to hold data that
1267 : * survives subtransaction commit but disappears on subtransaction abort.
1268 : * We make it a child of the immediate parent's CurTransactionContext.
1269 : */
1270 20052 : CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
1271 : "CurTransactionContext",
1272 : ALLOCSET_DEFAULT_SIZES);
1273 20052 : s->curTransactionContext = CurTransactionContext;
1274 :
1275 : /* Make the CurTransactionContext active. */
1276 20052 : MemoryContextSwitchTo(CurTransactionContext);
1277 20052 : }
1278 :
1279 : /*
1280 : * AtSubStart_ResourceOwner
1281 : */
1282 : static void
1283 20052 : AtSubStart_ResourceOwner(void)
1284 : {
1285 20052 : TransactionState s = CurrentTransactionState;
1286 :
1287 : Assert(s->parent != NULL);
1288 :
1289 : /*
1290 : * Create a resource owner for the subtransaction. We make it a child of
1291 : * the immediate parent's resource owner.
1292 : */
1293 20052 : s->curTransactionOwner =
1294 20052 : ResourceOwnerCreate(s->parent->curTransactionOwner,
1295 : "SubTransaction");
1296 :
1297 20052 : CurTransactionResourceOwner = s->curTransactionOwner;
1298 20052 : CurrentResourceOwner = s->curTransactionOwner;
1299 20052 : }
1300 :
1301 : /* ----------------------------------------------------------------
1302 : * CommitTransaction stuff
1303 : * ----------------------------------------------------------------
1304 : */
1305 :
1306 : /*
1307 : * RecordTransactionCommit
1308 : *
1309 : * Returns latest XID among xact and its children, or InvalidTransactionId
1310 : * if the xact has no XID. (We compute that here just because it's easier.)
1311 : *
1312 : * If you change this function, see RecordTransactionCommitPrepared also.
1313 : */
1314 : static TransactionId
1315 1004318 : RecordTransactionCommit(void)
1316 : {
1317 1004318 : TransactionId xid = GetTopTransactionIdIfAny();
1318 1004318 : bool markXidCommitted = TransactionIdIsValid(xid);
1319 1004318 : TransactionId latestXid = InvalidTransactionId;
1320 : int nrels;
1321 : RelFileLocator *rels;
1322 : int nchildren;
1323 : TransactionId *children;
1324 1004318 : int ndroppedstats = 0;
1325 1004318 : xl_xact_stats_item *droppedstats = NULL;
1326 1004318 : int nmsgs = 0;
1327 1004318 : SharedInvalidationMessage *invalMessages = NULL;
1328 1004318 : bool RelcacheInitFileInval = false;
1329 : bool wrote_xlog;
1330 :
1331 : /*
1332 : * Log pending invalidations for logical decoding of in-progress
1333 : * transactions. Normally for DDLs, we log this at each command end,
1334 : * however, for certain cases where we directly update the system table
1335 : * without a transaction block, the invalidations are not logged till this
1336 : * time.
1337 : */
1338 1004318 : if (XLogLogicalInfoActive())
1339 25156 : LogLogicalInvalidations();
1340 :
1341 : /* Get data needed for commit record */
1342 1004318 : nrels = smgrGetPendingDeletes(true, &rels);
1343 1004318 : nchildren = xactGetCommittedChildren(&children);
1344 1004318 : ndroppedstats = pgstat_get_transactional_drops(true, &droppedstats);
1345 1004318 : if (XLogStandbyInfoActive())
1346 403112 : nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
1347 : &RelcacheInitFileInval);
1348 1004318 : wrote_xlog = (XactLastRecEnd != 0);
1349 :
1350 : /*
1351 : * If we haven't been assigned an XID yet, we neither can, nor do we want
1352 : * to write a COMMIT record.
1353 : */
1354 1004318 : if (!markXidCommitted)
1355 : {
1356 : /*
1357 : * We expect that every RelationDropStorage is followed by a catalog
1358 : * update, and hence XID assignment, so we shouldn't get here with any
1359 : * pending deletes. Same is true for dropping stats.
1360 : *
1361 : * Use a real test not just an Assert to check this, since it's a bit
1362 : * fragile.
1363 : */
1364 750978 : if (nrels != 0 || ndroppedstats != 0)
1365 0 : elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
1366 :
1367 : /* Can't have child XIDs either; AssignTransactionId enforces this */
1368 : Assert(nchildren == 0);
1369 :
1370 : /*
1371 : * Transactions without an assigned xid can contain invalidation
1372 : * messages. While inplace updates do this, this is not known to be
1373 : * necessary; see comment at inplace CacheInvalidateHeapTuple().
1374 : * Extensions might still rely on this capability, and standbys may
1375 : * need to process those invals. We can't emit a commit record
1376 : * without an xid, and we don't want to force assigning an xid,
1377 : * because that'd be problematic for e.g. vacuum. Hence we emit a
1378 : * bespoke record for the invalidations. We don't want to use that in
1379 : * case a commit record is emitted, so they happen synchronously with
1380 : * commits (besides not wanting to emit more WAL records).
1381 : *
1382 : * XXX Every known use of this capability is a defect. Since an XID
1383 : * isn't controlling visibility of the change that prompted invals,
1384 : * other sessions need the inval even if this transactions aborts.
1385 : *
1386 : * ON COMMIT DELETE ROWS does a nontransactional index_build(), which
1387 : * queues a relcache inval, including in transactions without an xid
1388 : * that had read the (empty) table. Standbys don't need any ON COMMIT
1389 : * DELETE ROWS invals, but we've not done the work to withhold them.
1390 : */
1391 750978 : if (nmsgs != 0)
1392 : {
1393 18248 : LogStandbyInvalidations(nmsgs, invalMessages,
1394 : RelcacheInitFileInval);
1395 18248 : wrote_xlog = true; /* not strictly necessary */
1396 : }
1397 :
1398 : /*
1399 : * If we didn't create XLOG entries, we're done here; otherwise we
1400 : * should trigger flushing those entries the same as a commit record
1401 : * would. This will primarily happen for HOT pruning and the like; we
1402 : * want these to be flushed to disk in due time.
1403 : */
1404 750978 : if (!wrote_xlog)
1405 678666 : goto cleanup;
1406 : }
1407 : else
1408 : {
1409 : bool replorigin;
1410 :
1411 : /*
1412 : * Are we using the replication origins feature? Or, in other words,
1413 : * are we replaying remote actions?
1414 : */
1415 255326 : replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1416 1986 : replorigin_session_origin != DoNotReplicateId);
1417 :
1418 : /*
1419 : * Mark ourselves as within our "commit critical section". This
1420 : * forces any concurrent checkpoint to wait until we've updated
1421 : * pg_xact. Without this, it is possible for the checkpoint to set
1422 : * REDO after the XLOG record but fail to flush the pg_xact update to
1423 : * disk, leading to loss of the transaction commit if the system
1424 : * crashes a little later.
1425 : *
1426 : * Note: we could, but don't bother to, set this flag in
1427 : * RecordTransactionAbort. That's because loss of a transaction abort
1428 : * is noncritical; the presumption would be that it aborted, anyway.
1429 : *
1430 : * It's safe to change the delayChkptFlags flag of our own backend
1431 : * without holding the ProcArrayLock, since we're the only one
1432 : * modifying it. This makes checkpoint's determination of which xacts
1433 : * are delaying the checkpoint a bit fuzzy, but it doesn't matter.
1434 : *
1435 : * Note, it is important to get the commit timestamp after marking the
1436 : * transaction in the commit critical section. See
1437 : * RecordTransactionCommitPrepared.
1438 : */
1439 : Assert((MyProc->delayChkptFlags & DELAY_CHKPT_IN_COMMIT) == 0);
1440 253340 : START_CRIT_SECTION();
1441 253340 : MyProc->delayChkptFlags |= DELAY_CHKPT_IN_COMMIT;
1442 :
1443 : Assert(xactStopTimestamp == 0);
1444 :
1445 : /*
1446 : * Ensures the DELAY_CHKPT_IN_COMMIT flag write is globally visible
1447 : * before commit time is written.
1448 : */
1449 253340 : pg_write_barrier();
1450 :
1451 : /*
1452 : * Insert the commit XLOG record.
1453 : */
1454 253340 : XactLogCommitRecord(GetCurrentTransactionStopTimestamp(),
1455 : nchildren, children, nrels, rels,
1456 : ndroppedstats, droppedstats,
1457 : nmsgs, invalMessages,
1458 : RelcacheInitFileInval,
1459 : MyXactFlags,
1460 : InvalidTransactionId, NULL /* plain commit */ );
1461 :
1462 253340 : if (replorigin)
1463 : /* Move LSNs forward for this replication origin */
1464 1986 : replorigin_session_advance(replorigin_session_origin_lsn,
1465 : XactLastRecEnd);
1466 :
1467 : /*
1468 : * Record commit timestamp. The value comes from plain commit
1469 : * timestamp if there's no replication origin; otherwise, the
1470 : * timestamp was already set in replorigin_session_origin_timestamp by
1471 : * replication.
1472 : *
1473 : * We don't need to WAL-log anything here, as the commit record
1474 : * written above already contains the data.
1475 : */
1476 :
1477 253340 : if (!replorigin || replorigin_session_origin_timestamp == 0)
1478 251548 : replorigin_session_origin_timestamp = GetCurrentTransactionStopTimestamp();
1479 :
1480 253340 : TransactionTreeSetCommitTsData(xid, nchildren, children,
1481 : replorigin_session_origin_timestamp,
1482 : replorigin_session_origin);
1483 : }
1484 :
1485 : /*
1486 : * Check if we want to commit asynchronously. We can allow the XLOG flush
1487 : * to happen asynchronously if synchronous_commit=off, or if the current
1488 : * transaction has not performed any WAL-logged operation or didn't assign
1489 : * an xid. The transaction can end up not writing any WAL, even if it has
1490 : * an xid, if it only wrote to temporary and/or unlogged tables. It can
1491 : * end up having written WAL without an xid if it did HOT pruning. In
1492 : * case of a crash, the loss of such a transaction will be irrelevant;
1493 : * temp tables will be lost anyway, unlogged tables will be truncated and
1494 : * HOT pruning will be done again later. (Given the foregoing, you might
1495 : * think that it would be unnecessary to emit the XLOG record at all in
1496 : * this case, but we don't currently try to do that. It would certainly
1497 : * cause problems at least in Hot Standby mode, where the
1498 : * KnownAssignedXids machinery requires tracking every XID assignment. It
1499 : * might be OK to skip it only when wal_level < replica, but for now we
1500 : * don't.)
1501 : *
1502 : * However, if we're doing cleanup of any non-temp rels or committing any
1503 : * command that wanted to force sync commit, then we must flush XLOG
1504 : * immediately. (We must not allow asynchronous commit if there are any
1505 : * non-temp tables to be deleted, because we might delete the files before
1506 : * the COMMIT record is flushed to disk. We do allow asynchronous commit
1507 : * if all to-be-deleted tables are temporary though, since they are lost
1508 : * anyway if we crash.)
1509 : */
1510 325652 : if ((wrote_xlog && markXidCommitted &&
1511 325652 : synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
1512 84552 : forceSyncCommit || nrels > 0)
1513 : {
1514 241136 : XLogFlush(XactLastRecEnd);
1515 :
1516 : /*
1517 : * Now we may update the CLOG, if we wrote a COMMIT record above
1518 : */
1519 241136 : if (markXidCommitted)
1520 241136 : TransactionIdCommitTree(xid, nchildren, children);
1521 : }
1522 : else
1523 : {
1524 : /*
1525 : * Asynchronous commit case:
1526 : *
1527 : * This enables possible committed transaction loss in the case of a
1528 : * postmaster crash because WAL buffers are left unwritten. Ideally we
1529 : * could issue the WAL write without the fsync, but some
1530 : * wal_sync_methods do not allow separate write/fsync.
1531 : *
1532 : * Report the latest async commit LSN, so that the WAL writer knows to
1533 : * flush this commit.
1534 : */
1535 84516 : XLogSetAsyncXactLSN(XactLastRecEnd);
1536 :
1537 : /*
1538 : * We must not immediately update the CLOG, since we didn't flush the
1539 : * XLOG. Instead, we store the LSN up to which the XLOG must be
1540 : * flushed before the CLOG may be updated.
1541 : */
1542 84516 : if (markXidCommitted)
1543 12204 : TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
1544 : }
1545 :
1546 : /*
1547 : * If we entered a commit critical section, leave it now, and let
1548 : * checkpoints proceed.
1549 : */
1550 325652 : if (markXidCommitted)
1551 : {
1552 253340 : MyProc->delayChkptFlags &= ~DELAY_CHKPT_IN_COMMIT;
1553 253340 : END_CRIT_SECTION();
1554 : }
1555 :
1556 : /* Compute latestXid while we have the child XIDs handy */
1557 325652 : latestXid = TransactionIdLatest(xid, nchildren, children);
1558 :
1559 : /*
1560 : * Wait for synchronous replication, if required. Similar to the decision
1561 : * above about using committing asynchronously we only want to wait if
1562 : * this backend assigned an xid and wrote WAL. No need to wait if an xid
1563 : * was assigned due to temporary/unlogged tables or due to HOT pruning.
1564 : *
1565 : * Note that at this stage we have marked clog, but still show as running
1566 : * in the procarray and continue to hold locks.
1567 : */
1568 325652 : if (wrote_xlog && markXidCommitted)
1569 245414 : SyncRepWaitForLSN(XactLastRecEnd, true);
1570 :
1571 : /* remember end of last commit record */
1572 325652 : XactLastCommitEnd = XactLastRecEnd;
1573 :
1574 : /* Reset XactLastRecEnd until the next transaction writes something */
1575 325652 : XactLastRecEnd = 0;
1576 1004318 : cleanup:
1577 : /* Clean up local data */
1578 1004318 : if (rels)
1579 19540 : pfree(rels);
1580 1004318 : if (ndroppedstats)
1581 21160 : pfree(droppedstats);
1582 :
1583 1004318 : return latestXid;
1584 : }
1585 :
1586 :
1587 : /*
1588 : * AtCCI_LocalCache
1589 : */
1590 : static void
1591 1150314 : AtCCI_LocalCache(void)
1592 : {
1593 : /*
1594 : * Make any pending relation map changes visible. We must do this before
1595 : * processing local sinval messages, so that the map changes will get
1596 : * reflected into the relcache when relcache invals are processed.
1597 : */
1598 1150314 : AtCCI_RelationMap();
1599 :
1600 : /*
1601 : * Make catalog changes visible to me for the next command.
1602 : */
1603 1150314 : CommandEndInvalidationMessages();
1604 1150308 : }
1605 :
1606 : /*
1607 : * AtCommit_Memory
1608 : */
1609 : static void
1610 1007622 : AtCommit_Memory(void)
1611 : {
1612 1007622 : TransactionState s = CurrentTransactionState;
1613 :
1614 : /*
1615 : * Return to the memory context that was current before we started the
1616 : * transaction. (In principle, this could not be any of the contexts we
1617 : * are about to delete. If it somehow is, assertions in mcxt.c will
1618 : * complain.)
1619 : */
1620 1007622 : MemoryContextSwitchTo(s->priorContext);
1621 :
1622 : /*
1623 : * Release all transaction-local memory. TopTransactionContext survives
1624 : * but becomes empty; any sub-contexts go away.
1625 : */
1626 : Assert(TopTransactionContext != NULL);
1627 1007622 : MemoryContextReset(TopTransactionContext);
1628 :
1629 : /*
1630 : * Clear these pointers as a pro-forma matter. (Notionally, while
1631 : * TopTransactionContext still exists, it's currently not associated with
1632 : * this TransactionState struct.)
1633 : */
1634 1007622 : CurTransactionContext = NULL;
1635 1007622 : s->curTransactionContext = NULL;
1636 1007622 : }
1637 :
1638 : /* ----------------------------------------------------------------
1639 : * CommitSubTransaction stuff
1640 : * ----------------------------------------------------------------
1641 : */
1642 :
1643 : /*
1644 : * AtSubCommit_Memory
1645 : */
1646 : static void
1647 10686 : AtSubCommit_Memory(void)
1648 : {
1649 10686 : TransactionState s = CurrentTransactionState;
1650 :
1651 : Assert(s->parent != NULL);
1652 :
1653 : /* Return to parent transaction level's memory context. */
1654 10686 : CurTransactionContext = s->parent->curTransactionContext;
1655 10686 : MemoryContextSwitchTo(CurTransactionContext);
1656 :
1657 : /*
1658 : * Ordinarily we cannot throw away the child's CurTransactionContext,
1659 : * since the data it contains will be needed at upper commit. However, if
1660 : * there isn't actually anything in it, we can throw it away. This avoids
1661 : * a small memory leak in the common case of "trivial" subxacts.
1662 : */
1663 10686 : if (MemoryContextIsEmpty(s->curTransactionContext))
1664 : {
1665 10668 : MemoryContextDelete(s->curTransactionContext);
1666 10668 : s->curTransactionContext = NULL;
1667 : }
1668 10686 : }
1669 :
1670 : /*
1671 : * AtSubCommit_childXids
1672 : *
1673 : * Pass my own XID and my child XIDs up to my parent as committed children.
1674 : */
1675 : static void
1676 7366 : AtSubCommit_childXids(void)
1677 : {
1678 7366 : TransactionState s = CurrentTransactionState;
1679 : int new_nChildXids;
1680 :
1681 : Assert(s->parent != NULL);
1682 :
1683 : /*
1684 : * The parent childXids array will need to hold my XID and all my
1685 : * childXids, in addition to the XIDs already there.
1686 : */
1687 7366 : new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1688 :
1689 : /* Allocate or enlarge the parent array if necessary */
1690 7366 : if (s->parent->maxChildXids < new_nChildXids)
1691 : {
1692 : int new_maxChildXids;
1693 : TransactionId *new_childXids;
1694 :
1695 : /*
1696 : * Make it 2x what's needed right now, to avoid having to enlarge it
1697 : * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1698 : * is what ensures that we don't need to worry about integer overflow
1699 : * here or in the calculation of new_nChildXids.)
1700 : */
1701 3298 : new_maxChildXids = Min(new_nChildXids * 2,
1702 : (int) (MaxAllocSize / sizeof(TransactionId)));
1703 :
1704 3298 : if (new_maxChildXids < new_nChildXids)
1705 0 : ereport(ERROR,
1706 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1707 : errmsg("maximum number of committed subtransactions (%d) exceeded",
1708 : (int) (MaxAllocSize / sizeof(TransactionId)))));
1709 :
1710 : /*
1711 : * We keep the child-XID arrays in TopTransactionContext; this avoids
1712 : * setting up child-transaction contexts for what might be just a few
1713 : * bytes of grandchild XIDs.
1714 : */
1715 3298 : if (s->parent->childXids == NULL)
1716 : new_childXids =
1717 3214 : MemoryContextAlloc(TopTransactionContext,
1718 : new_maxChildXids * sizeof(TransactionId));
1719 : else
1720 84 : new_childXids = repalloc(s->parent->childXids,
1721 : new_maxChildXids * sizeof(TransactionId));
1722 :
1723 3298 : s->parent->childXids = new_childXids;
1724 3298 : s->parent->maxChildXids = new_maxChildXids;
1725 : }
1726 :
1727 : /*
1728 : * Copy all my XIDs to parent's array.
1729 : *
1730 : * Note: We rely on the fact that the XID of a child always follows that
1731 : * of its parent. By copying the XID of this subtransaction before the
1732 : * XIDs of its children, we ensure that the array stays ordered. Likewise,
1733 : * all XIDs already in the array belong to subtransactions started and
1734 : * subcommitted before us, so their XIDs must precede ours.
1735 : */
1736 7366 : s->parent->childXids[s->parent->nChildXids] = XidFromFullTransactionId(s->fullTransactionId);
1737 :
1738 7366 : if (s->nChildXids > 0)
1739 2016 : memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1740 2016 : s->childXids,
1741 2016 : s->nChildXids * sizeof(TransactionId));
1742 :
1743 7366 : s->parent->nChildXids = new_nChildXids;
1744 :
1745 : /* Release child's array to avoid leakage */
1746 7366 : if (s->childXids != NULL)
1747 2016 : pfree(s->childXids);
1748 : /* We must reset these to avoid double-free if fail later in commit */
1749 7366 : s->childXids = NULL;
1750 7366 : s->nChildXids = 0;
1751 7366 : s->maxChildXids = 0;
1752 7366 : }
1753 :
1754 : /* ----------------------------------------------------------------
1755 : * AbortTransaction stuff
1756 : * ----------------------------------------------------------------
1757 : */
1758 :
1759 : /*
1760 : * RecordTransactionAbort
1761 : *
1762 : * Returns latest XID among xact and its children, or InvalidTransactionId
1763 : * if the xact has no XID. (We compute that here just because it's easier.)
1764 : */
1765 : static TransactionId
1766 58680 : RecordTransactionAbort(bool isSubXact)
1767 : {
1768 58680 : TransactionId xid = GetCurrentTransactionIdIfAny();
1769 : TransactionId latestXid;
1770 : int nrels;
1771 : RelFileLocator *rels;
1772 58680 : int ndroppedstats = 0;
1773 58680 : xl_xact_stats_item *droppedstats = NULL;
1774 : int nchildren;
1775 : TransactionId *children;
1776 : TimestampTz xact_time;
1777 : bool replorigin;
1778 :
1779 : /*
1780 : * If we haven't been assigned an XID, nobody will care whether we aborted
1781 : * or not. Hence, we're done in that case. It does not matter if we have
1782 : * rels to delete (note that this routine is not responsible for actually
1783 : * deleting 'em). We cannot have any child XIDs, either.
1784 : */
1785 58680 : if (!TransactionIdIsValid(xid))
1786 : {
1787 : /* Reset XactLastRecEnd until the next transaction writes something */
1788 46848 : if (!isSubXact)
1789 38780 : XactLastRecEnd = 0;
1790 46848 : return InvalidTransactionId;
1791 : }
1792 :
1793 : /*
1794 : * We have a valid XID, so we should write an ABORT record for it.
1795 : *
1796 : * We do not flush XLOG to disk here, since the default assumption after a
1797 : * crash would be that we aborted, anyway. For the same reason, we don't
1798 : * need to worry about interlocking against checkpoint start.
1799 : */
1800 :
1801 : /*
1802 : * Check that we haven't aborted halfway through RecordTransactionCommit.
1803 : */
1804 11832 : if (TransactionIdDidCommit(xid))
1805 0 : elog(PANIC, "cannot abort transaction %u, it was already committed",
1806 : xid);
1807 :
1808 : /*
1809 : * Are we using the replication origins feature? Or, in other words, are
1810 : * we replaying remote actions?
1811 : */
1812 11886 : replorigin = (replorigin_session_origin != InvalidRepOriginId &&
1813 54 : replorigin_session_origin != DoNotReplicateId);
1814 :
1815 : /* Fetch the data we need for the abort record */
1816 11832 : nrels = smgrGetPendingDeletes(false, &rels);
1817 11832 : nchildren = xactGetCommittedChildren(&children);
1818 11832 : ndroppedstats = pgstat_get_transactional_drops(false, &droppedstats);
1819 :
1820 : /* XXX do we really need a critical section here? */
1821 11832 : START_CRIT_SECTION();
1822 :
1823 : /* Write the ABORT record */
1824 11832 : if (isSubXact)
1825 1298 : xact_time = GetCurrentTimestamp();
1826 : else
1827 : {
1828 10534 : xact_time = GetCurrentTransactionStopTimestamp();
1829 : }
1830 :
1831 11832 : XactLogAbortRecord(xact_time,
1832 : nchildren, children,
1833 : nrels, rels,
1834 : ndroppedstats, droppedstats,
1835 : MyXactFlags, InvalidTransactionId,
1836 : NULL);
1837 :
1838 11832 : if (replorigin)
1839 : /* Move LSNs forward for this replication origin */
1840 54 : replorigin_session_advance(replorigin_session_origin_lsn,
1841 : XactLastRecEnd);
1842 :
1843 : /*
1844 : * Report the latest async abort LSN, so that the WAL writer knows to
1845 : * flush this abort. There's nothing to be gained by delaying this, since
1846 : * WALWriter may as well do this when it can. This is important with
1847 : * streaming replication because if we don't flush WAL regularly we will
1848 : * find that large aborts leave us with a long backlog for when commits
1849 : * occur after the abort, increasing our window of data loss should
1850 : * problems occur at that point.
1851 : */
1852 11832 : if (!isSubXact)
1853 10534 : XLogSetAsyncXactLSN(XactLastRecEnd);
1854 :
1855 : /*
1856 : * Mark the transaction aborted in clog. This is not absolutely necessary
1857 : * but we may as well do it while we are here; also, in the subxact case
1858 : * it is helpful because XactLockTableWait makes use of it to avoid
1859 : * waiting for already-aborted subtransactions. It is OK to do it without
1860 : * having flushed the ABORT record to disk, because in event of a crash
1861 : * we'd be assumed to have aborted anyway.
1862 : */
1863 11832 : TransactionIdAbortTree(xid, nchildren, children);
1864 :
1865 11832 : END_CRIT_SECTION();
1866 :
1867 : /* Compute latestXid while we have the child XIDs handy */
1868 11832 : latestXid = TransactionIdLatest(xid, nchildren, children);
1869 :
1870 : /*
1871 : * If we're aborting a subtransaction, we can immediately remove failed
1872 : * XIDs from PGPROC's cache of running child XIDs. We do that here for
1873 : * subxacts, because we already have the child XID array at hand. For
1874 : * main xacts, the equivalent happens just after this function returns.
1875 : */
1876 11832 : if (isSubXact)
1877 1298 : XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
1878 :
1879 : /* Reset XactLastRecEnd until the next transaction writes something */
1880 11832 : if (!isSubXact)
1881 10534 : XactLastRecEnd = 0;
1882 :
1883 : /* And clean up local data */
1884 11832 : if (rels)
1885 1998 : pfree(rels);
1886 11832 : if (ndroppedstats)
1887 2736 : pfree(droppedstats);
1888 :
1889 11832 : return latestXid;
1890 : }
1891 :
1892 : /*
1893 : * AtAbort_Memory
1894 : */
1895 : static void
1896 84242 : AtAbort_Memory(void)
1897 : {
1898 : /*
1899 : * Switch into TransactionAbortContext, which should have some free space
1900 : * even if nothing else does. We'll work in this context until we've
1901 : * finished cleaning up.
1902 : *
1903 : * It is barely possible to get here when we've not been able to create
1904 : * TransactionAbortContext yet; if so use TopMemoryContext.
1905 : */
1906 84242 : if (TransactionAbortContext != NULL)
1907 84242 : MemoryContextSwitchTo(TransactionAbortContext);
1908 : else
1909 0 : MemoryContextSwitchTo(TopMemoryContext);
1910 84242 : }
1911 :
1912 : /*
1913 : * AtSubAbort_Memory
1914 : */
1915 : static void
1916 9366 : AtSubAbort_Memory(void)
1917 : {
1918 : Assert(TransactionAbortContext != NULL);
1919 :
1920 9366 : MemoryContextSwitchTo(TransactionAbortContext);
1921 9366 : }
1922 :
1923 :
1924 : /*
1925 : * AtAbort_ResourceOwner
1926 : */
1927 : static void
1928 49326 : AtAbort_ResourceOwner(void)
1929 : {
1930 : /*
1931 : * Make sure we have a valid ResourceOwner, if possible (else it will be
1932 : * NULL, which is OK)
1933 : */
1934 49326 : CurrentResourceOwner = TopTransactionResourceOwner;
1935 49326 : }
1936 :
1937 : /*
1938 : * AtSubAbort_ResourceOwner
1939 : */
1940 : static void
1941 9366 : AtSubAbort_ResourceOwner(void)
1942 : {
1943 9366 : TransactionState s = CurrentTransactionState;
1944 :
1945 : /* Make sure we have a valid ResourceOwner */
1946 9366 : CurrentResourceOwner = s->curTransactionOwner;
1947 9366 : }
1948 :
1949 :
1950 : /*
1951 : * AtSubAbort_childXids
1952 : */
1953 : static void
1954 1298 : AtSubAbort_childXids(void)
1955 : {
1956 1298 : TransactionState s = CurrentTransactionState;
1957 :
1958 : /*
1959 : * We keep the child-XID arrays in TopTransactionContext (see
1960 : * AtSubCommit_childXids). This means we'd better free the array
1961 : * explicitly at abort to avoid leakage.
1962 : */
1963 1298 : if (s->childXids != NULL)
1964 50 : pfree(s->childXids);
1965 1298 : s->childXids = NULL;
1966 1298 : s->nChildXids = 0;
1967 1298 : s->maxChildXids = 0;
1968 :
1969 : /*
1970 : * We could prune the unreportedXids array here. But we don't bother. That
1971 : * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1972 : * would likely introduce more CPU time into the more common paths, so we
1973 : * choose not to do that.
1974 : */
1975 1298 : }
1976 :
1977 : /* ----------------------------------------------------------------
1978 : * CleanupTransaction stuff
1979 : * ----------------------------------------------------------------
1980 : */
1981 :
1982 : /*
1983 : * AtCleanup_Memory
1984 : */
1985 : static void
1986 49326 : AtCleanup_Memory(void)
1987 : {
1988 49326 : TransactionState s = CurrentTransactionState;
1989 :
1990 : /* Should be at top level */
1991 : Assert(s->parent == NULL);
1992 :
1993 : /*
1994 : * Return to the memory context that was current before we started the
1995 : * transaction. (In principle, this could not be any of the contexts we
1996 : * are about to delete. If it somehow is, assertions in mcxt.c will
1997 : * complain.)
1998 : */
1999 49326 : MemoryContextSwitchTo(s->priorContext);
2000 :
2001 : /*
2002 : * Clear the special abort context for next time.
2003 : */
2004 49326 : if (TransactionAbortContext != NULL)
2005 49326 : MemoryContextReset(TransactionAbortContext);
2006 :
2007 : /*
2008 : * Release all transaction-local memory, the same as in AtCommit_Memory,
2009 : * except we must cope with the possibility that we didn't get as far as
2010 : * creating TopTransactionContext.
2011 : */
2012 49326 : if (TopTransactionContext != NULL)
2013 49326 : MemoryContextReset(TopTransactionContext);
2014 :
2015 : /*
2016 : * Clear these pointers as a pro-forma matter. (Notionally, while
2017 : * TopTransactionContext still exists, it's currently not associated with
2018 : * this TransactionState struct.)
2019 : */
2020 49326 : CurTransactionContext = NULL;
2021 49326 : s->curTransactionContext = NULL;
2022 49326 : }
2023 :
2024 :
2025 : /* ----------------------------------------------------------------
2026 : * CleanupSubTransaction stuff
2027 : * ----------------------------------------------------------------
2028 : */
2029 :
2030 : /*
2031 : * AtSubCleanup_Memory
2032 : */
2033 : static void
2034 9366 : AtSubCleanup_Memory(void)
2035 : {
2036 9366 : TransactionState s = CurrentTransactionState;
2037 :
2038 : Assert(s->parent != NULL);
2039 :
2040 : /*
2041 : * Return to the memory context that was current before we started the
2042 : * subtransaction. (In principle, this could not be any of the contexts
2043 : * we are about to delete. If it somehow is, assertions in mcxt.c will
2044 : * complain.)
2045 : */
2046 9366 : MemoryContextSwitchTo(s->priorContext);
2047 :
2048 : /* Update CurTransactionContext (might not be same as priorContext) */
2049 9366 : CurTransactionContext = s->parent->curTransactionContext;
2050 :
2051 : /*
2052 : * Clear the special abort context for next time.
2053 : */
2054 9366 : if (TransactionAbortContext != NULL)
2055 9366 : MemoryContextReset(TransactionAbortContext);
2056 :
2057 : /*
2058 : * Delete the subxact local memory contexts. Its CurTransactionContext can
2059 : * go too (note this also kills CurTransactionContexts from any children
2060 : * of the subxact).
2061 : */
2062 9366 : if (s->curTransactionContext)
2063 9366 : MemoryContextDelete(s->curTransactionContext);
2064 9366 : s->curTransactionContext = NULL;
2065 9366 : }
2066 :
2067 : /* ----------------------------------------------------------------
2068 : * interface routines
2069 : * ----------------------------------------------------------------
2070 : */
2071 :
2072 : /*
2073 : * StartTransaction
2074 : */
2075 : static void
2076 1056948 : StartTransaction(void)
2077 : {
2078 : TransactionState s;
2079 : VirtualTransactionId vxid;
2080 :
2081 : /*
2082 : * Let's just make sure the state stack is empty
2083 : */
2084 1056948 : s = &TopTransactionStateData;
2085 1056948 : CurrentTransactionState = s;
2086 :
2087 : Assert(!FullTransactionIdIsValid(XactTopFullTransactionId));
2088 :
2089 : /* check the current transaction state */
2090 : Assert(s->state == TRANS_DEFAULT);
2091 :
2092 : /*
2093 : * Set the current transaction state information appropriately during
2094 : * start processing. Note that once the transaction status is switched
2095 : * this process cannot fail until the user ID and the security context
2096 : * flags are fetched below.
2097 : */
2098 1056948 : s->state = TRANS_START;
2099 1056948 : s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
2100 :
2101 : /* Determine if statements are logged in this transaction */
2102 1056948 : xact_is_sampled = log_xact_sample_rate != 0 &&
2103 0 : (log_xact_sample_rate == 1 ||
2104 0 : pg_prng_double(&pg_global_prng_state) <= log_xact_sample_rate);
2105 :
2106 : /*
2107 : * initialize current transaction state fields
2108 : *
2109 : * note: prevXactReadOnly is not used at the outermost level
2110 : */
2111 1056948 : s->nestingLevel = 1;
2112 1056948 : s->gucNestLevel = 1;
2113 1056948 : s->childXids = NULL;
2114 1056948 : s->nChildXids = 0;
2115 1056948 : s->maxChildXids = 0;
2116 :
2117 : /*
2118 : * Once the current user ID and the security context flags are fetched,
2119 : * both will be properly reset even if transaction startup fails.
2120 : */
2121 1056948 : GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
2122 :
2123 : /* SecurityRestrictionContext should never be set outside a transaction */
2124 : Assert(s->prevSecContext == 0);
2125 :
2126 : /*
2127 : * Make sure we've reset xact state variables
2128 : *
2129 : * If recovery is still in progress, mark this transaction as read-only.
2130 : * We have lower level defences in XLogInsert and elsewhere to stop us
2131 : * from modifying data during recovery, but this gives the normal
2132 : * indication to the user that the transaction is read-only.
2133 : */
2134 1056948 : if (RecoveryInProgress())
2135 : {
2136 3322 : s->startedInRecovery = true;
2137 3322 : XactReadOnly = true;
2138 : }
2139 : else
2140 : {
2141 1053626 : s->startedInRecovery = false;
2142 1053626 : XactReadOnly = DefaultXactReadOnly;
2143 : }
2144 1056948 : XactDeferrable = DefaultXactDeferrable;
2145 1056948 : XactIsoLevel = DefaultXactIsoLevel;
2146 1056948 : forceSyncCommit = false;
2147 1056948 : MyXactFlags = 0;
2148 :
2149 : /*
2150 : * reinitialize within-transaction counters
2151 : */
2152 1056948 : s->subTransactionId = TopSubTransactionId;
2153 1056948 : currentSubTransactionId = TopSubTransactionId;
2154 1056948 : currentCommandId = FirstCommandId;
2155 1056948 : currentCommandIdUsed = false;
2156 :
2157 : /*
2158 : * initialize reported xid accounting
2159 : */
2160 1056948 : nUnreportedXids = 0;
2161 1056948 : s->didLogXid = false;
2162 :
2163 : /*
2164 : * must initialize resource-management stuff first
2165 : */
2166 1056948 : AtStart_Memory();
2167 1056948 : AtStart_ResourceOwner();
2168 :
2169 : /*
2170 : * Assign a new LocalTransactionId, and combine it with the proc number to
2171 : * form a virtual transaction id.
2172 : */
2173 1056948 : vxid.procNumber = MyProcNumber;
2174 1056948 : vxid.localTransactionId = GetNextLocalTransactionId();
2175 :
2176 : /*
2177 : * Lock the virtual transaction id before we announce it in the proc array
2178 : */
2179 1056948 : VirtualXactLockTableInsert(vxid);
2180 :
2181 : /*
2182 : * Advertise it in the proc array. We assume assignment of
2183 : * localTransactionId is atomic, and the proc number should be set
2184 : * already.
2185 : */
2186 : Assert(MyProc->vxid.procNumber == vxid.procNumber);
2187 1056948 : MyProc->vxid.lxid = vxid.localTransactionId;
2188 :
2189 : TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
2190 :
2191 : /*
2192 : * set transaction_timestamp() (a/k/a now()). Normally, we want this to
2193 : * be the same as the first command's statement_timestamp(), so don't do a
2194 : * fresh GetCurrentTimestamp() call (which'd be expensive anyway). But
2195 : * for transactions started inside procedures (i.e., nonatomic SPI
2196 : * contexts), we do need to advance the timestamp. Also, in a parallel
2197 : * worker, the timestamp should already have been provided by a call to
2198 : * SetParallelStartTimestamps().
2199 : */
2200 1056948 : if (!IsParallelWorker())
2201 : {
2202 1048710 : if (!SPI_inside_nonatomic_context())
2203 1044278 : xactStartTimestamp = stmtStartTimestamp;
2204 : else
2205 4432 : xactStartTimestamp = GetCurrentTimestamp();
2206 : }
2207 : else
2208 : Assert(xactStartTimestamp != 0);
2209 1056948 : pgstat_report_xact_timestamp(xactStartTimestamp);
2210 : /* Mark xactStopTimestamp as unset. */
2211 1056948 : xactStopTimestamp = 0;
2212 :
2213 : /*
2214 : * initialize other subsystems for new transaction
2215 : */
2216 1056948 : AtStart_GUC();
2217 1056948 : AtStart_Cache();
2218 1056948 : AfterTriggerBeginXact();
2219 :
2220 : /*
2221 : * done with start processing, set current transaction state to "in
2222 : * progress"
2223 : */
2224 1056948 : s->state = TRANS_INPROGRESS;
2225 :
2226 : /* Schedule transaction timeout */
2227 1056948 : if (TransactionTimeout > 0)
2228 2 : enable_timeout_after(TRANSACTION_TIMEOUT, TransactionTimeout);
2229 :
2230 1056948 : ShowTransactionState("StartTransaction");
2231 1056948 : }
2232 :
2233 :
2234 : /*
2235 : * CommitTransaction
2236 : *
2237 : * NB: if you change this routine, better look at PrepareTransaction too!
2238 : */
2239 : static void
2240 1007522 : CommitTransaction(void)
2241 : {
2242 1007522 : TransactionState s = CurrentTransactionState;
2243 : TransactionId latestXid;
2244 : bool is_parallel_worker;
2245 :
2246 1007522 : is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2247 :
2248 : /* Enforce parallel mode restrictions during parallel worker commit. */
2249 1007522 : if (is_parallel_worker)
2250 2734 : EnterParallelMode();
2251 :
2252 1007522 : ShowTransactionState("CommitTransaction");
2253 :
2254 : /*
2255 : * check the current transaction state
2256 : */
2257 1007522 : if (s->state != TRANS_INPROGRESS)
2258 0 : elog(WARNING, "CommitTransaction while in %s state",
2259 : TransStateAsString(s->state));
2260 : Assert(s->parent == NULL);
2261 :
2262 : /*
2263 : * Do pre-commit processing that involves calling user-defined code, such
2264 : * as triggers. SECURITY_RESTRICTED_OPERATION contexts must not queue an
2265 : * action that would run here, because that would bypass the sandbox.
2266 : * Since closing cursors could queue trigger actions, triggers could open
2267 : * cursors, etc, we have to keep looping until there's nothing left to do.
2268 : */
2269 : for (;;)
2270 : {
2271 : /*
2272 : * Fire all currently pending deferred triggers.
2273 : */
2274 1018606 : AfterTriggerFireDeferred();
2275 :
2276 : /*
2277 : * Close open portals (converting holdable ones into static portals).
2278 : * If there weren't any, we are done ... otherwise loop back to check
2279 : * if they queued deferred triggers. Lather, rinse, repeat.
2280 : */
2281 1018452 : if (!PreCommit_Portals(false))
2282 1007368 : break;
2283 : }
2284 :
2285 : /*
2286 : * The remaining actions cannot call any user-defined code, so it's safe
2287 : * to start shutting down within-transaction services. But note that most
2288 : * of this stuff could still throw an error, which would switch us into
2289 : * the transaction-abort path.
2290 : */
2291 :
2292 1007368 : CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_PRE_COMMIT
2293 : : XACT_EVENT_PRE_COMMIT);
2294 :
2295 : /*
2296 : * If this xact has started any unfinished parallel operation, clean up
2297 : * its workers, warning about leaked resources. (But we don't actually
2298 : * reset parallelModeLevel till entering TRANS_COMMIT, a bit below. This
2299 : * keeps parallel mode restrictions active as long as possible in a
2300 : * parallel worker.)
2301 : */
2302 1007368 : AtEOXact_Parallel(true);
2303 1007368 : if (is_parallel_worker)
2304 : {
2305 2734 : if (s->parallelModeLevel != 1)
2306 0 : elog(WARNING, "parallelModeLevel is %d not 1 at end of parallel worker transaction",
2307 : s->parallelModeLevel);
2308 : }
2309 : else
2310 : {
2311 1004634 : if (s->parallelModeLevel != 0)
2312 0 : elog(WARNING, "parallelModeLevel is %d not 0 at end of transaction",
2313 : s->parallelModeLevel);
2314 : }
2315 :
2316 : /* Shut down the deferred-trigger manager */
2317 1007368 : AfterTriggerEndXact(true);
2318 :
2319 : /*
2320 : * Let ON COMMIT management do its thing (must happen after closing
2321 : * cursors, to avoid dangling-reference problems)
2322 : */
2323 1007368 : PreCommit_on_commit_actions();
2324 :
2325 : /*
2326 : * Synchronize files that are created and not WAL-logged during this
2327 : * transaction. This must happen before AtEOXact_RelationMap(), so that we
2328 : * don't see committed-but-broken files after a crash.
2329 : */
2330 1007362 : smgrDoPendingSyncs(true, is_parallel_worker);
2331 :
2332 : /* close large objects before lower-level cleanup */
2333 1007362 : AtEOXact_LargeObject(true);
2334 :
2335 : /*
2336 : * Insert notifications sent by NOTIFY commands into the queue. This
2337 : * should be late in the pre-commit sequence to minimize time spent
2338 : * holding the notify-insertion lock. However, this could result in
2339 : * creating a snapshot, so we must do it before serializable cleanup.
2340 : */
2341 1007362 : PreCommit_Notify();
2342 :
2343 : /*
2344 : * Mark serializable transaction as complete for predicate locking
2345 : * purposes. This should be done as late as we can put it and still allow
2346 : * errors to be raised for failure patterns found at commit. This is not
2347 : * appropriate in a parallel worker however, because we aren't committing
2348 : * the leader's transaction and its serializable state will live on.
2349 : */
2350 1007362 : if (!is_parallel_worker)
2351 1004628 : PreCommit_CheckForSerializationFailure();
2352 :
2353 : /* Prevent cancel/die interrupt while cleaning up */
2354 1007052 : HOLD_INTERRUPTS();
2355 :
2356 : /* Commit updates to the relation map --- do this as late as possible */
2357 1007052 : AtEOXact_RelationMap(true, is_parallel_worker);
2358 :
2359 : /*
2360 : * set the current transaction state information appropriately during
2361 : * commit processing
2362 : */
2363 1007052 : s->state = TRANS_COMMIT;
2364 1007052 : s->parallelModeLevel = 0;
2365 1007052 : s->parallelChildXact = false; /* should be false already */
2366 :
2367 : /* Disable transaction timeout */
2368 1007052 : if (TransactionTimeout > 0)
2369 2 : disable_timeout(TRANSACTION_TIMEOUT, false);
2370 :
2371 1007052 : if (!is_parallel_worker)
2372 : {
2373 : /*
2374 : * We need to mark our XIDs as committed in pg_xact. This is where we
2375 : * durably commit.
2376 : */
2377 1004318 : latestXid = RecordTransactionCommit();
2378 : }
2379 : else
2380 : {
2381 : /*
2382 : * We must not mark our XID committed; the parallel leader is
2383 : * responsible for that.
2384 : */
2385 2734 : latestXid = InvalidTransactionId;
2386 :
2387 : /*
2388 : * Make sure the leader will know about any WAL we wrote before it
2389 : * commits.
2390 : */
2391 2734 : ParallelWorkerReportLastRecEnd(XactLastRecEnd);
2392 : }
2393 :
2394 : TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->vxid.lxid);
2395 :
2396 : /*
2397 : * Let others know about no transaction in progress by me. Note that this
2398 : * must be done _before_ releasing locks we hold and _after_
2399 : * RecordTransactionCommit.
2400 : */
2401 1007052 : ProcArrayEndTransaction(MyProc, latestXid);
2402 :
2403 : /*
2404 : * This is all post-commit cleanup. Note that if an error is raised here,
2405 : * it's too late to abort the transaction. This should be just
2406 : * noncritical resource releasing.
2407 : *
2408 : * The ordering of operations is not entirely random. The idea is:
2409 : * release resources visible to other backends (eg, files, buffer pins);
2410 : * then release locks; then release backend-local resources. We want to
2411 : * release locks at the point where any backend waiting for us will see
2412 : * our transaction as being fully cleaned up.
2413 : *
2414 : * Resources that can be associated with individual queries are handled by
2415 : * the ResourceOwner mechanism. The other calls here are for backend-wide
2416 : * state.
2417 : */
2418 :
2419 1007052 : CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2420 : : XACT_EVENT_COMMIT);
2421 :
2422 1007052 : CurrentResourceOwner = NULL;
2423 1007052 : ResourceOwnerRelease(TopTransactionResourceOwner,
2424 : RESOURCE_RELEASE_BEFORE_LOCKS,
2425 : true, true);
2426 :
2427 1007052 : AtEOXact_Aio(true);
2428 :
2429 : /* Check we've released all buffer pins */
2430 1007052 : AtEOXact_Buffers(true);
2431 :
2432 : /* Clean up the relation cache */
2433 1007052 : AtEOXact_RelationCache(true);
2434 :
2435 : /* Clean up the type cache */
2436 1007052 : AtEOXact_TypeCache();
2437 :
2438 : /*
2439 : * Make catalog changes visible to all backends. This has to happen after
2440 : * relcache references are dropped (see comments for
2441 : * AtEOXact_RelationCache), but before locks are released (if anyone is
2442 : * waiting for lock on a relation we've modified, we want them to know
2443 : * about the catalog change before they start using the relation).
2444 : */
2445 1007052 : AtEOXact_Inval(true);
2446 :
2447 1007052 : AtEOXact_MultiXact();
2448 :
2449 1007052 : ResourceOwnerRelease(TopTransactionResourceOwner,
2450 : RESOURCE_RELEASE_LOCKS,
2451 : true, true);
2452 1007052 : ResourceOwnerRelease(TopTransactionResourceOwner,
2453 : RESOURCE_RELEASE_AFTER_LOCKS,
2454 : true, true);
2455 :
2456 : /*
2457 : * Likewise, dropping of files deleted during the transaction is best done
2458 : * after releasing relcache and buffer pins. (This is not strictly
2459 : * necessary during commit, since such pins should have been released
2460 : * already, but this ordering is definitely critical during abort.) Since
2461 : * this may take many seconds, also delay until after releasing locks.
2462 : * Other backends will observe the attendant catalog changes and not
2463 : * attempt to access affected files.
2464 : */
2465 1007052 : smgrDoPendingDeletes(true);
2466 :
2467 : /*
2468 : * Send out notification signals to other backends (and do other
2469 : * post-commit NOTIFY cleanup). This must not happen until after our
2470 : * transaction is fully done from the viewpoint of other backends.
2471 : */
2472 1007052 : AtCommit_Notify();
2473 :
2474 : /*
2475 : * Everything after this should be purely internal-to-this-backend
2476 : * cleanup.
2477 : */
2478 1007052 : AtEOXact_GUC(true, 1);
2479 1007052 : AtEOXact_SPI(true);
2480 1007052 : AtEOXact_Enum();
2481 1007052 : AtEOXact_on_commit_actions(true);
2482 1007052 : AtEOXact_Namespace(true, is_parallel_worker);
2483 1007052 : AtEOXact_SMgr();
2484 1007052 : AtEOXact_Files(true);
2485 1007052 : AtEOXact_ComboCid();
2486 1007052 : AtEOXact_HashTables(true);
2487 1007052 : AtEOXact_PgStat(true, is_parallel_worker);
2488 1007052 : AtEOXact_Snapshot(true, false);
2489 1007052 : AtEOXact_ApplyLauncher(true);
2490 1007052 : AtEOXact_LogicalRepWorkers(true);
2491 1007052 : pgstat_report_xact_timestamp(0);
2492 :
2493 1007052 : ResourceOwnerDelete(TopTransactionResourceOwner);
2494 1007052 : s->curTransactionOwner = NULL;
2495 1007052 : CurTransactionResourceOwner = NULL;
2496 1007052 : TopTransactionResourceOwner = NULL;
2497 :
2498 1007052 : AtCommit_Memory();
2499 :
2500 1007052 : s->fullTransactionId = InvalidFullTransactionId;
2501 1007052 : s->subTransactionId = InvalidSubTransactionId;
2502 1007052 : s->nestingLevel = 0;
2503 1007052 : s->gucNestLevel = 0;
2504 1007052 : s->childXids = NULL;
2505 1007052 : s->nChildXids = 0;
2506 1007052 : s->maxChildXids = 0;
2507 :
2508 1007052 : XactTopFullTransactionId = InvalidFullTransactionId;
2509 1007052 : nParallelCurrentXids = 0;
2510 :
2511 : /*
2512 : * done with commit processing, set current transaction state back to
2513 : * default
2514 : */
2515 1007052 : s->state = TRANS_DEFAULT;
2516 :
2517 1007052 : RESUME_INTERRUPTS();
2518 1007052 : }
2519 :
2520 :
2521 : /*
2522 : * PrepareTransaction
2523 : *
2524 : * NB: if you change this routine, better look at CommitTransaction too!
2525 : */
2526 : static void
2527 668 : PrepareTransaction(void)
2528 : {
2529 668 : TransactionState s = CurrentTransactionState;
2530 668 : FullTransactionId fxid = GetCurrentFullTransactionId();
2531 : GlobalTransaction gxact;
2532 : TimestampTz prepared_at;
2533 :
2534 : Assert(!IsInParallelMode());
2535 :
2536 668 : ShowTransactionState("PrepareTransaction");
2537 :
2538 : /*
2539 : * check the current transaction state
2540 : */
2541 668 : if (s->state != TRANS_INPROGRESS)
2542 0 : elog(WARNING, "PrepareTransaction while in %s state",
2543 : TransStateAsString(s->state));
2544 : Assert(s->parent == NULL);
2545 :
2546 : /*
2547 : * Do pre-commit processing that involves calling user-defined code, such
2548 : * as triggers. Since closing cursors could queue trigger actions,
2549 : * triggers could open cursors, etc, we have to keep looping until there's
2550 : * nothing left to do.
2551 : */
2552 : for (;;)
2553 : {
2554 : /*
2555 : * Fire all currently pending deferred triggers.
2556 : */
2557 674 : AfterTriggerFireDeferred();
2558 :
2559 : /*
2560 : * Close open portals (converting holdable ones into static portals).
2561 : * If there weren't any, we are done ... otherwise loop back to check
2562 : * if they queued deferred triggers. Lather, rinse, repeat.
2563 : */
2564 674 : if (!PreCommit_Portals(true))
2565 668 : break;
2566 : }
2567 :
2568 668 : CallXactCallbacks(XACT_EVENT_PRE_PREPARE);
2569 :
2570 : /*
2571 : * The remaining actions cannot call any user-defined code, so it's safe
2572 : * to start shutting down within-transaction services. But note that most
2573 : * of this stuff could still throw an error, which would switch us into
2574 : * the transaction-abort path.
2575 : */
2576 :
2577 : /* Shut down the deferred-trigger manager */
2578 666 : AfterTriggerEndXact(true);
2579 :
2580 : /*
2581 : * Let ON COMMIT management do its thing (must happen after closing
2582 : * cursors, to avoid dangling-reference problems)
2583 : */
2584 666 : PreCommit_on_commit_actions();
2585 :
2586 : /*
2587 : * Synchronize files that are created and not WAL-logged during this
2588 : * transaction. This must happen before EndPrepare(), so that we don't see
2589 : * committed-but-broken files after a crash and COMMIT PREPARED.
2590 : */
2591 666 : smgrDoPendingSyncs(true, false);
2592 :
2593 : /* close large objects before lower-level cleanup */
2594 666 : AtEOXact_LargeObject(true);
2595 :
2596 : /* NOTIFY requires no work at this point */
2597 :
2598 : /*
2599 : * Mark serializable transaction as complete for predicate locking
2600 : * purposes. This should be done as late as we can put it and still allow
2601 : * errors to be raised for failure patterns found at commit.
2602 : */
2603 666 : PreCommit_CheckForSerializationFailure();
2604 :
2605 : /*
2606 : * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2607 : * this transaction. Having the prepared xact hold locks on another
2608 : * backend's temp table seems a bad idea --- for instance it would prevent
2609 : * the backend from exiting. There are other problems too, such as how to
2610 : * clean up the source backend's local buffers and ON COMMIT state if the
2611 : * prepared xact includes a DROP of a temp table.
2612 : *
2613 : * Other objects types, like functions, operators or extensions, share the
2614 : * same restriction as they should not be created, locked or dropped as
2615 : * this can mess up with this session or even a follow-up session trying
2616 : * to use the same temporary namespace.
2617 : *
2618 : * We must check this after executing any ON COMMIT actions, because they
2619 : * might still access a temp relation.
2620 : *
2621 : * XXX In principle this could be relaxed to allow some useful special
2622 : * cases, such as a temp table created and dropped all within the
2623 : * transaction. That seems to require much more bookkeeping though.
2624 : */
2625 666 : if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE))
2626 68 : ereport(ERROR,
2627 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2628 : errmsg("cannot PREPARE a transaction that has operated on temporary objects")));
2629 :
2630 : /*
2631 : * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2632 : * supported if we added cleanup logic to twophase.c, but for now it
2633 : * doesn't seem worth the trouble.
2634 : */
2635 598 : if (XactHasExportedSnapshots())
2636 0 : ereport(ERROR,
2637 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2638 : errmsg("cannot PREPARE a transaction that has exported snapshots")));
2639 :
2640 : /* Prevent cancel/die interrupt while cleaning up */
2641 598 : HOLD_INTERRUPTS();
2642 :
2643 : /*
2644 : * set the current transaction state information appropriately during
2645 : * prepare processing
2646 : */
2647 598 : s->state = TRANS_PREPARE;
2648 :
2649 : /* Disable transaction timeout */
2650 598 : if (TransactionTimeout > 0)
2651 0 : disable_timeout(TRANSACTION_TIMEOUT, false);
2652 :
2653 598 : prepared_at = GetCurrentTimestamp();
2654 :
2655 : /*
2656 : * Reserve the GID for this transaction. This could fail if the requested
2657 : * GID is invalid or already in use.
2658 : */
2659 598 : gxact = MarkAsPreparing(fxid, prepareGID, prepared_at,
2660 : GetUserId(), MyDatabaseId);
2661 574 : prepareGID = NULL;
2662 :
2663 : /*
2664 : * Collect data for the 2PC state file. Note that in general, no actual
2665 : * state change should happen in the called modules during this step,
2666 : * since it's still possible to fail before commit, and in that case we
2667 : * want transaction abort to be able to clean up. (In particular, the
2668 : * AtPrepare routines may error out if they find cases they cannot
2669 : * handle.) State cleanup should happen in the PostPrepare routines
2670 : * below. However, some modules can go ahead and clear state here because
2671 : * they wouldn't do anything with it during abort anyway.
2672 : *
2673 : * Note: because the 2PC state file records will be replayed in the same
2674 : * order they are made, the order of these calls has to match the order in
2675 : * which we want things to happen during COMMIT PREPARED or ROLLBACK
2676 : * PREPARED; in particular, pay attention to whether things should happen
2677 : * before or after releasing the transaction's locks.
2678 : */
2679 574 : StartPrepare(gxact);
2680 :
2681 574 : AtPrepare_Notify();
2682 574 : AtPrepare_Locks();
2683 570 : AtPrepare_PredicateLocks();
2684 570 : AtPrepare_PgStat();
2685 570 : AtPrepare_MultiXact();
2686 570 : AtPrepare_RelationMap();
2687 :
2688 : /*
2689 : * Here is where we really truly prepare.
2690 : *
2691 : * We have to record transaction prepares even if we didn't make any
2692 : * updates, because the transaction manager might get confused if we lose
2693 : * a global transaction.
2694 : */
2695 570 : EndPrepare(gxact);
2696 :
2697 : /*
2698 : * Now we clean up backend-internal state and release internal resources.
2699 : */
2700 :
2701 : /* Reset XactLastRecEnd until the next transaction writes something */
2702 570 : XactLastRecEnd = 0;
2703 :
2704 : /*
2705 : * Transfer our locks to a dummy PGPROC. This has to be done before
2706 : * ProcArrayClearTransaction(). Otherwise, a GetLockConflicts() would
2707 : * conclude "xact already committed or aborted" for our locks.
2708 : */
2709 570 : PostPrepare_Locks(fxid);
2710 :
2711 : /*
2712 : * Let others know about no transaction in progress by me. This has to be
2713 : * done *after* the prepared transaction has been marked valid, else
2714 : * someone may think it is unlocked and recyclable.
2715 : */
2716 570 : ProcArrayClearTransaction(MyProc);
2717 :
2718 : /*
2719 : * In normal commit-processing, this is all non-critical post-transaction
2720 : * cleanup. When the transaction is prepared, however, it's important
2721 : * that the locks and other per-backend resources are transferred to the
2722 : * prepared transaction's PGPROC entry. Note that if an error is raised
2723 : * here, it's too late to abort the transaction. XXX: This probably should
2724 : * be in a critical section, to force a PANIC if any of this fails, but
2725 : * that cure could be worse than the disease.
2726 : */
2727 :
2728 570 : CallXactCallbacks(XACT_EVENT_PREPARE);
2729 :
2730 570 : ResourceOwnerRelease(TopTransactionResourceOwner,
2731 : RESOURCE_RELEASE_BEFORE_LOCKS,
2732 : true, true);
2733 :
2734 570 : AtEOXact_Aio(true);
2735 :
2736 : /* Check we've released all buffer pins */
2737 570 : AtEOXact_Buffers(true);
2738 :
2739 : /* Clean up the relation cache */
2740 570 : AtEOXact_RelationCache(true);
2741 :
2742 : /* Clean up the type cache */
2743 570 : AtEOXact_TypeCache();
2744 :
2745 : /* notify doesn't need a postprepare call */
2746 :
2747 570 : PostPrepare_PgStat();
2748 :
2749 570 : PostPrepare_Inval();
2750 :
2751 570 : PostPrepare_smgr();
2752 :
2753 570 : PostPrepare_MultiXact(fxid);
2754 :
2755 570 : PostPrepare_PredicateLocks(fxid);
2756 :
2757 570 : ResourceOwnerRelease(TopTransactionResourceOwner,
2758 : RESOURCE_RELEASE_LOCKS,
2759 : true, true);
2760 570 : ResourceOwnerRelease(TopTransactionResourceOwner,
2761 : RESOURCE_RELEASE_AFTER_LOCKS,
2762 : true, true);
2763 :
2764 : /*
2765 : * Allow another backend to finish the transaction. After
2766 : * PostPrepare_Twophase(), the transaction is completely detached from our
2767 : * backend. The rest is just non-critical cleanup of backend-local state.
2768 : */
2769 570 : PostPrepare_Twophase();
2770 :
2771 : /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2772 570 : AtEOXact_GUC(true, 1);
2773 570 : AtEOXact_SPI(true);
2774 570 : AtEOXact_Enum();
2775 570 : AtEOXact_on_commit_actions(true);
2776 570 : AtEOXact_Namespace(true, false);
2777 570 : AtEOXact_SMgr();
2778 570 : AtEOXact_Files(true);
2779 570 : AtEOXact_ComboCid();
2780 570 : AtEOXact_HashTables(true);
2781 : /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2782 570 : AtEOXact_Snapshot(true, true);
2783 : /* we treat PREPARE as ROLLBACK so far as waking workers goes */
2784 570 : AtEOXact_ApplyLauncher(false);
2785 570 : AtEOXact_LogicalRepWorkers(false);
2786 570 : pgstat_report_xact_timestamp(0);
2787 :
2788 570 : CurrentResourceOwner = NULL;
2789 570 : ResourceOwnerDelete(TopTransactionResourceOwner);
2790 570 : s->curTransactionOwner = NULL;
2791 570 : CurTransactionResourceOwner = NULL;
2792 570 : TopTransactionResourceOwner = NULL;
2793 :
2794 570 : AtCommit_Memory();
2795 :
2796 570 : s->fullTransactionId = InvalidFullTransactionId;
2797 570 : s->subTransactionId = InvalidSubTransactionId;
2798 570 : s->nestingLevel = 0;
2799 570 : s->gucNestLevel = 0;
2800 570 : s->childXids = NULL;
2801 570 : s->nChildXids = 0;
2802 570 : s->maxChildXids = 0;
2803 :
2804 570 : XactTopFullTransactionId = InvalidFullTransactionId;
2805 570 : nParallelCurrentXids = 0;
2806 :
2807 : /*
2808 : * done with 1st phase commit processing, set current transaction state
2809 : * back to default
2810 : */
2811 570 : s->state = TRANS_DEFAULT;
2812 :
2813 570 : RESUME_INTERRUPTS();
2814 570 : }
2815 :
2816 :
2817 : /*
2818 : * AbortTransaction
2819 : */
2820 : static void
2821 49326 : AbortTransaction(void)
2822 : {
2823 49326 : TransactionState s = CurrentTransactionState;
2824 : TransactionId latestXid;
2825 : bool is_parallel_worker;
2826 :
2827 : /* Prevent cancel/die interrupt while cleaning up */
2828 49326 : HOLD_INTERRUPTS();
2829 :
2830 : /* Disable transaction timeout */
2831 49326 : if (TransactionTimeout > 0)
2832 2 : disable_timeout(TRANSACTION_TIMEOUT, false);
2833 :
2834 : /* Make sure we have a valid memory context and resource owner */
2835 49326 : AtAbort_Memory();
2836 49326 : AtAbort_ResourceOwner();
2837 :
2838 : /*
2839 : * Release any LW locks we might be holding as quickly as possible.
2840 : * (Regular locks, however, must be held till we finish aborting.)
2841 : * Releasing LW locks is critical since we might try to grab them again
2842 : * while cleaning up!
2843 : */
2844 49326 : LWLockReleaseAll();
2845 :
2846 : /* Clear wait information and command progress indicator */
2847 49326 : pgstat_report_wait_end();
2848 49326 : pgstat_progress_end_command();
2849 :
2850 49326 : pgaio_error_cleanup();
2851 :
2852 : /* Clean up buffer content locks, too */
2853 49326 : UnlockBuffers();
2854 :
2855 : /* Reset WAL record construction state */
2856 49326 : XLogResetInsertion();
2857 :
2858 : /* Cancel condition variable sleep */
2859 49326 : ConditionVariableCancelSleep();
2860 :
2861 : /*
2862 : * Also clean up any open wait for lock, since the lock manager will choke
2863 : * if we try to wait for another lock before doing this.
2864 : */
2865 49326 : LockErrorCleanup();
2866 :
2867 : /*
2868 : * If any timeout events are still active, make sure the timeout interrupt
2869 : * is scheduled. This covers possible loss of a timeout interrupt due to
2870 : * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2871 : * We delay this till after LockErrorCleanup so that we don't uselessly
2872 : * reschedule lock or deadlock check timeouts.
2873 : */
2874 49326 : reschedule_timeouts();
2875 :
2876 : /*
2877 : * Re-enable signals, in case we got here by longjmp'ing out of a signal
2878 : * handler. We do this fairly early in the sequence so that the timeout
2879 : * infrastructure will be functional if needed while aborting.
2880 : */
2881 49326 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
2882 :
2883 : /*
2884 : * check the current transaction state
2885 : */
2886 49326 : is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2887 49326 : if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2888 0 : elog(WARNING, "AbortTransaction while in %s state",
2889 : TransStateAsString(s->state));
2890 : Assert(s->parent == NULL);
2891 :
2892 : /*
2893 : * set the current transaction state information appropriately during the
2894 : * abort processing
2895 : */
2896 49326 : s->state = TRANS_ABORT;
2897 :
2898 : /*
2899 : * Reset user ID which might have been changed transiently. We need this
2900 : * to clean up in case control escaped out of a SECURITY DEFINER function
2901 : * or other local change of CurrentUserId; therefore, the prior value of
2902 : * SecurityRestrictionContext also needs to be restored.
2903 : *
2904 : * (Note: it is not necessary to restore session authorization or role
2905 : * settings here because those can only be changed via GUC, and GUC will
2906 : * take care of rolling them back if need be.)
2907 : */
2908 49326 : SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
2909 :
2910 : /* Forget about any active REINDEX. */
2911 49326 : ResetReindexState(s->nestingLevel);
2912 :
2913 : /* Reset logical streaming state. */
2914 49326 : ResetLogicalStreamingState();
2915 :
2916 : /* Reset snapshot export state. */
2917 49326 : SnapBuildResetExportedSnapshotState();
2918 :
2919 : /*
2920 : * If this xact has started any unfinished parallel operation, clean up
2921 : * its workers and exit parallel mode. Don't warn about leaked resources.
2922 : */
2923 49326 : AtEOXact_Parallel(false);
2924 49326 : s->parallelModeLevel = 0;
2925 49326 : s->parallelChildXact = false; /* should be false already */
2926 :
2927 : /*
2928 : * do abort processing
2929 : */
2930 49326 : AfterTriggerEndXact(false); /* 'false' means it's abort */
2931 49326 : AtAbort_Portals();
2932 49326 : smgrDoPendingSyncs(false, is_parallel_worker);
2933 49326 : AtEOXact_LargeObject(false);
2934 49326 : AtAbort_Notify();
2935 49326 : AtEOXact_RelationMap(false, is_parallel_worker);
2936 49326 : AtAbort_Twophase();
2937 :
2938 : /*
2939 : * Advertise the fact that we aborted in pg_xact (assuming that we got as
2940 : * far as assigning an XID to advertise). But if we're inside a parallel
2941 : * worker, skip this; the user backend must be the one to write the abort
2942 : * record.
2943 : */
2944 49326 : if (!is_parallel_worker)
2945 49314 : latestXid = RecordTransactionAbort(false);
2946 : else
2947 : {
2948 12 : latestXid = InvalidTransactionId;
2949 :
2950 : /*
2951 : * Since the parallel leader won't get our value of XactLastRecEnd in
2952 : * this case, we nudge WAL-writer ourselves in this case. See related
2953 : * comments in RecordTransactionAbort for why this matters.
2954 : */
2955 12 : XLogSetAsyncXactLSN(XactLastRecEnd);
2956 : }
2957 :
2958 : TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->vxid.lxid);
2959 :
2960 : /*
2961 : * Let others know about no transaction in progress by me. Note that this
2962 : * must be done _before_ releasing locks we hold and _after_
2963 : * RecordTransactionAbort.
2964 : */
2965 49326 : ProcArrayEndTransaction(MyProc, latestXid);
2966 :
2967 : /*
2968 : * Post-abort cleanup. See notes in CommitTransaction() concerning
2969 : * ordering. We can skip all of it if the transaction failed before
2970 : * creating a resource owner.
2971 : */
2972 49326 : if (TopTransactionResourceOwner != NULL)
2973 : {
2974 49326 : if (is_parallel_worker)
2975 12 : CallXactCallbacks(XACT_EVENT_PARALLEL_ABORT);
2976 : else
2977 49314 : CallXactCallbacks(XACT_EVENT_ABORT);
2978 :
2979 49326 : ResourceOwnerRelease(TopTransactionResourceOwner,
2980 : RESOURCE_RELEASE_BEFORE_LOCKS,
2981 : false, true);
2982 49326 : AtEOXact_Aio(false);
2983 49326 : AtEOXact_Buffers(false);
2984 49326 : AtEOXact_RelationCache(false);
2985 49326 : AtEOXact_TypeCache();
2986 49326 : AtEOXact_Inval(false);
2987 49326 : AtEOXact_MultiXact();
2988 49326 : ResourceOwnerRelease(TopTransactionResourceOwner,
2989 : RESOURCE_RELEASE_LOCKS,
2990 : false, true);
2991 49326 : ResourceOwnerRelease(TopTransactionResourceOwner,
2992 : RESOURCE_RELEASE_AFTER_LOCKS,
2993 : false, true);
2994 49326 : smgrDoPendingDeletes(false);
2995 :
2996 49326 : AtEOXact_GUC(false, 1);
2997 49326 : AtEOXact_SPI(false);
2998 49326 : AtEOXact_Enum();
2999 49326 : AtEOXact_on_commit_actions(false);
3000 49326 : AtEOXact_Namespace(false, is_parallel_worker);
3001 49326 : AtEOXact_SMgr();
3002 49326 : AtEOXact_Files(false);
3003 49326 : AtEOXact_ComboCid();
3004 49326 : AtEOXact_HashTables(false);
3005 49326 : AtEOXact_PgStat(false, is_parallel_worker);
3006 49326 : AtEOXact_ApplyLauncher(false);
3007 49326 : AtEOXact_LogicalRepWorkers(false);
3008 49326 : pgstat_report_xact_timestamp(0);
3009 : }
3010 :
3011 : /*
3012 : * State remains TRANS_ABORT until CleanupTransaction().
3013 : */
3014 49326 : RESUME_INTERRUPTS();
3015 49326 : }
3016 :
3017 : /*
3018 : * CleanupTransaction
3019 : */
3020 : static void
3021 49326 : CleanupTransaction(void)
3022 : {
3023 49326 : TransactionState s = CurrentTransactionState;
3024 :
3025 : /*
3026 : * State should still be TRANS_ABORT from AbortTransaction().
3027 : */
3028 49326 : if (s->state != TRANS_ABORT)
3029 0 : elog(FATAL, "CleanupTransaction: unexpected state %s",
3030 : TransStateAsString(s->state));
3031 :
3032 : /*
3033 : * do abort cleanup processing
3034 : */
3035 49326 : AtCleanup_Portals(); /* now safe to release portal memory */
3036 49326 : AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
3037 :
3038 49326 : CurrentResourceOwner = NULL; /* and resource owner */
3039 49326 : if (TopTransactionResourceOwner)
3040 49326 : ResourceOwnerDelete(TopTransactionResourceOwner);
3041 49326 : s->curTransactionOwner = NULL;
3042 49326 : CurTransactionResourceOwner = NULL;
3043 49326 : TopTransactionResourceOwner = NULL;
3044 :
3045 49326 : AtCleanup_Memory(); /* and transaction memory */
3046 :
3047 49326 : s->fullTransactionId = InvalidFullTransactionId;
3048 49326 : s->subTransactionId = InvalidSubTransactionId;
3049 49326 : s->nestingLevel = 0;
3050 49326 : s->gucNestLevel = 0;
3051 49326 : s->childXids = NULL;
3052 49326 : s->nChildXids = 0;
3053 49326 : s->maxChildXids = 0;
3054 49326 : s->parallelModeLevel = 0;
3055 49326 : s->parallelChildXact = false;
3056 :
3057 49326 : XactTopFullTransactionId = InvalidFullTransactionId;
3058 49326 : nParallelCurrentXids = 0;
3059 :
3060 : /*
3061 : * done with abort processing, set current transaction state back to
3062 : * default
3063 : */
3064 49326 : s->state = TRANS_DEFAULT;
3065 49326 : }
3066 :
3067 : /*
3068 : * StartTransactionCommand
3069 : */
3070 : void
3071 1241300 : StartTransactionCommand(void)
3072 : {
3073 1241300 : TransactionState s = CurrentTransactionState;
3074 :
3075 1241300 : switch (s->blockState)
3076 : {
3077 : /*
3078 : * if we aren't in a transaction block, we just do our usual start
3079 : * transaction.
3080 : */
3081 1054142 : case TBLOCK_DEFAULT:
3082 1054142 : StartTransaction();
3083 1054142 : s->blockState = TBLOCK_STARTED;
3084 1054142 : break;
3085 :
3086 : /*
3087 : * We are somewhere in a transaction block or subtransaction and
3088 : * about to start a new command. For now we do nothing, but
3089 : * someday we may do command-local resource initialization. (Note
3090 : * that any needed CommandCounterIncrement was done by the
3091 : * previous CommitTransactionCommand.)
3092 : */
3093 185384 : case TBLOCK_INPROGRESS:
3094 : case TBLOCK_IMPLICIT_INPROGRESS:
3095 : case TBLOCK_SUBINPROGRESS:
3096 185384 : break;
3097 :
3098 : /*
3099 : * Here we are in a failed transaction block (one of the commands
3100 : * caused an abort) so we do nothing but remain in the abort
3101 : * state. Eventually we will get a ROLLBACK command which will
3102 : * get us out of this state. (It is up to other code to ensure
3103 : * that no commands other than ROLLBACK will be processed in these
3104 : * states.)
3105 : */
3106 1774 : case TBLOCK_ABORT:
3107 : case TBLOCK_SUBABORT:
3108 1774 : break;
3109 :
3110 : /* These cases are invalid. */
3111 0 : case TBLOCK_STARTED:
3112 : case TBLOCK_BEGIN:
3113 : case TBLOCK_PARALLEL_INPROGRESS:
3114 : case TBLOCK_SUBBEGIN:
3115 : case TBLOCK_END:
3116 : case TBLOCK_SUBRELEASE:
3117 : case TBLOCK_SUBCOMMIT:
3118 : case TBLOCK_ABORT_END:
3119 : case TBLOCK_SUBABORT_END:
3120 : case TBLOCK_ABORT_PENDING:
3121 : case TBLOCK_SUBABORT_PENDING:
3122 : case TBLOCK_SUBRESTART:
3123 : case TBLOCK_SUBABORT_RESTART:
3124 : case TBLOCK_PREPARE:
3125 0 : elog(ERROR, "StartTransactionCommand: unexpected state %s",
3126 : BlockStateAsString(s->blockState));
3127 : break;
3128 : }
3129 :
3130 : /*
3131 : * We must switch to CurTransactionContext before returning. This is
3132 : * already done if we called StartTransaction, otherwise not.
3133 : */
3134 : Assert(CurTransactionContext != NULL);
3135 1241300 : MemoryContextSwitchTo(CurTransactionContext);
3136 1241300 : }
3137 :
3138 :
3139 : /*
3140 : * Simple system for saving and restoring transaction characteristics
3141 : * (isolation level, read only, deferrable). We need this for transaction
3142 : * chaining, so that we can set the characteristics of the new transaction to
3143 : * be the same as the previous one. (We need something like this because the
3144 : * GUC system resets the characteristics at transaction end, so for example
3145 : * just skipping the reset in StartTransaction() won't work.)
3146 : */
3147 : void
3148 1195616 : SaveTransactionCharacteristics(SavedTransactionCharacteristics *s)
3149 : {
3150 1195616 : s->save_XactIsoLevel = XactIsoLevel;
3151 1195616 : s->save_XactReadOnly = XactReadOnly;
3152 1195616 : s->save_XactDeferrable = XactDeferrable;
3153 1195616 : }
3154 :
3155 : void
3156 68 : RestoreTransactionCharacteristics(const SavedTransactionCharacteristics *s)
3157 : {
3158 68 : XactIsoLevel = s->save_XactIsoLevel;
3159 68 : XactReadOnly = s->save_XactReadOnly;
3160 68 : XactDeferrable = s->save_XactDeferrable;
3161 68 : }
3162 :
3163 : /*
3164 : * CommitTransactionCommand -- a wrapper function handling the
3165 : * loop over subtransactions to avoid a potentially dangerous recursion
3166 : * in CommitTransactionCommandInternal().
3167 : */
3168 : void
3169 1195174 : CommitTransactionCommand(void)
3170 : {
3171 : /*
3172 : * Repeatedly call CommitTransactionCommandInternal() until all the work
3173 : * is done.
3174 : */
3175 1195608 : while (!CommitTransactionCommandInternal())
3176 : {
3177 : }
3178 1194606 : }
3179 :
3180 : /*
3181 : * CommitTransactionCommandInternal - a function doing an iteration of work
3182 : * regarding handling the commit transaction command. In the case of
3183 : * subtransactions more than one iterations could be required. Returns
3184 : * true when no more iterations required, false otherwise.
3185 : */
3186 : static bool
3187 1195608 : CommitTransactionCommandInternal(void)
3188 : {
3189 1195608 : TransactionState s = CurrentTransactionState;
3190 : SavedTransactionCharacteristics savetc;
3191 :
3192 : /* Must save in case we need to restore below */
3193 1195608 : SaveTransactionCharacteristics(&savetc);
3194 :
3195 1195608 : switch (s->blockState)
3196 : {
3197 : /*
3198 : * These shouldn't happen. TBLOCK_DEFAULT means the previous
3199 : * StartTransactionCommand didn't set the STARTED state
3200 : * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
3201 : * by EndParallelWorkerTransaction(), not this function.
3202 : */
3203 0 : case TBLOCK_DEFAULT:
3204 : case TBLOCK_PARALLEL_INPROGRESS:
3205 0 : elog(FATAL, "CommitTransactionCommand: unexpected state %s",
3206 : BlockStateAsString(s->blockState));
3207 : break;
3208 :
3209 : /*
3210 : * If we aren't in a transaction block, just do our usual
3211 : * transaction commit, and return to the idle state.
3212 : */
3213 991956 : case TBLOCK_STARTED:
3214 991956 : CommitTransaction();
3215 991936 : s->blockState = TBLOCK_DEFAULT;
3216 991936 : break;
3217 :
3218 : /*
3219 : * We are completing a "BEGIN TRANSACTION" command, so we change
3220 : * to the "transaction block in progress" state and return. (We
3221 : * assume the BEGIN did nothing to the database, so we need no
3222 : * CommandCounterIncrement.)
3223 : */
3224 17650 : case TBLOCK_BEGIN:
3225 17650 : s->blockState = TBLOCK_INPROGRESS;
3226 17650 : break;
3227 :
3228 : /*
3229 : * This is the case when we have finished executing a command
3230 : * someplace within a transaction block. We increment the command
3231 : * counter and return.
3232 : */
3233 148126 : case TBLOCK_INPROGRESS:
3234 : case TBLOCK_IMPLICIT_INPROGRESS:
3235 : case TBLOCK_SUBINPROGRESS:
3236 148126 : CommandCounterIncrement();
3237 148126 : break;
3238 :
3239 : /*
3240 : * We are completing a "COMMIT" command. Do it and return to the
3241 : * idle state.
3242 : */
3243 12098 : case TBLOCK_END:
3244 12098 : CommitTransaction();
3245 11674 : s->blockState = TBLOCK_DEFAULT;
3246 11674 : if (s->chain)
3247 : {
3248 12 : StartTransaction();
3249 12 : s->blockState = TBLOCK_INPROGRESS;
3250 12 : s->chain = false;
3251 12 : RestoreTransactionCharacteristics(&savetc);
3252 : }
3253 11674 : break;
3254 :
3255 : /*
3256 : * Here we are in the middle of a transaction block but one of the
3257 : * commands caused an abort so we do nothing but remain in the
3258 : * abort state. Eventually we will get a ROLLBACK command.
3259 : */
3260 18 : case TBLOCK_ABORT:
3261 : case TBLOCK_SUBABORT:
3262 18 : break;
3263 :
3264 : /*
3265 : * Here we were in an aborted transaction block and we just got
3266 : * the ROLLBACK command from the user, so clean up the
3267 : * already-aborted transaction and return to the idle state.
3268 : */
3269 1376 : case TBLOCK_ABORT_END:
3270 1376 : CleanupTransaction();
3271 1376 : s->blockState = TBLOCK_DEFAULT;
3272 1376 : if (s->chain)
3273 : {
3274 12 : StartTransaction();
3275 12 : s->blockState = TBLOCK_INPROGRESS;
3276 12 : s->chain = false;
3277 12 : RestoreTransactionCharacteristics(&savetc);
3278 : }
3279 1376 : break;
3280 :
3281 : /*
3282 : * Here we were in a perfectly good transaction block but the user
3283 : * told us to ROLLBACK anyway. We have to abort the transaction
3284 : * and then clean up.
3285 : */
3286 2220 : case TBLOCK_ABORT_PENDING:
3287 2220 : AbortTransaction();
3288 2220 : CleanupTransaction();
3289 2220 : s->blockState = TBLOCK_DEFAULT;
3290 2220 : if (s->chain)
3291 : {
3292 18 : StartTransaction();
3293 18 : s->blockState = TBLOCK_INPROGRESS;
3294 18 : s->chain = false;
3295 18 : RestoreTransactionCharacteristics(&savetc);
3296 : }
3297 2220 : break;
3298 :
3299 : /*
3300 : * We are completing a "PREPARE TRANSACTION" command. Do it and
3301 : * return to the idle state.
3302 : */
3303 460 : case TBLOCK_PREPARE:
3304 460 : PrepareTransaction();
3305 364 : s->blockState = TBLOCK_DEFAULT;
3306 364 : break;
3307 :
3308 : /*
3309 : * The user issued a SAVEPOINT inside a transaction block. Start a
3310 : * subtransaction. (DefineSavepoint already did PushTransaction,
3311 : * so as to have someplace to put the SUBBEGIN state.)
3312 : */
3313 19330 : case TBLOCK_SUBBEGIN:
3314 19330 : StartSubTransaction();
3315 19330 : s->blockState = TBLOCK_SUBINPROGRESS;
3316 19330 : break;
3317 :
3318 : /*
3319 : * The user issued a RELEASE command, so we end the current
3320 : * subtransaction and return to the parent transaction. The parent
3321 : * might be ended too, so repeat till we find an INPROGRESS
3322 : * transaction or subtransaction.
3323 : */
3324 448 : case TBLOCK_SUBRELEASE:
3325 : do
3326 : {
3327 448 : CommitSubTransaction();
3328 448 : s = CurrentTransactionState; /* changed by pop */
3329 448 : } while (s->blockState == TBLOCK_SUBRELEASE);
3330 :
3331 : Assert(s->blockState == TBLOCK_INPROGRESS ||
3332 : s->blockState == TBLOCK_SUBINPROGRESS);
3333 276 : break;
3334 :
3335 : /*
3336 : * The user issued a COMMIT, so we end the current subtransaction
3337 : * hierarchy and perform final commit. We do this by rolling up
3338 : * any subtransactions into their parent, which leads to O(N^2)
3339 : * operations with respect to resource owners - this isn't that
3340 : * bad until we approach a thousands of savepoints but is
3341 : * necessary for correctness should after triggers create new
3342 : * resource owners.
3343 : */
3344 1076 : case TBLOCK_SUBCOMMIT:
3345 : do
3346 : {
3347 1076 : CommitSubTransaction();
3348 1076 : s = CurrentTransactionState; /* changed by pop */
3349 1076 : } while (s->blockState == TBLOCK_SUBCOMMIT);
3350 : /* If we had a COMMIT command, finish off the main xact too */
3351 942 : if (s->blockState == TBLOCK_END)
3352 : {
3353 : Assert(s->parent == NULL);
3354 734 : CommitTransaction();
3355 708 : s->blockState = TBLOCK_DEFAULT;
3356 708 : if (s->chain)
3357 : {
3358 18 : StartTransaction();
3359 18 : s->blockState = TBLOCK_INPROGRESS;
3360 18 : s->chain = false;
3361 18 : RestoreTransactionCharacteristics(&savetc);
3362 : }
3363 : }
3364 208 : else if (s->blockState == TBLOCK_PREPARE)
3365 : {
3366 : Assert(s->parent == NULL);
3367 208 : PrepareTransaction();
3368 206 : s->blockState = TBLOCK_DEFAULT;
3369 : }
3370 : else
3371 0 : elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3372 : BlockStateAsString(s->blockState));
3373 914 : break;
3374 :
3375 : /*
3376 : * The current already-failed subtransaction is ending due to a
3377 : * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3378 : * examine the parent (which could be in any of several states).
3379 : * As we need to examine the parent, return false to request the
3380 : * caller to do the next iteration.
3381 : */
3382 80 : case TBLOCK_SUBABORT_END:
3383 80 : CleanupSubTransaction();
3384 80 : return false;
3385 :
3386 : /*
3387 : * As above, but it's not dead yet, so abort first.
3388 : */
3389 354 : case TBLOCK_SUBABORT_PENDING:
3390 354 : AbortSubTransaction();
3391 354 : CleanupSubTransaction();
3392 354 : return false;
3393 :
3394 : /*
3395 : * The current subtransaction is the target of a ROLLBACK TO
3396 : * command. Abort and pop it, then start a new subtransaction
3397 : * with the same name.
3398 : */
3399 520 : case TBLOCK_SUBRESTART:
3400 : {
3401 : char *name;
3402 : int savepointLevel;
3403 :
3404 : /* save name and keep Cleanup from freeing it */
3405 520 : name = s->name;
3406 520 : s->name = NULL;
3407 520 : savepointLevel = s->savepointLevel;
3408 :
3409 520 : AbortSubTransaction();
3410 520 : CleanupSubTransaction();
3411 :
3412 520 : DefineSavepoint(NULL);
3413 520 : s = CurrentTransactionState; /* changed by push */
3414 520 : s->name = name;
3415 520 : s->savepointLevel = savepointLevel;
3416 :
3417 : /* This is the same as TBLOCK_SUBBEGIN case */
3418 : Assert(s->blockState == TBLOCK_SUBBEGIN);
3419 520 : StartSubTransaction();
3420 520 : s->blockState = TBLOCK_SUBINPROGRESS;
3421 : }
3422 520 : break;
3423 :
3424 : /*
3425 : * Same as above, but the subtransaction had already failed, so we
3426 : * don't need AbortSubTransaction.
3427 : */
3428 202 : case TBLOCK_SUBABORT_RESTART:
3429 : {
3430 : char *name;
3431 : int savepointLevel;
3432 :
3433 : /* save name and keep Cleanup from freeing it */
3434 202 : name = s->name;
3435 202 : s->name = NULL;
3436 202 : savepointLevel = s->savepointLevel;
3437 :
3438 202 : CleanupSubTransaction();
3439 :
3440 202 : DefineSavepoint(NULL);
3441 202 : s = CurrentTransactionState; /* changed by push */
3442 202 : s->name = name;
3443 202 : s->savepointLevel = savepointLevel;
3444 :
3445 : /* This is the same as TBLOCK_SUBBEGIN case */
3446 : Assert(s->blockState == TBLOCK_SUBBEGIN);
3447 202 : StartSubTransaction();
3448 202 : s->blockState = TBLOCK_SUBINPROGRESS;
3449 : }
3450 202 : break;
3451 : }
3452 :
3453 : /* Done, no more iterations required */
3454 1194606 : return true;
3455 : }
3456 :
3457 : /*
3458 : * AbortCurrentTransaction -- a wrapper function handling the
3459 : * loop over subtransactions to avoid potentially dangerous recursion in
3460 : * AbortCurrentTransactionInternal().
3461 : */
3462 : void
3463 48338 : AbortCurrentTransaction(void)
3464 : {
3465 : /*
3466 : * Repeatedly call AbortCurrentTransactionInternal() until all the work is
3467 : * done.
3468 : */
3469 48338 : while (!AbortCurrentTransactionInternal())
3470 : {
3471 : }
3472 48338 : }
3473 :
3474 : /*
3475 : * AbortCurrentTransactionInternal - a function doing an iteration of work
3476 : * regarding handling the current transaction abort. In the case of
3477 : * subtransactions more than one iterations could be required. Returns
3478 : * true when no more iterations required, false otherwise.
3479 : */
3480 : static bool
3481 48338 : AbortCurrentTransactionInternal(void)
3482 : {
3483 48338 : TransactionState s = CurrentTransactionState;
3484 :
3485 48338 : switch (s->blockState)
3486 : {
3487 98 : case TBLOCK_DEFAULT:
3488 98 : if (s->state == TRANS_DEFAULT)
3489 : {
3490 : /* we are idle, so nothing to do */
3491 : }
3492 : else
3493 : {
3494 : /*
3495 : * We can get here after an error during transaction start
3496 : * (state will be TRANS_START). Need to clean up the
3497 : * incompletely started transaction. First, adjust the
3498 : * low-level state to suppress warning message from
3499 : * AbortTransaction.
3500 : */
3501 0 : if (s->state == TRANS_START)
3502 0 : s->state = TRANS_INPROGRESS;
3503 0 : AbortTransaction();
3504 0 : CleanupTransaction();
3505 : }
3506 98 : break;
3507 :
3508 : /*
3509 : * If we aren't in a transaction block, we just do the basic abort
3510 : * & cleanup transaction. For this purpose, we treat an implicit
3511 : * transaction block as if it were a simple statement.
3512 : */
3513 44060 : case TBLOCK_STARTED:
3514 : case TBLOCK_IMPLICIT_INPROGRESS:
3515 44060 : AbortTransaction();
3516 44060 : CleanupTransaction();
3517 44060 : s->blockState = TBLOCK_DEFAULT;
3518 44060 : break;
3519 :
3520 : /*
3521 : * If we are in TBLOCK_BEGIN it means something screwed up right
3522 : * after reading "BEGIN TRANSACTION". We assume that the user
3523 : * will interpret the error as meaning the BEGIN failed to get him
3524 : * into a transaction block, so we should abort and return to idle
3525 : * state.
3526 : */
3527 0 : case TBLOCK_BEGIN:
3528 0 : AbortTransaction();
3529 0 : CleanupTransaction();
3530 0 : s->blockState = TBLOCK_DEFAULT;
3531 0 : break;
3532 :
3533 : /*
3534 : * We are somewhere in a transaction block and we've gotten a
3535 : * failure, so we abort the transaction and set up the persistent
3536 : * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3537 : */
3538 1398 : case TBLOCK_INPROGRESS:
3539 : case TBLOCK_PARALLEL_INPROGRESS:
3540 1398 : AbortTransaction();
3541 1398 : s->blockState = TBLOCK_ABORT;
3542 : /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3543 1398 : break;
3544 :
3545 : /*
3546 : * Here, we failed while trying to COMMIT. Clean up the
3547 : * transaction and return to idle state (we do not want to stay in
3548 : * the transaction).
3549 : */
3550 450 : case TBLOCK_END:
3551 450 : AbortTransaction();
3552 450 : CleanupTransaction();
3553 450 : s->blockState = TBLOCK_DEFAULT;
3554 450 : break;
3555 :
3556 : /*
3557 : * Here, we are already in an aborted transaction state and are
3558 : * waiting for a ROLLBACK, but for some reason we failed again! So
3559 : * we just remain in the abort state.
3560 : */
3561 100 : case TBLOCK_ABORT:
3562 : case TBLOCK_SUBABORT:
3563 100 : break;
3564 :
3565 : /*
3566 : * We are in a failed transaction and we got the ROLLBACK command.
3567 : * We have already aborted, we just need to cleanup and go to idle
3568 : * state.
3569 : */
3570 0 : case TBLOCK_ABORT_END:
3571 0 : CleanupTransaction();
3572 0 : s->blockState = TBLOCK_DEFAULT;
3573 0 : break;
3574 :
3575 : /*
3576 : * We are in a live transaction and we got a ROLLBACK command.
3577 : * Abort, cleanup, go to idle state.
3578 : */
3579 0 : case TBLOCK_ABORT_PENDING:
3580 0 : AbortTransaction();
3581 0 : CleanupTransaction();
3582 0 : s->blockState = TBLOCK_DEFAULT;
3583 0 : break;
3584 :
3585 : /*
3586 : * Here, we failed while trying to PREPARE. Clean up the
3587 : * transaction and return to idle state (we do not want to stay in
3588 : * the transaction).
3589 : */
3590 96 : case TBLOCK_PREPARE:
3591 96 : AbortTransaction();
3592 96 : CleanupTransaction();
3593 96 : s->blockState = TBLOCK_DEFAULT;
3594 96 : break;
3595 :
3596 : /*
3597 : * We got an error inside a subtransaction. Abort just the
3598 : * subtransaction, and go to the persistent SUBABORT state until
3599 : * we get ROLLBACK.
3600 : */
3601 2136 : case TBLOCK_SUBINPROGRESS:
3602 2136 : AbortSubTransaction();
3603 2136 : s->blockState = TBLOCK_SUBABORT;
3604 2136 : break;
3605 :
3606 : /*
3607 : * If we failed while trying to create a subtransaction, clean up
3608 : * the broken subtransaction and abort the parent. The same
3609 : * applies if we get a failure while ending a subtransaction. As
3610 : * we need to abort the parent, return false to request the caller
3611 : * to do the next iteration.
3612 : */
3613 0 : case TBLOCK_SUBBEGIN:
3614 : case TBLOCK_SUBRELEASE:
3615 : case TBLOCK_SUBCOMMIT:
3616 : case TBLOCK_SUBABORT_PENDING:
3617 : case TBLOCK_SUBRESTART:
3618 0 : AbortSubTransaction();
3619 0 : CleanupSubTransaction();
3620 0 : return false;
3621 :
3622 : /*
3623 : * Same as above, except the Abort() was already done.
3624 : */
3625 0 : case TBLOCK_SUBABORT_END:
3626 : case TBLOCK_SUBABORT_RESTART:
3627 0 : CleanupSubTransaction();
3628 0 : return false;
3629 : }
3630 :
3631 : /* Done, no more iterations required */
3632 48338 : return true;
3633 : }
3634 :
3635 : /*
3636 : * PreventInTransactionBlock
3637 : *
3638 : * This routine is to be called by statements that must not run inside
3639 : * a transaction block, typically because they have non-rollback-able
3640 : * side effects or do internal commits.
3641 : *
3642 : * If this routine completes successfully, then the calling statement is
3643 : * guaranteed that if it completes without error, its results will be
3644 : * committed immediately.
3645 : *
3646 : * If we have already started a transaction block, issue an error; also issue
3647 : * an error if we appear to be running inside a user-defined function (which
3648 : * could issue more commands and possibly cause a failure after the statement
3649 : * completes). Subtransactions are verboten too.
3650 : *
3651 : * We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
3652 : * that postgres.c follows through by committing after the statement is done.
3653 : *
3654 : * isTopLevel: passed down from ProcessUtility to determine whether we are
3655 : * inside a function. (We will always fail if this is false, but it's
3656 : * convenient to centralize the check here instead of making callers do it.)
3657 : * stmtType: statement type name, for error messages.
3658 : */
3659 : void
3660 211388 : PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
3661 : {
3662 : /*
3663 : * xact block already started?
3664 : */
3665 211388 : if (IsTransactionBlock())
3666 118 : ereport(ERROR,
3667 : (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3668 : /* translator: %s represents an SQL statement name */
3669 : errmsg("%s cannot run inside a transaction block",
3670 : stmtType)));
3671 :
3672 : /*
3673 : * subtransaction?
3674 : */
3675 211270 : if (IsSubTransaction())
3676 0 : ereport(ERROR,
3677 : (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3678 : /* translator: %s represents an SQL statement name */
3679 : errmsg("%s cannot run inside a subtransaction",
3680 : stmtType)));
3681 :
3682 : /*
3683 : * inside a function call?
3684 : */
3685 211270 : if (!isTopLevel)
3686 6 : ereport(ERROR,
3687 : (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3688 : /* translator: %s represents an SQL statement name */
3689 : errmsg("%s cannot be executed from a function", stmtType)));
3690 :
3691 : /* If we got past IsTransactionBlock test, should be in default state */
3692 211264 : if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3693 209556 : CurrentTransactionState->blockState != TBLOCK_STARTED)
3694 0 : elog(FATAL, "cannot prevent transaction chain");
3695 :
3696 : /* All okay. Set the flag to make sure the right thing happens later. */
3697 211264 : MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
3698 211264 : }
3699 :
3700 : /*
3701 : * WarnNoTransactionBlock
3702 : * RequireTransactionBlock
3703 : *
3704 : * These two functions allow for warnings or errors if a command is executed
3705 : * outside of a transaction block. This is useful for commands that have no
3706 : * effects that persist past transaction end (and so calling them outside a
3707 : * transaction block is presumably an error). DECLARE CURSOR is an example.
3708 : * While top-level transaction control commands (BEGIN/COMMIT/ABORT) and SET
3709 : * that have no effect issue warnings, all other no-effect commands generate
3710 : * errors.
3711 : *
3712 : * If we appear to be running inside a user-defined function, we do not
3713 : * issue anything, since the function could issue more commands that make
3714 : * use of the current statement's results. Likewise subtransactions.
3715 : * Thus these are inverses for PreventInTransactionBlock.
3716 : *
3717 : * isTopLevel: passed down from ProcessUtility to determine whether we are
3718 : * inside a function.
3719 : * stmtType: statement type name, for warning or error messages.
3720 : */
3721 : void
3722 2126 : WarnNoTransactionBlock(bool isTopLevel, const char *stmtType)
3723 : {
3724 2126 : CheckTransactionBlock(isTopLevel, false, stmtType);
3725 2126 : }
3726 :
3727 : void
3728 9880 : RequireTransactionBlock(bool isTopLevel, const char *stmtType)
3729 : {
3730 9880 : CheckTransactionBlock(isTopLevel, true, stmtType);
3731 9844 : }
3732 :
3733 : /*
3734 : * This is the implementation of the above two.
3735 : */
3736 : static void
3737 12006 : CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
3738 : {
3739 : /*
3740 : * xact block already started?
3741 : */
3742 12006 : if (IsTransactionBlock())
3743 11832 : return;
3744 :
3745 : /*
3746 : * subtransaction?
3747 : */
3748 174 : if (IsSubTransaction())
3749 0 : return;
3750 :
3751 : /*
3752 : * inside a function call?
3753 : */
3754 174 : if (!isTopLevel)
3755 116 : return;
3756 :
3757 58 : ereport(throwError ? ERROR : WARNING,
3758 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3759 : /* translator: %s represents an SQL statement name */
3760 : errmsg("%s can only be used in transaction blocks",
3761 : stmtType)));
3762 : }
3763 :
3764 : /*
3765 : * IsInTransactionBlock
3766 : *
3767 : * This routine is for statements that need to behave differently inside
3768 : * a transaction block than when running as single commands. ANALYZE is
3769 : * currently the only example.
3770 : *
3771 : * If this routine returns "false", then the calling statement is allowed
3772 : * to perform internal transaction-commit-and-start cycles; there is not a
3773 : * risk of messing up any transaction already in progress. (Note that this
3774 : * is not the identical guarantee provided by PreventInTransactionBlock,
3775 : * since we will not force a post-statement commit.)
3776 : *
3777 : * isTopLevel: passed down from ProcessUtility to determine whether we are
3778 : * inside a function.
3779 : */
3780 : bool
3781 4978 : IsInTransactionBlock(bool isTopLevel)
3782 : {
3783 : /*
3784 : * Return true on same conditions that would make
3785 : * PreventInTransactionBlock error out
3786 : */
3787 4978 : if (IsTransactionBlock())
3788 138 : return true;
3789 :
3790 4840 : if (IsSubTransaction())
3791 0 : return true;
3792 :
3793 4840 : if (!isTopLevel)
3794 112 : return true;
3795 :
3796 4728 : if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
3797 4728 : CurrentTransactionState->blockState != TBLOCK_STARTED)
3798 0 : return true;
3799 :
3800 4728 : return false;
3801 : }
3802 :
3803 :
3804 : /*
3805 : * Register or deregister callback functions for start- and end-of-xact
3806 : * operations.
3807 : *
3808 : * These functions are intended for use by dynamically loaded modules.
3809 : * For built-in modules we generally just hardwire the appropriate calls
3810 : * (mainly because it's easier to control the order that way, where needed).
3811 : *
3812 : * At transaction end, the callback occurs post-commit or post-abort, so the
3813 : * callback functions can only do noncritical cleanup.
3814 : */
3815 : void
3816 3564 : RegisterXactCallback(XactCallback callback, void *arg)
3817 : {
3818 : XactCallbackItem *item;
3819 :
3820 : item = (XactCallbackItem *)
3821 3564 : MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
3822 3564 : item->callback = callback;
3823 3564 : item->arg = arg;
3824 3564 : item->next = Xact_callbacks;
3825 3564 : Xact_callbacks = item;
3826 3564 : }
3827 :
3828 : void
3829 0 : UnregisterXactCallback(XactCallback callback, void *arg)
3830 : {
3831 : XactCallbackItem *item;
3832 : XactCallbackItem *prev;
3833 :
3834 0 : prev = NULL;
3835 0 : for (item = Xact_callbacks; item; prev = item, item = item->next)
3836 : {
3837 0 : if (item->callback == callback && item->arg == arg)
3838 : {
3839 0 : if (prev)
3840 0 : prev->next = item->next;
3841 : else
3842 0 : Xact_callbacks = item->next;
3843 0 : pfree(item);
3844 0 : break;
3845 : }
3846 : }
3847 0 : }
3848 :
3849 : static void
3850 2064984 : CallXactCallbacks(XactEvent event)
3851 : {
3852 : XactCallbackItem *item;
3853 : XactCallbackItem *next;
3854 :
3855 2381646 : for (item = Xact_callbacks; item; item = next)
3856 : {
3857 : /* allow callbacks to unregister themselves when called */
3858 316664 : next = item->next;
3859 316664 : item->callback(event, item->arg);
3860 : }
3861 2064982 : }
3862 :
3863 :
3864 : /*
3865 : * Register or deregister callback functions for start- and end-of-subxact
3866 : * operations.
3867 : *
3868 : * Pretty much same as above, but for subtransaction events.
3869 : *
3870 : * At subtransaction end, the callback occurs post-subcommit or post-subabort,
3871 : * so the callback functions can only do noncritical cleanup. At
3872 : * subtransaction start, the callback is called when the subtransaction has
3873 : * finished initializing.
3874 : */
3875 : void
3876 3564 : RegisterSubXactCallback(SubXactCallback callback, void *arg)
3877 : {
3878 : SubXactCallbackItem *item;
3879 :
3880 : item = (SubXactCallbackItem *)
3881 3564 : MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
3882 3564 : item->callback = callback;
3883 3564 : item->arg = arg;
3884 3564 : item->next = SubXact_callbacks;
3885 3564 : SubXact_callbacks = item;
3886 3564 : }
3887 :
3888 : void
3889 0 : UnregisterSubXactCallback(SubXactCallback callback, void *arg)
3890 : {
3891 : SubXactCallbackItem *item;
3892 : SubXactCallbackItem *prev;
3893 :
3894 0 : prev = NULL;
3895 0 : for (item = SubXact_callbacks; item; prev = item, item = item->next)
3896 : {
3897 0 : if (item->callback == callback && item->arg == arg)
3898 : {
3899 0 : if (prev)
3900 0 : prev->next = item->next;
3901 : else
3902 0 : SubXact_callbacks = item->next;
3903 0 : pfree(item);
3904 0 : break;
3905 : }
3906 : }
3907 0 : }
3908 :
3909 : static void
3910 50790 : CallSubXactCallbacks(SubXactEvent event,
3911 : SubTransactionId mySubid,
3912 : SubTransactionId parentSubid)
3913 : {
3914 : SubXactCallbackItem *item;
3915 : SubXactCallbackItem *next;
3916 :
3917 90504 : for (item = SubXact_callbacks; item; item = next)
3918 : {
3919 : /* allow callbacks to unregister themselves when called */
3920 39714 : next = item->next;
3921 39714 : item->callback(event, mySubid, parentSubid, item->arg);
3922 : }
3923 50790 : }
3924 :
3925 :
3926 : /* ----------------------------------------------------------------
3927 : * transaction block support
3928 : * ----------------------------------------------------------------
3929 : */
3930 :
3931 : /*
3932 : * BeginTransactionBlock
3933 : * This executes a BEGIN command.
3934 : */
3935 : void
3936 17650 : BeginTransactionBlock(void)
3937 : {
3938 17650 : TransactionState s = CurrentTransactionState;
3939 :
3940 17650 : switch (s->blockState)
3941 : {
3942 : /*
3943 : * We are not inside a transaction block, so allow one to begin.
3944 : */
3945 16686 : case TBLOCK_STARTED:
3946 16686 : s->blockState = TBLOCK_BEGIN;
3947 16686 : break;
3948 :
3949 : /*
3950 : * BEGIN converts an implicit transaction block to a regular one.
3951 : * (Note that we allow this even if we've already done some
3952 : * commands, which is a bit odd but matches historical practice.)
3953 : */
3954 964 : case TBLOCK_IMPLICIT_INPROGRESS:
3955 964 : s->blockState = TBLOCK_BEGIN;
3956 964 : break;
3957 :
3958 : /*
3959 : * Already a transaction block in progress.
3960 : */
3961 0 : case TBLOCK_INPROGRESS:
3962 : case TBLOCK_PARALLEL_INPROGRESS:
3963 : case TBLOCK_SUBINPROGRESS:
3964 : case TBLOCK_ABORT:
3965 : case TBLOCK_SUBABORT:
3966 0 : ereport(WARNING,
3967 : (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3968 : errmsg("there is already a transaction in progress")));
3969 0 : break;
3970 :
3971 : /* These cases are invalid. */
3972 0 : case TBLOCK_DEFAULT:
3973 : case TBLOCK_BEGIN:
3974 : case TBLOCK_SUBBEGIN:
3975 : case TBLOCK_END:
3976 : case TBLOCK_SUBRELEASE:
3977 : case TBLOCK_SUBCOMMIT:
3978 : case TBLOCK_ABORT_END:
3979 : case TBLOCK_SUBABORT_END:
3980 : case TBLOCK_ABORT_PENDING:
3981 : case TBLOCK_SUBABORT_PENDING:
3982 : case TBLOCK_SUBRESTART:
3983 : case TBLOCK_SUBABORT_RESTART:
3984 : case TBLOCK_PREPARE:
3985 0 : elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3986 : BlockStateAsString(s->blockState));
3987 : break;
3988 : }
3989 17650 : }
3990 :
3991 : /*
3992 : * PrepareTransactionBlock
3993 : * This executes a PREPARE command.
3994 : *
3995 : * Since PREPARE may actually do a ROLLBACK, the result indicates what
3996 : * happened: true for PREPARE, false for ROLLBACK.
3997 : *
3998 : * Note that we don't actually do anything here except change blockState.
3999 : * The real work will be done in the upcoming PrepareTransaction().
4000 : * We do it this way because it's not convenient to change memory context,
4001 : * resource owner, etc while executing inside a Portal.
4002 : */
4003 : bool
4004 672 : PrepareTransactionBlock(const char *gid)
4005 : {
4006 : TransactionState s;
4007 : bool result;
4008 :
4009 : /* Set up to commit the current transaction */
4010 672 : result = EndTransactionBlock(false);
4011 :
4012 : /* If successful, change outer tblock state to PREPARE */
4013 672 : if (result)
4014 : {
4015 668 : s = CurrentTransactionState;
4016 :
4017 890 : while (s->parent != NULL)
4018 222 : s = s->parent;
4019 :
4020 668 : if (s->blockState == TBLOCK_END)
4021 : {
4022 : /* Save GID where PrepareTransaction can find it again */
4023 668 : prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
4024 :
4025 668 : s->blockState = TBLOCK_PREPARE;
4026 : }
4027 : else
4028 : {
4029 : /*
4030 : * ignore case where we are not in a transaction;
4031 : * EndTransactionBlock already issued a warning.
4032 : */
4033 : Assert(s->blockState == TBLOCK_STARTED ||
4034 : s->blockState == TBLOCK_IMPLICIT_INPROGRESS);
4035 : /* Don't send back a PREPARE result tag... */
4036 0 : result = false;
4037 : }
4038 : }
4039 :
4040 672 : return result;
4041 : }
4042 :
4043 : /*
4044 : * EndTransactionBlock
4045 : * This executes a COMMIT command.
4046 : *
4047 : * Since COMMIT may actually do a ROLLBACK, the result indicates what
4048 : * happened: true for COMMIT, false for ROLLBACK.
4049 : *
4050 : * Note that we don't actually do anything here except change blockState.
4051 : * The real work will be done in the upcoming CommitTransactionCommand().
4052 : * We do it this way because it's not convenient to change memory context,
4053 : * resource owner, etc while executing inside a Portal.
4054 : */
4055 : bool
4056 14322 : EndTransactionBlock(bool chain)
4057 : {
4058 14322 : TransactionState s = CurrentTransactionState;
4059 14322 : bool result = false;
4060 :
4061 14322 : switch (s->blockState)
4062 : {
4063 : /*
4064 : * We are in a transaction block, so tell CommitTransactionCommand
4065 : * to COMMIT.
4066 : */
4067 12534 : case TBLOCK_INPROGRESS:
4068 12534 : s->blockState = TBLOCK_END;
4069 12534 : result = true;
4070 12534 : break;
4071 :
4072 : /*
4073 : * We are in an implicit transaction block. If AND CHAIN was
4074 : * specified, error. Otherwise commit, but issue a warning
4075 : * because there was no explicit BEGIN before this.
4076 : */
4077 48 : case TBLOCK_IMPLICIT_INPROGRESS:
4078 48 : if (chain)
4079 24 : ereport(ERROR,
4080 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4081 : /* translator: %s represents an SQL statement name */
4082 : errmsg("%s can only be used in transaction blocks",
4083 : "COMMIT AND CHAIN")));
4084 : else
4085 24 : ereport(WARNING,
4086 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4087 : errmsg("there is no transaction in progress")));
4088 24 : s->blockState = TBLOCK_END;
4089 24 : result = true;
4090 24 : break;
4091 :
4092 : /*
4093 : * We are in a failed transaction block. Tell
4094 : * CommitTransactionCommand it's time to exit the block.
4095 : */
4096 714 : case TBLOCK_ABORT:
4097 714 : s->blockState = TBLOCK_ABORT_END;
4098 714 : break;
4099 :
4100 : /*
4101 : * We are in a live subtransaction block. Set up to subcommit all
4102 : * open subtransactions and then commit the main transaction.
4103 : */
4104 942 : case TBLOCK_SUBINPROGRESS:
4105 2018 : while (s->parent != NULL)
4106 : {
4107 1076 : if (s->blockState == TBLOCK_SUBINPROGRESS)
4108 1076 : s->blockState = TBLOCK_SUBCOMMIT;
4109 : else
4110 0 : elog(FATAL, "EndTransactionBlock: unexpected state %s",
4111 : BlockStateAsString(s->blockState));
4112 1076 : s = s->parent;
4113 : }
4114 942 : if (s->blockState == TBLOCK_INPROGRESS)
4115 942 : s->blockState = TBLOCK_END;
4116 : else
4117 0 : elog(FATAL, "EndTransactionBlock: unexpected state %s",
4118 : BlockStateAsString(s->blockState));
4119 942 : result = true;
4120 942 : break;
4121 :
4122 : /*
4123 : * Here we are inside an aborted subtransaction. Treat the COMMIT
4124 : * as ROLLBACK: set up to abort everything and exit the main
4125 : * transaction.
4126 : */
4127 60 : case TBLOCK_SUBABORT:
4128 120 : while (s->parent != NULL)
4129 : {
4130 60 : if (s->blockState == TBLOCK_SUBINPROGRESS)
4131 0 : s->blockState = TBLOCK_SUBABORT_PENDING;
4132 60 : else if (s->blockState == TBLOCK_SUBABORT)
4133 60 : s->blockState = TBLOCK_SUBABORT_END;
4134 : else
4135 0 : elog(FATAL, "EndTransactionBlock: unexpected state %s",
4136 : BlockStateAsString(s->blockState));
4137 60 : s = s->parent;
4138 : }
4139 60 : if (s->blockState == TBLOCK_INPROGRESS)
4140 60 : s->blockState = TBLOCK_ABORT_PENDING;
4141 0 : else if (s->blockState == TBLOCK_ABORT)
4142 0 : s->blockState = TBLOCK_ABORT_END;
4143 : else
4144 0 : elog(FATAL, "EndTransactionBlock: unexpected state %s",
4145 : BlockStateAsString(s->blockState));
4146 60 : break;
4147 :
4148 : /*
4149 : * The user issued COMMIT when not inside a transaction. For
4150 : * COMMIT without CHAIN, issue a WARNING, staying in
4151 : * TBLOCK_STARTED state. The upcoming call to
4152 : * CommitTransactionCommand() will then close the transaction and
4153 : * put us back into the default state. For COMMIT AND CHAIN,
4154 : * error.
4155 : */
4156 24 : case TBLOCK_STARTED:
4157 24 : if (chain)
4158 6 : ereport(ERROR,
4159 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4160 : /* translator: %s represents an SQL statement name */
4161 : errmsg("%s can only be used in transaction blocks",
4162 : "COMMIT AND CHAIN")));
4163 : else
4164 18 : ereport(WARNING,
4165 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4166 : errmsg("there is no transaction in progress")));
4167 18 : result = true;
4168 18 : break;
4169 :
4170 : /*
4171 : * The user issued a COMMIT that somehow ran inside a parallel
4172 : * worker. We can't cope with that.
4173 : */
4174 0 : case TBLOCK_PARALLEL_INPROGRESS:
4175 0 : ereport(FATAL,
4176 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4177 : errmsg("cannot commit during a parallel operation")));
4178 : break;
4179 :
4180 : /* These cases are invalid. */
4181 0 : case TBLOCK_DEFAULT:
4182 : case TBLOCK_BEGIN:
4183 : case TBLOCK_SUBBEGIN:
4184 : case TBLOCK_END:
4185 : case TBLOCK_SUBRELEASE:
4186 : case TBLOCK_SUBCOMMIT:
4187 : case TBLOCK_ABORT_END:
4188 : case TBLOCK_SUBABORT_END:
4189 : case TBLOCK_ABORT_PENDING:
4190 : case TBLOCK_SUBABORT_PENDING:
4191 : case TBLOCK_SUBRESTART:
4192 : case TBLOCK_SUBABORT_RESTART:
4193 : case TBLOCK_PREPARE:
4194 0 : elog(FATAL, "EndTransactionBlock: unexpected state %s",
4195 : BlockStateAsString(s->blockState));
4196 : break;
4197 : }
4198 :
4199 : Assert(s->blockState == TBLOCK_STARTED ||
4200 : s->blockState == TBLOCK_END ||
4201 : s->blockState == TBLOCK_ABORT_END ||
4202 : s->blockState == TBLOCK_ABORT_PENDING);
4203 :
4204 14292 : s->chain = chain;
4205 :
4206 14292 : return result;
4207 : }
4208 :
4209 : /*
4210 : * UserAbortTransactionBlock
4211 : * This executes a ROLLBACK command.
4212 : *
4213 : * As above, we don't actually do anything here except change blockState.
4214 : */
4215 : void
4216 2852 : UserAbortTransactionBlock(bool chain)
4217 : {
4218 2852 : TransactionState s = CurrentTransactionState;
4219 :
4220 2852 : switch (s->blockState)
4221 : {
4222 : /*
4223 : * We are inside a transaction block and we got a ROLLBACK command
4224 : * from the user, so tell CommitTransactionCommand to abort and
4225 : * exit the transaction block.
4226 : */
4227 2002 : case TBLOCK_INPROGRESS:
4228 2002 : s->blockState = TBLOCK_ABORT_PENDING;
4229 2002 : break;
4230 :
4231 : /*
4232 : * We are inside a failed transaction block and we got a ROLLBACK
4233 : * command from the user. Abort processing is already done, so
4234 : * CommitTransactionCommand just has to cleanup and go back to
4235 : * idle state.
4236 : */
4237 662 : case TBLOCK_ABORT:
4238 662 : s->blockState = TBLOCK_ABORT_END;
4239 662 : break;
4240 :
4241 : /*
4242 : * We are inside a subtransaction. Mark everything up to top
4243 : * level as exitable.
4244 : */
4245 110 : case TBLOCK_SUBINPROGRESS:
4246 : case TBLOCK_SUBABORT:
4247 432 : while (s->parent != NULL)
4248 : {
4249 322 : if (s->blockState == TBLOCK_SUBINPROGRESS)
4250 302 : s->blockState = TBLOCK_SUBABORT_PENDING;
4251 20 : else if (s->blockState == TBLOCK_SUBABORT)
4252 20 : s->blockState = TBLOCK_SUBABORT_END;
4253 : else
4254 0 : elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4255 : BlockStateAsString(s->blockState));
4256 322 : s = s->parent;
4257 : }
4258 110 : if (s->blockState == TBLOCK_INPROGRESS)
4259 110 : s->blockState = TBLOCK_ABORT_PENDING;
4260 0 : else if (s->blockState == TBLOCK_ABORT)
4261 0 : s->blockState = TBLOCK_ABORT_END;
4262 : else
4263 0 : elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4264 : BlockStateAsString(s->blockState));
4265 110 : break;
4266 :
4267 : /*
4268 : * The user issued ABORT when not inside a transaction. For
4269 : * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
4270 : * The upcoming call to CommitTransactionCommand() will then put
4271 : * us back into the default state. For ROLLBACK AND CHAIN, error.
4272 : *
4273 : * We do the same thing with ABORT inside an implicit transaction,
4274 : * although in this case we might be rolling back actual database
4275 : * state changes. (It's debatable whether we should issue a
4276 : * WARNING in this case, but we have done so historically.)
4277 : */
4278 78 : case TBLOCK_STARTED:
4279 : case TBLOCK_IMPLICIT_INPROGRESS:
4280 78 : if (chain)
4281 30 : ereport(ERROR,
4282 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4283 : /* translator: %s represents an SQL statement name */
4284 : errmsg("%s can only be used in transaction blocks",
4285 : "ROLLBACK AND CHAIN")));
4286 : else
4287 48 : ereport(WARNING,
4288 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4289 : errmsg("there is no transaction in progress")));
4290 48 : s->blockState = TBLOCK_ABORT_PENDING;
4291 48 : break;
4292 :
4293 : /*
4294 : * The user issued an ABORT that somehow ran inside a parallel
4295 : * worker. We can't cope with that.
4296 : */
4297 0 : case TBLOCK_PARALLEL_INPROGRESS:
4298 0 : ereport(FATAL,
4299 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4300 : errmsg("cannot abort during a parallel operation")));
4301 : break;
4302 :
4303 : /* These cases are invalid. */
4304 0 : case TBLOCK_DEFAULT:
4305 : case TBLOCK_BEGIN:
4306 : case TBLOCK_SUBBEGIN:
4307 : case TBLOCK_END:
4308 : case TBLOCK_SUBRELEASE:
4309 : case TBLOCK_SUBCOMMIT:
4310 : case TBLOCK_ABORT_END:
4311 : case TBLOCK_SUBABORT_END:
4312 : case TBLOCK_ABORT_PENDING:
4313 : case TBLOCK_SUBABORT_PENDING:
4314 : case TBLOCK_SUBRESTART:
4315 : case TBLOCK_SUBABORT_RESTART:
4316 : case TBLOCK_PREPARE:
4317 0 : elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
4318 : BlockStateAsString(s->blockState));
4319 : break;
4320 : }
4321 :
4322 : Assert(s->blockState == TBLOCK_ABORT_END ||
4323 : s->blockState == TBLOCK_ABORT_PENDING);
4324 :
4325 2822 : s->chain = chain;
4326 2822 : }
4327 :
4328 : /*
4329 : * BeginImplicitTransactionBlock
4330 : * Start an implicit transaction block if we're not already in one.
4331 : *
4332 : * Unlike BeginTransactionBlock, this is called directly from the main loop
4333 : * in postgres.c, not within a Portal. So we can just change blockState
4334 : * without a lot of ceremony. We do not expect caller to do
4335 : * CommitTransactionCommand/StartTransactionCommand.
4336 : */
4337 : void
4338 89784 : BeginImplicitTransactionBlock(void)
4339 : {
4340 89784 : TransactionState s = CurrentTransactionState;
4341 :
4342 : /*
4343 : * If we are in STARTED state (that is, no transaction block is open),
4344 : * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4345 : * block.
4346 : *
4347 : * For caller convenience, we consider all other transaction states as
4348 : * legal here; otherwise the caller would need its own state check, which
4349 : * seems rather pointless.
4350 : */
4351 89784 : if (s->blockState == TBLOCK_STARTED)
4352 10800 : s->blockState = TBLOCK_IMPLICIT_INPROGRESS;
4353 89784 : }
4354 :
4355 : /*
4356 : * EndImplicitTransactionBlock
4357 : * End an implicit transaction block, if we're in one.
4358 : *
4359 : * Like EndTransactionBlock, we just make any needed blockState change here.
4360 : * The real work will be done in the upcoming CommitTransactionCommand().
4361 : */
4362 : void
4363 36144 : EndImplicitTransactionBlock(void)
4364 : {
4365 36144 : TransactionState s = CurrentTransactionState;
4366 :
4367 : /*
4368 : * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4369 : * allowing CommitTransactionCommand to commit whatever happened during
4370 : * the implicit transaction block as though it were a single statement.
4371 : *
4372 : * For caller convenience, we consider all other transaction states as
4373 : * legal here; otherwise the caller would need its own state check, which
4374 : * seems rather pointless.
4375 : */
4376 36144 : if (s->blockState == TBLOCK_IMPLICIT_INPROGRESS)
4377 9664 : s->blockState = TBLOCK_STARTED;
4378 36144 : }
4379 :
4380 : /*
4381 : * DefineSavepoint
4382 : * This executes a SAVEPOINT command.
4383 : */
4384 : void
4385 2692 : DefineSavepoint(const char *name)
4386 : {
4387 2692 : TransactionState s = CurrentTransactionState;
4388 :
4389 : /*
4390 : * Workers synchronize transaction state at the beginning of each parallel
4391 : * operation, so we can't account for new subtransactions after that
4392 : * point. (Note that this check will certainly error out if s->blockState
4393 : * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4394 : * below.)
4395 : */
4396 2692 : if (IsInParallelMode() || IsParallelWorker())
4397 0 : ereport(ERROR,
4398 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4399 : errmsg("cannot define savepoints during a parallel operation")));
4400 :
4401 2692 : switch (s->blockState)
4402 : {
4403 2680 : case TBLOCK_INPROGRESS:
4404 : case TBLOCK_SUBINPROGRESS:
4405 : /* Normal subtransaction start */
4406 2680 : PushTransaction();
4407 2680 : s = CurrentTransactionState; /* changed by push */
4408 :
4409 : /*
4410 : * Savepoint names, like the TransactionState block itself, live
4411 : * in TopTransactionContext.
4412 : */
4413 2680 : if (name)
4414 1958 : s->name = MemoryContextStrdup(TopTransactionContext, name);
4415 2680 : break;
4416 :
4417 : /*
4418 : * We disallow savepoint commands in implicit transaction blocks.
4419 : * There would be no great difficulty in allowing them so far as
4420 : * this module is concerned, but a savepoint seems inconsistent
4421 : * with exec_simple_query's behavior of abandoning the whole query
4422 : * string upon error. Also, the point of an implicit transaction
4423 : * block (as opposed to a regular one) is to automatically close
4424 : * after an error, so it's hard to see how a savepoint would fit
4425 : * into that.
4426 : *
4427 : * The error messages for this are phrased as if there were no
4428 : * active transaction block at all, which is historical but
4429 : * perhaps could be improved.
4430 : */
4431 12 : case TBLOCK_IMPLICIT_INPROGRESS:
4432 12 : ereport(ERROR,
4433 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4434 : /* translator: %s represents an SQL statement name */
4435 : errmsg("%s can only be used in transaction blocks",
4436 : "SAVEPOINT")));
4437 : break;
4438 :
4439 : /* These cases are invalid. */
4440 0 : case TBLOCK_DEFAULT:
4441 : case TBLOCK_STARTED:
4442 : case TBLOCK_BEGIN:
4443 : case TBLOCK_PARALLEL_INPROGRESS:
4444 : case TBLOCK_SUBBEGIN:
4445 : case TBLOCK_END:
4446 : case TBLOCK_SUBRELEASE:
4447 : case TBLOCK_SUBCOMMIT:
4448 : case TBLOCK_ABORT:
4449 : case TBLOCK_SUBABORT:
4450 : case TBLOCK_ABORT_END:
4451 : case TBLOCK_SUBABORT_END:
4452 : case TBLOCK_ABORT_PENDING:
4453 : case TBLOCK_SUBABORT_PENDING:
4454 : case TBLOCK_SUBRESTART:
4455 : case TBLOCK_SUBABORT_RESTART:
4456 : case TBLOCK_PREPARE:
4457 0 : elog(FATAL, "DefineSavepoint: unexpected state %s",
4458 : BlockStateAsString(s->blockState));
4459 : break;
4460 : }
4461 2680 : }
4462 :
4463 : /*
4464 : * ReleaseSavepoint
4465 : * This executes a RELEASE command.
4466 : *
4467 : * As above, we don't actually do anything here except change blockState.
4468 : */
4469 : void
4470 282 : ReleaseSavepoint(const char *name)
4471 : {
4472 282 : TransactionState s = CurrentTransactionState;
4473 : TransactionState target,
4474 : xact;
4475 :
4476 : /*
4477 : * Workers synchronize transaction state at the beginning of each parallel
4478 : * operation, so we can't account for transaction state change after that
4479 : * point. (Note that this check will certainly error out if s->blockState
4480 : * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4481 : * below.)
4482 : */
4483 282 : if (IsInParallelMode() || IsParallelWorker())
4484 0 : ereport(ERROR,
4485 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4486 : errmsg("cannot release savepoints during a parallel operation")));
4487 :
4488 282 : switch (s->blockState)
4489 : {
4490 : /*
4491 : * We can't release a savepoint if there is no savepoint defined.
4492 : */
4493 0 : case TBLOCK_INPROGRESS:
4494 0 : ereport(ERROR,
4495 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4496 : errmsg("savepoint \"%s\" does not exist", name)));
4497 : break;
4498 :
4499 6 : case TBLOCK_IMPLICIT_INPROGRESS:
4500 : /* See comment about implicit transactions in DefineSavepoint */
4501 6 : ereport(ERROR,
4502 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4503 : /* translator: %s represents an SQL statement name */
4504 : errmsg("%s can only be used in transaction blocks",
4505 : "RELEASE SAVEPOINT")));
4506 : break;
4507 :
4508 : /*
4509 : * We are in a non-aborted subtransaction. This is the only valid
4510 : * case.
4511 : */
4512 276 : case TBLOCK_SUBINPROGRESS:
4513 276 : break;
4514 :
4515 : /* These cases are invalid. */
4516 0 : case TBLOCK_DEFAULT:
4517 : case TBLOCK_STARTED:
4518 : case TBLOCK_BEGIN:
4519 : case TBLOCK_PARALLEL_INPROGRESS:
4520 : case TBLOCK_SUBBEGIN:
4521 : case TBLOCK_END:
4522 : case TBLOCK_SUBRELEASE:
4523 : case TBLOCK_SUBCOMMIT:
4524 : case TBLOCK_ABORT:
4525 : case TBLOCK_SUBABORT:
4526 : case TBLOCK_ABORT_END:
4527 : case TBLOCK_SUBABORT_END:
4528 : case TBLOCK_ABORT_PENDING:
4529 : case TBLOCK_SUBABORT_PENDING:
4530 : case TBLOCK_SUBRESTART:
4531 : case TBLOCK_SUBABORT_RESTART:
4532 : case TBLOCK_PREPARE:
4533 0 : elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4534 : BlockStateAsString(s->blockState));
4535 : break;
4536 : }
4537 :
4538 448 : for (target = s; PointerIsValid(target); target = target->parent)
4539 : {
4540 448 : if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4541 276 : break;
4542 : }
4543 :
4544 276 : if (!PointerIsValid(target))
4545 0 : ereport(ERROR,
4546 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4547 : errmsg("savepoint \"%s\" does not exist", name)));
4548 :
4549 : /* disallow crossing savepoint level boundaries */
4550 276 : if (target->savepointLevel != s->savepointLevel)
4551 0 : ereport(ERROR,
4552 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4553 : errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4554 :
4555 : /*
4556 : * Mark "commit pending" all subtransactions up to the target
4557 : * subtransaction. The actual commits will happen when control gets to
4558 : * CommitTransactionCommand.
4559 : */
4560 276 : xact = CurrentTransactionState;
4561 : for (;;)
4562 : {
4563 172 : Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
4564 448 : xact->blockState = TBLOCK_SUBRELEASE;
4565 448 : if (xact == target)
4566 276 : break;
4567 172 : xact = xact->parent;
4568 : Assert(PointerIsValid(xact));
4569 : }
4570 276 : }
4571 :
4572 : /*
4573 : * RollbackToSavepoint
4574 : * This executes a ROLLBACK TO <savepoint> command.
4575 : *
4576 : * As above, we don't actually do anything here except change blockState.
4577 : */
4578 : void
4579 734 : RollbackToSavepoint(const char *name)
4580 : {
4581 734 : TransactionState s = CurrentTransactionState;
4582 : TransactionState target,
4583 : xact;
4584 :
4585 : /*
4586 : * Workers synchronize transaction state at the beginning of each parallel
4587 : * operation, so we can't account for transaction state change after that
4588 : * point. (Note that this check will certainly error out if s->blockState
4589 : * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4590 : * below.)
4591 : */
4592 734 : if (IsInParallelMode() || IsParallelWorker())
4593 0 : ereport(ERROR,
4594 : (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4595 : errmsg("cannot rollback to savepoints during a parallel operation")));
4596 :
4597 734 : switch (s->blockState)
4598 : {
4599 : /*
4600 : * We can't rollback to a savepoint if there is no savepoint
4601 : * defined.
4602 : */
4603 6 : case TBLOCK_INPROGRESS:
4604 : case TBLOCK_ABORT:
4605 6 : ereport(ERROR,
4606 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4607 : errmsg("savepoint \"%s\" does not exist", name)));
4608 : break;
4609 :
4610 6 : case TBLOCK_IMPLICIT_INPROGRESS:
4611 : /* See comment about implicit transactions in DefineSavepoint */
4612 6 : ereport(ERROR,
4613 : (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4614 : /* translator: %s represents an SQL statement name */
4615 : errmsg("%s can only be used in transaction blocks",
4616 : "ROLLBACK TO SAVEPOINT")));
4617 : break;
4618 :
4619 : /*
4620 : * There is at least one savepoint, so proceed.
4621 : */
4622 722 : case TBLOCK_SUBINPROGRESS:
4623 : case TBLOCK_SUBABORT:
4624 722 : break;
4625 :
4626 : /* These cases are invalid. */
4627 0 : case TBLOCK_DEFAULT:
4628 : case TBLOCK_STARTED:
4629 : case TBLOCK_BEGIN:
4630 : case TBLOCK_PARALLEL_INPROGRESS:
4631 : case TBLOCK_SUBBEGIN:
4632 : case TBLOCK_END:
4633 : case TBLOCK_SUBRELEASE:
4634 : case TBLOCK_SUBCOMMIT:
4635 : case TBLOCK_ABORT_END:
4636 : case TBLOCK_SUBABORT_END:
4637 : case TBLOCK_ABORT_PENDING:
4638 : case TBLOCK_SUBABORT_PENDING:
4639 : case TBLOCK_SUBRESTART:
4640 : case TBLOCK_SUBABORT_RESTART:
4641 : case TBLOCK_PREPARE:
4642 0 : elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4643 : BlockStateAsString(s->blockState));
4644 : break;
4645 : }
4646 :
4647 774 : for (target = s; PointerIsValid(target); target = target->parent)
4648 : {
4649 774 : if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4650 722 : break;
4651 : }
4652 :
4653 722 : if (!PointerIsValid(target))
4654 0 : ereport(ERROR,
4655 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4656 : errmsg("savepoint \"%s\" does not exist", name)));
4657 :
4658 : /* disallow crossing savepoint level boundaries */
4659 722 : if (target->savepointLevel != s->savepointLevel)
4660 0 : ereport(ERROR,
4661 : (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4662 : errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4663 :
4664 : /*
4665 : * Mark "abort pending" all subtransactions up to the target
4666 : * subtransaction. The actual aborts will happen when control gets to
4667 : * CommitTransactionCommand.
4668 : */
4669 722 : xact = CurrentTransactionState;
4670 : for (;;)
4671 : {
4672 774 : if (xact == target)
4673 722 : break;
4674 52 : if (xact->blockState == TBLOCK_SUBINPROGRESS)
4675 52 : xact->blockState = TBLOCK_SUBABORT_PENDING;
4676 0 : else if (xact->blockState == TBLOCK_SUBABORT)
4677 0 : xact->blockState = TBLOCK_SUBABORT_END;
4678 : else
4679 0 : elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4680 : BlockStateAsString(xact->blockState));
4681 52 : xact = xact->parent;
4682 : Assert(PointerIsValid(xact));
4683 : }
4684 :
4685 : /* And mark the target as "restart pending" */
4686 722 : if (xact->blockState == TBLOCK_SUBINPROGRESS)
4687 520 : xact->blockState = TBLOCK_SUBRESTART;
4688 202 : else if (xact->blockState == TBLOCK_SUBABORT)
4689 202 : xact->blockState = TBLOCK_SUBABORT_RESTART;
4690 : else
4691 0 : elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4692 : BlockStateAsString(xact->blockState));
4693 722 : }
4694 :
4695 : /*
4696 : * BeginInternalSubTransaction
4697 : * This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
4698 : * TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_END,
4699 : * and TBLOCK_PREPARE states, and therefore it can safely be used in
4700 : * functions that might be called when not inside a BEGIN block or when
4701 : * running deferred triggers at COMMIT/PREPARE time. Also, it
4702 : * automatically does CommitTransactionCommand/StartTransactionCommand
4703 : * instead of expecting the caller to do it.
4704 : */
4705 : void
4706 17372 : BeginInternalSubTransaction(const char *name)
4707 : {
4708 17372 : TransactionState s = CurrentTransactionState;
4709 17372 : bool save_ExitOnAnyError = ExitOnAnyError;
4710 :
4711 : /*
4712 : * Errors within this function are improbable, but if one does happen we
4713 : * force a FATAL exit. Callers generally aren't prepared to handle losing
4714 : * control, and moreover our transaction state is probably corrupted if we
4715 : * fail partway through; so an ordinary ERROR longjmp isn't okay.
4716 : */
4717 17372 : ExitOnAnyError = true;
4718 :
4719 : /*
4720 : * We do not check for parallel mode here. It's permissible to start and
4721 : * end "internal" subtransactions while in parallel mode, so long as no
4722 : * new XIDs or command IDs are assigned. Enforcement of that occurs in
4723 : * AssignTransactionId() and CommandCounterIncrement().
4724 : */
4725 :
4726 17372 : switch (s->blockState)
4727 : {
4728 17372 : case TBLOCK_STARTED:
4729 : case TBLOCK_INPROGRESS:
4730 : case TBLOCK_IMPLICIT_INPROGRESS:
4731 : case TBLOCK_PARALLEL_INPROGRESS:
4732 : case TBLOCK_END:
4733 : case TBLOCK_PREPARE:
4734 : case TBLOCK_SUBINPROGRESS:
4735 : /* Normal subtransaction start */
4736 17372 : PushTransaction();
4737 17372 : s = CurrentTransactionState; /* changed by push */
4738 :
4739 : /*
4740 : * Savepoint names, like the TransactionState block itself, live
4741 : * in TopTransactionContext.
4742 : */
4743 17372 : if (name)
4744 1854 : s->name = MemoryContextStrdup(TopTransactionContext, name);
4745 17372 : break;
4746 :
4747 : /* These cases are invalid. */
4748 0 : case TBLOCK_DEFAULT:
4749 : case TBLOCK_BEGIN:
4750 : case TBLOCK_SUBBEGIN:
4751 : case TBLOCK_SUBRELEASE:
4752 : case TBLOCK_SUBCOMMIT:
4753 : case TBLOCK_ABORT:
4754 : case TBLOCK_SUBABORT:
4755 : case TBLOCK_ABORT_END:
4756 : case TBLOCK_SUBABORT_END:
4757 : case TBLOCK_ABORT_PENDING:
4758 : case TBLOCK_SUBABORT_PENDING:
4759 : case TBLOCK_SUBRESTART:
4760 : case TBLOCK_SUBABORT_RESTART:
4761 0 : elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4762 : BlockStateAsString(s->blockState));
4763 : break;
4764 : }
4765 :
4766 17372 : CommitTransactionCommand();
4767 17372 : StartTransactionCommand();
4768 :
4769 17372 : ExitOnAnyError = save_ExitOnAnyError;
4770 17372 : }
4771 :
4772 : /*
4773 : * ReleaseCurrentSubTransaction
4774 : *
4775 : * RELEASE (ie, commit) the innermost subtransaction, regardless of its
4776 : * savepoint name (if any).
4777 : * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4778 : */
4779 : void
4780 9162 : ReleaseCurrentSubTransaction(void)
4781 : {
4782 9162 : TransactionState s = CurrentTransactionState;
4783 :
4784 : /*
4785 : * We do not check for parallel mode here. It's permissible to start and
4786 : * end "internal" subtransactions while in parallel mode, so long as no
4787 : * new XIDs or command IDs are assigned.
4788 : */
4789 :
4790 9162 : if (s->blockState != TBLOCK_SUBINPROGRESS)
4791 0 : elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4792 : BlockStateAsString(s->blockState));
4793 : Assert(s->state == TRANS_INPROGRESS);
4794 9162 : MemoryContextSwitchTo(CurTransactionContext);
4795 9162 : CommitSubTransaction();
4796 9162 : s = CurrentTransactionState; /* changed by pop */
4797 : Assert(s->state == TRANS_INPROGRESS);
4798 9162 : }
4799 :
4800 : /*
4801 : * RollbackAndReleaseCurrentSubTransaction
4802 : *
4803 : * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
4804 : * of its savepoint name (if any).
4805 : * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
4806 : */
4807 : void
4808 8210 : RollbackAndReleaseCurrentSubTransaction(void)
4809 : {
4810 8210 : TransactionState s = CurrentTransactionState;
4811 :
4812 : /*
4813 : * We do not check for parallel mode here. It's permissible to start and
4814 : * end "internal" subtransactions while in parallel mode, so long as no
4815 : * new XIDs or command IDs are assigned.
4816 : */
4817 :
4818 8210 : switch (s->blockState)
4819 : {
4820 : /* Must be in a subtransaction */
4821 8210 : case TBLOCK_SUBINPROGRESS:
4822 : case TBLOCK_SUBABORT:
4823 8210 : break;
4824 :
4825 : /* These cases are invalid. */
4826 0 : case TBLOCK_DEFAULT:
4827 : case TBLOCK_STARTED:
4828 : case TBLOCK_BEGIN:
4829 : case TBLOCK_IMPLICIT_INPROGRESS:
4830 : case TBLOCK_PARALLEL_INPROGRESS:
4831 : case TBLOCK_SUBBEGIN:
4832 : case TBLOCK_INPROGRESS:
4833 : case TBLOCK_END:
4834 : case TBLOCK_SUBRELEASE:
4835 : case TBLOCK_SUBCOMMIT:
4836 : case TBLOCK_ABORT:
4837 : case TBLOCK_ABORT_END:
4838 : case TBLOCK_SUBABORT_END:
4839 : case TBLOCK_ABORT_PENDING:
4840 : case TBLOCK_SUBABORT_PENDING:
4841 : case TBLOCK_SUBRESTART:
4842 : case TBLOCK_SUBABORT_RESTART:
4843 : case TBLOCK_PREPARE:
4844 0 : elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4845 : BlockStateAsString(s->blockState));
4846 : break;
4847 : }
4848 :
4849 : /*
4850 : * Abort the current subtransaction, if needed.
4851 : */
4852 8210 : if (s->blockState == TBLOCK_SUBINPROGRESS)
4853 6356 : AbortSubTransaction();
4854 :
4855 : /* And clean it up, too */
4856 8210 : CleanupSubTransaction();
4857 :
4858 8210 : s = CurrentTransactionState; /* changed by pop */
4859 : Assert(s->blockState == TBLOCK_SUBINPROGRESS ||
4860 : s->blockState == TBLOCK_INPROGRESS ||
4861 : s->blockState == TBLOCK_IMPLICIT_INPROGRESS ||
4862 : s->blockState == TBLOCK_PARALLEL_INPROGRESS ||
4863 : s->blockState == TBLOCK_STARTED);
4864 8210 : }
4865 :
4866 : /*
4867 : * AbortOutOfAnyTransaction
4868 : *
4869 : * This routine is provided for error recovery purposes. It aborts any
4870 : * active transaction or transaction block, leaving the system in a known
4871 : * idle state.
4872 : */
4873 : void
4874 34916 : AbortOutOfAnyTransaction(void)
4875 : {
4876 34916 : TransactionState s = CurrentTransactionState;
4877 :
4878 : /* Ensure we're not running in a doomed memory context */
4879 34916 : AtAbort_Memory();
4880 :
4881 : /*
4882 : * Get out of any transaction or nested transaction
4883 : */
4884 : do
4885 : {
4886 34916 : switch (s->blockState)
4887 : {
4888 33792 : case TBLOCK_DEFAULT:
4889 33792 : if (s->state == TRANS_DEFAULT)
4890 : {
4891 : /* Not in a transaction, do nothing */
4892 : }
4893 : else
4894 : {
4895 : /*
4896 : * We can get here after an error during transaction start
4897 : * (state will be TRANS_START). Need to clean up the
4898 : * incompletely started transaction. First, adjust the
4899 : * low-level state to suppress warning message from
4900 : * AbortTransaction.
4901 : */
4902 0 : if (s->state == TRANS_START)
4903 0 : s->state = TRANS_INPROGRESS;
4904 0 : AbortTransaction();
4905 0 : CleanupTransaction();
4906 : }
4907 33792 : break;
4908 1102 : case TBLOCK_STARTED:
4909 : case TBLOCK_BEGIN:
4910 : case TBLOCK_INPROGRESS:
4911 : case TBLOCK_IMPLICIT_INPROGRESS:
4912 : case TBLOCK_PARALLEL_INPROGRESS:
4913 : case TBLOCK_END:
4914 : case TBLOCK_ABORT_PENDING:
4915 : case TBLOCK_PREPARE:
4916 : /* In a transaction, so clean up */
4917 1102 : AbortTransaction();
4918 1102 : CleanupTransaction();
4919 1102 : s->blockState = TBLOCK_DEFAULT;
4920 1102 : break;
4921 22 : case TBLOCK_ABORT:
4922 : case TBLOCK_ABORT_END:
4923 :
4924 : /*
4925 : * AbortTransaction is already done, still need Cleanup.
4926 : * However, if we failed partway through running ROLLBACK,
4927 : * there will be an active portal running that command, which
4928 : * we need to shut down before doing CleanupTransaction.
4929 : */
4930 22 : AtAbort_Portals();
4931 22 : CleanupTransaction();
4932 22 : s->blockState = TBLOCK_DEFAULT;
4933 22 : break;
4934 :
4935 : /*
4936 : * In a subtransaction, so clean it up and abort parent too
4937 : */
4938 0 : case TBLOCK_SUBBEGIN:
4939 : case TBLOCK_SUBINPROGRESS:
4940 : case TBLOCK_SUBRELEASE:
4941 : case TBLOCK_SUBCOMMIT:
4942 : case TBLOCK_SUBABORT_PENDING:
4943 : case TBLOCK_SUBRESTART:
4944 0 : AbortSubTransaction();
4945 0 : CleanupSubTransaction();
4946 0 : s = CurrentTransactionState; /* changed by pop */
4947 0 : break;
4948 :
4949 0 : case TBLOCK_SUBABORT:
4950 : case TBLOCK_SUBABORT_END:
4951 : case TBLOCK_SUBABORT_RESTART:
4952 : /* As above, but AbortSubTransaction already done */
4953 0 : if (s->curTransactionOwner)
4954 : {
4955 : /* As in TBLOCK_ABORT, might have a live portal to zap */
4956 0 : AtSubAbort_Portals(s->subTransactionId,
4957 0 : s->parent->subTransactionId,
4958 : s->curTransactionOwner,
4959 0 : s->parent->curTransactionOwner);
4960 : }
4961 0 : CleanupSubTransaction();
4962 0 : s = CurrentTransactionState; /* changed by pop */
4963 0 : break;
4964 : }
4965 34916 : } while (s->blockState != TBLOCK_DEFAULT);
4966 :
4967 : /* Should be out of all subxacts now */
4968 : Assert(s->parent == NULL);
4969 :
4970 : /*
4971 : * Revert to TopMemoryContext, to ensure we exit in a well-defined state
4972 : * whether there were any transactions to close or not. (Callers that
4973 : * don't intend to exit soon should switch to some other context to avoid
4974 : * long-term memory leaks.)
4975 : */
4976 34916 : MemoryContextSwitchTo(TopMemoryContext);
4977 34916 : }
4978 :
4979 : /*
4980 : * IsTransactionBlock --- are we within a transaction block?
4981 : */
4982 : bool
4983 603988 : IsTransactionBlock(void)
4984 : {
4985 603988 : TransactionState s = CurrentTransactionState;
4986 :
4987 603988 : if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
4988 474182 : return false;
4989 :
4990 129806 : return true;
4991 : }
4992 :
4993 : /*
4994 : * IsTransactionOrTransactionBlock --- are we within either a transaction
4995 : * or a transaction block? (The backend is only really "idle" when this
4996 : * returns false.)
4997 : *
4998 : * This should match up with IsTransactionBlock and IsTransactionState.
4999 : */
5000 : bool
5001 743602 : IsTransactionOrTransactionBlock(void)
5002 : {
5003 743602 : TransactionState s = CurrentTransactionState;
5004 :
5005 743602 : if (s->blockState == TBLOCK_DEFAULT)
5006 572036 : return false;
5007 :
5008 171566 : return true;
5009 : }
5010 :
5011 : /*
5012 : * TransactionBlockStatusCode - return status code to send in ReadyForQuery
5013 : */
5014 : char
5015 662156 : TransactionBlockStatusCode(void)
5016 : {
5017 662156 : TransactionState s = CurrentTransactionState;
5018 :
5019 662156 : switch (s->blockState)
5020 : {
5021 492406 : case TBLOCK_DEFAULT:
5022 : case TBLOCK_STARTED:
5023 492406 : return 'I'; /* idle --- not in transaction */
5024 167954 : case TBLOCK_BEGIN:
5025 : case TBLOCK_SUBBEGIN:
5026 : case TBLOCK_INPROGRESS:
5027 : case TBLOCK_IMPLICIT_INPROGRESS:
5028 : case TBLOCK_PARALLEL_INPROGRESS:
5029 : case TBLOCK_SUBINPROGRESS:
5030 : case TBLOCK_END:
5031 : case TBLOCK_SUBRELEASE:
5032 : case TBLOCK_SUBCOMMIT:
5033 : case TBLOCK_PREPARE:
5034 167954 : return 'T'; /* in transaction */
5035 1796 : case TBLOCK_ABORT:
5036 : case TBLOCK_SUBABORT:
5037 : case TBLOCK_ABORT_END:
5038 : case TBLOCK_SUBABORT_END:
5039 : case TBLOCK_ABORT_PENDING:
5040 : case TBLOCK_SUBABORT_PENDING:
5041 : case TBLOCK_SUBRESTART:
5042 : case TBLOCK_SUBABORT_RESTART:
5043 1796 : return 'E'; /* in failed transaction */
5044 : }
5045 :
5046 : /* should never get here */
5047 0 : elog(FATAL, "invalid transaction block state: %s",
5048 : BlockStateAsString(s->blockState));
5049 : return 0; /* keep compiler quiet */
5050 : }
5051 :
5052 : /*
5053 : * IsSubTransaction
5054 : */
5055 : bool
5056 1297976 : IsSubTransaction(void)
5057 : {
5058 1297976 : TransactionState s = CurrentTransactionState;
5059 :
5060 1297976 : if (s->nestingLevel >= 2)
5061 486 : return true;
5062 :
5063 1297490 : return false;
5064 : }
5065 :
5066 : /*
5067 : * StartSubTransaction
5068 : *
5069 : * If you're wondering why this is separate from PushTransaction: it's because
5070 : * we can't conveniently do this stuff right inside DefineSavepoint. The
5071 : * SAVEPOINT utility command will be executed inside a Portal, and if we
5072 : * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
5073 : * the Portal will undo those settings. So we make DefineSavepoint just
5074 : * push a dummy transaction block, and when control returns to the main
5075 : * idle loop, CommitTransactionCommand will be called, and we'll come here
5076 : * to finish starting the subtransaction.
5077 : */
5078 : static void
5079 20052 : StartSubTransaction(void)
5080 : {
5081 20052 : TransactionState s = CurrentTransactionState;
5082 :
5083 20052 : if (s->state != TRANS_DEFAULT)
5084 0 : elog(WARNING, "StartSubTransaction while in %s state",
5085 : TransStateAsString(s->state));
5086 :
5087 20052 : s->state = TRANS_START;
5088 :
5089 : /*
5090 : * Initialize subsystems for new subtransaction
5091 : *
5092 : * must initialize resource-management stuff first
5093 : */
5094 20052 : AtSubStart_Memory();
5095 20052 : AtSubStart_ResourceOwner();
5096 20052 : AfterTriggerBeginSubXact();
5097 :
5098 20052 : s->state = TRANS_INPROGRESS;
5099 :
5100 : /*
5101 : * Call start-of-subxact callbacks
5102 : */
5103 20052 : CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
5104 20052 : s->parent->subTransactionId);
5105 :
5106 20052 : ShowTransactionState("StartSubTransaction");
5107 20052 : }
5108 :
5109 : /*
5110 : * CommitSubTransaction
5111 : *
5112 : * The caller has to make sure to always reassign CurrentTransactionState
5113 : * if it has a local pointer to it after calling this function.
5114 : */
5115 : static void
5116 10686 : CommitSubTransaction(void)
5117 : {
5118 10686 : TransactionState s = CurrentTransactionState;
5119 :
5120 10686 : ShowTransactionState("CommitSubTransaction");
5121 :
5122 10686 : if (s->state != TRANS_INPROGRESS)
5123 0 : elog(WARNING, "CommitSubTransaction while in %s state",
5124 : TransStateAsString(s->state));
5125 :
5126 : /* Pre-commit processing goes here */
5127 :
5128 10686 : CallSubXactCallbacks(SUBXACT_EVENT_PRE_COMMIT_SUB, s->subTransactionId,
5129 10686 : s->parent->subTransactionId);
5130 :
5131 : /*
5132 : * If this subxact has started any unfinished parallel operation, clean up
5133 : * its workers and exit parallel mode. Warn about leaked resources.
5134 : */
5135 10686 : AtEOSubXact_Parallel(true, s->subTransactionId);
5136 10686 : if (s->parallelModeLevel != 0)
5137 : {
5138 0 : elog(WARNING, "parallelModeLevel is %d not 0 at end of subtransaction",
5139 : s->parallelModeLevel);
5140 0 : s->parallelModeLevel = 0;
5141 : }
5142 :
5143 : /* Do the actual "commit", such as it is */
5144 10686 : s->state = TRANS_COMMIT;
5145 :
5146 : /* Must CCI to ensure commands of subtransaction are seen as done */
5147 10686 : CommandCounterIncrement();
5148 :
5149 : /*
5150 : * Prior to 8.4 we marked subcommit in clog at this point. We now only
5151 : * perform that step, if required, as part of the atomic update of the
5152 : * whole transaction tree at top level commit or abort.
5153 : */
5154 :
5155 : /* Post-commit cleanup */
5156 10686 : if (FullTransactionIdIsValid(s->fullTransactionId))
5157 7366 : AtSubCommit_childXids();
5158 10686 : AfterTriggerEndSubXact(true);
5159 10686 : AtSubCommit_Portals(s->subTransactionId,
5160 10686 : s->parent->subTransactionId,
5161 10686 : s->parent->nestingLevel,
5162 10686 : s->parent->curTransactionOwner);
5163 10686 : AtEOSubXact_LargeObject(true, s->subTransactionId,
5164 10686 : s->parent->subTransactionId);
5165 10686 : AtSubCommit_Notify();
5166 :
5167 10686 : CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
5168 10686 : s->parent->subTransactionId);
5169 :
5170 10686 : ResourceOwnerRelease(s->curTransactionOwner,
5171 : RESOURCE_RELEASE_BEFORE_LOCKS,
5172 : true, false);
5173 10686 : AtEOSubXact_RelationCache(true, s->subTransactionId,
5174 10686 : s->parent->subTransactionId);
5175 10686 : AtEOSubXact_TypeCache();
5176 10686 : AtEOSubXact_Inval(true);
5177 10686 : AtSubCommit_smgr();
5178 :
5179 : /*
5180 : * The only lock we actually release here is the subtransaction XID lock.
5181 : */
5182 10686 : CurrentResourceOwner = s->curTransactionOwner;
5183 10686 : if (FullTransactionIdIsValid(s->fullTransactionId))
5184 7366 : XactLockTableDelete(XidFromFullTransactionId(s->fullTransactionId));
5185 :
5186 : /*
5187 : * Other locks should get transferred to their parent resource owner.
5188 : */
5189 10686 : ResourceOwnerRelease(s->curTransactionOwner,
5190 : RESOURCE_RELEASE_LOCKS,
5191 : true, false);
5192 10686 : ResourceOwnerRelease(s->curTransactionOwner,
5193 : RESOURCE_RELEASE_AFTER_LOCKS,
5194 : true, false);
5195 :
5196 10686 : AtEOXact_GUC(true, s->gucNestLevel);
5197 10686 : AtEOSubXact_SPI(true, s->subTransactionId);
5198 10686 : AtEOSubXact_on_commit_actions(true, s->subTransactionId,
5199 10686 : s->parent->subTransactionId);
5200 10686 : AtEOSubXact_Namespace(true, s->subTransactionId,
5201 10686 : s->parent->subTransactionId);
5202 10686 : AtEOSubXact_Files(true, s->subTransactionId,
5203 10686 : s->parent->subTransactionId);
5204 10686 : AtEOSubXact_HashTables(true, s->nestingLevel);
5205 10686 : AtEOSubXact_PgStat(true, s->nestingLevel);
5206 10686 : AtSubCommit_Snapshot(s->nestingLevel);
5207 :
5208 : /*
5209 : * We need to restore the upper transaction's read-only state, in case the
5210 : * upper is read-write while the child is read-only; GUC will incorrectly
5211 : * think it should leave the child state in place.
5212 : */
5213 10686 : XactReadOnly = s->prevXactReadOnly;
5214 :
5215 10686 : CurrentResourceOwner = s->parent->curTransactionOwner;
5216 10686 : CurTransactionResourceOwner = s->parent->curTransactionOwner;
5217 10686 : ResourceOwnerDelete(s->curTransactionOwner);
5218 10686 : s->curTransactionOwner = NULL;
5219 :
5220 10686 : AtSubCommit_Memory();
5221 :
5222 10686 : s->state = TRANS_DEFAULT;
5223 :
5224 10686 : PopTransaction();
5225 10686 : }
5226 :
5227 : /*
5228 : * AbortSubTransaction
5229 : */
5230 : static void
5231 9366 : AbortSubTransaction(void)
5232 : {
5233 9366 : TransactionState s = CurrentTransactionState;
5234 :
5235 : /* Prevent cancel/die interrupt while cleaning up */
5236 9366 : HOLD_INTERRUPTS();
5237 :
5238 : /* Make sure we have a valid memory context and resource owner */
5239 9366 : AtSubAbort_Memory();
5240 9366 : AtSubAbort_ResourceOwner();
5241 :
5242 : /*
5243 : * Release any LW locks we might be holding as quickly as possible.
5244 : * (Regular locks, however, must be held till we finish aborting.)
5245 : * Releasing LW locks is critical since we might try to grab them again
5246 : * while cleaning up!
5247 : *
5248 : * FIXME This may be incorrect --- Are there some locks we should keep?
5249 : * Buffer locks, for example? I don't think so but I'm not sure.
5250 : */
5251 9366 : LWLockReleaseAll();
5252 :
5253 9366 : pgstat_report_wait_end();
5254 9366 : pgstat_progress_end_command();
5255 :
5256 9366 : pgaio_error_cleanup();
5257 :
5258 9366 : UnlockBuffers();
5259 :
5260 : /* Reset WAL record construction state */
5261 9366 : XLogResetInsertion();
5262 :
5263 : /* Cancel condition variable sleep */
5264 9366 : ConditionVariableCancelSleep();
5265 :
5266 : /*
5267 : * Also clean up any open wait for lock, since the lock manager will choke
5268 : * if we try to wait for another lock before doing this.
5269 : */
5270 9366 : LockErrorCleanup();
5271 :
5272 : /*
5273 : * If any timeout events are still active, make sure the timeout interrupt
5274 : * is scheduled. This covers possible loss of a timeout interrupt due to
5275 : * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
5276 : * We delay this till after LockErrorCleanup so that we don't uselessly
5277 : * reschedule lock or deadlock check timeouts.
5278 : */
5279 9366 : reschedule_timeouts();
5280 :
5281 : /*
5282 : * Re-enable signals, in case we got here by longjmp'ing out of a signal
5283 : * handler. We do this fairly early in the sequence so that the timeout
5284 : * infrastructure will be functional if needed while aborting.
5285 : */
5286 9366 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
5287 :
5288 : /*
5289 : * check the current transaction state
5290 : */
5291 9366 : ShowTransactionState("AbortSubTransaction");
5292 :
5293 9366 : if (s->state != TRANS_INPROGRESS)
5294 0 : elog(WARNING, "AbortSubTransaction while in %s state",
5295 : TransStateAsString(s->state));
5296 :
5297 9366 : s->state = TRANS_ABORT;
5298 :
5299 : /*
5300 : * Reset user ID which might have been changed transiently. (See notes in
5301 : * AbortTransaction.)
5302 : */
5303 9366 : SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
5304 :
5305 : /* Forget about any active REINDEX. */
5306 9366 : ResetReindexState(s->nestingLevel);
5307 :
5308 : /* Reset logical streaming state. */
5309 9366 : ResetLogicalStreamingState();
5310 :
5311 : /*
5312 : * No need for SnapBuildResetExportedSnapshotState() here, snapshot
5313 : * exports are not supported in subtransactions.
5314 : */
5315 :
5316 : /*
5317 : * If this subxact has started any unfinished parallel operation, clean up
5318 : * its workers and exit parallel mode. Don't warn about leaked resources.
5319 : */
5320 9366 : AtEOSubXact_Parallel(false, s->subTransactionId);
5321 9366 : s->parallelModeLevel = 0;
5322 :
5323 : /*
5324 : * We can skip all this stuff if the subxact failed before creating a
5325 : * ResourceOwner...
5326 : */
5327 9366 : if (s->curTransactionOwner)
5328 : {
5329 9366 : AfterTriggerEndSubXact(false);
5330 9366 : AtSubAbort_Portals(s->subTransactionId,
5331 9366 : s->parent->subTransactionId,
5332 : s->curTransactionOwner,
5333 9366 : s->parent->curTransactionOwner);
5334 9366 : AtEOSubXact_LargeObject(false, s->subTransactionId,
5335 9366 : s->parent->subTransactionId);
5336 9366 : AtSubAbort_Notify();
5337 :
5338 : /* Advertise the fact that we aborted in pg_xact. */
5339 9366 : (void) RecordTransactionAbort(true);
5340 :
5341 : /* Post-abort cleanup */
5342 9366 : if (FullTransactionIdIsValid(s->fullTransactionId))
5343 1298 : AtSubAbort_childXids();
5344 :
5345 9366 : CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
5346 9366 : s->parent->subTransactionId);
5347 :
5348 9366 : ResourceOwnerRelease(s->curTransactionOwner,
5349 : RESOURCE_RELEASE_BEFORE_LOCKS,
5350 : false, false);
5351 :
5352 9366 : AtEOXact_Aio(false);
5353 9366 : AtEOSubXact_RelationCache(false, s->subTransactionId,
5354 9366 : s->parent->subTransactionId);
5355 9366 : AtEOSubXact_TypeCache();
5356 9366 : AtEOSubXact_Inval(false);
5357 9366 : ResourceOwnerRelease(s->curTransactionOwner,
5358 : RESOURCE_RELEASE_LOCKS,
5359 : false, false);
5360 9366 : ResourceOwnerRelease(s->curTransactionOwner,
5361 : RESOURCE_RELEASE_AFTER_LOCKS,
5362 : false, false);
5363 9366 : AtSubAbort_smgr();
5364 :
5365 9366 : AtEOXact_GUC(false, s->gucNestLevel);
5366 9366 : AtEOSubXact_SPI(false, s->subTransactionId);
5367 9366 : AtEOSubXact_on_commit_actions(false, s->subTransactionId,
5368 9366 : s->parent->subTransactionId);
5369 9366 : AtEOSubXact_Namespace(false, s->subTransactionId,
5370 9366 : s->parent->subTransactionId);
5371 9366 : AtEOSubXact_Files(false, s->subTransactionId,
5372 9366 : s->parent->subTransactionId);
5373 9366 : AtEOSubXact_HashTables(false, s->nestingLevel);
5374 9366 : AtEOSubXact_PgStat(false, s->nestingLevel);
5375 9366 : AtSubAbort_Snapshot(s->nestingLevel);
5376 : }
5377 :
5378 : /*
5379 : * Restore the upper transaction's read-only state, too. This should be
5380 : * redundant with GUC's cleanup but we may as well do it for consistency
5381 : * with the commit case.
5382 : */
5383 9366 : XactReadOnly = s->prevXactReadOnly;
5384 :
5385 9366 : RESUME_INTERRUPTS();
5386 9366 : }
5387 :
5388 : /*
5389 : * CleanupSubTransaction
5390 : *
5391 : * The caller has to make sure to always reassign CurrentTransactionState
5392 : * if it has a local pointer to it after calling this function.
5393 : */
5394 : static void
5395 9366 : CleanupSubTransaction(void)
5396 : {
5397 9366 : TransactionState s = CurrentTransactionState;
5398 :
5399 9366 : ShowTransactionState("CleanupSubTransaction");
5400 :
5401 9366 : if (s->state != TRANS_ABORT)
5402 0 : elog(WARNING, "CleanupSubTransaction while in %s state",
5403 : TransStateAsString(s->state));
5404 :
5405 9366 : AtSubCleanup_Portals(s->subTransactionId);
5406 :
5407 9366 : CurrentResourceOwner = s->parent->curTransactionOwner;
5408 9366 : CurTransactionResourceOwner = s->parent->curTransactionOwner;
5409 9366 : if (s->curTransactionOwner)
5410 9366 : ResourceOwnerDelete(s->curTransactionOwner);
5411 9366 : s->curTransactionOwner = NULL;
5412 :
5413 9366 : AtSubCleanup_Memory();
5414 :
5415 9366 : s->state = TRANS_DEFAULT;
5416 :
5417 9366 : PopTransaction();
5418 9366 : }
5419 :
5420 : /*
5421 : * PushTransaction
5422 : * Create transaction state stack entry for a subtransaction
5423 : *
5424 : * The caller has to make sure to always reassign CurrentTransactionState
5425 : * if it has a local pointer to it after calling this function.
5426 : */
5427 : static void
5428 20052 : PushTransaction(void)
5429 : {
5430 20052 : TransactionState p = CurrentTransactionState;
5431 : TransactionState s;
5432 :
5433 : /*
5434 : * We keep subtransaction state nodes in TopTransactionContext.
5435 : */
5436 : s = (TransactionState)
5437 20052 : MemoryContextAllocZero(TopTransactionContext,
5438 : sizeof(TransactionStateData));
5439 :
5440 : /*
5441 : * Assign a subtransaction ID, watching out for counter wraparound.
5442 : */
5443 20052 : currentSubTransactionId += 1;
5444 20052 : if (currentSubTransactionId == InvalidSubTransactionId)
5445 : {
5446 0 : currentSubTransactionId -= 1;
5447 0 : pfree(s);
5448 0 : ereport(ERROR,
5449 : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5450 : errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
5451 : }
5452 :
5453 : /*
5454 : * We can now stack a minimally valid subtransaction without fear of
5455 : * failure.
5456 : */
5457 20052 : s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
5458 20052 : s->subTransactionId = currentSubTransactionId;
5459 20052 : s->parent = p;
5460 20052 : s->nestingLevel = p->nestingLevel + 1;
5461 20052 : s->gucNestLevel = NewGUCNestLevel();
5462 20052 : s->savepointLevel = p->savepointLevel;
5463 20052 : s->state = TRANS_DEFAULT;
5464 20052 : s->blockState = TBLOCK_SUBBEGIN;
5465 20052 : GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
5466 20052 : s->prevXactReadOnly = XactReadOnly;
5467 20052 : s->startedInRecovery = p->startedInRecovery;
5468 20052 : s->parallelModeLevel = 0;
5469 20052 : s->parallelChildXact = (p->parallelModeLevel != 0 || p->parallelChildXact);
5470 20052 : s->topXidLogged = false;
5471 :
5472 20052 : CurrentTransactionState = s;
5473 :
5474 : /*
5475 : * AbortSubTransaction and CleanupSubTransaction have to be able to cope
5476 : * with the subtransaction from here on out; in particular they should not
5477 : * assume that it necessarily has a transaction context, resource owner,
5478 : * or XID.
5479 : */
5480 20052 : }
5481 :
5482 : /*
5483 : * PopTransaction
5484 : * Pop back to parent transaction state
5485 : *
5486 : * The caller has to make sure to always reassign CurrentTransactionState
5487 : * if it has a local pointer to it after calling this function.
5488 : */
5489 : static void
5490 20052 : PopTransaction(void)
5491 : {
5492 20052 : TransactionState s = CurrentTransactionState;
5493 :
5494 20052 : if (s->state != TRANS_DEFAULT)
5495 0 : elog(WARNING, "PopTransaction while in %s state",
5496 : TransStateAsString(s->state));
5497 :
5498 20052 : if (s->parent == NULL)
5499 0 : elog(FATAL, "PopTransaction with no parent");
5500 :
5501 20052 : CurrentTransactionState = s->parent;
5502 :
5503 : /* Let's just make sure CurTransactionContext is good */
5504 20052 : CurTransactionContext = s->parent->curTransactionContext;
5505 20052 : MemoryContextSwitchTo(CurTransactionContext);
5506 :
5507 : /* Ditto for ResourceOwner links */
5508 20052 : CurTransactionResourceOwner = s->parent->curTransactionOwner;
5509 20052 : CurrentResourceOwner = s->parent->curTransactionOwner;
5510 :
5511 : /* Free the old child structure */
5512 20052 : if (s->name)
5513 3812 : pfree(s->name);
5514 20052 : pfree(s);
5515 20052 : }
5516 :
5517 : /*
5518 : * EstimateTransactionStateSpace
5519 : * Estimate the amount of space that will be needed by
5520 : * SerializeTransactionState. It would be OK to overestimate slightly,
5521 : * but it's simple for us to work out the precise value, so we do.
5522 : */
5523 : Size
5524 910 : EstimateTransactionStateSpace(void)
5525 : {
5526 : TransactionState s;
5527 910 : Size nxids = 0;
5528 910 : Size size = SerializedTransactionStateHeaderSize;
5529 :
5530 4358 : for (s = CurrentTransactionState; s != NULL; s = s->parent)
5531 : {
5532 3448 : if (FullTransactionIdIsValid(s->fullTransactionId))
5533 1976 : nxids = add_size(nxids, 1);
5534 3448 : nxids = add_size(nxids, s->nChildXids);
5535 : }
5536 :
5537 910 : return add_size(size, mul_size(sizeof(TransactionId), nxids));
5538 : }
5539 :
5540 : /*
5541 : * SerializeTransactionState
5542 : * Write out relevant details of our transaction state that will be
5543 : * needed by a parallel worker.
5544 : *
5545 : * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
5546 : * associated with this transaction. These are serialized into a
5547 : * caller-supplied buffer big enough to hold the number of bytes reported by
5548 : * EstimateTransactionStateSpace(). We emit the XIDs in sorted order for the
5549 : * convenience of the receiving process.
5550 : */
5551 : void
5552 910 : SerializeTransactionState(Size maxsize, char *start_address)
5553 : {
5554 : TransactionState s;
5555 910 : Size nxids = 0;
5556 910 : Size i = 0;
5557 : TransactionId *workspace;
5558 : SerializedTransactionState *result;
5559 :
5560 910 : result = (SerializedTransactionState *) start_address;
5561 :
5562 910 : result->xactIsoLevel = XactIsoLevel;
5563 910 : result->xactDeferrable = XactDeferrable;
5564 910 : result->topFullTransactionId = XactTopFullTransactionId;
5565 910 : result->currentFullTransactionId =
5566 910 : CurrentTransactionState->fullTransactionId;
5567 910 : result->currentCommandId = currentCommandId;
5568 :
5569 : /*
5570 : * If we're running in a parallel worker and launching a parallel worker
5571 : * of our own, we can just pass along the information that was passed to
5572 : * us.
5573 : */
5574 910 : if (nParallelCurrentXids > 0)
5575 : {
5576 0 : result->nParallelCurrentXids = nParallelCurrentXids;
5577 0 : memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5578 : nParallelCurrentXids * sizeof(TransactionId));
5579 0 : return;
5580 : }
5581 :
5582 : /*
5583 : * OK, we need to generate a sorted list of XIDs that our workers should
5584 : * view as current. First, figure out how many there are.
5585 : */
5586 4358 : for (s = CurrentTransactionState; s != NULL; s = s->parent)
5587 : {
5588 3448 : if (FullTransactionIdIsValid(s->fullTransactionId))
5589 1976 : nxids = add_size(nxids, 1);
5590 3448 : nxids = add_size(nxids, s->nChildXids);
5591 : }
5592 : Assert(SerializedTransactionStateHeaderSize + nxids * sizeof(TransactionId)
5593 : <= maxsize);
5594 :
5595 : /* Copy them to our scratch space. */
5596 910 : workspace = palloc(nxids * sizeof(TransactionId));
5597 4358 : for (s = CurrentTransactionState; s != NULL; s = s->parent)
5598 : {
5599 3448 : if (FullTransactionIdIsValid(s->fullTransactionId))
5600 1976 : workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5601 3448 : if (s->nChildXids > 0)
5602 0 : memcpy(&workspace[i], s->childXids,
5603 0 : s->nChildXids * sizeof(TransactionId));
5604 3448 : i += s->nChildXids;
5605 : }
5606 : Assert(i == nxids);
5607 :
5608 : /* Sort them. */
5609 910 : qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5610 :
5611 : /* Copy data into output area. */
5612 910 : result->nParallelCurrentXids = nxids;
5613 910 : memcpy(&result->parallelCurrentXids[0], workspace,
5614 : nxids * sizeof(TransactionId));
5615 : }
5616 :
5617 : /*
5618 : * StartParallelWorkerTransaction
5619 : * Start a parallel worker transaction, restoring the relevant
5620 : * transaction state serialized by SerializeTransactionState.
5621 : */
5622 : void
5623 2746 : StartParallelWorkerTransaction(char *tstatespace)
5624 : {
5625 : SerializedTransactionState *tstate;
5626 :
5627 : Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
5628 2746 : StartTransaction();
5629 :
5630 2746 : tstate = (SerializedTransactionState *) tstatespace;
5631 2746 : XactIsoLevel = tstate->xactIsoLevel;
5632 2746 : XactDeferrable = tstate->xactDeferrable;
5633 2746 : XactTopFullTransactionId = tstate->topFullTransactionId;
5634 2746 : CurrentTransactionState->fullTransactionId =
5635 : tstate->currentFullTransactionId;
5636 2746 : currentCommandId = tstate->currentCommandId;
5637 2746 : nParallelCurrentXids = tstate->nParallelCurrentXids;
5638 2746 : ParallelCurrentXids = &tstate->parallelCurrentXids[0];
5639 :
5640 2746 : CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
5641 2746 : }
5642 :
5643 : /*
5644 : * EndParallelWorkerTransaction
5645 : * End a parallel worker transaction.
5646 : */
5647 : void
5648 2734 : EndParallelWorkerTransaction(void)
5649 : {
5650 : Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
5651 2734 : CommitTransaction();
5652 2734 : CurrentTransactionState->blockState = TBLOCK_DEFAULT;
5653 2734 : }
5654 :
5655 : /*
5656 : * ShowTransactionState
5657 : * Debug support
5658 : */
5659 : static void
5660 2114608 : ShowTransactionState(const char *str)
5661 : {
5662 : /* skip work if message will definitely not be printed */
5663 2114608 : if (message_level_is_interesting(DEBUG5))
5664 0 : ShowTransactionStateRec(str, CurrentTransactionState);
5665 2114608 : }
5666 :
5667 : /*
5668 : * ShowTransactionStateRec
5669 : * Recursive subroutine for ShowTransactionState
5670 : */
5671 : static void
5672 0 : ShowTransactionStateRec(const char *str, TransactionState s)
5673 : {
5674 : StringInfoData buf;
5675 :
5676 0 : if (s->parent)
5677 : {
5678 : /*
5679 : * Since this function recurses, it could be driven to stack overflow.
5680 : * This is just a debugging aid, so we can leave out some details
5681 : * instead of erroring out with check_stack_depth().
5682 : */
5683 0 : if (stack_is_too_deep())
5684 0 : ereport(DEBUG5,
5685 : (errmsg_internal("%s(%d): parent omitted to avoid stack overflow",
5686 : str, s->nestingLevel)));
5687 : else
5688 0 : ShowTransactionStateRec(str, s->parent);
5689 : }
5690 :
5691 0 : initStringInfo(&buf);
5692 0 : if (s->nChildXids > 0)
5693 : {
5694 : int i;
5695 :
5696 0 : appendStringInfo(&buf, ", children: %u", s->childXids[0]);
5697 0 : for (i = 1; i < s->nChildXids; i++)
5698 0 : appendStringInfo(&buf, " %u", s->childXids[i]);
5699 : }
5700 0 : ereport(DEBUG5,
5701 : (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
5702 : str, s->nestingLevel,
5703 : PointerIsValid(s->name) ? s->name : "unnamed",
5704 : BlockStateAsString(s->blockState),
5705 : TransStateAsString(s->state),
5706 : (unsigned int) XidFromFullTransactionId(s->fullTransactionId),
5707 : (unsigned int) s->subTransactionId,
5708 : (unsigned int) currentCommandId,
5709 : currentCommandIdUsed ? " (used)" : "",
5710 : buf.data)));
5711 0 : pfree(buf.data);
5712 0 : }
5713 :
5714 : /*
5715 : * BlockStateAsString
5716 : * Debug support
5717 : */
5718 : static const char *
5719 0 : BlockStateAsString(TBlockState blockState)
5720 : {
5721 0 : switch (blockState)
5722 : {
5723 0 : case TBLOCK_DEFAULT:
5724 0 : return "DEFAULT";
5725 0 : case TBLOCK_STARTED:
5726 0 : return "STARTED";
5727 0 : case TBLOCK_BEGIN:
5728 0 : return "BEGIN";
5729 0 : case TBLOCK_INPROGRESS:
5730 0 : return "INPROGRESS";
5731 0 : case TBLOCK_IMPLICIT_INPROGRESS:
5732 0 : return "IMPLICIT_INPROGRESS";
5733 0 : case TBLOCK_PARALLEL_INPROGRESS:
5734 0 : return "PARALLEL_INPROGRESS";
5735 0 : case TBLOCK_END:
5736 0 : return "END";
5737 0 : case TBLOCK_ABORT:
5738 0 : return "ABORT";
5739 0 : case TBLOCK_ABORT_END:
5740 0 : return "ABORT_END";
5741 0 : case TBLOCK_ABORT_PENDING:
5742 0 : return "ABORT_PENDING";
5743 0 : case TBLOCK_PREPARE:
5744 0 : return "PREPARE";
5745 0 : case TBLOCK_SUBBEGIN:
5746 0 : return "SUBBEGIN";
5747 0 : case TBLOCK_SUBINPROGRESS:
5748 0 : return "SUBINPROGRESS";
5749 0 : case TBLOCK_SUBRELEASE:
5750 0 : return "SUBRELEASE";
5751 0 : case TBLOCK_SUBCOMMIT:
5752 0 : return "SUBCOMMIT";
5753 0 : case TBLOCK_SUBABORT:
5754 0 : return "SUBABORT";
5755 0 : case TBLOCK_SUBABORT_END:
5756 0 : return "SUBABORT_END";
5757 0 : case TBLOCK_SUBABORT_PENDING:
5758 0 : return "SUBABORT_PENDING";
5759 0 : case TBLOCK_SUBRESTART:
5760 0 : return "SUBRESTART";
5761 0 : case TBLOCK_SUBABORT_RESTART:
5762 0 : return "SUBABORT_RESTART";
5763 : }
5764 0 : return "UNRECOGNIZED";
5765 : }
5766 :
5767 : /*
5768 : * TransStateAsString
5769 : * Debug support
5770 : */
5771 : static const char *
5772 0 : TransStateAsString(TransState state)
5773 : {
5774 0 : switch (state)
5775 : {
5776 0 : case TRANS_DEFAULT:
5777 0 : return "DEFAULT";
5778 0 : case TRANS_START:
5779 0 : return "START";
5780 0 : case TRANS_INPROGRESS:
5781 0 : return "INPROGRESS";
5782 0 : case TRANS_COMMIT:
5783 0 : return "COMMIT";
5784 0 : case TRANS_ABORT:
5785 0 : return "ABORT";
5786 0 : case TRANS_PREPARE:
5787 0 : return "PREPARE";
5788 : }
5789 0 : return "UNRECOGNIZED";
5790 : }
5791 :
5792 : /*
5793 : * xactGetCommittedChildren
5794 : *
5795 : * Gets the list of committed children of the current transaction. The return
5796 : * value is the number of child transactions. *ptr is set to point to an
5797 : * array of TransactionIds. The array is allocated in TopTransactionContext;
5798 : * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
5799 : * If there are no subxacts, *ptr is set to NULL.
5800 : */
5801 : int
5802 1016744 : xactGetCommittedChildren(TransactionId **ptr)
5803 : {
5804 1016744 : TransactionState s = CurrentTransactionState;
5805 :
5806 1016744 : if (s->nChildXids == 0)
5807 1015546 : *ptr = NULL;
5808 : else
5809 1198 : *ptr = s->childXids;
5810 :
5811 1016744 : return s->nChildXids;
5812 : }
5813 :
5814 : /*
5815 : * XLOG support routines
5816 : */
5817 :
5818 :
5819 : /*
5820 : * Log the commit record for a plain or twophase transaction commit.
5821 : *
5822 : * A 2pc commit will be emitted when twophase_xid is valid, a plain one
5823 : * otherwise.
5824 : */
5825 : XLogRecPtr
5826 253842 : XactLogCommitRecord(TimestampTz commit_time,
5827 : int nsubxacts, TransactionId *subxacts,
5828 : int nrels, RelFileLocator *rels,
5829 : int ndroppedstats, xl_xact_stats_item *droppedstats,
5830 : int nmsgs, SharedInvalidationMessage *msgs,
5831 : bool relcacheInval,
5832 : int xactflags, TransactionId twophase_xid,
5833 : const char *twophase_gid)
5834 : {
5835 : xl_xact_commit xlrec;
5836 : xl_xact_xinfo xl_xinfo;
5837 : xl_xact_dbinfo xl_dbinfo;
5838 : xl_xact_subxacts xl_subxacts;
5839 : xl_xact_relfilelocators xl_relfilelocators;
5840 : xl_xact_stats_items xl_dropped_stats;
5841 : xl_xact_invals xl_invals;
5842 : xl_xact_twophase xl_twophase;
5843 : xl_xact_origin xl_origin;
5844 : uint8 info;
5845 :
5846 : Assert(CritSectionCount > 0);
5847 :
5848 253842 : xl_xinfo.xinfo = 0;
5849 :
5850 : /* decide between a plain and 2pc commit */
5851 253842 : if (!TransactionIdIsValid(twophase_xid))
5852 253340 : info = XLOG_XACT_COMMIT;
5853 : else
5854 502 : info = XLOG_XACT_COMMIT_PREPARED;
5855 :
5856 : /* First figure out and collect all the information needed */
5857 :
5858 253842 : xlrec.xact_time = commit_time;
5859 :
5860 253842 : if (relcacheInval)
5861 7654 : xl_xinfo.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
5862 253842 : if (forceSyncCommit)
5863 990 : xl_xinfo.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
5864 253842 : if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5865 75844 : xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5866 :
5867 : /*
5868 : * Check if the caller would like to ask standbys for immediate feedback
5869 : * once this commit is applied.
5870 : */
5871 253842 : if (synchronous_commit >= SYNCHRONOUS_COMMIT_REMOTE_APPLY)
5872 4 : xl_xinfo.xinfo |= XACT_COMPLETION_APPLY_FEEDBACK;
5873 :
5874 : /*
5875 : * Relcache invalidations requires information about the current database
5876 : * and so does logical decoding.
5877 : */
5878 253842 : if (nmsgs > 0 || XLogLogicalInfoActive())
5879 : {
5880 151070 : xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5881 151070 : xl_dbinfo.dbId = MyDatabaseId;
5882 151070 : xl_dbinfo.tsId = MyDatabaseTableSpace;
5883 : }
5884 :
5885 253842 : if (nsubxacts > 0)
5886 : {
5887 1008 : xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5888 1008 : xl_subxacts.nsubxacts = nsubxacts;
5889 : }
5890 :
5891 253842 : if (nrels > 0)
5892 : {
5893 19550 : xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILELOCATORS;
5894 19550 : xl_relfilelocators.nrels = nrels;
5895 19550 : info |= XLR_SPECIAL_REL_UPDATE;
5896 : }
5897 :
5898 253842 : if (ndroppedstats > 0)
5899 : {
5900 21170 : xl_xinfo.xinfo |= XACT_XINFO_HAS_DROPPED_STATS;
5901 21170 : xl_dropped_stats.nitems = ndroppedstats;
5902 : }
5903 :
5904 253842 : if (nmsgs > 0)
5905 : {
5906 149358 : xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5907 149358 : xl_invals.nmsgs = nmsgs;
5908 : }
5909 :
5910 253842 : if (TransactionIdIsValid(twophase_xid))
5911 : {
5912 502 : xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5913 502 : xl_twophase.xid = twophase_xid;
5914 : Assert(twophase_gid != NULL);
5915 :
5916 502 : if (XLogLogicalInfoActive())
5917 76 : xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
5918 : }
5919 :
5920 : /* dump transaction origin information */
5921 253842 : if (replorigin_session_origin != InvalidRepOriginId)
5922 : {
5923 2028 : xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5924 :
5925 2028 : xl_origin.origin_lsn = replorigin_session_origin_lsn;
5926 2028 : xl_origin.origin_timestamp = replorigin_session_origin_timestamp;
5927 : }
5928 :
5929 253842 : if (xl_xinfo.xinfo != 0)
5930 163358 : info |= XLOG_XACT_HAS_INFO;
5931 :
5932 : /* Then include all the collected data into the commit record. */
5933 :
5934 253842 : XLogBeginInsert();
5935 :
5936 253842 : XLogRegisterData(&xlrec, sizeof(xl_xact_commit));
5937 :
5938 253842 : if (xl_xinfo.xinfo != 0)
5939 163358 : XLogRegisterData(&xl_xinfo.xinfo, sizeof(xl_xinfo.xinfo));
5940 :
5941 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5942 151070 : XLogRegisterData(&xl_dbinfo, sizeof(xl_dbinfo));
5943 :
5944 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5945 : {
5946 1008 : XLogRegisterData(&xl_subxacts,
5947 : MinSizeOfXactSubxacts);
5948 1008 : XLogRegisterData(subxacts,
5949 : nsubxacts * sizeof(TransactionId));
5950 : }
5951 :
5952 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILELOCATORS)
5953 : {
5954 19550 : XLogRegisterData(&xl_relfilelocators,
5955 : MinSizeOfXactRelfileLocators);
5956 19550 : XLogRegisterData(rels,
5957 : nrels * sizeof(RelFileLocator));
5958 : }
5959 :
5960 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_DROPPED_STATS)
5961 : {
5962 21170 : XLogRegisterData(&xl_dropped_stats,
5963 : MinSizeOfXactStatsItems);
5964 21170 : XLogRegisterData(droppedstats,
5965 : ndroppedstats * sizeof(xl_xact_stats_item));
5966 : }
5967 :
5968 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5969 : {
5970 149358 : XLogRegisterData(&xl_invals, MinSizeOfXactInvals);
5971 149358 : XLogRegisterData(msgs,
5972 : nmsgs * sizeof(SharedInvalidationMessage));
5973 : }
5974 :
5975 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5976 : {
5977 502 : XLogRegisterData(&xl_twophase, sizeof(xl_xact_twophase));
5978 502 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
5979 76 : XLogRegisterData(twophase_gid, strlen(twophase_gid) + 1);
5980 : }
5981 :
5982 253842 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5983 2028 : XLogRegisterData(&xl_origin, sizeof(xl_xact_origin));
5984 :
5985 : /* we allow filtering by xacts */
5986 253842 : XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
5987 :
5988 253842 : return XLogInsert(RM_XACT_ID, info);
5989 : }
5990 :
5991 : /*
5992 : * Log the commit record for a plain or twophase transaction abort.
5993 : *
5994 : * A 2pc abort will be emitted when twophase_xid is valid, a plain one
5995 : * otherwise.
5996 : */
5997 : XLogRecPtr
5998 11908 : XactLogAbortRecord(TimestampTz abort_time,
5999 : int nsubxacts, TransactionId *subxacts,
6000 : int nrels, RelFileLocator *rels,
6001 : int ndroppedstats, xl_xact_stats_item *droppedstats,
6002 : int xactflags, TransactionId twophase_xid,
6003 : const char *twophase_gid)
6004 : {
6005 : xl_xact_abort xlrec;
6006 : xl_xact_xinfo xl_xinfo;
6007 : xl_xact_subxacts xl_subxacts;
6008 : xl_xact_relfilelocators xl_relfilelocators;
6009 : xl_xact_stats_items xl_dropped_stats;
6010 : xl_xact_twophase xl_twophase;
6011 : xl_xact_dbinfo xl_dbinfo;
6012 : xl_xact_origin xl_origin;
6013 :
6014 : uint8 info;
6015 :
6016 : Assert(CritSectionCount > 0);
6017 :
6018 11908 : xl_xinfo.xinfo = 0;
6019 :
6020 : /* decide between a plain and 2pc abort */
6021 11908 : if (!TransactionIdIsValid(twophase_xid))
6022 11832 : info = XLOG_XACT_ABORT;
6023 : else
6024 76 : info = XLOG_XACT_ABORT_PREPARED;
6025 :
6026 :
6027 : /* First figure out and collect all the information needed */
6028 :
6029 11908 : xlrec.xact_time = abort_time;
6030 :
6031 11908 : if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
6032 4878 : xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
6033 :
6034 11908 : if (nsubxacts > 0)
6035 : {
6036 198 : xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
6037 198 : xl_subxacts.nsubxacts = nsubxacts;
6038 : }
6039 :
6040 11908 : if (nrels > 0)
6041 : {
6042 2006 : xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILELOCATORS;
6043 2006 : xl_relfilelocators.nrels = nrels;
6044 2006 : info |= XLR_SPECIAL_REL_UPDATE;
6045 : }
6046 :
6047 11908 : if (ndroppedstats > 0)
6048 : {
6049 2740 : xl_xinfo.xinfo |= XACT_XINFO_HAS_DROPPED_STATS;
6050 2740 : xl_dropped_stats.nitems = ndroppedstats;
6051 : }
6052 :
6053 11908 : if (TransactionIdIsValid(twophase_xid))
6054 : {
6055 76 : xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
6056 76 : xl_twophase.xid = twophase_xid;
6057 : Assert(twophase_gid != NULL);
6058 :
6059 76 : if (XLogLogicalInfoActive())
6060 26 : xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
6061 : }
6062 :
6063 11908 : if (TransactionIdIsValid(twophase_xid) && XLogLogicalInfoActive())
6064 : {
6065 26 : xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
6066 26 : xl_dbinfo.dbId = MyDatabaseId;
6067 26 : xl_dbinfo.tsId = MyDatabaseTableSpace;
6068 : }
6069 :
6070 : /*
6071 : * Dump transaction origin information. We need this during recovery to
6072 : * update the replication origin progress.
6073 : */
6074 11908 : if (replorigin_session_origin != InvalidRepOriginId)
6075 : {
6076 66 : xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
6077 :
6078 66 : xl_origin.origin_lsn = replorigin_session_origin_lsn;
6079 66 : xl_origin.origin_timestamp = replorigin_session_origin_timestamp;
6080 : }
6081 :
6082 11908 : if (xl_xinfo.xinfo != 0)
6083 6124 : info |= XLOG_XACT_HAS_INFO;
6084 :
6085 : /* Then include all the collected data into the abort record. */
6086 :
6087 11908 : XLogBeginInsert();
6088 :
6089 11908 : XLogRegisterData(&xlrec, MinSizeOfXactAbort);
6090 :
6091 11908 : if (xl_xinfo.xinfo != 0)
6092 6124 : XLogRegisterData(&xl_xinfo, sizeof(xl_xinfo));
6093 :
6094 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
6095 26 : XLogRegisterData(&xl_dbinfo, sizeof(xl_dbinfo));
6096 :
6097 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
6098 : {
6099 198 : XLogRegisterData(&xl_subxacts,
6100 : MinSizeOfXactSubxacts);
6101 198 : XLogRegisterData(subxacts,
6102 : nsubxacts * sizeof(TransactionId));
6103 : }
6104 :
6105 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILELOCATORS)
6106 : {
6107 2006 : XLogRegisterData(&xl_relfilelocators,
6108 : MinSizeOfXactRelfileLocators);
6109 2006 : XLogRegisterData(rels,
6110 : nrels * sizeof(RelFileLocator));
6111 : }
6112 :
6113 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_DROPPED_STATS)
6114 : {
6115 2740 : XLogRegisterData(&xl_dropped_stats,
6116 : MinSizeOfXactStatsItems);
6117 2740 : XLogRegisterData(droppedstats,
6118 : ndroppedstats * sizeof(xl_xact_stats_item));
6119 : }
6120 :
6121 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
6122 : {
6123 76 : XLogRegisterData(&xl_twophase, sizeof(xl_xact_twophase));
6124 76 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
6125 26 : XLogRegisterData(twophase_gid, strlen(twophase_gid) + 1);
6126 : }
6127 :
6128 11908 : if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
6129 66 : XLogRegisterData(&xl_origin, sizeof(xl_xact_origin));
6130 :
6131 : /* Include the replication origin */
6132 11908 : XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
6133 :
6134 11908 : return XLogInsert(RM_XACT_ID, info);
6135 : }
6136 :
6137 : /*
6138 : * Before 9.0 this was a fairly short function, but now it performs many
6139 : * actions for which the order of execution is critical.
6140 : */
6141 : static void
6142 43318 : xact_redo_commit(xl_xact_parsed_commit *parsed,
6143 : TransactionId xid,
6144 : XLogRecPtr lsn,
6145 : RepOriginId origin_id)
6146 : {
6147 : TransactionId max_xid;
6148 : TimestampTz commit_time;
6149 :
6150 : Assert(TransactionIdIsValid(xid));
6151 :
6152 43318 : max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
6153 :
6154 : /* Make sure nextXid is beyond any XID mentioned in the record. */
6155 43318 : AdvanceNextFullTransactionIdPastXid(max_xid);
6156 :
6157 : Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
6158 : (origin_id == InvalidRepOriginId));
6159 :
6160 43318 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
6161 40 : commit_time = parsed->origin_timestamp;
6162 : else
6163 43278 : commit_time = parsed->xact_time;
6164 :
6165 : /* Set the transaction commit timestamp and metadata */
6166 43318 : TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
6167 : commit_time, origin_id);
6168 :
6169 43318 : if (standbyState == STANDBY_DISABLED)
6170 : {
6171 : /*
6172 : * Mark the transaction committed in pg_xact.
6173 : */
6174 4452 : TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
6175 : }
6176 : else
6177 : {
6178 : /*
6179 : * If a transaction completion record arrives that has as-yet
6180 : * unobserved subtransactions then this will not have been fully
6181 : * handled by the call to RecordKnownAssignedTransactionIds() in the
6182 : * main recovery loop in xlog.c. So we need to do bookkeeping again to
6183 : * cover that case. This is confusing and it is easy to think this
6184 : * call is irrelevant, which has happened three times in development
6185 : * already. Leave it in.
6186 : */
6187 38866 : RecordKnownAssignedTransactionIds(max_xid);
6188 :
6189 : /*
6190 : * Mark the transaction committed in pg_xact. We use async commit
6191 : * protocol during recovery to provide information on database
6192 : * consistency for when users try to set hint bits. It is important
6193 : * that we do not set hint bits until the minRecoveryPoint is past
6194 : * this commit record. This ensures that if we crash we don't see hint
6195 : * bits set on changes made by transactions that haven't yet
6196 : * recovered. It's unlikely but it's good to be safe.
6197 : */
6198 38866 : TransactionIdAsyncCommitTree(xid, parsed->nsubxacts, parsed->subxacts, lsn);
6199 :
6200 : /*
6201 : * We must mark clog before we update the ProcArray.
6202 : */
6203 38866 : ExpireTreeKnownAssignedTransactionIds(xid, parsed->nsubxacts, parsed->subxacts, max_xid);
6204 :
6205 : /*
6206 : * Send any cache invalidations attached to the commit. We must
6207 : * maintain the same order of invalidation then release locks as
6208 : * occurs in CommitTransaction().
6209 : */
6210 38866 : ProcessCommittedInvalidationMessages(parsed->msgs, parsed->nmsgs,
6211 38866 : XactCompletionRelcacheInitFileInval(parsed->xinfo),
6212 : parsed->dbId, parsed->tsId);
6213 :
6214 : /*
6215 : * Release locks, if any. We do this for both two phase and normal one
6216 : * phase transactions. In effect we are ignoring the prepare phase and
6217 : * just going straight to lock release.
6218 : */
6219 38866 : if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
6220 18882 : StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
6221 : }
6222 :
6223 43318 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
6224 : {
6225 : /* recover apply progress */
6226 40 : replorigin_advance(origin_id, parsed->origin_lsn, lsn,
6227 : false /* backward */ , false /* WAL */ );
6228 : }
6229 :
6230 : /* Make sure files supposed to be dropped are dropped */
6231 43318 : if (parsed->nrels > 0)
6232 : {
6233 : /*
6234 : * First update minimum recovery point to cover this WAL record. Once
6235 : * a relation is deleted, there's no going back. The buffer manager
6236 : * enforces the WAL-first rule for normal updates to relation files,
6237 : * so that the minimum recovery point is always updated before the
6238 : * corresponding change in the data file is flushed to disk, but we
6239 : * have to do the same here since we're bypassing the buffer manager.
6240 : *
6241 : * Doing this before deleting the files means that if a deletion fails
6242 : * for some reason, you cannot start up the system even after restart,
6243 : * until you fix the underlying situation so that the deletion will
6244 : * succeed. Alternatively, we could update the minimum recovery point
6245 : * after deletion, but that would leave a small window where the
6246 : * WAL-first rule would be violated.
6247 : */
6248 4004 : XLogFlush(lsn);
6249 :
6250 : /* Make sure files supposed to be dropped are dropped */
6251 4004 : DropRelationFiles(parsed->xlocators, parsed->nrels, true);
6252 : }
6253 :
6254 43318 : if (parsed->nstats > 0)
6255 : {
6256 : /* see equivalent call for relations above */
6257 5218 : XLogFlush(lsn);
6258 :
6259 5218 : pgstat_execute_transactional_drops(parsed->nstats, parsed->stats, true);
6260 : }
6261 :
6262 : /*
6263 : * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
6264 : * in normal operation. For example, in CREATE DATABASE, we copy all files
6265 : * from the template database, and then commit the transaction. If we
6266 : * crash after all the files have been copied but before the commit, you
6267 : * have files in the data directory without an entry in pg_database. To
6268 : * minimize the window for that, we use ForceSyncCommit() to rush the
6269 : * commit record to disk as quick as possible. We have the same window
6270 : * during recovery, and forcing an XLogFlush() (which updates
6271 : * minRecoveryPoint during recovery) helps to reduce that problem window,
6272 : * for any user that requested ForceSyncCommit().
6273 : */
6274 43318 : if (XactCompletionForceSyncCommit(parsed->xinfo))
6275 96 : XLogFlush(lsn);
6276 :
6277 : /*
6278 : * If asked by the primary (because someone is waiting for a synchronous
6279 : * commit = remote_apply), we will need to ask walreceiver to send a reply
6280 : * immediately.
6281 : */
6282 43318 : if (XactCompletionApplyFeedback(parsed->xinfo))
6283 4 : XLogRequestWalReceiverReply();
6284 43318 : }
6285 :
6286 : /*
6287 : * Be careful with the order of execution, as with xact_redo_commit().
6288 : * The two functions are similar but differ in key places.
6289 : *
6290 : * Note also that an abort can be for a subtransaction and its children,
6291 : * not just for a top level abort. That means we have to consider
6292 : * topxid != xid, whereas in commit we would find topxid == xid always
6293 : * because subtransaction commit is never WAL logged.
6294 : */
6295 : static void
6296 3594 : xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid,
6297 : XLogRecPtr lsn, RepOriginId origin_id)
6298 : {
6299 : TransactionId max_xid;
6300 :
6301 : Assert(TransactionIdIsValid(xid));
6302 :
6303 : /* Make sure nextXid is beyond any XID mentioned in the record. */
6304 3594 : max_xid = TransactionIdLatest(xid,
6305 : parsed->nsubxacts,
6306 3594 : parsed->subxacts);
6307 3594 : AdvanceNextFullTransactionIdPastXid(max_xid);
6308 :
6309 3594 : if (standbyState == STANDBY_DISABLED)
6310 : {
6311 : /* Mark the transaction aborted in pg_xact, no need for async stuff */
6312 40 : TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
6313 : }
6314 : else
6315 : {
6316 : /*
6317 : * If a transaction completion record arrives that has as-yet
6318 : * unobserved subtransactions then this will not have been fully
6319 : * handled by the call to RecordKnownAssignedTransactionIds() in the
6320 : * main recovery loop in xlog.c. So we need to do bookkeeping again to
6321 : * cover that case. This is confusing and it is easy to think this
6322 : * call is irrelevant, which has happened three times in development
6323 : * already. Leave it in.
6324 : */
6325 3554 : RecordKnownAssignedTransactionIds(max_xid);
6326 :
6327 : /* Mark the transaction aborted in pg_xact, no need for async stuff */
6328 3554 : TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
6329 :
6330 : /*
6331 : * We must update the ProcArray after we have marked clog.
6332 : */
6333 3554 : ExpireTreeKnownAssignedTransactionIds(xid, parsed->nsubxacts, parsed->subxacts, max_xid);
6334 :
6335 : /*
6336 : * There are no invalidation messages to send or undo.
6337 : */
6338 :
6339 : /*
6340 : * Release locks, if any. There are no invalidations to send.
6341 : */
6342 3554 : if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
6343 2228 : StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
6344 : }
6345 :
6346 3594 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
6347 : {
6348 : /* recover apply progress */
6349 10 : replorigin_advance(origin_id, parsed->origin_lsn, lsn,
6350 : false /* backward */ , false /* WAL */ );
6351 : }
6352 :
6353 : /* Make sure files supposed to be dropped are dropped */
6354 3594 : if (parsed->nrels > 0)
6355 : {
6356 : /*
6357 : * See comments about update of minimum recovery point on truncation,
6358 : * in xact_redo_commit().
6359 : */
6360 632 : XLogFlush(lsn);
6361 :
6362 632 : DropRelationFiles(parsed->xlocators, parsed->nrels, true);
6363 : }
6364 :
6365 3594 : if (parsed->nstats > 0)
6366 : {
6367 : /* see equivalent call for relations above */
6368 836 : XLogFlush(lsn);
6369 :
6370 836 : pgstat_execute_transactional_drops(parsed->nstats, parsed->stats, true);
6371 : }
6372 3594 : }
6373 :
6374 : void
6375 47590 : xact_redo(XLogReaderState *record)
6376 : {
6377 47590 : uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
6378 :
6379 : /* Backup blocks are not used in xact records */
6380 : Assert(!XLogRecHasAnyBlockRefs(record));
6381 :
6382 47590 : if (info == XLOG_XACT_COMMIT)
6383 : {
6384 43230 : xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
6385 : xl_xact_parsed_commit parsed;
6386 :
6387 43230 : ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
6388 43230 : xact_redo_commit(&parsed, XLogRecGetXid(record),
6389 43230 : record->EndRecPtr, XLogRecGetOrigin(record));
6390 : }
6391 4360 : else if (info == XLOG_XACT_COMMIT_PREPARED)
6392 : {
6393 88 : xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
6394 : xl_xact_parsed_commit parsed;
6395 :
6396 88 : ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
6397 88 : xact_redo_commit(&parsed, parsed.twophase_xid,
6398 88 : record->EndRecPtr, XLogRecGetOrigin(record));
6399 :
6400 : /* Delete TwoPhaseState gxact entry and/or 2PC file. */
6401 88 : LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
6402 88 : PrepareRedoRemove(parsed.twophase_xid, false);
6403 88 : LWLockRelease(TwoPhaseStateLock);
6404 : }
6405 4272 : else if (info == XLOG_XACT_ABORT)
6406 : {
6407 3552 : xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
6408 : xl_xact_parsed_abort parsed;
6409 :
6410 3552 : ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
6411 3552 : xact_redo_abort(&parsed, XLogRecGetXid(record),
6412 3552 : record->EndRecPtr, XLogRecGetOrigin(record));
6413 : }
6414 720 : else if (info == XLOG_XACT_ABORT_PREPARED)
6415 : {
6416 42 : xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
6417 : xl_xact_parsed_abort parsed;
6418 :
6419 42 : ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
6420 42 : xact_redo_abort(&parsed, parsed.twophase_xid,
6421 42 : record->EndRecPtr, XLogRecGetOrigin(record));
6422 :
6423 : /* Delete TwoPhaseState gxact entry and/or 2PC file. */
6424 42 : LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
6425 42 : PrepareRedoRemove(parsed.twophase_xid, false);
6426 42 : LWLockRelease(TwoPhaseStateLock);
6427 : }
6428 678 : else if (info == XLOG_XACT_PREPARE)
6429 : {
6430 : /*
6431 : * Store xid and start/end pointers of the WAL record in TwoPhaseState
6432 : * gxact entry.
6433 : */
6434 152 : LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
6435 152 : PrepareRedoAdd(InvalidFullTransactionId,
6436 152 : XLogRecGetData(record),
6437 : record->ReadRecPtr,
6438 : record->EndRecPtr,
6439 152 : XLogRecGetOrigin(record));
6440 152 : LWLockRelease(TwoPhaseStateLock);
6441 : }
6442 526 : else if (info == XLOG_XACT_ASSIGNMENT)
6443 : {
6444 42 : xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
6445 :
6446 42 : if (standbyState >= STANDBY_INITIALIZED)
6447 42 : ProcArrayApplyXidAssignment(xlrec->xtop,
6448 42 : xlrec->nsubxacts, xlrec->xsub);
6449 : }
6450 484 : else if (info == XLOG_XACT_INVALIDATIONS)
6451 : {
6452 : /*
6453 : * XXX we do ignore this for now, what matters are invalidations
6454 : * written into the commit record.
6455 : */
6456 : }
6457 : else
6458 0 : elog(PANIC, "xact_redo: unknown op code %u", info);
6459 47590 : }
|