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/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_enum.h"
#include "catalog/storage.h"
#include "commands/async.h"
#include "commands/tablecmds.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "libpq/be-fsstubs.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/logicallauncher.h"
#include "replication/origin.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/lmgr.h"
#include "storage/md.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
Include dependency graph for xact.c:

Go to the source code of this file.

Data Structures

struct  TransactionStateData
 
struct  SerializedTransactionState
 
struct  XactCallbackItem
 
struct  SubXactCallbackItem
 

Macros

#define SerializedTransactionStateHeaderSize   offsetof(SerializedTransactionState, parallelCurrentXids)
 

Typedefs

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

Enumerations

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

Functions

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

Variables

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

Macro Definition Documentation

◆ SerializedTransactionStateHeaderSize

#define SerializedTransactionStateHeaderSize   offsetof(SerializedTransactionState, parallelCurrentXids)

Definition at line 215 of file xact.c.

Referenced by EstimateTransactionStateSpace(), and SerializeTransactionState().

Typedef Documentation

◆ SerializedTransactionState

◆ SubXactCallbackItem

◆ TBlockState

typedef enum TBlockState TBlockState

◆ TransactionState

Definition at line 197 of file xact.c.

◆ TransactionStateData

◆ TransState

typedef enum TransState TransState

◆ XactCallbackItem

Enumeration Type Documentation

◆ TBlockState

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

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

3182 {
3184 
3185  switch (s->blockState)
3186  {
3187  case TBLOCK_DEFAULT:
3188  if (s->state == TRANS_DEFAULT)
3189  {
3190  /* we are idle, so nothing to do */
3191  }
3192  else
3193  {
3194  /*
3195  * We can get here after an error during transaction start
3196  * (state will be TRANS_START). Need to clean up the
3197  * incompletely started transaction. First, adjust the
3198  * low-level state to suppress warning message from
3199  * AbortTransaction.
3200  */
3201  if (s->state == TRANS_START)
3202  s->state = TRANS_INPROGRESS;
3203  AbortTransaction();
3205  }
3206  break;
3207 
3208  /*
3209  * If we aren't in a transaction block, we just do the basic abort
3210  * & cleanup transaction. For this purpose, we treat an implicit
3211  * transaction block as if it were a simple statement.
3212  */
3213  case TBLOCK_STARTED:
3215  AbortTransaction();
3218  break;
3219 
3220  /*
3221  * If we are in TBLOCK_BEGIN it means something screwed up right
3222  * after reading "BEGIN TRANSACTION". We assume that the user
3223  * will interpret the error as meaning the BEGIN failed to get him
3224  * into a transaction block, so we should abort and return to idle
3225  * state.
3226  */
3227  case TBLOCK_BEGIN:
3228  AbortTransaction();
3231  break;
3232 
3233  /*
3234  * We are somewhere in a transaction block and we've gotten a
3235  * failure, so we abort the transaction and set up the persistent
3236  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3237  */
3238  case TBLOCK_INPROGRESS:
3240  AbortTransaction();
3241  s->blockState = TBLOCK_ABORT;
3242  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3243  break;
3244 
3245  /*
3246  * Here, we failed while trying to COMMIT. Clean up the
3247  * transaction and return to idle state (we do not want to stay in
3248  * the transaction).
3249  */
3250  case TBLOCK_END:
3251  AbortTransaction();
3254  break;
3255 
3256  /*
3257  * Here, we are already in an aborted transaction state and are
3258  * waiting for a ROLLBACK, but for some reason we failed again! So
3259  * we just remain in the abort state.
3260  */
3261  case TBLOCK_ABORT:
3262  case TBLOCK_SUBABORT:
3263  break;
3264 
3265  /*
3266  * We are in a failed transaction and we got the ROLLBACK command.
3267  * We have already aborted, we just need to cleanup and go to idle
3268  * state.
3269  */
3270  case TBLOCK_ABORT_END:
3273  break;
3274 
3275  /*
3276  * We are in a live transaction and we got a ROLLBACK command.
3277  * Abort, cleanup, go to idle state.
3278  */
3279  case TBLOCK_ABORT_PENDING:
3280  AbortTransaction();
3283  break;
3284 
3285  /*
3286  * Here, we failed while trying to PREPARE. Clean up the
3287  * transaction and return to idle state (we do not want to stay in
3288  * the transaction).
3289  */
3290  case TBLOCK_PREPARE:
3291  AbortTransaction();
3294  break;
3295 
3296  /*
3297  * We got an error inside a subtransaction. Abort just the
3298  * subtransaction, and go to the persistent SUBABORT state until
3299  * we get ROLLBACK.
3300  */
3301  case TBLOCK_SUBINPROGRESS:
3304  break;
3305 
3306  /*
3307  * If we failed while trying to create a subtransaction, clean up
3308  * the broken subtransaction and abort the parent. The same
3309  * applies if we get a failure while ending a subtransaction.
3310  */
3311  case TBLOCK_SUBBEGIN:
3312  case TBLOCK_SUBRELEASE:
3313  case TBLOCK_SUBCOMMIT:
3315  case TBLOCK_SUBRESTART:
3319  break;
3320 
3321  /*
3322  * Same as above, except the Abort() was already done.
3323  */
3324  case TBLOCK_SUBABORT_END:
3328  break;
3329  }
3330 }
void AbortCurrentTransaction(void)
Definition: xact.c:3181
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:2767
static void AbortSubTransaction(void)
Definition: xact.c:4896
static void AbortTransaction(void)
Definition: xact.c:2582
static void CleanupSubTransaction(void)
Definition: xact.c:5048

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4551 {
4553 
4554  /* Ensure we're not running in a doomed memory context */
4555  AtAbort_Memory();
4556 
4557  /*
4558  * Get out of any transaction or nested transaction
4559  */
4560  do
4561  {
4562  switch (s->blockState)
4563  {
4564  case TBLOCK_DEFAULT:
4565  if (s->state == TRANS_DEFAULT)
4566  {
4567  /* Not in a transaction, do nothing */
4568  }
4569  else
4570  {
4571  /*
4572  * We can get here after an error during transaction start
4573  * (state will be TRANS_START). Need to clean up the
4574  * incompletely started transaction. First, adjust the
4575  * low-level state to suppress warning message from
4576  * AbortTransaction.
4577  */
4578  if (s->state == TRANS_START)
4579  s->state = TRANS_INPROGRESS;
4580  AbortTransaction();
4582  }
4583  break;
4584  case TBLOCK_STARTED:
4585  case TBLOCK_BEGIN:
4586  case TBLOCK_INPROGRESS:
4589  case TBLOCK_END:
4590  case TBLOCK_ABORT_PENDING:
4591  case TBLOCK_PREPARE:
4592  /* In a transaction, so clean up */
4593  AbortTransaction();
4596  break;
4597  case TBLOCK_ABORT:
4598  case TBLOCK_ABORT_END:
4599 
4600  /*
4601  * AbortTransaction is already done, still need Cleanup.
4602  * However, if we failed partway through running ROLLBACK,
4603  * there will be an active portal running that command, which
4604  * we need to shut down before doing CleanupTransaction.
4605  */
4606  AtAbort_Portals();
4609  break;
4610 
4611  /*
4612  * In a subtransaction, so clean it up and abort parent too
4613  */
4614  case TBLOCK_SUBBEGIN:
4615  case TBLOCK_SUBINPROGRESS:
4616  case TBLOCK_SUBRELEASE:
4617  case TBLOCK_SUBCOMMIT:
4619  case TBLOCK_SUBRESTART:
4622  s = CurrentTransactionState; /* changed by pop */
4623  break;
4624 
4625  case TBLOCK_SUBABORT:
4626  case TBLOCK_SUBABORT_END:
4628  /* As above, but AbortSubTransaction already done */
4629  if (s->curTransactionOwner)
4630  {
4631  /* As in TBLOCK_ABORT, might have a live portal to zap */
4636  }
4638  s = CurrentTransactionState; /* changed by pop */
4639  break;
4640  }
4641  } while (s->blockState != TBLOCK_DEFAULT);
4642 
4643  /* Should be out of all subxacts now */
4644  Assert(s->parent == NULL);
4645 
4646  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4647  AtCleanup_Memory();
4648 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void AtCleanup_Memory(void)
Definition: xact.c:1816
void AtAbort_Portals(void)
Definition: portalmem.c:774
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:2767
struct TransactionStateData * parent
Definition: xact.c:194
SubTransactionId subTransactionId
Definition: xact.c:175
static void AbortSubTransaction(void)
Definition: xact.c:4896
static void AtAbort_Memory(void)
Definition: xact.c:1726
static void AbortTransaction(void)
Definition: xact.c:2582
#define Assert(condition)
Definition: c.h:738
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
static void CleanupSubTransaction(void)
Definition: xact.c:5048

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

Definition at line 4896 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(), ResetReindexState(), 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().

4897 {
4899 
4900  /* Prevent cancel/die interrupt while cleaning up */
4901  HOLD_INTERRUPTS();
4902 
4903  /* Make sure we have a valid memory context and resource owner */
4906 
4907  /*
4908  * Release any LW locks we might be holding as quickly as possible.
4909  * (Regular locks, however, must be held till we finish aborting.)
4910  * Releasing LW locks is critical since we might try to grab them again
4911  * while cleaning up!
4912  *
4913  * FIXME This may be incorrect --- Are there some locks we should keep?
4914  * Buffer locks, for example? I don't think so but I'm not sure.
4915  */
4916  LWLockReleaseAll();
4917 
4920  AbortBufferIO();
4921  UnlockBuffers();
4922 
4923  /* Reset WAL record construction state */
4925 
4926  /* Cancel condition variable sleep */
4928 
4929  /*
4930  * Also clean up any open wait for lock, since the lock manager will choke
4931  * if we try to wait for another lock before doing this.
4932  */
4933  LockErrorCleanup();
4934 
4935  /*
4936  * If any timeout events are still active, make sure the timeout interrupt
4937  * is scheduled. This covers possible loss of a timeout interrupt due to
4938  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4939  * We delay this till after LockErrorCleanup so that we don't uselessly
4940  * reschedule lock or deadlock check timeouts.
4941  */
4943 
4944  /*
4945  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4946  * handler. We do this fairly early in the sequence so that the timeout
4947  * infrastructure will be functional if needed while aborting.
4948  */
4950 
4951  /*
4952  * check the current transaction state
4953  */
4954  ShowTransactionState("AbortSubTransaction");
4955 
4956  if (s->state != TRANS_INPROGRESS)
4957  elog(WARNING, "AbortSubTransaction while in %s state",
4959 
4960  s->state = TRANS_ABORT;
4961 
4962  /*
4963  * Reset user ID which might have been changed transiently. (See notes in
4964  * AbortTransaction.)
4965  */
4967 
4968  /* Forget about any active REINDEX. */
4970 
4971  /* Exit from parallel mode, if necessary. */
4972  if (IsInParallelMode())
4973  {
4975  s->parallelModeLevel = 0;
4976  }
4977 
4978  /*
4979  * We can skip all this stuff if the subxact failed before creating a
4980  * ResourceOwner...
4981  */
4982  if (s->curTransactionOwner)
4983  {
4984  AfterTriggerEndSubXact(false);
4990  s->parent->subTransactionId);
4992 
4993  /* Advertise the fact that we aborted in pg_xact. */
4994  (void) RecordTransactionAbort(true);
4995 
4996  /* Post-abort cleanup */
4999 
5001  s->parent->subTransactionId);
5002 
5005  false, false);
5007  s->parent->subTransactionId);
5008  AtEOSubXact_Inval(false);
5011  false, false);
5014  false, false);
5015  AtSubAbort_smgr();
5016 
5017  AtEOXact_GUC(false, s->gucNestLevel);
5018  AtEOSubXact_SPI(false, s->subTransactionId);
5020  s->parent->subTransactionId);
5022  s->parent->subTransactionId);
5024  s->parent->subTransactionId);
5026  AtEOSubXact_PgStat(false, s->nestingLevel);
5029  }
5030 
5031  /*
5032  * Restore the upper transaction's read-only state, too. This should be
5033  * redundant with GUC's cleanup but we may as well do it for consistency
5034  * with the commit case.
5035  */
5037 
5039 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1771
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3206
void ResetReindexState(int nestLevel)
Definition: index.c:3909
static TransactionState CurrentTransactionState
Definition: xact.c:235
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:560
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1864
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:385
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1055
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
bool IsInParallelMode(void)
Definition: xact.c:997
void ConditionVariableCancelSleep(void)
static void AtSubAbort_Memory(void)
Definition: xact.c:1746
void XLogResetInsertion(void)
Definition: xloginsert.c:194
struct TransactionStateData * parent
Definition: xact.c:194
static void ShowTransactionState(const char *str)
Definition: xact.c:5309
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:874
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5948
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:1784
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1380
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2234
void LockErrorCleanup(void)
Definition: proc.c:711
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:632
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3694
void AtSubAbort_Notify(void)
Definition: async.c:1833
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:15430
void pgstat_progress_end_command(void)
Definition: pgstat.c:3286
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2863
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3587
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:5413
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
void AtSubAbort_smgr(void)
Definition: storage.c:898
bool prevXactReadOnly
Definition: xact.c:189
void AbortBufferIO(void)
Definition: bufmgr.c:4172
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4748
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4132
void LWLockReleaseAll(void)
Definition: lwlock.c:1911
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1203
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1614

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

Definition at line 2582 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::nestingLevel, 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(), ResetReindexState(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerRelease(), RESUME_INTERRUPTS, SetUserIdAndSecContext(), smgrDoPendingDeletes(), smgrDoPendingSyncs(), 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().

2583 {
2585  TransactionId latestXid;
2586  bool is_parallel_worker;
2587 
2588  /* Prevent cancel/die interrupt while cleaning up */
2589  HOLD_INTERRUPTS();
2590 
2591  /* Make sure we have a valid memory context and resource owner */
2592  AtAbort_Memory();
2594 
2595  /*
2596  * Release any LW locks we might be holding as quickly as possible.
2597  * (Regular locks, however, must be held till we finish aborting.)
2598  * Releasing LW locks is critical since we might try to grab them again
2599  * while cleaning up!
2600  */
2601  LWLockReleaseAll();
2602 
2603  /* Clear wait information and command progress indicator */
2606 
2607  /* Clean up buffer I/O and buffer context locks, too */
2608  AbortBufferIO();
2609  UnlockBuffers();
2610 
2611  /* Reset WAL record construction state */
2613 
2614  /* Cancel condition variable sleep */
2616 
2617  /*
2618  * Also clean up any open wait for lock, since the lock manager will choke
2619  * if we try to wait for another lock before doing this.
2620  */
2621  LockErrorCleanup();
2622 
2623  /*
2624  * If any timeout events are still active, make sure the timeout interrupt
2625  * is scheduled. This covers possible loss of a timeout interrupt due to
2626  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2627  * We delay this till after LockErrorCleanup so that we don't uselessly
2628  * reschedule lock or deadlock check timeouts.
2629  */
2631 
2632  /*
2633  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2634  * handler. We do this fairly early in the sequence so that the timeout
2635  * infrastructure will be functional if needed while aborting.
2636  */
2638 
2639  /*
2640  * check the current transaction state
2641  */
2642  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2643  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2644  elog(WARNING, "AbortTransaction while in %s state",
2646  Assert(s->parent == NULL);
2647 
2648  /*
2649  * set the current transaction state information appropriately during the
2650  * abort processing
2651  */
2652  s->state = TRANS_ABORT;
2653 
2654  /*
2655  * Reset user ID which might have been changed transiently. We need this
2656  * to clean up in case control escaped out of a SECURITY DEFINER function
2657  * or other local change of CurrentUserId; therefore, the prior value of
2658  * SecurityRestrictionContext also needs to be restored.
2659  *
2660  * (Note: it is not necessary to restore session authorization or role
2661  * settings here because those can only be changed via GUC, and GUC will
2662  * take care of rolling them back if need be.)
2663  */
2665 
2666  /* Forget about any active REINDEX. */
2668 
2669  /* If in parallel mode, clean up workers and exit parallel mode. */
2670  if (IsInParallelMode())
2671  {
2672  AtEOXact_Parallel(false);
2673  s->parallelModeLevel = 0;
2674  }
2675 
2676  /*
2677  * do abort processing
2678  */
2679  AfterTriggerEndXact(false); /* 'false' means it's abort */
2680  AtAbort_Portals();
2681  smgrDoPendingSyncs(false, is_parallel_worker);
2682  AtEOXact_LargeObject(false);
2683  AtAbort_Notify();
2684  AtEOXact_RelationMap(false, is_parallel_worker);
2685  AtAbort_Twophase();
2686 
2687  /*
2688  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2689  * far as assigning an XID to advertise). But if we're inside a parallel
2690  * worker, skip this; the user backend must be the one to write the abort
2691  * record.
2692  */
2693  if (!is_parallel_worker)
2694  latestXid = RecordTransactionAbort(false);
2695  else
2696  {
2697  latestXid = InvalidTransactionId;
2698 
2699  /*
2700  * Since the parallel master won't get our value of XactLastRecEnd in
2701  * this case, we nudge WAL-writer ourselves in this case. See related
2702  * comments in RecordTransactionAbort for why this matters.
2703  */
2705  }
2706 
2707  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2708 
2709  /*
2710  * Let others know about no transaction in progress by me. Note that this
2711  * must be done _before_ releasing locks we hold and _after_
2712  * RecordTransactionAbort.
2713  */
2714  ProcArrayEndTransaction(MyProc, latestXid);
2715 
2716  /*
2717  * Post-abort cleanup. See notes in CommitTransaction() concerning
2718  * ordering. We can skip all of it if the transaction failed before
2719  * creating a resource owner.
2720  */
2721  if (TopTransactionResourceOwner != NULL)
2722  {
2723  if (is_parallel_worker)
2725  else
2727 
2730  false, true);
2731  AtEOXact_Buffers(false);
2732  AtEOXact_RelationCache(false);
2733  AtEOXact_Inval(false);
2737  false, true);
2740  false, true);
2741  smgrDoPendingDeletes(false);
2742 
2743  AtEOXact_GUC(false, 1);
2744  AtEOXact_SPI(false);
2745  AtEOXact_Enum();
2747  AtEOXact_Namespace(false, is_parallel_worker);
2748  AtEOXact_SMgr();
2749  AtEOXact_Files(false);
2751  AtEOXact_HashTables(false);
2752  AtEOXact_PgStat(false, is_parallel_worker);
2753  AtEOXact_ApplyLauncher(false);
2755  }
2756 
2757  /*
2758  * State remains TRANS_ABORT until CleanupTransaction().
2759  */
2761 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:832
void ResetReindexState(int nestLevel)
Definition: index.c:3909
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:595
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1758
static TransactionState CurrentTransactionState
Definition: xact.c:235
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:949
uint32 TransactionId
Definition: c.h:513
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:560
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3338
XLogRecPtr XactLastRecEnd
Definition: xlog.c:363
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3061
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2443
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3532
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15398
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:397
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2896
void AtAbort_Portals(void)
Definition: portalmem.c:774
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:638
bool IsInParallelMode(void)
Definition: xact.c:997
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4652
void XLogResetInsertion(void)
Definition: xloginsert.c:194
struct TransactionStateData * parent
Definition: xact.c:194
void AtAbort_Twophase(void)
Definition: twophase.c:317
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2147
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5948
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1380
void LockErrorCleanup(void)
Definition: proc.c:711
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3694
static void AtAbort_Memory(void)
Definition: xact.c:1726
void pgstat_progress_end_command(void)
Definition: pgstat.c:3286
void AtAbort_Notify(void)
Definition: async.c:1743
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2682
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5413
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:4172
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void LWLockReleaseAll(void)
Definition: lwlock.c:1911
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1838
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1222
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4064
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1614
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:513
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
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:1056
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define XLogLogicalInfoActive()
Definition: xlog.h:208
struct TransactionStateData * parent
Definition: xact.c:194
#define MinSizeOfXactAssignment
Definition: xact.h:198
FullTransactionId fullTransactionId
Definition: xact.c:174
#define IsParallelWorker()
Definition: parallel.h:61
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:324
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:148
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:583
#define XLogStandbyInfoActive()
Definition: xlog.h:205
static int nUnreportedXids
Definition: xact.c:232
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
#define Assert(condition)
Definition: c.h:738
static TransactionStateData TopTransactionStateData
Definition: xact.c:223
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1872
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
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:121

◆ AtAbort_Memory()

static void AtAbort_Memory ( void  )
static

Definition at line 1726 of file xact.c.

References MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

1727 {
1728  /*
1729  * Switch into TransactionAbortContext, which should have some free space
1730  * even if nothing else does. We'll work in this context until we've
1731  * finished cleaning up.
1732  *
1733  * It is barely possible to get here when we've not been able to create
1734  * TransactionAbortContext yet; if so use TopMemoryContext.
1735  */
1736  if (TransactionAbortContext != NULL)
1738  else
1740 }
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 1758 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

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

◆ AtCCI_LocalCache()

static void AtCCI_LocalCache ( void  )
static

Definition at line 1449 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

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

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

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1817 {
1819 
1820  /*
1821  * Now that we're "out" of a transaction, have the system allocate things
1822  * in the top memory context instead of per-transaction contexts.
1823  */
1825 
1826  /*
1827  * Clear the special abort context for next time.
1828  */
1829  if (TransactionAbortContext != NULL)
1831 
1832  /*
1833  * Release all transaction-local memory.
1834  */
1835  if (TopTransactionContext != NULL)
1837  TopTransactionContext = NULL;
1838  CurTransactionContext = NULL;
1840 }
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:738

◆ AtCommit_Memory()

static void AtCommit_Memory ( void  )
static

Definition at line 1468 of file xact.c.

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

Referenced by CommitTransaction(), and PrepareTransaction().

1469 {
1470  /*
1471  * Now that we're "out" of a transaction, have the system allocate things
1472  * in the top memory context instead of per-transaction contexts.
1473  */
1475 
1476  /*
1477  * Release all transaction-local memory.
1478  */
1479  Assert(TopTransactionContext != NULL);
1481  TopTransactionContext = NULL;
1482  CurTransactionContext = NULL;
1484 }
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:738

◆ AtStart_Cache()

static void AtStart_Cache ( void  )
static

Definition at line 1069 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

1070 {
1072 }
void AcceptInvalidationMessages(void)
Definition: inval.c:681

◆ AtStart_Memory()

static void AtStart_Memory ( void  )
static

Definition at line 1078 of file xact.c.

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

Referenced by StartTransaction().

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

◆ AtStart_ResourceOwner()

static void AtStart_ResourceOwner ( void  )
static

Definition at line 1125 of file xact.c.

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

Referenced by StartTransaction().

1126 {
1128 
1129  /*
1130  * We shouldn't have a transaction resource owner already.
1131  */
1133 
1134  /*
1135  * Create a toplevel resource owner for the transaction.
1136  */
1137  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1138 
1142 }
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:738
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ AtSubAbort_childXids()

static void AtSubAbort_childXids ( void  )
static

Definition at line 1784 of file xact.c.

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

Referenced by AbortSubTransaction().

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

◆ AtSubAbort_Memory()

static void AtSubAbort_Memory ( void  )
static

Definition at line 1746 of file xact.c.

References Assert, and MemoryContextSwitchTo().

Referenced by AbortSubTransaction().

1747 {
1749 
1751 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:275
#define Assert(condition)
Definition: c.h:738

◆ AtSubAbort_ResourceOwner()

static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1771 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1772 {
1774 
1775  /* Make sure we have a valid ResourceOwner */
1777 }
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 1852 of file xact.c.

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

Referenced by CleanupSubTransaction().

1853 {
1855 
1856  Assert(s->parent != NULL);
1857 
1858  /* Make sure we're not in an about-to-be-deleted context */
1861 
1862  /*
1863  * Clear the special abort context for next time.
1864  */
1865  if (TransactionAbortContext != NULL)
1867 
1868  /*
1869  * Delete the subxact local memory contexts. Its CurTransactionContext can
1870  * go too (note this also kills CurTransactionContexts from any children
1871  * of the subxact).
1872  */
1873  if (s->curTransactionContext)
1875  s->curTransactionContext = NULL;
1876 }
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:738

◆ AtSubCommit_childXids()

static void AtSubCommit_childXids ( void  )
static

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

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

◆ AtSubCommit_Memory()

static void AtSubCommit_Memory ( void  )
static

Definition at line 1495 of file xact.c.

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

Referenced by CommitSubTransaction().

1496 {
1498 
1499  Assert(s->parent != NULL);
1500 
1501  /* Return to parent transaction level's memory context. */
1504 
1505  /*
1506  * Ordinarily we cannot throw away the child's CurTransactionContext,
1507  * since the data it contains will be needed at upper commit. However, if
1508  * there isn't actually anything in it, we can throw it away. This avoids
1509  * a small memory leak in the common case of "trivial" subxacts.
1510  */
1512  {
1514  s->curTransactionContext = NULL;
1515  }
1516 }
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:738

◆ AtSubStart_Memory()

static void AtSubStart_Memory ( void  )
static

Definition at line 1153 of file xact.c.

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

Referenced by StartSubTransaction().

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

◆ AtSubStart_ResourceOwner()

static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1177 of file xact.c.

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

Referenced by StartSubTransaction().

1178 {
1180 
1181  Assert(s->parent != NULL);
1182 
1183  /*
1184  * Create a resource owner for the subtransaction. We make it a child of
1185  * the immediate parent's resource owner.
1186  */
1187  s->curTransactionOwner =
1189  "SubTransaction");
1190 
1193 }
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:738
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4010 of file xact.c.

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

Referenced by exec_simple_query().

4011 {
4013 
4014  /*
4015  * If we are in STARTED state (that is, no transaction block is open),
4016  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4017  * block.
4018  *
4019  * For caller convenience, we consider all other transaction states as
4020  * legal here; otherwise the caller would need its own state check, which
4021  * seems rather pointless.
4022  */
4023  if (s->blockState == TBLOCK_STARTED)
4025 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

4379 {
4381 
4382  /*
4383  * Workers synchronize transaction state at the beginning of each parallel
4384  * operation, so we can't account for new subtransactions after that
4385  * point. We might be able to make an exception for the type of
4386  * subtransaction established by this function, which is typically used in
4387  * contexts where we're going to release or roll back the subtransaction
4388  * before proceeding further, so that no enduring change to the
4389  * transaction state occurs. For now, however, we prohibit this case along
4390  * with all the others.
4391  */
4392  if (IsInParallelMode())
4393  ereport(ERROR,
4394  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4395  errmsg("cannot start subtransactions during a parallel operation")));
4396 
4397  switch (s->blockState)
4398  {
4399  case TBLOCK_STARTED:
4400  case TBLOCK_INPROGRESS:
4402  case TBLOCK_END:
4403  case TBLOCK_PREPARE:
4404  case TBLOCK_SUBINPROGRESS:
4405  /* Normal subtransaction start */
4406  PushTransaction();
4407  s = CurrentTransactionState; /* changed by push */
4408 
4409  /*
4410  * Savepoint names, like the TransactionState block itself, live
4411  * in TopTransactionContext.
4412  */
4413  if (name)
4415  break;
4416 
4417  /* These cases are invalid. */
4418  case TBLOCK_DEFAULT:
4419  case TBLOCK_BEGIN:
4421  case TBLOCK_SUBBEGIN:
4422  case TBLOCK_SUBRELEASE:
4423  case TBLOCK_SUBCOMMIT:
4424  case TBLOCK_ABORT:
4425  case TBLOCK_SUBABORT:
4426  case TBLOCK_ABORT_END:
4427  case TBLOCK_SUBABORT_END:
4428  case TBLOCK_ABORT_PENDING:
4430  case TBLOCK_SUBRESTART:
4432  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4434  break;
4435  }
4436 
4439 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void CommitTransactionCommand(void)
Definition: xact.c:2917
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5360
#define ereport(elevel,...)
Definition: elog.h:144
static void PushTransaction(void)
Definition: xact.c:5081
void StartTransactionCommand(void)
Definition: xact.c:2816
const char * name
Definition: encode.c:555
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:214

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

3609 {
3611 
3612  switch (s->blockState)
3613  {
3614  /*
3615  * We are not inside a transaction block, so allow one to begin.
3616  */
3617  case TBLOCK_STARTED:
3618  s->blockState = TBLOCK_BEGIN;
3619  break;
3620 
3621  /*
3622  * BEGIN converts an implicit transaction block to a regular one.
3623  * (Note that we allow this even if we've already done some
3624  * commands, which is a bit odd but matches historical practice.)
3625  */
3627  s->blockState = TBLOCK_BEGIN;
3628  break;
3629 
3630  /*
3631  * Already a transaction block in progress.
3632  */
3633  case TBLOCK_INPROGRESS:
3635  case TBLOCK_SUBINPROGRESS:
3636  case TBLOCK_ABORT:
3637  case TBLOCK_SUBABORT:
3638  ereport(WARNING,
3639  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3640  errmsg("there is already a transaction in progress")));
3641  break;
3642 
3643  /* These cases are invalid. */
3644  case TBLOCK_DEFAULT:
3645  case TBLOCK_BEGIN:
3646  case TBLOCK_SUBBEGIN:
3647  case TBLOCK_END:
3648  case TBLOCK_SUBRELEASE:
3649  case TBLOCK_SUBCOMMIT:
3650  case TBLOCK_ABORT_END:
3651  case TBLOCK_SUBABORT_END:
3652  case TBLOCK_ABORT_PENDING:
3654  case TBLOCK_SUBRESTART:
3656  case TBLOCK_PREPARE:
3657  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3659  break;
3660  }
3661 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5360
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

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

5361 {
5362  switch (blockState)
5363  {
5364  case TBLOCK_DEFAULT:
5365  return "DEFAULT";
5366  case TBLOCK_STARTED:
5367  return "STARTED";
5368  case TBLOCK_BEGIN:
5369  return "BEGIN";
5370  case TBLOCK_INPROGRESS:
5371  return "INPROGRESS";
5373  return "IMPLICIT_INPROGRESS";
5375  return "PARALLEL_INPROGRESS";
5376  case TBLOCK_END:
5377  return "END";
5378  case TBLOCK_ABORT:
5379  return "ABORT";
5380  case TBLOCK_ABORT_END:
5381  return "ABORT_END";
5382  case TBLOCK_ABORT_PENDING:
5383  return "ABORT_PENDING";
5384  case TBLOCK_PREPARE:
5385  return "PREPARE";
5386  case TBLOCK_SUBBEGIN:
5387  return "SUBBEGIN";
5388  case TBLOCK_SUBINPROGRESS:
5389  return "SUBINPROGRESS";
5390  case TBLOCK_SUBRELEASE:
5391  return "SUBRELEASE";
5392  case TBLOCK_SUBCOMMIT:
5393  return "SUBCOMMIT";
5394  case TBLOCK_SUBABORT:
5395  return "SUBABORT";
5396  case TBLOCK_SUBABORT_END:
5397  return "SUBABORT_END";
5399  return "SUBABORT_PENDING";
5400  case TBLOCK_SUBRESTART:
5401  return "SUBRESTART";
5403  return "SUBABORT_RESTART";
5404  }
5405  return "UNRECOGNIZED";
5406 }

◆ CallSubXactCallbacks()

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

Definition at line 3587 of file xact.c.

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

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

3590 {
3591  SubXactCallbackItem *item;
3592 
3593  for (item = SubXact_callbacks; item; item = item->next)
3594  item->callback(event, mySubid, parentSubid, item->arg);
3595 }
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 3532 of file xact.c.

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

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

3533 {
3534  XactCallbackItem *item;
3535 
3536  for (item = Xact_callbacks; item; item = item->next)
3537  item->callback(event, item->arg);
3538 }
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 3425 of file xact.c.

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3426 {
3427  /*
3428  * xact block already started?
3429  */
3430  if (IsTransactionBlock())
3431  return;
3432 
3433  /*
3434  * subtransaction?
3435  */
3436  if (IsSubTransaction())
3437  return;
3438 
3439  /*
3440  * inside a function call?
3441  */
3442  if (!isTopLevel)
3443  return;
3444 
3445  ereport(throwError ? ERROR : WARNING,
3446  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3447  /* translator: %s represents an SQL statement name */
3448  errmsg("%s can only be used in transaction blocks",
3449  stmtType)));
3450 }
int errcode(int sqlerrcode)
Definition: elog.c:610
bool IsTransactionBlock(void)
Definition: xact.c:4654
#define ERROR
Definition: elog.h:43
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
bool IsSubTransaction(void)
Definition: xact.c:4727
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

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

5049 {
5051 
5052  ShowTransactionState("CleanupSubTransaction");
5053 
5054  if (s->state != TRANS_ABORT)
5055  elog(WARNING, "CleanupSubTransaction while in %s state",
5057 
5059 
5062  if (s->curTransactionOwner)
5064  s->curTransactionOwner = NULL;
5065 
5067 
5068  s->state = TRANS_DEFAULT;
5069 
5070  PopTransaction();
5071 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void PopTransaction(void)
Definition: xact.c:5140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
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:5309
SubTransactionId subTransactionId
Definition: xact.c:175
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1083
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1852
static const char * TransStateAsString(TransState state)
Definition: xact.c:5413
#define elog(elevel,...)
Definition: elog.h:214

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

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

2768 {
2770 
2771  /*
2772  * State should still be TRANS_ABORT from AbortTransaction().
2773  */
2774  if (s->state != TRANS_ABORT)
2775  elog(FATAL, "CleanupTransaction: unexpected state %s",
2777 
2778  /*
2779  * do abort cleanup processing
2780  */
2781  AtCleanup_Portals(); /* now safe to release portal memory */
2782  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2783 
2784  CurrentResourceOwner = NULL; /* and resource owner */
2787  s->curTransactionOwner = NULL;
2790 
2791  AtCleanup_Memory(); /* and transaction memory */
2792 
2795  s->nestingLevel = 0;
2796  s->gucNestLevel = 0;
2797  s->childXids = NULL;
2798  s->nChildXids = 0;
2799  s->maxChildXids = 0;
2800  s->parallelModeLevel = 0;
2801 
2804 
2805  /*
2806  * done with abort processing, set current transaction state back to
2807  * default
2808  */
2809  s->state = TRANS_DEFAULT;
2810 }
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:712
static void AtCleanup_Memory(void)
Definition: xact.c:1816
int nParallelCurrentXids
Definition: xact.c:109
void AtCleanup_Portals(void)
Definition: portalmem.c:851
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:5413
#define InvalidSubTransactionId
Definition: c.h:519
#define InvalidFullTransactionId
Definition: transam.h:56
#define elog(elevel,...)
Definition: elog.h:214
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1006 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(), ATExecDropExpression(), ATExecDropIdentity(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), ATParseTransformCmd(), ATRewriteTables(), AttachPartitionEnsureIndexes(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTrigger(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRelation(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DropClonedTriggersFromPartition(), 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(), 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().

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

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

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

4788 {
4790 
4791  ShowTransactionState("CommitSubTransaction");
4792 
4793  if (s->state != TRANS_INPROGRESS)
4794  elog(WARNING, "CommitSubTransaction while in %s state",
4796 
4797  /* Pre-commit processing goes here */
4798 
4800  s->parent->subTransactionId);
4801 
4802  /* If in parallel mode, clean up workers and exit parallel mode. */
4803  if (IsInParallelMode())
4804  {
4806  s->parallelModeLevel = 0;
4807  }
4808 
4809  /* Do the actual "commit", such as it is */
4810  s->state = TRANS_COMMIT;
4811 
4812  /* Must CCI to ensure commands of subtransaction are seen as done */
4814 
4815  /*
4816  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4817  * perform that step, if required, as part of the atomic update of the
4818  * whole transaction tree at top level commit or abort.
4819  */
4820 
4821  /* Post-commit cleanup */
4824  AfterTriggerEndSubXact(true);
4829  s->parent->subTransactionId);
4831 
4833  s->parent->subTransactionId);
4834 
4837  true, false);
4839  s->parent->subTransactionId);
4840  AtEOSubXact_Inval(true);
4841  AtSubCommit_smgr();
4842 
4843  /*
4844  * The only lock we actually release here is the subtransaction XID lock.
4845  */
4849 
4850  /*
4851  * Other locks should get transferred to their parent resource owner.
4852  */
4855  true, false);
4858  true, false);
4859 
4860  AtEOXact_GUC(true, s->gucNestLevel);
4863  s->parent->subTransactionId);
4865  s->parent->subTransactionId);
4867  s->parent->subTransactionId);
4869  AtEOSubXact_PgStat(true, s->nestingLevel);
4872 
4873  /*
4874  * We need to restore the upper transaction's read-only state, in case the
4875  * upper is read-write while the child is read-only; GUC will incorrectly
4876  * think it should leave the child state in place.
4877  */
4879 
4883  s->curTransactionOwner = NULL;
4884 
4886 
4887  s->state = TRANS_DEFAULT;
4888 
4889  PopTransaction();
4890 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3206
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void AtSubCommit_Memory(void)
Definition: xact.c:1495
static void PopTransaction(void)
Definition: xact.c:5140
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1864
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
void AtSubCommit_smgr(void)
Definition: storage.c:878
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:1524
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
bool IsInParallelMode(void)
Definition: xact.c:997
struct TransactionStateData * parent
Definition: xact.c:194
void AtSubCommit_Notify(void)
Definition: async.c:1763
static void ShowTransactionState(const char *str)
Definition: xact.c:5309
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:874
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5948
SubTransactionId subTransactionId
Definition: xact.c:175
FullTransactionId fullTransactionId
Definition: xact.c:174
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2234
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:632
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:15430
void CommandCounterIncrement(void)
Definition: xact.c:1006
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2863
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3587
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:5413
bool prevXactReadOnly
Definition: xact.c:189
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4748
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1008
#define elog(elevel,...)
Definition: elog.h:214
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:600
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4132
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1203
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:936

◆ CommitTransaction()

static void CommitTransaction ( void  )
static

Definition at line 2046 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(), smgrDoPendingSyncs(), 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().

2047 {
2049  TransactionId latestXid;
2050  bool is_parallel_worker;
2051 
2052  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2053 
2054  /* Enforce parallel mode restrictions during parallel worker commit. */
2055  if (is_parallel_worker)
2057 
2058  ShowTransactionState("CommitTransaction");
2059 
2060  /*
2061  * check the current transaction state
2062  */
2063  if (s->state != TRANS_INPROGRESS)
2064  elog(WARNING, "CommitTransaction while in %s state",
2066  Assert(s->parent == NULL);
2067 
2068  /*
2069  * Do pre-commit processing that involves calling user-defined code, such
2070  * as triggers. Since closing cursors could queue trigger actions,
2071  * triggers could open cursors, etc, we have to keep looping until there's
2072  * nothing left to do.
2073  */
2074  for (;;)
2075  {
2076  /*
2077  * Fire all currently pending deferred triggers.
2078  */
2080 
2081  /*
2082  * Close open portals (converting holdable ones into static portals).
2083  * If there weren't any, we are done ... otherwise loop back to check
2084  * if they queued deferred triggers. Lather, rinse, repeat.
2085  */
2086  if (!PreCommit_Portals(false))
2087  break;
2088  }
2089 
2092 
2093  /*
2094  * The remaining actions cannot call any user-defined code, so it's safe
2095  * to start shutting down within-transaction services. But note that most
2096  * of this stuff could still throw an error, which would switch us into
2097  * the transaction-abort path.
2098  */
2099 
2100  /* If we might have parallel workers, clean them up now. */
2101  if (IsInParallelMode())
2102  AtEOXact_Parallel(true);
2103 
2104  /* Shut down the deferred-trigger manager */
2105  AfterTriggerEndXact(true);
2106 
2107  /*
2108  * Let ON COMMIT management do its thing (must happen after closing
2109  * cursors, to avoid dangling-reference problems)
2110  */
2112 
2113  /*
2114  * Synchronize files that are created and not WAL-logged during this
2115  * transaction. This must happen before AtEOXact_RelationMap(), so that we
2116  * don't see committed-but-broken files after a crash.
2117  */
2118  smgrDoPendingSyncs(true, is_parallel_worker);
2119 
2120  /* close large objects before lower-level cleanup */
2121  AtEOXact_LargeObject(true);
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. However, this could result in
2127  * creating a snapshot, so we must do it before serializable cleanup.
2128  */
2129  PreCommit_Notify();
2130 
2131  /*
2132  * Mark serializable transaction as complete for predicate locking
2133  * purposes. This should be done as late as we can put it and still allow
2134  * errors to be raised for failure patterns found at commit. This is not
2135  * appropriate in a parallel worker however, because we aren't committing
2136  * the leader's transaction and its serializable state will live on.
2137  */
2138  if (!is_parallel_worker)
2140 
2141  /* Prevent cancel/die interrupt while cleaning up */
2142  HOLD_INTERRUPTS();
2143 
2144  /* Commit updates to the relation map --- do this as late as possible */
2145  AtEOXact_RelationMap(true, is_parallel_worker);
2146 
2147  /*
2148  * set the current transaction state information appropriately during
2149  * commit processing
2150  */
2151  s->state = TRANS_COMMIT;
2152  s->parallelModeLevel = 0;
2153 
2154  if (!is_parallel_worker)
2155  {
2156  /*
2157  * We need to mark our XIDs as committed in pg_xact. This is where we
2158  * durably commit.
2159  */
2160  latestXid = RecordTransactionCommit();
2161  }
2162  else
2163  {
2164  /*
2165  * We must not mark our XID committed; the parallel master is
2166  * responsible for that.
2167  */
2168  latestXid = InvalidTransactionId;
2169 
2170  /*
2171  * Make sure the master will know about any WAL we wrote before it
2172  * commits.
2173  */
2175  }
2176 
2177  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2178 
2179  /*
2180  * Let others know about no transaction in progress by me. Note that this
2181  * must be done _before_ releasing locks we hold and _after_
2182  * RecordTransactionCommit.
2183  */
2184  ProcArrayEndTransaction(MyProc, latestXid);
2185 
2186  /*
2187  * This is all post-commit cleanup. Note that if an error is raised here,
2188  * it's too late to abort the transaction. This should be just
2189  * noncritical resource releasing.
2190  *
2191  * The ordering of operations is not entirely random. The idea is:
2192  * release resources visible to other backends (eg, files, buffer pins);
2193  * then release locks; then release backend-local resources. We want to
2194  * release locks at the point where any backend waiting for us will see
2195  * our transaction as being fully cleaned up.
2196  *
2197  * Resources that can be associated with individual queries are handled by
2198  * the ResourceOwner mechanism. The other calls here are for backend-wide
2199  * state.
2200  */
2201 
2202  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2203  : XACT_EVENT_COMMIT);
2204 
2207  true, true);
2208 
2209  /* Check we've released all buffer pins */
2210  AtEOXact_Buffers(true);
2211 
2212  /* Clean up the relation cache */
2213  AtEOXact_RelationCache(true);
2214 
2215  /*
2216  * Make catalog changes visible to all backends. This has to happen after
2217  * relcache references are dropped (see comments for
2218  * AtEOXact_RelationCache), but before locks are released (if anyone is
2219  * waiting for lock on a relation we've modified, we want them to know
2220  * about the catalog change before they start using the relation).
2221  */
2222  AtEOXact_Inval(true);
2223 
2225 
2228  true, true);
2231  true, true);
2232 
2233  /*
2234  * Likewise, dropping of files deleted during the transaction is best done
2235  * after releasing relcache and buffer pins. (This is not strictly
2236  * necessary during commit, since such pins should have been released
2237  * already, but this ordering is definitely critical during abort.) Since
2238  * this may take many seconds, also delay until after releasing locks.
2239  * Other backends will observe the attendant catalog changes and not
2240  * attempt to access affected files.
2241  */
2242  smgrDoPendingDeletes(true);
2243 
2244  AtCommit_Notify();
2245  AtEOXact_GUC(true, 1);
2246  AtEOXact_SPI(true);
2247  AtEOXact_Enum();
2249  AtEOXact_Namespace(true, is_parallel_worker);
2250  AtEOXact_SMgr();
2251  AtEOXact_Files(true);
2253  AtEOXact_HashTables(true);
2254  AtEOXact_PgStat(true, is_parallel_worker);
2255  AtEOXact_Snapshot(true, false);
2256  AtEOXact_ApplyLauncher(true);
2258 
2259  CurrentResourceOwner = NULL;
2261  s->curTransactionOwner = NULL;
2264 
2265  AtCommit_Memory();
2266 
2269  s->nestingLevel = 0;
2270  s->gucNestLevel = 0;
2271  s->childXids = NULL;
2272  s->nChildXids = 0;
2273  s->maxChildXids = 0;
2274 
2277 
2278  /*
2279  * done with commit processing, set current transaction state back to
2280  * default
2281  */
2282  s->state = TRANS_DEFAULT;
2283 
2285 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:832
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:595
static TransactionState CurrentTransactionState
Definition: xact.c:235
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:949
uint32 TransactionId
Definition: c.h:513
static void AtCommit_Memory(void)
Definition: xact.c:1468
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3338
XLogRecPtr XactLastRecEnd
Definition: xlog.c:363
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3061
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2443
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3532
int nParallelCurrentXids
Definition: xact.c:109
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15398
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:397
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1209
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2896
TBlockState blockState
Definition: xact.c:179
TransactionId * childXids
Definition: xact.c:184
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4759
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void PreCommit_Notify(void)
Definition: async.c:880
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
void AtEOXact_SMgr(void)
Definition: smgr.c:638
bool IsInParallelMode(void)
Definition: xact.c:997
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4652
struct TransactionStateData * parent
Definition: xact.c:194
static void ShowTransactionState(const char *str)
Definition: xact.c:5309
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2147
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5948
#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:1493
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4596
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5413
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:964
#define InvalidSubTransactionId
Definition: c.h:519
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:672
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:15298
#define InvalidFullTransactionId
Definition: transam.h:56
void AtCommit_Notify(void)
Definition: async.c:982
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1838
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1222
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4064
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091
LocalTransactionId lxid
Definition: proc.h:106

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

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

2918 {
2920 
2921  if (s->chain)
2923 
2924  switch (s->blockState)
2925  {
2926  /*
2927  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2928  * StartTransactionCommand didn't set the STARTED state
2929  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2930  * by EndParallelWorkerTransaction(), not this function.
2931  */
2932  case TBLOCK_DEFAULT:
2934  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2936  break;
2937 
2938  /*
2939  * If we aren't in a transaction block, just do our usual
2940  * transaction commit, and return to the idle state.
2941  */
2942  case TBLOCK_STARTED:
2945  break;
2946 
2947  /*
2948  * We are completing a "BEGIN TRANSACTION" command, so we change
2949  * to the "transaction block in progress" state and return. (We
2950  * assume the BEGIN did nothing to the database, so we need no
2951  * CommandCounterIncrement.)
2952  */
2953  case TBLOCK_BEGIN:
2955  break;
2956 
2957  /*
2958  * This is the case when we have finished executing a command
2959  * someplace within a transaction block. We increment the command
2960  * counter and return.
2961  */
2962  case TBLOCK_INPROGRESS:
2964  case TBLOCK_SUBINPROGRESS:
2966  break;
2967 
2968  /*
2969  * We are completing a "COMMIT" command. Do it and return to the
2970  * idle state.
2971  */
2972  case TBLOCK_END:
2975  if (s->chain)
2976  {
2977  StartTransaction();
2979  s->chain = false;
2981  }
2982  break;
2983 
2984  /*
2985  * Here we are in the middle of a transaction block but one of the
2986  * commands caused an abort so we do nothing but remain in the
2987  * abort state. Eventually we will get a ROLLBACK command.
2988  */
2989  case TBLOCK_ABORT:
2990  case TBLOCK_SUBABORT:
2991  break;
2992 
2993  /*
2994  * Here we were in an aborted transaction block and we just got
2995  * the ROLLBACK command from the user, so clean up the
2996  * already-aborted transaction and return to the idle state.
2997  */
2998  case TBLOCK_ABORT_END:
3001  if (s->chain)
3002  {
3003  StartTransaction();
3005  s->chain = false;
3007  }
3008  break;
3009 
3010  /*
3011  * Here we were in a perfectly good transaction block but the user
3012  * told us to ROLLBACK anyway. We have to abort the transaction
3013  * and then clean up.
3014  */
3015  case TBLOCK_ABORT_PENDING:
3016  AbortTransaction();
3019  if (s->chain)
3020  {
3021  StartTransaction();
3023  s->chain = false;
3025  }
3026  break;
3027 
3028  /*
3029  * We are completing a "PREPARE TRANSACTION" command. Do it and
3030  * return to the idle state.
3031  */
3032  case TBLOCK_PREPARE:
3035  break;
3036 
3037  /*
3038  * We were just issued a SAVEPOINT inside a transaction block.
3039  * Start a subtransaction. (DefineSavepoint already did
3040  * PushTransaction, so as to have someplace to put the SUBBEGIN
3041  * state.)
3042  */
3043  case TBLOCK_SUBBEGIN:
3046  break;
3047 
3048  /*
3049  * We were issued a RELEASE command, so we end the current
3050  * subtransaction and return to the parent transaction. The parent
3051  * might be ended too, so repeat till we find an INPROGRESS
3052  * transaction or subtransaction.
3053  */
3054  case TBLOCK_SUBRELEASE:
3055  do
3056  {
3058  s = CurrentTransactionState; /* changed by pop */
3059  } while (s->blockState == TBLOCK_SUBRELEASE);
3060 
3063  break;
3064 
3065  /*
3066  * We were issued a COMMIT, so we end the current subtransaction
3067  * hierarchy and perform final commit. We do this by rolling up
3068  * any subtransactions into their parent, which leads to O(N^2)
3069  * operations with respect to resource owners - this isn't that
3070  * bad until we approach a thousands of savepoints but is
3071  * necessary for correctness should after triggers create new
3072  * resource owners.
3073  */
3074  case TBLOCK_SUBCOMMIT:
3075  do
3076  {
3078  s = CurrentTransactionState; /* changed by pop */
3079  } while (s->blockState == TBLOCK_SUBCOMMIT);
3080  /* If we had a COMMIT command, finish off the main xact too */
3081  if (s->blockState == TBLOCK_END)
3082  {
3083  Assert(s->parent == NULL);
3086  }
3087  else if (s->blockState == TBLOCK_PREPARE)
3088  {
3089  Assert(s->parent == NULL);
3092  }
3093  else
3094  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3096  break;
3097 
3098  /*
3099  * The current already-failed subtransaction is ending due to a
3100  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3101  * examine the parent (which could be in any of several states).
3102  */
3103  case TBLOCK_SUBABORT_END:
3106  break;
3107 
3108  /*
3109  * As above, but it's not dead yet, so abort first.
3110  */
3115  break;
3116 
3117  /*
3118  * The current subtransaction is the target of a ROLLBACK TO
3119  * command. Abort and pop it, then start a new subtransaction
3120  * with the same name.
3121  */
3122  case TBLOCK_SUBRESTART:
3123  {
3124  char *name;
3125  int savepointLevel;
3126 
3127  /* save name and keep Cleanup from freeing it */
3128  name = s->name;
3129  s->name = NULL;
3130  savepointLevel = s->savepointLevel;
3131 
3134 
3135  DefineSavepoint(NULL);
3136  s = CurrentTransactionState; /* changed by push */
3137  s->name = name;
3138  s->savepointLevel = savepointLevel;
3139 
3140  /* This is the same as TBLOCK_SUBBEGIN case */
3144  }
3145  break;
3146 
3147  /*
3148  * Same as above, but the subtransaction had already failed, so we
3149  * don't need AbortSubTransaction.
3150  */
3152  {
3153  char *name;
3154  int savepointLevel;
3155 
3156  /* save name and keep Cleanup from freeing it */
3157  name = s->name;
3158  s->name = NULL;
3159  savepointLevel = s->savepointLevel;
3160 
3162 
3163  DefineSavepoint(NULL);
3164  s = CurrentTransactionState; /* changed by push */
3165  s->name = name;
3166  s->savepointLevel = savepointLevel;
3167 
3168  /* This is the same as TBLOCK_SUBBEGIN case */
3172  }
3173  break;
3174  }
3175 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AssertState(condition)
Definition: c.h:741
void CommitTransactionCommand(void)
Definition: xact.c:2917
void DefineSavepoint(const char *name)
Definition: xact.c:4057
TBlockState blockState
Definition: xact.c:179
static void PrepareTransaction(void)
Definition: xact.c:2294
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4787
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2767
struct TransactionStateData * parent
Definition: xact.c:194
void SaveTransactionCharacteristics(void)
Definition: xact.c:2897
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5360
static void AbortSubTransaction(void)
Definition: xact.c:4896
void CommandCounterIncrement(void)
Definition: xact.c:1006
static void AbortTransaction(void)
Definition: xact.c:2582
static void StartTransaction(void)
Definition: xact.c:1887
#define Assert(condition)
Definition: c.h:738
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2905
static void CleanupSubTransaction(void)
Definition: xact.c:5048
const char * name
Definition: encode.c:555
#define elog(elevel,...)
Definition: elog.h:214
static void CommitTransaction(void)
Definition: xact.c:2046
static void StartSubTransaction(void)
Definition: xact.c:4750

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

4058 {
4060 
4061  /*
4062  * Workers synchronize transaction state at the beginning of each parallel
4063  * operation, so we can't account for new subtransactions after that
4064  * point. (Note that this check will certainly error out if s->blockState
4065  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4066  * below.)
4067  */
4068  if (IsInParallelMode())
4069  ereport(ERROR,
4070  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4071  errmsg("cannot define savepoints during a parallel operation")));
4072 
4073  switch (s->blockState)
4074  {
4075  case TBLOCK_INPROGRESS:
4076  case TBLOCK_SUBINPROGRESS:
4077  /* Normal subtransaction start */
4078  PushTransaction();
4079  s = CurrentTransactionState; /* changed by push */
4080 
4081  /*
4082  * Savepoint names, like the TransactionState block itself, live
4083  * in TopTransactionContext.
4084  */
4085  if (name)
4087  break;
4088 
4089  /*
4090  * We disallow savepoint commands in implicit transaction blocks.
4091  * There would be no great difficulty in allowing them so far as
4092  * this module is concerned, but a savepoint seems inconsistent
4093  * with exec_simple_query's behavior of abandoning the whole query
4094  * string upon error. Also, the point of an implicit transaction
4095  * block (as opposed to a regular one) is to automatically close
4096  * after an error, so it's hard to see how a savepoint would fit
4097  * into that.
4098  *
4099  * The error messages for this are phrased as if there were no
4100  * active transaction block at all, which is historical but
4101  * perhaps could be improved.
4102  */
4104  ereport(ERROR,
4105  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4106  /* translator: %s represents an SQL statement name */
4107  errmsg("%s can only be used in transaction blocks",
4108  "SAVEPOINT")));
4109  break;
4110 
4111  /* These cases are invalid. */
4112  case TBLOCK_DEFAULT:
4113  case TBLOCK_STARTED:
4114  case TBLOCK_BEGIN:
4116  case TBLOCK_SUBBEGIN:
4117  case TBLOCK_END:
4118  case TBLOCK_SUBRELEASE:
4119  case TBLOCK_SUBCOMMIT:
4120  case TBLOCK_ABORT:
4121  case TBLOCK_SUBABORT:
4122  case TBLOCK_ABORT_END:
4123  case TBLOCK_SUBABORT_END:
4124  case TBLOCK_ABORT_PENDING:
4126  case TBLOCK_SUBRESTART:
4128  case TBLOCK_PREPARE:
4129  elog(FATAL, "DefineSavepoint: unexpected state %s",
4131  break;
4132  }
4133 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5360
#define ereport(elevel,...)
Definition: elog.h:144
static void PushTransaction(void)
Definition: xact.c:5081
const char * name
Definition: encode.c:555
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:214

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4035 of file xact.c.

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

Referenced by exec_simple_query().

4036 {
4038 
4039  /*
4040  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4041  * allowing CommitTransactionCommand to commit whatever happened during
4042  * the implicit transaction block as though it were a single statement.
4043  *
4044  * For caller convenience, we consider all other transaction states as
4045  * legal here; otherwise the caller would need its own state check, which
4046  * seems rather pointless.
4047  */
4050 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5297 of file xact.c.

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

Referenced by ParallelWorkerMain().

5298 {
5302 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
#define Assert(condition)
Definition: c.h:738
static void CommitTransaction(void)
Definition: xact.c:2046

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3728 of file xact.c.

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

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

3729 {
3731  bool result = false;
3732 
3733  switch (s->blockState)
3734  {
3735  /*
3736  * We are in a transaction block, so tell CommitTransactionCommand
3737  * to COMMIT.
3738  */
3739  case TBLOCK_INPROGRESS:
3740  s->blockState = TBLOCK_END;
3741  result = true;
3742  break;
3743 
3744  /*
3745  * We are in an implicit transaction block. If AND CHAIN was
3746  * specified, error. Otherwise commit, but issue a warning
3747  * because there was no explicit BEGIN before this.
3748  */
3750  if (chain)
3751  ereport(ERROR,
3752  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3753  /* translator: %s represents an SQL statement name */
3754  errmsg("%s can only be used in transaction blocks",
3755  "COMMIT AND CHAIN")));
3756  else
3757  ereport(WARNING,
3758  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3759  errmsg("there is no transaction in progress")));
3760  s->blockState = TBLOCK_END;
3761  result = true;
3762  break;
3763 
3764  /*
3765  * We are in a failed transaction block. Tell
3766  * CommitTransactionCommand it's time to exit the block.
3767  */
3768  case TBLOCK_ABORT:
3770  break;
3771 
3772  /*
3773  * We are in a live subtransaction block. Set up to subcommit all
3774  * open subtransactions and then commit the main transaction.
3775  */
3776  case TBLOCK_SUBINPROGRESS:
3777  while (s->parent != NULL)
3778  {
3779  if (s->blockState == TBLOCK_SUBINPROGRESS)
3781  else
3782  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3784  s = s->parent;
3785  }
3786  if (s->blockState == TBLOCK_INPROGRESS)
3787  s->blockState = TBLOCK_END;
3788  else
3789  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3791  result = true;
3792  break;
3793 
3794  /*
3795  * Here we are inside an aborted subtransaction. Treat the COMMIT
3796  * as ROLLBACK: set up to abort everything and exit the main
3797  * transaction.
3798  */
3799  case TBLOCK_SUBABORT:
3800  while (s->parent != NULL)
3801  {
3802  if (s->blockState == TBLOCK_SUBINPROGRESS)
3804  else if (s->blockState == TBLOCK_SUBABORT)
3806  else
3807  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3809  s = s->parent;
3810  }
3811  if (s->blockState == TBLOCK_INPROGRESS)
3813  else if (s->blockState == TBLOCK_ABORT)
3815  else
3816  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3818  break;
3819 
3820  /*
3821  * The user issued COMMIT when not inside a transaction. For
3822  * COMMIT without CHAIN, issue a WARNING, staying in
3823  * TBLOCK_STARTED state. The upcoming call to
3824  * CommitTransactionCommand() will then close the transaction and
3825  * put us back into the default state. For COMMIT AND CHAIN,
3826  * error.
3827  */
3828  case TBLOCK_STARTED:
3829  if (chain)
3830  ereport(ERROR,
3831  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3832  /* translator: %s represents an SQL statement name */
3833  errmsg("%s can only be used in transaction blocks",
3834  "COMMIT AND CHAIN")));
3835  else
3836  ereport(WARNING,
3837  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3838  errmsg("there is no transaction in progress")));
3839  result = true;
3840  break;
3841 
3842  /*
3843  * The user issued a COMMIT that somehow ran inside a parallel
3844  * worker. We can't cope with that.
3845  */
3847  ereport(FATAL,
3848  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3849  errmsg("cannot commit during a parallel operation")));
3850  break;
3851 
3852  /* These cases are invalid. */
3853  case TBLOCK_DEFAULT:
3854  case TBLOCK_BEGIN:
3855  case TBLOCK_SUBBEGIN:
3856  case TBLOCK_END:
3857  case TBLOCK_SUBRELEASE:
3858  case TBLOCK_SUBCOMMIT:
3859  case TBLOCK_ABORT_END:
3860  case TBLOCK_SUBABORT_END:
3861  case TBLOCK_ABORT_PENDING:
3863  case TBLOCK_SUBRESTART:
3865  case TBLOCK_PREPARE:
3866  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3868  break;
3869  }
3870 
3872  s->blockState == TBLOCK_END ||
3873  s->blockState == TBLOCK_ABORT_END ||
3875 
3876  s->chain = chain;
3877 
3878  return result;
3879 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5360
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 964 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

965 {
967 
968  Assert(s->parallelModeLevel >= 0);
969 
970  ++s->parallelModeLevel;
971 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5174 of file xact.c.

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

Referenced by InitializeParallelDSM().

5175 {
5176  TransactionState s;
5177  Size nxids = 0;
5179 
5180  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5181  {
5183  nxids = add_size(nxids, 1);
5184  nxids = add_size(nxids, s->nChildXids);
5185  }
5186 
5187  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5188 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
uint32 TransactionId
Definition: c.h:513
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
struct TransactionStateData * parent
Definition: xact.c:194
FullTransactionId fullTransactionId
Definition: xact.c:174
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define SerializedTransactionStateHeaderSize
Definition: xact.c:215
size_t Size
Definition: c.h:466

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 977 of file xact.c.

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

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

978 {
980 
981  Assert(s->parallelModeLevel > 0);
983 
984  --s->parallelModeLevel;
985 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
bool ParallelContextActive(void)
Definition: parallel.c:978
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1054 of file xact.c.

References forceSyncCommit.

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

1055 {
1056  forceSyncCommit = true;
1057 }
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:61
#define Assert(condition)
Definition: c.h:738
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:55
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:1574

◆ 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:513
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:515
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
#define Assert(condition)
Definition: c.h:738
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:258
#define InvalidLocalTransactionId
Definition: lock.h:68
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:106

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 452 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

453 {
457 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
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.

Referenced by pg_current_xact_id_if_assigned().

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(), and nextval_internal().

396 {
400 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
#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 3463 of file xact.c.

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

Referenced by vacuum().

3464 {
3465  /*
3466  * Return true on same conditions that would make
3467  * PreventInTransactionBlock error out
3468  */
3469  if (IsTransactionBlock())
3470  return true;
3471 
3472  if (IsSubTransaction())
3473  return true;
3474 
3475  if (!isTopLevel)
3476  return true;
3477 
3480  return true;
3481 
3482  return false;
3483 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
bool IsTransactionBlock(void)
Definition: xact.c:4654
bool IsSubTransaction(void)
Definition: xact.c:4727

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

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

4655 {
4657 
4659  return false;
4660 
4661  return true;
4662 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()