PostgreSQL Source Code  git master
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/namespace.h"
#include "catalog/pg_enum.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 "pg_trace.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/md.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 dependency graph for xact.c:

Go to the source code of this file.

Data Structures

struct  TransactionStateData
 
struct  SerializedTransactionState
 
struct  XactCallbackItem
 
struct  SubXactCallbackItem
 

Macros

#define SerializedTransactionStateHeaderSize   offsetof(SerializedTransactionState, parallelCurrentXids)
 

Typedefs

typedef enum TransState TransState
 
typedef enum TBlockState TBlockState
 
typedef struct TransactionStateData TransactionStateData
 
typedef TransactionStateDataTransactionState
 
typedef struct SerializedTransactionState SerializedTransactionState
 
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_IMPLICIT_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 CheckTransactionBlock (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)
 
FullTransactionId GetTopFullTransactionId (void)
 
FullTransactionId GetTopFullTransactionIdIfAny (void)
 
FullTransactionId GetCurrentFullTransactionId (void)
 
FullTransactionId GetCurrentFullTransactionIdIfAny (void)
 
void MarkCurrentTransactionIdLoggedIfAny (void)
 
TransactionId GetStableLatestTransactionId (void)
 
SubTransactionId GetCurrentSubTransactionId (void)
 
bool SubTransactionIsActive (SubTransactionId subxid)
 
CommandId GetCurrentCommandId (bool used)
 
void SetParallelStartTimestamps (TimestampTz xact_ts, TimestampTz stmt_ts)
 
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 SaveTransactionCharacteristics (void)
 
void RestoreTransactionCharacteristics (void)
 
void CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void PreventInTransactionBlock (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionBlock (bool isTopLevel, const char *stmtType)
 
void RequireTransactionBlock (bool isTopLevel, const char *stmtType)
 
bool IsInTransactionBlock (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 (const char *gid)
 
bool EndTransactionBlock (bool chain)
 
void UserAbortTransactionBlock (bool chain)
 
void BeginImplicitTransactionBlock (void)
 
void EndImplicitTransactionBlock (void)
 
void DefineSavepoint (const char *name)
 
void ReleaseSavepoint (const char *name)
 
void RollbackToSavepoint (const char *name)
 
void BeginInternalSubTransaction (const 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, int xactflags, TransactionId twophase_xid, const char *twophase_gid)
 
XLogRecPtr XactLogAbortRecord (TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int xactflags, TransactionId twophase_xid, const char *twophase_gid)
 
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
 
FullTransactionId XactTopFullTransactionId = {InvalidTransactionId}
 
int nParallelCurrentXids = 0
 
TransactionIdParallelCurrentXids
 
int MyXactFlags
 
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
 
bool xact_is_sampled = false
 
static MemoryContext TransactionAbortContext = NULL
 
static XactCallbackItemXact_callbacks = NULL
 
static SubXactCallbackItemSubXact_callbacks = NULL
 
static int save_XactIsoLevel
 
static bool save_XactReadOnly
 
static bool save_XactDeferrable
 

Macro Definition Documentation

◆ SerializedTransactionStateHeaderSize

#define SerializedTransactionStateHeaderSize   offsetof(SerializedTransactionState, parallelCurrentXids)

Definition at line 214 of file xact.c.

Referenced by EstimateTransactionStateSpace(), and SerializeTransactionState().

Typedef Documentation

◆ SerializedTransactionState

◆ SubXactCallbackItem

◆ TBlockState

typedef enum TBlockState TBlockState

◆ TransactionState

Definition at line 196 of file xact.c.

◆ TransactionStateData

◆ TransState

typedef enum TransState TransState

◆ XactCallbackItem

Enumeration Type Documentation

◆ TBlockState

Enumerator
TBLOCK_DEFAULT 
TBLOCK_STARTED 
TBLOCK_BEGIN 
TBLOCK_INPROGRESS 
TBLOCK_IMPLICIT_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 139 of file xact.c.

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

◆ TransState

enum TransState
Enumerator
TRANS_DEFAULT 
TRANS_START 
TRANS_INPROGRESS 
TRANS_COMMIT 
TRANS_ABORT 
TRANS_PREPARE 

Definition at line 123 of file xact.c.

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

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3162 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_IMPLICIT_INPROGRESS, 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 _SPI_rollback(), AbortCurrentTransaction(), AutoVacLauncherMain(), PostgresMain(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), and SnapBuildClearExportedSnapshot().

3163 {
3165 
3166  switch (s->blockState)
3167  {
3168  case TBLOCK_DEFAULT:
3169  if (s->state == TRANS_DEFAULT)
3170  {
3171  /* we are idle, so nothing to do */
3172  }
3173  else
3174  {
3175  /*
3176  * We can get here after an error during transaction start
3177  * (state will be TRANS_START). Need to clean up the
3178  * incompletely started transaction. First, adjust the
3179  * low-level state to suppress warning message from
3180  * AbortTransaction.
3181  */
3182  if (s->state == TRANS_START)
3183  s->state = TRANS_INPROGRESS;
3184  AbortTransaction();
3186  }
3187  break;
3188 
3189  /*
3190  * If we aren't in a transaction block, we just do the basic abort
3191  * & cleanup transaction. For this purpose, we treat an implicit
3192  * transaction block as if it were a simple statement.
3193  */
3194  case TBLOCK_STARTED:
3196  AbortTransaction();
3199  break;
3200 
3201  /*
3202  * If we are in TBLOCK_BEGIN it means something screwed up right
3203  * after reading "BEGIN TRANSACTION". We assume that the user
3204  * will interpret the error as meaning the BEGIN failed to get him
3205  * into a transaction block, so we should abort and return to idle
3206  * state.
3207  */
3208  case TBLOCK_BEGIN:
3209  AbortTransaction();
3212  break;
3213 
3214  /*
3215  * We are somewhere in a transaction block and we've gotten a
3216  * failure, so we abort the transaction and set up the persistent
3217  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3218  */
3219  case TBLOCK_INPROGRESS:
3221  AbortTransaction();
3222  s->blockState = TBLOCK_ABORT;
3223  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3224  break;
3225 
3226  /*
3227  * Here, we failed while trying to COMMIT. Clean up the
3228  * transaction and return to idle state (we do not want to stay in
3229  * the transaction).
3230  */
3231  case TBLOCK_END:
3232  AbortTransaction();
3235  break;
3236 
3237  /*
3238  * Here, we are already in an aborted transaction state and are
3239  * waiting for a ROLLBACK, but for some reason we failed again! So
3240  * we just remain in the abort state.
3241  */
3242  case TBLOCK_ABORT:
3243  case TBLOCK_SUBABORT:
3244  break;
3245 
3246  /*
3247  * We are in a failed transaction and we got the ROLLBACK command.
3248  * We have already aborted, we just need to cleanup and go to idle
3249  * state.
3250  */
3251  case TBLOCK_ABORT_END:
3254  break;
3255 
3256  /*
3257  * We are in a live transaction and we got a ROLLBACK command.
3258  * Abort, cleanup, go to idle state.
3259  */
3260  case TBLOCK_ABORT_PENDING:
3261  AbortTransaction();
3264  break;
3265 
3266  /*
3267  * Here, we failed while trying to PREPARE. Clean up the
3268  * transaction and return to idle state (we do not want to stay in
3269  * the transaction).
3270  */
3271  case TBLOCK_PREPARE:
3272  AbortTransaction();
3275  break;
3276 
3277  /*
3278  * We got an error inside a subtransaction. Abort just the
3279  * subtransaction, and go to the persistent SUBABORT state until
3280  * we get ROLLBACK.
3281  */
3282  case TBLOCK_SUBINPROGRESS:
3285  break;
3286 
3287  /*
3288  * If we failed while trying to create a subtransaction, clean up
3289  * the broken subtransaction and abort the parent. The same
3290  * applies if we get a failure while ending a subtransaction.
3291  */
3292  case TBLOCK_SUBBEGIN:
3293  case TBLOCK_SUBRELEASE:
3294  case TBLOCK_SUBCOMMIT:
3296  case TBLOCK_SUBRESTART:
3300  break;
3301 
3302  /*
3303  * Same as above, except the Abort() was already done.
3304  */
3305  case TBLOCK_SUBABORT_END:
3309  break;
3310  }
3311 }
void AbortCurrentTransaction(void)
Definition: xact.c:3162
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
static void CleanupTransaction(void)
Definition: xact.c:2748
static void AbortSubTransaction(void)
Definition: xact.c:4877
static void AbortTransaction(void)
Definition: xact.c:2567
static void CleanupSubTransaction(void)
Definition: xact.c:5026

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

Definition at line 4531 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AtAbort_Memory(), AtAbort_Portals(), AtCleanup_Memory(), AtSubAbort_Portals(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::curTransactionOwner, TransactionStateData::parent, TransactionStateData::state, TransactionStateData::subTransactionId, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, 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(), perform_work_item(), RemoveTempRelationsCallback(), and ShutdownPostgres().

4532 {
4534 
4535  /* Ensure we're not running in a doomed memory context */
4536  AtAbort_Memory();
4537 
4538  /*
4539  * Get out of any transaction or nested transaction
4540  */
4541  do
4542  {
4543  switch (s->blockState)
4544  {
4545  case TBLOCK_DEFAULT:
4546  if (s->state == TRANS_DEFAULT)
4547  {
4548  /* Not in a transaction, do nothing */
4549  }
4550  else
4551  {
4552  /*
4553  * We can get here after an error during transaction start
4554  * (state will be TRANS_START). Need to clean up the
4555  * incompletely started transaction. First, adjust the
4556  * low-level state to suppress warning message from
4557  * AbortTransaction.
4558  */
4559  if (s->state == TRANS_START)
4560  s->state = TRANS_INPROGRESS;
4561  AbortTransaction();
4563  }
4564  break;
4565  case TBLOCK_STARTED:
4566  case TBLOCK_BEGIN:
4567  case TBLOCK_INPROGRESS:
4570  case TBLOCK_END:
4571  case TBLOCK_ABORT_PENDING:
4572  case TBLOCK_PREPARE:
4573  /* In a transaction, so clean up */
4574  AbortTransaction();
4577  break;
4578  case TBLOCK_ABORT:
4579  case TBLOCK_ABORT_END:
4580 
4581  /*
4582  * AbortTransaction is already done, still need Cleanup.
4583  * However, if we failed partway through running ROLLBACK,
4584  * there will be an active portal running that command, which
4585  * we need to shut down before doing CleanupTransaction.
4586  */
4587  AtAbort_Portals();
4590  break;
4591 
4592  /*
4593  * In a subtransaction, so clean it up and abort parent too
4594  */
4595  case TBLOCK_SUBBEGIN:
4596  case TBLOCK_SUBINPROGRESS:
4597  case TBLOCK_SUBRELEASE:
4598  case TBLOCK_SUBCOMMIT:
4600  case TBLOCK_SUBRESTART:
4603  s = CurrentTransactionState; /* changed by pop */
4604  break;
4605 
4606  case TBLOCK_SUBABORT:
4607  case TBLOCK_SUBABORT_END:
4609  /* As above, but AbortSubTransaction already done */
4610  if (s->curTransactionOwner)
4611  {
4612  /* As in TBLOCK_ABORT, might have a live portal to zap */
4617  }
4619  s = CurrentTransactionState; /* changed by pop */
4620  break;
4621  }
4622  } while (s->blockState != TBLOCK_DEFAULT);
4623 
4624  /* Should be out of all subxacts now */
4625  Assert(s->parent == NULL);
4626 
4627  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4628  AtCleanup_Memory();
4629 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static void AtCleanup_Memory(void)
Definition: xact.c:1815
void AtAbort_Portals(void)
Definition: portalmem.c:772
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
static void CleanupTransaction(void)
Definition: xact.c:2748
struct TransactionStateData * parent
Definition: xact.c:193
SubTransactionId subTransactionId
Definition: xact.c:174
static void AbortSubTransaction(void)
Definition: xact.c:4877
static void AtAbort_Memory(void)
Definition: xact.c:1725
static void AbortTransaction(void)
Definition: xact.c:2567
#define Assert(condition)
Definition: c.h:739
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:968
static void CleanupSubTransaction(void)
Definition: xact.c:5026

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

Definition at line 4877 of file xact.c.

References AbortBufferIO(), AfterTriggerEndSubXact(), AtEOSubXact_ApplyLauncher(), 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(), ConditionVariableCancelSleep(), CurrentTransactionState, TransactionStateData::curTransactionOwner, elog, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, 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, TransStateAsString(), UnBlockSig, UnlockBuffers(), WARNING, XactReadOnly, and XLogResetInsertion().

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

4878 {
4880 
4881  /* Prevent cancel/die interrupt while cleaning up */
4882  HOLD_INTERRUPTS();
4883 
4884  /* Make sure we have a valid memory context and resource owner */
4887 
4888  /*
4889  * Release any LW locks we might be holding as quickly as possible.
4890  * (Regular locks, however, must be held till we finish aborting.)
4891  * Releasing LW locks is critical since we might try to grab them again
4892  * while cleaning up!
4893  *
4894  * FIXME This may be incorrect --- Are there some locks we should keep?
4895  * Buffer locks, for example? I don't think so but I'm not sure.
4896  */
4897  LWLockReleaseAll();
4898 
4901  AbortBufferIO();
4902  UnlockBuffers();
4903 
4904  /* Reset WAL record construction state */
4906 
4907  /* Cancel condition variable sleep */
4909 
4910  /*
4911  * Also clean up any open wait for lock, since the lock manager will choke
4912  * if we try to wait for another lock before doing this.
4913  */
4914  LockErrorCleanup();
4915 
4916  /*
4917  * If any timeout events are still active, make sure the timeout interrupt
4918  * is scheduled. This covers possible loss of a timeout interrupt due to
4919  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4920  * We delay this till after LockErrorCleanup so that we don't uselessly
4921  * reschedule lock or deadlock check timeouts.
4922  */
4924 
4925  /*
4926  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4927  * handler. We do this fairly early in the sequence so that the timeout
4928  * infrastructure will be functional if needed while aborting.
4929  */
4931 
4932  /*
4933  * check the current transaction state
4934  */
4935  ShowTransactionState("AbortSubTransaction");
4936 
4937  if (s->state != TRANS_INPROGRESS)
4938  elog(WARNING, "AbortSubTransaction while in %s state",
4940 
4941  s->state = TRANS_ABORT;
4942 
4943  /*
4944  * Reset user ID which might have been changed transiently. (See notes in
4945  * AbortTransaction.)
4946  */
4948 
4949  /* Exit from parallel mode, if necessary. */
4950  if (IsInParallelMode())
4951  {
4953  s->parallelModeLevel = 0;
4954  }
4955 
4956  /*
4957  * We can skip all this stuff if the subxact failed before creating a
4958  * ResourceOwner...
4959  */
4960  if (s->curTransactionOwner)
4961  {
4962  AfterTriggerEndSubXact(false);
4968  s->parent->subTransactionId);
4970 
4971  /* Advertise the fact that we aborted in pg_xact. */
4972  (void) RecordTransactionAbort(true);
4973 
4974  /* Post-abort cleanup */
4977 
4979  s->parent->subTransactionId);
4980 
4983  false, false);
4985  s->parent->subTransactionId);
4986  AtEOSubXact_Inval(false);
4989  false, false);
4992  false, false);
4993  AtSubAbort_smgr();
4994 
4995  AtEOXact_GUC(false, s->gucNestLevel);
4996  AtEOSubXact_SPI(false, s->subTransactionId);
4998  s->parent->subTransactionId);
5000  s->parent->subTransactionId);
5002  s->parent->subTransactionId);
5004  AtEOSubXact_PgStat(false, s->nestingLevel);
5007  }
5008 
5009  /*
5010  * Restore the upper transaction's read-only state, too. This should be
5011  * redundant with GUC's cleanup but we may as well do it for consistency
5012  * with the commit case.
5013  */
5015 
5017 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1770
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3076
static TransactionState CurrentTransactionState
Definition: xact.c:234
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1860
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:385
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1055
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
bool IsInParallelMode(void)
Definition: xact.c:996
void ConditionVariableCancelSleep(void)
static void AtSubAbort_Memory(void)
Definition: xact.c:1745
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:193
static void ShowTransactionState(const char *str)
Definition: xact.c:5287
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:890
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5792
SubTransactionId subTransactionId
Definition: xact.c:174
sigset_t UnBlockSig
Definition: pqsignal.c:22
FullTransactionId fullTransactionId
Definition: xact.c:173
static void AtSubAbort_childXids(void)
Definition: xact.c:1783
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2179
void LockErrorCleanup(void)
Definition: proc.c:711
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:632
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3574
void AtSubAbort_Notify(void)
Definition: async.c:1826
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:14721
void pgstat_progress_end_command(void)
Definition: pgstat.c:3269
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2785
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3568
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:191
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:968
void AtSubAbort_smgr(void)
Definition: storage.c:595
bool prevXactReadOnly
Definition: xact.c:188
void AbortBufferIO(void)
Definition: bufmgr.c:4030
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5108
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:228
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4069
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1169
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1613

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

Definition at line 2567 of file xact.c.

References AbortBufferIO(), AfterTriggerEndXact(), Assert, AtAbort_Memory(), AtAbort_Notify(), AtAbort_Portals(), AtAbort_ResourceOwner(), AtAbort_Twophase(), AtEOXact_ApplyLauncher(), AtEOXact_Buffers(), AtEOXact_ComboCid(), AtEOXact_Enum(), 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, 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().

2568 {
2570  TransactionId latestXid;
2571  bool is_parallel_worker;
2572 
2573  /* Prevent cancel/die interrupt while cleaning up */
2574  HOLD_INTERRUPTS();
2575 
2576  /* Make sure we have a valid memory context and resource owner */
2577  AtAbort_Memory();
2579 
2580  /*
2581  * Release any LW locks we might be holding as quickly as possible.
2582  * (Regular locks, however, must be held till we finish aborting.)
2583  * Releasing LW locks is critical since we might try to grab them again
2584  * while cleaning up!
2585  */
2586  LWLockReleaseAll();
2587 
2588  /* Clear wait information and command progress indicator */
2591 
2592  /* Clean up buffer I/O and buffer context locks, too */
2593  AbortBufferIO();
2594  UnlockBuffers();
2595 
2596  /* Reset WAL record construction state */
2598 
2599  /* Cancel condition variable sleep */
2601 
2602  /*
2603  * Also clean up any open wait for lock, since the lock manager will choke
2604  * if we try to wait for another lock before doing this.
2605  */
2606  LockErrorCleanup();
2607 
2608  /*
2609  * If any timeout events are still active, make sure the timeout interrupt
2610  * is scheduled. This covers possible loss of a timeout interrupt due to
2611  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2612  * We delay this till after LockErrorCleanup so that we don't uselessly
2613  * reschedule lock or deadlock check timeouts.
2614  */
2616 
2617  /*
2618  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2619  * handler. We do this fairly early in the sequence so that the timeout
2620  * infrastructure will be functional if needed while aborting.
2621  */
2623 
2624  /*
2625  * check the current transaction state
2626  */
2627  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2628  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2629  elog(WARNING, "AbortTransaction while in %s state",
2631  Assert(s->parent == NULL);
2632 
2633  /*
2634  * set the current transaction state information appropriately during the
2635  * abort processing
2636  */
2637  s->state = TRANS_ABORT;
2638 
2639  /*
2640  * Reset user ID which might have been changed transiently. We need this
2641  * to clean up in case control escaped out of a SECURITY DEFINER function
2642  * or other local change of CurrentUserId; therefore, the prior value of
2643  * SecurityRestrictionContext also needs to be restored.
2644  *
2645  * (Note: it is not necessary to restore session authorization or role
2646  * settings here because those can only be changed via GUC, and GUC will
2647  * take care of rolling them back if need be.)
2648  */
2650 
2651  /* If in parallel mode, clean up workers and exit parallel mode. */
2652  if (IsInParallelMode())
2653  {
2654  AtEOXact_Parallel(false);
2655  s->parallelModeLevel = 0;
2656  }
2657 
2658  /*
2659  * do abort processing
2660  */
2661  AfterTriggerEndXact(false); /* 'false' means it's abort */
2662  AtAbort_Portals();
2663  AtEOXact_LargeObject(false);
2664  AtAbort_Notify();
2665  AtEOXact_RelationMap(false, is_parallel_worker);
2666  AtAbort_Twophase();
2667 
2668  /*
2669  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2670  * far as assigning an XID to advertise). But if we're inside a parallel
2671  * worker, skip this; the user backend must be the one to write the abort
2672  * record.
2673  */
2674  if (!is_parallel_worker)
2675  latestXid = RecordTransactionAbort(false);
2676  else
2677  {
2678  latestXid = InvalidTransactionId;
2679 
2680  /*
2681  * Since the parallel master won't get our value of XactLastRecEnd in
2682  * this case, we nudge WAL-writer ourselves in this case. See related
2683  * comments in RecordTransactionAbort for why this matters.
2684  */
2686  }
2687 
2688  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2689 
2690  /*
2691  * Let others know about no transaction in progress by me. Note that this
2692  * must be done _before_ releasing locks we hold and _after_
2693  * RecordTransactionAbort.
2694  */
2695  ProcArrayEndTransaction(MyProc, latestXid);
2696 
2697  /*
2698  * Post-abort cleanup. See notes in CommitTransaction() concerning
2699  * ordering. We can skip all of it if the transaction failed before
2700  * creating a resource owner.
2701  */
2702  if (TopTransactionResourceOwner != NULL)
2703  {
2704  if (is_parallel_worker)
2706  else
2708 
2711  false, true);
2712  AtEOXact_Buffers(false);
2713  AtEOXact_RelationCache(false);
2714  AtEOXact_Inval(false);
2718  false, true);
2721  false, true);
2722  smgrDoPendingDeletes(false);
2723 
2724  AtEOXact_GUC(false, 1);
2725  AtEOXact_SPI(false);
2726  AtEOXact_Enum();
2728  AtEOXact_Namespace(false, is_parallel_worker);
2729  AtEOXact_SMgr();
2730  AtEOXact_Files(false);
2732  AtEOXact_HashTables(false);
2733  AtEOXact_PgStat(false, is_parallel_worker);
2734  AtEOXact_ApplyLauncher(false);
2736  }
2737 
2738  /*
2739  * State remains TRANS_ABORT until CleanupTransaction().
2740  */
2742 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:848
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:430
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1757
static TransactionState CurrentTransactionState
Definition: xact.c:234
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:949
uint32 TransactionId
Definition: c.h:514
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3321
XLogRecPtr XactLastRecEnd
Definition: xlog.c:352
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2938
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2423
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3513
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:14689
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:398
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2818
void AtAbort_Portals(void)
Definition: portalmem.c:772
TBlockState blockState
Definition: xact.c:178
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
TransState state
Definition: xact.c:177
void AtEOXact_SMgr(void)
Definition: smgr.c:655
bool IsInParallelMode(void)
Definition: xact.c:996
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5012
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:193
void AtAbort_Twophase(void)
Definition: twophase.c:317
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2092
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5792
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
void LockErrorCleanup(void)
Definition: proc.c:711
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3574
static void AtAbort_Memory(void)
Definition: xact.c:1725
void pgstat_progress_end_command(void)
Definition: pgstat.c:3269
void AtAbort_Notify(void)
Definition: async.c:1736
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2643
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:363
void AtEOXact_MultiXact(void)
Definition: multixact.c:1664
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void AbortBufferIO(void)
Definition: bufmgr.c:4030
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:228
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1834
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1188
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4003
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1613
LocalTransactionId lxid
Definition: proc.h:106

◆ AssignTransactionId()

static void AssignTransactionId ( TransactionState  s)
static

Definition at line 553 of file xact.c.

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

Referenced by GetCurrentFullTransactionId(), GetCurrentTransactionId(), GetTopFullTransactionId(), and GetTopTransactionId().

554 {
555  bool isSubXact = (s->parent != NULL);
556  ResourceOwner currentOwner;
557  bool log_unknown_top = false;
558 
559  /* Assert that caller didn't screw up */
562 
563  /*
564  * Workers synchronize transaction state at the beginning of each parallel
565  * operation, so we can't account for new XIDs at this point.
566  */
568  elog(ERROR, "cannot assign XIDs during a parallel operation");
569 
570  /*
571  * Ensure parent(s) have XIDs, so that a child always has an XID later
572  * than its parent. Mustn't recurse here, or we might get a stack
573  * overflow if we're at the bottom of a huge stack of subtransactions none
574  * of which have XIDs yet.
575  */
576  if (isSubXact && !FullTransactionIdIsValid(s->parent->fullTransactionId))
577  {
578  TransactionState p = s->parent;
579  TransactionState *parents;
580  size_t parentOffset = 0;
581 
582  parents = palloc(sizeof(TransactionState) * s->nestingLevel);
583  while (p != NULL && !FullTransactionIdIsValid(p->fullTransactionId))
584  {
585  parents[parentOffset++] = p;
586  p = p->parent;
587  }
588 
589  /*
590  * This is technically a recursive call, but the recursion will never
591  * be more than one layer deep.
592  */
593  while (parentOffset != 0)
594  AssignTransactionId(parents[--parentOffset]);
595 
596  pfree(parents);
597  }
598 
599  /*
600  * When wal_level=logical, guarantee that a subtransaction's xid can only
601  * be seen in the WAL stream if its toplevel xid has been logged before.
602  * If necessary we log an xact_assignment record with fewer than
603  * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
604  * for a transaction even though it appears in a WAL record, we just might
605  * superfluously log something. That can happen when an xid is included
606  * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
607  * xl_standby_locks.
608  */
609  if (isSubXact && XLogLogicalInfoActive() &&
611  log_unknown_top = true;
612 
613  /*
614  * Generate a new FullTransactionId and record its xid in PG_PROC and
615  * pg_subtrans.
616  *
617  * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
618  * shared storage other than PG_PROC; because if there's no room for it in
619  * PG_PROC, the subtrans entry is needed to ensure that other backends see
620  * the Xid as "running". See GetNewTransactionId.
621  */
622  s->fullTransactionId = GetNewTransactionId(isSubXact);
623  if (!isSubXact)
625 
626  if (isSubXact)
629 
630  /*
631  * If it's a top-level transaction, the predicate locking system needs to
632  * be told about it too.
633  */
634  if (!isSubXact)
636 
637  /*
638  * Acquire lock on the transaction XID. (We assume this cannot block.) We
639  * have to ensure that the lock is assigned to the transaction's own
640  * ResourceOwner.
641  */
642  currentOwner = CurrentResourceOwner;
644 
646 
647  CurrentResourceOwner = currentOwner;
648 
649  /*
650  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
651  * top-level transaction we issue a WAL record for the assignment. We
652  * include the top-level xid and all the subxids that have not yet been
653  * reported using XLOG_XACT_ASSIGNMENT records.
654  *
655  * This is required to limit the amount of shared memory required in a hot
656  * standby server to keep track of in-progress XIDs. See notes for
657  * RecordKnownAssignedTransactionIds().
658  *
659  * We don't keep track of the immediate parent of each subxid, only the
660  * top-level transaction that each subxact belongs to. This is correct in
661  * recovery only because aborted subtransactions are separately WAL
662  * logged.
663  *
664  * This is correct even for the case where several levels above us didn't
665  * have an xid assigned as we recursed up to them beforehand.
666  */
667  if (isSubXact && XLogStandbyInfoActive())
668  {
670  nUnreportedXids++;
671 
672  /*
673  * ensure this test matches similar one in
674  * RecoverPreparedTransactions()
675  */
677  log_unknown_top)
678  {
679  xl_xact_assignment xlrec;
680 
681  /*
682  * xtop is always set by now because we recurse up transaction
683  * stack to the highest unassigned xid and then come back down
684  */
685  xlrec.xtop = GetTopTransactionId();
687  xlrec.nsubxacts = nUnreportedXids;
688 
689  XLogBeginInsert();
690  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
692  nUnreportedXids * sizeof(TransactionId));
693 
694  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
695 
696  nUnreportedXids = 0;
697  /* mark top, not current xact as having been logged */
699  }
700  }
701 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
uint32 TransactionId
Definition: c.h:514
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
TransactionId xtop
Definition: xact.h:193
TransactionId GetTopTransactionId(void)
Definition: xact.c:394
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
void pfree(void *pointer)
Definition: mcxt.c:1056
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define XLogLogicalInfoActive()
Definition: xlog.h:198
struct TransactionStateData * parent
Definition: xact.c:193
#define MinSizeOfXactAssignment
Definition: xact.h:198
FullTransactionId fullTransactionId
Definition: xact.c:173
#define IsParallelWorker()
Definition: parallel.h:60
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:148
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:583
#define XLogStandbyInfoActive()
Definition: xlog.h:195
static int nUnreportedXids
Definition: xact.c:231
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
#define Assert(condition)
Definition: c.h:739
static TransactionStateData TopTransactionStateData
Definition: xact.c:222
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1875
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
FullTransactionId GetNewTransactionId(bool isSubXact)
Definition: varsup.c:49
static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: xact.c:232
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553
void SubTransSetParent(TransactionId xid, TransactionId parent)
Definition: subtrans.c:74
void XLogBeginInsert(void)
Definition: xloginsert.c:120

◆ AtAbort_Memory()

static void AtAbort_Memory ( void  )
static

Definition at line 1725 of file xact.c.

References MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

1726 {
1727  /*
1728  * Switch into TransactionAbortContext, which should have some free space
1729  * even if nothing else does. We'll work in this context until we've
1730  * finished cleaning up.
1731  *
1732  * It is barely possible to get here when we've not been able to create
1733  * TransactionAbortContext yet; if so use TopMemoryContext.
1734  */
1735  if (TransactionAbortContext != NULL)
1737  else
1739 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:274
MemoryContext TopMemoryContext
Definition: mcxt.c:44

◆ AtAbort_ResourceOwner()

static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1757 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

1758 {
1759  /*
1760  * Make sure we have a valid ResourceOwner, if possible (else it will be
1761  * NULL, which is OK)
1762  */
1764 }
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142

◆ AtCCI_LocalCache()

static void AtCCI_LocalCache ( void  )
static

Definition at line 1448 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

1449 {
1450  /*
1451  * Make any pending relation map changes visible. We must do this before
1452  * processing local sinval messages, so that the map changes will get
1453  * reflected into the relcache when relcache invals are processed.
1454  */
1456 
1457  /*
1458  * Make catalog changes visible to me for the next command.
1459  */
1461 }
void CommandEndInvalidationMessages(void)
Definition: inval.c:1085
void AtCCI_RelationMap(void)
Definition: relmapper.c:439

◆ AtCleanup_Memory()

static void AtCleanup_Memory ( void  )
static

Definition at line 1815 of file xact.c.

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

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1816 {
1818 
1819  /*
1820  * Now that we're "out" of a transaction, have the system allocate things
1821  * in the top memory context instead of per-transaction contexts.
1822  */
1824 
1825  /*
1826  * Clear the special abort context for next time.
1827  */
1828  if (TransactionAbortContext != NULL)
1830 
1831  /*
1832  * Release all transaction-local memory.
1833  */
1834  if (TopTransactionContext != NULL)
1836  TopTransactionContext = NULL;
1837  CurTransactionContext = NULL;
1839 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c:274
struct TransactionStateData * parent
Definition: xact.c:193
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:739

◆ AtCommit_Memory()

static void AtCommit_Memory ( void  )
static

Definition at line 1467 of file xact.c.

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

Referenced by CommitTransaction(), and PrepareTransaction().

1468 {
1469  /*
1470  * Now that we're "out" of a transaction, have the system allocate things
1471  * in the top memory context instead of per-transaction contexts.
1472  */
1474 
1475  /*
1476  * Release all transaction-local memory.
1477  */
1478  Assert(TopTransactionContext != NULL);
1480  TopTransactionContext = NULL;
1481  CurTransactionContext = NULL;
1483 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:739

◆ AtStart_Cache()

static void AtStart_Cache ( void  )
static

Definition at line 1068 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

1069 {
1071 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681

◆ AtStart_Memory()

static void AtStart_Memory ( void  )
static

Definition at line 1077 of file xact.c.

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

Referenced by StartTransaction().

1078 {
1080 
1081  /*
1082  * If this is the first time through, create a private context for
1083  * AbortTransaction to work in. By reserving some space now, we can
1084  * insulate AbortTransaction from out-of-memory scenarios. Like
1085  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1086  * size, so that space will be reserved immediately.
1087  */
1088  if (TransactionAbortContext == NULL)
1091  "TransactionAbortContext",
1092  32 * 1024,
1093  32 * 1024,
1094  32 * 1024);
1095 
1096  /*
1097  * We shouldn't have a transaction context already.
1098  */
1099  Assert(TopTransactionContext == NULL);
1100 
1101  /*
1102  * Create a toplevel context for the transaction.
1103  */
1106  "TopTransactionContext",
1108 
1109  /*
1110  * In a top-level transaction, CurTransactionContext is the same as
1111  * TopTransactionContext.
1112  */
1115 
1116  /* Make the CurTransactionContext active. */
1118 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AllocSetContextCreate
Definition: memutils.h:170
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c:274
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:739

◆ AtStart_ResourceOwner()

static void AtStart_ResourceOwner ( void  )
static

Definition at line 1124 of file xact.c.

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

Referenced by StartTransaction().

1125 {
1127 
1128  /*
1129  * We shouldn't have a transaction resource owner already.
1130  */
1132 
1133  /*
1134  * Create a toplevel resource owner for the transaction.
1135  */
1136  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1137 
1141 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
ResourceOwner curTransactionOwner
Definition: xact.c:182
#define Assert(condition)
Definition: c.h:739
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ AtSubAbort_childXids()

static void AtSubAbort_childXids ( void  )
static

Definition at line 1783 of file xact.c.

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

Referenced by AbortSubTransaction().

1784 {
1786 
1787  /*
1788  * We keep the child-XID arrays in TopTransactionContext (see
1789  * AtSubCommit_childXids). This means we'd better free the array
1790  * explicitly at abort to avoid leakage.
1791  */
1792  if (s->childXids != NULL)
1793  pfree(s->childXids);
1794  s->childXids = NULL;
1795  s->nChildXids = 0;
1796  s->maxChildXids = 0;
1797 
1798  /*
1799  * We could prune the unreportedXids array here. But we don't bother. That
1800  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1801  * would likely introduce more CPU time into the more common paths, so we
1802  * choose not to do that.
1803  */
1804 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TransactionId * childXids
Definition: xact.c:183
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ AtSubAbort_Memory()

static void AtSubAbort_Memory ( void  )
static

Definition at line 1745 of file xact.c.

References Assert, and MemoryContextSwitchTo().

Referenced by AbortSubTransaction().

1746 {
1748 
1750 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:274
#define Assert(condition)
Definition: c.h:739

◆ AtSubAbort_ResourceOwner()

static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1770 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1771 {
1773 
1774  /* Make sure we have a valid ResourceOwner */
1776 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
ResourceOwner curTransactionOwner
Definition: xact.c:182

◆ AtSubCleanup_Memory()

static void AtSubCleanup_Memory ( void  )
static

Definition at line 1851 of file xact.c.

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

Referenced by CleanupSubTransaction().

1852 {
1854 
1855  Assert(s->parent != NULL);
1856 
1857  /* Make sure we're not in an about-to-be-deleted context */
1860 
1861  /*
1862  * Clear the special abort context for next time.
1863  */
1864  if (TransactionAbortContext != NULL)
1866 
1867  /*
1868  * Delete the subxact local memory contexts. Its CurTransactionContext can
1869  * go too (note this also kills CurTransactionContexts from any children
1870  * of the subxact).
1871  */
1872  if (s->curTransactionContext)
1874  s->curTransactionContext = NULL;
1875 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c:274
struct TransactionStateData * parent
Definition: xact.c:193
MemoryContext curTransactionContext
Definition: xact.c:181
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:739

◆ AtSubCommit_childXids()

static void AtSubCommit_childXids ( void  )
static

Definition at line 1523 of file xact.c.

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

Referenced by CommitSubTransaction().

1524 {
1526  int new_nChildXids;
1527 
1528  Assert(s->parent != NULL);
1529 
1530  /*
1531  * The parent childXids array will need to hold my XID and all my
1532  * childXids, in addition to the XIDs already there.
1533  */
1534  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1535 
1536  /* Allocate or enlarge the parent array if necessary */
1537  if (s->parent->maxChildXids < new_nChildXids)
1538  {
1539  int new_maxChildXids;
1540  TransactionId *new_childXids;
1541 
1542  /*
1543  * Make it 2x what's needed right now, to avoid having to enlarge it
1544  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1545  * is what ensures that we don't need to worry about integer overflow
1546  * here or in the calculation of new_nChildXids.)
1547  */
1548  new_maxChildXids = Min(new_nChildXids * 2,
1549  (int) (MaxAllocSize / sizeof(TransactionId)));
1550 
1551  if (new_maxChildXids < new_nChildXids)
1552  ereport(ERROR,
1553  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1554  errmsg("maximum number of committed subtransactions (%d) exceeded",
1555  (int) (MaxAllocSize / sizeof(TransactionId)))));
1556 
1557  /*
1558  * We keep the child-XID arrays in TopTransactionContext; this avoids
1559  * setting up child-transaction contexts for what might be just a few
1560  * bytes of grandchild XIDs.
1561  */
1562  if (s->parent->childXids == NULL)
1563  new_childXids =
1565  new_maxChildXids * sizeof(TransactionId));
1566  else
1567  new_childXids = repalloc(s->parent->childXids,
1568  new_maxChildXids * sizeof(TransactionId));
1569 
1570  s->parent->childXids = new_childXids;
1571  s->parent->maxChildXids = new_maxChildXids;
1572  }
1573 
1574  /*
1575  * Copy all my XIDs to parent's array.
1576  *
1577  * Note: We rely on the fact that the XID of a child always follows that
1578  * of its parent. By copying the XID of this subtransaction before the
1579  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1580  * all XIDs already in the array belong to subtransactions started and
1581  * subcommitted before us, so their XIDs must precede ours.
1582  */
1584 
1585  if (s->nChildXids > 0)
1586  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1587  s->childXids,
1588  s->nChildXids * sizeof(TransactionId));
1589 
1590  s->parent->nChildXids = new_nChildXids;
1591 
1592  /* Release child's array to avoid leakage */
1593  if (s->childXids != NULL)
1594  pfree(s->childXids);
1595  /* We must reset these to avoid double-free if fail later in commit */
1596  s->childXids = NULL;
1597  s->nChildXids = 0;
1598  s->maxChildXids = 0;
1599 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
MemoryContext TopTransactionContext
Definition: mcxt.c:49
uint32 TransactionId
Definition: c.h:514
#define Min(x, y)
Definition: c.h:911
int errcode(int sqlerrcode)
Definition: elog.c:608
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:183
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
struct TransactionStateData * parent
Definition: xact.c:193
FullTransactionId fullTransactionId
Definition: xact.c:173
#define ereport(elevel, rest)
Definition: elog.h:141
#define MaxAllocSize
Definition: memutils.h:40
#define Assert(condition)
Definition: c.h:739
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int errmsg(const char *fmt,...)
Definition: elog.c:822
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796

◆ AtSubCommit_Memory()

static void AtSubCommit_Memory ( void  )
static

Definition at line 1494 of file xact.c.

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

Referenced by CommitSubTransaction().

1495 {
1497 
1498  Assert(s->parent != NULL);
1499 
1500  /* Return to parent transaction level's memory context. */
1503 
1504  /*
1505  * Ordinarily we cannot throw away the child's CurTransactionContext,
1506  * since the data it contains will be needed at upper commit. However, if
1507  * there isn't actually anything in it, we can throw it away. This avoids
1508  * a small memory leak in the common case of "trivial" subxacts.
1509  */
1511  {
1513  s->curTransactionContext = NULL;
1514  }
1515 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:451
struct TransactionStateData * parent
Definition: xact.c:193
MemoryContext curTransactionContext
Definition: xact.c:181
#define Assert(condition)
Definition: c.h:739

◆ AtSubStart_Memory()

static void AtSubStart_Memory ( void  )
static

Definition at line 1152 of file xact.c.

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

Referenced by StartSubTransaction().

1153 {
1155 
1156  Assert(CurTransactionContext != NULL);
1157 
1158  /*
1159  * Create a CurTransactionContext, which will be used to hold data that
1160  * survives subtransaction commit but disappears on subtransaction abort.
1161  * We make it a child of the immediate parent's CurTransactionContext.
1162  */
1164  "CurTransactionContext",
1167 
1168  /* Make the CurTransactionContext active. */
1170 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AllocSetContextCreate
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext curTransactionContext
Definition: xact.c:181
#define Assert(condition)
Definition: c.h:739

◆ AtSubStart_ResourceOwner()

static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1176 of file xact.c.

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

Referenced by StartSubTransaction().

1177 {
1179 
1180  Assert(s->parent != NULL);
1181 
1182  /*
1183  * Create a resource owner for the subtransaction. We make it a child of
1184  * the immediate parent's resource owner.
1185  */
1186  s->curTransactionOwner =
1188  "SubTransaction");
1189 
1192 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
ResourceOwner curTransactionOwner
Definition: xact.c:182
struct TransactionStateData * parent
Definition: xact.c:193
#define Assert(condition)
Definition: c.h:739
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 3991 of file xact.c.

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

Referenced by exec_simple_query().

3992 {
3994 
3995  /*
3996  * If we are in STARTED state (that is, no transaction block is open),
3997  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3998  * block.
3999  *
4000  * For caller convenience, we consider all other transaction states as
4001  * legal here; otherwise the caller would need its own state check, which
4002  * seems rather pointless.
4003  */
4004  if (s->blockState == TBLOCK_STARTED)
4006 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4359 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_IMPLICIT_INPROGRESS, 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(), pltcl_subtransaction(), PLy_spi_subtransaction_begin(), PLy_subtransaction_enter(), ReorderBufferCommit(), and ReorderBufferImmediateInvalidation().

4360 {
4362 
4363  /*
4364  * Workers synchronize transaction state at the beginning of each parallel
4365  * operation, so we can't account for new subtransactions after that
4366  * point. We might be able to make an exception for the type of
4367  * subtransaction established by this function, which is typically used in
4368  * contexts where we're going to release or roll back the subtransaction
4369  * before proceeding further, so that no enduring change to the
4370  * transaction state occurs. For now, however, we prohibit this case along
4371  * with all the others.
4372  */
4373  if (IsInParallelMode())
4374  ereport(ERROR,
4375  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4376  errmsg("cannot start subtransactions during a parallel operation")));
4377 
4378  switch (s->blockState)
4379  {
4380  case TBLOCK_STARTED:
4381  case TBLOCK_INPROGRESS:
4383  case TBLOCK_END:
4384  case TBLOCK_PREPARE:
4385  case TBLOCK_SUBINPROGRESS:
4386  /* Normal subtransaction start */
4387  PushTransaction();
4388  s = CurrentTransactionState; /* changed by push */
4389 
4390  /*
4391  * Savepoint names, like the TransactionState block itself, live
4392  * in TopTransactionContext.
4393  */
4394  if (name)
4396  break;
4397 
4398  /* These cases are invalid. */
4399  case TBLOCK_DEFAULT:
4400  case TBLOCK_BEGIN:
4402  case TBLOCK_SUBBEGIN:
4403  case TBLOCK_SUBRELEASE:
4404  case TBLOCK_SUBCOMMIT:
4405  case TBLOCK_ABORT:
4406  case TBLOCK_SUBABORT:
4407  case TBLOCK_ABORT_END:
4408  case TBLOCK_SUBABORT_END:
4409  case TBLOCK_ABORT_PENDING:
4411  case TBLOCK_SUBRESTART:
4413  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4415  break;
4416  }
4417 
4420 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void CommitTransactionCommand(void)
Definition: xact.c:2898
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5059
void StartTransactionCommand(void)
Definition: xact.c:2797
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:228

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3589 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_IMPLICIT_INPROGRESS, 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().

3590 {
3592 
3593  switch (s->blockState)
3594  {
3595  /*
3596  * We are not inside a transaction block, so allow one to begin.
3597  */
3598  case TBLOCK_STARTED:
3599  s->blockState = TBLOCK_BEGIN;
3600  break;
3601 
3602  /*
3603  * BEGIN converts an implicit transaction block to a regular one.
3604  * (Note that we allow this even if we've already done some
3605  * commands, which is a bit odd but matches historical practice.)
3606  */
3608  s->blockState = TBLOCK_BEGIN;
3609  break;
3610 
3611  /*
3612  * Already a transaction block in progress.
3613  */
3614  case TBLOCK_INPROGRESS:
3616  case TBLOCK_SUBINPROGRESS:
3617  case TBLOCK_ABORT:
3618  case TBLOCK_SUBABORT:
3619  ereport(WARNING,
3620  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3621  errmsg("there is already a transaction in progress")));
3622  break;
3623 
3624  /* These cases are invalid. */
3625  case TBLOCK_DEFAULT:
3626  case TBLOCK_BEGIN:
3627  case TBLOCK_SUBBEGIN:
3628  case TBLOCK_END:
3629  case TBLOCK_SUBRELEASE:
3630  case TBLOCK_SUBCOMMIT:
3631  case TBLOCK_ABORT_END:
3632  case TBLOCK_SUBABORT_END:
3633  case TBLOCK_ABORT_PENDING:
3635  case TBLOCK_SUBRESTART:
3637  case TBLOCK_PREPARE:
3638  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3640  break;
3641  }
3642 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

Definition at line 5338 of file xact.c.

References TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, 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().

5339 {
5340  switch (blockState)
5341  {
5342  case TBLOCK_DEFAULT:
5343  return "DEFAULT";
5344  case TBLOCK_STARTED:
5345  return "STARTED";
5346  case TBLOCK_BEGIN:
5347  return "BEGIN";
5348  case TBLOCK_INPROGRESS:
5349  return "INPROGRESS";
5351  return "IMPLICIT_INPROGRESS";
5353  return "PARALLEL_INPROGRESS";
5354  case TBLOCK_END:
5355  return "END";
5356  case TBLOCK_ABORT:
5357  return "ABORT";
5358  case TBLOCK_ABORT_END:
5359  return "ABORT_END";
5360  case TBLOCK_ABORT_PENDING:
5361  return "ABORT_PENDING";
5362  case TBLOCK_PREPARE:
5363  return "PREPARE";
5364  case TBLOCK_SUBBEGIN:
5365  return "SUBBEGIN";
5366  case TBLOCK_SUBINPROGRESS:
5367  return "SUBINPROGRESS";
5368  case TBLOCK_SUBRELEASE:
5369  return "SUBRELEASE";
5370  case TBLOCK_SUBCOMMIT:
5371  return "SUBCOMMIT";
5372  case TBLOCK_SUBABORT:
5373  return "SUBABORT";
5374  case TBLOCK_SUBABORT_END:
5375  return "SUBABORT_END";
5377  return "SUBABORT_PENDING";
5378  case TBLOCK_SUBRESTART:
5379  return "SUBRESTART";
5381  return "SUBABORT_RESTART";
5382  }
5383  return "UNRECOGNIZED";
5384 }

◆ CallSubXactCallbacks()

static void CallSubXactCallbacks ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
static

Definition at line 3568 of file xact.c.

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

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

3571 {
3572  SubXactCallbackItem *item;
3573 
3574  for (item = SubXact_callbacks; item; item = item->next)
3575  item->callback(event, mySubid, parentSubid, item->arg);
3576 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:298
struct SubXactCallbackItem * next
Definition: xact.c:293
SubXactCallback callback
Definition: xact.c:294

◆ CallXactCallbacks()

static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3513 of file xact.c.

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

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

3514 {
3515  XactCallbackItem *item;
3516 
3517  for (item = Xact_callbacks; item; item = item->next)
3518  item->callback(event, item->arg);
3519 }
struct XactCallbackItem * next
Definition: xact.c:281
void * arg
Definition: xact.c:283
XactCallback callback
Definition: xact.c:282
static XactCallbackItem * Xact_callbacks
Definition: xact.c:286

◆ CheckTransactionBlock()

static void CheckTransactionBlock ( bool  isTopLevel,
bool  throwError,
const char *  stmtType 
)
static

Definition at line 3406 of file xact.c.

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3407 {
3408  /*
3409  * xact block already started?
3410  */
3411  if (IsTransactionBlock())
3412  return;
3413 
3414  /*
3415  * subtransaction?
3416  */
3417  if (IsSubTransaction())
3418  return;
3419 
3420  /*
3421  * inside a function call?
3422  */
3423  if (!isTopLevel)
3424  return;
3425 
3426  ereport(throwError ? ERROR : WARNING,
3427  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3428  /* translator: %s represents an SQL statement name */
3429  errmsg("%s can only be used in transaction blocks",
3430  stmtType)));
3431 }
int errcode(int sqlerrcode)
Definition: elog.c:608
bool IsTransactionBlock(void)
Definition: xact.c:4635
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
bool IsSubTransaction(void)
Definition: xact.c:4708
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

Definition at line 5026 of file xact.c.

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

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

5027 {
5029 
5030  ShowTransactionState("CleanupSubTransaction");
5031 
5032  if (s->state != TRANS_ABORT)
5033  elog(WARNING, "CleanupSubTransaction while in %s state",
5035 
5037 
5040  if (s->curTransactionOwner)
5042  s->curTransactionOwner = NULL;
5043 
5045 
5046  s->state = TRANS_DEFAULT;
5047 
5048  PopTransaction();
5049 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static void PopTransaction(void)
Definition: xact.c:5118
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:688
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
struct TransactionStateData * parent
Definition: xact.c:193
static void ShowTransactionState(const char *str)
Definition: xact.c:5287
SubTransactionId subTransactionId
Definition: xact.c:174
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1081
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1851
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
#define elog(elevel,...)
Definition: elog.h:228

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

Definition at line 2748 of file xact.c.

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

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

2749 {
2751 
2752  /*
2753  * State should still be TRANS_ABORT from AbortTransaction().
2754  */
2755  if (s->state != TRANS_ABORT)
2756  elog(FATAL, "CleanupTransaction: unexpected state %s",
2758 
2759  /*
2760  * do abort cleanup processing
2761  */
2762  AtCleanup_Portals(); /* now safe to release portal memory */
2763  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2764 
2765  CurrentResourceOwner = NULL; /* and resource owner */
2768  s->curTransactionOwner = NULL;
2771 
2772  AtCleanup_Memory(); /* and transaction memory */
2773 
2776  s->nestingLevel = 0;
2777  s->gucNestLevel = 0;
2778  s->childXids = NULL;
2779  s->nChildXids = 0;
2780  s->maxChildXids = 0;
2781  s->parallelModeLevel = 0;
2782 
2785 
2786  /*
2787  * done with abort processing, set current transaction state back to
2788  * default
2789  */
2790  s->state = TRANS_DEFAULT;
2791 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
static TransactionState CurrentTransactionState
Definition: xact.c:234
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:688
static void AtCleanup_Memory(void)
Definition: xact.c:1815
int nParallelCurrentXids
Definition: xact.c:108
void AtCleanup_Portals(void)
Definition: portalmem.c:849
TransactionId * childXids
Definition: xact.c:183
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
#define FATAL
Definition: elog.h:52
SubTransactionId subTransactionId
Definition: xact.c:174
FullTransactionId fullTransactionId
Definition: xact.c:173
int parallelModeLevel
Definition: xact.c:191
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
#define InvalidSubTransactionId
Definition: c.h:520
#define InvalidFullTransactionId
Definition: transam.h:52
#define elog(elevel,...)
Definition: elog.h:228
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1005 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(), addFkRecurseReferenced(), addFkRecurseReferencing(), AddRoleMems(), AlterPublicationOptions(), AlterRole(), apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAlterColumnType(), ATExecCmd(), ATExecDetachPartition(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropIdentity(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), AttachPartitionEnsureIndexes(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTrigger(), 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(), IndexSetParentIndex(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), LogicalRepSyncTableStart(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), ri_PerformCheck(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), standard_ProcessUtility(), StoreConstraints(), StorePartitionBound(), tryAttachPartitionForeignKey(), vacuum(), and validatePartitionedIndex().

1006 {
1007  /*
1008  * If the current value of the command counter hasn't been "used" to mark
1009  * tuples, we need not increment it, since there's no need to distinguish
1010  * a read-only command from others. This helps postpone command counter
1011  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1012  */
1014  {
1015  /*
1016  * Workers synchronize transaction state at the beginning of each
1017  * parallel operation, so we can't account for new commands after that
1018  * point.
1019  */
1021  elog(ERROR, "cannot start commands during a parallel operation");
1022 
1023  currentCommandId += 1;
1025  {
1026  currentCommandId -= 1;
1027  ereport(ERROR,
1028  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1029  errmsg("cannot have more than 2^32-2 commands in a transaction")));
1030  }
1031  currentCommandIdUsed = false;
1032 
1033  /* Propagate new command ID into static snapshots */
1035 
1036  /*
1037  * Make any catalog changes done by the just-completed command visible
1038  * in the local syscache. We obviously don't need to do this after a
1039  * read-only command. (But see hacks in inval.c to make real sure we
1040  * don't think a command that queued inval messages was read-only.)
1041  */
1042  AtCCI_LocalCache();
1043  }
1044 }
int errcode(int sqlerrcode)
Definition: elog.c:608
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define IsParallelWorker()
Definition: parallel.h:60
#define InvalidCommandId
Definition: c.h:531
static void AtCCI_LocalCache(void)
Definition: xact.c:1448
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:546
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
static CommandId currentCommandId
Definition: xact.c:241
static bool currentCommandIdUsed
Definition: xact.c:242

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

Definition at line 4768 of file xact.c.

References AfterTriggerEndSubXact(), AtEOSubXact_ApplyLauncher(), 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::fullTransactionId, FullTransactionIdIsValid, TransactionStateData::gucNestLevel, IsInParallelMode(), TransactionStateData::nestingLevel, 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, TransStateAsString(), WARNING, XactLockTableDelete(), XactReadOnly, and XidFromFullTransactionId.

Referenced by CommitTransactionCommand(), and ReleaseCurrentSubTransaction().

4769 {
4771 
4772  ShowTransactionState("CommitSubTransaction");
4773 
4774  if (s->state != TRANS_INPROGRESS)
4775  elog(WARNING, "CommitSubTransaction while in %s state",
4777 
4778  /* Pre-commit processing goes here */
4779 
4781  s->parent->subTransactionId);
4782 
4783  /* If in parallel mode, clean up workers and exit parallel mode. */
4784  if (IsInParallelMode())
4785  {
4787  s->parallelModeLevel = 0;
4788  }
4789 
4790  /* Do the actual "commit", such as it is */
4791  s->state = TRANS_COMMIT;
4792 
4793  /* Must CCI to ensure commands of subtransaction are seen as done */
4795 
4796  /*
4797  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4798  * perform that step, if required, as part of the atomic update of the
4799  * whole transaction tree at top level commit or abort.
4800  */
4801 
4802  /* Post-commit cleanup */
4805  AfterTriggerEndSubXact(true);
4810  s->parent->subTransactionId);
4812 
4814  s->parent->subTransactionId);
4815 
4818  true, false);
4820  s->parent->subTransactionId);
4821  AtEOSubXact_Inval(true);
4822  AtSubCommit_smgr();
4823 
4824  /*
4825  * The only lock we actually release here is the subtransaction XID lock.
4826  */
4830 
4831  /*
4832  * Other locks should get transferred to their parent resource owner.
4833  */
4836  true, false);
4839  true, false);
4840 
4841  AtEOXact_GUC(true, s->gucNestLevel);
4844  s->parent->subTransactionId);
4846  s->parent->subTransactionId);
4848  s->parent->subTransactionId);
4850  AtEOSubXact_PgStat(true, s->nestingLevel);
4853 
4854  /*
4855  * We need to restore the upper transaction's read-only state, in case the
4856  * upper is read-write while the child is read-only; GUC will incorrectly
4857  * think it should leave the child state in place.
4858  */
4860 
4864  s->curTransactionOwner = NULL;
4865 
4867 
4868  s->state = TRANS_DEFAULT;
4869 
4870  PopTransaction();
4871 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3076
static TransactionState CurrentTransactionState
Definition: xact.c:234
static void AtSubCommit_Memory(void)
Definition: xact.c:1494
static void PopTransaction(void)
Definition: xact.c:5118
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1860
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:688
void AtSubCommit_smgr(void)
Definition: storage.c:575
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:385
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:1034
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static void AtSubCommit_childXids(void)
Definition: xact.c:1523
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
bool IsInParallelMode(void)
Definition: xact.c:996
struct TransactionStateData * parent
Definition: xact.c:193
void AtSubCommit_Notify(void)
Definition: async.c:1756
static void ShowTransactionState(const char *str)
Definition: xact.c:5287
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:890
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5792
SubTransactionId subTransactionId
Definition: xact.c:174
FullTransactionId fullTransactionId
Definition: xact.c:173
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2179
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:632
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:14721
void CommandCounterIncrement(void)
Definition: xact.c:1005
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2785
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3568
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:191
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
bool prevXactReadOnly
Definition: xact.c:188
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5108
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define elog(elevel,...)
Definition: elog.h:228
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:600
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4069
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1169
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:934

◆ CommitTransaction()

static void CommitTransaction ( void  )
static

Definition at line 2045 of file xact.c.

References AfterTriggerEndXact(), AfterTriggerFireDeferred(), Assert, AtCommit_Memory(), AtCommit_Notify(), AtEOXact_ApplyLauncher(), AtEOXact_Buffers(), AtEOXact_ComboCid(), AtEOXact_Enum(), 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::fullTransactionId, TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, InvalidFullTransactionId, InvalidSubTransactionId, InvalidTransactionId, IsInParallelMode(), PGPROC::lxid, TransactionStateData::maxChildXids, MyProc, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, 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, TransStateAsString(), WARNING, XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_COMMIT, XACT_EVENT_PARALLEL_PRE_COMMIT, XACT_EVENT_PRE_COMMIT, and XactLastRecEnd.

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

2046 {
2048  TransactionId latestXid;
2049  bool is_parallel_worker;
2050 
2051  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2052 
2053  /* Enforce parallel mode restrictions during parallel worker commit. */
2054  if (is_parallel_worker)
2056 
2057  ShowTransactionState("CommitTransaction");
2058 
2059  /*
2060  * check the current transaction state
2061  */
2062  if (s->state != TRANS_INPROGRESS)
2063  elog(WARNING, "CommitTransaction while in %s state",
2065  Assert(s->parent == NULL);
2066 
2067  /*
2068  * Do pre-commit processing that involves calling user-defined code, such
2069  * as triggers. Since closing cursors could queue trigger actions,
2070  * triggers could open cursors, etc, we have to keep looping until there's
2071  * nothing left to do.
2072  */
2073  for (;;)
2074  {
2075  /*
2076  * Fire all currently pending deferred triggers.
2077  */
2079 
2080  /*
2081  * Close open portals (converting holdable ones into static portals).
2082  * If there weren't any, we are done ... otherwise loop back to check
2083  * if they queued deferred triggers. Lather, rinse, repeat.
2084  */
2085  if (!PreCommit_Portals(false))
2086  break;
2087  }
2088 
2091 
2092  /*
2093  * The remaining actions cannot call any user-defined code, so it's safe
2094  * to start shutting down within-transaction services. But note that most
2095  * of this stuff could still throw an error, which would switch us into
2096  * the transaction-abort path.
2097  */
2098 
2099  /* If we might have parallel workers, clean them up now. */
2100  if (IsInParallelMode())
2101  AtEOXact_Parallel(true);
2102 
2103  /* Shut down the deferred-trigger manager */
2104  AfterTriggerEndXact(true);
2105 
2106  /*
2107  * Let ON COMMIT management do its thing (must happen after closing
2108  * cursors, to avoid dangling-reference problems)
2109  */
2111 
2112  /* close large objects before lower-level cleanup */
2113  AtEOXact_LargeObject(true);
2114 
2115  /*
2116  * Insert notifications sent by NOTIFY commands into the queue. This
2117  * should be late in the pre-commit sequence to minimize time spent
2118  * holding the notify-insertion lock. However, this could result in
2119  * creating a snapshot, so we must do it before serializable cleanup.
2120  */
2121  PreCommit_Notify();
2122 
2123  /*
2124  * Mark serializable transaction as complete for predicate locking
2125  * purposes. This should be done as late as we can put it and still allow
2126  * errors to be raised for failure patterns found at commit. This is not
2127  * appropriate in a parallel worker however, because we aren't committing
2128  * the leader's transaction and its serializable state will live on.
2129  */
2130  if (!is_parallel_worker)
2132 
2133  /* Prevent cancel/die interrupt while cleaning up */
2134  HOLD_INTERRUPTS();
2135 
2136  /* Commit updates to the relation map --- do this as late as possible */
2137  AtEOXact_RelationMap(true, is_parallel_worker);
2138 
2139  /*
2140  * set the current transaction state information appropriately during
2141  * commit processing
2142  */
2143  s->state = TRANS_COMMIT;
2144  s->parallelModeLevel = 0;
2145 
2146  if (!is_parallel_worker)
2147  {
2148  /*
2149  * We need to mark our XIDs as committed in pg_xact. This is where we
2150  * durably commit.
2151  */
2152  latestXid = RecordTransactionCommit();
2153  }
2154  else
2155  {
2156  /*
2157  * We must not mark our XID committed; the parallel master is
2158  * responsible for that.
2159  */
2160  latestXid = InvalidTransactionId;
2161 
2162  /*
2163  * Make sure the master will know about any WAL we wrote before it
2164  * commits.
2165  */
2167  }
2168 
2169  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2170 
2171  /*
2172  * Let others know about no transaction in progress by me. Note that this
2173  * must be done _before_ releasing locks we hold and _after_
2174  * RecordTransactionCommit.
2175  */
2176  ProcArrayEndTransaction(MyProc, latestXid);
2177 
2178  /*
2179  * This is all post-commit cleanup. Note that if an error is raised here,
2180  * it's too late to abort the transaction. This should be just
2181  * noncritical resource releasing.
2182  *
2183  * The ordering of operations is not entirely random. The idea is:
2184  * release resources visible to other backends (eg, files, buffer pins);
2185  * then release locks; then release backend-local resources. We want to
2186  * release locks at the point where any backend waiting for us will see
2187  * our transaction as being fully cleaned up.
2188  *
2189  * Resources that can be associated with individual queries are handled by
2190  * the ResourceOwner mechanism. The other calls here are for backend-wide
2191  * state.
2192  */
2193 
2194  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2195  : XACT_EVENT_COMMIT);
2196 
2199  true, true);
2200 
2201  /* Check we've released all buffer pins */
2202  AtEOXact_Buffers(true);
2203 
2204  /* Clean up the relation cache */
2205  AtEOXact_RelationCache(true);
2206 
2207  /*
2208  * Make catalog changes visible to all backends. This has to happen after
2209  * relcache references are dropped (see comments for
2210  * AtEOXact_RelationCache), but before locks are released (if anyone is
2211  * waiting for lock on a relation we've modified, we want them to know
2212  * about the catalog change before they start using the relation).
2213  */
2214  AtEOXact_Inval(true);
2215 
2217 
2220  true, true);
2223  true, true);
2224 
2225  /*
2226  * Likewise, dropping of files deleted during the transaction is best done
2227  * after releasing relcache and buffer pins. (This is not strictly
2228  * necessary during commit, since such pins should have been released
2229  * already, but this ordering is definitely critical during abort.) Since
2230  * this may take many seconds, also delay until after releasing locks.
2231  * Other backends will observe the attendant catalog changes and not
2232  * attempt to access affected files.
2233  */
2234  smgrDoPendingDeletes(true);
2235 
2236  AtCommit_Notify();
2237  AtEOXact_GUC(true, 1);
2238  AtEOXact_SPI(true);
2239  AtEOXact_Enum();
2241  AtEOXact_Namespace(true, is_parallel_worker);
2242  AtEOXact_SMgr();
2243  AtEOXact_Files(true);
2245  AtEOXact_HashTables(true);
2246  AtEOXact_PgStat(true, is_parallel_worker);
2247  AtEOXact_Snapshot(true, false);
2248  AtEOXact_ApplyLauncher(true);
2250 
2251  CurrentResourceOwner = NULL;
2253  s->curTransactionOwner = NULL;
2256 
2257  AtCommit_Memory();
2258 
2261  s->nestingLevel = 0;
2262  s->gucNestLevel = 0;
2263  s->childXids = NULL;
2264  s->nChildXids = 0;
2265  s->maxChildXids = 0;
2266 
2269 
2270  /*
2271  * done with commit processing, set current transaction state back to
2272  * default
2273  */
2274  s->state = TRANS_DEFAULT;
2275 
2277 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:848
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:430
static TransactionState CurrentTransactionState
Definition: xact.c:234
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:949
uint32 TransactionId
Definition: c.h:514
static void AtCommit_Memory(void)
Definition: xact.c:1467
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3321
XLogRecPtr XactLastRecEnd
Definition: xlog.c:352
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2938
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2423
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:688
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3513
int nParallelCurrentXids
Definition: xact.c:108
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:14689
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:398
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1208
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2818
TBlockState blockState
Definition: xact.c:178
TransactionId * childXids
Definition: xact.c:183
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4812
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void PreCommit_Notify(void)
Definition: async.c:885
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
void AtEOXact_SMgr(void)
Definition: smgr.c:655
bool IsInParallelMode(void)
Definition: xact.c:996
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5012
struct TransactionStateData * parent
Definition: xact.c:193
static void ShowTransactionState(const char *str)
Definition: xact.c:5287
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2092
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5792
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:174
FullTransactionId fullTransactionId
Definition: xact.c:173
#define WARNING
Definition: elog.h:40
void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
Definition: parallel.c:1453
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4956
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5391
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:363
void AtEOXact_MultiXact(void)
Definition: multixact.c:1664
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void EnterParallelMode(void)
Definition: xact.c:963
#define InvalidSubTransactionId
Definition: c.h:520
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:670
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:14589
#define InvalidFullTransactionId
Definition: transam.h:52
void AtCommit_Notify(void)
Definition: async.c:987
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:228
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1834
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1188
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4003
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091
LocalTransactionId lxid
Definition: proc.h:106

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2898 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AssertState, TransactionStateData::blockState, BlockStateAsString(), TransactionStateData::chain, CleanupSubTransaction(), CleanupTransaction(), CommandCounterIncrement(), CommitSubTransaction(), CommitTransaction(), CommitTransactionCommand(), CurrentTransactionState, DefineSavepoint(), elog, ERROR, FATAL, TransactionStateData::name, TransactionStateData::parent, PrepareTransaction(), RestoreTransactionCharacteristics(), TransactionStateData::savepointLevel, SaveTransactionCharacteristics(), StartSubTransaction(), StartTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, 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 _SPI_commit(), apply_handle_commit(), ApplyWorkerMain(), autoprewarm_database_main(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), CommitTransactionCommand(), DefineIndex(), do_autovacuum(), exec_replication_command(), finish_xact_command(), get_database_list(), get_subscription_list(), IdentifySystem(), index_drop(), initialize_worker_spi(), InitPostgres(), LogicalRepSyncTableStart(), maybe_reread_subscription(), movedb(), ParallelWorkerMain(), pg_attribute_noreturn(), process_syncing_tables_for_apply(), ProcessCatchupInterrupt(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleTables(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

2899 {
2901 
2902  if (s->chain)
2904 
2905  switch (s->blockState)
2906  {
2907  /*
2908  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2909  * StartTransactionCommand didn't set the STARTED state
2910  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2911  * by EndParallelWorkerTransaction(), not this function.
2912  */
2913  case TBLOCK_DEFAULT:
2915  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2917  break;
2918 
2919  /*
2920  * If we aren't in a transaction block, just do our usual
2921  * transaction commit, and return to the idle state.
2922  */
2923  case TBLOCK_STARTED:
2926  break;
2927 
2928  /*
2929  * We are completing a "BEGIN TRANSACTION" command, so we change
2930  * to the "transaction block in progress" state and return. (We
2931  * assume the BEGIN did nothing to the database, so we need no
2932  * CommandCounterIncrement.)
2933  */
2934  case TBLOCK_BEGIN:
2936  break;
2937 
2938  /*
2939  * This is the case when we have finished executing a command
2940  * someplace within a transaction block. We increment the command
2941  * counter and return.
2942  */
2943  case TBLOCK_INPROGRESS:
2945  case TBLOCK_SUBINPROGRESS:
2947  break;
2948 
2949  /*
2950  * We are completing a "COMMIT" command. Do it and return to the
2951  * idle state.
2952  */
2953  case TBLOCK_END:
2956  if (s->chain)
2957  {
2958  StartTransaction();
2960  s->chain = false;
2962  }
2963  break;
2964 
2965  /*
2966  * Here we are in the middle of a transaction block but one of the
2967  * commands caused an abort so we do nothing but remain in the
2968  * abort state. Eventually we will get a ROLLBACK command.
2969  */
2970  case TBLOCK_ABORT:
2971  case TBLOCK_SUBABORT:
2972  break;
2973 
2974  /*
2975  * Here we were in an aborted transaction block and we just got
2976  * the ROLLBACK command from the user, so clean up the
2977  * already-aborted transaction and return to the idle state.
2978  */
2979  case TBLOCK_ABORT_END:
2982  if (s->chain)
2983  {
2984  StartTransaction();
2986  s->chain = false;
2988  }
2989  break;
2990 
2991  /*
2992  * Here we were in a perfectly good transaction block but the user
2993  * told us to ROLLBACK anyway. We have to abort the transaction
2994  * and then clean up.
2995  */
2996  case TBLOCK_ABORT_PENDING:
2997  AbortTransaction();
3000  if (s->chain)
3001  {
3002  StartTransaction();
3004  s->chain = false;
3006  }
3007  break;
3008 
3009  /*
3010  * We are completing a "PREPARE TRANSACTION" command. Do it and
3011  * return to the idle state.
3012  */
3013  case TBLOCK_PREPARE:
3016  break;
3017 
3018  /*
3019  * We were just issued a SAVEPOINT inside a transaction block.
3020  * Start a subtransaction. (DefineSavepoint already did
3021  * PushTransaction, so as to have someplace to put the SUBBEGIN
3022  * state.)
3023  */
3024  case TBLOCK_SUBBEGIN:
3027  break;
3028 
3029  /*
3030  * We were issued a RELEASE command, so we end the current
3031  * subtransaction and return to the parent transaction. The parent
3032  * might be ended too, so repeat till we find an INPROGRESS
3033  * transaction or subtransaction.
3034  */
3035  case TBLOCK_SUBRELEASE:
3036  do
3037  {
3039  s = CurrentTransactionState; /* changed by pop */
3040  } while (s->blockState == TBLOCK_SUBRELEASE);
3041 
3044  break;
3045 
3046  /*
3047  * We were issued a COMMIT, so we end the current subtransaction
3048  * hierarchy and perform final commit. We do this by rolling up
3049  * any subtransactions into their parent, which leads to O(N^2)
3050  * operations with respect to resource owners - this isn't that
3051  * bad until we approach a thousands of savepoints but is
3052  * necessary for correctness should after triggers create new
3053  * resource owners.
3054  */
3055  case TBLOCK_SUBCOMMIT:
3056  do
3057  {
3059  s = CurrentTransactionState; /* changed by pop */
3060  } while (s->blockState == TBLOCK_SUBCOMMIT);
3061  /* If we had a COMMIT command, finish off the main xact too */
3062  if (s->blockState == TBLOCK_END)
3063  {
3064  Assert(s->parent == NULL);
3067  }
3068  else if (s->blockState == TBLOCK_PREPARE)
3069  {
3070  Assert(s->parent == NULL);
3073  }
3074  else
3075  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3077  break;
3078 
3079  /*
3080  * The current already-failed subtransaction is ending due to a
3081  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3082  * examine the parent (which could be in any of several states).
3083  */
3084  case TBLOCK_SUBABORT_END:
3087  break;
3088 
3089  /*
3090  * As above, but it's not dead yet, so abort first.
3091  */
3096  break;
3097 
3098  /*
3099  * The current subtransaction is the target of a ROLLBACK TO
3100  * command. Abort and pop it, then start a new subtransaction
3101  * with the same name.
3102  */
3103  case TBLOCK_SUBRESTART:
3104  {
3105  char *name;
3106  int savepointLevel;
3107 
3108  /* save name and keep Cleanup from freeing it */
3109  name = s->name;
3110  s->name = NULL;
3111  savepointLevel = s->savepointLevel;
3112 
3115 
3116  DefineSavepoint(NULL);
3117  s = CurrentTransactionState; /* changed by push */
3118  s->name = name;
3119  s->savepointLevel = savepointLevel;
3120 
3121  /* This is the same as TBLOCK_SUBBEGIN case */
3125  }
3126  break;
3127 
3128  /*
3129  * Same as above, but the subtransaction had already failed, so we
3130  * don't need AbortSubTransaction.
3131  */
3133  {
3134  char *name;
3135  int savepointLevel;
3136 
3137  /* save name and keep Cleanup from freeing it */
3138  name = s->name;
3139  s->name = NULL;
3140  savepointLevel = s->savepointLevel;
3141 
3143 
3144  DefineSavepoint(NULL);
3145  s = CurrentTransactionState; /* changed by push */
3146  s->name = name;
3147  s->savepointLevel = savepointLevel;
3148 
3149  /* This is the same as TBLOCK_SUBBEGIN case */
3153  }
3154  break;
3155  }
3156 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AssertState(condition)
Definition: c.h:742
void CommitTransactionCommand(void)
Definition: xact.c:2898
void DefineSavepoint(const char *name)
Definition: xact.c:4038
TBlockState blockState
Definition: xact.c:178
static void PrepareTransaction(void)
Definition: xact.c:2286
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4768
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2748
struct TransactionStateData * parent
Definition: xact.c:193
void SaveTransactionCharacteristics(void)
Definition: xact.c:2878
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
static void AbortSubTransaction(void)
Definition: xact.c:4877
void CommandCounterIncrement(void)
Definition: xact.c:1005
static void AbortTransaction(void)
Definition: xact.c:2567
static void StartTransaction(void)
Definition: xact.c:1886
#define Assert(condition)
Definition: c.h:739
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2886
static void CleanupSubTransaction(void)
Definition: xact.c:5026
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:228
static void CommitTransaction(void)
Definition: xact.c:2045
static void StartSubTransaction(void)
Definition: xact.c:4731

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

Definition at line 4038 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_IMPLICIT_INPROGRESS, 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().

4039 {
4041 
4042  /*
4043  * Workers synchronize transaction state at the beginning of each parallel
4044  * operation, so we can't account for new subtransactions after that
4045  * point. (Note that this check will certainly error out if s->blockState
4046  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4047  * below.)
4048  */
4049  if (IsInParallelMode())
4050  ereport(ERROR,
4051  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4052  errmsg("cannot define savepoints during a parallel operation")));
4053 
4054  switch (s->blockState)
4055  {
4056  case TBLOCK_INPROGRESS:
4057  case TBLOCK_SUBINPROGRESS:
4058  /* Normal subtransaction start */
4059  PushTransaction();
4060  s = CurrentTransactionState; /* changed by push */
4061 
4062  /*
4063  * Savepoint names, like the TransactionState block itself, live
4064  * in TopTransactionContext.
4065  */
4066  if (name)
4068  break;
4069 
4070  /*
4071  * We disallow savepoint commands in implicit transaction blocks.
4072  * There would be no great difficulty in allowing them so far as
4073  * this module is concerned, but a savepoint seems inconsistent
4074  * with exec_simple_query's behavior of abandoning the whole query
4075  * string upon error. Also, the point of an implicit transaction
4076  * block (as opposed to a regular one) is to automatically close
4077  * after an error, so it's hard to see how a savepoint would fit
4078  * into that.
4079  *
4080  * The error messages for this are phrased as if there were no
4081  * active transaction block at all, which is historical but
4082  * perhaps could be improved.
4083  */
4085  ereport(ERROR,
4086  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4087  /* translator: %s represents an SQL statement name */
4088  errmsg("%s can only be used in transaction blocks",
4089  "SAVEPOINT")));
4090  break;
4091 
4092  /* These cases are invalid. */
4093  case TBLOCK_DEFAULT:
4094  case TBLOCK_STARTED:
4095  case TBLOCK_BEGIN:
4097  case TBLOCK_SUBBEGIN:
4098  case TBLOCK_END:
4099  case TBLOCK_SUBRELEASE:
4100  case TBLOCK_SUBCOMMIT:
4101  case TBLOCK_ABORT:
4102  case TBLOCK_SUBABORT:
4103  case TBLOCK_ABORT_END:
4104  case TBLOCK_SUBABORT_END:
4105  case TBLOCK_ABORT_PENDING:
4107  case TBLOCK_SUBRESTART:
4109  case TBLOCK_PREPARE:
4110  elog(FATAL, "DefineSavepoint: unexpected state %s",
4112  break;
4113  }
4114 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
MemoryContext TopTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5059
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:228

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4016 of file xact.c.

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

Referenced by exec_simple_query().

4017 {
4019 
4020  /*
4021  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4022  * allowing CommitTransactionCommand to commit whatever happened during
4023  * the implicit transaction block as though it were a single statement.
4024  *
4025  * For caller convenience, we consider all other transaction states as
4026  * legal here; otherwise the caller would need its own state check, which
4027  * seems rather pointless.
4028  */
4031 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5275 of file xact.c.

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

Referenced by ParallelWorkerMain().

5276 {
5280 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
#define Assert(condition)
Definition: c.h:739
static void CommitTransaction(void)
Definition: xact.c:2045

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3709 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), TransactionStateData::chain, CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, TransactionStateData::parent, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, 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().

3710 {
3712  bool result = false;
3713 
3714  switch (s->blockState)
3715  {
3716  /*
3717  * We are in a transaction block, so tell CommitTransactionCommand
3718  * to COMMIT.
3719  */
3720  case TBLOCK_INPROGRESS:
3721  s->blockState = TBLOCK_END;
3722  result = true;
3723  break;
3724 
3725  /*
3726  * We are in an implicit transaction block. If AND CHAIN was
3727  * specified, error. Otherwise commit, but issue a warning
3728  * because there was no explicit BEGIN before this.
3729  */
3731  if (chain)
3732  ereport(ERROR,
3733  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3734  /* translator: %s represents an SQL statement name */
3735  errmsg("%s can only be used in transaction blocks",
3736  "COMMIT AND CHAIN")));
3737  else
3738  ereport(WARNING,
3739  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3740  errmsg("there is no transaction in progress")));
3741  s->blockState = TBLOCK_END;
3742  result = true;
3743  break;
3744 
3745  /*
3746  * We are in a failed transaction block. Tell
3747  * CommitTransactionCommand it's time to exit the block.
3748  */
3749  case TBLOCK_ABORT:
3751  break;
3752 
3753  /*
3754  * We are in a live subtransaction block. Set up to subcommit all
3755  * open subtransactions and then commit the main transaction.
3756  */
3757  case TBLOCK_SUBINPROGRESS:
3758  while (s->parent != NULL)
3759  {
3760  if (s->blockState == TBLOCK_SUBINPROGRESS)
3762  else
3763  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3765  s = s->parent;
3766  }
3767  if (s->blockState == TBLOCK_INPROGRESS)
3768  s->blockState = TBLOCK_END;
3769  else
3770  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3772  result = true;
3773  break;
3774 
3775  /*
3776  * Here we are inside an aborted subtransaction. Treat the COMMIT
3777  * as ROLLBACK: set up to abort everything and exit the main
3778  * transaction.
3779  */
3780  case TBLOCK_SUBABORT:
3781  while (s->parent != NULL)
3782  {
3783  if (s->blockState == TBLOCK_SUBINPROGRESS)
3785  else if (s->blockState == TBLOCK_SUBABORT)
3787  else
3788  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3790  s = s->parent;
3791  }
3792  if (s->blockState == TBLOCK_INPROGRESS)
3794  else if (s->blockState == TBLOCK_ABORT)
3796  else
3797  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3799  break;
3800 
3801  /*
3802  * The user issued COMMIT when not inside a transaction. For
3803  * COMMIT without CHAIN, issue a WARNING, staying in
3804  * TBLOCK_STARTED state. The upcoming call to
3805  * CommitTransactionCommand() will then close the transaction and
3806  * put us back into the default state. For COMMIT AND CHAIN,
3807  * error.
3808  */
3809  case TBLOCK_STARTED:
3810  if (chain)
3811  ereport(ERROR,
3812  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3813  /* translator: %s represents an SQL statement name */
3814  errmsg("%s can only be used in transaction blocks",
3815  "COMMIT AND CHAIN")));
3816  else
3817  ereport(WARNING,
3818  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3819  errmsg("there is no transaction in progress")));
3820  result = true;
3821  break;
3822 
3823  /*
3824  * The user issued a COMMIT that somehow ran inside a parallel
3825  * worker. We can't cope with that.
3826  */
3828  ereport(FATAL,
3829  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3830  errmsg("cannot commit during a parallel operation")));
3831  break;
3832 
3833  /* These cases are invalid. */
3834  case TBLOCK_DEFAULT:
3835  case TBLOCK_BEGIN:
3836  case TBLOCK_SUBBEGIN:
3837  case TBLOCK_END:
3838  case TBLOCK_SUBRELEASE:
3839  case TBLOCK_SUBCOMMIT:
3840  case TBLOCK_ABORT_END:
3841  case TBLOCK_SUBABORT_END:
3842  case TBLOCK_ABORT_PENDING:
3844  case TBLOCK_SUBRESTART:
3846  case TBLOCK_PREPARE:
3847  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3849  break;
3850  }
3851 
3853  s->blockState == TBLOCK_END ||
3854  s->blockState == TBLOCK_ABORT_END ||
3856 
3857  s->chain = chain;
3858 
3859  return result;
3860 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:193
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 963 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

964 {
966 
967  Assert(s->parallelModeLevel >= 0);
968 
969  ++s->parallelModeLevel;
970 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5152 of file xact.c.

References add_size(), TransactionStateData::fullTransactionId, FullTransactionIdIsValid, mul_size(), TransactionStateData::nChildXids, TransactionStateData::parent, and SerializedTransactionStateHeaderSize.

Referenced by InitializeParallelDSM().

5153 {
5154  TransactionState s;
5155  Size nxids = 0;
5157 
5158  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5159  {
5161  nxids = add_size(nxids, 1);
5162  nxids = add_size(nxids, s->nChildXids);
5163  }
5164 
5165  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5166 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
uint32 TransactionId
Definition: c.h:514
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
struct TransactionStateData * parent
Definition: xact.c:193
FullTransactionId fullTransactionId
Definition: xact.c:173
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define SerializedTransactionStateHeaderSize
Definition: xact.c:214
size_t Size
Definition: c.h:467

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 976 of file xact.c.

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

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

977 {
979 
980  Assert(s->parallelModeLevel > 0);
982 
983  --s->parallelModeLevel;
984 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
bool ParallelContextActive(void)
Definition: parallel.c:944
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1053 of file xact.c.

References forceSyncCommit.

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

1054 {
1055  forceSyncCommit = true;
1056 }
static bool forceSyncCommit
Definition: xact.c:264

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 745 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

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

746 {
747  /* this is global to a transaction, not subtransaction-local */
748  if (used)
749  {
750  /*
751  * Forbid setting currentCommandIdUsed in a parallel worker, because
752  * we have no provision for communicating this back to the master. We
753  * could relax this restriction when currentCommandIdUsed was already
754  * true at the start of the parallel operation.
755  */
757  currentCommandIdUsed = true;
758  }
759  return currentCommandId;
760 }
#define IsParallelWorker()
Definition: parallel.h:60
#define Assert(condition)
Definition: c.h:739
static CommandId currentCommandId
Definition: xact.c:241
static bool currentCommandIdUsed
Definition: xact.c:242

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 480 of file xact.c.

References AssignTransactionId(), CurrentTransactionState, TransactionStateData::fullTransactionId, and FullTransactionIdIsValid.

481 {
483 
486  return s->fullTransactionId;
487 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:173
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 498 of file xact.c.

References TransactionStateData::fullTransactionId.

499 {
501 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 791 of file xact.c.

References stmtStartTimestamp.

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

792 {
793  return stmtStartTimestamp;
794 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:252

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 439 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

440 {
442 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 803 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

804 {
805  if (xactStopTimestamp != 0)
806  return xactStopTimestamp;
807  return GetCurrentTimestamp();
808 }
static TimestampTz xactStopTimestamp
Definition: xact.c:253
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 525 of file xact.c.

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

Referenced by xid_age().

526 {
528  static TransactionId stablexid = InvalidTransactionId;
529 
530  if (lxid != MyProc->lxid)
531  {
532  lxid = MyProc->lxid;
533  stablexid = GetTopTransactionIdIfAny();
534  if (!TransactionIdIsValid(stablexid))
535  stablexid = ReadNewTransactionId();
536  }
537 
538  Assert(TransactionIdIsValid(stablexid));
539 
540  return stablexid;
541 }
uint32 TransactionId
Definition: c.h:514
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:516
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:409
#define Assert(condition)
Definition: c.h:739
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
#define InvalidLocalTransactionId
Definition: lock.h:68
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:106

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 451 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

452 {
456 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
static TransactionStateData TopTransactionStateData
Definition: xact.c:222
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 467 of file xact.c.

References XactTopFullTransactionId.

468 {
470 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 394 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

Referenced by AlterSequence(), AssignTransactionId(), do_setval(), fill_seq_with_data(), log_heap_new_cid(), nextval_internal(), and txid_current().

395 {
399 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:222
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3444 of file xact.c.

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

Referenced by vacuum().

3445 {
3446  /*
3447  * Return true on same conditions that would make
3448  * PreventInTransactionBlock error out
3449  */
3450  if (IsTransactionBlock())
3451  return true;
3452 
3453  if (IsSubTransaction())
3454  return true;
3455 
3456  if (!isTopLevel)
3457  return true;
3458 
3461  return true;
3462 
3463  return false;
3464 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4635
bool IsSubTransaction(void)
Definition: xact.c:4708

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4635 of file xact.c.

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

Referenced by CheckTransactionBlock(), CreateReplicationSlot(), exec_replication_command(), IsInTransactionBlock(), PreventInTransactionBlock(), and standard_ProcessUtility().

4636 {
4638 
4640  return false;
4641 
4642  return true;
4643 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 355 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit(), apply_handle_origin(), check_client_encoding(), check_default_table_access_method(), check_default_tablespace(), check_role(), check_session_authorization(), check_temp_tablespaces(), check_transaction_read_only(), check_TSCurrentConfig(), check_XactIsoLevel(), CreateInitDecodingContext(), ensure_transaction(), GetMessageEncoding(), LogLogicalMessage(), maybe_reread_subscription(), pg_attribute_noreturn(), pg_do_encoding_conversion(), PrepareClientEncoding(), PrepareTempTablespaces(), process_syncing_tables_for_apply(), process_syncing_tables_for_sync(), RelationClearRelation(), RelationIdGetRelation(), RelationInitPhysicalAddr(), RelationReloadNailed(), replorigin_create(), replorigin_drop(), SearchCatCacheInternal(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

356 {
358 
359  /*
360  * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
361  * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
362  * TRANS_PREPARE since it might be too soon or too late within those
363  * transition states to do anything interesting. Hence, the only "valid"
364  * state is TRANS_INPROGRESS.
365  */
366  return (s->state == TRANS_INPROGRESS);
367 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TransState state
Definition: xact.c:177

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 509 of file xact.c.

References TransactionStateData::didLogXid, TransactionStateData::fullTransactionId, and FullTransactionIdIsValid.

Referenced by XLogInsertRecord().

510 {
513 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ PopTransaction()

static void PopTransaction ( void  )