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 "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 "pg_trace.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 215 of file xact.c.

Referenced by EstimateTransactionStateSpace(), and SerializeTransactionState().

Typedef Documentation

◆ SerializedTransactionState

◆ SubXactCallbackItem

◆ TBlockState

◆ TransactionState

Definition at line 197 of file xact.c.

◆ TransactionStateData

◆ 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 140 of file xact.c.

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

◆ TransState

enum TransState
Enumerator
TRANS_DEFAULT 
TRANS_START 
TRANS_INPROGRESS 
TRANS_COMMIT 
TRANS_ABORT 
TRANS_PREPARE 

Definition at line 124 of file xact.c.

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

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3159 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().

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

Definition at line 4505 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().

4506 {
4508 
4509  /* Ensure we're not running in a doomed memory context */
4510  AtAbort_Memory();
4511 
4512  /*
4513  * Get out of any transaction or nested transaction
4514  */
4515  do
4516  {
4517  switch (s->blockState)
4518  {
4519  case TBLOCK_DEFAULT:
4520  if (s->state == TRANS_DEFAULT)
4521  {
4522  /* Not in a transaction, do nothing */
4523  }
4524  else
4525  {
4526  /*
4527  * We can get here after an error during transaction start
4528  * (state will be TRANS_START). Need to clean up the
4529  * incompletely started transaction. First, adjust the
4530  * low-level state to suppress warning message from
4531  * AbortTransaction.
4532  */
4533  if (s->state == TRANS_START)
4534  s->state = TRANS_INPROGRESS;
4535  AbortTransaction();
4537  }
4538  break;
4539  case TBLOCK_STARTED:
4540  case TBLOCK_BEGIN:
4541  case TBLOCK_INPROGRESS:
4544  case TBLOCK_END:
4545  case TBLOCK_ABORT_PENDING:
4546  case TBLOCK_PREPARE:
4547  /* In a transaction, so clean up */
4548  AbortTransaction();
4551  break;
4552  case TBLOCK_ABORT:
4553  case TBLOCK_ABORT_END:
4554 
4555  /*
4556  * AbortTransaction is already done, still need Cleanup.
4557  * However, if we failed partway through running ROLLBACK,
4558  * there will be an active portal running that command, which
4559  * we need to shut down before doing CleanupTransaction.
4560  */
4561  AtAbort_Portals();
4564  break;
4565 
4566  /*
4567  * In a subtransaction, so clean it up and abort parent too
4568  */
4569  case TBLOCK_SUBBEGIN:
4570  case TBLOCK_SUBINPROGRESS:
4571  case TBLOCK_SUBRELEASE:
4572  case TBLOCK_SUBCOMMIT:
4574  case TBLOCK_SUBRESTART:
4577  s = CurrentTransactionState; /* changed by pop */
4578  break;
4579 
4580  case TBLOCK_SUBABORT:
4581  case TBLOCK_SUBABORT_END:
4583  /* As above, but AbortSubTransaction already done */
4584  if (s->curTransactionOwner)
4585  {
4586  /* As in TBLOCK_ABORT, might have a live portal to zap */
4591  }
4593  s = CurrentTransactionState; /* changed by pop */
4594  break;
4595  }
4596  } while (s->blockState != TBLOCK_DEFAULT);
4597 
4598  /* Should be out of all subxacts now */
4599  Assert(s->parent == NULL);
4600 
4601  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4602  AtCleanup_Memory();
4603 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void AtCleanup_Memory(void)
Definition: xact.c:1813
void AtAbort_Portals(void)
Definition: portalmem.c:772
TBlockState blockState
Definition: xact.c:179
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
static void CleanupTransaction(void)
Definition: xact.c:2745
struct TransactionStateData * parent
Definition: xact.c:194
SubTransactionId subTransactionId
Definition: xact.c:175
static void AbortSubTransaction(void)
Definition: xact.c:4852
static void AtAbort_Memory(void)
Definition: xact.c:1723
static void AbortTransaction(void)
Definition: xact.c:2564
#define Assert(condition)
Definition: c.h:732
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:968
static void CleanupSubTransaction(void)
Definition: xact.c:5001

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

Definition at line 4852 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().

4853 {
4855 
4856  /* Prevent cancel/die interrupt while cleaning up */
4857  HOLD_INTERRUPTS();
4858 
4859  /* Make sure we have a valid memory context and resource owner */
4862 
4863  /*
4864  * Release any LW locks we might be holding as quickly as possible.
4865  * (Regular locks, however, must be held till we finish aborting.)
4866  * Releasing LW locks is critical since we might try to grab them again
4867  * while cleaning up!
4868  *
4869  * FIXME This may be incorrect --- Are there some locks we should keep?
4870  * Buffer locks, for example? I don't think so but I'm not sure.
4871  */
4872  LWLockReleaseAll();
4873 
4876  AbortBufferIO();
4877  UnlockBuffers();
4878 
4879  /* Reset WAL record construction state */
4881 
4882  /* Cancel condition variable sleep */
4884 
4885  /*
4886  * Also clean up any open wait for lock, since the lock manager will choke
4887  * if we try to wait for another lock before doing this.
4888  */
4889  LockErrorCleanup();
4890 
4891  /*
4892  * If any timeout events are still active, make sure the timeout interrupt
4893  * is scheduled. This covers possible loss of a timeout interrupt due to
4894  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4895  * We delay this till after LockErrorCleanup so that we don't uselessly
4896  * reschedule lock or deadlock check timeouts.
4897  */
4899 
4900  /*
4901  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4902  * handler. We do this fairly early in the sequence so that the timeout
4903  * infrastructure will be functional if needed while aborting.
4904  */
4906 
4907  /*
4908  * check the current transaction state
4909  */
4910  ShowTransactionState("AbortSubTransaction");
4911 
4912  if (s->state != TRANS_INPROGRESS)
4913  elog(WARNING, "AbortSubTransaction while in %s state",
4915 
4916  s->state = TRANS_ABORT;
4917 
4918  /*
4919  * Reset user ID which might have been changed transiently. (See notes in
4920  * AbortTransaction.)
4921  */
4923 
4924  /* Exit from parallel mode, if necessary. */
4925  if (IsInParallelMode())
4926  {
4928  s->parallelModeLevel = 0;
4929  }
4930 
4931  /*
4932  * We can skip all this stuff if the subxact failed before creating a
4933  * ResourceOwner...
4934  */
4935  if (s->curTransactionOwner)
4936  {
4937  AfterTriggerEndSubXact(false);
4943  s->parent->subTransactionId);
4945 
4946  /* Advertise the fact that we aborted in pg_xact. */
4947  (void) RecordTransactionAbort(true);
4948 
4949  /* Post-abort cleanup */
4952 
4954  s->parent->subTransactionId);
4955 
4958  false, false);
4960  s->parent->subTransactionId);
4961  AtEOSubXact_Inval(false);
4964  false, false);
4967  false, false);
4968  AtSubAbort_smgr();
4969 
4970  AtEOXact_GUC(false, s->gucNestLevel);
4971  AtEOSubXact_SPI(false, s->subTransactionId);
4973  s->parent->subTransactionId);
4975  s->parent->subTransactionId);
4977  s->parent->subTransactionId);
4979  AtEOSubXact_PgStat(false, s->nestingLevel);
4982  }
4983 
4984  /*
4985  * Restore the upper transaction's read-only state, too. This should be
4986  * redundant with GUC's cleanup but we may as well do it for consistency
4987  * with the commit case.
4988  */
4990 
4992 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1768
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3077
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:408
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1055
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
bool IsInParallelMode(void)
Definition: xact.c:994
void ConditionVariableCancelSleep(void)
static void AtSubAbort_Memory(void)
Definition: xact.c:1743
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:194
static void ShowTransactionState(const char *str)
Definition: xact.c:5262
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:900
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
SubTransactionId subTransactionId
Definition: xact.c:175
sigset_t UnBlockSig
Definition: pqsignal.c:22
FullTransactionId fullTransactionId
Definition: xact.c:174
static void AtSubAbort_childXids(void)
Definition: xact.c:1781
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2181
void LockErrorCleanup(void)
Definition: proc.c:712
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:634
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3562
void AtSubAbort_Notify(void)
Definition: async.c:1729
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:14683
void pgstat_progress_end_command(void)
Definition: pgstat.c:3271
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2750
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3566
bool XactReadOnly
Definition: xact.c:78
int parallelModeLevel
Definition: xact.c:192
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5366
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:968
void AtSubAbort_smgr(void)
Definition: storage.c:564
bool prevXactReadOnly
Definition: xact.c:189
void AbortBufferIO(void)
Definition: bufmgr.c:4018
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5100
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:226
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:1170
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1611

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

Definition at line 2564 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().

2565 {
2567  TransactionId latestXid;
2568  bool is_parallel_worker;
2569 
2570  /* Prevent cancel/die interrupt while cleaning up */
2571  HOLD_INTERRUPTS();
2572 
2573  /* Make sure we have a valid memory context and resource owner */
2574  AtAbort_Memory();
2576 
2577  /*
2578  * Release any LW locks we might be holding as quickly as possible.
2579  * (Regular locks, however, must be held till we finish aborting.)
2580  * Releasing LW locks is critical since we might try to grab them again
2581  * while cleaning up!
2582  */
2583  LWLockReleaseAll();
2584 
2585  /* Clear wait information and command progress indicator */
2588 
2589  /* Clean up buffer I/O and buffer context locks, too */
2590  AbortBufferIO();
2591  UnlockBuffers();
2592 
2593  /* Reset WAL record construction state */
2595 
2596  /* Cancel condition variable sleep */
2598 
2599  /*
2600  * Also clean up any open wait for lock, since the lock manager will choke
2601  * if we try to wait for another lock before doing this.
2602  */
2603  LockErrorCleanup();
2604 
2605  /*
2606  * If any timeout events are still active, make sure the timeout interrupt
2607  * is scheduled. This covers possible loss of a timeout interrupt due to
2608  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2609  * We delay this till after LockErrorCleanup so that we don't uselessly
2610  * reschedule lock or deadlock check timeouts.
2611  */
2613 
2614  /*
2615  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2616  * handler. We do this fairly early in the sequence so that the timeout
2617  * infrastructure will be functional if needed while aborting.
2618  */
2620 
2621  /*
2622  * check the current transaction state
2623  */
2624  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2625  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2626  elog(WARNING, "AbortTransaction while in %s state",
2628  Assert(s->parent == NULL);
2629 
2630  /*
2631  * set the current transaction state information appropriately during the
2632  * abort processing
2633  */
2634  s->state = TRANS_ABORT;
2635 
2636  /*
2637  * Reset user ID which might have been changed transiently. We need this
2638  * to clean up in case control escaped out of a SECURITY DEFINER function
2639  * or other local change of CurrentUserId; therefore, the prior value of
2640  * SecurityRestrictionContext also needs to be restored.
2641  *
2642  * (Note: it is not necessary to restore session authorization or role
2643  * settings here because those can only be changed via GUC, and GUC will
2644  * take care of rolling them back if need be.)
2645  */
2647 
2648  /* If in parallel mode, clean up workers and exit parallel mode. */
2649  if (IsInParallelMode())
2650  {
2651  AtEOXact_Parallel(false);
2652  s->parallelModeLevel = 0;
2653  }
2654 
2655  /*
2656  * do abort processing
2657  */
2658  AfterTriggerEndXact(false); /* 'false' means it's abort */
2659  AtAbort_Portals();
2660  AtEOXact_LargeObject(false);
2661  AtAbort_Notify();
2662  AtEOXact_RelationMap(false, is_parallel_worker);
2663  AtAbort_Twophase();
2664 
2665  /*
2666  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2667  * far as assigning an XID to advertise). But if we're inside a parallel
2668  * worker, skip this; the user backend must be the one to write the abort
2669  * record.
2670  */
2671  if (!is_parallel_worker)
2672  latestXid = RecordTransactionAbort(false);
2673  else
2674  {
2675  latestXid = InvalidTransactionId;
2676 
2677  /*
2678  * Since the parallel master won't get our value of XactLastRecEnd in
2679  * this case, we nudge WAL-writer ourselves in this case. See related
2680  * comments in RecordTransactionAbort for why this matters.
2681  */
2683  }
2684 
2685  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2686 
2687  /*
2688  * Let others know about no transaction in progress by me. Note that this
2689  * must be done _before_ releasing locks we hold and _after_
2690  * RecordTransactionAbort.
2691  */
2692  ProcArrayEndTransaction(MyProc, latestXid);
2693 
2694  /*
2695  * Post-abort cleanup. See notes in CommitTransaction() concerning
2696  * ordering. We can skip all of it if the transaction failed before
2697  * creating a resource owner.
2698  */
2699  if (TopTransactionResourceOwner != NULL)
2700  {
2701  if (is_parallel_worker)
2703  else
2705 
2708  false, true);
2709  AtEOXact_Buffers(false);
2710  AtEOXact_RelationCache(false);
2711  AtEOXact_Inval(false);
2715  false, true);
2718  false, true);
2719  smgrDoPendingDeletes(false);
2720 
2721  AtEOXact_GUC(false, 1);
2722  AtEOXact_SPI(false);
2723  AtEOXact_Enum();
2725  AtEOXact_Namespace(false, is_parallel_worker);
2726  AtEOXact_SMgr();
2727  AtEOXact_Files(false);
2729  AtEOXact_HashTables(false);
2730  AtEOXact_PgStat(false, is_parallel_worker);
2731  AtEOXact_ApplyLauncher(false);
2733  }
2734 
2735  /*
2736  * State remains TRANS_ABORT until CleanupTransaction().
2737  */
2739 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:858
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:399
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1755
static TransactionState CurrentTransactionState
Definition: xact.c:235
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:949
uint32 TransactionId
Definition: c.h:507
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:492
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3323
XLogRecPtr XactLastRecEnd
Definition: xlog.c:352
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2939
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2423
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3511
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:14651
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:396
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:408
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2783
void AtAbort_Portals(void)
Definition: portalmem.c:772
TBlockState blockState
Definition: xact.c:179
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
TransState state
Definition: xact.c:178
void AtEOXact_SMgr(void)
Definition: smgr.c:689
bool IsInParallelMode(void)
Definition: xact.c:994
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:5004
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:194
void AtAbort_Twophase(void)
Definition: twophase.c:318
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2094
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
#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:712
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3562
static void AtAbort_Memory(void)
Definition: xact.c:1723
void pgstat_progress_end_command(void)
Definition: pgstat.c:3271
void AtAbort_Notify(void)
Definition: async.c:1622
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2636
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:732
void AtEOXact_Enum(void)
Definition: pg_enum.c:610
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5366
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:4018
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:226
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:593
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1189
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4003
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1611
LocalTransactionId lxid
Definition: proc.h:106

◆ AssignTransactionId()

static void AssignTransactionId ( TransactionState  s)
static

Definition at line 554 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().

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

References MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

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

◆ AtAbort_ResourceOwner()

static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1755 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

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

◆ AtCCI_LocalCache()

static void AtCCI_LocalCache ( void  )
static

Definition at line 1446 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

1447 {
1448  /*
1449  * Make any pending relation map changes visible. We must do this before
1450  * processing local sinval messages, so that the map changes will get
1451  * reflected into the relcache when relcache invals are processed.
1452  */
1454 
1455  /*
1456  * Make catalog changes visible to me for the next command.
1457  */
1459 }
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 1813 of file xact.c.

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

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1814 {
1816 
1817  /*
1818  * Now that we're "out" of a transaction, have the system allocate things
1819  * in the top memory context instead of per-transaction contexts.
1820  */
1822 
1823  /*
1824  * Clear the special abort context for next time.
1825  */
1826  if (TransactionAbortContext != NULL)
1828 
1829  /*
1830  * Release all transaction-local memory.
1831  */
1832  if (TopTransactionContext != NULL)
1834  TopTransactionContext = NULL;
1835  CurTransactionContext = NULL;
1837 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:275
struct TransactionStateData * parent
Definition: xact.c:194
MemoryContext curTransactionContext
Definition: xact.c:182
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:732

◆ AtCommit_Memory()

static void AtCommit_Memory ( void  )
static

Definition at line 1465 of file xact.c.

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

Referenced by CommitTransaction(), and PrepareTransaction().

1466 {
1467  /*
1468  * Now that we're "out" of a transaction, have the system allocate things
1469  * in the top memory context instead of per-transaction contexts.
1470  */
1472 
1473  /*
1474  * Release all transaction-local memory.
1475  */
1476  Assert(TopTransactionContext != NULL);
1478  TopTransactionContext = NULL;
1479  CurTransactionContext = NULL;
1481 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:182
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:732

◆ AtStart_Cache()

static void AtStart_Cache ( void  )
static

Definition at line 1066 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

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

◆ AtStart_Memory()

static void AtStart_Memory ( void  )
static

Definition at line 1075 of file xact.c.

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

Referenced by StartTransaction().

1076 {
1078 
1079  /*
1080  * If this is the first time through, create a private context for
1081  * AbortTransaction to work in. By reserving some space now, we can
1082  * insulate AbortTransaction from out-of-memory scenarios. Like
1083  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1084  * size, so that space will be reserved immediately.
1085  */
1086  if (TransactionAbortContext == NULL)
1089  "TransactionAbortContext",
1090  32 * 1024,
1091  32 * 1024,
1092  32 * 1024);
1093 
1094  /*
1095  * We shouldn't have a transaction context already.
1096  */
1097  Assert(TopTransactionContext == NULL);
1098 
1099  /*
1100  * Create a toplevel context for the transaction.
1101  */
1104  "TopTransactionContext",
1106 
1107  /*
1108  * In a top-level transaction, CurTransactionContext is the same as
1109  * TopTransactionContext.
1110  */
1113 
1114  /* Make the CurTransactionContext active. */
1116 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AllocSetContextCreate
Definition: memutils.h:169
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:275
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:191
MemoryContext curTransactionContext
Definition: xact.c:182
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:732

◆ AtStart_ResourceOwner()

static void AtStart_ResourceOwner ( void  )
static

Definition at line 1122 of file xact.c.

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

Referenced by StartTransaction().

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

◆ AtSubAbort_childXids()

static void AtSubAbort_childXids ( void  )
static

Definition at line 1781 of file xact.c.

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

Referenced by AbortSubTransaction().

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

◆ AtSubAbort_Memory()

static void AtSubAbort_Memory ( void  )
static

Definition at line 1743 of file xact.c.

References Assert, and MemoryContextSwitchTo().

Referenced by AbortSubTransaction().

1744 {
1746 
1748 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:275
#define Assert(condition)
Definition: c.h:732

◆ AtSubAbort_ResourceOwner()

static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1768 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1769 {
1771 
1772  /* Make sure we have a valid ResourceOwner */
1774 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
ResourceOwner curTransactionOwner
Definition: xact.c:183

◆ AtSubCleanup_Memory()

static void AtSubCleanup_Memory ( void  )
static

Definition at line 1849 of file xact.c.

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

Referenced by CleanupSubTransaction().

1850 {
1852 
1853  Assert(s->parent != NULL);
1854 
1855  /* Make sure we're not in an about-to-be-deleted context */
1858 
1859  /*
1860  * Clear the special abort context for next time.
1861  */
1862  if (TransactionAbortContext != NULL)
1864 
1865  /*
1866  * Delete the subxact local memory contexts. Its CurTransactionContext can
1867  * go too (note this also kills CurTransactionContexts from any children
1868  * of the subxact).
1869  */
1870  if (s->curTransactionContext)
1872  s->curTransactionContext = NULL;
1873 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:275
struct TransactionStateData * parent
Definition: xact.c:194
MemoryContext curTransactionContext
Definition: xact.c:182
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:732

◆ AtSubCommit_childXids()

static void AtSubCommit_childXids ( void  )
static

Definition at line 1521 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().

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

◆ AtSubCommit_Memory()

static void AtSubCommit_Memory ( void  )
static

Definition at line 1492 of file xact.c.

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

Referenced by CommitSubTransaction().

1493 {
1495 
1496  Assert(s->parent != NULL);
1497 
1498  /* Return to parent transaction level's memory context. */
1501 
1502  /*
1503  * Ordinarily we cannot throw away the child's CurTransactionContext,
1504  * since the data it contains will be needed at upper commit. However, if
1505  * there isn't actually anything in it, we can throw it away. This avoids
1506  * a small memory leak in the common case of "trivial" subxacts.
1507  */
1509  {
1511  s->curTransactionContext = NULL;
1512  }
1513 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:194
MemoryContext curTransactionContext
Definition: xact.c:182
#define Assert(condition)
Definition: c.h:732

◆ AtSubStart_Memory()

static void AtSubStart_Memory ( void  )
static

Definition at line 1150 of file xact.c.

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

Referenced by StartSubTransaction().

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

◆ AtSubStart_ResourceOwner()

static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1174 of file xact.c.

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

Referenced by StartSubTransaction().

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 3965 of file xact.c.

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

Referenced by exec_simple_query().

3966 {
3968 
3969  /*
3970  * If we are in STARTED state (that is, no transaction block is open),
3971  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3972  * block.
3973  *
3974  * For caller convenience, we consider all other transaction states as
3975  * legal here; otherwise the caller would need its own state check, which
3976  * seems rather pointless.
3977  */
3978  if (s->blockState == TBLOCK_STARTED)
3980 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4333 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().

4334 {
4336 
4337  /*
4338  * Workers synchronize transaction state at the beginning of each parallel
4339  * operation, so we can't account for new subtransactions after that
4340  * point. We might be able to make an exception for the type of
4341  * subtransaction established by this function, which is typically used in
4342  * contexts where we're going to release or roll back the subtransaction
4343  * before proceeding further, so that no enduring change to the
4344  * transaction state occurs. For now, however, we prohibit this case along
4345  * with all the others.
4346  */
4347  if (IsInParallelMode())
4348  ereport(ERROR,
4349  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4350  errmsg("cannot start subtransactions during a parallel operation")));
4351 
4352  switch (s->blockState)
4353  {
4354  case TBLOCK_STARTED:
4355  case TBLOCK_INPROGRESS:
4357  case TBLOCK_END:
4358  case TBLOCK_PREPARE:
4359  case TBLOCK_SUBINPROGRESS:
4360  /* Normal subtransaction start */
4361  PushTransaction();
4362  s = CurrentTransactionState; /* changed by push */
4363 
4364  /*
4365  * Savepoint names, like the TransactionState block itself, live
4366  * in TopTransactionContext.
4367  */
4368  if (name)
4370  break;
4371 
4372  /* These cases are invalid. */
4373  case TBLOCK_DEFAULT:
4374  case TBLOCK_BEGIN:
4376  case TBLOCK_SUBBEGIN:
4377  case TBLOCK_SUBRELEASE:
4378  case TBLOCK_SUBCOMMIT:
4379  case TBLOCK_ABORT:
4380  case TBLOCK_SUBABORT:
4381  case TBLOCK_ABORT_END:
4382  case TBLOCK_SUBABORT_END:
4383  case TBLOCK_ABORT_PENDING:
4385  case TBLOCK_SUBRESTART:
4387  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4389  break;
4390  }
4391 
4394 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void CommitTransactionCommand(void)
Definition: xact.c:2895
int errcode(int sqlerrcode)
Definition: elog.c:570
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:994
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5034
void StartTransactionCommand(void)
Definition: xact.c:2794
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:784
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1148
#define elog(elevel,...)
Definition: elog.h:226

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3587 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().

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

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

Definition at line 5313 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().

5314 {
5315  switch (blockState)
5316  {
5317  case TBLOCK_DEFAULT:
5318  return "DEFAULT";
5319  case TBLOCK_STARTED:
5320  return "STARTED";
5321  case TBLOCK_BEGIN:
5322  return "BEGIN";
5323  case TBLOCK_INPROGRESS:
5324  return "INPROGRESS";
5326  return "IMPLICIT_INPROGRESS";
5328  return "PARALLEL_INPROGRESS";
5329  case TBLOCK_END:
5330  return "END";
5331  case TBLOCK_ABORT:
5332  return "ABORT";
5333  case TBLOCK_ABORT_END:
5334  return "ABORT_END";
5335  case TBLOCK_ABORT_PENDING:
5336  return "ABORT_PENDING";
5337  case TBLOCK_PREPARE:
5338  return "PREPARE";
5339  case TBLOCK_SUBBEGIN:
5340  return "SUBBEGIN";
5341  case TBLOCK_SUBINPROGRESS:
5342  return "SUBINPROGRESS";
5343  case TBLOCK_SUBRELEASE:
5344  return "SUBRELEASE";
5345  case TBLOCK_SUBCOMMIT:
5346  return "SUBCOMMIT";
5347  case TBLOCK_SUBABORT:
5348  return "SUBABORT";
5349  case TBLOCK_SUBABORT_END:
5350  return "SUBABORT_END";
5352  return "SUBABORT_PENDING";
5353  case TBLOCK_SUBRESTART:
5354  return "SUBRESTART";
5356  return "SUBABORT_RESTART";
5357  }
5358  return "UNRECOGNIZED";
5359 }

◆ CallSubXactCallbacks()

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

Definition at line 3566 of file xact.c.

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

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

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

◆ CallXactCallbacks()

static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3511 of file xact.c.

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

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

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

◆ CheckTransactionBlock()

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

Definition at line 3403 of file xact.c.

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3404 {
3405  /*
3406  * xact block already started?
3407  */
3408  if (IsTransactionBlock())
3409  return;
3410 
3411  /*
3412  * subtransaction?
3413  */
3414  if (IsSubTransaction())
3415  return;
3416 
3417  /*
3418  * inside a function call?
3419  */
3420  if (!isTopLevel)
3421  return;
3422 
3423  ereport(throwError ? ERROR : WARNING,
3424  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3425  /* translator: %s represents an SQL statement name */
3426  errmsg("%s can only be used in transaction blocks",
3427  stmtType)));
3428  return;
3429 }
int errcode(int sqlerrcode)
Definition: elog.c:570
bool IsTransactionBlock(void)
Definition: xact.c:4609
#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:4682
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

Definition at line 5001 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().

5002 {
5004 
5005  ShowTransactionState("CleanupSubTransaction");
5006 
5007  if (s->state != TRANS_ABORT)
5008  elog(WARNING, "CleanupSubTransaction while in %s state",
5010 
5012 
5015  if (s->curTransactionOwner)
5017  s->curTransactionOwner = NULL;
5018 
5020 
5021  s->state = TRANS_DEFAULT;
5022 
5023  PopTransaction();
5024 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void PopTransaction(void)
Definition: xact.c:5093
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:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
struct TransactionStateData * parent
Definition: xact.c:194
static void ShowTransactionState(const char *str)
Definition: xact.c:5262
SubTransactionId subTransactionId
Definition: xact.c:175
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1081
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1849
static const char * TransStateAsString(TransState state)
Definition: xact.c:5366
#define elog(elevel,...)
Definition: elog.h:226

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

Definition at line 2745 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().

2746 {
2748 
2749  /*
2750  * State should still be TRANS_ABORT from AbortTransaction().
2751  */
2752  if (s->state != TRANS_ABORT)
2753  elog(FATAL, "CleanupTransaction: unexpected state %s",
2755 
2756  /*
2757  * do abort cleanup processing
2758  */
2759  AtCleanup_Portals(); /* now safe to release portal memory */
2760  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2761 
2762  CurrentResourceOwner = NULL; /* and resource owner */
2765  s->curTransactionOwner = NULL;
2768 
2769  AtCleanup_Memory(); /* and transaction memory */
2770 
2773  s->nestingLevel = 0;
2774  s->gucNestLevel = 0;
2775  s->childXids = NULL;
2776  s->nChildXids = 0;
2777  s->maxChildXids = 0;
2778  s->parallelModeLevel = 0;
2779 
2782 
2783  /*
2784  * done with abort processing, set current transaction state back to
2785  * default
2786  */
2787  s->state = TRANS_DEFAULT;
2788 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:1813
int nParallelCurrentXids
Definition: xact.c:109
void AtCleanup_Portals(void)
Definition: portalmem.c:849
TransactionId * childXids
Definition: xact.c:184
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
#define FATAL
Definition: elog.h:52
SubTransactionId subTransactionId
Definition: xact.c:175
FullTransactionId fullTransactionId
Definition: xact.c:174
int parallelModeLevel
Definition: xact.c:192
static const char * TransStateAsString(TransState state)
Definition: xact.c:5366
#define InvalidSubTransactionId
Definition: c.h:513
#define InvalidFullTransactionId
Definition: transam.h:52
#define elog(elevel,...)
Definition: elog.h:226
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1003 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().

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

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

Definition at line 4743 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().

4744 {
4746 
4747  ShowTransactionState("CommitSubTransaction");
4748 
4749  if (s->state != TRANS_INPROGRESS)
4750  elog(WARNING, "CommitSubTransaction while in %s state",
4752 
4753  /* Pre-commit processing goes here */
4754 
4756  s->parent->subTransactionId);
4757 
4758  /* If in parallel mode, clean up workers and exit parallel mode. */
4759  if (IsInParallelMode())
4760  {
4762  s->parallelModeLevel = 0;
4763  }
4764 
4765  /* Do the actual "commit", such as it is */
4766  s->state = TRANS_COMMIT;
4767 
4768  /* Must CCI to ensure commands of subtransaction are seen as done */
4770 
4771  /*
4772  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4773  * perform that step, if required, as part of the atomic update of the
4774  * whole transaction tree at top level commit or abort.
4775  */
4776 
4777  /* Post-commit cleanup */
4780  AfterTriggerEndSubXact(true);
4785  s->parent->subTransactionId);
4787 
4789  s->parent->subTransactionId);
4790 
4793  true, false);
4795  s->parent->subTransactionId);
4796  AtEOSubXact_Inval(true);
4797  AtSubCommit_smgr();
4798 
4799  /*
4800  * The only lock we actually release here is the subtransaction XID lock.
4801  */
4805 
4806  /*
4807  * Other locks should get transferred to their parent resource owner.
4808  */
4811  true, false);
4814  true, false);
4815 
4816  AtEOXact_GUC(true, s->gucNestLevel);
4819  s->parent->subTransactionId);
4821  s->parent->subTransactionId);
4823  s->parent->subTransactionId);
4825  AtEOSubXact_PgStat(true, s->nestingLevel);
4828 
4829  /*
4830  * We need to restore the upper transaction's read-only state, in case the
4831  * upper is read-write while the child is read-only; GUC will incorrectly
4832  * think it should leave the child state in place.
4833  */
4835 
4839  s->curTransactionOwner = NULL;
4840 
4842 
4843  s->state = TRANS_DEFAULT;
4844 
4845  PopTransaction();
4846 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3077
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void AtSubCommit_Memory(void)
Definition: xact.c:1492
static void PopTransaction(void)
Definition: xact.c:5093
#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:544
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:1521
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
bool IsInParallelMode(void)
Definition: xact.c:994
struct TransactionStateData * parent
Definition: xact.c:194
void AtSubCommit_Notify(void)
Definition: async.c:1672
static void ShowTransactionState(const char *str)
Definition: xact.c:5262
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:900
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5729
SubTransactionId subTransactionId
Definition: xact.c:175
FullTransactionId fullTransactionId
Definition: xact.c:174
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2181
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:634
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:14683
void CommandCounterIncrement(void)
Definition: xact.c:1003
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2750
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3566
bool XactReadOnly
Definition: xact.c:78
int parallelModeLevel
Definition: xact.c:192
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5366
bool prevXactReadOnly
Definition: xact.c:189
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:5100
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define elog(elevel,...)
Definition: elog.h:226
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:1170
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:934

◆ CommitTransaction()

static void CommitTransaction ( void  )
static

Definition at line 2043 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().

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

2896 {
2898 
2899  if (s->chain)
2901 
2902  switch (s->blockState)
2903  {
2904  /*
2905  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2906  * StartTransactionCommand didn't set the STARTED state
2907  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2908  * by EndParallelWorkerTransaction(), not this function.
2909  */
2910  case TBLOCK_DEFAULT:
2912  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2914  break;
2915 
2916  /*
2917  * If we aren't in a transaction block, just do our usual
2918  * transaction commit, and return to the idle state.
2919  */
2920  case TBLOCK_STARTED:
2923  break;
2924 
2925  /*
2926  * We are completing a "BEGIN TRANSACTION" command, so we change
2927  * to the "transaction block in progress" state and return. (We
2928  * assume the BEGIN did nothing to the database, so we need no
2929  * CommandCounterIncrement.)
2930  */
2931  case TBLOCK_BEGIN:
2933  break;
2934 
2935  /*
2936  * This is the case when we have finished executing a command
2937  * someplace within a transaction block. We increment the command
2938  * counter and return.
2939  */
2940  case TBLOCK_INPROGRESS:
2942  case TBLOCK_SUBINPROGRESS:
2944  break;
2945 
2946  /*
2947  * We are completing a "COMMIT" command. Do it and return to the
2948  * idle state.
2949  */
2950  case TBLOCK_END:
2953  if (s->chain)
2954  {
2955  StartTransaction();
2957  s->chain = false;
2959  }
2960  break;
2961 
2962  /*
2963  * Here we are in the middle of a transaction block but one of the
2964  * commands caused an abort so we do nothing but remain in the
2965  * abort state. Eventually we will get a ROLLBACK command.
2966  */
2967  case TBLOCK_ABORT:
2968  case TBLOCK_SUBABORT:
2969  break;
2970 
2971  /*
2972  * Here we were in an aborted transaction block and we just got
2973  * the ROLLBACK command from the user, so clean up the
2974  * already-aborted transaction and return to the idle state.
2975  */
2976  case TBLOCK_ABORT_END:
2979  if (s->chain)
2980  {
2981  StartTransaction();
2983  s->chain = false;
2985  }
2986  break;
2987 
2988  /*
2989  * Here we were in a perfectly good transaction block but the user
2990  * told us to ROLLBACK anyway. We have to abort the transaction
2991  * and then clean up.
2992  */
2993  case TBLOCK_ABORT_PENDING:
2994  AbortTransaction();
2997  if (s->chain)
2998  {
2999  StartTransaction();
3001  s->chain = false;
3003  }
3004  break;
3005 
3006  /*
3007  * We are completing a "PREPARE TRANSACTION" command. Do it and
3008  * return to the idle state.
3009  */
3010  case TBLOCK_PREPARE:
3013  break;
3014 
3015  /*
3016  * We were just issued a SAVEPOINT inside a transaction block.
3017  * Start a subtransaction. (DefineSavepoint already did
3018  * PushTransaction, so as to have someplace to put the SUBBEGIN
3019  * state.)
3020  */
3021  case TBLOCK_SUBBEGIN:
3024  break;
3025 
3026  /*
3027  * We were issued a RELEASE command, so we end the current
3028  * subtransaction and return to the parent transaction. The parent
3029  * might be ended too, so repeat till we find an INPROGRESS
3030  * transaction or subtransaction.
3031  */
3032  case TBLOCK_SUBRELEASE:
3033  do
3034  {
3036  s = CurrentTransactionState; /* changed by pop */
3037  } while (s->blockState == TBLOCK_SUBRELEASE);
3038 
3041  break;
3042 
3043  /*
3044  * We were issued a COMMIT, so we end the current subtransaction
3045  * hierarchy and perform final commit. We do this by rolling up
3046  * any subtransactions into their parent, which leads to O(N^2)
3047  * operations with respect to resource owners - this isn't that
3048  * bad until we approach a thousands of savepoints but is
3049  * necessary for correctness should after triggers create new
3050  * resource owners.
3051  */
3052  case TBLOCK_SUBCOMMIT:
3053  do
3054  {
3056  s = CurrentTransactionState; /* changed by pop */
3057  } while (s->blockState == TBLOCK_SUBCOMMIT);
3058  /* If we had a COMMIT command, finish off the main xact too */
3059  if (s->blockState == TBLOCK_END)
3060  {
3061  Assert(s->parent == NULL);
3064  }
3065  else if (s->blockState == TBLOCK_PREPARE)
3066  {
3067  Assert(s->parent == NULL);
3070  }
3071  else
3072  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3074  break;
3075 
3076  /*
3077  * The current already-failed subtransaction is ending due to a
3078  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3079  * examine the parent (which could be in any of several states).
3080  */
3081  case TBLOCK_SUBABORT_END:
3084  break;
3085 
3086  /*
3087  * As above, but it's not dead yet, so abort first.
3088  */
3093  break;
3094 
3095  /*
3096  * The current subtransaction is the target of a ROLLBACK TO
3097  * command. Abort and pop it, then start a new subtransaction
3098  * with the same name.
3099  */
3100  case TBLOCK_SUBRESTART:
3101  {
3102  char *name;
3103  int savepointLevel;
3104 
3105  /* save name and keep Cleanup from freeing it */
3106  name = s->name;
3107  s->name = NULL;
3108  savepointLevel = s->savepointLevel;
3109 
3112 
3113  DefineSavepoint(NULL);
3114  s = CurrentTransactionState; /* changed by push */
3115  s->name = name;
3116  s->savepointLevel = savepointLevel;
3117 
3118  /* This is the same as TBLOCK_SUBBEGIN case */
3122  }
3123  break;
3124 
3125  /*
3126  * Same as above, but the subtransaction had already failed, so we
3127  * don't need AbortSubTransaction.
3128  */
3130  {
3131  char *name;
3132  int savepointLevel;
3133 
3134  /* save name and keep Cleanup from freeing it */
3135  name = s->name;
3136  s->name = NULL;
3137  savepointLevel = s->savepointLevel;
3138 
3140 
3141  DefineSavepoint(NULL);
3142  s = CurrentTransactionState; /* changed by push */
3143  s->name = name;
3144  s->savepointLevel = savepointLevel;
3145 
3146  /* This is the same as TBLOCK_SUBBEGIN case */
3150  }
3151  break;
3152  }
3153 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AssertState(condition)
Definition: c.h:735
void CommitTransactionCommand(void)
Definition: xact.c:2895
void DefineSavepoint(const char *name)
Definition: xact.c:4012
TBlockState blockState
Definition: xact.c:179
static void PrepareTransaction(void)
Definition: xact.c:2283
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4743
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2745
struct TransactionStateData * parent
Definition: xact.c:194
void SaveTransactionCharacteristics(void)
Definition: xact.c:2875
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
static void AbortSubTransaction(void)
Definition: xact.c:4852
void CommandCounterIncrement(void)
Definition: xact.c:1003
static void AbortTransaction(void)
Definition: xact.c:2564
static void StartTransaction(void)
Definition: xact.c:1884
#define Assert(condition)
Definition: c.h:732
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2883
static void CleanupSubTransaction(void)
Definition: xact.c:5001
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:226
static void CommitTransaction(void)
Definition: xact.c:2043
static void StartSubTransaction(void)
Definition: xact.c:4705

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

Definition at line 4012 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().

4013 {
4015 
4016  /*
4017  * Workers synchronize transaction state at the beginning of each parallel
4018  * operation, so we can't account for new subtransactions after that
4019  * point. (Note that this check will certainly error out if s->blockState
4020  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4021  * below.)
4022  */
4023  if (IsInParallelMode())
4024  ereport(ERROR,
4025  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4026  errmsg("cannot define savepoints during a parallel operation")));
4027 
4028  switch (s->blockState)
4029  {
4030  case TBLOCK_INPROGRESS:
4031  case TBLOCK_SUBINPROGRESS:
4032  /* Normal subtransaction start */
4033  PushTransaction();
4034  s = CurrentTransactionState; /* changed by push */
4035 
4036  /*
4037  * Savepoint names, like the TransactionState block itself, live
4038  * in TopTransactionContext.
4039  */
4040  if (name)
4042  break;
4043 
4044  /*
4045  * We disallow savepoint commands in implicit transaction blocks.
4046  * There would be no great difficulty in allowing them so far as
4047  * this module is concerned, but a savepoint seems inconsistent
4048  * with exec_simple_query's behavior of abandoning the whole query
4049  * string upon error. Also, the point of an implicit transaction
4050  * block (as opposed to a regular one) is to automatically close
4051  * after an error, so it's hard to see how a savepoint would fit
4052  * into that.
4053  *
4054  * The error messages for this are phrased as if there were no
4055  * active transaction block at all, which is historical but
4056  * perhaps could be improved.
4057  */
4059  ereport(ERROR,
4060  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4061  /* translator: %s represents an SQL statement name */
4062  errmsg("%s can only be used in transaction blocks",
4063  "SAVEPOINT")));
4064  break;
4065 
4066  /* These cases are invalid. */
4067  case TBLOCK_DEFAULT:
4068  case TBLOCK_STARTED:
4069  case TBLOCK_BEGIN:
4071  case TBLOCK_SUBBEGIN:
4072  case TBLOCK_END:
4073  case TBLOCK_SUBRELEASE:
4074  case TBLOCK_SUBCOMMIT:
4075  case TBLOCK_ABORT:
4076  case TBLOCK_SUBABORT:
4077  case TBLOCK_ABORT_END:
4078  case TBLOCK_SUBABORT_END:
4079  case TBLOCK_ABORT_PENDING:
4081  case TBLOCK_SUBRESTART:
4083  case TBLOCK_PREPARE:
4084  elog(FATAL, "DefineSavepoint: unexpected state %s",
4086  break;
4087  }
4088 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:570
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:994
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5034
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:784
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1148
#define elog(elevel,...)
Definition: elog.h:226

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 3990 of file xact.c.

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

Referenced by exec_simple_query().

3991 {
3993 
3994  /*
3995  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3996  * allowing CommitTransactionCommand to commit whatever happened during
3997  * the implicit transaction block as though it were a single statement.
3998  *
3999  * For caller convenience, we consider all other transaction states as
4000  * legal here; otherwise the caller would need its own state check, which
4001  * seems rather pointless.
4002  */
4005 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5250 of file xact.c.

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

Referenced by ParallelWorkerMain().

5251 {
5255 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
#define Assert(condition)
Definition: c.h:732
static void CommitTransaction(void)
Definition: xact.c:2043

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3707 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), TransactionStateData::chain, CurrentTransactionState, elog, ereport, errcode(), errmsg(), 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().

3708 {
3710  bool result = false;
3711 
3712  switch (s->blockState)
3713  {
3714  /*
3715  * We are in a transaction block, so tell CommitTransactionCommand
3716  * to COMMIT.
3717  */
3718  case TBLOCK_INPROGRESS:
3719  s->blockState = TBLOCK_END;
3720  result = true;
3721  break;
3722 
3723  /*
3724  * In an implicit transaction block, commit, but issue a warning
3725  * because there was no explicit BEGIN before this.
3726  */
3728  ereport(WARNING,
3729  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3730  errmsg("there is no transaction in progress")));
3731  s->blockState = TBLOCK_END;
3732  result = true;
3733  break;
3734 
3735  /*
3736  * We are in a failed transaction block. Tell
3737  * CommitTransactionCommand it's time to exit the block.
3738  */
3739  case TBLOCK_ABORT:
3741  break;
3742 
3743  /*
3744  * We are in a live subtransaction block. Set up to subcommit all
3745  * open subtransactions and then commit the main transaction.
3746  */
3747  case TBLOCK_SUBINPROGRESS:
3748  while (s->parent != NULL)
3749  {
3750  if (s->blockState == TBLOCK_SUBINPROGRESS)
3752  else
3753  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3755  s = s->parent;
3756  }
3757  if (s->blockState == TBLOCK_INPROGRESS)
3758  s->blockState = TBLOCK_END;
3759  else
3760  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3762  result = true;
3763  break;
3764 
3765  /*
3766  * Here we are inside an aborted subtransaction. Treat the COMMIT
3767  * as ROLLBACK: set up to abort everything and exit the main
3768  * transaction.
3769  */
3770  case TBLOCK_SUBABORT:
3771  while (s->parent != NULL)
3772  {
3773  if (s->blockState == TBLOCK_SUBINPROGRESS)
3775  else if (s->blockState == TBLOCK_SUBABORT)
3777  else
3778  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3780  s = s->parent;
3781  }
3782  if (s->blockState == TBLOCK_INPROGRESS)
3784  else if (s->blockState == TBLOCK_ABORT)
3786  else
3787  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3789  break;
3790 
3791  /*
3792  * The user issued COMMIT when not inside a transaction. Issue a
3793  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3794  * CommitTransactionCommand() will then close the transaction and
3795  * put us back into the default state.
3796  */
3797  case TBLOCK_STARTED:
3798  ereport(WARNING,
3799  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3800  errmsg("there is no transaction in progress")));
3801  result = true;
3802  break;
3803 
3804  /*
3805  * The user issued a COMMIT that somehow ran inside a parallel
3806  * worker. We can't cope with that.
3807  */
3809  ereport(FATAL,
3810  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3811  errmsg("cannot commit during a parallel operation")));
3812  break;
3813 
3814  /* These cases are invalid. */
3815  case TBLOCK_DEFAULT:
3816  case TBLOCK_BEGIN:
3817  case TBLOCK_SUBBEGIN:
3818  case TBLOCK_END:
3819  case TBLOCK_SUBRELEASE:
3820  case TBLOCK_SUBCOMMIT:
3821  case TBLOCK_ABORT_END:
3822  case TBLOCK_SUBABORT_END:
3823  case TBLOCK_ABORT_PENDING:
3825  case TBLOCK_SUBRESTART:
3827  case TBLOCK_PREPARE:
3828  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3830  break;
3831  }
3832 
3834  s->blockState == TBLOCK_END ||
3835  s->blockState == TBLOCK_ABORT_END ||
3837 
3838  s->chain = chain;
3839 
3840  return result;
3841 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:570
TBlockState blockState
Definition: xact.c:179
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:732
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 961 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

962 {
964 
965  Assert(s->parallelModeLevel >= 0);
966 
967  ++s->parallelModeLevel;
968 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:732

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5127 of file xact.c.

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

Referenced by InitializeParallelDSM().

5128 {
5129  TransactionState s;
5130  Size nxids = 0;
5132 
5133  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5134  {
5136  nxids = add_size(nxids, 1);
5137  nxids = add_size(nxids, s->nChildXids);
5138  }
5139 
5140  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5141 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
uint32 TransactionId
Definition: c.h:507
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
struct TransactionStateData * parent
Definition: xact.c:194
FullTransactionId fullTransactionId
Definition: xact.c:174
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:215
size_t Size
Definition: c.h:466

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 974 of file xact.c.

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

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

975 {
977 
978  Assert(s->parallelModeLevel > 0);
980 
981  --s->parallelModeLevel;
982 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
bool ParallelContextActive(void)
Definition: parallel.c:945
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:732

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1051 of file xact.c.

References forceSyncCommit.

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

1052 {
1053  forceSyncCommit = true;
1054 }
static bool forceSyncCommit
Definition: xact.c:265

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 746 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().

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 481 of file xact.c.

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

482 {
484 
487  return s->fullTransactionId;
488 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:174
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 499 of file xact.c.

References TransactionStateData::fullTransactionId.

500 {
502 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 792 of file xact.c.

References stmtStartTimestamp.

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

793 {
794  return stmtStartTimestamp;
795 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:253

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 440 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

441 {
443 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 804 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 526 of file xact.c.

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

Referenced by xid_age().

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

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 452 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

453 {
457 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
static TransactionStateData TopTransactionStateData
Definition: xact.c:223
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 468 of file xact.c.

References XactTopFullTransactionId.

469 {
471 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 395 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().

396 {
400 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:223
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3442 of file xact.c.

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

Referenced by vacuum().

3443 {
3444  /*
3445  * Return true on same conditions that would make
3446  * PreventInTransactionBlock error out
3447  */
3448  if (IsTransactionBlock())
3449  return true;
3450 
3451  if (IsSubTransaction())
3452  return true;
3453 
3454  if (!isTopLevel)
3455  return true;
3456 
3459  return true;
3460 
3461  return false;
3462 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
bool IsTransactionBlock(void)
Definition: xact.c:4609
bool IsSubTransaction(void)
Definition: xact.c:4682

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4609 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().

4610 {
4612 
4614  return false;
4615 
4616  return true;
4617 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 356 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().

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

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 510 of file xact.c.

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

Referenced by XLogInsertRecord().

511 {
514 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ PopTransaction()