PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xact.c File Reference
#include "postgres.h"
#include <time.h>
#include <unistd.h>
#include "access/commit_ts.h"
#include "access/multixact.h"
#include "access/parallel.h"
#include "access/subtrans.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/storage.h"
#include "commands/async.h"
#include "commands/tablecmds.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "libpq/be-fsstubs.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/logicallauncher.h"
#include "replication/origin.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
#include "pg_trace.h"
Include dependency graph for xact.c:

Go to the source code of this file.

Data Structures

struct  TransactionStateData
 
struct  XactCallbackItem
 
struct  SubXactCallbackItem
 

Typedefs

typedef enum TransState TransState
 
typedef enum TBlockState TBlockState
 
typedef struct TransactionStateData TransactionStateData
 
typedef TransactionStateDataTransactionState
 
typedef struct XactCallbackItem XactCallbackItem
 
typedef struct SubXactCallbackItem SubXactCallbackItem
 

Enumerations

enum  TransState {
  TRANS_DEFAULT, TRANS_START, TRANS_INPROGRESS, TRANS_COMMIT,
  TRANS_ABORT, TRANS_PREPARE
}
 
enum  TBlockState {
  TBLOCK_DEFAULT, TBLOCK_STARTED, TBLOCK_BEGIN, TBLOCK_INPROGRESS,
  TBLOCK_PARALLEL_INPROGRESS, TBLOCK_END, TBLOCK_ABORT, TBLOCK_ABORT_END,
  TBLOCK_ABORT_PENDING, TBLOCK_PREPARE, TBLOCK_SUBBEGIN, TBLOCK_SUBINPROGRESS,
  TBLOCK_SUBRELEASE, TBLOCK_SUBCOMMIT, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END,
  TBLOCK_SUBABORT_PENDING, TBLOCK_SUBRESTART, TBLOCK_SUBABORT_RESTART
}
 

Functions

static void AssignTransactionId (TransactionState s)
 
static void AbortTransaction (void)
 
static void AtAbort_Memory (void)
 
static void AtCleanup_Memory (void)
 
static void AtAbort_ResourceOwner (void)
 
static void AtCCI_LocalCache (void)
 
static void AtCommit_Memory (void)
 
static void AtStart_Cache (void)
 
static void AtStart_Memory (void)
 
static void AtStart_ResourceOwner (void)
 
static void CallXactCallbacks (XactEvent event)
 
static void CallSubXactCallbacks (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
 
static void CleanupTransaction (void)
 
static void CheckTransactionChain (bool isTopLevel, bool throwError, const char *stmtType)
 
static void CommitTransaction (void)
 
static TransactionId RecordTransactionAbort (bool isSubXact)
 
static void StartTransaction (void)
 
static void StartSubTransaction (void)
 
static void CommitSubTransaction (void)
 
static void AbortSubTransaction (void)
 
static void CleanupSubTransaction (void)
 
static void PushTransaction (void)
 
static void PopTransaction (void)
 
static void AtSubAbort_Memory (void)
 
static void AtSubCleanup_Memory (void)
 
static void AtSubAbort_ResourceOwner (void)
 
static void AtSubCommit_Memory (void)
 
static void AtSubStart_Memory (void)
 
static void AtSubStart_ResourceOwner (void)
 
static void ShowTransactionState (const char *str)
 
static void ShowTransactionStateRec (const char *str, TransactionState state)
 
static const char * BlockStateAsString (TBlockState blockState)
 
static const char * TransStateAsString (TransState state)
 
bool IsTransactionState (void)
 
bool IsAbortedTransactionBlockState (void)
 
TransactionId GetTopTransactionId (void)
 
TransactionId GetTopTransactionIdIfAny (void)
 
TransactionId GetCurrentTransactionId (void)
 
TransactionId GetCurrentTransactionIdIfAny (void)
 
void MarkCurrentTransactionIdLoggedIfAny (void)
 
TransactionId GetStableLatestTransactionId (void)
 
SubTransactionId GetCurrentSubTransactionId (void)
 
bool SubTransactionIsActive (SubTransactionId subxid)
 
CommandId GetCurrentCommandId (bool used)
 
TimestampTz GetCurrentTransactionStartTimestamp (void)
 
TimestampTz GetCurrentStatementStartTimestamp (void)
 
TimestampTz GetCurrentTransactionStopTimestamp (void)
 
void SetCurrentStatementStartTimestamp (void)
 
static void SetCurrentTransactionStopTimestamp (void)
 
int GetCurrentTransactionNestLevel (void)
 
bool TransactionIdIsCurrentTransactionId (TransactionId xid)
 
bool TransactionStartedDuringRecovery (void)
 
void EnterParallelMode (void)
 
void ExitParallelMode (void)
 
bool IsInParallelMode (void)
 
void CommandCounterIncrement (void)
 
void ForceSyncCommit (void)
 
static TransactionId RecordTransactionCommit (void)
 
static void AtSubCommit_childXids (void)
 
static void AtSubAbort_childXids (void)
 
static void PrepareTransaction (void)
 
void StartTransactionCommand (void)
 
void CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void PreventTransactionChain (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionChain (bool isTopLevel, const char *stmtType)
 
void RequireTransactionChain (bool isTopLevel, const char *stmtType)
 
bool IsInTransactionChain (bool isTopLevel)
 
void RegisterXactCallback (XactCallback callback, void *arg)
 
void UnregisterXactCallback (XactCallback callback, void *arg)
 
void RegisterSubXactCallback (SubXactCallback callback, void *arg)
 
void UnregisterSubXactCallback (SubXactCallback callback, void *arg)
 
void BeginTransactionBlock (void)
 
bool PrepareTransactionBlock (char *gid)
 
bool EndTransactionBlock (void)
 
void UserAbortTransactionBlock (void)
 
void DefineSavepoint (char *name)
 
void ReleaseSavepoint (List *options)
 
void RollbackToSavepoint (List *options)
 
void BeginInternalSubTransaction (char *name)
 
void ReleaseCurrentSubTransaction (void)
 
void RollbackAndReleaseCurrentSubTransaction (void)
 
void AbortOutOfAnyTransaction (void)
 
bool IsTransactionBlock (void)
 
bool IsTransactionOrTransactionBlock (void)
 
char TransactionBlockStatusCode (void)
 
bool IsSubTransaction (void)
 
Size EstimateTransactionStateSpace (void)
 
void SerializeTransactionState (Size maxsize, char *start_address)
 
void StartParallelWorkerTransaction (char *tstatespace)
 
void EndParallelWorkerTransaction (void)
 
int xactGetCommittedChildren (TransactionId **ptr)
 
XLogRecPtr XactLogCommitRecord (TimestampTz commit_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInval, bool forceSync, TransactionId twophase_xid)
 
XLogRecPtr XactLogAbortRecord (TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, TransactionId twophase_xid)
 
static void xact_redo_commit (xl_xact_parsed_commit *parsed, TransactionId xid, XLogRecPtr lsn, RepOriginId origin_id)
 
static void xact_redo_abort (xl_xact_parsed_abort *parsed, TransactionId xid)
 
void xact_redo (XLogReaderState *record)
 

Variables

int DefaultXactIsoLevel = XACT_READ_COMMITTED
 
int XactIsoLevel
 
bool DefaultXactReadOnly = false
 
bool XactReadOnly
 
bool DefaultXactDeferrable = false
 
bool XactDeferrable
 
int synchronous_commit = SYNCHRONOUS_COMMIT_ON
 
TransactionId XactTopTransactionId = InvalidTransactionId
 
int nParallelCurrentXids = 0
 
TransactionIdParallelCurrentXids
 
bool MyXactAccessedTempRel = false
 
static TransactionStateData TopTransactionStateData
 
static int nUnreportedXids
 
static TransactionId unreportedXids [PGPROC_MAX_CACHED_SUBXIDS]
 
static TransactionState CurrentTransactionState = &TopTransactionStateData
 
static SubTransactionId currentSubTransactionId
 
static CommandId currentCommandId
 
static bool currentCommandIdUsed
 
static TimestampTz xactStartTimestamp
 
static TimestampTz stmtStartTimestamp
 
static TimestampTz xactStopTimestamp
 
static char * prepareGID
 
static bool forceSyncCommit = false
 
static MemoryContext TransactionAbortContext = NULL
 
static XactCallbackItemXact_callbacks = NULL
 
static SubXactCallbackItemSubXact_callbacks = NULL
 

Typedef Documentation

Definition at line 193 of file xact.c.

Enumeration Type Documentation

Enumerator
TBLOCK_DEFAULT 
TBLOCK_STARTED 
TBLOCK_BEGIN 
TBLOCK_INPROGRESS 
TBLOCK_PARALLEL_INPROGRESS 
TBLOCK_END 
TBLOCK_ABORT 
TBLOCK_ABORT_END 
TBLOCK_ABORT_PENDING 
TBLOCK_PREPARE 
TBLOCK_SUBBEGIN 
TBLOCK_SUBINPROGRESS 
TBLOCK_SUBRELEASE 
TBLOCK_SUBCOMMIT 
TBLOCK_SUBABORT 
TBLOCK_SUBABORT_END 
TBLOCK_SUBABORT_PENDING 
TBLOCK_SUBRESTART 
TBLOCK_SUBABORT_RESTART 

Definition at line 138 of file xact.c.

139 {
140  /* not-in-transaction-block states */
141  TBLOCK_DEFAULT, /* idle */
142  TBLOCK_STARTED, /* running single-query transaction */
143 
144  /* transaction block states */
145  TBLOCK_BEGIN, /* starting transaction block */
146  TBLOCK_INPROGRESS, /* live transaction */
147  TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
148  TBLOCK_END, /* COMMIT received */
149  TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
150  TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
151  TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
152  TBLOCK_PREPARE, /* live xact, PREPARE received */
153 
154  /* subtransaction states */
155  TBLOCK_SUBBEGIN, /* starting a subtransaction */
156  TBLOCK_SUBINPROGRESS, /* live subtransaction */
157  TBLOCK_SUBRELEASE, /* RELEASE received */
158  TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
159  TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
160  TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
161  TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
162  TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
163  TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
164 } TBlockState;
TBlockState
Definition: xact.c:138
enum TransState
Enumerator
TRANS_DEFAULT 
TRANS_START 
TRANS_INPROGRESS 
TRANS_COMMIT 
TRANS_ABORT 
TRANS_PREPARE 

Definition at line 122 of file xact.c.

123 {
124  TRANS_DEFAULT, /* idle */
125  TRANS_START, /* transaction starting */
126  TRANS_INPROGRESS, /* inside a valid transaction */
127  TRANS_COMMIT, /* commit in progress */
128  TRANS_ABORT, /* abort in progress */
129  TRANS_PREPARE /* prepare in progress */
130 } TransState;
TransState
Definition: xact.c:122

Function Documentation

void AbortCurrentTransaction ( void  )

Definition at line 2984 of file xact.c.

References AbortCurrentTransaction(), AbortSubTransaction(), AbortTransaction(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::state, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by AbortCurrentTransaction(), AutoVacLauncherMain(), PostgresMain(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), and SnapBuildClearExportedSnapshot().

2985 {
2987 
2988  switch (s->blockState)
2989  {
2990  case TBLOCK_DEFAULT:
2991  if (s->state == TRANS_DEFAULT)
2992  {
2993  /* we are idle, so nothing to do */
2994  }
2995  else
2996  {
2997  /*
2998  * We can get here after an error during transaction start
2999  * (state will be TRANS_START). Need to clean up the
3000  * incompletely started transaction. First, adjust the
3001  * low-level state to suppress warning message from
3002  * AbortTransaction.
3003  */
3004  if (s->state == TRANS_START)
3005  s->state = TRANS_INPROGRESS;
3006  AbortTransaction();
3008  }
3009  break;
3010 
3011  /*
3012  * if we aren't in a transaction block, we just do the basic abort
3013  * & cleanup transaction.
3014  */
3015  case TBLOCK_STARTED:
3016  AbortTransaction();
3019  break;
3020 
3021  /*
3022  * If we are in TBLOCK_BEGIN it means something screwed up right
3023  * after reading "BEGIN TRANSACTION". We assume that the user
3024  * will interpret the error as meaning the BEGIN failed to get him
3025  * into a transaction block, so we should abort and return to idle
3026  * state.
3027  */
3028  case TBLOCK_BEGIN:
3029  AbortTransaction();
3032  break;
3033 
3034  /*
3035  * We are somewhere in a transaction block and we've gotten a
3036  * failure, so we abort the transaction and set up the persistent
3037  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3038  */
3039  case TBLOCK_INPROGRESS:
3041  AbortTransaction();
3042  s->blockState = TBLOCK_ABORT;
3043  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3044  break;
3045 
3046  /*
3047  * Here, we failed while trying to COMMIT. Clean up the
3048  * transaction and return to idle state (we do not want to stay in
3049  * the transaction).
3050  */
3051  case TBLOCK_END:
3052  AbortTransaction();
3055  break;
3056 
3057  /*
3058  * Here, we are already in an aborted transaction state and are
3059  * waiting for a ROLLBACK, but for some reason we failed again! So
3060  * we just remain in the abort state.
3061  */
3062  case TBLOCK_ABORT:
3063  case TBLOCK_SUBABORT:
3064  break;
3065 
3066  /*
3067  * We are in a failed transaction and we got the ROLLBACK command.
3068  * We have already aborted, we just need to cleanup and go to idle
3069  * state.
3070  */
3071  case TBLOCK_ABORT_END:
3074  break;
3075 
3076  /*
3077  * We are in a live transaction and we got a ROLLBACK command.
3078  * Abort, cleanup, go to idle state.
3079  */
3080  case TBLOCK_ABORT_PENDING:
3081  AbortTransaction();
3084  break;
3085 
3086  /*
3087  * Here, we failed while trying to PREPARE. Clean up the
3088  * transaction and return to idle state (we do not want to stay in
3089  * the transaction).
3090  */
3091  case TBLOCK_PREPARE:
3092  AbortTransaction();
3095  break;
3096 
3097  /*
3098  * We got an error inside a subtransaction. Abort just the
3099  * subtransaction, and go to the persistent SUBABORT state until
3100  * we get ROLLBACK.
3101  */
3102  case TBLOCK_SUBINPROGRESS:
3105  break;
3106 
3107  /*
3108  * If we failed while trying to create a subtransaction, clean up
3109  * the broken subtransaction and abort the parent. The same
3110  * applies if we get a failure while ending a subtransaction.
3111  */
3112  case TBLOCK_SUBBEGIN:
3113  case TBLOCK_SUBRELEASE:
3114  case TBLOCK_SUBCOMMIT:
3116  case TBLOCK_SUBRESTART:
3120  break;
3121 
3122  /*
3123  * Same as above, except the Abort() was already done.
3124  */
3125  case TBLOCK_SUBABORT_END:
3129  break;
3130  }
3131 }
void AbortCurrentTransaction(void)
Definition: xact.c:2984
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
TransState state
Definition: xact.c:175
static void CleanupTransaction(void)
Definition: xact.c:2626
static void AbortSubTransaction(void)
Definition: xact.c:4543
static void AbortTransaction(void)
Definition: xact.c:2446
static void CleanupSubTransaction(void)
Definition: xact.c:4688
void AbortOutOfAnyTransaction ( void  )

Definition at line 4220 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, NULL, TransactionStateData::parent, TransactionStateData::state, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by do_autovacuum(), RemoveTempRelationsCallback(), and ShutdownPostgres().

4221 {
4223 
4224  /*
4225  * Get out of any transaction or nested transaction
4226  */
4227  do
4228  {
4229  switch (s->blockState)
4230  {
4231  case TBLOCK_DEFAULT:
4232  if (s->state == TRANS_DEFAULT)
4233  {
4234  /* Not in a transaction, do nothing */
4235  }
4236  else
4237  {
4238  /*
4239  * We can get here after an error during transaction start
4240  * (state will be TRANS_START). Need to clean up the
4241  * incompletely started transaction. First, adjust the
4242  * low-level state to suppress warning message from
4243  * AbortTransaction.
4244  */
4245  if (s->state == TRANS_START)
4246  s->state = TRANS_INPROGRESS;
4247  AbortTransaction();
4249  }
4250  break;
4251  case TBLOCK_STARTED:
4252  case TBLOCK_BEGIN:
4253  case TBLOCK_INPROGRESS:
4255  case TBLOCK_END:
4256  case TBLOCK_ABORT_PENDING:
4257  case TBLOCK_PREPARE:
4258  /* In a transaction, so clean up */
4259  AbortTransaction();
4262  break;
4263  case TBLOCK_ABORT:
4264  case TBLOCK_ABORT_END:
4265  /* AbortTransaction already done, still need Cleanup */
4268  break;
4269 
4270  /*
4271  * In a subtransaction, so clean it up and abort parent too
4272  */
4273  case TBLOCK_SUBBEGIN:
4274  case TBLOCK_SUBINPROGRESS:
4275  case TBLOCK_SUBRELEASE:
4276  case TBLOCK_SUBCOMMIT:
4278  case TBLOCK_SUBRESTART:
4281  s = CurrentTransactionState; /* changed by pop */
4282  break;
4283 
4284  case TBLOCK_SUBABORT:
4285  case TBLOCK_SUBABORT_END:
4287  /* As above, but AbortSubTransaction already done */
4289  s = CurrentTransactionState; /* changed by pop */
4290  break;
4291  }
4292  } while (s->blockState != TBLOCK_DEFAULT);
4293 
4294  /* Should be out of all subxacts now */
4295  Assert(s->parent == NULL);
4296 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
TransState state
Definition: xact.c:175
static void CleanupTransaction(void)
Definition: xact.c:2626
struct TransactionStateData * parent
Definition: xact.c:190
static void AbortSubTransaction(void)
Definition: xact.c:4543
static void AbortTransaction(void)
Definition: xact.c:2446
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void CleanupSubTransaction(void)
Definition: xact.c:4688
static void AbortSubTransaction ( void  )
static

Definition at line 4543 of file xact.c.

References AbortBufferIO(), AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubAbort_childXids(), AtSubAbort_Memory(), AtSubAbort_Notify(), AtSubAbort_Portals(), AtSubAbort_ResourceOwner(), AtSubAbort_smgr(), AtSubAbort_Snapshot(), CallSubXactCallbacks(), CurrentTransactionState, TransactionStateData::curTransactionOwner, elog, TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, IsInParallelMode(), LockErrorCleanup(), LWLockReleaseAll(), TransactionStateData::nestingLevel, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PG_SETMASK, pgstat_progress_end_command(), pgstat_report_wait_end(), TransactionStateData::prevSecContext, TransactionStateData::prevUser, TransactionStateData::prevXactReadOnly, RecordTransactionAbort(), reschedule_timeouts(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerRelease(), RESUME_INTERRUPTS, SetUserIdAndSecContext(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, SUBXACT_EVENT_ABORT_SUB, TRANS_ABORT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, TransStateAsString(), UnBlockSig, UnlockBuffers(), WARNING, XactReadOnly, and XLogResetInsertion().

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), CommitTransactionCommand(), and RollbackAndReleaseCurrentSubTransaction().

4544 {
4546 
4547  /* Prevent cancel/die interrupt while cleaning up */
4548  HOLD_INTERRUPTS();
4549 
4550  /* Make sure we have a valid memory context and resource owner */
4553 
4554  /*
4555  * Release any LW locks we might be holding as quickly as possible.
4556  * (Regular locks, however, must be held till we finish aborting.)
4557  * Releasing LW locks is critical since we might try to grab them again
4558  * while cleaning up!
4559  *
4560  * FIXME This may be incorrect --- Are there some locks we should keep?
4561  * Buffer locks, for example? I don't think so but I'm not sure.
4562  */
4563  LWLockReleaseAll();
4564 
4567  AbortBufferIO();
4568  UnlockBuffers();
4569 
4570  /* Reset WAL record construction state */
4572 
4573  /*
4574  * Also clean up any open wait for lock, since the lock manager will choke
4575  * if we try to wait for another lock before doing this.
4576  */
4577  LockErrorCleanup();
4578 
4579  /*
4580  * If any timeout events are still active, make sure the timeout interrupt
4581  * is scheduled. This covers possible loss of a timeout interrupt due to
4582  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4583  * We delay this till after LockErrorCleanup so that we don't uselessly
4584  * reschedule lock or deadlock check timeouts.
4585  */
4587 
4588  /*
4589  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4590  * handler. We do this fairly early in the sequence so that the timeout
4591  * infrastructure will be functional if needed while aborting.
4592  */
4594 
4595  /*
4596  * check the current transaction state
4597  */
4598  ShowTransactionState("AbortSubTransaction");
4599 
4600  if (s->state != TRANS_INPROGRESS)
4601  elog(WARNING, "AbortSubTransaction while in %s state",
4603 
4604  s->state = TRANS_ABORT;
4605 
4606  /*
4607  * Reset user ID which might have been changed transiently. (See notes in
4608  * AbortTransaction.)
4609  */
4611 
4612  /* Exit from parallel mode, if necessary. */
4613  if (IsInParallelMode())
4614  {
4616  s->parallelModeLevel = 0;
4617  }
4618 
4619  /*
4620  * We can skip all this stuff if the subxact failed before creating a
4621  * ResourceOwner...
4622  */
4623  if (s->curTransactionOwner)
4624  {
4625  AfterTriggerEndSubXact(false);
4631  s->parent->subTransactionId);
4633 
4634  /* Advertise the fact that we aborted in pg_clog. */
4635  (void) RecordTransactionAbort(true);
4636 
4637  /* Post-abort cleanup */
4640 
4642  s->parent->subTransactionId);
4643 
4646  false, false);
4648  s->parent->subTransactionId);
4649  AtEOSubXact_Inval(false);
4652  false, false);
4655  false, false);
4656  AtSubAbort_smgr();
4657 
4658  AtEOXact_GUC(false, s->gucNestLevel);
4659  AtEOSubXact_SPI(false, s->subTransactionId);
4661  s->parent->subTransactionId);
4663  s->parent->subTransactionId);
4665  s->parent->subTransactionId);
4667  AtEOSubXact_PgStat(false, s->nestingLevel);
4669  }
4670 
4671  /*
4672  * Restore the upper transaction's read-only state, too. This should be
4673  * redundant with GUC's cleanup but we may as well do it for consistency
4674  * with the commit case.
4675  */
4677 
4679 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1684
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3066
static TransactionState CurrentTransactionState
Definition: xact.c:231
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:395
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1824
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:214
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:408
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1018
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
bool IsInParallelMode(void)
Definition: xact.c:912
static void AtSubAbort_Memory(void)
Definition: xact.c:1659
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:190
static void ShowTransactionState(const char *str)
Definition: xact.c:4950
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5040
SubTransactionId subTransactionId
Definition: xact.c:172
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void AtSubAbort_childXids(void)
Definition: xact.c:1697
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1124
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2034
void LockErrorCleanup(void)
Definition: proc.c:677
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:674
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3501
void AtSubAbort_Notify(void)
Definition: async.c:1666
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:12540
void pgstat_progress_end_command(void)
Definition: pgstat.c:2951
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2458
TransactionId transactionId
Definition: xact.c:171
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3392
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:189
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:894
void AtSubAbort_smgr(void)
Definition: storage.c:471
bool prevXactReadOnly
Definition: xact.c:186
void AbortBufferIO(void)
Definition: bufmgr.c:3957
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4442
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:979
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define elog
Definition: elog.h:219
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:3793
void LWLockReleaseAll(void)
Definition: lwlock.c:1813
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:899
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1528
static void AbortTransaction ( void  )
static

Definition at line 2446 of file xact.c.

References AbortBufferIO(), AfterTriggerEndXact(), Assert, AtAbort_Memory(), AtAbort_Notify(), AtAbort_Portals(), AtAbort_ResourceOwner(), AtAbort_Twophase(), AtEOXact_Buffers(), AtEOXact_CatCache(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_Inval(), AtEOXact_LargeObject(), AtEOXact_MultiXact(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_Parallel(), AtEOXact_PgStat(), AtEOXact_RelationCache(), AtEOXact_RelationMap(), AtEOXact_SMgr(), AtEOXact_SPI(), TransactionStateData::blockState, CallXactCallbacks(), ConditionVariableCancelSleep(), CurrentTransactionState, elog, HOLD_INTERRUPTS, InvalidTransactionId, IsInParallelMode(), LockErrorCleanup(), LWLockReleaseAll(), PGPROC::lxid, MyProc, NULL, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PG_SETMASK, pgstat_progress_end_command(), pgstat_report_wait_end(), pgstat_report_xact_timestamp(), TransactionStateData::prevSecContext, TransactionStateData::prevUser, ProcArrayEndTransaction(), RecordTransactionAbort(), reschedule_timeouts(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerRelease(), RESUME_INTERRUPTS, SetUserIdAndSecContext(), smgrDoPendingDeletes(), TransactionStateData::state, TBLOCK_PARALLEL_INPROGRESS, TopTransactionResourceOwner, TRANS_ABORT, TRANS_INPROGRESS, TRANS_PREPARE, TransStateAsString(), UnBlockSig, UnlockBuffers(), WARNING, XACT_EVENT_ABORT, XACT_EVENT_PARALLEL_ABORT, XactLastRecEnd, XLogResetInsertion(), and XLogSetAsyncXactLSN().

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), and CommitTransactionCommand().

2447 {
2449  TransactionId latestXid;
2450  bool is_parallel_worker;
2451 
2452  /* Prevent cancel/die interrupt while cleaning up */
2453  HOLD_INTERRUPTS();
2454 
2455  /* Make sure we have a valid memory context and resource owner */
2456  AtAbort_Memory();
2458 
2459  /*
2460  * Release any LW locks we might be holding as quickly as possible.
2461  * (Regular locks, however, must be held till we finish aborting.)
2462  * Releasing LW locks is critical since we might try to grab them again
2463  * while cleaning up!
2464  */
2465  LWLockReleaseAll();
2466 
2467  /* Clear wait information and command progress indicator */
2470 
2471  /* Clean up buffer I/O and buffer context locks, too */
2472  AbortBufferIO();
2473  UnlockBuffers();
2474 
2475  /* Reset WAL record construction state */
2477 
2478  /* Cancel condition variable sleep */
2480 
2481  /*
2482  * Also clean up any open wait for lock, since the lock manager will choke
2483  * if we try to wait for another lock before doing this.
2484  */
2485  LockErrorCleanup();
2486 
2487  /*
2488  * If any timeout events are still active, make sure the timeout interrupt
2489  * is scheduled. This covers possible loss of a timeout interrupt due to
2490  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2491  * We delay this till after LockErrorCleanup so that we don't uselessly
2492  * reschedule lock or deadlock check timeouts.
2493  */
2495 
2496  /*
2497  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2498  * handler. We do this fairly early in the sequence so that the timeout
2499  * infrastructure will be functional if needed while aborting.
2500  */
2502 
2503  /*
2504  * check the current transaction state
2505  */
2506  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2507  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2508  elog(WARNING, "AbortTransaction while in %s state",
2510  Assert(s->parent == NULL);
2511 
2512  /*
2513  * set the current transaction state information appropriately during the
2514  * abort processing
2515  */
2516  s->state = TRANS_ABORT;
2517 
2518  /*
2519  * Reset user ID which might have been changed transiently. We need this
2520  * to clean up in case control escaped out of a SECURITY DEFINER function
2521  * or other local change of CurrentUserId; therefore, the prior value of
2522  * SecurityRestrictionContext also needs to be restored.
2523  *
2524  * (Note: it is not necessary to restore session authorization or role
2525  * settings here because those can only be changed via GUC, and GUC will
2526  * take care of rolling them back if need be.)
2527  */
2529 
2530  /* If in parallel mode, clean up workers and exit parallel mode. */
2531  if (IsInParallelMode())
2532  {
2533  AtEOXact_Parallel(false);
2534  s->parallelModeLevel = 0;
2535  }
2536 
2537  /*
2538  * do abort processing
2539  */
2540  AfterTriggerEndXact(false); /* 'false' means it's abort */
2541  AtAbort_Portals();
2542  AtEOXact_LargeObject(false);
2543  AtAbort_Notify();
2544  AtEOXact_RelationMap(false);
2545  AtAbort_Twophase();
2546 
2547  /*
2548  * Advertise the fact that we aborted in pg_clog (assuming that we got as
2549  * far as assigning an XID to advertise). But if we're inside a parallel
2550  * worker, skip this; the user backend must be the one to write the abort
2551  * record.
2552  */
2553  if (!is_parallel_worker)
2554  latestXid = RecordTransactionAbort(false);
2555  else
2556  {
2557  latestXid = InvalidTransactionId;
2558 
2559  /*
2560  * Since the parallel master won't get our value of XactLastRecEnd in
2561  * this case, we nudge WAL-writer ourselves in this case. See related
2562  * comments in RecordTransactionAbort for why this matters.
2563  */
2565  }
2566 
2567  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2568 
2569  /*
2570  * Let others know about no transaction in progress by me. Note that this
2571  * must be done _before_ releasing locks we hold and _after_
2572  * RecordTransactionAbort.
2573  */
2574  ProcArrayEndTransaction(MyProc, latestXid);
2575 
2576  /*
2577  * Post-abort cleanup. See notes in CommitTransaction() concerning
2578  * ordering. We can skip all of it if the transaction failed before
2579  * creating a resource owner.
2580  */
2582  {
2583  if (is_parallel_worker)
2585  else
2587 
2590  false, true);
2591  AtEOXact_Buffers(false);
2592  AtEOXact_RelationCache(false);
2593  AtEOXact_Inval(false);
2597  false, true);
2600  false, true);
2601  smgrDoPendingDeletes(false);
2602  AtEOXact_CatCache(false);
2603 
2604  AtEOXact_GUC(false, 1);
2605  AtEOXact_SPI(false);
2607  AtEOXact_Namespace(false, is_parallel_worker);
2608  AtEOXact_SMgr();
2609  AtEOXact_Files();
2611  AtEOXact_HashTables(false);
2612  AtEOXact_PgStat(false);
2614  }
2615 
2616  /*
2617  * State remains TRANS_ABORT until CleanupTransaction().
2618  */
2620 }
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:306
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1671
static TransactionState CurrentTransactionState
Definition: xact.c:231
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:920
uint32 TransactionId
Definition: c.h:393
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:395
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3003
XLogRecPtr XactLastRecEnd
Definition: xlog.c:335
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2915
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2398
void AtEOXact_RelationMap(bool isCommit)
Definition: relmapper.c:456
void AtEOXact_CatCache(bool isCommit)
Definition: catcache.c:548
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3337
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:12498
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:394
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:408
void AtAbort_Portals(void)
Definition: portalmem.c:746
TBlockState blockState
Definition: xact.c:176
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
TransState state
Definition: xact.c:175
void AtEOXact_SMgr(void)
Definition: smgr.c:798
bool IsInParallelMode(void)
Definition: xact.c:912
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4320
void XLogResetInsertion(void)
Definition: xloginsert.c:193
void AtEOXact_Files(void)
Definition: fd.c:2489
struct TransactionStateData * parent
Definition: xact.c:190
void AtAbort_Twophase(void)
Definition: twophase.c:288
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5040
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1124
void LockErrorCleanup(void)
Definition: proc.c:677
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3501
static void AtAbort_Memory(void)
Definition: xact.c:1639
void pgstat_progress_end_command(void)
Definition: pgstat.c:2951
void AtAbort_Notify(void)
Definition: async.c:1584
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2594
int parallelModeLevel
Definition: xact.c:189
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:186
void AtEOXact_MultiXact(void)
Definition: multixact.c:1662
void AbortBufferIO(void)
Definition: bufmgr.c:3957
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define elog
Definition: elog.h:219
void LWLockReleaseAll(void)
Definition: lwlock.c:1813
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1798
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:633
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:918
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3738
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1528
void AtEOXact_PgStat(bool isCommit)
Definition: pgstat.c:1951
LocalTransactionId lxid
Definition: proc.h:95
static void AssignTransactionId ( TransactionState  s)
static

Definition at line 488 of file xact.c.

References Assert, CurrentResourceOwner, TransactionStateData::curTransactionOwner, TransactionStateData::didLogXid, elog, ERROR, GetNewTransactionId(), GetTopTransactionId(), IsInParallelMode(), IsParallelWorker, MinSizeOfXactAssignment, TransactionStateData::nestingLevel, xl_xact_assignment::nsubxacts, NULL, nUnreportedXids, palloc(), TransactionStateData::parent, pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PGPROC_MAX_CACHED_SUBXIDS, RegisterPredicateLockingXid(), TransactionStateData::state, SubTransSetParent(), TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, unreportedXids, XactLockTableInsert(), XactTopTransactionId, XLOG_XACT_ASSIGNMENT, XLogBeginInsert(), XLogInsert(), XLogLogicalInfoActive, XLogRegisterData(), XLogStandbyInfoActive, and xl_xact_assignment::xtop.

Referenced by GetCurrentTransactionId(), and GetTopTransactionId().

489 {
490  bool isSubXact = (s->parent != NULL);
491  ResourceOwner currentOwner;
492  bool log_unknown_top = false;
493 
494  /* Assert that caller didn't screw up */
497 
498  /*
499  * Workers synchronize transaction state at the beginning of each parallel
500  * operation, so we can't account for new XIDs at this point.
501  */
503  elog(ERROR, "cannot assign XIDs during a parallel operation");
504 
505  /*
506  * Ensure parent(s) have XIDs, so that a child always has an XID later
507  * than its parent. Musn't recurse here, or we might get a stack overflow
508  * if we're at the bottom of a huge stack of subtransactions none of which
509  * have XIDs yet.
510  */
511  if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
512  {
513  TransactionState p = s->parent;
514  TransactionState *parents;
515  size_t parentOffset = 0;
516 
517  parents = palloc(sizeof(TransactionState) * s->nestingLevel);
518  while (p != NULL && !TransactionIdIsValid(p->transactionId))
519  {
520  parents[parentOffset++] = p;
521  p = p->parent;
522  }
523 
524  /*
525  * This is technically a recursive call, but the recursion will never
526  * be more than one layer deep.
527  */
528  while (parentOffset != 0)
529  AssignTransactionId(parents[--parentOffset]);
530 
531  pfree(parents);
532  }
533 
534  /*
535  * When wal_level=logical, guarantee that a subtransaction's xid can only
536  * be seen in the WAL stream if its toplevel xid has been logged before.
537  * If necessary we log an xact_assignment record with fewer than
538  * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
539  * for a transaction even though it appears in a WAL record, we just might
540  * superfluously log something. That can happen when an xid is included
541  * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
542  * xl_standby_locks.
543  */
544  if (isSubXact && XLogLogicalInfoActive() &&
546  log_unknown_top = true;
547 
548  /*
549  * Generate a new Xid and record it in PG_PROC and pg_subtrans.
550  *
551  * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
552  * shared storage other than PG_PROC; because if there's no room for it in
553  * PG_PROC, the subtrans entry is needed to ensure that other backends see
554  * the Xid as "running". See GetNewTransactionId.
555  */
556  s->transactionId = GetNewTransactionId(isSubXact);
557  if (!isSubXact)
559 
560  if (isSubXact)
562 
563  /*
564  * If it's a top-level transaction, the predicate locking system needs to
565  * be told about it too.
566  */
567  if (!isSubXact)
569 
570  /*
571  * Acquire lock on the transaction XID. (We assume this cannot block.) We
572  * have to ensure that the lock is assigned to the transaction's own
573  * ResourceOwner.
574  */
575  currentOwner = CurrentResourceOwner;
576  PG_TRY();
577  {
580  }
581  PG_CATCH();
582  {
583  /* Ensure CurrentResourceOwner is restored on error */
584  CurrentResourceOwner = currentOwner;
585  PG_RE_THROW();
586  }
587  PG_END_TRY();
588  CurrentResourceOwner = currentOwner;
589 
590  /*
591  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
592  * top-level transaction we issue a WAL record for the assignment. We
593  * include the top-level xid and all the subxids that have not yet been
594  * reported using XLOG_XACT_ASSIGNMENT records.
595  *
596  * This is required to limit the amount of shared memory required in a hot
597  * standby server to keep track of in-progress XIDs. See notes for
598  * RecordKnownAssignedTransactionIds().
599  *
600  * We don't keep track of the immediate parent of each subxid, only the
601  * top-level transaction that each subxact belongs to. This is correct in
602  * recovery only because aborted subtransactions are separately WAL
603  * logged.
604  *
605  * This is correct even for the case where several levels above us didn't
606  * have an xid assigned as we recursed up to them beforehand.
607  */
608  if (isSubXact && XLogStandbyInfoActive())
609  {
611  nUnreportedXids++;
612 
613  /*
614  * ensure this test matches similar one in
615  * RecoverPreparedTransactions()
616  */
618  log_unknown_top)
619  {
620  xl_xact_assignment xlrec;
621 
622  /*
623  * xtop is always set by now because we recurse up transaction
624  * stack to the highest unassigned xid and then come back down
625  */
626  xlrec.xtop = GetTopTransactionId();
628  xlrec.nsubxacts = nUnreportedXids;
629 
630  XLogBeginInsert();
631  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
633  nUnreportedXids * sizeof(TransactionId));
634 
635  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
636 
637  nUnreportedXids = 0;
638  /* mark top, not current xact as having been logged */
640  }
641  }
642 }
TransactionId XactTopTransactionId
Definition: xact.c:107
uint32 TransactionId
Definition: c.h:393
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void SubTransSetParent(TransactionId xid, TransactionId parent, bool overwriteOK)
Definition: subtrans.c:76
TransactionId GetNewTransactionId(bool isSubXact)
Definition: varsup.c:48
TransactionId xtop
Definition: xact.h:163
TransactionId GetTopTransactionId(void)
Definition: xact.c:388
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
void pfree(void *pointer)
Definition: mcxt.c:992
bool IsInParallelMode(void)
Definition: xact.c:912
#define ERROR
Definition: elog.h:43
#define XLogLogicalInfoActive()
Definition: xlog.h:162
struct TransactionStateData * parent
Definition: xact.c:190
#define MinSizeOfXactAssignment
Definition: xact.h:168
#define IsParallelWorker()
Definition: parallel.h:53
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:120
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:513
#define XLogStandbyInfoActive()
Definition: xlog.h:159
static int nUnreportedXids
Definition: xact.c:228
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:35
TransactionId transactionId
Definition: xact.c:171
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static TransactionStateData TopTransactionStateData
Definition: xact.c:200
#define PG_RE_THROW()
Definition: elog.h:314
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1804
void * palloc(Size size)
Definition: mcxt.c:891
static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: xact.c:229
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:488
#define PG_TRY()
Definition: elog.h:284
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define PG_END_TRY()
Definition: elog.h:300
static void AtAbort_Memory ( void  )
static

Definition at line 1639 of file xact.c.

References MemoryContextSwitchTo(), NULL, and TopMemoryContext.

Referenced by AbortTransaction().

1640 {
1641  /*
1642  * Switch into TransactionAbortContext, which should have some free space
1643  * even if nothing else does. We'll work in this context until we've
1644  * finished cleaning up.
1645  *
1646  * It is barely possible to get here when we've not been able to create
1647  * TransactionAbortContext yet; if so use TopMemoryContext.
1648  */
1651  else
1653 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:268
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define NULL
Definition: c.h:226
static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1671 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

1672 {
1673  /*
1674  * Make sure we have a valid ResourceOwner, if possible (else it will be
1675  * NULL, which is OK)
1676  */
1678 }
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void AtCCI_LocalCache ( void  )
static

Definition at line 1363 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

1364 {
1365  /*
1366  * Make any pending relation map changes visible. We must do this before
1367  * processing local sinval messages, so that the map changes will get
1368  * reflected into the relcache when relcache invals are processed.
1369  */
1371 
1372  /*
1373  * Make catalog changes visible to me for the next command.
1374  */
1376 }
void CommandEndInvalidationMessages(void)
Definition: inval.c:1056
void AtCCI_RelationMap(void)
Definition: relmapper.c:422
static void AtCleanup_Memory ( void  )
static

Definition at line 1729 of file xact.c.

References Assert, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), NULL, TransactionStateData::parent, TopMemoryContext, and TopTransactionContext.

Referenced by CleanupTransaction().

1730 {
1732 
1733  /*
1734  * Now that we're "out" of a transaction, have the system allocate things
1735  * in the top memory context instead of per-transaction contexts.
1736  */
1738 
1739  /*
1740  * Clear the special abort context for next time.
1741  */
1744 
1745  /*
1746  * Release all transaction-local memory.
1747  */
1748  if (TopTransactionContext != NULL)
1753 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:268
struct TransactionStateData * parent
Definition: xact.c:190
MemoryContext curTransactionContext
Definition: xact.c:179
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:88
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtCommit_Memory ( void  )
static

Definition at line 1382 of file xact.c.

References Assert, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextSwitchTo(), NULL, TopMemoryContext, and TopTransactionContext.

Referenced by CommitTransaction(), and PrepareTransaction().

1383 {
1384  /*
1385  * Now that we're "out" of a transaction, have the system allocate things
1386  * in the top memory context instead of per-transaction contexts.
1387  */
1389 
1390  /*
1391  * Release all transaction-local memory.
1392  */
1398 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
MemoryContext curTransactionContext
Definition: xact.c:179
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtStart_Cache ( void  )
static

Definition at line 984 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

985 {
987 }
void AcceptInvalidationMessages(void)
Definition: inval.c:670
static void AtStart_Memory ( void  )
static

Definition at line 993 of file xact.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextSwitchTo(), NULL, TopMemoryContext, and TopTransactionContext.

Referenced by StartTransaction().

994 {
996 
997  /*
998  * If this is the first time through, create a private context for
999  * AbortTransaction to work in. By reserving some space now, we can
1000  * insulate AbortTransaction from out-of-memory scenarios. Like
1001  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1002  * size, so that space will be reserved immediately.
1003  */
1007  "TransactionAbortContext",
1008  32 * 1024,
1009  32 * 1024,
1010  32 * 1024);
1011 
1012  /*
1013  * We shouldn't have a transaction context already.
1014  */
1016 
1017  /*
1018  * Create a toplevel context for the transaction.
1019  */
1022  "TopTransactionContext",
1024 
1025  /*
1026  * In a top-level transaction, CurTransactionContext is the same as
1027  * TopTransactionContext.
1028  */
1031 
1032  /* Make the CurTransactionContext active. */
1034 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:268
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
MemoryContext curTransactionContext
Definition: xact.c:179
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtStart_ResourceOwner ( void  )
static

Definition at line 1040 of file xact.c.

References Assert, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, NULL, ResourceOwnerCreate(), and TopTransactionResourceOwner.

Referenced by StartTransaction().

1041 {
1043 
1044  /*
1045  * We shouldn't have a transaction resource owner already.
1046  */
1048 
1049  /*
1050  * Create a toplevel resource owner for the transaction.
1051  */
1052  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1053 
1057 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:180
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
static void AtSubAbort_childXids ( void  )
static

Definition at line 1697 of file xact.c.

References TransactionStateData::childXids, CurrentTransactionState, TransactionStateData::maxChildXids, TransactionStateData::nChildXids, NULL, and pfree().

Referenced by AbortSubTransaction().

1698 {
1700 
1701  /*
1702  * We keep the child-XID arrays in TopTransactionContext (see
1703  * AtSubCommit_childXids). This means we'd better free the array
1704  * explicitly at abort to avoid leakage.
1705  */
1706  if (s->childXids != NULL)
1707  pfree(s->childXids);
1708  s->childXids = NULL;
1709  s->nChildXids = 0;
1710  s->maxChildXids = 0;
1711 
1712  /*
1713  * We could prune the unreportedXids array here. But we don't bother. That
1714  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1715  * would likely introduce more CPU time into the more common paths, so we
1716  * choose not to do that.
1717  */
1718 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TransactionId * childXids
Definition: xact.c:181
void pfree(void *pointer)
Definition: mcxt.c:992
#define NULL
Definition: c.h:226
static void AtSubAbort_Memory ( void  )
static

Definition at line 1659 of file xact.c.

References Assert, MemoryContextSwitchTo(), and NULL.

Referenced by AbortSubTransaction().

1660 {
1662 
1664 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:268
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1684 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1685 {
1687 
1688  /* Make sure we have a valid ResourceOwner */
1690 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:180
static void AtSubCleanup_Memory ( void  )
static

Definition at line 1765 of file xact.c.

References Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), NULL, and TransactionStateData::parent.

Referenced by CleanupSubTransaction().

1766 {
1768 
1769  Assert(s->parent != NULL);
1770 
1771  /* Make sure we're not in an about-to-be-deleted context */
1774 
1775  /*
1776  * Clear the special abort context for next time.
1777  */
1780 
1781  /*
1782  * Delete the subxact local memory contexts. Its CurTransactionContext can
1783  * go too (note this also kills CurTransactionContexts from any children
1784  * of the subxact).
1785  */
1786  if (s->curTransactionContext)
1789 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:268
struct TransactionStateData * parent
Definition: xact.c:190
MemoryContext curTransactionContext
Definition: xact.c:179
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:88
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtSubCommit_childXids ( void  )
static

Definition at line 1438 of file xact.c.

References Assert, TransactionStateData::childXids, CurrentTransactionState, ereport, errcode(), errmsg(), ERROR, MaxAllocSize, TransactionStateData::maxChildXids, MemoryContextAlloc(), Min, TransactionStateData::nChildXids, NULL, TransactionStateData::parent, pfree(), repalloc(), TopTransactionContext, and TransactionStateData::transactionId.

Referenced by CommitSubTransaction().

1439 {
1441  int new_nChildXids;
1442 
1443  Assert(s->parent != NULL);
1444 
1445  /*
1446  * The parent childXids array will need to hold my XID and all my
1447  * childXids, in addition to the XIDs already there.
1448  */
1449  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1450 
1451  /* Allocate or enlarge the parent array if necessary */
1452  if (s->parent->maxChildXids < new_nChildXids)
1453  {
1454  int new_maxChildXids;
1455  TransactionId *new_childXids;
1456 
1457  /*
1458  * Make it 2x what's needed right now, to avoid having to enlarge it
1459  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1460  * is what ensures that we don't need to worry about integer overflow
1461  * here or in the calculation of new_nChildXids.)
1462  */
1463  new_maxChildXids = Min(new_nChildXids * 2,
1464  (int) (MaxAllocSize / sizeof(TransactionId)));
1465 
1466  if (new_maxChildXids < new_nChildXids)
1467  ereport(ERROR,
1468  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1469  errmsg("maximum number of committed subtransactions (%d) exceeded",
1470  (int) (MaxAllocSize / sizeof(TransactionId)))));
1471 
1472  /*
1473  * We keep the child-XID arrays in TopTransactionContext; this avoids
1474  * setting up child-transaction contexts for what might be just a few
1475  * bytes of grandchild XIDs.
1476  */
1477  if (s->parent->childXids == NULL)
1478  new_childXids =
1480  new_maxChildXids * sizeof(TransactionId));
1481  else
1482  new_childXids = repalloc(s->parent->childXids,
1483  new_maxChildXids * sizeof(TransactionId));
1484 
1485  s->parent->childXids = new_childXids;
1486  s->parent->maxChildXids = new_maxChildXids;
1487  }
1488 
1489  /*
1490  * Copy all my XIDs to parent's array.
1491  *
1492  * Note: We rely on the fact that the XID of a child always follows that
1493  * of its parent. By copying the XID of this subtransaction before the
1494  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1495  * all XIDs already in the array belong to subtransactions started and
1496  * subcommitted before us, so their XIDs must precede ours.
1497  */
1499 
1500  if (s->nChildXids > 0)
1501  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1502  s->childXids,
1503  s->nChildXids * sizeof(TransactionId));
1504 
1505  s->parent->nChildXids = new_nChildXids;
1506 
1507  /* Release child's array to avoid leakage */
1508  if (s->childXids != NULL)
1509  pfree(s->childXids);
1510  /* We must reset these to avoid double-free if fail later in commit */
1511  s->childXids = NULL;
1512  s->nChildXids = 0;
1513  s->maxChildXids = 0;
1514 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
MemoryContext TopTransactionContext
Definition: mcxt.c:48
uint32 TransactionId
Definition: c.h:393
#define Min(x, y)
Definition: c.h:801
int errcode(int sqlerrcode)
Definition: elog.c:575
TransactionId * childXids
Definition: xact.c:181
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
struct TransactionStateData * parent
Definition: xact.c:190
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
TransactionId transactionId
Definition: xact.c:171
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
static void AtSubCommit_Memory ( void  )
static

Definition at line 1409 of file xact.c.

References Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextIsEmpty(), MemoryContextSwitchTo(), NULL, and TransactionStateData::parent.

Referenced by CommitSubTransaction().

1410 {
1412 
1413  Assert(s->parent != NULL);
1414 
1415  /* Return to parent transaction level's memory context. */
1418 
1419  /*
1420  * Ordinarily we cannot throw away the child's CurTransactionContext,
1421  * since the data it contains will be needed at upper commit. However, if
1422  * there isn't actually anything in it, we can throw it away. This avoids
1423  * a small memory leak in the common case of "trivial" subxacts.
1424  */
1426  {
1429  }
1430 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:460
struct TransactionStateData * parent
Definition: xact.c:190
MemoryContext curTransactionContext
Definition: xact.c:179
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtSubStart_Memory ( void  )
static

Definition at line 1068 of file xact.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextSwitchTo(), and NULL.

Referenced by StartSubTransaction().

1069 {
1071 
1073 
1074  /*
1075  * Create a CurTransactionContext, which will be used to hold data that
1076  * survives subtransaction commit but disappears on subtransaction abort.
1077  * We make it a child of the immediate parent's CurTransactionContext.
1078  */
1080  "CurTransactionContext",
1083 
1084  /* Make the CurTransactionContext active. */
1086 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
MemoryContext curTransactionContext
Definition: xact.c:179
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1092 of file xact.c.

References Assert, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, NULL, TransactionStateData::parent, and ResourceOwnerCreate().

Referenced by StartSubTransaction().

1093 {
1095 
1096  Assert(s->parent != NULL);
1097 
1098  /*
1099  * Create a resource owner for the subtransaction. We make it a child of
1100  * the immediate parent's resource owner.
1101  */
1102  s->curTransactionOwner =
1104  "SubTransaction");
1105 
1108 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:180
struct TransactionStateData * parent
Definition: xact.c:190
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void BeginInternalSubTransaction ( char *  name)

Definition at line 4051 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CommitTransactionCommand(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), MemoryContextStrdup(), TransactionStateData::name, PushTransaction(), StartTransactionCommand(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and TopTransactionContext.

Referenced by exec_stmt_block(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), pltcl_returnnext(), pltcl_subtrans_begin(), PLy_spi_subtransaction_begin(), PLy_subtransaction_enter(), ReorderBufferCommit(), and ReorderBufferImmediateInvalidation().

4052 {
4054 
4055  /*
4056  * Workers synchronize transaction state at the beginning of each parallel
4057  * operation, so we can't account for new subtransactions after that
4058  * point. We might be able to make an exception for the type of
4059  * subtransaction established by this function, which is typically used in
4060  * contexts where we're going to release or roll back the subtransaction
4061  * before proceeding further, so that no enduring change to the
4062  * transaction state occurs. For now, however, we prohibit this case along
4063  * with all the others.
4064  */
4065  if (IsInParallelMode())
4066  ereport(ERROR,
4067  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4068  errmsg("cannot start subtransactions during a parallel operation")));
4069 
4070  switch (s->blockState)
4071  {
4072  case TBLOCK_STARTED:
4073  case TBLOCK_INPROGRESS:
4074  case TBLOCK_END:
4075  case TBLOCK_PREPARE:
4076  case TBLOCK_SUBINPROGRESS:
4077  /* Normal subtransaction start */
4078  PushTransaction();
4079  s = CurrentTransactionState; /* changed by push */
4080 
4081  /*
4082  * Savepoint names, like the TransactionState block itself, live
4083  * in TopTransactionContext.
4084  */
4085  if (name)
4087  break;
4088 
4089  /* These cases are invalid. */
4090  case TBLOCK_DEFAULT:
4091  case TBLOCK_BEGIN:
4093  case TBLOCK_SUBBEGIN:
4094  case TBLOCK_SUBRELEASE:
4095  case TBLOCK_SUBCOMMIT:
4096  case TBLOCK_ABORT:
4097  case TBLOCK_SUBABORT:
4098  case TBLOCK_ABORT_END:
4099  case TBLOCK_SUBABORT_END:
4100  case TBLOCK_ABORT_PENDING:
4102  case TBLOCK_SUBRESTART:
4104  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4106  break;
4107  }
4108 
4111 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
MemoryContext TopTransactionContext
Definition: mcxt.c:48
void CommitTransactionCommand(void)
Definition: xact.c:2745
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:176
bool IsInParallelMode(void)
Definition: xact.c:912
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5001
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4721
void StartTransactionCommand(void)
Definition: xact.c:2675
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1152
#define elog
Definition: elog.h:219
void BeginTransactionBlock ( void  )

Definition at line 3413 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and WARNING.

Referenced by standard_ProcessUtility().

3414 {
3416 
3417  switch (s->blockState)
3418  {
3419  /*
3420  * We are not inside a transaction block, so allow one to begin.
3421  */
3422  case TBLOCK_STARTED:
3423  s->blockState = TBLOCK_BEGIN;
3424  break;
3425 
3426  /*
3427  * Already a transaction block in progress.
3428  */
3429  case TBLOCK_INPROGRESS:
3431  case TBLOCK_SUBINPROGRESS:
3432  case TBLOCK_ABORT:
3433  case TBLOCK_SUBABORT:
3434  ereport(WARNING,
3435  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3436  errmsg("there is already a transaction in progress")));
3437  break;
3438 
3439  /* These cases are invalid. */
3440  case TBLOCK_DEFAULT:
3441  case TBLOCK_BEGIN:
3442  case TBLOCK_SUBBEGIN:
3443  case TBLOCK_END:
3444  case TBLOCK_SUBRELEASE:
3445  case TBLOCK_SUBCOMMIT:
3446  case TBLOCK_ABORT_END:
3447  case TBLOCK_SUBABORT_END:
3448  case TBLOCK_ABORT_PENDING:
3450  case TBLOCK_SUBRESTART:
3452  case TBLOCK_PREPARE:
3453  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3455  break;
3456  }
3457 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:176
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5001
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static const char * BlockStateAsString ( TBlockState  blockState)
static

Definition at line 5001 of file xact.c.

References TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, and TBLOCK_SUBRESTART.

Referenced by BeginInternalSubTransaction(), BeginTransactionBlock(), CommitTransactionCommand(), DefineSavepoint(), EndTransactionBlock(), ReleaseCurrentSubTransaction(), ReleaseSavepoint(), RollbackAndReleaseCurrentSubTransaction(), RollbackToSavepoint(), ShowTransactionStateRec(), StartTransactionCommand(), TransactionBlockStatusCode(), and UserAbortTransactionBlock().

5002 {
5003  switch (blockState)
5004  {
5005  case TBLOCK_DEFAULT:
5006  return "DEFAULT";
5007  case TBLOCK_STARTED:
5008  return "STARTED";
5009  case TBLOCK_BEGIN:
5010  return "BEGIN";
5011  case TBLOCK_INPROGRESS:
5012  return "INPROGRESS";
5014  return "PARALLEL_INPROGRESS";
5015  case TBLOCK_END:
5016  return "END";
5017  case TBLOCK_ABORT:
5018  return "ABORT";
5019  case TBLOCK_ABORT_END:
5020  return "ABORT END";
5021  case TBLOCK_ABORT_PENDING:
5022  return "ABORT PEND";
5023  case TBLOCK_PREPARE:
5024  return "PREPARE";
5025  case TBLOCK_SUBBEGIN:
5026  return "SUB BEGIN";
5027  case TBLOCK_SUBINPROGRESS:
5028  return "SUB INPROGRS";
5029  case TBLOCK_SUBRELEASE:
5030  return "SUB RELEASE";
5031  case TBLOCK_SUBCOMMIT:
5032  return "SUB COMMIT";
5033  case TBLOCK_SUBABORT:
5034  return "SUB ABORT";
5035  case TBLOCK_SUBABORT_END:
5036  return "SUB ABORT END";
5038  return "SUB ABRT PEND";
5039  case TBLOCK_SUBRESTART:
5040  return "SUB RESTART";
5042  return "SUB AB RESTRT";
5043  }
5044  return "UNRECOGNIZED";
5045 }
static void CallSubXactCallbacks ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
static

Definition at line 3392 of file xact.c.

References SubXactCallbackItem::arg, SubXactCallbackItem::callback, and SubXactCallbackItem::next.

Referenced by AbortSubTransaction(), CommitSubTransaction(), and StartSubTransaction().

3395 {
3396  SubXactCallbackItem *item;
3397 
3398  for (item = SubXact_callbacks; item; item = item->next)
3399  (*item->callback) (event, mySubid, parentSubid, item->arg);
3400 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:292
struct SubXactCallbackItem * next
Definition: xact.c:287
SubXactCallback callback
Definition: xact.c:288
static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3337 of file xact.c.

References XactCallbackItem::arg, XactCallbackItem::callback, and XactCallbackItem::next.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

3338 {
3339  XactCallbackItem *item;
3340 
3341  for (item = Xact_callbacks; item; item = item->next)
3342  (*item->callback) (event, item->arg);
3343 }
struct XactCallbackItem * next
Definition: xact.c:275
void * arg
Definition: xact.c:277
XactCallback callback
Definition: xact.c:276
static XactCallbackItem * Xact_callbacks
Definition: xact.c:280
static void CheckTransactionChain ( bool  isTopLevel,
bool  throwError,
const char *  stmtType 
)
static

Definition at line 3229 of file xact.c.

References ereport, errcode(), errmsg(), ERROR, IsSubTransaction(), IsTransactionBlock(), and WARNING.

Referenced by RequireTransactionChain(), and WarnNoTransactionChain().

3230 {
3231  /*
3232  * xact block already started?
3233  */
3234  if (IsTransactionBlock())
3235  return;
3236 
3237  /*
3238  * subtransaction?
3239  */
3240  if (IsSubTransaction())
3241  return;
3242 
3243  /*
3244  * inside a function call?
3245  */
3246  if (!isTopLevel)
3247  return;
3248 
3249  ereport(throwError ? ERROR : WARNING,
3250  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3251  /* translator: %s represents an SQL statement name */
3252  errmsg("%s can only be used in transaction blocks",
3253  stmtType)));
3254  return;
3255 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsTransactionBlock(void)
Definition: xact.c:4302
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
bool IsSubTransaction(void)
Definition: xact.c:4374
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void CleanupSubTransaction ( void  )
static

Definition at line 4688 of file xact.c.

References AtSubCleanup_Memory(), AtSubCleanup_Portals(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, NULL, TransactionStateData::parent, PopTransaction(), ResourceOwnerDelete(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, TRANS_ABORT, TRANS_DEFAULT, TransStateAsString(), and WARNING.

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), CommitTransactionCommand(), and RollbackAndReleaseCurrentSubTransaction().

4689 {
4691 
4692  ShowTransactionState("CleanupSubTransaction");
4693 
4694  if (s->state != TRANS_ABORT)
4695  elog(WARNING, "CleanupSubTransaction while in %s state",
4697 
4699 
4702  if (s->curTransactionOwner)
4705 
4707 
4708  s->state = TRANS_DEFAULT;
4709 
4710  PopTransaction();
4711 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
static void PopTransaction(void)
Definition: xact.c:4780
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:687
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
struct TransactionStateData * parent
Definition: xact.c:190
static void ShowTransactionState(const char *str)
Definition: xact.c:4950
SubTransactionId subTransactionId
Definition: xact.c:172
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1007
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1765
#define NULL
Definition: c.h:226
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
#define elog
Definition: elog.h:219
static void CleanupTransaction ( void  )
static

Definition at line 2626 of file xact.c.

References AtCleanup_Memory(), AtCleanup_Portals(), AtEOXact_Snapshot(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, FATAL, TransactionStateData::gucNestLevel, InvalidSubTransactionId, InvalidTransactionId, TransactionStateData::maxChildXids, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, NULL, TransactionStateData::parallelModeLevel, ResourceOwnerDelete(), TransactionStateData::state, TransactionStateData::subTransactionId, TopTransactionResourceOwner, TRANS_ABORT, TRANS_DEFAULT, TransactionStateData::transactionId, TransStateAsString(), and XactTopTransactionId.

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), and CommitTransactionCommand().

2627 {
2629 
2630  /*
2631  * State should still be TRANS_ABORT from AbortTransaction().
2632  */
2633  if (s->state != TRANS_ABORT)
2634  elog(FATAL, "CleanupTransaction: unexpected state %s",
2636 
2637  /*
2638  * do abort cleanup processing
2639  */
2640  AtCleanup_Portals(); /* now safe to release portal memory */
2641  AtEOXact_Snapshot(false); /* and release the transaction's snapshots */
2642 
2643  CurrentResourceOwner = NULL; /* and resource owner */
2649 
2650  AtCleanup_Memory(); /* and transaction memory */
2651 
2654  s->nestingLevel = 0;
2655  s->gucNestLevel = 0;
2656  s->childXids = NULL;
2657  s->nChildXids = 0;
2658  s->maxChildXids = 0;
2659  s->parallelModeLevel = 0;
2660 
2663 
2664  /*
2665  * done with abort processing, set current transaction state back to
2666  * default
2667  */
2668  s->state = TRANS_DEFAULT;
2669 }
TransactionId XactTopTransactionId
Definition: xact.c:107
static TransactionState CurrentTransactionState
Definition: xact.c:231
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:687
static void AtCleanup_Memory(void)
Definition: xact.c:1729
int nParallelCurrentXids
Definition: xact.c:108
void AtCleanup_Portals(void)
Definition: portalmem.c:818
TransactionId * childXids
Definition: xact.c:181
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
#define FATAL
Definition: elog.h:52
void AtEOXact_Snapshot(bool isCommit)
Definition: snapmgr.c:1054
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:172
TransactionId transactionId
Definition: xact.c:171
int parallelModeLevel
Definition: xact.c:189
#define NULL
Definition: c.h:226
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
#define InvalidSubTransactionId
Definition: c.h:399
#define elog
Definition: elog.h:219
void CommandCounterIncrement ( void  )

Definition at line 921 of file xact.c.

References AtCCI_LocalCache(), currentCommandId, currentCommandIdUsed, elog, ereport, errcode(), errmsg(), ERROR, InvalidCommandId, IsInParallelMode(), IsParallelWorker, and SnapshotSetCommandId().

Referenced by _SPI_execute_plan(), acquire_inherited_sample_rows(), AddRoleMems(), AlterPublicationOptions(), AlterRole(), apply_handle_delete(), apply_handle_insert(), apply_handle_update(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAlterColumnType(), ATExecCmd(), ATExecDropColumn(), ATExecDropConstraint(), ATExecSetTableSpace(), CommitSubTransaction(), CommitTransactionCommand(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineType(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DropRole(), EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerInvoke(), EventTriggerSQLDrop(), EventTriggerTableRewrite(), exec_eval_simple_expr(), exec_execute_message(), exec_parse_message(), exec_simple_query(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), execute_sql_string(), ExplainOnePlan(), findTypeInputFunction(), findTypeOutputFunction(), fmgr_sql(), ImportForeignSchema(), index_build(), index_create(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), PreCommit_on_commit_actions(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_relation(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), ri_PerformCheck(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), and StoreConstraints().

922 {
923  /*
924  * If the current value of the command counter hasn't been "used" to mark
925  * tuples, we need not increment it, since there's no need to distinguish
926  * a read-only command from others. This helps postpone command counter
927  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
928  */
930  {
931  /*
932  * Workers synchronize transaction state at the beginning of each
933  * parallel operation, so we can't account for new commands after that
934  * point.
935  */
937  elog(ERROR, "cannot start commands during a parallel operation");
938 
939  currentCommandId += 1;
941  {
942  currentCommandId -= 1;
943  ereport(ERROR,
944  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
945  errmsg("cannot have more than 2^32-2 commands in a transaction")));
946  }
947  currentCommandIdUsed = false;
948 
949  /* Propagate new command ID into static snapshots */
951 
952  /*
953  * Make any catalog changes done by the just-completed command visible
954  * in the local syscache. We obviously don't need to do this after a
955  * read-only command. (But see hacks in inval.c to make real sure we
956  * don't think a command that queued inval messages was read-only.)
957  */
959  }
960 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsInParallelMode(void)
Definition: xact.c:912
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define IsParallelWorker()
Definition: parallel.h:53
#define InvalidCommandId
Definition: c.h:410
static void AtCCI_LocalCache(void)
Definition: xact.c:1363
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:540
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static CommandId currentCommandId
Definition: xact.c:238
static bool currentCommandIdUsed
Definition: xact.c:239
static void CommitSubTransaction ( void  )
static

Definition at line 4435 of file xact.c.

References AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubCommit_childXids(), AtSubCommit_Memory(), AtSubCommit_Notify(), AtSubCommit_Portals(), AtSubCommit_smgr(), AtSubCommit_Snapshot(), CallSubXactCallbacks(), CommandCounterIncrement(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, TransactionStateData::gucNestLevel, IsInParallelMode(), TransactionStateData::nestingLevel, NULL, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PopTransaction(), TransactionStateData::prevXactReadOnly, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, SUBXACT_EVENT_COMMIT_SUB, SUBXACT_EVENT_PRE_COMMIT_SUB, TRANS_COMMIT, TRANS_DEFAULT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, TransStateAsString(), WARNING, XactLockTableDelete(), and XactReadOnly.

Referenced by CommitTransactionCommand(), and ReleaseCurrentSubTransaction().

4436 {
4438 
4439  ShowTransactionState("CommitSubTransaction");
4440 
4441  if (s->state != TRANS_INPROGRESS)
4442  elog(WARNING, "CommitSubTransaction while in %s state",
4444 
4445  /* Pre-commit processing goes here */
4446 
4448  s->parent->subTransactionId);
4449 
4450  /* If in parallel mode, clean up workers and exit parallel mode. */
4451  if (IsInParallelMode())
4452  {
4454  s->parallelModeLevel = 0;
4455  }
4456 
4457  /* Do the actual "commit", such as it is */
4458  s->state = TRANS_COMMIT;
4459 
4460  /* Must CCI to ensure commands of subtransaction are seen as done */
4462 
4463  /*
4464  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4465  * perform that step, if required, as part of the atomic update of the
4466  * whole transaction tree at top level commit or abort.
4467  */
4468 
4469  /* Post-commit cleanup */
4472  AfterTriggerEndSubXact(true);
4477  s->parent->subTransactionId);
4479 
4481  s->parent->subTransactionId);
4482 
4485  true, false);
4487  s->parent->subTransactionId);
4488  AtEOSubXact_Inval(true);
4489  AtSubCommit_smgr();
4490 
4491  /*
4492  * The only lock we actually release here is the subtransaction XID lock.
4493  */
4497 
4498  /*
4499  * Other locks should get transferred to their parent resource owner.
4500  */
4503  true, false);
4506  true, false);
4507 
4508  AtEOXact_GUC(true, s->gucNestLevel);
4511  s->parent->subTransactionId);
4513  s->parent->subTransactionId);
4515  s->parent->subTransactionId);
4517  AtEOSubXact_PgStat(true, s->nestingLevel);
4519 
4520  /*
4521  * We need to restore the upper transaction's read-only state, in case the
4522  * upper is read-write while the child is read-only; GUC will incorrectly
4523  * think it should leave the child state in place.
4524  */
4526 
4531 
4533 
4534  s->state = TRANS_DEFAULT;
4535 
4536  PopTransaction();
4537 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3066
static TransactionState CurrentTransactionState
Definition: xact.c:231
static void AtSubCommit_Memory(void)
Definition: xact.c:1409
static void PopTransaction(void)
Definition: xact.c:4780
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1824
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:687
void AtSubCommit_smgr(void)
Definition: storage.c:451
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:214
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:997
static void AtSubCommit_childXids(void)
Definition: xact.c:1438
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
bool IsInParallelMode(void)
Definition: xact.c:912
struct TransactionStateData * parent
Definition: xact.c:190
void AtSubCommit_Notify(void)
Definition: async.c:1634
static void ShowTransactionState(const char *str)
Definition: xact.c:4950
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5040
SubTransactionId subTransactionId
Definition: xact.c:172
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2034
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:674
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:12540
void CommandCounterIncrement(void)
Definition: xact.c:921
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2458
TransactionId transactionId
Definition: xact.c:171
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3392
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:189
#define NULL
Definition: c.h:226
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
bool prevXactReadOnly
Definition: xact.c:186
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4442
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:979
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:530
#define elog
Definition: elog.h:219
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:3793
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:899
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:860
static void CommitTransaction ( void  )
static

Definition at line 1938 of file xact.c.

References AfterTriggerEndXact(), AfterTriggerFireDeferred(), Assert, AtCommit_ApplyLauncher(), AtCommit_Memory(), AtCommit_Notify(), AtEOXact_Buffers(), AtEOXact_CatCache(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_Inval(), AtEOXact_LargeObject(), AtEOXact_MultiXact(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_Parallel(), AtEOXact_PgStat(), AtEOXact_RelationCache(), AtEOXact_RelationMap(), AtEOXact_SMgr(), AtEOXact_Snapshot(), AtEOXact_SPI(), TransactionStateData::blockState, CallXactCallbacks(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, EnterParallelMode(), TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, InvalidSubTransactionId, InvalidTransactionId, IsInParallelMode(), PGPROC::lxid, TransactionStateData::maxChildXids, MyProc, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, NULL, TransactionStateData::parallelModeLevel, ParallelWorkerReportLastRecEnd(), TransactionStateData::parent, pgstat_report_xact_timestamp(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), PreCommit_on_commit_actions(), PreCommit_Portals(), ProcArrayEndTransaction(), RecordTransactionCommit(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), RESUME_INTERRUPTS, ShowTransactionState(), smgrDoPendingDeletes(), TransactionStateData::state, TransactionStateData::subTransactionId, TBLOCK_PARALLEL_INPROGRESS, TopTransactionResourceOwner, TRANS_COMMIT, TRANS_DEFAULT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransStateAsString(), WARNING, XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_COMMIT, XACT_EVENT_PARALLEL_PRE_COMMIT, XACT_EVENT_PRE_COMMIT, XactLastRecEnd, and XactTopTransactionId.

Referenced by CommitTransactionCommand(), EndParallelWorkerTransaction(), EndRestoreBlobs(), restore_toc_entry(), and RestoreArchive().

1939 {
1941  TransactionId latestXid;
1942  bool is_parallel_worker;
1943 
1944  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
1945 
1946  /* Enforce parallel mode restrictions during parallel worker commit. */
1947  if (is_parallel_worker)
1949 
1950  ShowTransactionState("CommitTransaction");
1951 
1952  /*
1953  * check the current transaction state
1954  */
1955  if (s->state != TRANS_INPROGRESS)
1956  elog(WARNING, "CommitTransaction while in %s state",
1958  Assert(s->parent == NULL);
1959 
1960  /*
1961  * Do pre-commit processing that involves calling user-defined code, such
1962  * as triggers. Since closing cursors could queue trigger actions,
1963  * triggers could open cursors, etc, we have to keep looping until there's
1964  * nothing left to do.
1965  */
1966  for (;;)
1967  {
1968  /*
1969  * Fire all currently pending deferred triggers.
1970  */
1972 
1973  /*
1974  * Close open portals (converting holdable ones into static portals).
1975  * If there weren't any, we are done ... otherwise loop back to check
1976  * if they queued deferred triggers. Lather, rinse, repeat.
1977  */
1978  if (!PreCommit_Portals(false))
1979  break;
1980  }
1981 
1984 
1985  /*
1986  * The remaining actions cannot call any user-defined code, so it's safe
1987  * to start shutting down within-transaction services. But note that most
1988  * of this stuff could still throw an error, which would switch us into
1989  * the transaction-abort path.
1990  */
1991 
1992  /* If we might have parallel workers, clean them up now. */
1993  if (IsInParallelMode())
1994  AtEOXact_Parallel(true);
1995 
1996  /* Shut down the deferred-trigger manager */
1997  AfterTriggerEndXact(true);
1998 
1999  /*
2000  * Let ON COMMIT management do its thing (must happen after closing
2001  * cursors, to avoid dangling-reference problems)
2002  */
2004 
2005  /* close large objects before lower-level cleanup */
2006  AtEOXact_LargeObject(true);
2007 
2008  /*
2009  * Mark serializable transaction as complete for predicate locking
2010  * purposes. This should be done as late as we can put it and still allow
2011  * errors to be raised for failure patterns found at commit.
2012  */
2014 
2015  /*
2016  * Insert notifications sent by NOTIFY commands into the queue. This
2017  * should be late in the pre-commit sequence to minimize time spent
2018  * holding the notify-insertion lock.
2019  */
2020  PreCommit_Notify();
2021 
2022  /* Prevent cancel/die interrupt while cleaning up */
2023  HOLD_INTERRUPTS();
2024 
2025  /* Commit updates to the relation map --- do this as late as possible */
2026  AtEOXact_RelationMap(true);
2027 
2028  /*
2029  * set the current transaction state information appropriately during
2030  * commit processing
2031  */
2032  s->state = TRANS_COMMIT;
2033  s->parallelModeLevel = 0;
2034 
2035  if (!is_parallel_worker)
2036  {
2037  /*
2038  * We need to mark our XIDs as committed in pg_clog. This is where we
2039  * durably commit.
2040  */
2041  latestXid = RecordTransactionCommit();
2042  }
2043  else
2044  {
2045  /*
2046  * We must not mark our XID committed; the parallel master is
2047  * responsible for that.
2048  */
2049  latestXid = InvalidTransactionId;
2050 
2051  /*
2052  * Make sure the master will know about any WAL we wrote before it
2053  * commits.
2054  */
2056  }
2057 
2058  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2059 
2060  /*
2061  * Let others know about no transaction in progress by me. Note that this
2062  * must be done _before_ releasing locks we hold and _after_
2063  * RecordTransactionCommit.
2064  */
2065  ProcArrayEndTransaction(MyProc, latestXid);
2066 
2067  /*
2068  * This is all post-commit cleanup. Note that if an error is raised here,
2069  * it's too late to abort the transaction. This should be just
2070  * noncritical resource releasing.
2071  *
2072  * The ordering of operations is not entirely random. The idea is:
2073  * release resources visible to other backends (eg, files, buffer pins);
2074  * then release locks; then release backend-local resources. We want to
2075  * release locks at the point where any backend waiting for us will see
2076  * our transaction as being fully cleaned up.
2077  *
2078  * Resources that can be associated with individual queries are handled by
2079  * the ResourceOwner mechanism. The other calls here are for backend-wide
2080  * state.
2081  */
2082 
2083  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2084  : XACT_EVENT_COMMIT);
2085 
2088  true, true);
2089 
2090  /* Check we've released all buffer pins */
2091  AtEOXact_Buffers(true);
2092 
2093  /* Clean up the relation cache */
2094  AtEOXact_RelationCache(true);
2095 
2096  /*
2097  * Make catalog changes visible to all backends. This has to happen after
2098  * relcache references are dropped (see comments for
2099  * AtEOXact_RelationCache), but before locks are released (if anyone is
2100  * waiting for lock on a relation we've modified, we want them to know
2101  * about the catalog change before they start using the relation).
2102  */
2103  AtEOXact_Inval(true);
2104 
2106 
2109  true, true);
2112  true, true);
2113 
2114  /*
2115  * Likewise, dropping of files deleted during the transaction is best done
2116  * after releasing relcache and buffer pins. (This is not strictly
2117  * necessary during commit, since such pins should have been released
2118  * already, but this ordering is definitely critical during abort.) Since
2119  * this may take many seconds, also delay until after releasing locks.
2120  * Other backends will observe the attendant catalog changes and not
2121  * attempt to access affected files.
2122  */
2123  smgrDoPendingDeletes(true);
2124 
2125  /* Check we've released all catcache entries */
2126  AtEOXact_CatCache(true);
2127 
2128  AtCommit_Notify();
2129  AtEOXact_GUC(true, 1);
2130  AtEOXact_SPI(true);
2132  AtEOXact_Namespace(true, is_parallel_worker);
2133  AtEOXact_SMgr();
2134  AtEOXact_Files();
2136  AtEOXact_HashTables(true);
2137  AtEOXact_PgStat(true);
2138  AtEOXact_Snapshot(true);
2141 
2147 
2148  AtCommit_Memory();
2149 
2152  s->nestingLevel = 0;
2153  s->gucNestLevel = 0;
2154  s->childXids = NULL;
2155  s->nChildXids = 0;
2156  s->maxChildXids = 0;
2157 
2160 
2161  /*
2162  * done with commit processing, set current transaction state back to
2163  * default
2164  */
2165  s->state = TRANS_DEFAULT;
2166 
2168 }
TransactionId XactTopTransactionId
Definition: xact.c:107
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:306
static TransactionState CurrentTransactionState
Definition: xact.c:231
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:920
uint32 TransactionId
Definition: c.h:393
static void AtCommit_Memory(void)
Definition: xact.c:1382
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3003
XLogRecPtr XactLastRecEnd
Definition: xlog.c:335
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2915
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2398
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void AtEOXact_RelationMap(bool isCommit)
Definition: relmapper.c:456
void AtEOXact_CatCache(bool isCommit)
Definition: catcache.c:548
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:687
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3337
int nParallelCurrentXids
Definition: xact.c:108
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:12498
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:394
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1124
TBlockState blockState
Definition: xact.c:176
TransactionId * childXids
Definition: xact.c:181
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4629
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
void PreCommit_Notify(void)
Definition: async.c:772
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
void AtEOXact_SMgr(void)
Definition: smgr.c:798
bool IsInParallelMode(void)
Definition: xact.c:912
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4320
void AtEOXact_Files(void)
Definition: fd.c:2489
struct TransactionStateData * parent
Definition: xact.c:190
static void ShowTransactionState(const char *str)
Definition: xact.c:4950
void AtEOXact_Snapshot(bool isCommit)
Definition: snapmgr.c:1054
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5040
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:172
#define WARNING
Definition: elog.h:40
void AtCommit_ApplyLauncher(void)
Definition: launcher.c:527
TransactionId transactionId
Definition: xact.c:171
void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
Definition: parallel.c:1156
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4264
int parallelModeLevel
Definition: xact.c:189
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:186
void AtEOXact_MultiXact(void)
Definition: multixact.c:1662
void EnterParallelMode(void)
Definition: xact.c:879
#define InvalidSubTransactionId
Definition: c.h:399
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:630
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:12427
void AtCommit_Notify(void)
Definition: async.c:871
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define elog
Definition: elog.h:219
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1798
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:633
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:918
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3738
void AtEOXact_PgStat(bool isCommit)
Definition: pgstat.c:1951
LocalTransactionId lxid
Definition: proc.h:95
void CommitTransactionCommand ( void  )

Definition at line 2745 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AssertState, TransactionStateData::blockState, BlockStateAsString(), CleanupSubTransaction(), CleanupTransaction(), CommandCounterIncrement(), CommitSubTransaction(), CommitTransaction(), CommitTransactionCommand(), CurrentTransactionState, DefineSavepoint(), elog, ERROR, FATAL, TransactionStateData::name, name, NULL, TransactionStateData::parent, PrepareTransaction(), TransactionStateData::savepointLevel, StartSubTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, and TBLOCK_SUBRESTART.

Referenced by apply_handle_commit(), ApplyLoop(), ApplyWorkerMain(), BeginInternalSubTransaction(), cluster(), CommitTransactionCommand(), DefineIndex(), do_autovacuum(), finish_xact_command(), get_database_list(), get_subscription_list(), IdentifySystem(), index_drop(), initialize_worker_spi(), InitPostgres(), movedb(), ParallelWorkerMain(), ProcessCatchupInterrupt(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleTables(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

2746 {
2748 
2749  switch (s->blockState)
2750  {
2751  /*
2752  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2753  * StartTransactionCommand didn't set the STARTED state
2754  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2755  * by EndParallelWorkerTransaction(), not this function.
2756  */
2757  case TBLOCK_DEFAULT:
2759  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2761  break;
2762 
2763  /*
2764  * If we aren't in a transaction block, just do our usual
2765  * transaction commit, and return to the idle state.
2766  */
2767  case TBLOCK_STARTED:
2770  break;
2771 
2772  /*
2773  * We are completing a "BEGIN TRANSACTION" command, so we change
2774  * to the "transaction block in progress" state and return. (We
2775  * assume the BEGIN did nothing to the database, so we need no
2776  * CommandCounterIncrement.)
2777  */
2778  case TBLOCK_BEGIN:
2780  break;
2781 
2782  /*
2783  * This is the case when we have finished executing a command
2784  * someplace within a transaction block. We increment the command
2785  * counter and return.
2786  */
2787  case TBLOCK_INPROGRESS:
2788  case TBLOCK_SUBINPROGRESS:
2790  break;
2791 
2792  /*
2793  * We are completing a "COMMIT" command. Do it and return to the
2794  * idle state.
2795  */
2796  case TBLOCK_END:
2799  break;
2800 
2801  /*
2802  * Here we are in the middle of a transaction block but one of the
2803  * commands caused an abort so we do nothing but remain in the
2804  * abort state. Eventually we will get a ROLLBACK command.
2805  */
2806  case TBLOCK_ABORT:
2807  case TBLOCK_SUBABORT:
2808  break;
2809 
2810  /*
2811  * Here we were in an aborted transaction block and we just got
2812  * the ROLLBACK command from the user, so clean up the
2813  * already-aborted transaction and return to the idle state.
2814  */
2815  case TBLOCK_ABORT_END:
2818  break;
2819 
2820  /*
2821  * Here we were in a perfectly good transaction block but the user
2822  * told us to ROLLBACK anyway. We have to abort the transaction
2823  * and then clean up.
2824  */
2825  case TBLOCK_ABORT_PENDING:
2826  AbortTransaction();
2829  break;
2830 
2831  /*
2832  * We are completing a "PREPARE TRANSACTION" command. Do it and
2833  * return to the idle state.
2834  */
2835  case TBLOCK_PREPARE:
2838  break;
2839 
2840  /*
2841  * We were just issued a SAVEPOINT inside a transaction block.
2842  * Start a subtransaction. (DefineSavepoint already did
2843  * PushTransaction, so as to have someplace to put the SUBBEGIN
2844  * state.)
2845  */
2846  case TBLOCK_SUBBEGIN:
2849  break;
2850 
2851  /*
2852  * We were issued a RELEASE command, so we end the current
2853  * subtransaction and return to the parent transaction. The parent
2854  * might be ended too, so repeat till we find an INPROGRESS
2855  * transaction or subtransaction.
2856  */
2857  case TBLOCK_SUBRELEASE:
2858  do
2859  {
2861  s = CurrentTransactionState; /* changed by pop */
2862  } while (s->blockState == TBLOCK_SUBRELEASE);
2863 
2866  break;
2867 
2868  /*
2869  * We were issued a COMMIT, so we end the current subtransaction
2870  * hierarchy and perform final commit. We do this by rolling up
2871  * any subtransactions into their parent, which leads to O(N^2)
2872  * operations with respect to resource owners - this isn't that
2873  * bad until we approach a thousands of savepoints but is
2874  * necessary for correctness should after triggers create new
2875  * resource owners.
2876  */
2877  case TBLOCK_SUBCOMMIT:
2878  do
2879  {
2881  s = CurrentTransactionState; /* changed by pop */
2882  } while (s->blockState == TBLOCK_SUBCOMMIT);
2883  /* If we had a COMMIT command, finish off the main xact too */
2884  if (s->blockState == TBLOCK_END)
2885  {
2886  Assert(s->parent == NULL);
2889  }
2890  else if (s->blockState == TBLOCK_PREPARE)
2891  {
2892  Assert(s->parent == NULL);
2895  }
2896  else
2897  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2899  break;
2900 
2901  /*
2902  * The current already-failed subtransaction is ending due to a
2903  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2904  * examine the parent (which could be in any of several states).
2905  */
2906  case TBLOCK_SUBABORT_END:
2909  break;
2910 
2911  /*
2912  * As above, but it's not dead yet, so abort first.
2913  */
2918  break;
2919 
2920  /*
2921  * The current subtransaction is the target of a ROLLBACK TO
2922  * command. Abort and pop it, then start a new subtransaction
2923  * with the same name.
2924  */
2925  case TBLOCK_SUBRESTART:
2926  {
2927  char *name;
2928  int savepointLevel;
2929 
2930  /* save name and keep Cleanup from freeing it */
2931  name = s->name;
2932  s->name = NULL;
2933  savepointLevel = s->savepointLevel;
2934 
2937 
2939  s = CurrentTransactionState; /* changed by push */
2940  s->name = name;
2941  s->savepointLevel = savepointLevel;
2942 
2943  /* This is the same as TBLOCK_SUBBEGIN case */
2947  }
2948  break;
2949 
2950  /*
2951  * Same as above, but the subtransaction had already failed, so we
2952  * don't need AbortSubTransaction.
2953  */
2955  {
2956  char *name;
2957  int savepointLevel;
2958 
2959  /* save name and keep Cleanup from freeing it */
2960  name = s->name;
2961  s->name = NULL;
2962  savepointLevel = s->savepointLevel;
2963 
2965 
2967  s = CurrentTransactionState; /* changed by push */
2968  s->name = name;
2969  s->savepointLevel = savepointLevel;
2970 
2971  /* This is the same as TBLOCK_SUBBEGIN case */
2975  }
2976  break;
2977  }
2978 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
#define AssertState(condition)
Definition: c.h:673
void CommitTransactionCommand(void)
Definition: xact.c:2745
TBlockState blockState
Definition: xact.c:176
static void PrepareTransaction(void)
Definition: xact.c:2177
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4435
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2626
struct TransactionStateData * parent
Definition: xact.c:190
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5001
static void AbortSubTransaction(void)
Definition: xact.c:4543
void DefineSavepoint(char *name)
Definition: xact.c:3746
void CommandCounterIncrement(void)
Definition: xact.c:921
static void AbortTransaction(void)
Definition: xact.c:2446
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static void CleanupSubTransaction(void)
Definition: xact.c:4688
const char * name
Definition: encode.c:521
static void CommitTransaction(void)
Definition: xact.c:1938
#define elog
Definition: elog.h:219
static void StartSubTransaction(void)
Definition: xact.c:4397
void DefineSavepoint ( char *  name)

Definition at line 3746 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), MemoryContextStrdup(), TransactionStateData::name, PushTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and TopTransactionContext.

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

3747 {
3749 
3750  /*
3751  * Workers synchronize transaction state at the beginning of each parallel
3752  * operation, so we can't account for new subtransactions after that
3753  * point. (Note that this check will certainly error out if s->blockState
3754  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3755  * below.)
3756  */
3757  if (IsInParallelMode())
3758  ereport(ERROR,
3759  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3760  errmsg("cannot define savepoints during a parallel operation")));
3761 
3762  switch (s->blockState)
3763  {
3764  case TBLOCK_INPROGRESS:
3765  case TBLOCK_SUBINPROGRESS:
3766  /* Normal subtransaction start */
3767  PushTransaction();
3768  s = CurrentTransactionState; /* changed by push */
3769 
3770  /*
3771  * Savepoint names, like the TransactionState block itself, live
3772  * in TopTransactionContext.
3773  */
3774  if (name)
3776  break;
3777 
3778  /* These cases are invalid. */
3779  case TBLOCK_DEFAULT:
3780  case TBLOCK_STARTED:
3781  case TBLOCK_BEGIN:
3783  case TBLOCK_SUBBEGIN:
3784  case TBLOCK_END:
3785  case TBLOCK_SUBRELEASE:
3786  case TBLOCK_SUBCOMMIT:
3787  case TBLOCK_ABORT:
3788  case TBLOCK_SUBABORT:
3789  case TBLOCK_ABORT_END:
3790  case TBLOCK_SUBABORT_END:
3791  case TBLOCK_ABORT_PENDING:
3793  case TBLOCK_SUBRESTART:
3795  case TBLOCK_PREPARE:
3796  elog(FATAL, "DefineSavepoint: unexpected state %s",
3798  break;
3799  }
3800 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
MemoryContext TopTransactionContext
Definition: mcxt.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:176
bool IsInParallelMode(void)
Definition: xact.c:912
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5001
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4721
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1152
#define elog
Definition: elog.h:219
void EndParallelWorkerTransaction ( void  )

Definition at line 4938 of file xact.c.

References Assert, TransactionStateData::blockState, CommitTransaction(), TBLOCK_DEFAULT, and TBLOCK_PARALLEL_INPROGRESS.

Referenced by ParallelWorkerMain().

4939 {
4943 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
#define Assert(condition)
Definition: c.h:670
static void CommitTransaction(void)
Definition: xact.c:1938
bool EndTransactionBlock ( void  )

Definition at line 3523 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, NULL, TransactionStateData::parent, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and WARNING.

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

3524 {
3526  bool result = false;
3527 
3528  switch (s->blockState)
3529  {
3530  /*
3531  * We are in a transaction block, so tell CommitTransactionCommand
3532  * to COMMIT.
3533  */
3534  case TBLOCK_INPROGRESS:
3535  s->blockState = TBLOCK_END;
3536  result = true;
3537  break;
3538 
3539  /*
3540  * We are in a failed transaction block. Tell
3541  * CommitTransactionCommand it's time to exit the block.
3542  */
3543  case TBLOCK_ABORT:
3545  break;
3546 
3547  /*
3548  * We are in a live subtransaction block. Set up to subcommit all
3549  * open subtransactions and then commit the main transaction.
3550  */
3551  case TBLOCK_SUBINPROGRESS:
3552  while (s->parent != NULL)
3553  {
3554  if (s->blockState == TBLOCK_SUBINPROGRESS)
3556  else
3557  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3559  s = s->parent;
3560  }
3561  if (s->blockState == TBLOCK_INPROGRESS)
3562  s->blockState = TBLOCK_END;
3563  else
3564  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3566  result = true;
3567  break;
3568 
3569  /*
3570  * Here we are inside an aborted subtransaction. Treat the COMMIT
3571  * as ROLLBACK: set up to abort everything and exit the main
3572  * transaction.
3573  */
3574  case TBLOCK_SUBABORT:
3575  while (s->parent != NULL)
3576  {
3577  if (s->blockState == TBLOCK_SUBINPROGRESS)
3579  else if (s->blockState == TBLOCK_SUBABORT)
3581  else
3582  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3584  s = s->parent;
3585  }
3586  if (s->blockState == TBLOCK_INPROGRESS)
3588  else if (s->blockState == TBLOCK_ABORT)
3590  else
3591  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3593  break;
3594 
3595  /*
3596  * The user issued COMMIT when not inside a transaction. Issue a
3597  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3598  * CommitTransactionCommand() will then close the transaction and
3599  * put us back into the default state.
3600  */
3601  case TBLOCK_STARTED:
3602  ereport(WARNING,
3603  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3604  errmsg("there is no transaction in progress")));
3605  result = true;
3606  break;
3607 
3608  /*
3609  * The user issued a COMMIT that somehow ran inside a parallel
3610  * worker. We can't cope with that.
3611  */
3613  ereport(FATAL,
3614  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3615  errmsg("cannot commit during a parallel operation")));
3616  break;
3617 
3618  /* These cases are invalid. */
3619  case TBLOCK_DEFAULT:
3620  case TBLOCK_BEGIN:
3621  case TBLOCK_SUBBEGIN:
3622  case TBLOCK_END:
3623  case TBLOCK_SUBRELEASE:
3624  case TBLOCK_SUBCOMMIT:
3625  case TBLOCK_ABORT_END:
3626  case TBLOCK_SUBABORT_END:
3627  case TBLOCK_ABORT_PENDING:
3629  case TBLOCK_SUBRESTART:
3631  case TBLOCK_PREPARE:
3632  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3634  break;
3635  }
3636 
3637  return result;
3638 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:176
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:190
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5001
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void EnterParallelMode ( void  )

Definition at line 879 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

Referenced by CommitTransaction(), ExecutePlan(), and ParallelWorkerMain().

880 {
882 
883  Assert(s->parallelModeLevel >= 0);
884 
885  ++s->parallelModeLevel;
886 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
int parallelModeLevel
Definition: xact.c:189
#define Assert(condition)
Definition: c.h:670
Size EstimateTransactionStateSpace ( void  )

Definition at line 4814 of file xact.c.

References add_size(), mul_size(), TransactionStateData::nChildXids, nParallelCurrentXids, NULL, TransactionStateData::parent, TransactionStateData::transactionId, and TransactionIdIsValid.

Referenced by InitializeParallelDSM().

4815 {
4816  TransactionState s;
4817  Size nxids = 6; /* iso level, deferrable, top & current XID,
4818  * command counter, XID count */
4819 
4820  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4821  {
4823  nxids = add_size(nxids, 1);
4824  nxids = add_size(nxids, s->nChildXids);
4825  }
4826 
4827  nxids = add_size(nxids, nParallelCurrentXids);
4828  return mul_size(nxids, sizeof(TransactionId));
4829 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
uint32 TransactionId
Definition: c.h:393
int nParallelCurrentXids
Definition: xact.c:108
struct TransactionStateData * parent
Definition: xact.c:190
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
TransactionId transactionId
Definition: xact.c:171
#define NULL
Definition: c.h:226
size_t Size
Definition: c.h:352
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ExitParallelMode ( void  )

Definition at line 892 of file xact.c.

References Assert, CurrentTransactionState, ParallelContextActive(), and TransactionStateData::parallelModeLevel.

Referenced by ExecutePlan(), and ParallelWorkerMain().

893 {
895 
896  Assert(s->parallelModeLevel > 0);
898 
899  --s->parallelModeLevel;
900 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
bool ParallelContextActive(void)
Definition: parallel.c:681
int parallelModeLevel
Definition: xact.c:189
#define Assert(condition)
Definition: c.h:670
void ForceSyncCommit ( void  )

Definition at line 969 of file xact.c.

References forceSyncCommit.

Referenced by createdb(), CreateTableSpace(), dropdb(), DropTableSpace(), and movedb().

970 {
971  forceSyncCommit = true;
972 }
static bool forceSyncCommit
Definition: xact.c:261
CommandId GetCurrentCommandId ( bool  used)

Definition at line 686 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and TransactionStateData::parallelModeLevel.

Referenced by ATRewriteTable(), CopyFrom(), GetSnapshotData(), intorel_startup(), pgrowlocks(), RegisterRelcacheInvalidation(), RelationFindReplTupleByIndex(), RelationFindReplTupleSeq(), simple_heap_delete(), simple_heap_insert(), simple_heap_update(), standard_ExecutorStart(), toast_save_datum(), transientrel_startup(), and UpdateActiveSnapshotCommandId().

687 {
688  /* this is global to a transaction, not subtransaction-local */
689  if (used)
690  {
691  /*
692  * Forbid setting currentCommandIdUsed in parallel mode, because we
693  * have no provision for communicating this back to the master. We
694  * could relax this restriction when currentCommandIdUsed was already
695  * true at the start of the parallel operation.
696  */
698  currentCommandIdUsed = true;
699  }
700  return currentCommandId;
701 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
int parallelModeLevel
Definition: xact.c:189
#define Assert(condition)
Definition: c.h:670
static CommandId currentCommandId
Definition: xact.c:238
static bool currentCommandIdUsed
Definition: xact.c:239
TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 716 of file xact.c.

References stmtStartTimestamp.

Referenced by check_log_duration(), CreatePortal(), pgstat_report_activity(), statement_timestamp(), and StorePreparedStatement().

717 {
718  return stmtStartTimestamp;
719 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:249
TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 433 of file xact.c.

References TransactionStateData::transactionId.

Referenced by RecordTransactionAbort(), ReorderBufferCommit(), and XLogRecordAssemble().

434 {
436 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TransactionId transactionId
Definition: xact.c:171
TimestampTz GetCurrentTransactionStartTimestamp ( void  )
TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 728 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

729 {
730  if (xactStopTimestamp != 0)
731  return xactStopTimestamp;
732  return GetCurrentTimestamp();
733 }
static TimestampTz xactStopTimestamp
Definition: xact.c:250
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1688
TransactionId GetStableLatestTransactionId ( void  )

Definition at line 460 of file xact.c.

References Assert, GetTopTransactionIdIfAny(), InvalidLocalTransactionId, InvalidTransactionId, PGPROC::lxid, MyProc, ReadNewTransactionId(), and TransactionIdIsValid.

Referenced by xid_age().

461 {
463  static TransactionId stablexid = InvalidTransactionId;
464 
465  if (lxid != MyProc->lxid)
466  {
467  lxid = MyProc->lxid;
468  stablexid = GetTopTransactionIdIfAny();
469  if (!TransactionIdIsValid(stablexid))
470  stablexid = ReadNewTransactionId();
471  }
472 
473  Assert(TransactionIdIsValid(stablexid));
474 
475  return stablexid;
476 }
uint32 TransactionId
Definition: c.h:393
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
uint32 LocalTransactionId
Definition: c.h:395
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:403
#define Assert(condition)
Definition: c.h:670
#define InvalidLocalTransactionId
Definition: lock.h:70
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:95
TransactionId GetTopTransactionId ( void  )

Definition at line 388 of file xact.c.

References AssignTransactionId(), TransactionIdIsValid, and XactTopTransactionId.

Referenced by AlterSequence(), AssignTransactionId(), AtEOXact_Snapshot(), do_setval(), ExportSnapshot(), fill_seq_with_data(), log_heap_new_cid(), LogAccessExclusiveLock(), LogAccessExclusiveLockPrepare(), nextval_internal(), SnapBuildExportSnapshot(), and txid_current().

389 {
392  return XactTopTransactionId;
393 }
TransactionId XactTopTransactionId
Definition: xact.c:107
static TransactionStateData TopTransactionStateData
Definition: xact.c:200
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:488
bool IsAbortedTransactionBlockState ( void  )
bool IsInTransactionChain ( bool  isTopLevel)

Definition at line 3268 of file xact.c.

References TransactionStateData::blockState, IsSubTransaction(), IsTransactionBlock(), TBLOCK_DEFAULT, and TBLOCK_STARTED.

Referenced by vacuum().

3269 {
3270  /*
3271  * Return true on same conditions that would make PreventTransactionChain
3272  * error out
3273  */
3274  if (IsTransactionBlock())
3275  return true;
3276 
3277  if (IsSubTransaction())
3278  return true;
3279 
3280  if (!isTopLevel)
3281  return true;
3282 
3285  return true;
3286 
3287  return false;
3288 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
bool IsTransactionBlock(void)
Definition: xact.c:4302
bool IsSubTransaction(void)
Definition: xact.c:4374
bool IsSubTransaction ( void  )
bool IsTransactionBlock ( void  )

Definition at line 4302 of file xact.c.

References TransactionStateData::blockState, CurrentTransactionState, TBLOCK_DEFAULT, and TBLOCK_STARTED.

Referenced by CheckTransactionChain(), IsInTransactionChain(), and PreventTransactionChain().

4303 {
4305 
4307  return false;
4308 
4309  return true;
4310 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
bool IsTransactionOrTransactionBlock ( void  )

Definition at line 4320 of file xact.c.

References TransactionStateData::blockState, CurrentTransactionState, and TBLOCK_DEFAULT.

Referenced by PostgresMain(), ProcessCatchupInterrupt(), ProcessNotifyInterrupt(), RecoveryConflictInterrupt(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), SnapBuildExportSnapshot(), and StartupDecodingContext().

4321 {
4323 
4324  if (s->blockState == TBLOCK_DEFAULT)
4325  return false;
4326 
4327  return true;
4328 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TBlockState blockState
Definition: xact.c:176
bool IsTransactionState ( void  )

Definition at line 349 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit(), apply_handle_origin(), check_client_encoding(), check_default_tablespace(), check_role(), check_session_authorization(), check_temp_tablespaces(), check_transaction_read_only(), check_TSCurrentConfig(), check_XactIsoLevel(), CreateInitDecodingContext(), ensure_transaction(), LogLogicalMessage(), pg_do_encoding_conversion(), PrepareClientEncoding(), PrepareTempTablespaces(), RelationClearRelation(), RelationIdGetRelation(), RelationInitPhysicalAddr(), replorigin_create(), replorigin_drop(), SearchCatCache(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

350 {
352 
353  /*
354  * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
355  * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
356  * TRANS_PREPARE since it might be too soon or too late within those
357  * transition states to do anything interesting. Hence, the only "valid"
358  * state is TRANS_INPROGRESS.
359  */
360  return (s->state == TRANS_INPROGRESS);
361 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TransState state
Definition: xact.c:175
void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 444 of file xact.c.

References TransactionStateData::didLogXid, TransactionStateData::transactionId, and TransactionIdIsValid.

Referenced by XLogInsertRecord().

445 {
448 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
TransactionId transactionId
Definition: xact.c:171
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void PopTransaction ( void  )
static

Definition at line 4780 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, FATAL, MemoryContextSwitchTo(), TransactionStateData::name, NULL, TransactionStateData::parent, pfree(), TransactionStateData::state, TRANS_DEFAULT, TransStateAsString(), and WARNING.

Referenced by CleanupSubTransaction(), and CommitSubTransaction().

4781 {
4783 
4784  if (s->state != TRANS_DEFAULT)
4785  elog(WARNING, "PopTransaction while in %s state",
4787 
4788  if (s->parent == NULL)
4789  elog(FATAL, "PopTransaction with no parent");
4790 
4792 
4793  /* Let's just make sure CurTransactionContext is good */
4796 
4797  /* Ditto for ResourceOwner links */
4800 
4801  /* Free the old child structure */
4802  if (s->name)
4803  pfree(s->name);
4804  pfree(s);
4805 }
static TransactionState CurrentTransactionState
Definition: xact.c:231
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
TransState state
Definition: xact.c:175
ResourceOwner curTransactionOwner
Definition: xact.c:180
void pfree(void *pointer)
Definition: mcxt.c:992
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:190
MemoryContext curTransactionContext
Definition: xact.c:179
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:226
static const char * TransStateAsString(TransState state)
Definition: xact.c:5052
#define elog
Definition: elog.h:219
static void PrepareTransaction ( void  )
static

Definition at line 2177 of file xact.c.

References AfterTriggerEndXact(), AfterTriggerFireDeferred(), Assert, AtCommit_Memory(), AtEOXact_Buffers(), AtEOXact_CatCache(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_LargeObject(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_RelationCache(), AtEOXact_SMgr(), AtEOXact_Snapshot(), AtEOXact_SPI(), AtPrepare_Locks(), AtPrepare_MultiXact(), AtPrepare_Notify(), AtPrepare_PgStat(), AtPrepare_PredicateLocks(), AtPrepare_RelationMap(), BufmgrCommit(), CallXactCallbacks(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, EndPrepare(), ereport, errcode(), errmsg(), ERROR, GetCurrentTimestamp(), GetCurrentTransactionId(), GetUserId(), TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, InvalidSubTransactionId, InvalidTransactionId, IsInParallelMode(), MarkAsPreparing(), TransactionStateData::maxChildXids, MyDatabaseId, MyProc, MyXactAccessedTempRel, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, NULL, TransactionStateData::parent, pgstat_report_xact_timestamp(), PostPrepare_Inval(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_PgStat(), PostPrepare_PredicateLocks(), PostPrepare_smgr(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_on_commit_actions(), PreCommit_Portals(), prepareGID, ProcArrayClearTransaction(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), RESUME_INTERRUPTS, ShowTransactionState(), StartPrepare(), TransactionStateData::state, TransactionStateData::subTransactionId, TopTransactionResourceOwner, TRANS_DEFAULT, TRANS_INPROGRESS, TRANS_PREPARE, TransactionStateData::transactionId, TransStateAsString(), WARNING, XACT_EVENT_PRE_PREPARE, XACT_EVENT_PREPARE, XactHasExportedSnapshots(), XactLastRecEnd, and XactTopTransactionId.

Referenced by CommitTransactionCommand().

2178 {
2181  GlobalTransaction gxact;
2182  TimestampTz prepared_at;
2183 
2185 
2186  ShowTransactionState("PrepareTransaction");
2187 
2188  /*
2189  * check the current transaction state
2190  */
2191  if (s->state != TRANS_INPROGRESS)
2192  elog(WARNING, "PrepareTransaction while in %s state",
2194  Assert(s->parent == NULL);
2195 
2196  /*
2197  * Do pre-commit processing that involves calling user-defined code, such
2198  * as triggers. Since closing cursors could queue trigger actions,
2199  * triggers could open cursors, etc, we have to keep looping until there's
2200  * nothing left to do.
2201  */
2202  for (;;)
2203  {
2204  /*
2205  * Fire all currently pending deferred triggers.
2206  */
2208 
2209  /*
2210  * Close open portals (converting holdable ones into static portals).
2211  * If there weren't any, we are done ... otherwise loop back to check
2212  * if they queued deferred triggers. Lather, rinse, repeat.
2213  */
2214  if (!PreCommit_Portals(true))
2215  break;
2216  }
2217 
2219 
2220  /*
2221  * The remaining actions cannot call any user-defined code, so it's safe
2222  * to start shutting down within-transaction services. But note that most
2223  * of this stuff could still throw an error, which would switch us into
2224  * the transaction-abort path.
2225  */
2226 
2227  /* Shut down the deferred-trigger manager */
2228  AfterTriggerEndXact(true);
2229 
2230  /*
2231  * Let ON COMMIT management do its thing (must happen after closing
2232  * cursors, to avoid dangling-reference problems)
2233  */
2235 
2236  /* close large objects before lower-level cleanup */
2237  AtEOXact_LargeObject(true);
2238 
2239  /*
2240  * Mark serializable transaction as complete for predicate locking
2241  * purposes. This should be done as late as we can put it and still allow
2242  * errors to be raised for failure patterns found at commit.
2243  */
2245 
2246  /* NOTIFY will be handled below */
2247 
2248  /*
2249  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2250  * this transaction. Having the prepared xact hold locks on another
2251  * backend's temp table seems a bad idea --- for instance it would prevent
2252  * the backend from exiting. There are other problems too, such as how to
2253  * clean up the source backend's local buffers and ON COMMIT state if the
2254  * prepared xact includes a DROP of a temp table.
2255  *
2256  * We must check this after executing any ON COMMIT actions, because they
2257  * might still access a temp relation.
2258  *
2259  * XXX In principle this could be relaxed to allow some useful special
2260  * cases, such as a temp table created and dropped all within the
2261  * transaction. That seems to require much more bookkeeping though.
2262  */
2264  ereport(ERROR,
2265  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2266  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2267 
2268  /*
2269  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2270  * supported if we added cleanup logic to twophase.c, but for now it
2271  * doesn't seem worth the trouble.
2272  */
2274  ereport(ERROR,
2275  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2276  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2277 
2278  /* Prevent cancel/die interrupt while cleaning up */
2279  HOLD_INTERRUPTS();
2280 
2281  /*
2282  * set the current transaction state information appropriately during
2283  * prepare processing
2284  */
2285  s->state = TRANS_PREPARE;
2286 
2287  prepared_at = GetCurrentTimestamp();
2288 
2289  /* Tell bufmgr and smgr to prepare for commit */
2290  BufmgrCommit();
2291 
2292  /*
2293  * Reserve the GID for this transaction. This could fail if the requested
2294  * GID is invalid or already in use.
2295  */
2296  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2297  GetUserId(), MyDatabaseId);
2298  prepareGID = NULL;
2299 
2300  /*
2301  * Collect data for the 2PC state file. Note that in general, no actual
2302  * state change should happen in the called modules during this step,
2303  * since it's still possible to fail before commit, and in that case we
2304  * want transaction abort to be able to clean up. (In particular, the
2305  * AtPrepare routines may error out if they find cases they cannot
2306  * handle.) State cleanup should happen in the PostPrepare routines
2307  * below. However, some modules can go ahead and clear state here because
2308  * they wouldn't do anything with it during abort anyway.
2309  *
2310  * Note: because the 2PC state file records will be replayed in the same
2311  * order they are made, the order of these calls has to match the order in
2312  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2313  * PREPARED; in particular, pay attention to whether things should happen
2314  * before or after releasing the transaction's locks.
2315  */
2316  StartPrepare(gxact);
2317 
2318  AtPrepare_Notify();
2319  AtPrepare_Locks();
2321  AtPrepare_PgStat();
2324 
2325  /*
2326  * Here is where we really truly prepare.
2327  *
2328  * We have to record transaction prepares even if we didn't make any
2329  * updates, because the transaction manager might get confused if we lose
2330  * a global transaction.
2331  */
2332  EndPrepare(gxact);
2333 
2334  /*
2335  * Now we clean up backend-internal state and release internal resources.
2336  */
2337 
2338  /* Reset XactLastRecEnd until the next transaction writes something */
2339  XactLastRecEnd = 0;
2340 
2341  /*
2342  * Let others know about no transaction in progress by me. This has to be
2343  * done *after* the prepared transaction has been marked valid, else
2344  * someone may think it is unlocked and recyclable.
2345  */
2347 
2348  /*
2349  * In normal commit-processing, this is all non-critical post-transaction
2350  * cleanup. When the transaction is prepared, however, it's important
2351  * that the locks and other per-backend resources are transferred to the
2352  * prepared transaction's PGPROC entry. Note that if an error is raised
2353  * here, it's too late to abort the transaction. XXX: This probably should
2354  * be in a critical section, to force a PANIC if any of this fails, but
2355  * that cure could be worse than the disease.
2356  */
2357 
2359 
2362  true, true);
2363 
2364  /* Check we've released all buffer pins */
2365  AtEOXact_Buffers(true);
2366 
2367  /* Clean up the relation cache */
2368  AtEOXact_RelationCache(true);
2369 
2370  /* notify doesn't need a postprepare call */
2371 
2373 
2375 
2376  PostPrepare_smgr();
2377 
2378  PostPrepare_MultiXact(xid);
2379 
2380  PostPrepare_Locks(xid);
2382 
2385  true, true);
2388  true, true);
2389 
2390  /*
2391  * Allow another backend to finish the transaction. After
2392  * PostPrepare_Twophase(), the transaction is completely detached from our
2393  * backend. The rest is just non-critical cleanup of backend-local state.
2394  */
2396 
2397  /* Check we've released all catcache entries */
2398  AtEOXact_CatCache(true);
2399 
2400  /* PREPARE acts the same as COMMIT as far as GUC is concerned */
2401  AtEOXact_GUC(true, 1);
2402  AtEOXact_SPI(true);
2404  AtEOXact_Namespace(true, false);
2405  AtEOXact_SMgr();
2406  AtEOXact_Files();
2408  AtEOXact_HashTables(true);
2409  /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
2410  AtEOXact_Snapshot(true);
2412 
2418 
2419  AtCommit_Memory();
2420 
2423  s->nestingLevel = 0;
2424  s->gucNestLevel = 0;
2425  s->childXids = NULL;
2426  s->nChildXids = 0;
2427  s->maxChildXids = 0;
2428 
2431 
2432  /*
2433  * done with 1st phase commit processing, set current transaction state
2434  * back to default
2435  */
2436  s->state = TRANS_DEFAULT;
2437 
2439 }
TransactionId XactTopTransactionId
Definition: xact.c:107
void PostPrepare_PredicateLocks(TransactionId xid)
Definition: predicate.c:4791
void StartPrepare(GlobalTransaction gxact)
Definition: twophase.c:948
static TransactionState CurrentTransactionState
Definition: xact.c:231
bool XactHasExportedSnapshots(void)
Definition: snapmgr.c:1557
void AtPrepare_MultiXact(void)
Definition: multixact.c:1691
uint32 TransactionId
Definition: c.h:393
static void AtCommit_Memory(void)
Definition: xact.c:1382
Oid GetUserId(void)
Definition: miscinit.c:283
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3003
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1688
XLogRecPtr XactLastRecEnd
Definition: xlog.c:335
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2915