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