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